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.
16 #if !defined(HAVE_FREEIMAGE) && defined(_WIN32)
21 #include <FreeImage.h>
24 #pragma comment( lib, "FreeImage.lib" )
26 #elif defined(HAVE_WINCODEC)
27 //#include <initguid.h>
33 #include <Image_AlienPixMap.hxx>
35 #include <Message.hxx>
36 #include <Message_Messenger.hxx>
37 #include <NCollection_Array1.hxx>
38 #include <Standard_ArrayStreamBuffer.hxx>
39 #include <TCollection_AsciiString.hxx>
40 #include <TCollection_ExtendedString.hxx>
41 #include <OSD_OpenFile.hxx>
45 IMPLEMENT_STANDARD_RTTIEXT(Image_AlienPixMap,Image_PixMap)
50 static Image_Format convertFromFreeFormat (FREE_IMAGE_TYPE theFormatFI,
51 FREE_IMAGE_COLOR_TYPE theColorTypeFI,
52 unsigned theBitsPerPixel)
56 case FIT_RGBF: return Image_Format_RGBF;
57 case FIT_RGBAF: return Image_Format_RGBAF;
58 case FIT_FLOAT: return Image_Format_GrayF;
61 switch (theColorTypeFI)
65 return Image_Format_Gray;
69 if (Image_PixMap::IsBigEndianHost())
71 return (theBitsPerPixel == 32) ? Image_Format_RGB32 : Image_Format_RGB;
75 return (theBitsPerPixel == 32) ? Image_Format_BGR32 : Image_Format_BGR;
80 return Image_PixMap::IsBigEndianHost() ? Image_Format_RGBA : Image_Format_BGRA;
83 return Image_Format_UNKNOWN;
87 return Image_Format_UNKNOWN;
91 static FREE_IMAGE_TYPE convertToFreeFormat (Image_Format theFormat)
95 case Image_Format_GrayF:
96 case Image_Format_AlphaF:
98 case Image_Format_RGBAF:
100 case Image_Format_RGBF:
102 case Image_Format_RGBA:
103 case Image_Format_BGRA:
104 case Image_Format_RGB32:
105 case Image_Format_BGR32:
106 case Image_Format_RGB:
107 case Image_Format_BGR:
108 case Image_Format_Gray:
109 case Image_Format_Alpha:
116 //! Wrapper for accessing C++ stream from FreeImage.
117 class Image_FreeImageStream
120 //! Construct wrapper over input stream.
121 Image_FreeImageStream (std::istream& theStream)
122 : myIStream (&theStream), myOStream (NULL), myInitPos (theStream.tellg()) {}
125 FreeImageIO GetFiIO() const
128 memset (&anIo, 0, sizeof(anIo));
129 if (myIStream != NULL)
131 anIo.read_proc = readProc;
132 anIo.seek_proc = seekProc;
133 anIo.tell_proc = tellProc;
135 if (myOStream != NULL)
137 anIo.write_proc = writeProc;
142 //! Simulate fread().
143 static unsigned int DLL_CALLCONV readProc (void* theBuffer, unsigned int theSize, unsigned int theCount, fi_handle theHandle)
145 Image_FreeImageStream* aThis = (Image_FreeImageStream* )theHandle;
146 if (aThis->myIStream == NULL)
151 if (!aThis->myIStream->read ((char* )theBuffer, std::streamsize(theSize) * std::streamsize(theCount)))
153 //aThis->myIStream->clear();
155 const std::streamsize aNbRead = aThis->myIStream->gcount();
156 return (unsigned int )(aNbRead / theSize);
159 //! Simulate fwrite().
160 static unsigned int DLL_CALLCONV writeProc (void* theBuffer, unsigned int theSize, unsigned int theCount, fi_handle theHandle)
162 Image_FreeImageStream* aThis = (Image_FreeImageStream* )theHandle;
163 if (aThis->myOStream != NULL
164 && aThis->myOStream->write ((const char* )theBuffer, std::streamsize(theSize) * std::streamsize(theCount)))
171 //! Simulate fseek().
172 static int DLL_CALLCONV seekProc (fi_handle theHandle, long theOffset, int theOrigin)
174 Image_FreeImageStream* aThis = (Image_FreeImageStream* )theHandle;
175 if (aThis->myIStream == NULL)
180 bool isSeekDone = false;
184 if (aThis->myIStream->seekg ((std::streamoff )aThis->myInitPos + theOffset, std::ios::beg))
190 if (aThis->myIStream->seekg (theOffset, std::ios::cur))
196 if (aThis->myIStream->seekg (theOffset, std::ios::end))
202 return isSeekDone ? 0 : -1;
205 //! Simulate ftell().
206 static long DLL_CALLCONV tellProc (fi_handle theHandle)
208 Image_FreeImageStream* aThis = (Image_FreeImageStream* )theHandle;
209 const long aPos = aThis->myIStream != NULL ? (long )(aThis->myIStream->tellg() - aThis->myInitPos) : 0;
213 std::istream* myIStream;
214 std::ostream* myOStream;
215 std::streampos myInitPos;
218 #elif defined(HAVE_WINCODEC)
220 //! Return a zero GUID
221 static GUID getNullGuid()
223 GUID aGuid = { 0, 0, 0, { 0, 0, 0, 0, 0, 0, 0, 0 } };
227 //! Sentry over IUnknown pointer.
228 template<class T> class Image_ComPtr
231 //! Empty constructor.
241 //! Return TRUE if pointer is NULL.
242 bool IsNull() const { return myPtr == NULL; }
244 //! Release the pointer.
254 //! Return pointer for initialization.
257 Standard_ASSERT_RAISE (myPtr == NULL, "Pointer cannot be initialized twice!");
262 T* get() { return myPtr; }
265 T* operator->() { return get(); }
267 //! Cast handle to contained type
268 T& operator*() { return *get(); }
274 //! Convert WIC GUID to Image_Format.
275 static Image_Format convertFromWicFormat (const WICPixelFormatGUID& theFormat)
277 if (theFormat == GUID_WICPixelFormat32bppBGRA)
279 return Image_Format_BGRA;
281 else if (theFormat == GUID_WICPixelFormat32bppBGR)
283 return Image_Format_BGR32;
285 else if (theFormat == GUID_WICPixelFormat24bppRGB)
287 return Image_Format_RGB;
289 else if (theFormat == GUID_WICPixelFormat24bppBGR)
291 return Image_Format_BGR;
293 else if (theFormat == GUID_WICPixelFormat8bppGray)
295 return Image_Format_Gray;
297 return Image_Format_UNKNOWN;
300 //! Convert Image_Format to WIC GUID.
301 static WICPixelFormatGUID convertToWicFormat (Image_Format theFormat)
305 case Image_Format_BGRA: return GUID_WICPixelFormat32bppBGRA;
306 case Image_Format_BGR32: return GUID_WICPixelFormat32bppBGR;
307 case Image_Format_RGB: return GUID_WICPixelFormat24bppRGB;
308 case Image_Format_BGR: return GUID_WICPixelFormat24bppBGR;
309 case Image_Format_Gray: return GUID_WICPixelFormat8bppGray;
310 case Image_Format_Alpha: return GUID_WICPixelFormat8bppGray; // GUID_WICPixelFormat8bppAlpha
311 case Image_Format_GrayF: // GUID_WICPixelFormat32bppGrayFloat
312 case Image_Format_AlphaF:
313 case Image_Format_RGBAF: // GUID_WICPixelFormat128bppRGBAFloat
314 case Image_Format_RGBF: // GUID_WICPixelFormat96bppRGBFloat
315 case Image_Format_RGBA: // GUID_WICPixelFormat32bppRGBA
316 case Image_Format_RGB32: // GUID_WICPixelFormat32bppRGB
318 return getNullGuid();
325 // =======================================================================
326 // function : Image_AlienPixMap
328 // =======================================================================
329 Image_AlienPixMap::Image_AlienPixMap()
335 // =======================================================================
336 // function : ~Image_AlienPixMap
338 // =======================================================================
339 Image_AlienPixMap::~Image_AlienPixMap()
344 // =======================================================================
345 // function : InitWrapper
347 // =======================================================================
348 bool Image_AlienPixMap::InitWrapper (Image_Format,
358 // =======================================================================
359 // function : InitTrash
361 // =======================================================================
362 #ifdef HAVE_FREEIMAGE
363 bool Image_AlienPixMap::InitTrash (Image_Format thePixelFormat,
364 const Standard_Size theSizeX,
365 const Standard_Size theSizeY,
366 const Standard_Size /*theSizeRowBytes*/)
369 FREE_IMAGE_TYPE aFormatFI = convertToFreeFormat (thePixelFormat);
370 int aBitsPerPixel = (int )Image_PixMap::SizePixelBytes (thePixelFormat) * 8;
371 if (aFormatFI == FIT_UNKNOWN)
373 aFormatFI = FIT_BITMAP;
377 FIBITMAP* anImage = FreeImage_AllocateT (aFormatFI, (int )theSizeX, (int )theSizeY, aBitsPerPixel);
378 Image_Format aFormat = convertFromFreeFormat (FreeImage_GetImageType(anImage),
379 FreeImage_GetColorType(anImage),
380 FreeImage_GetBPP (anImage));
381 if (thePixelFormat == Image_Format_BGR32
382 || thePixelFormat == Image_Format_RGB32)
384 //FreeImage_SetTransparent (anImage, FALSE);
385 aFormat = (aFormat == Image_Format_BGRA) ? Image_Format_BGR32 : Image_Format_RGB32;
388 Image_PixMap::InitWrapper (aFormat, FreeImage_GetBits (anImage),
389 FreeImage_GetWidth (anImage), FreeImage_GetHeight (anImage), FreeImage_GetPitch (anImage));
392 // assign image after wrapper initialization (virtual Clear() called inside)
393 myLibImage = anImage;
396 #elif defined(HAVE_WINCODEC)
397 bool Image_AlienPixMap::InitTrash (Image_Format thePixelFormat,
398 const Standard_Size theSizeX,
399 const Standard_Size theSizeY,
400 const Standard_Size theSizeRowBytes)
403 Image_Format aFormat = thePixelFormat;
406 case Image_Format_RGB:
407 aFormat = Image_Format_BGR;
409 case Image_Format_RGB32:
410 aFormat = Image_Format_BGR32;
412 case Image_Format_RGBA:
413 aFormat = Image_Format_BGRA;
419 if (!Image_PixMap::InitTrash (aFormat, theSizeX, theSizeY, theSizeRowBytes))
427 bool Image_AlienPixMap::InitTrash (Image_Format thePixelFormat,
428 const Standard_Size theSizeX,
429 const Standard_Size theSizeY,
430 const Standard_Size theSizeRowBytes)
432 return Image_PixMap::InitTrash (thePixelFormat, theSizeX, theSizeY, theSizeRowBytes);
436 // =======================================================================
437 // function : InitCopy
439 // =======================================================================
440 bool Image_AlienPixMap::InitCopy (const Image_PixMap& theCopy)
442 if (&theCopy == this)
444 // self-copying disallowed
447 if (!InitTrash (theCopy.Format(), theCopy.SizeX(), theCopy.SizeY(), theCopy.SizeRowBytes()))
452 if (myImgFormat == theCopy.Format())
454 if (SizeRowBytes() == theCopy.SizeRowBytes()
455 && TopDownInc() == theCopy.TopDownInc())
457 // copy with one call
458 memcpy (ChangeData(), theCopy.Data(), std::min (SizeBytes(), theCopy.SizeBytes()));
463 const Standard_Size aRowSizeBytes = std::min (SizeRowBytes(), theCopy.SizeRowBytes());
464 for (Standard_Size aRow = 0; aRow < myData.SizeY; ++aRow)
466 memcpy (ChangeRow (aRow), theCopy.Row (aRow), aRowSizeBytes);
471 // pixel format conversion required
476 // =======================================================================
479 // =======================================================================
480 void Image_AlienPixMap::Clear()
482 Image_PixMap::Clear();
483 #ifdef HAVE_FREEIMAGE
484 if (myLibImage != NULL)
486 FreeImage_Unload (myLibImage);
492 // =======================================================================
493 // function : IsTopDownDefault
495 // =======================================================================
496 bool Image_AlienPixMap::IsTopDownDefault()
498 #ifdef HAVE_FREEIMAGE
500 #elif defined(HAVE_WINCODEC)
507 // =======================================================================
510 // =======================================================================
511 #ifdef HAVE_FREEIMAGE
512 bool Image_AlienPixMap::Load (const Standard_Byte* theData,
513 Standard_Size theLength,
514 const TCollection_AsciiString& theImagePath)
519 const TCollection_ExtendedString aFileNameW (theImagePath);
521 FREE_IMAGE_FORMAT aFIF = FIF_UNKNOWN;
522 FIMEMORY* aFiMem = NULL;
525 aFiMem = FreeImage_OpenMemory ((BYTE* )theData, (DWORD )theLength);
526 aFIF = FreeImage_GetFileTypeFromMemory (aFiMem, 0);
531 aFIF = FreeImage_GetFileTypeU (aFileNameW.ToWideString(), 0);
533 aFIF = FreeImage_GetFileType (theImagePath.ToCString(), 0);
536 if (aFIF == FIF_UNKNOWN)
538 // no signature? try to guess the file format from the file extension
539 aFIF = FreeImage_GetFIFFromFilename (theImagePath.ToCString());
541 if ((aFIF == FIF_UNKNOWN) || !FreeImage_FIFSupportsReading (aFIF))
543 ::Message::DefaultMessenger()->Send (TCollection_AsciiString ("Error: image '") + theImagePath + "' has unsupported file format.",
547 FreeImage_CloseMemory (aFiMem);
555 // 'Play' the GIF to generate each frame (as 32bpp) instead of returning raw frame data when loading
556 aLoadFlags = GIF_PLAYBACK;
558 else if (aFIF == FIF_ICO)
560 // convert to 32bpp and create an alpha channel from the AND-mask when loading
561 aLoadFlags = ICO_MAKEALPHA;
564 FIBITMAP* anImage = NULL;
567 anImage = FreeImage_LoadFromMemory (aFIF, aFiMem, aLoadFlags);
568 FreeImage_CloseMemory (aFiMem);
574 anImage = FreeImage_LoadU (aFIF, aFileNameW.ToWideString(), aLoadFlags);
576 anImage = FreeImage_Load (aFIF, theImagePath.ToCString(), aLoadFlags);
581 TCollection_AsciiString aMessage = "Error: image file '";
582 aMessage.AssignCat (theImagePath);
583 aMessage.AssignCat ("' is missing or invalid.");
584 ::Message::DefaultMessenger()->Send (aMessage, Message_Fail);
588 Image_Format aFormat = convertFromFreeFormat (FreeImage_GetImageType(anImage),
589 FreeImage_GetColorType(anImage),
590 FreeImage_GetBPP (anImage));
591 if (aFormat == Image_Format_UNKNOWN)
593 //anImage = FreeImage_ConvertTo24Bits (anImage);
594 ::Message::DefaultMessenger()->Send ( TCollection_AsciiString ("Error: image '") + theImagePath + "' has unsupported pixel format.",
599 Image_PixMap::InitWrapper (aFormat, FreeImage_GetBits (anImage),
600 FreeImage_GetWidth (anImage), FreeImage_GetHeight (anImage), FreeImage_GetPitch (anImage));
603 // assign image after wrapper initialization (virtual Clear() called inside)
604 myLibImage = anImage;
608 bool Image_AlienPixMap::Load (std::istream& theStream,
609 const TCollection_AsciiString& theFileName)
613 Image_FreeImageStream aStream (theStream);
614 FreeImageIO aFiIO = aStream.GetFiIO();
616 FREE_IMAGE_FORMAT aFIF = FreeImage_GetFileTypeFromHandle (&aFiIO, &aStream, 0);
617 if (aFIF == FIF_UNKNOWN)
619 // no signature? try to guess the file format from the file extension
620 aFIF = FreeImage_GetFIFFromFilename (theFileName.ToCString());
622 if ((aFIF == FIF_UNKNOWN) || !FreeImage_FIFSupportsReading (aFIF))
624 ::Message::DefaultMessenger()->Send (TCollection_AsciiString ("Error: image stream '") + theFileName + "' has unsupported file format.",
632 // 'Play' the GIF to generate each frame (as 32bpp) instead of returning raw frame data when loading
633 aLoadFlags = GIF_PLAYBACK;
635 else if (aFIF == FIF_ICO)
637 // convert to 32bpp and create an alpha channel from the AND-mask when loading
638 aLoadFlags = ICO_MAKEALPHA;
641 FIBITMAP* anImage = FreeImage_LoadFromHandle (aFIF, &aFiIO, &aStream, aLoadFlags);
644 ::Message::DefaultMessenger()->Send (TCollection_AsciiString ("Error: image stream '") + theFileName + "' is missing or invalid.",
649 Image_Format aFormat = convertFromFreeFormat (FreeImage_GetImageType(anImage),
650 FreeImage_GetColorType(anImage),
651 FreeImage_GetBPP (anImage));
652 if (aFormat == Image_Format_UNKNOWN)
654 ::Message::DefaultMessenger()->Send (TCollection_AsciiString ("Error: image stream '") + theFileName + "' has unsupported pixel format.",
659 Image_PixMap::InitWrapper (aFormat, FreeImage_GetBits (anImage),
660 FreeImage_GetWidth (anImage), FreeImage_GetHeight (anImage), FreeImage_GetPitch (anImage));
663 // assign image after wrapper initialization (virtual Clear() called inside)
664 myLibImage = anImage;
668 #elif defined(HAVE_WINCODEC)
669 bool Image_AlienPixMap::Load (const Standard_Byte* theData,
670 Standard_Size theLength,
671 const TCollection_AsciiString& theFileName)
675 Image_ComPtr<IWICImagingFactory> aWicImgFactory;
676 CoInitializeEx (NULL, COINIT_MULTITHREADED);
677 if (CoCreateInstance (CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&aWicImgFactory.ChangePtr())) != S_OK)
679 Message::DefaultMessenger()->Send ("Error: cannot initialize WIC Imaging Factory", Message_Fail);
683 Image_ComPtr<IWICBitmapDecoder> aWicDecoder;
684 Image_ComPtr<IWICStream> aWicStream;
687 if (aWicImgFactory->CreateStream (&aWicStream.ChangePtr()) != S_OK
688 || aWicStream->InitializeFromMemory ((BYTE* )theData, (DWORD )theLength) != S_OK)
690 Message::DefaultMessenger()->Send ("Error: cannot initialize WIC Stream", Message_Fail);
693 if (aWicImgFactory->CreateDecoderFromStream (aWicStream.get(), NULL, WICDecodeMetadataCacheOnDemand, &aWicDecoder.ChangePtr()) != S_OK)
695 Message::DefaultMessenger()->Send ("Error: cannot create WIC Image Decoder", Message_Fail);
701 const TCollection_ExtendedString aFileNameW (theFileName);
702 if (aWicImgFactory->CreateDecoderFromFilename (aFileNameW.ToWideString(), NULL, GENERIC_READ, WICDecodeMetadataCacheOnDemand, &aWicDecoder.ChangePtr()) != S_OK)
704 Message::DefaultMessenger()->Send ("Error: cannot create WIC Image Decoder", Message_Fail);
709 UINT aFrameCount = 0, aFrameSizeX = 0, aFrameSizeY = 0;
710 WICPixelFormatGUID aWicPixelFormat = getNullGuid();
711 Image_ComPtr<IWICBitmapFrameDecode> aWicFrameDecode;
712 if (aWicDecoder->GetFrameCount (&aFrameCount) != S_OK
714 || aWicDecoder->GetFrame (0, &aWicFrameDecode.ChangePtr()) != S_OK
715 || aWicFrameDecode->GetSize (&aFrameSizeX, &aFrameSizeY) != S_OK
716 || aWicFrameDecode->GetPixelFormat (&aWicPixelFormat))
718 Message::DefaultMessenger()->Send ("Error: cannot get WIC Image Frame", Message_Fail);
722 Image_ComPtr<IWICFormatConverter> aWicConvertedFrame;
723 Image_Format aPixelFormat = convertFromWicFormat (aWicPixelFormat);
724 if (aPixelFormat == Image_Format_UNKNOWN)
726 aPixelFormat = Image_Format_RGB;
727 if (aWicImgFactory->CreateFormatConverter (&aWicConvertedFrame.ChangePtr()) != S_OK
728 || aWicConvertedFrame->Initialize (aWicFrameDecode.get(), convertToWicFormat (aPixelFormat), WICBitmapDitherTypeNone, NULL, 0.0f, WICBitmapPaletteTypeCustom) != S_OK)
730 Message::DefaultMessenger()->Send ("Error: cannot convert WIC Image Frame to RGB format", Message_Fail);
733 aWicFrameDecode.Nullify();
736 if (!Image_PixMap::InitTrash (aPixelFormat, aFrameSizeX, aFrameSizeY))
738 Message::DefaultMessenger()->Send ("Error: cannot initialize memory for image", Message_Fail);
742 IWICBitmapSource* aWicSrc = aWicFrameDecode.get();
743 if(!aWicConvertedFrame.IsNull())
745 aWicSrc = aWicConvertedFrame.get();
747 if (aWicSrc->CopyPixels (NULL, (UINT )SizeRowBytes(), (UINT )SizeBytes(), ChangeData()) != S_OK)
749 Message::DefaultMessenger()->Send ("Error: cannot copy pixels from WIC Image", Message_Fail);
755 bool Image_AlienPixMap::Load (std::istream& theStream,
756 const TCollection_AsciiString& theFilePath)
760 // fallback copying stream data into transient buffer
761 const std::streamoff aStart = theStream.tellg();
762 theStream.seekg (0, std::ios::end);
763 const Standard_Integer aLen = Standard_Integer(theStream.tellg() - aStart);
764 theStream.seekg (aStart);
767 Message::DefaultMessenger()->Send ("Error: empty stream", Message_Fail);
771 NCollection_Array1<Standard_Byte> aBuff (1, aLen);
772 if (!theStream.read ((char* )&aBuff.ChangeFirst(), aBuff.Size()))
774 Message::DefaultMessenger()->Send ("Error: unable to read stream", Message_Fail);
778 return Load (&aBuff.ChangeFirst(), aBuff.Size(), theFilePath);
781 bool Image_AlienPixMap::Load (std::istream& ,
782 const TCollection_AsciiString& )
785 Message::DefaultMessenger()->Send ("Error: no image library available", Message_Fail);
788 bool Image_AlienPixMap::Load (const Standard_Byte* ,
790 const TCollection_AsciiString& )
793 Message::DefaultMessenger()->Send ("Error: no image library available", Message_Fail);
798 // =======================================================================
799 // function : savePPM
801 // =======================================================================
802 bool Image_AlienPixMap::savePPM (const TCollection_AsciiString& theFileName) const
810 FILE* aFile = OSD_OpenFile (theFileName.ToCString(), "wb");
817 fprintf (aFile, "P6\n%d %d\n255\n", (int )SizeX(), (int )SizeY());
818 fprintf (aFile, "# Image stored by OpenCASCADE framework in linear RGB colorspace\n");
822 for (Standard_Size aRow = 0; aRow < SizeY(); ++aRow)
824 for (Standard_Size aCol = 0; aCol < SizeX(); ++aCol)
826 // extremely SLOW but universal (implemented for all supported pixel formats)
827 const Quantity_ColorRGBA aColor = PixelColor ((Standard_Integer )aCol, (Standard_Integer )aRow);
828 aByte = Standard_Byte(aColor.GetRGB().Red() * 255.0); fwrite (&aByte, 1, 1, aFile);
829 aByte = Standard_Byte(aColor.GetRGB().Green() * 255.0); fwrite (&aByte, 1, 1, aFile);
830 aByte = Standard_Byte(aColor.GetRGB().Blue() * 255.0); fwrite (&aByte, 1, 1, aFile);
839 // =======================================================================
842 // =======================================================================
843 bool Image_AlienPixMap::Save (const TCollection_AsciiString& theFileName)
845 #ifdef HAVE_FREEIMAGE
846 if (myLibImage == NULL)
852 const TCollection_ExtendedString aFileNameW (theFileName.ToCString(), Standard_True);
853 FREE_IMAGE_FORMAT anImageFormat = FreeImage_GetFIFFromFilenameU (aFileNameW.ToWideString());
855 FREE_IMAGE_FORMAT anImageFormat = FreeImage_GetFIFFromFilename (theFileName.ToCString());
857 if (anImageFormat == FIF_UNKNOWN)
860 std::cerr << "Image_PixMap, image format doesn't supported!\n";
867 FreeImage_FlipVertical (myLibImage);
871 // FreeImage doesn't provide flexible format conversion API
872 // so we should perform multiple conversions in some cases!
873 FIBITMAP* anImageToDump = myLibImage;
874 switch (anImageFormat)
879 if (Format() == Image_Format_BGR32
880 || Format() == Image_Format_RGB32)
882 // stupid FreeImage treats reserved byte as alpha if some bytes not set to 0xFF
883 for (Standard_Size aRow = 0; aRow < SizeY(); ++aRow)
885 for (Standard_Size aCol = 0; aCol < SizeX(); ++aCol)
887 myData.ChangeValue (aRow, aCol)[3] = 0xFF;
891 else if (FreeImage_GetImageType (myLibImage) != FIT_BITMAP)
893 anImageToDump = FreeImage_ConvertToType (myLibImage, FIT_BITMAP);
899 FIBITMAP* aTmpBitmap = myLibImage;
900 if (FreeImage_GetImageType (myLibImage) != FIT_BITMAP)
902 aTmpBitmap = FreeImage_ConvertToType (myLibImage, FIT_BITMAP);
903 if (aTmpBitmap == NULL)
909 if (FreeImage_GetBPP (aTmpBitmap) != 24)
911 FIBITMAP* aTmpBitmap24 = FreeImage_ConvertTo24Bits (aTmpBitmap);
912 if (aTmpBitmap != myLibImage)
914 FreeImage_Unload (aTmpBitmap);
916 if (aTmpBitmap24 == NULL)
920 aTmpBitmap = aTmpBitmap24;
923 // need conversion to image with palette (requires 24bit bitmap)
924 anImageToDump = FreeImage_ColorQuantize (aTmpBitmap, FIQ_NNQUANT);
925 if (aTmpBitmap != myLibImage)
927 FreeImage_Unload (aTmpBitmap);
934 if (Format() == Image_Format_Gray
935 || Format() == Image_Format_Alpha)
937 anImageToDump = FreeImage_ConvertToType (myLibImage, FIT_FLOAT);
939 else if (Format() == Image_Format_RGBA
940 || Format() == Image_Format_BGRA)
942 anImageToDump = FreeImage_ConvertToType (myLibImage, FIT_RGBAF);
946 FREE_IMAGE_TYPE aImgTypeFI = FreeImage_GetImageType (myLibImage);
947 if (aImgTypeFI != FIT_RGBF
948 && aImgTypeFI != FIT_RGBAF
949 && aImgTypeFI != FIT_FLOAT)
951 anImageToDump = FreeImage_ConvertToType (myLibImage, FIT_RGBF);
958 if (FreeImage_GetImageType (myLibImage) != FIT_BITMAP)
960 anImageToDump = FreeImage_ConvertToType (myLibImage, FIT_BITMAP);
961 if (anImageToDump == NULL)
967 if (FreeImage_GetBPP (anImageToDump) != 24)
969 FIBITMAP* aTmpBitmap24 = FreeImage_ConvertTo24Bits (anImageToDump);
970 if (anImageToDump != myLibImage)
972 FreeImage_Unload (anImageToDump);
974 if (aTmpBitmap24 == NULL)
978 anImageToDump = aTmpBitmap24;
984 if (anImageToDump == NULL)
990 bool isSaved = (FreeImage_SaveU (anImageFormat, anImageToDump, aFileNameW.ToWideString()) != FALSE);
992 bool isSaved = (FreeImage_Save (anImageFormat, anImageToDump, theFileName.ToCString()) != FALSE);
994 if (anImageToDump != myLibImage)
996 FreeImage_Unload (anImageToDump);
1000 #elif defined(HAVE_WINCODEC)
1002 TCollection_AsciiString aFileNameLower = theFileName;
1003 aFileNameLower.LowerCase();
1004 GUID aFileFormat = getNullGuid();
1005 if (aFileNameLower.EndsWith (".ppm"))
1007 return savePPM (theFileName);
1009 else if (aFileNameLower.EndsWith (".bmp"))
1011 aFileFormat = GUID_ContainerFormatBmp;
1013 else if (aFileNameLower.EndsWith (".png"))
1015 aFileFormat = GUID_ContainerFormatPng;
1017 else if (aFileNameLower.EndsWith (".jpg")
1018 || aFileNameLower.EndsWith (".jpeg"))
1020 aFileFormat = GUID_ContainerFormatJpeg;
1022 else if (aFileNameLower.EndsWith (".tiff"))
1024 aFileFormat = GUID_ContainerFormatTiff;
1026 else if (aFileNameLower.EndsWith (".gif"))
1028 aFileFormat = GUID_ContainerFormatGif;
1031 if (aFileFormat == getNullGuid())
1033 Message::DefaultMessenger()->Send ("Error: unsupported image format", Message_Fail);
1037 Image_ComPtr<IWICImagingFactory> aWicImgFactory;
1038 CoInitializeEx (NULL, COINIT_MULTITHREADED);
1039 if (CoCreateInstance (CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&aWicImgFactory.ChangePtr())) != S_OK)
1041 Message::DefaultMessenger()->Send ("Error: cannot initialize WIC Imaging Factory", Message_Fail);
1045 Image_ComPtr<IWICStream> aWicFileStream;
1046 Image_ComPtr<IWICBitmapEncoder> aWicEncoder;
1047 const TCollection_ExtendedString aFileNameW (theFileName);
1048 if (aWicImgFactory->CreateStream (&aWicFileStream.ChangePtr()) != S_OK
1049 || aWicFileStream->InitializeFromFilename (aFileNameW.ToWideString(), GENERIC_WRITE) != S_OK)
1051 Message::DefaultMessenger()->Send ("Error: cannot create WIC File Stream", Message_Fail);
1054 if (aWicImgFactory->CreateEncoder (aFileFormat, NULL, &aWicEncoder.ChangePtr()) != S_OK
1055 || aWicEncoder->Initialize (aWicFileStream.get(), WICBitmapEncoderNoCache) != S_OK)
1057 Message::DefaultMessenger()->Send ("Error: cannot create WIC Encoder", Message_Fail);
1061 const WICPixelFormatGUID aWicPixelFormat = convertToWicFormat (myImgFormat);
1062 if (aWicPixelFormat == getNullGuid())
1064 Message::DefaultMessenger()->Send ("Error: unsupported pixel format", Message_Fail);
1068 WICPixelFormatGUID aWicPixelFormatRes = aWicPixelFormat;
1069 Image_ComPtr<IWICBitmapFrameEncode> aWicFrameEncode;
1070 if (aWicEncoder->CreateNewFrame (&aWicFrameEncode.ChangePtr(), NULL) != S_OK
1071 || aWicFrameEncode->Initialize (NULL) != S_OK
1072 || aWicFrameEncode->SetSize ((UINT )SizeX(), (UINT )SizeY()) != S_OK
1073 || aWicFrameEncode->SetPixelFormat (&aWicPixelFormatRes) != S_OK)
1075 Message::DefaultMessenger()->Send ("Error: cannot create WIC Frame", Message_Fail);
1079 if (aWicPixelFormatRes != aWicPixelFormat)
1081 Message::DefaultMessenger()->Send ("Error: pixel format is unsupported by image format", Message_Fail);
1087 if (aWicFrameEncode->WritePixels ((UINT )SizeY(), (UINT )SizeRowBytes(), (UINT )SizeBytes(), (BYTE* )Data()) != S_OK)
1089 Message::DefaultMessenger()->Send ("Error: cannot write pixels to WIC Frame", Message_Fail);
1095 for (Standard_Size aRow = 0; aRow < SizeY(); ++aRow)
1097 if (aWicFrameEncode->WritePixels (1, (UINT )SizeRowBytes(), (UINT )SizeRowBytes(), (BYTE* )Row (aRow)) != S_OK)
1099 Message::DefaultMessenger()->Send ("Error: cannot write pixels to WIC Frame", Message_Fail);
1105 if (aWicFrameEncode->Commit() != S_OK
1106 || aWicEncoder->Commit() != S_OK)
1108 Message::DefaultMessenger()->Send ("Error: cannot commit data to WIC Frame", Message_Fail);
1111 if (aWicFileStream->Commit (STGC_DEFAULT) != S_OK)
1113 //Message::DefaultMessenger()->Send ("Error: cannot commit data to WIC File Stream", Message_Fail);
1118 const Standard_Integer aLen = theFileName.Length();
1119 if ((aLen >= 4) && (theFileName.Value (aLen - 3) == '.')
1120 && strcasecmp( theFileName.ToCString() + aLen - 3, "ppm") == 0 )
1122 return savePPM (theFileName);
1125 std::cerr << "Image_PixMap, no image library available! Image saved in PPM format.\n";
1127 return savePPM (theFileName);
1131 // =======================================================================
1132 // function : AdjustGamma
1134 // =======================================================================
1135 bool Image_AlienPixMap::AdjustGamma (const Standard_Real theGammaCorr)
1137 #ifdef HAVE_FREEIMAGE
1138 return FreeImage_AdjustGamma (myLibImage, theGammaCorr) != FALSE;
1140 (void )theGammaCorr;