diff --git a/src/loaders/external_jpg/meson.build b/src/loaders/external_jpg/meson.build new file mode 100644 index 00000000..aeecf9f1 --- /dev/null +++ b/src/loaders/external_jpg/meson.build @@ -0,0 +1,18 @@ +source_file = [ + 'tvgJpgLoader.h', + 'tvgJpgLoader.cpp', +] + +jpg_dep = dependency('libturbojpeg', required: false) + +if not jpg_dep.found() + jpg_dep = cc.find_library('turbojpeg', required: false) +endif + +if jpg_dep.found() + subloader_dep += [declare_dependency( + include_directories : include_directories('.'), + dependencies : jpg_dep, + sources : source_file + )] +endif diff --git a/src/loaders/external_jpg/tvgJpgLoader.cpp b/src/loaders/external_jpg/tvgJpgLoader.cpp new file mode 100644 index 00000000..f3327fbd --- /dev/null +++ b/src/loaders/external_jpg/tvgJpgLoader.cpp @@ -0,0 +1,153 @@ +/* + * Copyright (c) 2021 Samsung Electronics Co., Ltd. All rights reserved. + + * 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 +#include +#include "tvgLoader.h" +#include "tvgJpgLoader.h" + +/************************************************************************/ +/* Internal Class Implementation */ +/************************************************************************/ + +void JpgLoader::clear() +{ + if (freeData) free(data); + data = nullptr; + size = 0; + freeData = false; +} + +/************************************************************************/ +/* External Class Implementation */ +/************************************************************************/ + +JpgLoader::JpgLoader() +{ + jpegDecompressor = tjInitDecompress(); +} + + +JpgLoader::~JpgLoader() +{ + if (freeData) free(data); + tjDestroy(jpegDecompressor); + + //This image is shared with raster engine. + tjFree(image); +} + + +bool JpgLoader::open(const string& path) +{ + clear(); + + 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 *) malloc(size); + if (!data) goto finalize; + + if (fread(data, size, 1, jpegFile) < 1) goto failure; + + int width, height, subSample, colorSpace; + if (tjDecompressHeader3(jpegDecompressor, data, size, &width, &height, &subSample, &colorSpace) < 0) { + TVGERR("JPG LOADER", "%s", tjGetErrorStr()); + goto failure; + } + + w = static_cast(width); + h = static_cast(height); + ret = true; + freeData = true; + + goto finalize; + +failure: + clear(); + +finalize: + fclose(jpegFile); + return ret; +} + + +bool JpgLoader::open(const char* data, uint32_t size, bool copy) +{ + clear(); + + int width, height, subSample, colorSpace; + if (tjDecompressHeader3(jpegDecompressor, (unsigned char *) data, size, &width, &height, &subSample, &colorSpace) < 0) return false; + + if (copy) { + this->data = (unsigned char *) malloc(size); + if (!this->data) return false; + memcpy((unsigned char *)this->data, data, size); + freeData = true; + } else { + this->data = (unsigned char *) data; + } + + w = static_cast(width); + h = static_cast(height); + this->size = size; + + return true; +} + + +bool JpgLoader::read() +{ + if (image) tjFree(image); + image = (unsigned char *)tjAlloc(static_cast(w) * static_cast(h) * tjPixelSize[TJPF_BGRX]); + if (!image) return false; + + //decompress jpg image + if (tjDecompress2(jpegDecompressor, data, size, image, static_cast(w), 0, static_cast(h), TJPF_BGRX, 0) < 0) { + TVGERR("JPG LOADER", "%s", tjGetErrorStr()); + tjFree(image); + image = nullptr; + return false; + } + + return true; +} + + +bool JpgLoader::close() +{ + clear(); + return true; +} + + +const uint32_t* JpgLoader::pixels() +{ + return (const uint32_t*) image; +} diff --git a/src/loaders/external_jpg/tvgJpgLoader.h b/src/loaders/external_jpg/tvgJpgLoader.h new file mode 100644 index 00000000..1aeaa04c --- /dev/null +++ b/src/loaders/external_jpg/tvgJpgLoader.h @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2021 Samsung Electronics Co., Ltd. All rights reserved. + + * 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. + */ +#ifndef _TVG_JPG_LOADER_H_ +#define _TVG_JPG_LOADER_H_ + +using tjhandle = void*; + +//TODO: Use Task? +class JpgLoader : public LoadModule +{ +public: + JpgLoader(); + ~JpgLoader(); + + using LoadModule::open; + bool open(const string& path) override; + bool open(const char* data, uint32_t size, bool copy) override; + bool read() override; + bool close() override; + + const uint32_t* pixels() override; + +private: + void clear(); + + tjhandle jpegDecompressor; + unsigned char* data = nullptr; + unsigned char *image = nullptr; + unsigned long size = 0; + bool freeData = false; +}; + +#endif //_TVG_JPG_LOADER_H_ diff --git a/src/loaders/external_png/meson.build b/src/loaders/external_png/meson.build new file mode 100644 index 00000000..61a386f7 --- /dev/null +++ b/src/loaders/external_png/meson.build @@ -0,0 +1,14 @@ +source_file = [ + 'tvgPngLoader.h', + 'tvgPngLoader.cpp', +] + +png_dep = meson.get_compiler('cpp').find_library('libpng') + +if png_dep.found() + subloader_dep += [declare_dependency( + include_directories : include_directories('.'), + dependencies : png_dep, + sources : source_file + )] +endif diff --git a/src/loaders/external_png/tvgPngLoader.cpp b/src/loaders/external_png/tvgPngLoader.cpp new file mode 100644 index 00000000..15ccfaa3 --- /dev/null +++ b/src/loaders/external_png/tvgPngLoader.cpp @@ -0,0 +1,91 @@ +/* + * Copyright (c) 2020-2021 Samsung Electronics Co., Ltd. All rights reserved. + + * 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 "tvgLoader.h" +#include "tvgPngLoader.h" + +PngLoader::PngLoader() +{ + image = static_cast(calloc(1, sizeof(png_image))); + image->version = PNG_IMAGE_VERSION; + image->opaque = NULL; +} + +PngLoader::~PngLoader() +{ + if (content) { + free((void*)content); + content = nullptr; + } + free(image); +} + +bool PngLoader::open(const string& path) +{ + image->opaque = NULL; + + if (!png_image_begin_read_from_file(image, path.c_str())) return false; + + w = image->width; + h = image->height; + + return true; +} + +bool PngLoader::open(const char* data, uint32_t size, bool copy) +{ + image->opaque = NULL; + + if (!png_image_begin_read_from_memory(image, data, size)) return false; + + w = image->width; + h = image->height; + + return true; +} + +bool PngLoader::read() +{ + png_bytep buffer; + image->format = PNG_FORMAT_BGRA; + buffer = static_cast(malloc(PNG_IMAGE_SIZE((*image)))); + if (!buffer) { + //out of memory, only time when libpng doesnt free its data + png_image_free(image); + return false; + } + if (!png_image_finish_read(image, NULL, buffer, 0, NULL)) return false; + content = reinterpret_cast(buffer); + + return true; +} + +bool PngLoader::close() +{ + png_image_free(image); + return true; +} + +const uint32_t* PngLoader::pixels() +{ + return this->content; +} diff --git a/src/loaders/external_png/tvgPngLoader.h b/src/loaders/external_png/tvgPngLoader.h new file mode 100644 index 00000000..ab2c8169 --- /dev/null +++ b/src/loaders/external_png/tvgPngLoader.h @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2020-2021 Samsung Electronics Co., Ltd. All rights reserved. + + * 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. + */ +#ifndef _TVG_PNG_LOADER_H_ +#define _TVG_PNG_LOADER_H_ + +#include + +class PngLoader : public LoadModule +{ +public: + PngLoader(); + ~PngLoader(); + + using LoadModule::open; + bool open(const string& path) override; + bool open(const char* data, uint32_t size, bool copy) override; + bool read() override; + bool close() override; + + const uint32_t* pixels() override; + +private: + png_imagep image = nullptr; + const uint32_t* content = nullptr; +}; + +#endif //_TVG_PNG_LOADER_H_ diff --git a/src/loaders/jpg/meson.build b/src/loaders/jpg/meson.build index f76d03f3..97848485 100644 --- a/src/loaders/jpg/meson.build +++ b/src/loaders/jpg/meson.build @@ -3,16 +3,7 @@ source_file = [ 'tvgJpgLoader.cpp', ] -jpg_dep = dependency('libturbojpeg', required: false) -if not jpg_dep.found() - jpg_dep = cc.find_library('turbojpeg', required: false) -endif -if not jpg_dep.found() - error('JPEG image loading requires libturbojpeg or turbojpeg, neither was found.') -endif - subloader_dep += [declare_dependency( include_directories : include_directories('.'), - dependencies : jpg_dep, sources : source_file )] diff --git a/src/loaders/jpg/tvgJpgLoader.cpp b/src/loaders/jpg/tvgJpgLoader.cpp index f3327fbd..850d79b8 100644 --- a/src/loaders/jpg/tvgJpgLoader.cpp +++ b/src/loaders/jpg/tvgJpgLoader.cpp @@ -20,8 +20,6 @@ * SOFTWARE. */ -#include -#include #include "tvgLoader.h" #include "tvgJpgLoader.h" @@ -29,13 +27,6 @@ /* Internal Class Implementation */ /************************************************************************/ -void JpgLoader::clear() -{ - if (freeData) free(data); - data = nullptr; - size = 0; - freeData = false; -} /************************************************************************/ /* External Class Implementation */ @@ -43,111 +34,51 @@ void JpgLoader::clear() JpgLoader::JpgLoader() { - jpegDecompressor = tjInitDecompress(); + //TODO: } JpgLoader::~JpgLoader() { - if (freeData) free(data); - tjDestroy(jpegDecompressor); - - //This image is shared with raster engine. - tjFree(image); + //TODO: } bool JpgLoader::open(const string& path) { - clear(); + //TODO: - 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 *) malloc(size); - if (!data) goto finalize; - - if (fread(data, size, 1, jpegFile) < 1) goto failure; - - int width, height, subSample, colorSpace; - if (tjDecompressHeader3(jpegDecompressor, data, size, &width, &height, &subSample, &colorSpace) < 0) { - TVGERR("JPG LOADER", "%s", tjGetErrorStr()); - goto failure; - } - - w = static_cast(width); - h = static_cast(height); - ret = true; - freeData = true; - - goto finalize; - -failure: - clear(); - -finalize: - fclose(jpegFile); - return ret; + return false; } bool JpgLoader::open(const char* data, uint32_t size, bool copy) { - clear(); + //TODO: - int width, height, subSample, colorSpace; - if (tjDecompressHeader3(jpegDecompressor, (unsigned char *) data, size, &width, &height, &subSample, &colorSpace) < 0) return false; - - if (copy) { - this->data = (unsigned char *) malloc(size); - if (!this->data) return false; - memcpy((unsigned char *)this->data, data, size); - freeData = true; - } else { - this->data = (unsigned char *) data; - } - - w = static_cast(width); - h = static_cast(height); - this->size = size; - - return true; + return false; } bool JpgLoader::read() { - if (image) tjFree(image); - image = (unsigned char *)tjAlloc(static_cast(w) * static_cast(h) * tjPixelSize[TJPF_BGRX]); - if (!image) return false; + //TODO: - //decompress jpg image - if (tjDecompress2(jpegDecompressor, data, size, image, static_cast(w), 0, static_cast(h), TJPF_BGRX, 0) < 0) { - TVGERR("JPG LOADER", "%s", tjGetErrorStr()); - tjFree(image); - image = nullptr; - return false; - } - - return true; + return false; } bool JpgLoader::close() { - clear(); - return true; + //TODO: + + return false; } const uint32_t* JpgLoader::pixels() { - return (const uint32_t*) image; -} + //TODO: + + return nullptr; +} \ No newline at end of file diff --git a/src/loaders/jpg/tvgJpgLoader.h b/src/loaders/jpg/tvgJpgLoader.h index 1aeaa04c..c654d648 100644 --- a/src/loaders/jpg/tvgJpgLoader.h +++ b/src/loaders/jpg/tvgJpgLoader.h @@ -22,8 +22,6 @@ #ifndef _TVG_JPG_LOADER_H_ #define _TVG_JPG_LOADER_H_ -using tjhandle = void*; - //TODO: Use Task? class JpgLoader : public LoadModule { @@ -38,15 +36,6 @@ public: bool close() override; const uint32_t* pixels() override; - -private: - void clear(); - - tjhandle jpegDecompressor; - unsigned char* data = nullptr; - unsigned char *image = nullptr; - unsigned long size = 0; - bool freeData = false; }; #endif //_TVG_JPG_LOADER_H_ diff --git a/src/loaders/meson.build b/src/loaders/meson.build index 24b65448..d61fef43 100644 --- a/src/loaders/meson.build +++ b/src/loaders/meson.build @@ -1,24 +1,30 @@ subloader_dep = [] +if get_option('loaders').contains('tvg') == true + subdir('tvg') +endif + if get_option('loaders').contains('svg') == true subdir('svg') endif if get_option('loaders').contains('png') == true - subdir('png') -endif - -if get_option('loaders').contains('tvg') == true - subdir('tvg') + subdir('external_png') + if not png_dep.found() + subdir('png') + endif endif if get_option('loaders').contains('jpg') == true - subdir('jpg') + subdir('external_jpg') + if not jpg_dep.found() + subdir('jpg') + endif endif subdir('raw') loader_dep = declare_dependency( - dependencies: subloader_dep, - include_directories : include_directories('.'), + dependencies: subloader_dep, + include_directories : include_directories('.'), ) diff --git a/src/loaders/png/meson.build b/src/loaders/png/meson.build old mode 100755 new mode 100644 index 4472b717..1de51742 --- a/src/loaders/png/meson.build +++ b/src/loaders/png/meson.build @@ -3,10 +3,7 @@ source_file = [ 'tvgPngLoader.cpp', ] -png_dep = meson.get_compiler('cpp').find_library('libpng') - subloader_dep += [declare_dependency( include_directories : include_directories('.'), - dependencies : png_dep, sources : source_file )] diff --git a/src/loaders/png/tvgPngLoader.cpp b/src/loaders/png/tvgPngLoader.cpp old mode 100755 new mode 100644 index 15ccfaa3..ff686fe4 --- a/src/loaders/png/tvgPngLoader.cpp +++ b/src/loaders/png/tvgPngLoader.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020-2021 Samsung Electronics Co., Ltd. All rights reserved. + * Copyright (c) 2021 Samsung Electronics Co., Ltd. All rights reserved. * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -23,69 +23,63 @@ #include "tvgLoader.h" #include "tvgPngLoader.h" +/************************************************************************/ +/* Internal Class Implementation */ +/************************************************************************/ + + + +/************************************************************************/ +/* External Class Implementation */ +/************************************************************************/ + PngLoader::PngLoader() { - image = static_cast(calloc(1, sizeof(png_image))); - image->version = PNG_IMAGE_VERSION; - image->opaque = NULL; + //TODO: } + PngLoader::~PngLoader() { - if (content) { - free((void*)content); - content = nullptr; - } - free(image); + //TODO: } + bool PngLoader::open(const string& path) { - image->opaque = NULL; + //TODO: - if (!png_image_begin_read_from_file(image, path.c_str())) return false; - - w = image->width; - h = image->height; - - return true; + return false; } + bool PngLoader::open(const char* data, uint32_t size, bool copy) { - image->opaque = NULL; + //TODO: - if (!png_image_begin_read_from_memory(image, data, size)) return false; - - w = image->width; - h = image->height; - - return true; + return false; } + bool PngLoader::read() { - png_bytep buffer; - image->format = PNG_FORMAT_BGRA; - buffer = static_cast(malloc(PNG_IMAGE_SIZE((*image)))); - if (!buffer) { - //out of memory, only time when libpng doesnt free its data - png_image_free(image); - return false; - } - if (!png_image_finish_read(image, NULL, buffer, 0, NULL)) return false; - content = reinterpret_cast(buffer); + //TODO: - return true; + return false; } + bool PngLoader::close() { - png_image_free(image); - return true; + //TODO: + + return false; } + const uint32_t* PngLoader::pixels() { - return this->content; -} + //TODO: + + return nullptr; +} \ No newline at end of file diff --git a/src/loaders/png/tvgPngLoader.h b/src/loaders/png/tvgPngLoader.h old mode 100755 new mode 100644 index ab2c8169..ac7d6f16 --- a/src/loaders/png/tvgPngLoader.h +++ b/src/loaders/png/tvgPngLoader.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020-2021 Samsung Electronics Co., Ltd. All rights reserved. + * Copyright (c) 2021 Samsung Electronics Co., Ltd. All rights reserved. * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -22,8 +22,7 @@ #ifndef _TVG_PNG_LOADER_H_ #define _TVG_PNG_LOADER_H_ -#include - +//TODO: Use Task? class PngLoader : public LoadModule { public: @@ -37,10 +36,6 @@ public: bool close() override; const uint32_t* pixels() override; - -private: - png_imagep image = nullptr; - const uint32_t* content = nullptr; }; #endif //_TVG_PNG_LOADER_H_