Skip to content

Commit 87af18b

Browse files
committed
textures: Use libturbojpeg to view jpgs
1 parent bd141b5 commit 87af18b

2 files changed

Lines changed: 23 additions & 27 deletions

File tree

app/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ BUILD_PRX = 1
2525
LIBDIR = ../libs/lib
2626
LDFLAGS =
2727
LIBS = -lintrafont -lglib2d -lxmp -lmpg123 -lvorbisfile -lvorbis -lopusfile -lopus -lFLAC -logg \
28-
-larchive -llzma -ljpeg -lpng16 -lstdc++ -lz \
28+
-larchive -llzma -lturbojpeg -ljpeg -lpng16 -lstdc++ -lz \
2929
-lpspkubridge -lpspsystemctrl_user -lpspusbdevice \
3030
-lpspgu -lpspvram -lpspaudio -lpsppower -lpspreg -lpspusb -lpspusbstor -lpspumd
3131

app/source/textures.cpp

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#include <assert.h>
22
#include <cstdio>
33
#include <cstring>
4-
#include <jpeglib.h>
54
#include <png.h>
5+
#include <turbojpeg.h>
66

77
#include "fs.h"
88
#include "libnsbmp.h"
@@ -179,47 +179,43 @@ namespace Textures {
179179
}
180180

181181
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+
}
188187

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);
192192
return nullptr;
193193
}
194194

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) {
199196
Log::Error("%s g2d does not support images greater than 512x512\n", __func__);
200-
jpeg_destroy_decompress(&info);
197+
tjDestroy(tj);
201198
return nullptr;
202199
}
203200

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));
206203
if (!buffer) {
207204
Log::Error("%s failed to allocate buffer\n", __func__);
208-
jpeg_destroy_decompress(&info);
205+
tjDestroy(tj);
209206
return nullptr;
210207
}
211208

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;
215214
}
216215

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);
221217
std::free(buffer);
222-
218+
tjDestroy(tj);
223219
return tex;
224220
}
225221

0 commit comments

Comments
 (0)