|
1 | 1 | #include <assert.h> |
2 | 2 | #include <cstdio> |
3 | 3 | #include <cstring> |
4 | | -#include <jpeglib.h> |
5 | 4 | #include <png.h> |
| 5 | +#include <turbojpeg.h> |
6 | 6 |
|
7 | 7 | #include "fs.h" |
8 | 8 | #include "libnsbmp.h" |
@@ -179,47 +179,43 @@ namespace Textures { |
179 | 179 | } |
180 | 180 |
|
181 | 181 | g2dTexture *LoadImageBufferJPEG(unsigned char *data, int size) { |
182 | | - struct jpeg_decompress_struct info; |
183 | | - struct jpeg_error_mgr err; |
184 | | - |
185 | | - info.err = jpeg_std_error(&err); |
186 | | - jpeg_create_decompress(&info); |
187 | | - jpeg_mem_src(&info, data, size); |
| 182 | + tjhandle tj = tjInitDecompress(); |
| 183 | + if (!tj) { |
| 184 | + Log::Error("%s(tjInitDecompress) failed: %s\n", __func__, tjGetErrorStr()); |
| 185 | + return nullptr; |
| 186 | + } |
188 | 187 |
|
189 | | - if (jpeg_read_header(&info, TRUE) != JPEG_HEADER_OK) { |
190 | | - Log::Error("%s(jpeg_read_header) failed\n", __func__); |
191 | | - jpeg_destroy_decompress(&info); |
| 188 | + int width = 0, height = 0, jpegSubsamp = 0; |
| 189 | + if (tjDecompressHeader2(tj, data, size, &width, &height, &jpegSubsamp) != 0) { |
| 190 | + Log::Error("%s(tjDecompressHeader2) failed: %s\n", __func__, tjGetErrorStr()); |
| 191 | + tjDestroy(tj); |
192 | 192 | return nullptr; |
193 | 193 | } |
194 | 194 |
|
195 | | - info.out_color_space = JCS_EXT_RGBA; |
196 | | - jpeg_start_decompress(&info); |
197 | | - |
198 | | - if (info.output_width > 512 || info.output_height > 512) { |
| 195 | + if (width > 512 || height > 512) { |
199 | 196 | Log::Error("%s g2d does not support images greater than 512x512\n", __func__); |
200 | | - jpeg_destroy_decompress(&info); |
| 197 | + tjDestroy(tj); |
201 | 198 | return nullptr; |
202 | 199 | } |
203 | 200 |
|
204 | | - int stride = info.output_width * 4; |
205 | | - u8 *buffer = static_cast<u8 *>(std::malloc(stride * info.output_height)); |
| 201 | + int stride = width * 4; |
| 202 | + u8 *buffer = static_cast<u8 *>(std::malloc(stride * height)); |
206 | 203 | if (!buffer) { |
207 | 204 | Log::Error("%s failed to allocate buffer\n", __func__); |
208 | | - jpeg_destroy_decompress(&info); |
| 205 | + tjDestroy(tj); |
209 | 206 | return nullptr; |
210 | 207 | } |
211 | 208 |
|
212 | | - while (info.output_scanline < info.output_height) { |
213 | | - u8 *ptr = buffer + stride * info.output_scanline; |
214 | | - jpeg_read_scanlines(&info, &ptr, 1); |
| 209 | + if (tjDecompress2(tj, data, size, buffer, width, stride, height, TJPF_RGBA, TJFLAG_FASTDCT) != 0) { |
| 210 | + Log::Error("%s(tjDecompress2) failed: %s\n", __func__, tjGetErrorStr()); |
| 211 | + std::free(buffer); |
| 212 | + tjDestroy(tj); |
| 213 | + return nullptr; |
215 | 214 | } |
216 | 215 |
|
217 | | - g2dTexture *tex = g2dTexLoad(buffer, info.output_width, info.output_height, G2D_SWIZZLE); |
218 | | - |
219 | | - jpeg_finish_decompress(&info); |
220 | | - jpeg_destroy_decompress(&info); |
| 216 | + g2dTexture *tex = g2dTexLoad(buffer, width, height, G2D_SWIZZLE); |
221 | 217 | std::free(buffer); |
222 | | - |
| 218 | + tjDestroy(tj); |
223 | 219 | return tex; |
224 | 220 | } |
225 | 221 |
|
|
0 commit comments