1 // Created on: 2010-09-16
3 // Copyright (c) 2010-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
17 #include <FreeImage.h>
20 #pragma comment( lib, "FreeImage.lib" )
24 #include <Image_AlienPixMap.hxx>
26 #include <TCollection_AsciiString.hxx>
33 static Image_PixMap::ImgFormat convertFromFreeFormat (FREE_IMAGE_TYPE theFormatFI,
34 FREE_IMAGE_COLOR_TYPE theColorTypeFI,
35 unsigned theBitsPerPixel)
39 case FIT_RGBF: return Image_PixMap::ImgRGBF;
40 case FIT_RGBAF: return Image_PixMap::ImgRGBAF;
41 case FIT_FLOAT: return Image_PixMap::ImgGrayF;
44 switch (theColorTypeFI)
48 return Image_PixMap::ImgGray;
52 if (Image_PixMap::IsBigEndianHost())
54 return (theBitsPerPixel == 32) ? Image_PixMap::ImgRGB32 : Image_PixMap::ImgRGB;
58 return (theBitsPerPixel == 32) ? Image_PixMap::ImgBGR32 : Image_PixMap::ImgBGR;
63 return Image_PixMap::IsBigEndianHost() ? Image_PixMap::ImgRGBA : Image_PixMap::ImgBGRA;
66 return Image_PixMap::ImgUNKNOWN;
70 return Image_PixMap::ImgUNKNOWN;
74 static FREE_IMAGE_TYPE convertToFreeFormat (Image_PixMap::ImgFormat theFormat)
78 case Image_PixMap::ImgGrayF:
80 case Image_PixMap::ImgRGBAF:
82 case Image_PixMap::ImgRGBF:
84 case Image_PixMap::ImgRGBA:
85 case Image_PixMap::ImgBGRA:
86 case Image_PixMap::ImgRGB32:
87 case Image_PixMap::ImgBGR32:
88 case Image_PixMap::ImgRGB:
89 case Image_PixMap::ImgBGR:
90 case Image_PixMap::ImgGray:
99 IMPLEMENT_STANDARD_HANDLE (Image_AlienPixMap, Image_PixMap)
100 IMPLEMENT_STANDARD_RTTIEXT(Image_AlienPixMap, Image_PixMap)
102 // =======================================================================
103 // function : Image_AlienPixMap
105 // =======================================================================
106 Image_AlienPixMap::Image_AlienPixMap()
112 // =======================================================================
113 // function : ~Image_AlienPixMap
115 // =======================================================================
116 Image_AlienPixMap::~Image_AlienPixMap()
121 // =======================================================================
122 // function : InitWrapper
124 // =======================================================================
125 bool Image_AlienPixMap::InitWrapper (ImgFormat,
135 // =======================================================================
136 // function : InitTrash
138 // =======================================================================
139 #ifdef HAVE_FREEIMAGE
140 bool Image_AlienPixMap::InitTrash (ImgFormat thePixelFormat,
141 const Standard_Size theSizeX,
142 const Standard_Size theSizeY,
143 const Standard_Size /*theSizeRowBytes*/)
146 FREE_IMAGE_TYPE aFormatFI = convertToFreeFormat (thePixelFormat);
147 int aBitsPerPixel = (int )Image_PixMap::SizePixelBytes (thePixelFormat) * 8;
148 if (aFormatFI == FIT_UNKNOWN)
150 aFormatFI = FIT_BITMAP;
154 FIBITMAP* anImage = FreeImage_AllocateT (aFormatFI, (int )theSizeX, (int )theSizeY, aBitsPerPixel);
155 Image_PixMap::ImgFormat aFormat = convertFromFreeFormat (FreeImage_GetImageType (anImage),
156 FreeImage_GetColorType (anImage),
157 FreeImage_GetBPP (anImage));
158 if (thePixelFormat == Image_PixMap::ImgBGR32
159 || thePixelFormat == Image_PixMap::ImgRGB32)
161 //FreeImage_SetTransparent (anImage, FALSE);
162 aFormat = (aFormat == Image_PixMap::ImgBGRA) ? Image_PixMap::ImgBGR32 : Image_PixMap::ImgRGB32;
165 Image_PixMap::InitWrapper (aFormat, FreeImage_GetBits (anImage),
166 FreeImage_GetWidth (anImage), FreeImage_GetHeight (anImage), FreeImage_GetPitch (anImage));
169 // assign image after wrapper initialization (virtual Clear() called inside)
170 myLibImage = anImage;
174 bool Image_AlienPixMap::InitTrash (ImgFormat thePixelFormat,
175 const Standard_Size theSizeX,
176 const Standard_Size theSizeY,
177 const Standard_Size theSizeRowBytes)
179 return Image_PixMap::InitTrash (thePixelFormat, theSizeX, theSizeY, theSizeRowBytes);
183 // =======================================================================
184 // function : InitCopy
186 // =======================================================================
187 bool Image_AlienPixMap::InitCopy (const Image_PixMap& theCopy)
189 if (&theCopy == this)
191 // self-copying disallowed
194 if (!InitTrash (theCopy.Format(), theCopy.SizeX(), theCopy.SizeY(), theCopy.SizeRowBytes()))
199 if (myImgFormat == theCopy.Format())
201 if (SizeRowBytes() == theCopy.SizeRowBytes()
202 && TopDownInc() == theCopy.TopDownInc())
204 // copy with one call
205 memcpy (ChangeData(), theCopy.Data(), std::min (SizeBytes(), theCopy.SizeBytes()));
210 const Standard_Size aRowSizeBytes = std::min (SizeRowBytes(), theCopy.SizeRowBytes());
211 for (Standard_Size aRow = 0; aRow < myData.SizeY; ++aRow)
213 memcpy (ChangeRow (aRow), theCopy.Row (aRow), aRowSizeBytes);
218 // pixel format conversion required
223 // =======================================================================
226 // =======================================================================
227 void Image_AlienPixMap::Clear()
229 Image_PixMap::Clear();
230 #ifdef HAVE_FREEIMAGE
231 if (myLibImage != NULL)
233 FreeImage_Unload (myLibImage);
239 // =======================================================================
242 // =======================================================================
243 #ifdef HAVE_FREEIMAGE
244 bool Image_AlienPixMap::Load (const TCollection_AsciiString& theImagePath)
247 FREE_IMAGE_FORMAT aFIF = FreeImage_GetFileType (theImagePath.ToCString(), 0);
248 if (aFIF == FIF_UNKNOWN)
250 // no signature? try to guess the file format from the file extension
251 aFIF = FreeImage_GetFIFFromFilename (theImagePath.ToCString());
253 if ((aFIF == FIF_UNKNOWN) || !FreeImage_FIFSupportsReading (aFIF))
255 // unsupported image format
262 // 'Play' the GIF to generate each frame (as 32bpp) instead of returning raw frame data when loading
263 aLoadFlags = GIF_PLAYBACK;
265 else if (aFIF == FIF_ICO)
267 // convert to 32bpp and create an alpha channel from the AND-mask when loading
268 aLoadFlags = ICO_MAKEALPHA;
271 FIBITMAP* anImage = FreeImage_Load (aFIF, theImagePath.ToCString(), aLoadFlags);
277 Image_PixMap::ImgFormat aFormat = convertFromFreeFormat (FreeImage_GetImageType (anImage),
278 FreeImage_GetColorType (anImage),
279 FreeImage_GetBPP (anImage));
280 if (aFormat == Image_PixMap::ImgUNKNOWN)
282 //anImage = FreeImage_ConvertTo24Bits (anImage);
286 Image_PixMap::InitWrapper (aFormat, FreeImage_GetBits (anImage),
287 FreeImage_GetWidth (anImage), FreeImage_GetHeight (anImage), FreeImage_GetPitch (anImage));
290 // assign image after wrapper initialization (virtual Clear() called inside)
291 myLibImage = anImage;
295 bool Image_AlienPixMap::Load (const TCollection_AsciiString&)
302 // =======================================================================
303 // function : savePPM
305 // =======================================================================
306 bool Image_AlienPixMap::savePPM (const TCollection_AsciiString& theFileName) const
314 FILE* aFile = fopen (theFileName.ToCString(), "wb");
321 fprintf (aFile, "P6\n%d %d\n255\n", (int )SizeX(), (int )SizeY());
322 fprintf (aFile, "# Image stored by OpenCASCADE framework in linear RGB colorspace\n");
325 Quantity_Color aColor;
326 Quantity_Parameter aDummy;
328 for (Standard_Size aRow = 0; aRow < SizeY(); ++aRow)
330 for (Standard_Size aCol = 0; aCol < SizeX(); ++aCol)
332 // extremely SLOW but universal (implemented for all supported pixel formats)
333 aColor = PixelColor ((Standard_Integer )aCol, (Standard_Integer )aRow, aDummy);
334 aByte = Standard_Byte(aColor.Red() * 255.0); fwrite (&aByte, 1, 1, aFile);
335 aByte = Standard_Byte(aColor.Green() * 255.0); fwrite (&aByte, 1, 1, aFile);
336 aByte = Standard_Byte(aColor.Blue() * 255.0); fwrite (&aByte, 1, 1, aFile);
345 // =======================================================================
348 // =======================================================================
349 bool Image_AlienPixMap::Save (const TCollection_AsciiString& theFileName)
351 #ifdef HAVE_FREEIMAGE
352 if (myLibImage == NULL)
357 FREE_IMAGE_FORMAT anImageFormat = FreeImage_GetFIFFromFilename (theFileName.ToCString());
358 if (anImageFormat == FIF_UNKNOWN)
360 std::cerr << "Image_PixMap, image format doesn't supported!\n";
366 FreeImage_FlipVertical (myLibImage);
370 // FreeImage doesn't provide flexible format convertion API
371 // so we should perform multiple convertions in some cases!
372 FIBITMAP* anImageToDump = myLibImage;
373 switch (anImageFormat)
378 if (Format() == Image_PixMap::ImgBGR32
379 || Format() == Image_PixMap::ImgRGB32)
381 // stupid FreeImage treats reserved byte as alpha if some bytes not set to 0xFF
382 for (Standard_Size aRow = 0; aRow < SizeY(); ++aRow)
384 for (Standard_Size aCol = 0; aCol < SizeX(); ++aCol)
386 myData.ChangeValue (aRow, aCol)[3] = 0xFF;
390 else if (FreeImage_GetImageType (myLibImage) != FIT_BITMAP)
392 anImageToDump = FreeImage_ConvertToType (myLibImage, FIT_BITMAP);
398 FIBITMAP* aTmpBitmap = myLibImage;
399 if (FreeImage_GetImageType (myLibImage) != FIT_BITMAP)
401 aTmpBitmap = FreeImage_ConvertToType (myLibImage, FIT_BITMAP);
402 if (aTmpBitmap == NULL)
408 if (FreeImage_GetBPP (aTmpBitmap) != 24)
410 FIBITMAP* aTmpBitmap24 = FreeImage_ConvertTo24Bits (aTmpBitmap);
411 if (aTmpBitmap != myLibImage)
413 FreeImage_Unload (aTmpBitmap);
415 if (aTmpBitmap24 == NULL)
419 aTmpBitmap = aTmpBitmap24;
422 // need convertion to image with pallete (requires 24bit bitmap)
423 anImageToDump = FreeImage_ColorQuantize (aTmpBitmap, FIQ_NNQUANT);
424 if (aTmpBitmap != myLibImage)
426 FreeImage_Unload (aTmpBitmap);
432 if (Format() == Image_PixMap::ImgGray)
434 anImageToDump = FreeImage_ConvertToType (myLibImage, FIT_FLOAT);
436 else if (Format() == Image_PixMap::ImgRGBA
437 || Format() == Image_PixMap::ImgBGRA)
439 anImageToDump = FreeImage_ConvertToType (myLibImage, FIT_RGBAF);
443 FREE_IMAGE_TYPE aImgTypeFI = FreeImage_GetImageType (myLibImage);
444 if (aImgTypeFI != FIT_RGBF
445 && aImgTypeFI != FIT_RGBAF
446 && aImgTypeFI != FIT_FLOAT)
448 anImageToDump = FreeImage_ConvertToType (myLibImage, FIT_RGBF);
455 if (FreeImage_GetImageType (myLibImage) != FIT_BITMAP)
457 anImageToDump = FreeImage_ConvertToType (myLibImage, FIT_BITMAP);
458 if (anImageToDump == NULL)
464 if (FreeImage_GetBPP (anImageToDump) != 24)
466 FIBITMAP* aTmpBitmap24 = FreeImage_ConvertTo24Bits (anImageToDump);
467 if (anImageToDump != myLibImage)
469 FreeImage_Unload (anImageToDump);
471 if (aTmpBitmap24 == NULL)
475 anImageToDump = aTmpBitmap24;
481 if (anImageToDump == NULL)
486 bool isSaved = (FreeImage_Save (anImageFormat, anImageToDump, theFileName.ToCString()) != FALSE);
487 if (anImageToDump != myLibImage)
489 FreeImage_Unload (anImageToDump);
493 const Standard_Integer aLen = theFileName.Length();
494 if ((aLen >= 4) && (theFileName.Value (aLen - 3) == '.')
495 && strcasecmp( theFileName.ToCString() + aLen - 3, "ppm") == 0 )
497 return savePPM (theFileName);
499 std::cerr << "Image_PixMap, no image library available! Image saved in PPM format.\n";
500 return savePPM (theFileName);
504 // =======================================================================
505 // function : AdjustGamma
507 // =======================================================================
508 #ifdef HAVE_FREEIMAGE
509 Standard_EXPORT bool Image_AlienPixMap::AdjustGamma (const Standard_Real theGammaCorr)
511 return FreeImage_AdjustGamma (myLibImage, theGammaCorr) != FALSE;
514 Standard_EXPORT bool Image_AlienPixMap::AdjustGamma (const Standard_Real)