diff --git a/src/loaders/jpg/tvgJpgLoader.cpp b/src/loaders/jpg/tvgJpgLoader.cpp index e9daa9db..d92ada1c 100644 --- a/src/loaders/jpg/tvgJpgLoader.cpp +++ b/src/loaders/jpg/tvgJpgLoader.cpp @@ -21,6 +21,7 @@ */ #include +#include #include "tvgLoaderMgr.h" #include "tvgJpgLoader.h" @@ -30,10 +31,10 @@ void JpgLoader::clear() { - tjFree(data); - data = NULL; - pointer = NULL; + if (freeData) free(data); + data = nullptr; size = 0; + freeData = false; } /************************************************************************/ @@ -45,34 +46,41 @@ JpgLoader::JpgLoader() jpegDecompressor = tjInitDecompress(); } + JpgLoader::~JpgLoader() { - if (data) tjFree(data); + if (freeData) free(data); tjDestroy(jpegDecompressor); + + //This image is shared with raster engine. tjFree(image); - image = NULL; } + bool JpgLoader::open(const string& path) { clear(); - bool success = false; - FILE *jpegFile = NULL; - if ((jpegFile = fopen(path.c_str(), "rb")) == NULL) return false; + auto jpegFile = fopen(path.c_str(), "rb"); + if (!jpegFile) return false; + + auto ret = false; //determine size if (fseek(jpegFile, 0, SEEK_END) < 0) goto finalize; if (((size = ftell(jpegFile)) < 1)) goto finalize; if (fseek(jpegFile, 0, SEEK_SET)) goto finalize; - data = (unsigned char *) tjAlloc(size); + data = (unsigned char *) malloc(size); if (!data) goto finalize; if (fread(data, size, 1, jpegFile) < 1) goto failure; - pointer = data; - success = true; + //FIXME: Decompress header here + + ret = true; + freeData = true; + goto finalize; failure: @@ -80,9 +88,10 @@ failure: finalize: fclose(jpegFile); - return success; + return ret; } + bool JpgLoader::open(const char* data, uint32_t size, bool copy) { clear(); @@ -91,24 +100,26 @@ bool JpgLoader::open(const char* data, uint32_t size, bool copy) this->data = (unsigned char *) malloc(size); if (!this->data) return false; memcpy((unsigned char *)this->data, data, size); - this->pointer = this->data; + freeData = true; } else { - this->pointer = (unsigned char *)data; + this->data = (unsigned char *) data; } this->size = size; + + //FIXME: Decompress header here + return true; } + bool JpgLoader::read() { - int width, height; - //decompress header + int width, height; int inSubsamp, inColorspace; if (tjDecompressHeader3(jpegDecompressor, data, size, &width, &height, &inSubsamp, &inColorspace) < 0) return false; - //alloc image buffer if (image) tjFree(image); image = (unsigned char *)tjAlloc(width * height * tjPixelSize[TJPF_BGRX]); if (!image) return false; @@ -116,22 +127,25 @@ bool JpgLoader::read() //decompress jpg image if (tjDecompress2(jpegDecompressor, data, size, image, width, 0, height, TJPF_BGRX, 0) < 0) { tjFree(image); - image = NULL; + image = nullptr; return false; } vw = w = width; vh = h = height; + return true; } + bool JpgLoader::close() { clear(); return true; } + const uint32_t* JpgLoader::pixels() { - return (const uint32_t*)image; + return (const uint32_t*) image; } diff --git a/src/loaders/jpg/tvgJpgLoader.h b/src/loaders/jpg/tvgJpgLoader.h index 1b03b207..0063da2f 100644 --- a/src/loaders/jpg/tvgJpgLoader.h +++ b/src/loaders/jpg/tvgJpgLoader.h @@ -22,8 +22,9 @@ #ifndef _TVG_JPG_LOADER_H_ #define _TVG_JPG_LOADER_H_ -#include +using tjhandle = void*; +//TODO: Use Task? class JpgLoader : public Loader { public: @@ -43,9 +44,9 @@ private: tjhandle jpegDecompressor; unsigned char* data = nullptr; - const unsigned char* pointer = nullptr; - unsigned long size = 0; unsigned char *image = nullptr; + unsigned long size = 0; + bool freeData = false; }; #endif //_TVG_JPG_LOADER_H_