changed: gcc8 base update
[opensg.git] / Source / System / Image / OSGImageFunctions.inl
blob6f99cf9928dee4b9089c715d93dbfbe1c7eda7a8
1 /*---------------------------------------------------------------------------*\
2  *                                OpenSG                                     *
3  *                                                                           *
4  *                                                                           *
5  *             Copyright (C) 2000-2002 by the OpenSG Forum                   *
6  *                                                                           *
7  *                            www.opensg.org                                 *
8  *                                                                           *
9  *   contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de          *
10  *                                                                           *
11 \*---------------------------------------------------------------------------*/
13 /*---------------------------------------------------------------------------*\
14  *                                License                                    *
15  *                                                                           *
16  * This library is free software; you can redistribute it and/or modify it   *
17  * under the terms of the GNU Library General Public License as published    *
18  * by the Free Software Foundation, version 2.                               *
19  *                                                                           *
20  * This library is distributed in the hope that it will be useful, but       *
21  * WITHOUT ANY WARRANTY; without even the implied warranty of                *
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU         *
23  * Library General Public License for more details.                          *
24  *                                                                           *
25  * You should have received a copy of the GNU Library General Public         *
26  * License along with this library; if not, write to the Free Software       *
27  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.                 *
28  *                                                                           *
29 \*---------------------------------------------------------------------------*/
31 /*---------------------------------------------------------------------------*\
32  *                                Changes                                    *
33  *                                                                           *
34  *                                                                           *
35  *                                                                           *
36  *                                                                           *
37  *                                                                           *
38  *                                                                           *
39 \*---------------------------------------------------------------------------*/
41 OSG_BEGIN_NAMESPACE
43 template<class SrcImageStore> inline
44 bool createComposedImage (const SrcImageStore &imageVec,
45                                 Image         *image,
46                                 SliceDataType  sliceDataType,
47                                 bool           flipY        )
49     UInt32 dataSize, i, n = imageVec.size();
50     Int32 w = 0;
51     Int32 h = 0;
52     UInt8 *destData;
53     const UInt8 *srcData;
54     Image::PixelFormat pf = Image::OSG_INVALID_PF;
55     Image::Type        dt = Image::OSG_INVALID_IMAGEDATATYPE;
56     bool needColor = false, needAlpha = false, needCopy = false;
57     ImageUnrecPtr copy = Image::create();
58     UInt32 depth, frameCount, sideCount;
60     if (n) 
61     {
62         for (i = 0; i < n; i++) 
63         {
64             if ( i == 0 ) 
65             {
66                 pf = Image::PixelFormat(imageVec[0]->getPixelFormat());
67                 dt = Image::Type(imageVec[0]->getDataType());
68                 w = imageVec[0]->getWidth();
69                 h = imageVec[0]->getHeight();
70                 needAlpha = imageVec[0]->hasAlphaChannel();
71                 needColor = imageVec[0]->hasColorChannel();
72             }
73             else 
74             {
75                 needAlpha |= imageVec[i]->hasAlphaChannel();
76                 needColor |= imageVec[i]->hasColorChannel();
77                 if (Image::PixelFormat(imageVec[i]->getPixelFormat()) != pf) 
78                 {
79                     needCopy = true;          
80                     FWARNING (( "Image has different PF while composing\n" ));
81                     pf = Image::OSG_INVALID_PF;
82                 }
83                 if (Image::Type(imageVec[i]->getDataType()) != dt) 
84                 {
85                     needCopy = true;          
86                     FWARNING (( "Image has different DT while composing\n" ));
87                     dt = Image::OSG_INVALID_IMAGEDATATYPE;
88                 }
89                 if (imageVec[i]->getWidth() != w) 
90                 {
91                     needCopy = true;          
92                     FWARNING(("Image has different width while composing\n"));
93                     w = osgMax ( w, imageVec[i]->getWidth());
94                 }
95                 if (imageVec[i]->getHeight() != h) 
96                 {
97                     needCopy = true;          
98                     FWARNING(("Image has different height while composing\n"));
99                     h = osgMax ( h, imageVec[i]->getHeight());
100                 }
101             }      
102         }
104         if (pf == Image::OSG_INVALID_PF) 
105         {
106             if (needColor)
107                 if (needAlpha)
108                     pf = OSG::Image::OSG_RGBA_PF;
109                 else
110                     pf = OSG::Image::OSG_RGB_PF;
111             else
112                 if (needAlpha)
113                     pf = OSG::Image::OSG_LA_PF;
114                 else
115                     pf = OSG::Image::OSG_L_PF;
116         }
118         if (dt == Image::OSG_INVALID_IMAGEDATATYPE)
119             dt = Image::OSG_UINT8_IMAGEDATA;
121         depth = frameCount = sideCount = 1;
122         switch (sliceDataType) 
123         {
124             case FRAME_SDT:
125                 frameCount = n;
126                 break;
127             case SIDE_SDT:
128                 sideCount = n;
129                 break;
130             case INVALID_SDT:
131             case DEPTH_SDT:
132             default:
133                 depth = n;
134                 break;
135         }
136       
137         image->set( pf, w, h, depth, 1, frameCount, 0.0, 
138                     0, dt, true, sideCount );
140         destData   = image->editData();
141         dataSize   = image->getSize() / n;
142         UInt32 bpl = dataSize / h;
145         if (needCopy) 
146         {
147             FLOG (("Image data/type/size missmatch while composing\n"));
148         }
150         for (i = 0; i < n; i++) 
151         {
152             if (needCopy) 
153             {
154                 copy->set(imageVec[i]);
155                 if ( Image::PixelFormat(copy->getPixelFormat()) != pf )
156                     copy->reformat(pf);
157                 if ( Image::Type(copy->getDataType()) != dt )
158                     copy->convertDataTypeTo(dt);
159                 if ( (w != copy->getWidth()) || (h != copy->getHeight()))
160                     copy->scale(w,h,copy->getDepth());
161                 srcData = copy->getData();
162             }
163             else 
164             {
165                 srcData = imageVec[i]->getData();
166             }
168             if (flipY == true)
169             {
170                 srcData += dataSize;
171                 for(Int32 y = 0; y < h; ++y)
172                 {
173                     srcData -= bpl;
174                     memcpy ( destData, srcData, bpl );
175                     destData += bpl;
176                 }
177             }
178             else
179             {
180                 memcpy ( destData, srcData, dataSize );
181                 destData += dataSize;
182             }
183         }
184     }
186     imageVec[0]->dump();
187     image->dump();
189     return true;
192 template<class ValueT> inline
193 void swapImageByteOrder(Image * const pImage)
195     if(pImage == NULL)
196         return;
198     void   *pData  = static_cast<void *>(pImage->editData());
199     UInt32  uiSize = pImage->getSize() / sizeof(ValueT);
201     osgSwapMem<sizeof(ValueT)>(pData, uiSize);
204 template<class ValueT, ValueT (*ConvF)(ValueT)> inline
205 void swapAndConvertImageByteOrder(Image * const pImage)
207     if(pImage == NULL)
208         return;
210     ValueT *pData  = reinterpret_cast<ValueT *>(pImage->editData());
211     UInt32  uiSize = pImage->getSize() / sizeof(ValueT);
213     for(UInt32 i = 0; i < uiSize; ++i)
214     {
215         osgSwapMem<sizeof(ValueT)>(&pData[i], 1);
216         pData[i] = ConvF(pData[i]);
217     }
220 template<class ValueT, ValueT MinVal> inline
221 ValueT clampMin(ValueT val)
223     if(val < MinVal)
224         val = MinVal;
226     return val;
229 template<class ValueT, ValueT CompVal, ValueT ReplaceVal> inline
230 ValueT clampMin(ValueT val)
232     if(val < CompVal)
233         val = ReplaceVal;
235     return val;
238 template<class ValueT> inline
239 ValueT doNothing(ValueT val)
241     return val;
244 OSG_END_NAMESPACE