2 * Copyright (c) 2021 - 2023 the ThorVG project. All rights reserved.
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:
11 * The above copyright notice and this permission notice shall be included in all
12 * copies or substantial portions of the Software.
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
24 #include <turbojpeg.h>
25 #include "tvgLoader.h"
26 #include "tvgJpgLoader.h"
28 /************************************************************************/
29 /* Internal Class Implementation */
30 /************************************************************************/
32 void JpgLoader::clear()
34 if (freeData) free(data);
40 /************************************************************************/
41 /* External Class Implementation */
42 /************************************************************************/
44 JpgLoader::JpgLoader()
46 jpegDecompressor = tjInitDecompress();
50 JpgLoader::~JpgLoader()
52 if (freeData) free(data);
53 tjDestroy(jpegDecompressor);
55 //This image is shared with raster engine.
60 bool JpgLoader::open(const string& path)
64 auto jpegFile = fopen(path.c_str(), "rb");
65 if (!jpegFile) return false;
70 if (fseek(jpegFile, 0, SEEK_END) < 0) goto finalize;
71 if (((size = ftell(jpegFile)) < 1)) goto finalize;
72 if (fseek(jpegFile, 0, SEEK_SET)) goto finalize;
74 data = (unsigned char *) malloc(size);
75 if (!data) goto finalize;
79 if (fread(data, size, 1, jpegFile) < 1) goto failure;
81 int width, height, subSample, colorSpace;
82 if (tjDecompressHeader3(jpegDecompressor, data, size, &width, &height, &subSample, &colorSpace) < 0) {
83 TVGERR("JPG LOADER", "%s", tjGetErrorStr());
87 w = static_cast<float>(width);
88 h = static_cast<float>(height);
102 bool JpgLoader::open(const char* data, uint32_t size, bool copy)
106 int width, height, subSample, colorSpace;
107 if (tjDecompressHeader3(jpegDecompressor, (unsigned char *) data, size, &width, &height, &subSample, &colorSpace) < 0) return false;
110 this->data = (unsigned char *) malloc(size);
111 if (!this->data) return false;
112 memcpy((unsigned char *)this->data, data, size);
115 this->data = (unsigned char *) data;
119 w = static_cast<float>(width);
120 h = static_cast<float>(height);
127 bool JpgLoader::read()
129 if (image) tjFree(image);
130 image = (unsigned char *)tjAlloc(static_cast<int>(w) * static_cast<int>(h) * tjPixelSize[TJPF_BGRX]);
131 if (!image) return false;
133 //decompress jpg image
134 if (tjDecompress2(jpegDecompressor, data, size, image, static_cast<int>(w), 0, static_cast<int>(h), TJPF_BGRX, 0) < 0) {
135 TVGERR("JPG LOADER", "%s", tjGetErrorStr());
145 bool JpgLoader::close()
152 unique_ptr<Surface> JpgLoader::bitmap()
154 if (!image) return nullptr;
156 auto surface = static_cast<Surface*>(malloc(sizeof(Surface)));
157 surface->buffer = (uint32_t*)(image);
161 surface->cs = SwCanvas::ARGB8888;
163 return unique_ptr<Surface>(surface);