1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203
|
// Created on: 2010-09-16
// Created by: KGV
// Copyright (c) 2010-2014 OPEN CASCADE SAS
//
// This file is part of Open CASCADE Technology software library.
//
// This library is free software; you can redistribute it and/or modify it under
// the terms of the GNU Lesser General Public License version 2.1 as published
// by the Free Software Foundation, with special exception defined in the file
// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
// distribution for complete text of the license and disclaimer of any warranty.
//
// Alternatively, this file may be used under the terms of Open CASCADE
// commercial license or contractual agreement.
#if !defined(HAVE_FREEIMAGE) && defined(_WIN32)
#define HAVE_WINCODEC
#endif
#ifdef HAVE_FREEIMAGE
#include <FreeImage.h>
#ifdef _MSC_VER
#pragma comment( lib, "FreeImage.lib" )
#endif
#elif defined(HAVE_WINCODEC)
#include <wincodec.h>
// prevent warnings on MSVC10
#include <Standard_WarningsDisable.hxx>
#include <Standard_TypeDef.hxx>
#include <Standard_WarningsRestore.hxx>
#undef min
#undef max
#ifdef _MSC_VER
#pragma comment(lib, "Ole32.lib")
#endif
#elif defined(__EMSCRIPTEN__)
#include <emscripten/emscripten.h>
#endif
#include <Image_AlienPixMap.hxx>
#include <gp.hxx>
#include <Message.hxx>
#include <Message_Messenger.hxx>
#include <NCollection_Array1.hxx>
#include <Standard_ArrayStreamBuffer.hxx>
#include <TCollection_AsciiString.hxx>
#include <TCollection_ExtendedString.hxx>
#include <OSD_OpenFile.hxx>
#include <fstream>
#include <algorithm>
IMPLEMENT_STANDARD_RTTIEXT(Image_AlienPixMap,Image_PixMap)
namespace
{
#ifdef HAVE_FREEIMAGE
static Image_Format convertFromFreeFormat (FREE_IMAGE_TYPE theFormatFI,
FREE_IMAGE_COLOR_TYPE theColorTypeFI,
unsigned theBitsPerPixel)
{
switch (theFormatFI)
{
case FIT_RGBF: return Image_Format_RGBF;
case FIT_RGBAF: return Image_Format_RGBAF;
case FIT_FLOAT: return Image_Format_GrayF;
case FIT_BITMAP:
{
switch (theColorTypeFI)
{
case FIC_MINISBLACK:
{
return Image_Format_Gray;
}
case FIC_RGB:
{
if (Image_PixMap::IsBigEndianHost())
{
return (theBitsPerPixel == 32) ? Image_Format_RGB32 : Image_Format_RGB;
}
else
{
return (theBitsPerPixel == 32) ? Image_Format_BGR32 : Image_Format_BGR;
}
}
case FIC_RGBALPHA:
{
return Image_PixMap::IsBigEndianHost() ? Image_Format_RGBA : Image_Format_BGRA;
}
default:
return Image_Format_UNKNOWN;
}
}
default:
return Image_Format_UNKNOWN;
}
}
static FREE_IMAGE_TYPE convertToFreeFormat (Image_Format theFormat)
{
switch (theFormat)
{
case Image_Format_GrayF:
case Image_Format_AlphaF:
return FIT_FLOAT;
case Image_Format_RGBAF:
return FIT_RGBAF;
case Image_Format_RGBF:
return FIT_RGBF;
case Image_Format_RGBA:
case Image_Format_BGRA:
case Image_Format_RGB32:
case Image_Format_BGR32:
case Image_Format_RGB:
case Image_Format_BGR:
case Image_Format_Gray:
case Image_Format_Alpha:
return FIT_BITMAP;
default:
return FIT_UNKNOWN;
}
}
//! Wrapper for accessing C++ stream from FreeImage.
class Image_FreeImageStream
{
public:
//! Construct wrapper over input stream.
Image_FreeImageStream (std::istream& theStream)
: myIStream (&theStream), myOStream (NULL), myInitPos (theStream.tellg()) {}
//! Get io object.
FreeImageIO GetFiIO() const
{
FreeImageIO anIo;
memset (&anIo, 0, sizeof(anIo));
if (myIStream != NULL)
{
anIo.read_proc = readProc;
anIo.seek_proc = seekProc;
anIo.tell_proc = tellProc;
}
if (myOStream != NULL)
{
anIo.write_proc = writeProc;
}
return anIo;
}
public:
//! Simulate fread().
static unsigned int DLL_CALLCONV readProc (void* theBuffer, unsigned int theSize, unsigned int theCount, fi_handle theHandle)
{
Image_FreeImageStream* aThis = (Image_FreeImageStream* )theHandle;
if (aThis->myIStream == NULL)
{
return 0;
}
if (!aThis->myIStream->read ((char* )theBuffer, std::streamsize(theSize) * std::streamsize(theCount)))
{
//aThis->myIStream->clear();
}
const std::streamsize aNbRead = aThis->myIStream->gcount();
return (unsigned int )(aNbRead / theSize);
}
//! Simulate fwrite().
static unsigned int DLL_CALLCONV writeProc (void* theBuffer, unsigned int theSize, unsigned int theCount, fi_handle theHandle)
{
Image_FreeImageStream* aThis = (Image_FreeImageStream* )theHandle;
if (aThis->myOStream != NULL
&& aThis->myOStream->write ((const char* )theBuffer, std::streamsize(theSize) * std::streamsize(theCount)))
{
return theCount;
}
return 0;
}
//! Simulate fseek().
static int DLL_CALLCONV seekProc (fi_handle theHandle, long theOffset, int theOrigin)
{
Image_FreeImageStream* aThis = (Image_FreeImageStream* )theHandle;
if (aThis->myIStream == NULL)
{
return -1;
}
bool isSeekDone = false;
switch (theOrigin)
{
case SEEK_SET:
if (aThis->myIStream->seekg ((std::streamoff )aThis->myInitPos + theOffset, std::ios::beg))
{
isSeekDone = true;
}
break;
case SEEK_CUR:
if (aThis->myIStream->seekg (theOffset, std::ios::cur))
{
isSeekDone = true;
}
break;
case SEEK_END:
if (aThis->myIStream->seekg (theOffset, std::ios::end))
{
isSeekDone = true;
}
break;
}
return isSeekDone ? 0 : -1;
}
//! Simulate ftell().
static long DLL_CALLCONV tellProc (fi_handle theHandle)
{
Image_FreeImageStream* aThis = (Image_FreeImageStream* )theHandle;
const long aPos = aThis->myIStream != NULL ? (long )(aThis->myIStream->tellg() - aThis->myInitPos) : 0;
return aPos;
}
private:
std::istream* myIStream;
std::ostream* myOStream;
std::streampos myInitPos;
};
#elif defined(HAVE_WINCODEC)
//! Return a zero GUID
static GUID getNullGuid()
{
GUID aGuid = { 0, 0, 0, { 0, 0, 0, 0, 0, 0, 0, 0 } };
return aGuid;
}
//! Sentry over IUnknown pointer.
template<class T> class Image_ComPtr
{
public:
//! Empty constructor.
Image_ComPtr()
: myPtr (NULL) {}
//! Destructor.
~Image_ComPtr()
{
Nullify();
}
//! Return TRUE if pointer is NULL.
bool IsNull() const { return myPtr == NULL; }
//! Release the pointer.
void Nullify()
{
if (myPtr != NULL)
{
myPtr->Release();
myPtr = NULL;
}
}
//! Return pointer for initialization.
T*& ChangePtr()
{
Standard_ASSERT_RAISE (myPtr == NULL, "Pointer cannot be initialized twice!");
return myPtr;
}
//! Return pointer.
T* get() { return myPtr; }
//! Return pointer.
T* operator->() { return get(); }
//! Cast handle to contained type
T& operator*() { return *get(); }
private:
T* myPtr;
};
//! Convert WIC GUID to Image_Format.
static Image_Format convertFromWicFormat (const WICPixelFormatGUID& theFormat)
{
if (theFormat == GUID_WICPixelFormat32bppBGRA)
{
return Image_Format_BGRA;
}
else if (theFormat == GUID_WICPixelFormat32bppBGR)
{
return Image_Format_BGR32;
}
else if (theFormat == GUID_WICPixelFormat24bppRGB)
{
return Image_Format_RGB;
}
else if (theFormat == GUID_WICPixelFormat24bppBGR)
{
return Image_Format_BGR;
}
else if (theFormat == GUID_WICPixelFormat8bppGray)
{
return Image_Format_Gray;
}
return Image_Format_UNKNOWN;
}
//! Convert Image_Format to WIC GUID.
static WICPixelFormatGUID convertToWicFormat (Image_Format theFormat)
{
switch (theFormat)
{
case Image_Format_BGRA: return GUID_WICPixelFormat32bppBGRA;
case Image_Format_BGR32: return GUID_WICPixelFormat32bppBGR;
case Image_Format_RGB: return GUID_WICPixelFormat24bppRGB;
case Image_Format_BGR: return GUID_WICPixelFormat24bppBGR;
case Image_Format_Gray: return GUID_WICPixelFormat8bppGray;
case Image_Format_Alpha: return GUID_WICPixelFormat8bppGray; // GUID_WICPixelFormat8bppAlpha
case Image_Format_GrayF: // GUID_WICPixelFormat32bppGrayFloat
case Image_Format_AlphaF:
case Image_Format_RGBAF: // GUID_WICPixelFormat128bppRGBAFloat
case Image_Format_RGBF: // GUID_WICPixelFormat96bppRGBFloat
case Image_Format_RGBA: // GUID_WICPixelFormat32bppRGBA
case Image_Format_RGB32: // GUID_WICPixelFormat32bppRGB
default:
return getNullGuid();
}
}
#endif
}
// =======================================================================
// function : Image_AlienPixMap
// purpose :
// =======================================================================
Image_AlienPixMap::Image_AlienPixMap()
: myLibImage (NULL)
{
SetTopDown (false);
}
// =======================================================================
// function : ~Image_AlienPixMap
// purpose :
// =======================================================================
Image_AlienPixMap::~Image_AlienPixMap()
{
Clear();
}
// =======================================================================
// function : InitWrapper
// purpose :
// =======================================================================
bool Image_AlienPixMap::InitWrapper (Image_Format,
Standard_Byte*,
const Standard_Size,
const Standard_Size,
const Standard_Size)
{
Clear();
return false;
}
// =======================================================================
// function : InitTrash
// purpose :
// =======================================================================
#ifdef HAVE_FREEIMAGE
bool Image_AlienPixMap::InitTrash (Image_Format thePixelFormat,
const Standard_Size theSizeX,
const Standard_Size theSizeY,
const Standard_Size /*theSizeRowBytes*/)
{
Clear();
FREE_IMAGE_TYPE aFormatFI = convertToFreeFormat (thePixelFormat);
int aBitsPerPixel = (int )Image_PixMap::SizePixelBytes (thePixelFormat) * 8;
if (aFormatFI == FIT_UNKNOWN)
{
aFormatFI = FIT_BITMAP;
aBitsPerPixel = 24;
}
FIBITMAP* anImage = FreeImage_AllocateT (aFormatFI, (int )theSizeX, (int )theSizeY, aBitsPerPixel);
Image_Format aFormat = convertFromFreeFormat (FreeImage_GetImageType(anImage),
FreeImage_GetColorType(anImage),
FreeImage_GetBPP (anImage));
if (thePixelFormat == Image_Format_BGR32
|| thePixelFormat == Image_Format_RGB32)
{
//FreeImage_SetTransparent (anImage, FALSE);
aFormat = (aFormat == Image_Format_BGRA) ? Image_Format_BGR32 : Image_Format_RGB32;
}
Image_PixMap::InitWrapper (aFormat, FreeImage_GetBits (anImage),
FreeImage_GetWidth (anImage), FreeImage_GetHeight (anImage), FreeImage_GetPitch (anImage));
SetTopDown (false);
// assign image after wrapper initialization (virtual Clear() called inside)
myLibImage = anImage;
return true;
}
#elif defined(HAVE_WINCODEC)
bool Image_AlienPixMap::InitTrash (Image_Format thePixelFormat,
const Standard_Size theSizeX,
const Standard_Size theSizeY,
const Standard_Size theSizeRowBytes)
{
Clear();
Image_Format aFormat = thePixelFormat;
switch (aFormat)
{
case Image_Format_RGB:
aFormat = Image_Format_BGR;
break;
case Image_Format_RGB32:
aFormat = Image_Format_BGR32;
break;
case Image_Format_RGBA:
aFormat = Image_Format_BGRA;
break;
default:
break;
}
if (!Image_PixMap::InitTrash (aFormat, theSizeX, theSizeY, theSizeRowBytes))
{
return false;
}
SetTopDown (true);
return true;
}
#else
bool Image_AlienPixMap::InitTrash (Image_Format thePixelFormat,
const Standard_Size theSizeX,
const Standard_Size theSizeY,
const Standard_Size theSizeRowBytes)
{
return Image_PixMap::InitTrash (thePixelFormat, theSizeX, theSizeY, theSizeRowBytes);
}
#endif
// =======================================================================
// function : InitCopy
// purpose :
// =======================================================================
bool Image_AlienPixMap::InitCopy (const Image_PixMap& theCopy)
{
if (&theCopy == this)
{
// self-copying disallowed
return false;
}
if (!InitTrash (theCopy.Format(), theCopy.SizeX(), theCopy.SizeY(), theCopy.SizeRowBytes()))
{
return false;
}
if (myImgFormat == theCopy.Format())
{
if (SizeRowBytes() == theCopy.SizeRowBytes()
&& TopDownInc() == theCopy.TopDownInc())
{
// copy with one call
memcpy (ChangeData(), theCopy.Data(), std::min (SizeBytes(), theCopy.SizeBytes()));
return true;
}
// copy row-by-row
const Standard_Size aRowSizeBytes = std::min (SizeRowBytes(), theCopy.SizeRowBytes());
for (Standard_Size aRow = 0; aRow < myData.SizeY; ++aRow)
{
memcpy (ChangeRow (aRow), theCopy.Row (aRow), aRowSizeBytes);
}
return true;
}
// pixel format conversion required
Clear();
return false;
}
// =======================================================================
// function : Clear
// purpose :
// =======================================================================
void Image_AlienPixMap::Clear()
{
Image_PixMap::Clear();
#ifdef HAVE_FREEIMAGE
if (myLibImage != NULL)
{
FreeImage_Unload (myLibImage);
myLibImage = NULL;
}
#elif defined(__EMSCRIPTEN__)
if (myLibImage != NULL)
{
free ((void* )myLibImage);
myLibImage = NULL;
}
#endif
}
// =======================================================================
// function : IsTopDownDefault
// purpose :
// =======================================================================
bool Image_AlienPixMap::IsTopDownDefault()
{
#ifdef HAVE_FREEIMAGE
return false;
#elif defined(HAVE_WINCODEC)
return true;
#else
return false;
#endif
}
// =======================================================================
// function : Load
// purpose :
// =======================================================================
#ifdef HAVE_FREEIMAGE
bool Image_AlienPixMap::Load (const Standard_Byte* theData,
Standard_Size theLength,
const TCollection_AsciiString& theImagePath)
{
Clear();
#ifdef _WIN32
const TCollection_ExtendedString aFileNameW (theImagePath);
#endif
FREE_IMAGE_FORMAT aFIF = FIF_UNKNOWN;
FIMEMORY* aFiMem = NULL;
if (theData != NULL)
{
aFiMem = FreeImage_OpenMemory ((BYTE* )theData, (DWORD )theLength);
aFIF = FreeImage_GetFileTypeFromMemory (aFiMem, 0);
}
else
{
#ifdef _WIN32
aFIF = FreeImage_GetFileTypeU (aFileNameW.ToWideString(), 0);
#else
aFIF = FreeImage_GetFileType (theImagePath.ToCString(), 0);
#endif
}
if (aFIF == FIF_UNKNOWN)
{
// no signature? try to guess the file format from the file extension
aFIF = FreeImage_GetFIFFromFilename (theImagePath.ToCString());
}
if ((aFIF == FIF_UNKNOWN) || !FreeImage_FIFSupportsReading (aFIF))
{
::Message::SendFail (TCollection_AsciiString ("Error: image '") + theImagePath + "' has unsupported file format");
if (aFiMem != NULL)
{
FreeImage_CloseMemory (aFiMem);
}
return false;
}
int aLoadFlags = 0;
if (aFIF == FIF_GIF)
{
// 'Play' the GIF to generate each frame (as 32bpp) instead of returning raw frame data when loading
aLoadFlags = GIF_PLAYBACK;
}
else if (aFIF == FIF_ICO)
{
// convert to 32bpp and create an alpha channel from the AND-mask when loading
aLoadFlags = ICO_MAKEALPHA;
}
FIBITMAP* anImage = NULL;
if (theData != NULL)
{
anImage = FreeImage_LoadFromMemory (aFIF, aFiMem, aLoadFlags);
FreeImage_CloseMemory (aFiMem);
aFiMem = NULL;
}
else
{
#ifdef _WIN32
anImage = FreeImage_LoadU (aFIF, aFileNameW.ToWideString(), aLoadFlags);
#else
anImage = FreeImage_Load (aFIF, theImagePath.ToCString(), aLoadFlags);
#endif
}
if (anImage == NULL)
{
::Message::SendFail (TCollection_AsciiString ("Error: image file '") + theImagePath + "' is missing or invalid");
return false;
}
Image_Format aFormat = Image_Format_UNKNOWN;
if (FreeImage_GetBPP (anImage) == 1)
{
FIBITMAP* aTmpImage = FreeImage_ConvertTo8Bits (anImage);
FreeImage_Unload (anImage);
anImage = aTmpImage;
}
if (anImage != NULL)
{
aFormat = convertFromFreeFormat (FreeImage_GetImageType(anImage),
FreeImage_GetColorType(anImage),
FreeImage_GetBPP (anImage));
if (aFormat == Image_Format_UNKNOWN)
{
FIBITMAP* aTmpImage = FreeImage_ConvertTo24Bits (anImage);
FreeImage_Unload (anImage);
anImage = aTmpImage;
if (anImage != NULL)
{
aFormat = convertFromFreeFormat (FreeImage_GetImageType(anImage),
FreeImage_GetColorType(anImage),
FreeImage_GetBPP (anImage));
}
}
}
if (aFormat == Image_Format_UNKNOWN)
{
::Message::SendFail (TCollection_AsciiString ("Error: image '") + theImagePath + "' has unsupported pixel format");
return false;
}
Image_PixMap::InitWrapper (aFormat, FreeImage_GetBits (anImage),
FreeImage_GetWidth (anImage), FreeImage_GetHeight (anImage), FreeImage_GetPitch (anImage));
SetTopDown (false);
// assign image after wrapper initialization (virtual Clear() called inside)
myLibImage = anImage;
return true;
}
bool Image_AlienPixMap::Load (std::istream& theStream,
const TCollection_AsciiString& theFileName)
{
Clear();
Image_FreeImageStream aStream (theStream);
FreeImageIO aFiIO = aStream.GetFiIO();
FREE_IMAGE_FORMAT aFIF = FreeImage_GetFileTypeFromHandle (&aFiIO, &aStream, 0);
if (aFIF == FIF_UNKNOWN)
{
// no signature? try to guess the file format from the file extension
aFIF = FreeImage_GetFIFFromFilename (theFileName.ToCString());
}
if ((aFIF == FIF_UNKNOWN) || !FreeImage_FIFSupportsReading (aFIF))
{
::Message::SendFail (TCollection_AsciiString ("Error: image stream '") + theFileName + "' has unsupported file format");
return false;
}
int aLoadFlags = 0;
if (aFIF == FIF_GIF)
{
// 'Play' the GIF to generate each frame (as 32bpp) instead of returning raw frame data when loading
aLoadFlags = GIF_PLAYBACK;
}
else if (aFIF == FIF_ICO)
{
// convert to 32bpp and create an alpha channel from the AND-mask when loading
aLoadFlags = ICO_MAKEALPHA;
}
FIBITMAP* anImage = FreeImage_LoadFromHandle (aFIF, &aFiIO, &aStream, aLoadFlags);
if (anImage == NULL)
{
::Message::SendFail (TCollection_AsciiString ("Error: image stream '") + theFileName + "' is missing or invalid");
return false;
}
Image_Format aFormat = convertFromFreeFormat (FreeImage_GetImageType(anImage),
FreeImage_GetColorType(anImage),
FreeImage_GetBPP (anImage));
if (aFormat == Image_Format_UNKNOWN)
{
::Message::SendFail (TCollection_AsciiString ("Error: image stream '") + theFileName + "' has unsupported pixel format");
return false;
}
Image_PixMap::InitWrapper (aFormat, FreeImage_GetBits (anImage),
FreeImage_GetWidth (anImage), FreeImage_GetHeight (anImage), FreeImage_GetPitch (anImage));
SetTopDown (false);
// assign image after wrapper initialization (virtual Clear() called inside)
myLibImage = anImage;
return true;
}
#elif defined(HAVE_WINCODEC)
bool Image_AlienPixMap::Load (const Standard_Byte* theData,
Standard_Size theLength,
const TCollection_AsciiString& theFileName)
{
Clear();
Image_ComPtr<IWICImagingFactory> aWicImgFactory;
CoInitializeEx (NULL, COINIT_MULTITHREADED);
if (CoCreateInstance (CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&aWicImgFactory.ChangePtr())) != S_OK)
{
Message::SendFail ("Error: cannot initialize WIC Imaging Factory");
return false;
}
Image_ComPtr<IWICBitmapDecoder> aWicDecoder;
Image_ComPtr<IWICStream> aWicStream;
if (theData != NULL)
{
if (aWicImgFactory->CreateStream (&aWicStream.ChangePtr()) != S_OK
|| aWicStream->InitializeFromMemory ((BYTE* )theData, (DWORD )theLength) != S_OK)
{
Message::SendFail ("Error: cannot initialize WIC Stream");
return false;
}
if (aWicImgFactory->CreateDecoderFromStream (aWicStream.get(), NULL, WICDecodeMetadataCacheOnDemand, &aWicDecoder.ChangePtr()) != S_OK)
{
Message::SendFail ("Error: cannot create WIC Image Decoder");
return false;
}
}
else
{
const TCollection_ExtendedString aFileNameW (theFileName);
if (aWicImgFactory->CreateDecoderFromFilename (aFileNameW.ToWideString(), NULL, GENERIC_READ, WICDecodeMetadataCacheOnDemand, &aWicDecoder.ChangePtr()) != S_OK)
{
Message::SendFail ("Error: cannot create WIC Image Decoder");
return false;
}
}
UINT aFrameCount = 0, aFrameSizeX = 0, aFrameSizeY = 0;
WICPixelFormatGUID aWicPixelFormat = getNullGuid();
Image_ComPtr<IWICBitmapFrameDecode> aWicFrameDecode;
if (aWicDecoder->GetFrameCount (&aFrameCount) != S_OK
|| aFrameCount < 1
|| aWicDecoder->GetFrame (0, &aWicFrameDecode.ChangePtr()) != S_OK
|| aWicFrameDecode->GetSize (&aFrameSizeX, &aFrameSizeY) != S_OK
|| aWicFrameDecode->GetPixelFormat (&aWicPixelFormat))
{
Message::SendFail ("Error: cannot get WIC Image Frame");
return false;
}
Image_ComPtr<IWICFormatConverter> aWicConvertedFrame;
Image_Format aPixelFormat = convertFromWicFormat (aWicPixelFormat);
if (aPixelFormat == Image_Format_UNKNOWN)
{
aPixelFormat = Image_Format_RGB;
if (aWicImgFactory->CreateFormatConverter (&aWicConvertedFrame.ChangePtr()) != S_OK
|| aWicConvertedFrame->Initialize (aWicFrameDecode.get(), convertToWicFormat (aPixelFormat), WICBitmapDitherTypeNone, NULL, 0.0f, WICBitmapPaletteTypeCustom) != S_OK)
{
Message::SendFail ("Error: cannot convert WIC Image Frame to RGB format");
return false;
}
aWicFrameDecode.Nullify();
}
if (!Image_PixMap::InitTrash (aPixelFormat, aFrameSizeX, aFrameSizeY))
{
Message::SendFail ("Error: cannot initialize memory for image");
return false;
}
IWICBitmapSource* aWicSrc = aWicFrameDecode.get();
if(!aWicConvertedFrame.IsNull())
{
aWicSrc = aWicConvertedFrame.get();
}
if (aWicSrc->CopyPixels (NULL, (UINT )SizeRowBytes(), (UINT )SizeBytes(), ChangeData()) != S_OK)
{
Message::SendFail ("Error: cannot copy pixels from WIC Image");
return false;
}
SetTopDown (true);
return true;
}
bool Image_AlienPixMap::Load (std::istream& theStream,
const TCollection_AsciiString& theFilePath)
{
Clear();
// fallback copying stream data into transient buffer
const std::streamoff aStart = theStream.tellg();
theStream.seekg (0, std::ios::end);
const Standard_Integer aLen = Standard_Integer(theStream.tellg() - aStart);
theStream.seekg (aStart);
if (aLen <= 0)
{
Message::SendFail ("Error: empty stream");
return false;
}
NCollection_Array1<Standard_Byte> aBuff (1, aLen);
if (!theStream.read ((char* )&aBuff.ChangeFirst(), aBuff.Size()))
{
Message::SendFail ("Error: unable to read stream");
return false;
}
return Load (&aBuff.ChangeFirst(), aBuff.Size(), theFilePath);
}
#elif defined(__EMSCRIPTEN__)
bool Image_AlienPixMap::Load (std::istream& ,
const TCollection_AsciiString& )
{
Clear();
Message::SendFail ("Error: no image library available for decoding stream");
return false;
}
bool Image_AlienPixMap::Load (const Standard_Byte* theData,
Standard_Size theLength,
const TCollection_AsciiString& theImagePath)
{
Clear();
if (theData != NULL)
{
(void )theLength;
Message::SendFail ("Error: no image library available for decoding in-memory buffer");
return false;
}
int aSizeX = 0, aSizeY = 0;
char* anImgData = emscripten_get_preloaded_image_data (theImagePath.ToCString(), &aSizeX, &aSizeY);
if (anImgData == NULL)
{
Message::SendFail() << "Error: image '" << theImagePath << "' is not preloaded";
return false;
}
Image_PixMap::InitWrapper (Image_Format_RGBA, (Standard_Byte* )anImgData, aSizeX, aSizeY);
SetTopDown (true);
myLibImage = (FIBITMAP* )anImgData;
return true;
}
#else
bool Image_AlienPixMap::Load (std::istream& ,
const TCollection_AsciiString& )
{
Clear();
Message::SendFail ("Error: no image library available");
return false;
}
bool Image_AlienPixMap::Load (const Standard_Byte* ,
Standard_Size ,
const TCollection_AsciiString& )
{
Clear();
Message::SendFail ("Error: no image library available");
return false;
}
#endif
// =======================================================================
// function : savePPM
// purpose :
// =======================================================================
bool Image_AlienPixMap::savePPM (const TCollection_AsciiString& theFileName) const
{
if (IsEmpty())
{
return false;
}
// Open file
FILE* aFile = OSD_OpenFile (theFileName.ToCString(), "wb");
if (aFile == NULL)
{
return false;
}
// Write header
fprintf (aFile, "P6\n%d %d\n255\n", (int )SizeX(), (int )SizeY());
fprintf (aFile, "# Image stored by OpenCASCADE framework in linear RGB colorspace\n");
// Write pixel data
Standard_Byte aByte;
for (Standard_Size aRow = 0; aRow < SizeY(); ++aRow)
{
for (Standard_Size aCol = 0; aCol < SizeX(); ++aCol)
{
// extremely SLOW but universal (implemented for all supported pixel formats)
const Quantity_ColorRGBA aColor = PixelColor ((Standard_Integer )aCol, (Standard_Integer )aRow);
aByte = Standard_Byte(aColor.GetRGB().Red() * 255.0); fwrite (&aByte, 1, 1, aFile);
aByte = Standard_Byte(aColor.GetRGB().Green() * 255.0); fwrite (&aByte, 1, 1, aFile);
aByte = Standard_Byte(aColor.GetRGB().Blue() * 255.0); fwrite (&aByte, 1, 1, aFile);
}
}
// Close file
fclose (aFile);
return true;
}
// =======================================================================
// function : Save
// purpose :
// =======================================================================
bool Image_AlienPixMap::Save (const TCollection_AsciiString& theFileName)
{
#ifdef HAVE_FREEIMAGE
if (myLibImage == NULL)
{
return false;
}
#ifdef _WIN32
const TCollection_ExtendedString aFileNameW (theFileName.ToCString(), Standard_True);
FREE_IMAGE_FORMAT anImageFormat = FreeImage_GetFIFFromFilenameU (aFileNameW.ToWideString());
#else
FREE_IMAGE_FORMAT anImageFormat = FreeImage_GetFIFFromFilename (theFileName.ToCString());
#endif
if (anImageFormat == FIF_UNKNOWN)
{
#ifdef OCCT_DEBUG
std::cerr << "Image_PixMap, image format doesn't supported!\n";
#endif
return false;
}
if (IsTopDown())
{
FreeImage_FlipVertical (myLibImage);
SetTopDown (false);
}
// FreeImage doesn't provide flexible format conversion API
// so we should perform multiple conversions in some cases!
FIBITMAP* anImageToDump = myLibImage;
switch (anImageFormat)
{
case FIF_PNG:
case FIF_BMP:
{
if (Format() == Image_Format_BGR32
|| Format() == Image_Format_RGB32)
{
// stupid FreeImage treats reserved byte as alpha if some bytes not set to 0xFF
for (Standard_Size aRow = 0; aRow < SizeY(); ++aRow)
{
for (Standard_Size aCol = 0; aCol < SizeX(); ++aCol)
{
myData.ChangeValue (aRow, aCol)[3] = 0xFF;
}
}
}
else if (FreeImage_GetImageType (myLibImage) != FIT_BITMAP)
{
anImageToDump = FreeImage_ConvertToType (myLibImage, FIT_BITMAP);
}
break;
}
case FIF_GIF:
{
FIBITMAP* aTmpBitmap = myLibImage;
if (FreeImage_GetImageType (myLibImage) != FIT_BITMAP)
{
aTmpBitmap = FreeImage_ConvertToType (myLibImage, FIT_BITMAP);
if (aTmpBitmap == NULL)
{
return false;
}
}
if (FreeImage_GetBPP (aTmpBitmap) != 24)
{
FIBITMAP* aTmpBitmap24 = FreeImage_ConvertTo24Bits (aTmpBitmap);
if (aTmpBitmap != myLibImage)
{
FreeImage_Unload (aTmpBitmap);
}
if (aTmpBitmap24 == NULL)
{
return false;
}
aTmpBitmap = aTmpBitmap24;
}
// need conversion to image with palette (requires 24bit bitmap)
anImageToDump = FreeImage_ColorQuantize (aTmpBitmap, FIQ_NNQUANT);
if (aTmpBitmap != myLibImage)
{
FreeImage_Unload (aTmpBitmap);
}
break;
}
case FIF_HDR:
case FIF_EXR:
{
if (Format() == Image_Format_Gray
|| Format() == Image_Format_Alpha)
{
anImageToDump = FreeImage_ConvertToType (myLibImage, FIT_FLOAT);
}
else if (Format() == Image_Format_RGBA
|| Format() == Image_Format_BGRA)
{
anImageToDump = FreeImage_ConvertToType (myLibImage, FIT_RGBAF);
}
else
{
FREE_IMAGE_TYPE aImgTypeFI = FreeImage_GetImageType (myLibImage);
if (aImgTypeFI != FIT_RGBF
&& aImgTypeFI != FIT_RGBAF
&& aImgTypeFI != FIT_FLOAT)
{
anImageToDump = FreeImage_ConvertToType (myLibImage, FIT_RGBF);
}
}
break;
}
default:
{
if (FreeImage_GetImageType (myLibImage) != FIT_BITMAP)
{
anImageToDump = FreeImage_ConvertToType (myLibImage, FIT_BITMAP);
if (anImageToDump == NULL)
{
return false;
}
}
if (FreeImage_GetBPP (anImageToDump) != 24)
{
FIBITMAP* aTmpBitmap24 = FreeImage_ConvertTo24Bits (anImageToDump);
if (anImageToDump != myLibImage)
{
FreeImage_Unload (anImageToDump);
}
if (aTmpBitmap24 == NULL)
{
return false;
}
anImageToDump = aTmpBitmap24;
}
break;
}
}
if (anImageToDump == NULL)
{
return false;
}
#ifdef _WIN32
bool isSaved = (FreeImage_SaveU (anImageFormat, anImageToDump, aFileNameW.ToWideString()) != FALSE);
#else
bool isSaved = (FreeImage_Save (anImageFormat, anImageToDump, theFileName.ToCString()) != FALSE);
#endif
if (anImageToDump != myLibImage)
{
FreeImage_Unload (anImageToDump);
}
return isSaved;
#elif defined(HAVE_WINCODEC)
TCollection_AsciiString aFileNameLower = theFileName;
aFileNameLower.LowerCase();
GUID aFileFormat = getNullGuid();
if (aFileNameLower.EndsWith (".ppm"))
{
return savePPM (theFileName);
}
else if (aFileNameLower.EndsWith (".bmp"))
{
aFileFormat = GUID_ContainerFormatBmp;
}
else if (aFileNameLower.EndsWith (".png"))
{
aFileFormat = GUID_ContainerFormatPng;
}
else if (aFileNameLower.EndsWith (".jpg")
|| aFileNameLower.EndsWith (".jpeg"))
{
aFileFormat = GUID_ContainerFormatJpeg;
}
else if (aFileNameLower.EndsWith (".tiff"))
{
aFileFormat = GUID_ContainerFormatTiff;
}
else if (aFileNameLower.EndsWith (".gif"))
{
aFileFormat = GUID_ContainerFormatGif;
}
if (aFileFormat == getNullGuid())
{
Message::SendFail ("Error: unsupported image format");
return false;
}
Image_ComPtr<IWICImagingFactory> aWicImgFactory;
CoInitializeEx (NULL, COINIT_MULTITHREADED);
if (CoCreateInstance (CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&aWicImgFactory.ChangePtr())) != S_OK)
{
Message::SendFail ("Error: cannot initialize WIC Imaging Factory");
return false;
}
Image_ComPtr<IWICStream> aWicFileStream;
Image_ComPtr<IWICBitmapEncoder> aWicEncoder;
const TCollection_ExtendedString aFileNameW (theFileName);
if (aWicImgFactory->CreateStream (&aWicFileStream.ChangePtr()) != S_OK
|| aWicFileStream->InitializeFromFilename (aFileNameW.ToWideString(), GENERIC_WRITE) != S_OK)
{
Message::SendFail ("Error: cannot create WIC File Stream");
return false;
}
if (aWicImgFactory->CreateEncoder (aFileFormat, NULL, &aWicEncoder.ChangePtr()) != S_OK
|| aWicEncoder->Initialize (aWicFileStream.get(), WICBitmapEncoderNoCache) != S_OK)
{
Message::SendFail ("Error: cannot create WIC Encoder");
return false;
}
const WICPixelFormatGUID aWicPixelFormat = convertToWicFormat (myImgFormat);
if (aWicPixelFormat == getNullGuid())
{
Message::SendFail ("Error: unsupported pixel format");
return false;
}
WICPixelFormatGUID aWicPixelFormatRes = aWicPixelFormat;
Image_ComPtr<IWICBitmapFrameEncode> aWicFrameEncode;
if (aWicEncoder->CreateNewFrame (&aWicFrameEncode.ChangePtr(), NULL) != S_OK
|| aWicFrameEncode->Initialize (NULL) != S_OK
|| aWicFrameEncode->SetSize ((UINT )SizeX(), (UINT )SizeY()) != S_OK
|| aWicFrameEncode->SetPixelFormat (&aWicPixelFormatRes) != S_OK)
{
Message::SendFail ("Error: cannot create WIC Frame");
return false;
}
if (aWicPixelFormatRes != aWicPixelFormat)
{
Message::SendFail ("Error: pixel format is unsupported by image format");
return false;
}
if (IsTopDown())
{
if (aWicFrameEncode->WritePixels ((UINT )SizeY(), (UINT )SizeRowBytes(), (UINT )SizeBytes(), (BYTE* )Data()) != S_OK)
{
Message::SendFail ("Error: cannot write pixels to WIC Frame");
return false;
}
}
else
{
for (Standard_Size aRow = 0; aRow < SizeY(); ++aRow)
{
if (aWicFrameEncode->WritePixels (1, (UINT )SizeRowBytes(), (UINT )SizeRowBytes(), (BYTE* )Row (aRow)) != S_OK)
{
Message::SendFail ("Error: cannot write pixels to WIC Frame");
return false;
}
}
}
if (aWicFrameEncode->Commit() != S_OK
|| aWicEncoder->Commit() != S_OK)
{
Message::SendFail ("Error: cannot commit data to WIC Frame");
return false;
}
if (aWicFileStream->Commit (STGC_DEFAULT) != S_OK)
{
//Message::Send ("Error: cannot commit data to WIC File Stream", Message_Fail);
//return false;
}
return true;
#else
const Standard_Integer aLen = theFileName.Length();
if ((aLen >= 4) && (theFileName.Value (aLen - 3) == '.')
&& strcasecmp( theFileName.ToCString() + aLen - 3, "ppm") == 0 )
{
return savePPM (theFileName);
}
Message::SendTrace ("Image_PixMap, no image library available! Image saved in PPM format");
return savePPM (theFileName);
#endif
}
// =======================================================================
// function : AdjustGamma
// purpose :
// =======================================================================
bool Image_AlienPixMap::AdjustGamma (const Standard_Real theGammaCorr)
{
#ifdef HAVE_FREEIMAGE
return FreeImage_AdjustGamma (myLibImage, theGammaCorr) != FALSE;
#else
(void )theGammaCorr;
return false;
#endif
}
|