1 // Author: Ilya Khramov
2 // Copyright (c) 2019 OPEN CASCADE SAS
4 // This file is part of Open CASCADE Technology software library.
6 // This library is free software; you can redistribute it and/or modify it under
7 // the terms of the GNU Lesser General Public License version 2.1 as published
8 // by the Free Software Foundation, with special exception defined in the file
9 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
10 // distribution for complete text of the license and disclaimer of any warranty.
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
15 #include <Graphic3d_CubeMapPacked.hxx>
17 #include <Image_AlienPixMap.hxx>
18 #include <Image_DDSParser.hxx>
20 IMPLEMENT_STANDARD_RTTIEXT(Graphic3d_CubeMapPacked, Graphic3d_CubeMap)
22 // =======================================================================
23 // function : Graphic3d_CubeMapPacked
25 // =======================================================================
26 Graphic3d_CubeMapPacked::Graphic3d_CubeMapPacked (const TCollection_AsciiString& theFilePath,
27 const Graphic3d_ValidatedCubeMapOrder theOrder)
29 Graphic3d_CubeMap (theFilePath),
34 // =======================================================================
35 // function : Graphic3d_CubeMapPacked
37 // =======================================================================
38 Graphic3d_CubeMapPacked::Graphic3d_CubeMapPacked (const Handle(Image_PixMap)& theImage,
39 const Graphic3d_ValidatedCubeMapOrder theOrder)
41 Graphic3d_CubeMap (Handle(Image_PixMap)()),
45 if (checkImage (theImage, myTileNumberX))
51 // =======================================================================
52 // function : CompressedValue
54 // =======================================================================
55 Handle(Image_CompressedPixMap) Graphic3d_CubeMapPacked::CompressedValue (const Handle(Image_SupportedFormats)& theSupported)
57 if (myTileNumberX == 0
58 || !myPixMap.IsNull())
60 return Handle(Image_CompressedPixMap)();
63 TCollection_AsciiString aFilePath;
64 myPath.SystemName (aFilePath);
65 if (!aFilePath.IsEmpty())
67 const unsigned int aTileIndex = myOrder[myCurrentSide];
68 Handle(Image_CompressedPixMap) anImage = Image_DDSParser::Load (theSupported, aFilePath, (Standard_Integer )aTileIndex);
70 && anImage->NbFaces() == 6
71 && anImage->SizeX() == anImage->SizeY())
73 myIsTopDown = anImage->IsTopDown();
77 return Handle(Image_CompressedPixMap)();
80 // =======================================================================
83 // =======================================================================
84 Handle(Image_PixMap) Graphic3d_CubeMapPacked::Value (const Handle(Image_SupportedFormats)& theSupported)
86 if (myTileNumberX != 0)
88 if (myPixMap.IsNull())
90 TCollection_AsciiString aFilePath;
91 myPath.SystemName (aFilePath);
92 if (!aFilePath.IsEmpty())
94 tryLoadImage (theSupported, aFilePath);
98 if (!myPixMap.IsNull())
100 Handle(Image_PixMap) aWrapper = new Image_PixMap();
102 Standard_Size aTileSize = myPixMap->SizeX() / myTileNumberX;
104 myIsTopDown = myPixMap->IsTopDown();
106 Graphic3d_CubeMapOrder anOrder = myOrder;
110 myPixMap->SetTopDown (true);
111 anOrder.Swap (Graphic3d_CMS_POS_Y, Graphic3d_CMS_NEG_Y);
114 unsigned int aTileIndexX = anOrder[myCurrentSide] % myTileNumberX;
115 unsigned int aTileIndexY = anOrder[myCurrentSide] / myTileNumberX;
117 aTileIndexY = myIsTopDown ? aTileIndexY : (6 / myTileNumberX - 1 - aTileIndexY);
119 if (aWrapper->InitWrapper (myPixMap->Format(),
120 myPixMap->ChangeRawValue(aTileIndexY * aTileSize, aTileIndexX * aTileSize),
123 myPixMap->SizeRowBytes()))
125 myPixMap->SetTopDown (myIsTopDown);
130 myPixMap->SetTopDown(myIsTopDown);
135 return Handle(Image_PixMap)();
138 // =======================================================================
139 // function : checkOrder
141 // =======================================================================
142 Standard_Boolean Graphic3d_CubeMapPacked::checkOrder (const NCollection_Array1<unsigned int>& theOrder)
144 Standard_Boolean anOrderIsValid = Standard_True;
146 if (theOrder.Size() != 6)
148 anOrderIsValid = Standard_False;
152 for (unsigned int i = 0; i < 6 && anOrderIsValid; ++i)
156 anOrderIsValid = Standard_False;
160 for (unsigned int j = i + 1; j < 6; ++j)
162 if (theOrder[i] == theOrder[j])
164 anOrderIsValid = Standard_False;
173 throw Standard_Failure ("Ivalid order format in tiles of Graphic3d_CubeMapPacked");
176 return anOrderIsValid;
179 // =======================================================================
180 // function : checkImage
182 // =======================================================================
183 Standard_Boolean Graphic3d_CubeMapPacked::checkImage (const Handle(Image_PixMap)& theImage,
184 unsigned int& theTileNumberX)
186 Standard_Size aSizeX = theImage->SizeX();
187 Standard_Size aSizeY = theImage->SizeY();
189 if ((aSizeY % aSizeX == 0) && (aSizeY / aSizeX == 6))
193 else if ((aSizeX % aSizeY == 0) && (aSizeX / aSizeY == 6))
197 else if ((aSizeX % 2 == 0) && (aSizeY % 3 == 0) && (aSizeX / 2 == aSizeY / 3))
201 else if ((aSizeX % 3 == 0) && (aSizeY % 2 == 0) && (aSizeX / 3 == aSizeY / 2))
207 return Standard_False;
210 return Standard_True;
213 // =======================================================================
214 // function : tryLoadImage
216 // =======================================================================
217 void Graphic3d_CubeMapPacked::tryLoadImage (const Handle(Image_SupportedFormats)& theSupported,
218 const TCollection_AsciiString& theFilePath)
220 Handle(Image_AlienPixMap) anImage = new Image_AlienPixMap;
221 if (anImage->Load (theFilePath))
223 if (checkImage (anImage, myTileNumberX))
225 convertToCompatible (theSupported, anImage);