Skip to content

Commit b57b357

Browse files
committed
textures: Introduce support for tiff using libtiff v4.7.0 and reintroduce support for pgm, ppm and psd using stb_image
1 parent 7782d65 commit b57b357

14 files changed

Lines changed: 11019 additions & 19 deletions

File tree

app/Makefile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ VERSION_MAJOR := 4
1414
VERSION_MINOR := 1
1515
VERSION_MICRO := 2
1616

17-
INCDIR = ../libs/ ../libs/include ../libs/libnsbmp ../libs/libnsgif ../libs/include/opus include
17+
INCDIR = ../libs/ ../libs/include ../libs/include/libtiff ../libs/libnsbmp ../libs/libnsgif \
18+
../libs/include/opus include
1819
CFLAGS = -Os -mno-gpopt -Wall -ffast-math -Wno-narrowing -Wno-unused-variable \
1920
-DVERSION_MAJOR=$(VERSION_MAJOR) -DVERSION_MINOR=$(VERSION_MINOR) -DVERSION_MICRO=$(VERSION_MICRO)
2021
CXXFLAGS = $(CFLAGS) -fno-exceptions -fno-rtti -std=gnu++17
@@ -25,7 +26,7 @@ BUILD_PRX = 1
2526
LIBDIR = ../libs/lib
2627
LDFLAGS =
2728
LIBS = -lintrafont -lglib2d -lxmp -lmpg123 -lvorbisfile -lvorbis -lopusfile -lopus -lFLAC -logg \
28-
-larchive -lbz2 -llzma -lturbojpeg -ljpeg -lpng16 -lstdc++ -lz \
29+
-larchive -ltiff -lbz2 -llzma -lturbojpeg -ljpeg -lpng16 -lstdc++ -lz \
2930
-lpspkubridge -lpspsystemctrl_user -lpspusbdevice \
3031
-lpspgu -lpspvram -lpspaudio -lpsppower -lpspreg -lpspusb -lpspusbstor -lpspumd
3132

