123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552 |
- //-----------------------------------------------------------------------------
- // Copyright (c) 2012 GarageGames, LLC
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to
- // deal in the Software without restriction, including without limitation the
- // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
- // sell copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
- // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
- // IN THE SOFTWARE.
- //-----------------------------------------------------------------------------
- #include "platform/platform.h"
- #include "console/console.h"
- #include "core/stream/fileStream.h"
- #include "core/stream/memStream.h"
- #include "core/strings/stringFunctions.h"
- #include "gfx/bitmap/gBitmap.h"
- #include "gfx/bitmap/imageUtils.h"
- #include "gfx/bitmap/loaders/ies/ies_loader.h"
- #ifdef __clang__
- #define STBIWDEF static inline
- #endif
- #pragma warning( push )
- #pragma warning( disable : 4505 ) // unreferenced function removed.
- #ifndef STB_IMAGE_IMPLEMENTATION
- #define STB_IMAGE_IMPLEMENTATION
- #define STB_IMAGE_STATIC
- #include "stb_image.h"
- #endif
- #define STB_IMAGE_WRITE_IMPLEMENTATION
- #define STB_IMAGE_WRITE_STATIC
- #include "stb_image_write.h"
- #pragma warning(pop)
- static bool sReadSTB(const Torque::Path& path, GBitmap* bitmap);
- static bool sReadStreamSTB(Stream& stream, GBitmap* bitmap, U32 len);
- static bool sWriteSTB(const Torque::Path& path, GBitmap* bitmap, U32 compressionLevel);
- static bool sWriteStreamSTB(const String& bmType, Stream& stream, GBitmap* bitmap, U32 compressionLevel);
- // stbi_write callback / rextimmy.
- static void stbiWriteFunc(void* context, void* data, int size)
- {
- Stream* stream = static_cast<Stream*>(context);
- stream->write(size);
- stream->write(size, data);
- }
- static struct _privateRegisterSTB
- {
- _privateRegisterSTB()
- {
- GBitmap::Registration reg;
- reg.priority = 100;
- reg.extensions.push_back("png");
- reg.extensions.push_back("bmp");
- reg.extensions.push_back("jpg");
- reg.extensions.push_back("jpeg");
- reg.extensions.push_back("psd");
- reg.extensions.push_back("hdr");
- reg.extensions.push_back("tga");
- reg.extensions.push_back("ies");
- reg.readFunc = sReadSTB;
- reg.readStreamFunc = sReadStreamSTB;
- reg.writeFunc = sWriteSTB;
- reg.writeStreamFunc = sWriteStreamSTB;
- // for png only.
- reg.defaultCompression = 6;
- GBitmap::sRegisterFormat(reg);
- }
- } sStaticRegisterSTB;
- bool sReadSTB(const Torque::Path& path, GBitmap* bitmap)
- {
- PROFILE_SCOPE(sReadSTB);
- S32 x, y, n, channels;
- String ext = path.getExtension();
- // if this is an ies profile we need to create a texture for it.
- if (ext.equal("ies"))
- {
- String textureName = path.getFullPath();
- textureName.replace(".ies", ".png");
- x = 256;
- y = 1;
- n = 4;
- channels = 4;
- GFXFormat format = GFXFormatR8G8B8A8;
- if (Torque::FS::IsFile(textureName.c_str()))
- {
- // if the txture already exist, load it.
- unsigned char* data = stbi_load(textureName.c_str(), &x, &y, &n, channels);
- // actually allocate the bitmap space...
- bitmap->allocateBitmap(x, y,
- false, // don't extrude miplevels...
- format); // use determined format...
- U8* pBase = (U8*)bitmap->getBits();
- U32 rowBytes = bitmap->getByteSize();
- dMemcpy(pBase, data, rowBytes);
- stbi_image_free(data);
- return true;
- }
- else
- {
- FileStream* readIes = new FileStream;
- if (!readIes->open(path.getFullPath(), Torque::FS::File::Read))
- {
- Con::printf("Failed to open IES profile:%s", path.getFullFileName().c_str());
- return false;
- }
- if (readIes->getStatus() != Stream::Ok)
- {
- Con::printf("Failed to open IES profile:%s", path.getFullFileName().c_str());
- return false;
- }
- U32 buffSize = readIes->getStreamSize();
- char* buffer = new char[buffSize];
- readIes->read(buffSize, buffer);
- IESFileInfo info;
- IESLoadHelper IESLoader;
- if (!IESLoader.load(buffer, buffSize, info))
- {
- Con::printf("Failed to load IES profile:%s \n LoaderError: %s", path.getFullFileName().c_str(), info.error().c_str());
- return false;
- }
- float* data = new float[x*y*channels];
- if (!IESLoader.saveAs1D(info, data, x, channels))
- {
- Con::printf("Failed to create 2d Texture for IES profile:%s", path.getFullFileName().c_str());
- return false;
- }
- // use stb function to convert float data to uchar
- unsigned char* dataChar = stbi__hdr_to_ldr(data, x, y, channels);
- bitmap->deleteImage();
- // actually allocate the bitmap space...
- bitmap->allocateBitmap(x, y,
- false,
- format);
- U8* pBase = (U8*)bitmap->getBits();
- U32 rowBytes = x * y * channels;
- dMemcpy(pBase, dataChar, rowBytes);
- stbi_image_free(dataChar);
- sWriteSTB(textureName, bitmap, 10);
- return true;
- }
- }
- if (!stbi_info(path.getFullPath().c_str(), &x, &y, &channels))
- {
- const char* stbErr = stbi_failure_reason();
- if (!stbErr)
- stbErr = "Unknown Error!";
- Con::errorf("STB get file info: %s", stbErr);
- }
- // do this to map 2 channels to 4, 2 channel not supported by gbitmap yet..
- if (channels == 2)
- channels = 4;
- if (!ext.equal("png"))
- {
- if (stbi_is_16_bit(path.getFullPath().c_str()))
- {
- U16* data = stbi_load_16(path.getFullPath().c_str(), &x, &y, &n, channels);
- // if succesful deal make the bitmap, else try other loaders.
- if (data)
- {
- GFXFormat format;
- if (n == 1)
- format = GFXFormatL16;
- else
- format = GFXFormatR16G16B16A16; // not sure if this is correct.
- bitmap->deleteImage();
- // actually allocate the bitmap space...
- bitmap->allocateBitmap(x, y,
- false, // don't extrude miplevels...
- format); // use determined format...
- U16* pBase = (U16*)bitmap->getBits();
- U32 rowBytes = bitmap->getByteSize();
- dMemcpy(pBase, data, rowBytes);
- stbi_image_free(data);
- return true;
- }
- }
- }
- if (ext.equal("hdr"))
- {
- // force load to 4 channel.
- float* data = stbi_loadf(path.getFullPath().c_str(), &x, &y, &n, 0);
- unsigned char* dataChar = stbi__hdr_to_ldr(data, x, y, n);
- bitmap->deleteImage();
- // actually allocate the bitmap space...
- bitmap->allocateBitmap(x, y,
- false,
- GFXFormatR8G8B8);
- U8* pBase = (U8*)bitmap->getBits();
- U32 rowBytes = x * y * n;
- dMemcpy(pBase, dataChar, rowBytes);
- //stbi_image_free(data);
- stbi_image_free(dataChar);
- return true;
- }
- unsigned char* data = stbi_load(path.getFullPath().c_str(), &x, &y, &n, channels);
- bitmap->deleteImage();
- GFXFormat format;
- switch (channels) {
- case 1:
- format = GFXFormatA8;
- break;
- case 2:
- format = GFXFormatA8L8;
- break;
- case 3:
- format = GFXFormatR8G8B8;
- break;
- case 4:
- format = GFXFormatR8G8B8A8;
- break;
- default:
- return false;
- }
- // actually allocate the bitmap space...
- bitmap->allocateBitmap(x, y,
- false, // don't extrude miplevels...
- format); // use determined format...
- U8* pBase = (U8*)bitmap->getBits();
- U32 rowBytes = bitmap->getByteSize();
- dMemcpy(pBase, data, rowBytes);
- stbi_image_free(data);
- // Check this bitmap for transparency
- if (channels == 4)
- bitmap->checkForTransparency();
- return true;
- }
- bool sReadStreamSTB(Stream& stream, GBitmap* bitmap, U32 len)
- {
- PROFILE_SCOPE(sReadStreamSTB);
- // only used for font at the moment.
- U8* data = new U8[len];
- stream.read(len, data);
- S32 width, height, comp = 0;
- unsigned char* pixelData = stbi_load_from_memory((const U8*)data, (int)len, &width, &height, &comp, 0);
- if (!pixelData)
- {
- const char* stbErr = stbi_failure_reason();
- if (!stbErr)
- stbErr = "Unknown Error!";
- Con::errorf("sReadStreamSTB Error: %s", stbErr);
- return false;
- }
- bitmap->deleteImage();
- //work out what format we need to use - todo floating point?
- GFXFormat fmt = GFXFormat_FIRST;
- switch (comp)
- {
- case 1: fmt = GFXFormatA8; break;
- case 2: fmt = GFXFormatA8L8; break; //todo check this
- case 3: fmt = GFXFormatR8G8B8; break;
- case 4: fmt = GFXFormatR8G8B8A8; break;
- }
- bitmap->allocateBitmap(width, height, false, fmt);
- U8* pBase = bitmap->getWritableBits(0);
- U32 rowBytes = bitmap->getByteSize();
- dMemcpy(pBase, pixelData, rowBytes);
- dFree(data);
- dFree(pixelData);
- return true;
- }
- /**
- * Write bitmap to an image file.
- *
- * @param[in] path Destination image file path. File name extension determines image format.
- * ".bmp" for Microsoft Bitmap.
- * ".hdr" for High Dynamic Range (HDR).
- * ".jpg" or ".jpeg" for Joint Photographic Experts Group (JPEG).
- * ".png" for Portable Network Graphics (PNG).
- * ".tga" for Truevision TGA (TARGA).
- *
- *
- * @param[in] bitmap Source bitmap to encode image from.
- * @param compressionLevel Image format specific compression level.
- * For JPEG sets the quality level percentage, range 0 to 100.
- * Not used for other image formats.
- */
- bool sWriteSTB(const Torque::Path& path, GBitmap* bitmap, U32 compressionLevel)
- {
- PROFILE_SCOPE(sWriteSTB);
- // get our data to be saved.
- U32 width = bitmap->getWidth();
- U32 height = bitmap->getHeight();
- U32 bytes = bitmap->getBytesPerPixel();
- GFXFormat format = bitmap->getFormat();
- String ext = path.getExtension();
- // we always have at least 1
- U32 comp = 1;
- if (format == GFXFormatR8G8B8)
- {
- comp = 3;
- }
- else if (format == GFXFormatR8G8B8A8 || format == GFXFormatR8G8B8X8 || format == GFXFormatR8G8B8A8_LINEAR_FORCE)
- {
- comp = 4;
- }
- if (ext.equal("png"))
- {
- stbi_write_png_compression_level = compressionLevel;
- if (stbi_write_png(path.getFullPath().c_str(), width, height, comp, bitmap->getWritableBits(), 0))
- return true;
- }
- if (ext.equal("tga"))
- {
- if (stbi_write_tga(path.getFullPath().c_str(), width, height, comp, bitmap->getWritableBits()))
- return true;
- }
- if (ext.equal("bmp"))
- {
- if (stbi_write_bmp(path.getFullPath().c_str(), width, height, comp, bitmap->getWritableBits()))
- return true;
- }
- if (ext.equal("jpg") || ext.equal("jpeg"))
- {
- if (stbi_write_jpg(path.getFullPath().c_str(), width, height, comp, bitmap->getWritableBits(), compressionLevel))
- return true;
- }
- if (ext.equal("hdr"))
- {
- if (stbi_write_hdr(path.getFullPath().c_str(), width, height, comp, (const F32*)bitmap->getWritableBits()))
- return true;
- }
- return false;
- }
- bool sWriteStreamSTB(const String& bmType, Stream& stream, GBitmap* bitmap, U32 compressionLevel)
- {
- PROFILE_SCOPE(sWriteStreamSTB);
- S32 width = bitmap->getWidth();
- S32 height = bitmap->getHeight();
- const U8* pPixelData = bitmap->getBits();
- S32 channels = bitmap->getBytesPerPixel();
- if (bmType == String("png"))
- {
- stbi_write_png_compression_level = compressionLevel;
- if (stbi_write_png_to_func(stbiWriteFunc, &stream, width, height, channels, pPixelData, width * channels))
- return true;
- }
- else if (bmType == String("tga"))
- {
- if (stbi_write_tga_to_func(stbiWriteFunc, &stream, width, height, channels, pPixelData))
- return true;
- }
- else if (bmType == String("bmp"))
- {
- if (stbi_write_bmp_to_func(stbiWriteFunc, &stream, width, height, channels, pPixelData))
- return true;
- }
- else if (bmType == String("jpg") || bmType == String("jpeg"))
- {
- if (stbi_write_jpg_to_func(stbiWriteFunc, &stream, width, height, channels, pPixelData, compressionLevel))
- return true;
- }
- else if (bmType == String("hdr"))
- {
- if (stbi_write_hdr_to_func(stbiWriteFunc, &stream, width, height, channels, (const F32*)pPixelData))
- return true;
- }
- return false;
- }
- struct DeferredPNGWriterData
- {
- S32 width = 0;
- S32 height = 0;
- S32 channels = 0;
- dsize_t offset = 0;
- U8* pPixelData = NULL;
- GFXFormat format;
- Stream* pStream = NULL;
- };
- DeferredPNGWriter::DeferredPNGWriter() :
- mData(NULL),
- mActive(false)
- {
- mData = new DeferredPNGWriterData();
- }
- DeferredPNGWriter::~DeferredPNGWriter()
- {
- if (mData)
- {
- SAFE_DELETE_ARRAY(mData->pPixelData);
- }
- SAFE_DELETE(mData);
- }
- bool DeferredPNGWriter::begin(GFXFormat format, S32 width, S32 height, Stream& stream)
- {
- // ONLY RGB bitmap writing supported at this time!
- AssertFatal(format == GFXFormatR8G8B8 ||
- format == GFXFormatR8G8B8A8 ||
- format == GFXFormatR8G8B8X8 ||
- format == GFXFormatA8 ||
- format == GFXFormatR5G6B5, "DeferredPNGWriter::begin: ONLY RGB bitmap writing supported at this time.");
- if (format != GFXFormatR8G8B8 &&
- format != GFXFormatR8G8B8A8 &&
- format != GFXFormatR8G8B8X8 &&
- format != GFXFormatA8 &&
- format != GFXFormatR5G6B5)
- {
- return false;
- }
- mData->pStream = &stream;
- mData->width = width;
- mData->height = height;
- mData->format = format;
- const size_t dataSize = GFXFormat_getByteSize(format) * width * height;
- mData->pPixelData = new U8[dataSize];
- mActive = true;
- return true;
- }
- void DeferredPNGWriter::append(GBitmap* bitmap, U32 rows)
- {
- AssertFatal(mActive, "Cannot append to an inactive DeferredPNGWriter!");
- if (mData->channels == 0)
- {
- mData->channels = bitmap->getBytesPerPixel();
- }
- const dsize_t dataChuckSize = bitmap->getByteSize();
- const U8* pSrcData = bitmap->getBits();
- U8* pDstData = mData->pPixelData + mData->offset;
- dMemcpy(pDstData, pSrcData, dataChuckSize);
- mData->offset += dataChuckSize;
- }
- void DeferredPNGWriter::end()
- {
- AssertFatal(mActive, "Cannot end an inactive DeferredPNGWriter!");
- stbi_write_png_to_func(stbiWriteFunc, mData->pStream, mData->width, mData->height, mData->channels, mData->pPixelData, mData->width * mData->channels);
- mActive = false;
- }
|