| 1 | /* |
| 2 | * Copyright (c) 2021 - 2024 the ThorVG project. All rights reserved. |
| 3 | |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | * of this software and associated documentation files (the "Software"), to deal |
| 6 | * in the Software without restriction, including without limitation the rights |
| 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | * copies of the Software, and to permit persons to whom the Software is |
| 9 | * furnished to do so, subject to the following conditions: |
| 10 | |
| 11 | * The above copyright notice and this permission notice shall be included in all |
| 12 | * copies or substantial portions of the Software. |
| 13 | |
| 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 20 | * SOFTWARE. |
| 21 | */ |
| 22 | |
| 23 | #include <memory.h> |
| 24 | #include <turbojpeg.h> |
| 25 | #include "tvgJpgLoader.h" |
| 26 | |
| 27 | /************************************************************************/ |
| 28 | /* Internal Class Implementation */ |
| 29 | /************************************************************************/ |
| 30 | |
| 31 | void JpgLoader::clear() |
| 32 | { |
| 33 | if (freeData) free(data); |
| 34 | data = nullptr; |
| 35 | size = 0; |
| 36 | freeData = false; |
| 37 | } |
| 38 | |
| 39 | |
| 40 | /************************************************************************/ |
| 41 | /* External Class Implementation */ |
| 42 | /************************************************************************/ |
| 43 | |
| 44 | JpgLoader::JpgLoader() : ImageLoader(FileType::Jpg) |
| 45 | { |
| 46 | jpegDecompressor = tjInitDecompress(); |
| 47 | } |
| 48 | |
| 49 | |
| 50 | JpgLoader::~JpgLoader() |
| 51 | { |
| 52 | clear(); |
| 53 | tjDestroy(jpegDecompressor); |
| 54 | |
| 55 | //This image is shared with raster engine. |
| 56 | tjFree(surface.buf8); |
| 57 | } |
| 58 | |
| 59 | |
| 60 | bool JpgLoader::open(const string& path) |
| 61 | { |
| 62 | #ifdef THORVG_FILE_IO_SUPPORT |
| 63 | auto jpegFile = fopen(path.c_str(), "rb"); |
| 64 | if (!jpegFile) return false; |
| 65 | |
| 66 | auto ret = false; |
| 67 | |
| 68 | //determine size |
| 69 | if (fseek(jpegFile, 0, SEEK_END) < 0) goto finalize; |
| 70 | if (((size = ftell(jpegFile)) < 1)) goto finalize; |
| 71 | if (fseek(jpegFile, 0, SEEK_SET)) goto finalize; |
| 72 | |
| 73 | data = (unsigned char *) malloc(size); |
| 74 | if (!data) goto finalize; |
| 75 | |
| 76 | freeData = true; |
| 77 | |
| 78 | if (fread(data, size, 1, jpegFile) < 1) goto failure; |
| 79 | |
| 80 | int width, height, subSample, colorSpace; |
| 81 | if (tjDecompressHeader3(jpegDecompressor, data, size, &width, &height, &subSample, &colorSpace) < 0) { |
| 82 | TVGERR("JPG LOADER", "%s", tjGetErrorStr()); |
| 83 | goto failure; |
| 84 | } |
| 85 | |
| 86 | w = static_cast<float>(width); |
| 87 | h = static_cast<float>(height); |
| 88 | ret = true; |
| 89 | |
| 90 | goto finalize; |
| 91 | |
| 92 | failure: |
| 93 | clear(); |
| 94 | |
| 95 | finalize: |
| 96 | fclose(jpegFile); |
| 97 | return ret; |
| 98 | #else |
| 99 | return false; |
| 100 | #endif |
| 101 | } |
| 102 | |
| 103 | |
| 104 | bool JpgLoader::open(const char* data, uint32_t size, bool copy) |
| 105 | { |
| 106 | int width, height, subSample, colorSpace; |
| 107 | if (tjDecompressHeader3(jpegDecompressor, (unsigned char *) data, size, &width, &height, &subSample, &colorSpace) < 0) return false; |
| 108 | |
| 109 | if (copy) { |
| 110 | this->data = (unsigned char *) malloc(size); |
| 111 | if (!this->data) return false; |
| 112 | memcpy((unsigned char *)this->data, data, size); |
| 113 | freeData = true; |
| 114 | } else { |
| 115 | this->data = (unsigned char *) data; |
| 116 | freeData = false; |
| 117 | } |
| 118 | |
| 119 | w = static_cast<float>(width); |
| 120 | h = static_cast<float>(height); |
| 121 | this->size = size; |
| 122 | |
| 123 | return true; |
| 124 | } |
| 125 | |
| 126 | |
| 127 | bool JpgLoader::read() |
| 128 | { |
| 129 | if (!LoadModule::read()) return true; |
| 130 | |
| 131 | if (w == 0 || h == 0) return false; |
| 132 | |
| 133 | //determine the image format |
| 134 | TJPF format; |
| 135 | if (ImageLoader::cs == ColorSpace::ARGB8888 || ImageLoader::cs == ColorSpace::ARGB8888S) { |
| 136 | format = TJPF_BGRX; |
| 137 | surface.cs = ColorSpace::ARGB8888; |
| 138 | } else { |
| 139 | format = TJPF_RGBX; |
| 140 | surface.cs = ColorSpace::ABGR8888; |
| 141 | } |
| 142 | |
| 143 | auto image = (unsigned char *)tjAlloc(static_cast<int>(w) * static_cast<int>(h) * tjPixelSize[format]); |
| 144 | if (!image) return false; |
| 145 | |
| 146 | //decompress jpg image |
| 147 | if (tjDecompress2(jpegDecompressor, data, size, image, static_cast<int>(w), 0, static_cast<int>(h), format, 0) < 0) { |
| 148 | TVGERR("JPG LOADER", "%s", tjGetErrorStr()); |
| 149 | tjFree(image); |
| 150 | image = nullptr; |
| 151 | return false; |
| 152 | } |
| 153 | |
| 154 | //setup the surface |
| 155 | surface.buf8 = image; |
| 156 | surface.stride = w; |
| 157 | surface.w = w; |
| 158 | surface.h = h; |
| 159 | surface.channelSize = sizeof(uint32_t); |
| 160 | surface.premultiplied = true; |
| 161 | |
| 162 | clear(); |
| 163 | return true; |
| 164 | } |