| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359 |
- #include "BsFreeImgImporter.h"
- #include "BsResource.h"
- #include "BsDebug.h"
- #include "BsDataStream.h"
- #include "BsTextureManager.h"
- #include "BsTexture.h"
- #include "BsTextureImportOptions.h"
- #include "BsFileSystem.h"
- #include "BsCoreApplication.h"
- #include "BsCoreThread.h"
- #include "BsCoreThreadAccessor.h"
- #include "FreeImage.h"
- using namespace std::placeholders;
- namespace BansheeEngine
- {
- void FreeImageLoadErrorHandler(FREE_IMAGE_FORMAT fif, const char *message)
- {
- // Callback method as required by FreeImage to report problems
- const char* typeName = FreeImage_GetFormatFromFIF(fif);
- if (typeName)
- {
- gDebug().logError("FreeImage error: '" + String(message) + "' when loading format " + typeName);
- }
- else
- {
- gDebug().logError("FreeImage error: '" + String(message) + "'");
- }
- }
- FreeImgImporter::FreeImgImporter()
- :SpecificImporter()
- {
- FreeImage_Initialise(false);
- // Register codecs
- WStringStream strExt;
- strExt << "Supported formats: ";
- bool first = true;
- for (int i = 0; i < FreeImage_GetFIFCount(); ++i)
- {
- // Skip DDS codec since FreeImage does not have the option
- // to keep DXT data compressed, we'll use our own codec
- if ((FREE_IMAGE_FORMAT)i == FIF_DDS)
- continue;
- WString exts = toWString(String(FreeImage_GetFIFExtensionList((FREE_IMAGE_FORMAT)i)));
- if (!first)
- {
- strExt << ",";
- }
- first = false;
- strExt << exts;
- // Pull off individual formats (separated by comma by FI)
- Vector<WString> extsVector = StringUtil::split(exts, L",");
- for (auto v = extsVector.begin(); v != extsVector.end(); ++v)
- {
- auto findIter = std::find(mExtensions.begin(), mExtensions.end(), *v);
- if(findIter == mExtensions.end())
- {
- WString ext = *v;
- StringUtil::toLowerCase(ext);
- mExtensionToFID.insert(std::make_pair(ext, i));
- mExtensions.push_back(ext);
- }
- }
- }
- // Set error handler
- FreeImage_SetOutputMessage(FreeImageLoadErrorHandler);
- }
- FreeImgImporter::~FreeImgImporter()
- {
- FreeImage_DeInitialise();
- }
- bool FreeImgImporter::isExtensionSupported(const WString& ext) const
- {
- WString lowerCaseExt = ext;
- StringUtil::toLowerCase(lowerCaseExt);
- return find(mExtensions.begin(), mExtensions.end(), lowerCaseExt) != mExtensions.end();
- }
- bool FreeImgImporter::isMagicNumberSupported(const UINT8* magicNumPtr, UINT32 numBytes) const
- {
- WString ext = magicNumToExtension(magicNumPtr, numBytes);
- return isExtensionSupported(ext);
- }
- WString FreeImgImporter::magicNumToExtension(const UINT8* magic, UINT32 maxBytes) const
- {
- // Set error handler
- FreeImage_SetOutputMessage(FreeImageLoadErrorHandler);
- FIMEMORY* fiMem =
- FreeImage_OpenMemory((BYTE*)magic, static_cast<DWORD>(maxBytes));
- FREE_IMAGE_FORMAT fif = FreeImage_GetFileTypeFromMemory(fiMem, (int)maxBytes);
- FreeImage_CloseMemory(fiMem);
- if (fif != FIF_UNKNOWN)
- {
- WString ext = toWString(String(FreeImage_GetFormatFromFIF(fif)));
- StringUtil::toLowerCase(ext);
- return ext;
- }
- else
- {
- return StringUtil::WBLANK;
- }
- }
- ImportOptionsPtr FreeImgImporter::createImportOptions() const
- {
- return bs_shared_ptr_new<TextureImportOptions>();
- }
- ResourcePtr FreeImgImporter::import(const Path& filePath, ConstImportOptionsPtr importOptions)
- {
- const TextureImportOptions* textureImportOptions = static_cast<const TextureImportOptions*>(importOptions.get());
- DataStreamPtr fileData = FileSystem::openFile(filePath, true);
- PixelDataPtr imgData = importRawImage(fileData);
- if(imgData == nullptr || imgData->getData() == nullptr)
- return nullptr;
- UINT32 numMips = 0;
- if (textureImportOptions->getGenerateMipmaps())
- {
- UINT32 maxPossibleMip = PixelUtil::getMaxMipmaps(imgData->getWidth(), imgData->getHeight(), imgData->getDepth(), imgData->getFormat());
- if (textureImportOptions->getMaxMip() == 0)
- {
- numMips = maxPossibleMip;
- }
- else
- {
- numMips = std::min(maxPossibleMip, textureImportOptions->getMaxMip());
- }
- }
- int usage = TU_DEFAULT;
- if (textureImportOptions->getCPUReadable())
- usage |= TU_CPUCACHED;
- bool sRGB = textureImportOptions->getSRGB();
- TexturePtr newTexture = Texture::_createPtr(TEX_TYPE_2D,
- imgData->getWidth(), imgData->getHeight(), numMips, textureImportOptions->getFormat(), usage, sRGB);
- Vector<PixelDataPtr> mipLevels;
- if (numMips > 0)
- mipLevels = PixelUtil::genMipmaps(*imgData, MipMapGenOptions());
- else
- mipLevels.insert(mipLevels.begin(), imgData);
- for (UINT32 mip = 0; mip < (UINT32)mipLevels.size(); ++mip)
- {
- UINT32 subresourceIdx = newTexture->getProperties().mapToSubresourceIdx(0, mip);
- PixelDataPtr dst = newTexture->getProperties().allocateSubresourceBuffer(subresourceIdx);
- PixelUtil::bulkPixelConversion(*mipLevels[mip], *dst);
- newTexture->writeSubresource(gCoreAccessor(), subresourceIdx, dst, false);
- }
- fileData->close();
- WString fileName = filePath.getWFilename(false);
- newTexture->setName(fileName);
- return newTexture;
- }
- PixelDataPtr FreeImgImporter::importRawImage(DataStreamPtr fileData)
- {
- if(fileData->size() > std::numeric_limits<UINT32>::max())
- {
- BS_EXCEPT(InternalErrorException, "File size larger than supported!");
- }
- UINT32 magicLen = std::min((UINT32)fileData->size(), 32u);
- UINT8 magicBuf[32];
- fileData->read(magicBuf, magicLen);
- fileData->seek(0);
- WString fileExtension = magicNumToExtension(magicBuf, magicLen);
- auto findFormat = mExtensionToFID.find(fileExtension);
- if(findFormat == mExtensionToFID.end())
- {
- BS_EXCEPT(InvalidParametersException, "Type of the file provided is not supported by this importer. File type: " + toString(fileExtension));
- }
- FREE_IMAGE_FORMAT imageFormat = (FREE_IMAGE_FORMAT)findFormat->second;
- // Set error handler
- FreeImage_SetOutputMessage(FreeImageLoadErrorHandler);
- // Buffer stream into memory (TODO: override IO functions instead?)
- MemoryDataStream memStream(fileData);
- fileData->close();
- FIMEMORY* fiMem = FreeImage_OpenMemory(memStream.getPtr(), static_cast<DWORD>(memStream.size()));
- FIBITMAP* fiBitmap = FreeImage_LoadFromMemory(
- (FREE_IMAGE_FORMAT)imageFormat, fiMem);
- if (!fiBitmap)
- {
- BS_EXCEPT(InternalErrorException, "Error decoding image");
- }
- UINT32 width = FreeImage_GetWidth(fiBitmap);
- UINT32 height = FreeImage_GetHeight(fiBitmap);
- PixelFormat format = PF_UNKNOWN;
- // Must derive format first, this may perform conversions
- FREE_IMAGE_TYPE imageType = FreeImage_GetImageType(fiBitmap);
- FREE_IMAGE_COLOR_TYPE colourType = FreeImage_GetColorType(fiBitmap);
- unsigned bpp = FreeImage_GetBPP(fiBitmap);
- switch(imageType)
- {
- case FIT_UNKNOWN:
- case FIT_COMPLEX:
- case FIT_UINT32:
- case FIT_INT32:
- case FIT_DOUBLE:
- default:
- BS_EXCEPT(InternalErrorException, "Unknown or unsupported image format");
- break;
- case FIT_BITMAP:
- // Standard image type
- // Perform any colour conversions for greyscale
- if (colourType == FIC_MINISWHITE || colourType == FIC_MINISBLACK)
- {
- FIBITMAP* newBitmap = FreeImage_ConvertToGreyscale(fiBitmap);
- // free old bitmap and replace
- FreeImage_Unload(fiBitmap);
- fiBitmap = newBitmap;
- // get new formats
- bpp = FreeImage_GetBPP(fiBitmap);
- colourType = FreeImage_GetColorType(fiBitmap);
- }
- // Perform any colour conversions for RGB
- else if (bpp < 8 || colourType == FIC_PALETTE || colourType == FIC_CMYK)
- {
- FIBITMAP* newBitmap = FreeImage_ConvertTo24Bits(fiBitmap);
- // free old bitmap and replace
- FreeImage_Unload(fiBitmap);
- fiBitmap = newBitmap;
- // get new formats
- bpp = FreeImage_GetBPP(fiBitmap);
- colourType = FreeImage_GetColorType(fiBitmap);
- }
- // by this stage, 8-bit is greyscale, 16/24/32 bit are RGB[A]
- switch(bpp)
- {
- case 8:
- format = PF_R8;
- break;
- case 16:
- // Determine 555 or 565 from green mask
- // cannot be 16-bit greyscale since that's FIT_UINT16
- if(FreeImage_GetGreenMask(fiBitmap) == FI16_565_GREEN_MASK)
- {
- assert(false && "Format not supported by the engine. TODO.");
- return nullptr;
- }
- else
- {
- assert(false && "Format not supported by the engine. TODO.");
- return nullptr;
- // FreeImage doesn't support 4444 format so must be 1555
- }
- break;
- case 24:
- // FreeImage differs per platform
- // PF_BYTE_BGR[A] for little endian (== PF_ARGB native)
- // PF_BYTE_RGB[A] for big endian (== PF_RGBA native)
- #if FREEIMAGE_COLORORDER == FREEIMAGE_COLORORDER_RGB
- format = PF_BYTE_RGB;
- #else
- format = PF_BYTE_BGR;
- #endif
- break;
- case 32:
- #if FREEIMAGE_COLORORDER == FREEIMAGE_COLORORDER_RGB
- format = PF_BYTE_RGBA;
- #else
- format = PF_BYTE_BGRA;
- #endif
- break;
- };
- break;
- case FIT_UINT16:
- case FIT_INT16:
- // 16-bit greyscale
- assert(false && "No INT pixel formats supported currently. TODO.");
- return nullptr;
- break;
- case FIT_FLOAT:
- // Single-component floating point data
- format = PF_FLOAT32_R;
- break;
- case FIT_RGB16:
- format = PF_FLOAT16_RGB;
- break;
- case FIT_RGBA16:
- format = PF_FLOAT16_RGBA;
- break;
- case FIT_RGBF:
- format = PF_FLOAT32_RGB;
- break;
- case FIT_RGBAF:
- format = PF_FLOAT32_RGBA;
- break;
- };
- unsigned char* srcData = FreeImage_GetBits(fiBitmap);
- unsigned srcPitch = FreeImage_GetPitch(fiBitmap);
- // Final data - invert image and trim pitch at the same time
- UINT32 dstPitch = width * PixelUtil::getNumElemBytes(format);
- UINT32 size = dstPitch * height;
- // Bind output buffer
- PixelDataPtr texData = bs_shared_ptr_new<PixelData>(width, height, 1, format);
- texData->allocateInternalBuffer();
- UINT8* output = texData->getData();
- UINT8* pSrc;
- UINT8* pDst = output;
- for (UINT32 y = 0; y < height; ++y)
- {
- pSrc = srcData + (height - y - 1) * srcPitch;
- memcpy(pDst, pSrc, dstPitch);
- pDst += dstPitch;
- }
- FreeImage_Unload(fiBitmap);
- FreeImage_CloseMemory(fiMem);
- return texData;
- }
- }
|