app/source/fs.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ namespace FS {
123123
else if ((strncasecmp(ext, "bmp", 3) == 0) || (strncasecmp(ext, "gif", 3) == 0) || (strncasecmp(ext, "jpg", 3) == 0)
124124
|| (strncasecmp(ext, "jpeg", 4) == 0) || (strncasecmp(ext, "pgm", 3) == 0) || (strncasecmp(ext, "ppm", 3) == 0)
125125
|| (strncasecmp(ext, "png", 3) == 0) || (strncasecmp(ext, "psd", 3) == 0) || (strncasecmp(ext, "tga", 3) == 0)
126-
|| (strncasecmp(ext, "webp", 4) == 0)) {
126+
|| (strncasecmp(ext, "tif", 3) == 0) || (strncasecmp(ext, "tiff", 4) == 0)) {
127127
return FileTypeImage;
128128
}
129129
else if ((strncasecmp(ext, "cfg", 3) == 0) || (strncasecmp(ext, "conf", 4) == 0) || (strncasecmp(ext, "ini", 3) == 0)

app/source/textures.cpp

Lines changed: 104 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,25 @@
88
#include "libnsbmp.h"
99
#include "nsgif.h"
1010
#include "log.h"
11+
12+
// STB
13+
#define STB_IMAGE_IMPLEMENTATION
14+
#define STBI_NO_STDIO
15+
#define STBI_NO_BMP
16+
#define STBI_NO_GIF
17+
#define STBI_NO_HDR
18+
#define STBI_NO_JPEG
19+
#define STBI_NO_PIC
20+
#define STBI_NO_PNG
21+
#define STBI_ONLY_PNM
22+
#define STBI_ONLY_PSD
23+
#define STBI_ONLY_TGA
24+
#include "stb_image.h"
25+
26+
// TIFF
27+
#include "tiffio.h"
28+
#include "tiffiop.h"
29+
1130
#include "textures.h"
1231
#include "utils.h"
1332

@@ -240,30 +259,99 @@ namespace Textures {
240259
return tex;
241260
}
242261

243-
g2dTexture *LoadImage(const std::string &path, int size) {
244-
int ret = 0;
245-
unsigned char *data = static_cast<unsigned char *>(std::malloc(size));
246-
247-
if (R_FAILED(ret = FS::ReadFile(path, data, size))) {
248-
Log::Error("%s(FS::ReadFile) failed: 0x%08x\n", __func__, ret);
249-
std::free(data);
262+
static g2dTexture *LoadImageBuffer(const unsigned char *data, int size) {
263+
int width = 0, height = 0, channels = 0;
264+
g2dTexture *tex = nullptr;
265+
266+
unsigned char *img = stbi_load_from_memory(data, size, &width, &height, &channels, STBI_rgb_alpha);
267+
268+
if (!img) {
269+
Log::Error("%s(stbi_load_from_memory) failed: %s\n", __func__, stbi_failure_reason());
250270
return nullptr;
251271
}
272+
273+
if (width > 512 || height > 512) {
274+
Log::Error("%s g2d does not support images greater than 512x512\n", __func__);
275+
stbi_image_free(img);
276+
return nullptr;
277+
}
278+
279+
tex = g2dTexLoad(img, width, height, G2D_SWIZZLE);
280+
stbi_image_free(img);
281+
return tex;
282+
}
252283

284+
static g2dTexture *LoadImageTIFF(const std::string &path) {
285+
TIFF *tif = TIFFOpen(path.c_str(), "r");
253286
g2dTexture *tex = nullptr;
254-
const char *ext = FS::GetFileExt(path.c_str());
255287

256-
if (strncasecmp(ext, "bmp", 3) == 0) {
257-
tex = Textures::LoadImageBufferBMP(data, size);
288+
if (tif) {
289+
std::size_t pixelCount = 0;
290+
u32 *raster = nullptr;
291+
int width = 0, height = 0;
292+
293+
TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &width);
294+
TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &height);
295+
pixelCount = width * height;
296+
297+
raster = (u32 *)_TIFFCheckMalloc(tif, pixelCount, sizeof(u32), "raster buffer");
298+
if (raster != nullptr) {
299+
if (TIFFReadRGBAImageOriented(tif, width, height, raster, ORIENTATION_TOPLEFT)) {
300+
tex = g2dTexLoad(reinterpret_cast<unsigned char*>(raster), width, height, G2D_SWIZZLE);
301+
_TIFFfree(raster);
302+
}
303+
else {
304+
Log::Error("%s(TIFFReadRGBAImage) failed\n", __func__);
305+
}
306+
307+
}
308+
else {
309+
Log::Error("%s(_TIFFmalloc) failed\n", __func__);
310+
}
311+
312+
TIFFClose(tif);
313+
return tex;
258314
}
259-
else if (strncasecmp(ext, "gif", 3) == 0) {
260-
tex = Textures::LoadImageBufferGIF(data, size);
315+
else {
316+
Log::Error("%s(TIFFOpen) failed\n", __func__);
261317
}
262-
else if ((strncasecmp(ext, "jpeg", 4) == 0) || (strncasecmp(ext, "jpg", 3) == 0)) {
263-
tex = Textures::LoadImageBufferJPEG(data, size);
318+
319+
return tex;
320+
}
321+
322+
g2dTexture *LoadImage(const std::string &path, int size) {
323+
int ret = 0;
324+
unsigned char *data = static_cast<unsigned char *>(std::malloc(size));
325+
326+
g2dTexture *tex = nullptr;
327+
const char *ext = FS::GetFileExt(path.c_str());
328+
329+
if ((strncasecmp(ext, "tif", 3) == 0) || (strncasecmp(ext, "tiff", 4) == 0)) {
330+
tex = Textures::LoadImageTIFF(path);
264331
}
265-
else if (strncasecmp(ext, "png", 3) == 0) {
266-
tex = Textures::LoadImageBufferPNG(data, size);
332+
else {
333+
if (R_FAILED(ret = FS::ReadFile(path, data, size))) {
334+
Log::Error("%s(FS::ReadFile) failed: 0x%08x\n", __func__, ret);
335+
std::free(data);
336+
return nullptr;
337+
}
338+
339+
if (strncasecmp(ext, "bmp", 3) == 0) {
340+
tex = Textures::LoadImageBufferBMP(data, size);
341+
}
342+
else if (strncasecmp(ext, "gif", 3) == 0) {
343+
tex = Textures::LoadImageBufferGIF(data, size);
344+
}
345+
else if ((strncasecmp(ext, "jpeg", 4) == 0) || (strncasecmp(ext, "jpg", 3) == 0)) {
346+
tex = Textures::LoadImageBufferJPEG(data, size);
347+
}
348+
else if (strncasecmp(ext, "png", 3) == 0) {
349+
tex = Textures::LoadImageBufferPNG(data, size);
350+
}
351+
else if ((strncasecmp(ext, "pgm", 3) == 0) || (strncasecmp(ext, "ppm", 3) == 0) || (strncasecmp(ext, "psd", 3) == 0)
352+
|| (strncasecmp(ext, "tga", 3) == 0)) {
353+
tex = Textures::LoadImageBuffer(data, size);
354+
}
267355
}
268356

269357
std::free(data);

libs/include/libtiff/LICENSE.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# LibTIFF license
2+
3+
Copyright © 1988-1997 Sam Leffler\
4+
Copyright © 1991-1997 Silicon Graphics, Inc.
5+
6+
Permission to use, copy, modify, distribute, and sell this software and
7+
its documentation for any purpose is hereby granted without fee, provided
8+
that (i) the above copyright notices and this permission notice appear in
9+
all copies of the software and related documentation, and (ii) the names of
10+
Sam Leffler and Silicon Graphics may not be used in any advertising or
11+
publicity relating to the software without the specific, prior written
12+
permission of Sam Leffler and Silicon Graphics.
13+
14+
THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
15+
EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
16+
WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
17+
18+
IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
19+
ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
20+
OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
21+
WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
22+
LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
23+
OF THIS SOFTWARE.

libs/include/libtiff/tif_config.h

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
/* clang-format off */
2+
/* clang-format disabled because CMake scripts are very sensitive to the
3+
* formatting of this file. configure_file variables of type "" are
4+
* modified by clang-format and won't be substituted.
5+
*/
6+
7+
/* libtiff/tif_config.h.cmake.in. Not generated, but originated from autoheader. */
8+
/* This file must be kept up-to-date with needed substitutions from libtiff/tif_config.h.in. */
9+
10+
#include "tiffconf.h"
11+
12+
/* Support CCITT Group 3 & 4 algorithms */
13+
#define CCITT_SUPPORT 1
14+
15+
/* Pick up YCbCr subsampling info from the JPEG data stream to support files
16+
lacking the tag (default enabled). */
17+
#define CHECK_JPEG_YCBCR_SUBSAMPLING 1
18+
19+
/* enable partial strip reading for large strips (experimental) */
20+
/* #undef CHUNKY_STRIP_READ_SUPPORT */
21+
22+
/* Support C++ stream API (requires C++ compiler) */
23+
#define CXX_SUPPORT 1
24+
25+
/* enable deferred strip/tile offset/size loading (experimental) */
26+
/* #undef DEFER_STRILE_LOAD */
27+
28+
/* Define to 1 if you have the <assert.h> header file. */
29+
#define HAVE_ASSERT_H 1
30+
31+
/* Define to 1 if you have the declaration of `optarg', and to 0 if you don't. */
32+
#define HAVE_DECL_OPTARG 1
33+
34+
/* Define to 1 if you have the <fcntl.h> header file. */
35+
#define HAVE_FCNTL_H 1
36+
37+
/* Define to 1 if fseeko (and presumably ftello) exists and is declared. */
38+
/* #undef HAVE_FSEEKO */
39+
40+
/* Define to 1 if you have the `getopt' function. */
41+
#define HAVE_GETOPT 1
42+
43+
/* Define to 1 if you have the <GLUT/glut.h> header file. */
44+
/* #undef HAVE_GLUT_GLUT_H */
45+
46+
/* Define to 1 if you have the <GL/glut.h> header file. */
47+
#define HAVE_GL_GLUT_H 1
48+
49+
/* Define to 1 if you have the <GL/glu.h> header file. */
50+
#define HAVE_GL_GLU_H 1
51+
52+
/* Define to 1 if you have the <GL/gl.h> header file. */
53+
#define HAVE_GL_GL_H 1
54+
55+
/* Define to 1 if you have the <io.h> header file. */
56+
/* #undef HAVE_IO_H */
57+
58+
/* Define to 1 if you have the `jbg_newlen' function. */
59+
/* #undef HAVE_JBG_NEWLEN */
60+
61+
/* Define to 1 if you have the `mmap' function. */
62+
/* #undef HAVE_MMAP */
63+
64+
/* Define to 1 if you have the <OpenGL/glu.h> header file. */
65+
/* #undef HAVE_OPENGL_GLU_H */
66+
67+
/* Define to 1 if you have the <OpenGL/gl.h> header file. */
68+
/* #undef HAVE_OPENGL_GL_H */
69+
70+
/* Define to 1 if you have the `setmode' function. */
71+
/* #undef HAVE_SETMODE */
72+
73+
/* Define to 1 if you have the <strings.h> header file. */
74+
#define HAVE_STRINGS_H 1
75+
76+
/* Define to 1 if you have the <sys/types.h> header file. */
77+
#define HAVE_SYS_TYPES_H 1
78+
79+
/* Define to 1 if you have the <unistd.h> header file. */
80+
#define HAVE_UNISTD_H 1
81+
82+
/* 8/12 bit libjpeg dual mode enabled */
83+
/* #undef JPEG_DUAL_MODE_8_12 */
84+
85+
/* Support LERC compression */
86+
/* #undef LERC_SUPPORT */
87+
88+
/* 12bit libjpeg primary include file with path */
89+
#define LIBJPEG_12_PATH ""
90+
91+
/* Support LZMA2 compression */
92+
#define LZMA_SUPPORT 1
93+
94+
/* Name of package */
95+
#define PACKAGE "LibTIFF Software"
96+
97+
/* Define to the address where bug reports for this package should be sent. */
98+
#define PACKAGE_BUGREPORT "tiff@lists.osgeo.org"
99+
100+
/* Define to the full name of this package. */
101+
#define PACKAGE_NAME "LibTIFF Software"
102+
103+
/* Define to the one symbol short name of this package. */
104+
#define PACKAGE_TARNAME "tiff"
105+
106+
/* Define to the home page for this package. */
107+
#define PACKAGE_URL ""
108+
109+
/* Size of size_t */
110+
#define SIZEOF_SIZE_T 4
111+
112+
/* Default size of the strip in bytes (when strip chopping enabled) */
113+
#define STRIP_SIZE_DEFAULT 8192
114+
115+
/** Maximum number of TIFF IFDs that libtiff can iterate through in a file. */
116+
#define TIFF_MAX_DIR_COUNT 1048576
117+
118+
/* define to use win32 IO system */
119+
/* #undef USE_WIN32_FILEIO */
120+
121+
/* Support WEBP compression */
122+
/* #undef WEBP_SUPPORT */
123+
124+
/* Support ZSTD compression */
125+
/* #undef ZSTD_SUPPORT */
126+
127+
128+
/* Define WORDS_BIGENDIAN to 1 if your processor stores words with the most
129+
significant byte first (like Motorola and SPARC, unlike Intel). */
130+
#if defined AC_APPLE_UNIVERSAL_BUILD
131+
# if defined __BIG_ENDIAN__
132+
# define WORDS_BIGENDIAN 1
133+
# endif
134+
#else
135+
# ifndef WORDS_BIGENDIAN
136+
# undef WORDS_BIGENDIAN
137+
# endif
138+
#endif
139+
140+
#if !defined(__MINGW32__)
141+
# define TIFF_SIZE_FORMAT "zu"
142+
#endif
143+
#if SIZEOF_SIZE_T == 8
144+
# define TIFF_SSIZE_FORMAT PRId64
145+
# if defined(__MINGW32__)
146+
# define TIFF_SIZE_FORMAT PRIu64
147+
# endif
148+
#elif SIZEOF_SIZE_T == 4
149+
# define TIFF_SSIZE_FORMAT PRId32
150+
# if defined(__MINGW32__)
151+
# define TIFF_SIZE_FORMAT PRIu32
152+
# endif
153+
#else
154+
# error "Unsupported size_t size; please submit a bug report"
155+
#endif
156+
157+
/* clang-format on */

0 commit comments

Comments
 (0)