555 |
jrf |
1 |
/* stbi-1.29 - public domain JPEG/PNG reader - http://nothings.org/stb_image.c
|
|
|
2 |
when you control the images you're loading
|
|
|
3 |
no warranty implied; use at your own risk
|
|
|
4 |
|
|
|
5 |
QUICK NOTES:
|
|
|
6 |
Primarily of interest to game developers and other people who can
|
|
|
7 |
avoid problematic images and only need the trivial interface
|
|
|
8 |
|
|
|
9 |
JPEG baseline (no JPEG progressive)
|
|
|
10 |
PNG 8-bit only
|
|
|
11 |
|
|
|
12 |
TGA (not sure what subset, if a subset)
|
|
|
13 |
BMP non-1bpp, non-RLE
|
|
|
14 |
PSD (composited view only, no extra channels)
|
|
|
15 |
|
|
|
16 |
GIF (*comp always reports as 4-channel)
|
|
|
17 |
HDR (radiance rgbE format)
|
|
|
18 |
PIC (Softimage PIC)
|
|
|
19 |
|
|
|
20 |
- decoded from memory or through stdio FILE (define STBI_NO_STDIO to remove code)
|
|
|
21 |
- supports installable dequantizing-IDCT, YCbCr-to-RGB conversion (define STBI_SIMD)
|
|
|
22 |
|
|
|
23 |
Latest revisions:
|
|
|
24 |
1.29 (2010-08-16) various warning fixes from Aurelien Pocheville
|
|
|
25 |
1.28 (2010-08-01) fix bug in GIF palette transparency (SpartanJ)
|
|
|
26 |
1.27 (2010-08-01) cast-to-uint8 to fix warnings (Laurent Gomila)
|
|
|
27 |
allow trailing 0s at end of image data (Laurent Gomila)
|
|
|
28 |
1.26 (2010-07-24) fix bug in file buffering for PNG reported by SpartanJ
|
|
|
29 |
1.25 (2010-07-17) refix trans_data warning (Won Chun)
|
|
|
30 |
1.24 (2010-07-12) perf improvements reading from files
|
|
|
31 |
minor perf improvements for jpeg
|
|
|
32 |
deprecated type-specific functions in hope of feedback
|
|
|
33 |
attempt to fix trans_data warning (Won Chun)
|
|
|
34 |
1.23 fixed bug in iPhone support
|
|
|
35 |
1.22 (2010-07-10) removed image *writing* support to stb_image_write.h
|
|
|
36 |
stbi_info support from Jetro Lauha
|
|
|
37 |
GIF support from Jean-Marc Lienher
|
|
|
38 |
iPhone PNG-extensions from James Brown
|
|
|
39 |
warning-fixes from Nicolas Schulz and Janez Zemva
|
|
|
40 |
1.21 fix use of 'uint8' in header (reported by jon blow)
|
|
|
41 |
1.20 added support for Softimage PIC, by Tom Seddon
|
|
|
42 |
|
|
|
43 |
See end of file for full revision history.
|
|
|
44 |
|
|
|
45 |
TODO:
|
|
|
46 |
stbi_info support for BMP,PSD,HDR,PIC
|
|
|
47 |
rewrite stbi_info and load_file variations to share file handling code
|
|
|
48 |
(current system allows individual functions to be called directly,
|
|
|
49 |
since each does all the work, but I doubt anyone uses this in practice)
|
|
|
50 |
|
|
|
51 |
|
|
|
52 |
============================ Contributors =========================
|
|
|
53 |
|
|
|
54 |
Image formats Optimizations & bugfixes
|
|
|
55 |
Sean Barrett (jpeg, png, bmp) Fabian "ryg" Giesen
|
|
|
56 |
Nicolas Schulz (hdr, psd)
|
|
|
57 |
Jonathan Dummer (tga) Bug fixes & warning fixes
|
|
|
58 |
Jean-Marc Lienher (gif) Marc LeBlanc
|
|
|
59 |
Tom Seddon (pic) Christpher Lloyd
|
|
|
60 |
Thatcher Ulrich (psd) Dave Moore
|
|
|
61 |
Won Chun
|
|
|
62 |
the Horde3D community
|
|
|
63 |
Extensions, features Janez Zemva
|
|
|
64 |
Jetro Lauha (stbi_info) Jonathan Blow
|
|
|
65 |
James "moose2000" Brown (iPhone PNG) Laurent Gomila
|
|
|
66 |
Aruelien Pocheville
|
|
|
67 |
|
|
|
68 |
If your name should be here but isn't, let Sean know.
|
|
|
69 |
|
|
|
70 |
*/
|
|
|
71 |
|
|
|
72 |
#include "stb_image.h"
|
|
|
73 |
|
|
|
74 |
#ifndef STBI_HEADER_FILE_ONLY
|
|
|
75 |
|
|
|
76 |
#ifndef STBI_NO_HDR
|
|
|
77 |
#include <math.h> // ldexp
|
|
|
78 |
#include <string.h> // strcmp
|
|
|
79 |
#endif
|
|
|
80 |
|
|
|
81 |
#ifndef STBI_NO_STDIO
|
|
|
82 |
#include <stdio.h>
|
|
|
83 |
#endif
|
|
|
84 |
#include <stdlib.h>
|
|
|
85 |
#include <memory.h>
|
|
|
86 |
#include <assert.h>
|
|
|
87 |
#include <stdarg.h>
|
|
|
88 |
|
|
|
89 |
#ifndef _MSC_VER
|
|
|
90 |
#ifdef __cplusplus
|
|
|
91 |
#define __forceinline inline
|
|
|
92 |
#else
|
|
|
93 |
#define __forceinline
|
|
|
94 |
#endif
|
|
|
95 |
#endif
|
|
|
96 |
|
|
|
97 |
|
|
|
98 |
// implementation:
|
|
|
99 |
typedef unsigned char uint8;
|
|
|
100 |
typedef unsigned short uint16;
|
|
|
101 |
typedef signed short int16;
|
|
|
102 |
typedef unsigned int uint32;
|
|
|
103 |
typedef signed int int32;
|
|
|
104 |
typedef unsigned int uint;
|
|
|
105 |
|
|
|
106 |
// should produce compiler error if size is wrong
|
|
|
107 |
typedef unsigned char validate_uint32[sizeof(uint32)==4 ? 1 : -1];
|
|
|
108 |
|
|
|
109 |
#if defined(STBI_NO_STDIO) && !defined(STBI_NO_WRITE)
|
|
|
110 |
#define STBI_NO_WRITE
|
|
|
111 |
#endif
|
|
|
112 |
|
|
|
113 |
#define STBI_NOTUSED(v) v=v
|
|
|
114 |
|
|
|
115 |
#ifdef _MSC_VER
|
|
|
116 |
#define STBI_HAS_LRTOL
|
|
|
117 |
#endif
|
|
|
118 |
|
|
|
119 |
#ifdef STBI_HAS_LRTOL
|
|
|
120 |
#define stbi_lrot(x,y) _lrotl(x,y)
|
|
|
121 |
#else
|
|
|
122 |
#define stbi_lrot(x,y) (((x) << (y)) | ((x) >> (32 - (y))))
|
|
|
123 |
#endif
|
|
|
124 |
|
|
|
125 |
//////////////////////////////////////////////////////////////////////////////
|
|
|
126 |
//
|
|
|
127 |
// Generic API that works on all image types
|
|
|
128 |
//
|
|
|
129 |
|
|
|
130 |
// deprecated functions
|
|
|
131 |
|
|
|
132 |
// is it a jpeg?
|
|
|
133 |
extern int stbi_jpeg_test_memory (stbi_uc const *buffer, int len);
|
|
|
134 |
extern stbi_uc *stbi_jpeg_load_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
|
|
|
135 |
extern int stbi_jpeg_info_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp);
|
|
|
136 |
|
|
|
137 |
#ifndef STBI_NO_STDIO
|
|
|
138 |
extern stbi_uc *stbi_jpeg_load (char const *filename, int *x, int *y, int *comp, int req_comp);
|
|
|
139 |
extern int stbi_jpeg_test_file (FILE *f);
|
|
|
140 |
extern stbi_uc *stbi_jpeg_load_from_file (FILE *f, int *x, int *y, int *comp, int req_comp);
|
|
|
141 |
|
|
|
142 |
extern int stbi_jpeg_info (char const *filename, int *x, int *y, int *comp);
|
|
|
143 |
extern int stbi_jpeg_info_from_file (FILE *f, int *x, int *y, int *comp);
|
|
|
144 |
#endif
|
|
|
145 |
|
|
|
146 |
// is it a png?
|
|
|
147 |
extern int stbi_png_test_memory (stbi_uc const *buffer, int len);
|
|
|
148 |
extern stbi_uc *stbi_png_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
|
|
|
149 |
extern int stbi_png_info_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp);
|
|
|
150 |
|
|
|
151 |
#ifndef STBI_NO_STDIO
|
|
|
152 |
extern stbi_uc *stbi_png_load (char const *filename, int *x, int *y, int *comp, int req_comp);
|
|
|
153 |
extern int stbi_png_info (char const *filename, int *x, int *y, int *comp);
|
|
|
154 |
extern int stbi_png_test_file (FILE *f);
|
|
|
155 |
extern stbi_uc *stbi_png_load_from_file (FILE *f, int *x, int *y, int *comp, int req_comp);
|
|
|
156 |
extern int stbi_png_info_from_file (FILE *f, int *x, int *y, int *comp);
|
|
|
157 |
#endif
|
|
|
158 |
|
|
|
159 |
// is it a bmp?
|
|
|
160 |
extern int stbi_bmp_test_memory (stbi_uc const *buffer, int len);
|
|
|
161 |
|
|
|
162 |
extern stbi_uc *stbi_bmp_load (char const *filename, int *x, int *y, int *comp, int req_comp);
|
|
|
163 |
extern stbi_uc *stbi_bmp_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
|
|
|
164 |
#ifndef STBI_NO_STDIO
|
|
|
165 |
extern int stbi_bmp_test_file (FILE *f);
|
|
|
166 |
extern stbi_uc *stbi_bmp_load_from_file (FILE *f, int *x, int *y, int *comp, int req_comp);
|
|
|
167 |
#endif
|
|
|
168 |
|
|
|
169 |
// is it a tga?
|
|
|
170 |
extern int stbi_tga_test_memory (stbi_uc const *buffer, int len);
|
|
|
171 |
|
|
|
172 |
extern stbi_uc *stbi_tga_load (char const *filename, int *x, int *y, int *comp, int req_comp);
|
|
|
173 |
extern stbi_uc *stbi_tga_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
|
|
|
174 |
#ifndef STBI_NO_STDIO
|
|
|
175 |
extern int stbi_tga_test_file (FILE *f);
|
|
|
176 |
extern stbi_uc *stbi_tga_load_from_file (FILE *f, int *x, int *y, int *comp, int req_comp);
|
|
|
177 |
#endif
|
|
|
178 |
|
|
|
179 |
// is it a psd?
|
|
|
180 |
extern int stbi_psd_test_memory (stbi_uc const *buffer, int len);
|
|
|
181 |
|
|
|
182 |
extern stbi_uc *stbi_psd_load (char const *filename, int *x, int *y, int *comp, int req_comp);
|
|
|
183 |
extern stbi_uc *stbi_psd_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
|
|
|
184 |
#ifndef STBI_NO_STDIO
|
|
|
185 |
extern int stbi_psd_test_file (FILE *f);
|
|
|
186 |
extern stbi_uc *stbi_psd_load_from_file (FILE *f, int *x, int *y, int *comp, int req_comp);
|
|
|
187 |
#endif
|
|
|
188 |
|
|
|
189 |
// is it an hdr?
|
|
|
190 |
extern int stbi_hdr_test_memory (stbi_uc const *buffer, int len);
|
|
|
191 |
|
|
|
192 |
extern float * stbi_hdr_load (char const *filename, int *x, int *y, int *comp, int req_comp);
|
|
|
193 |
extern float * stbi_hdr_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
|
|
|
194 |
extern stbi_uc *stbi_hdr_load_rgbe (char const *filename, int *x, int *y, int *comp, int req_comp);
|
|
|
195 |
#ifndef STBI_NO_STDIO
|
|
|
196 |
extern int stbi_hdr_test_file (FILE *f);
|
|
|
197 |
extern float * stbi_hdr_load_from_file (FILE *f, int *x, int *y, int *comp, int req_comp);
|
|
|
198 |
extern stbi_uc *stbi_hdr_load_rgbe_file (FILE *f, int *x, int *y, int *comp, int req_comp);
|
|
|
199 |
#endif
|
|
|
200 |
|
|
|
201 |
// is it a pic?
|
|
|
202 |
extern int stbi_pic_test_memory (stbi_uc const *buffer, int len);
|
|
|
203 |
|
|
|
204 |
extern stbi_uc *stbi_pic_load (char const *filename, int *x, int *y, int *comp, int req_comp);
|
|
|
205 |
extern stbi_uc *stbi_pic_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
|
|
|
206 |
#ifndef STBI_NO_STDIO
|
|
|
207 |
extern int stbi_pic_test_file (FILE *f);
|
|
|
208 |
extern stbi_uc *stbi_pic_load_from_file (FILE *f, int *x, int *y, int *comp, int req_comp);
|
|
|
209 |
#endif
|
|
|
210 |
|
|
|
211 |
// is it a gif?
|
|
|
212 |
extern int stbi_gif_test_memory (stbi_uc const *buffer, int len);
|
|
|
213 |
|
|
|
214 |
extern stbi_uc *stbi_gif_load (char const *filename, int *x, int *y, int *comp, int req_comp);
|
|
|
215 |
extern stbi_uc *stbi_gif_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp);
|
|
|
216 |
extern int stbi_gif_info_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp);
|
|
|
217 |
|
|
|
218 |
#ifndef STBI_NO_STDIO
|
|
|
219 |
extern int stbi_gif_test_file (FILE *f);
|
|
|
220 |
extern stbi_uc *stbi_gif_load_from_file (FILE *f, int *x, int *y, int *comp, int req_comp);
|
|
|
221 |
extern int stbi_gif_info (char const *filename, int *x, int *y, int *comp);
|
|
|
222 |
extern int stbi_gif_info_from_file (FILE *f, int *x, int *y, int *comp);
|
|
|
223 |
#endif
|
|
|
224 |
|
|
|
225 |
|
|
|
226 |
// this is not threadsafe
|
|
|
227 |
static const char *failure_reason;
|
|
|
228 |
|
|
|
229 |
const char *stbi_failure_reason(void)
|
|
|
230 |
{
|
|
|
231 |
return failure_reason;
|
|
|
232 |
}
|
|
|
233 |
|
|
|
234 |
static int e(const char *str)
|
|
|
235 |
{
|
|
|
236 |
failure_reason = str;
|
|
|
237 |
return 0;
|
|
|
238 |
}
|
|
|
239 |
|
|
|
240 |
#ifdef STBI_NO_FAILURE_STRINGS
|
|
|
241 |
#define e(x,y) 0
|
|
|
242 |
#elif defined(STBI_FAILURE_USERMSG)
|
|
|
243 |
#define e(x,y) e(y)
|
|
|
244 |
#else
|
|
|
245 |
#define e(x,y) e(x)
|
|
|
246 |
#endif
|
|
|
247 |
|
|
|
248 |
#define epf(x,y) ((float *) (e(x,y)?NULL:NULL))
|
|
|
249 |
#define epuc(x,y) ((unsigned char *) (e(x,y)?NULL:NULL))
|
|
|
250 |
|
|
|
251 |
void stbi_image_free(void *retval_from_stbi_load)
|
|
|
252 |
{
|
|
|
253 |
free(retval_from_stbi_load);
|
|
|
254 |
}
|
|
|
255 |
|
|
|
256 |
#define MAX_LOADERS 32
|
|
|
257 |
stbi_loader *loaders[MAX_LOADERS];
|
|
|
258 |
static int max_loaders = 0;
|
|
|
259 |
|
|
|
260 |
int stbi_register_loader(stbi_loader *loader)
|
|
|
261 |
{
|
|
|
262 |
int i;
|
|
|
263 |
for (i=0; i < MAX_LOADERS; ++i) {
|
|
|
264 |
// already present?
|
|
|
265 |
if (loaders[i] == loader)
|
|
|
266 |
return 1;
|
|
|
267 |
// end of the list?
|
|
|
268 |
if (loaders[i] == NULL) {
|
|
|
269 |
loaders[i] = loader;
|
|
|
270 |
max_loaders = i+1;
|
|
|
271 |
return 1;
|
|
|
272 |
}
|
|
|
273 |
}
|
|
|
274 |
// no room for it
|
|
|
275 |
return 0;
|
|
|
276 |
}
|
|
|
277 |
|
|
|
278 |
#ifndef STBI_NO_HDR
|
|
|
279 |
static float *ldr_to_hdr(stbi_uc *data, int x, int y, int comp);
|
|
|
280 |
static stbi_uc *hdr_to_ldr(float *data, int x, int y, int comp);
|
|
|
281 |
#endif
|
|
|
282 |
|
|
|
283 |
#ifndef STBI_NO_STDIO
|
|
|
284 |
unsigned char *stbi_load(char const *filename, int *x, int *y, int *comp, int req_comp)
|
|
|
285 |
{
|
|
|
286 |
FILE *f = fopen(filename, "rb");
|
|
|
287 |
unsigned char *result;
|
|
|
288 |
if (!f) return epuc("can't fopen", "Unable to open file");
|
|
|
289 |
result = stbi_load_from_file(f,x,y,comp,req_comp);
|
|
|
290 |
fclose(f);
|
|
|
291 |
return result;
|
|
|
292 |
}
|
|
|
293 |
|
|
|
294 |
unsigned char *stbi_load_from_file(FILE *f, int *x, int *y, int *comp, int req_comp)
|
|
|
295 |
{
|
|
|
296 |
int i;
|
|
|
297 |
if (stbi_jpeg_test_file(f)) return stbi_jpeg_load_from_file(f,x,y,comp,req_comp);
|
|
|
298 |
if (stbi_png_test_file(f)) return stbi_png_load_from_file(f,x,y,comp,req_comp);
|
|
|
299 |
if (stbi_bmp_test_file(f)) return stbi_bmp_load_from_file(f,x,y,comp,req_comp);
|
|
|
300 |
if (stbi_gif_test_file(f)) return stbi_gif_load_from_file(f,x,y,comp,req_comp);
|
|
|
301 |
if (stbi_psd_test_file(f)) return stbi_psd_load_from_file(f,x,y,comp,req_comp);
|
|
|
302 |
if (stbi_pic_test_file(f)) return stbi_pic_load_from_file(f,x,y,comp,req_comp);
|
|
|
303 |
|
|
|
304 |
#ifndef STBI_NO_HDR
|
|
|
305 |
if (stbi_hdr_test_file(f)) {
|
|
|
306 |
float *hdr = stbi_hdr_load_from_file(f, x,y,comp,req_comp);
|
|
|
307 |
return hdr_to_ldr(hdr, *x, *y, req_comp ? req_comp : *comp);
|
|
|
308 |
}
|
|
|
309 |
#endif
|
|
|
310 |
|
|
|
311 |
for (i=0; i < max_loaders; ++i)
|
|
|
312 |
if (loaders[i]->test_file(f))
|
|
|
313 |
return loaders[i]->load_from_file(f,x,y,comp,req_comp);
|
|
|
314 |
// test tga last because it's a crappy test!
|
|
|
315 |
if (stbi_tga_test_file(f))
|
|
|
316 |
return stbi_tga_load_from_file(f,x,y,comp,req_comp);
|
|
|
317 |
return epuc("unknown image type", "Image not of any known type, or corrupt");
|
|
|
318 |
}
|
|
|
319 |
#endif
|
|
|
320 |
|
|
|
321 |
unsigned char *stbi_load_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp)
|
|
|
322 |
{
|
|
|
323 |
int i;
|
|
|
324 |
if (stbi_jpeg_test_memory(buffer,len)) return stbi_jpeg_load_from_memory(buffer,len,x,y,comp,req_comp);
|
|
|
325 |
if (stbi_png_test_memory(buffer,len)) return stbi_png_load_from_memory(buffer,len,x,y,comp,req_comp);
|
|
|
326 |
if (stbi_bmp_test_memory(buffer,len)) return stbi_bmp_load_from_memory(buffer,len,x,y,comp,req_comp);
|
|
|
327 |
if (stbi_gif_test_memory(buffer,len)) return stbi_gif_load_from_memory(buffer,len,x,y,comp,req_comp);
|
|
|
328 |
if (stbi_psd_test_memory(buffer,len)) return stbi_psd_load_from_memory(buffer,len,x,y,comp,req_comp);
|
|
|
329 |
if (stbi_pic_test_memory(buffer,len)) return stbi_pic_load_from_memory(buffer,len,x,y,comp,req_comp);
|
|
|
330 |
|
|
|
331 |
#ifndef STBI_NO_HDR
|
|
|
332 |
if (stbi_hdr_test_memory(buffer, len)) {
|
|
|
333 |
float *hdr = stbi_hdr_load_from_memory(buffer, len,x,y,comp,req_comp);
|
|
|
334 |
return hdr_to_ldr(hdr, *x, *y, req_comp ? req_comp : *comp);
|
|
|
335 |
}
|
|
|
336 |
#endif
|
|
|
337 |
|
|
|
338 |
for (i=0; i < max_loaders; ++i)
|
|
|
339 |
if (loaders[i]->test_memory(buffer,len))
|
|
|
340 |
return loaders[i]->load_from_memory(buffer,len,x,y,comp,req_comp);
|
|
|
341 |
// test tga last because it's a crappy test!
|
|
|
342 |
if (stbi_tga_test_memory(buffer,len))
|
|
|
343 |
return stbi_tga_load_from_memory(buffer,len,x,y,comp,req_comp);
|
|
|
344 |
return epuc("unknown image type", "Image not of any known type, or corrupt");
|
|
|
345 |
}
|
|
|
346 |
|
|
|
347 |
#ifndef STBI_NO_HDR
|
|
|
348 |
|
|
|
349 |
#ifndef STBI_NO_STDIO
|
|
|
350 |
float *stbi_loadf(char const *filename, int *x, int *y, int *comp, int req_comp)
|
|
|
351 |
{
|
|
|
352 |
FILE *f = fopen(filename, "rb");
|
|
|
353 |
float *result;
|
|
|
354 |
if (!f) return epf("can't fopen", "Unable to open file");
|
|
|
355 |
result = stbi_loadf_from_file(f,x,y,comp,req_comp);
|
|
|
356 |
fclose(f);
|
|
|
357 |
return result;
|
|
|
358 |
}
|
|
|
359 |
|
|
|
360 |
float *stbi_loadf_from_file(FILE *f, int *x, int *y, int *comp, int req_comp)
|
|
|
361 |
{
|
|
|
362 |
unsigned char *data;
|
|
|
363 |
#ifndef STBI_NO_HDR
|
|
|
364 |
if (stbi_hdr_test_file(f))
|
|
|
365 |
return stbi_hdr_load_from_file(f,x,y,comp,req_comp);
|
|
|
366 |
#endif
|
|
|
367 |
data = stbi_load_from_file(f, x, y, comp, req_comp);
|
|
|
368 |
if (data)
|
|
|
369 |
return ldr_to_hdr(data, *x, *y, req_comp ? req_comp : *comp);
|
|
|
370 |
return epf("unknown image type", "Image not of any known type, or corrupt");
|
|
|
371 |
}
|
|
|
372 |
#endif
|
|
|
373 |
|
|
|
374 |
float *stbi_loadf_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp)
|
|
|
375 |
{
|
|
|
376 |
stbi_uc *data;
|
|
|
377 |
#ifndef STBI_NO_HDR
|
|
|
378 |
if (stbi_hdr_test_memory(buffer, len))
|
|
|
379 |
return stbi_hdr_load_from_memory(buffer, len,x,y,comp,req_comp);
|
|
|
380 |
#endif
|
|
|
381 |
data = stbi_load_from_memory(buffer, len, x, y, comp, req_comp);
|
|
|
382 |
if (data)
|
|
|
383 |
return ldr_to_hdr(data, *x, *y, req_comp ? req_comp : *comp);
|
|
|
384 |
return epf("unknown image type", "Image not of any known type, or corrupt");
|
|
|
385 |
}
|
|
|
386 |
#endif
|
|
|
387 |
|
|
|
388 |
// these is-hdr-or-not is defined independent of whether STBI_NO_HDR is
|
|
|
389 |
// defined, for API simplicity; if STBI_NO_HDR is defined, it always
|
|
|
390 |
// reports false!
|
|
|
391 |
|
|
|
392 |
int stbi_is_hdr_from_memory(stbi_uc const *buffer, int len)
|
|
|
393 |
{
|
|
|
394 |
#ifndef STBI_NO_HDR
|
|
|
395 |
return stbi_hdr_test_memory(buffer, len);
|
|
|
396 |
#else
|
|
|
397 |
STBI_NOTUSED(buffer);
|
|
|
398 |
STBI_NOTUSED(len);
|
|
|
399 |
return 0;
|
|
|
400 |
#endif
|
|
|
401 |
}
|
|
|
402 |
|
|
|
403 |
#ifndef STBI_NO_STDIO
|
|
|
404 |
extern int stbi_is_hdr (char const *filename)
|
|
|
405 |
{
|
|
|
406 |
FILE *f = fopen(filename, "rb");
|
|
|
407 |
int result=0;
|
|
|
408 |
if (f) {
|
|
|
409 |
result = stbi_is_hdr_from_file(f);
|
|
|
410 |
fclose(f);
|
|
|
411 |
}
|
|
|
412 |
return result;
|
|
|
413 |
}
|
|
|
414 |
|
|
|
415 |
extern int stbi_is_hdr_from_file(FILE *f)
|
|
|
416 |
{
|
|
|
417 |
#ifndef STBI_NO_HDR
|
|
|
418 |
return stbi_hdr_test_file(f);
|
|
|
419 |
#else
|
|
|
420 |
return 0;
|
|
|
421 |
#endif
|
|
|
422 |
}
|
|
|
423 |
|
|
|
424 |
#endif
|
|
|
425 |
|
|
|
426 |
#ifndef STBI_NO_HDR
|
|
|
427 |
static float h2l_gamma_i=1.0f/2.2f, h2l_scale_i=1.0f;
|
|
|
428 |
static float l2h_gamma=2.2f, l2h_scale=1.0f;
|
|
|
429 |
|
|
|
430 |
void stbi_hdr_to_ldr_gamma(float gamma) { h2l_gamma_i = 1/gamma; }
|
|
|
431 |
void stbi_hdr_to_ldr_scale(float scale) { h2l_scale_i = 1/scale; }
|
|
|
432 |
|
|
|
433 |
void stbi_ldr_to_hdr_gamma(float gamma) { l2h_gamma = gamma; }
|
|
|
434 |
void stbi_ldr_to_hdr_scale(float scale) { l2h_scale = scale; }
|
|
|
435 |
#endif
|
|
|
436 |
|
|
|
437 |
|
|
|
438 |
//////////////////////////////////////////////////////////////////////////////
|
|
|
439 |
//
|
|
|
440 |
// Common code used by all image loaders
|
|
|
441 |
//
|
|
|
442 |
|
|
|
443 |
enum
|
|
|
444 |
{
|
|
|
445 |
SCAN_load=0,
|
|
|
446 |
SCAN_type,
|
|
|
447 |
SCAN_header
|
|
|
448 |
};
|
|
|
449 |
|
|
|
450 |
typedef struct
|
|
|
451 |
{
|
|
|
452 |
uint32 img_x, img_y;
|
|
|
453 |
int img_n, img_out_n;
|
|
|
454 |
|
|
|
455 |
#ifndef STBI_NO_STDIO
|
|
|
456 |
FILE *img_file;
|
|
|
457 |
int buflen;
|
|
|
458 |
uint8 buffer_start[128];
|
|
|
459 |
int from_file;
|
|
|
460 |
#endif
|
|
|
461 |
uint8 *img_buffer, *img_buffer_end;
|
|
|
462 |
} stbi;
|
|
|
463 |
|
|
|
464 |
#ifndef STBI_NO_STDIO
|
|
|
465 |
static void start_file(stbi *s, FILE *f)
|
|
|
466 |
{
|
|
|
467 |
s->img_file = f;
|
|
|
468 |
s->buflen = sizeof(s->buffer_start);
|
|
|
469 |
s->img_buffer_end = s->buffer_start + s->buflen;
|
|
|
470 |
s->img_buffer = s->img_buffer_end;
|
|
|
471 |
s->from_file = 1;
|
|
|
472 |
}
|
|
|
473 |
#endif
|
|
|
474 |
|
|
|
475 |
static void start_mem(stbi *s, uint8 const *buffer, int len)
|
|
|
476 |
{
|
|
|
477 |
#ifndef STBI_NO_STDIO
|
|
|
478 |
s->img_file = NULL;
|
|
|
479 |
s->from_file = 0;
|
|
|
480 |
#endif
|
|
|
481 |
s->img_buffer = (uint8 *) buffer;
|
|
|
482 |
s->img_buffer_end = (uint8 *) buffer+len;
|
|
|
483 |
}
|
|
|
484 |
|
|
|
485 |
#ifndef STBI_NO_STDIO
|
|
|
486 |
static void refill_buffer(stbi *s)
|
|
|
487 |
{
|
|
|
488 |
int n = fread(s->buffer_start, 1, s->buflen, s->img_file);
|
|
|
489 |
if (n == 0) {
|
|
|
490 |
s->from_file = 0;
|
|
|
491 |
s->img_buffer = s->img_buffer_end-1;
|
|
|
492 |
*s->img_buffer = 0;
|
|
|
493 |
} else {
|
|
|
494 |
s->img_buffer = s->buffer_start;
|
|
|
495 |
s->img_buffer_end = s->buffer_start + n;
|
|
|
496 |
}
|
|
|
497 |
}
|
|
|
498 |
#endif
|
|
|
499 |
|
|
|
500 |
__forceinline static int get8(stbi *s)
|
|
|
501 |
{
|
|
|
502 |
if (s->img_buffer < s->img_buffer_end)
|
|
|
503 |
return *s->img_buffer++;
|
|
|
504 |
#ifndef STBI_NO_STDIO
|
|
|
505 |
if (s->from_file) {
|
|
|
506 |
refill_buffer(s);
|
|
|
507 |
return *s->img_buffer++;
|
|
|
508 |
}
|
|
|
509 |
#endif
|
|
|
510 |
return 0;
|
|
|
511 |
}
|
|
|
512 |
|
|
|
513 |
__forceinline static int at_eof(stbi *s)
|
|
|
514 |
{
|
|
|
515 |
#ifndef STBI_NO_STDIO
|
|
|
516 |
if (s->img_file) {
|
|
|
517 |
if (!feof(s->img_file)) return 0;
|
|
|
518 |
// if feof() is true, check if buffer = end
|
|
|
519 |
// special case: we've only got the special 0 character at the end
|
|
|
520 |
if (s->from_file == 0) return 1;
|
|
|
521 |
}
|
|
|
522 |
#endif
|
|
|
523 |
return s->img_buffer >= s->img_buffer_end;
|
|
|
524 |
}
|
|
|
525 |
|
|
|
526 |
__forceinline static uint8 get8u(stbi *s)
|
|
|
527 |
{
|
|
|
528 |
return (uint8) get8(s);
|
|
|
529 |
}
|
|
|
530 |
|
|
|
531 |
static void skip(stbi *s, int n)
|
|
|
532 |
{
|
|
|
533 |
#ifndef STBI_NO_STDIO
|
|
|
534 |
if (s->img_file) {
|
|
|
535 |
int blen = s->img_buffer_end - s->img_buffer;
|
|
|
536 |
if (blen < n) {
|
|
|
537 |
s->img_buffer = s->img_buffer_end;
|
|
|
538 |
fseek(s->img_file, n - blen, SEEK_CUR);
|
|
|
539 |
return;
|
|
|
540 |
}
|
|
|
541 |
}
|
|
|
542 |
#endif
|
|
|
543 |
s->img_buffer += n;
|
|
|
544 |
}
|
|
|
545 |
|
|
|
546 |
static int getn(stbi *s, stbi_uc *buffer, int n)
|
|
|
547 |
{
|
|
|
548 |
#ifndef STBI_NO_STDIO
|
|
|
549 |
if (s->img_file) {
|
|
|
550 |
int blen = s->img_buffer_end - s->img_buffer;
|
|
|
551 |
if (blen < n) {
|
|
|
552 |
int res;
|
|
|
553 |
memcpy(buffer, s->img_buffer, blen);
|
|
|
554 |
res = ((int) fread(buffer + blen, 1, n - blen, s->img_file) == (n-blen));
|
|
|
555 |
s->img_buffer = s->img_buffer_end;
|
|
|
556 |
return res;
|
|
|
557 |
}
|
|
|
558 |
}
|
|
|
559 |
#endif
|
|
|
560 |
if (s->img_buffer+n <= s->img_buffer_end) {
|
|
|
561 |
memcpy(buffer, s->img_buffer, n);
|
|
|
562 |
s->img_buffer += n;
|
|
|
563 |
return 1;
|
|
|
564 |
} else
|
|
|
565 |
return 0;
|
|
|
566 |
}
|
|
|
567 |
|
|
|
568 |
static int get16(stbi *s)
|
|
|
569 |
{
|
|
|
570 |
int z = get8(s);
|
|
|
571 |
return (z << 8) + get8(s);
|
|
|
572 |
}
|
|
|
573 |
|
|
|
574 |
static uint32 get32(stbi *s)
|
|
|
575 |
{
|
|
|
576 |
uint32 z = get16(s);
|
|
|
577 |
return (z << 16) + get16(s);
|
|
|
578 |
}
|
|
|
579 |
|
|
|
580 |
static int get16le(stbi *s)
|
|
|
581 |
{
|
|
|
582 |
int z = get8(s);
|
|
|
583 |
return z + (get8(s) << 8);
|
|
|
584 |
}
|
|
|
585 |
|
|
|
586 |
static uint32 get32le(stbi *s)
|
|
|
587 |
{
|
|
|
588 |
uint32 z = get16le(s);
|
|
|
589 |
return z + (get16le(s) << 16);
|
|
|
590 |
}
|
|
|
591 |
|
|
|
592 |
//////////////////////////////////////////////////////////////////////////////
|
|
|
593 |
//
|
|
|
594 |
// generic converter from built-in img_n to req_comp
|
|
|
595 |
// individual types do this automatically as much as possible (e.g. jpeg
|
|
|
596 |
// does all cases internally since it needs to colorspace convert anyway,
|
|
|
597 |
// and it never has alpha, so very few cases ). png can automatically
|
|
|
598 |
// interleave an alpha=255 channel, but falls back to this for other cases
|
|
|
599 |
//
|
|
|
600 |
// assume data buffer is malloced, so malloc a new one and free that one
|
|
|
601 |
// only failure mode is malloc failing
|
|
|
602 |
|
|
|
603 |
static uint8 compute_y(int r, int g, int b)
|
|
|
604 |
{
|
|
|
605 |
return (uint8) (((r*77) + (g*150) + (29*b)) >> 8);
|
|
|
606 |
}
|
|
|
607 |
|
|
|
608 |
static unsigned char *convert_format(unsigned char *data, int img_n, int req_comp, uint x, uint y)
|
|
|
609 |
{
|
|
|
610 |
int i,j;
|
|
|
611 |
unsigned char *good;
|
|
|
612 |
|
|
|
613 |
if (req_comp == img_n) return data;
|
|
|
614 |
assert(req_comp >= 1 && req_comp <= 4);
|
|
|
615 |
|
|
|
616 |
good = (unsigned char *) malloc(req_comp * x * y);
|
|
|
617 |
if (good == NULL) {
|
|
|
618 |
free(data);
|
|
|
619 |
return epuc("outofmem", "Out of memory");
|
|
|
620 |
}
|
|
|
621 |
|
|
|
622 |
for (j=0; j < (int) y; ++j) {
|
|
|
623 |
unsigned char *src = data + j * x * img_n ;
|
|
|
624 |
unsigned char *dest = good + j * x * req_comp;
|
|
|
625 |
|
|
|
626 |
#define COMBO(a,b) ((a)*8+(b))
|
|
|
627 |
#define CASE(a,b) case COMBO(a,b): for(i=x-1; i >= 0; --i, src += a, dest += b)
|
|
|
628 |
// convert source image with img_n components to one with req_comp components;
|
|
|
629 |
// avoid switch per pixel, so use switch per scanline and massive macros
|
|
|
630 |
switch (COMBO(img_n, req_comp)) {
|
|
|
631 |
CASE(1,2) dest[0]=src[0], dest[1]=255; break;
|
|
|
632 |
CASE(1,3) dest[0]=dest[1]=dest[2]=src[0]; break;
|
|
|
633 |
CASE(1,4) dest[0]=dest[1]=dest[2]=src[0], dest[3]=255; break;
|
|
|
634 |
CASE(2,1) dest[0]=src[0]; break;
|
|
|
635 |
CASE(2,3) dest[0]=dest[1]=dest[2]=src[0]; break;
|
|
|
636 |
CASE(2,4) dest[0]=dest[1]=dest[2]=src[0], dest[3]=src[1]; break;
|
|
|
637 |
CASE(3,4) dest[0]=src[0],dest[1]=src[1],dest[2]=src[2],dest[3]=255; break;
|
|
|
638 |
CASE(3,1) dest[0]=compute_y(src[0],src[1],src[2]); break;
|
|
|
639 |
CASE(3,2) dest[0]=compute_y(src[0],src[1],src[2]), dest[1] = 255; break;
|
|
|
640 |
CASE(4,1) dest[0]=compute_y(src[0],src[1],src[2]); break;
|
|
|
641 |
CASE(4,2) dest[0]=compute_y(src[0],src[1],src[2]), dest[1] = src[3]; break;
|
|
|
642 |
CASE(4,3) dest[0]=src[0],dest[1]=src[1],dest[2]=src[2]; break;
|
|
|
643 |
default: assert(0);
|
|
|
644 |
}
|
|
|
645 |
#undef CASE
|
|
|
646 |
}
|
|
|
647 |
|
|
|
648 |
free(data);
|
|
|
649 |
return good;
|
|
|
650 |
}
|
|
|
651 |
|
|
|
652 |
#ifndef STBI_NO_HDR
|
|
|
653 |
static float *ldr_to_hdr(stbi_uc *data, int x, int y, int comp)
|
|
|
654 |
{
|
|
|
655 |
int i,k,n;
|
|
|
656 |
float *output = (float *) malloc(x * y * comp * sizeof(float));
|
|
|
657 |
if (output == NULL) { free(data); return epf("outofmem", "Out of memory"); }
|
|
|
658 |
// compute number of non-alpha components
|
|
|
659 |
if (comp & 1) n = comp; else n = comp-1;
|
|
|
660 |
for (i=0; i < x*y; ++i) {
|
|
|
661 |
for (k=0; k < n; ++k) {
|
|
|
662 |
output[i*comp + k] = (float) pow(data[i*comp+k]/255.0f, l2h_gamma) * l2h_scale;
|
|
|
663 |
}
|
|
|
664 |
if (k < comp) output[i*comp + k] = data[i*comp+k]/255.0f;
|
|
|
665 |
}
|
|
|
666 |
free(data);
|
|
|
667 |
return output;
|
|
|
668 |
}
|
|
|
669 |
|
|
|
670 |
#define float2int(x) ((int) (x))
|
|
|
671 |
static stbi_uc *hdr_to_ldr(float *data, int x, int y, int comp)
|
|
|
672 |
{
|
|
|
673 |
int i,k,n;
|
|
|
674 |
stbi_uc *output = (stbi_uc *) malloc(x * y * comp);
|
|
|
675 |
if (output == NULL) { free(data); return epuc("outofmem", "Out of memory"); }
|
|
|
676 |
// compute number of non-alpha components
|
|
|
677 |
if (comp & 1) n = comp; else n = comp-1;
|
|
|
678 |
for (i=0; i < x*y; ++i) {
|
|
|
679 |
for (k=0; k < n; ++k) {
|
|
|
680 |
float z = (float) pow(data[i*comp+k]*h2l_scale_i, h2l_gamma_i) * 255 + 0.5f;
|
|
|
681 |
if (z < 0) z = 0;
|
|
|
682 |
if (z > 255) z = 255;
|
|
|
683 |
output[i*comp + k] = (uint8) float2int(z);
|
|
|
684 |
}
|
|
|
685 |
if (k < comp) {
|
|
|
686 |
float z = data[i*comp+k] * 255 + 0.5f;
|
|
|
687 |
if (z < 0) z = 0;
|
|
|
688 |
if (z > 255) z = 255;
|
|
|
689 |
output[i*comp + k] = (uint8) float2int(z);
|
|
|
690 |
}
|
|
|
691 |
}
|
|
|
692 |
free(data);
|
|
|
693 |
return output;
|
|
|
694 |
}
|
|
|
695 |
#endif
|
|
|
696 |
|
|
|
697 |
//////////////////////////////////////////////////////////////////////////////
|
|
|
698 |
//
|
|
|
699 |
// "baseline" JPEG/JFIF decoder (not actually fully baseline implementation)
|
|
|
700 |
//
|
|
|
701 |
// simple implementation
|
|
|
702 |
// - channel subsampling of at most 2 in each dimension
|
|
|
703 |
// - doesn't support delayed output of y-dimension
|
|
|
704 |
// - simple interface (only one output format: 8-bit interleaved RGB)
|
|
|
705 |
// - doesn't try to recover corrupt jpegs
|
|
|
706 |
// - doesn't allow partial loading, loading multiple at once
|
|
|
707 |
// - still fast on x86 (copying globals into locals doesn't help x86)
|
|
|
708 |
// - allocates lots of intermediate memory (full size of all components)
|
|
|
709 |
// - non-interleaved case requires this anyway
|
|
|
710 |
// - allows good upsampling (see next)
|
|
|
711 |
// high-quality
|
|
|
712 |
// - upsampled channels are bilinearly interpolated, even across blocks
|
|
|
713 |
// - quality integer IDCT derived from IJG's 'slow'
|
|
|
714 |
// performance
|
|
|
715 |
// - fast huffman; reasonable integer IDCT
|
|
|
716 |
// - uses a lot of intermediate memory, could cache poorly
|
|
|
717 |
// - load http://nothings.org/remote/anemones.jpg 3 times on 2.8Ghz P4
|
|
|
718 |
// stb_jpeg: 1.34 seconds (MSVC6, default release build)
|
|
|
719 |
// stb_jpeg: 1.06 seconds (MSVC6, processor = Pentium Pro)
|
|
|
720 |
// IJL11.dll: 1.08 seconds (compiled by intel)
|
|
|
721 |
// IJG 1998: 0.98 seconds (MSVC6, makefile provided by IJG)
|
|
|
722 |
// IJG 1998: 0.95 seconds (MSVC6, makefile + proc=PPro)
|
|
|
723 |
|
|
|
724 |
// huffman decoding acceleration
|
|
|
725 |
#define FAST_BITS 9 // larger handles more cases; smaller stomps less cache
|
|
|
726 |
|
|
|
727 |
typedef struct
|
|
|
728 |
{
|
|
|
729 |
uint8 fast[1 << FAST_BITS];
|
|
|
730 |
// weirdly, repacking this into AoS is a 10% speed loss, instead of a win
|
|
|
731 |
uint16 code[256];
|
|
|
732 |
uint8 values[256];
|
|
|
733 |
uint8 size[257];
|
|
|
734 |
unsigned int maxcode[18];
|
|
|
735 |
int delta[17]; // old 'firstsymbol' - old 'firstcode'
|
|
|
736 |
} huffman;
|
|
|
737 |
|
|
|
738 |
typedef struct
|
|
|
739 |
{
|
|
|
740 |
#ifdef STBI_SIMD
|
|
|
741 |
unsigned short dequant2[4][64];
|
|
|
742 |
#endif
|
|
|
743 |
stbi s;
|
|
|
744 |
huffman huff_dc[4];
|
|
|
745 |
huffman huff_ac[4];
|
|
|
746 |
uint8 dequant[4][64];
|
|
|
747 |
|
|
|
748 |
// sizes for components, interleaved MCUs
|
|
|
749 |
int img_h_max, img_v_max;
|
|
|
750 |
int img_mcu_x, img_mcu_y;
|
|
|
751 |
int img_mcu_w, img_mcu_h;
|
|
|
752 |
|
|
|
753 |
// definition of jpeg image component
|
|
|
754 |
struct
|
|
|
755 |
{
|
|
|
756 |
int id;
|
|
|
757 |
int h,v;
|
|
|
758 |
int tq;
|
|
|
759 |
int hd,ha;
|
|
|
760 |
int dc_pred;
|
|
|
761 |
|
|
|
762 |
int x,y,w2,h2;
|
|
|
763 |
uint8 *data;
|
|
|
764 |
void *raw_data;
|
|
|
765 |
uint8 *linebuf;
|
|
|
766 |
} img_comp[4];
|
|
|
767 |
|
|
|
768 |
uint32 code_buffer; // jpeg entropy-coded buffer
|
|
|
769 |
int code_bits; // number of valid bits
|
|
|
770 |
unsigned char marker; // marker seen while filling entropy buffer
|
|
|
771 |
int nomore; // flag if we saw a marker so must stop
|
|
|
772 |
|
|
|
773 |
int scan_n, order[4];
|
|
|
774 |
int restart_interval, todo;
|
|
|
775 |
} jpeg;
|
|
|
776 |
|
|
|
777 |
static int build_huffman(huffman *h, int *count)
|
|
|
778 |
{
|
|
|
779 |
int i,j,k=0,code;
|
|
|
780 |
// build size list for each symbol (from JPEG spec)
|
|
|
781 |
for (i=0; i < 16; ++i)
|
|
|
782 |
for (j=0; j < count[i]; ++j)
|
|
|
783 |
h->size[k++] = (uint8) (i+1);
|
|
|
784 |
h->size[k] = 0;
|
|
|
785 |
|
|
|
786 |
// compute actual symbols (from jpeg spec)
|
|
|
787 |
code = 0;
|
|
|
788 |
k = 0;
|
|
|
789 |
for(j=1; j <= 16; ++j) {
|
|
|
790 |
// compute delta to add to code to compute symbol id
|
|
|
791 |
h->delta[j] = k - code;
|
|
|
792 |
if (h->size[k] == j) {
|
|
|
793 |
while (h->size[k] == j)
|
|
|
794 |
h->code[k++] = (uint16) (code++);
|
|
|
795 |
if (code-1 >= (1 << j)) return e("bad code lengths","Corrupt JPEG");
|
|
|
796 |
}
|
|
|
797 |
// compute largest code + 1 for this size, preshifted as needed later
|
|
|
798 |
h->maxcode[j] = code << (16-j);
|
|
|
799 |
code <<= 1;
|
|
|
800 |
}
|
|
|
801 |
h->maxcode[j] = 0xffffffff;
|
|
|
802 |
|
|
|
803 |
// build non-spec acceleration table; 255 is flag for not-accelerated
|
|
|
804 |
memset(h->fast, 255, 1 << FAST_BITS);
|
|
|
805 |
for (i=0; i < k; ++i) {
|
|
|
806 |
int s = h->size[i];
|
|
|
807 |
if (s <= FAST_BITS) {
|
|
|
808 |
int c = h->code[i] << (FAST_BITS-s);
|
|
|
809 |
int m = 1 << (FAST_BITS-s);
|
|
|
810 |
for (j=0; j < m; ++j) {
|
|
|
811 |
h->fast[c+j] = (uint8) i;
|
|
|
812 |
}
|
|
|
813 |
}
|
|
|
814 |
}
|
|
|
815 |
return 1;
|
|
|
816 |
}
|
|
|
817 |
|
|
|
818 |
static void grow_buffer_unsafe(jpeg *j)
|
|
|
819 |
{
|
|
|
820 |
do {
|
|
|
821 |
int b = j->nomore ? 0 : get8(&j->s);
|
|
|
822 |
if (b == 0xff) {
|
|
|
823 |
int c = get8(&j->s);
|
|
|
824 |
if (c != 0) {
|
|
|
825 |
j->marker = (unsigned char) c;
|
|
|
826 |
j->nomore = 1;
|
|
|
827 |
return;
|
|
|
828 |
}
|
|
|
829 |
}
|
|
|
830 |
j->code_buffer |= b << (24 - j->code_bits);
|
|
|
831 |
j->code_bits += 8;
|
|
|
832 |
} while (j->code_bits <= 24);
|
|
|
833 |
}
|
|
|
834 |
|
|
|
835 |
// (1 << n) - 1
|
|
|
836 |
static uint32 bmask[17]={0,1,3,7,15,31,63,127,255,511,1023,2047,4095,8191,16383,32767,65535};
|
|
|
837 |
|
|
|
838 |
// decode a jpeg huffman value from the bitstream
|
|
|
839 |
__forceinline static int decode(jpeg *j, huffman *h)
|
|
|
840 |
{
|
|
|
841 |
unsigned int temp;
|
|
|
842 |
int c,k;
|
|
|
843 |
|
|
|
844 |
if (j->code_bits < 16) grow_buffer_unsafe(j);
|
|
|
845 |
|
|
|
846 |
// look at the top FAST_BITS and determine what symbol ID it is,
|
|
|
847 |
// if the code is <= FAST_BITS
|
|
|
848 |
c = (j->code_buffer >> (32 - FAST_BITS)) & ((1 << FAST_BITS)-1);
|
|
|
849 |
k = h->fast[c];
|
|
|
850 |
if (k < 255) {
|
|
|
851 |
int s = h->size[k];
|
|
|
852 |
if (s > j->code_bits)
|
|
|
853 |
return -1;
|
|
|
854 |
j->code_buffer <<= s;
|
|
|
855 |
j->code_bits -= s;
|
|
|
856 |
return h->values[k];
|
|
|
857 |
}
|
|
|
858 |
|
|
|
859 |
// naive test is to shift the code_buffer down so k bits are
|
|
|
860 |
// valid, then test against maxcode. To speed this up, we've
|
|
|
861 |
// preshifted maxcode left so that it has (16-k) 0s at the
|
|
|
862 |
// end; in other words, regardless of the number of bits, it
|
|
|
863 |
// wants to be compared against something shifted to have 16;
|
|
|
864 |
// that way we don't need to shift inside the loop.
|
|
|
865 |
temp = j->code_buffer >> 16;
|
|
|
866 |
for (k=FAST_BITS+1 ; ; ++k)
|
|
|
867 |
if (temp < h->maxcode[k])
|
|
|
868 |
break;
|
|
|
869 |
if (k == 17) {
|
|
|
870 |
// error! code not found
|
|
|
871 |
j->code_bits -= 16;
|
|
|
872 |
return -1;
|
|
|
873 |
}
|
|
|
874 |
|
|
|
875 |
if (k > j->code_bits)
|
|
|
876 |
return -1;
|
|
|
877 |
|
|
|
878 |
// convert the huffman code to the symbol id
|
|
|
879 |
c = ((j->code_buffer >> (32 - k)) & bmask[k]) + h->delta[k];
|
|
|
880 |
assert((((j->code_buffer) >> (32 - h->size[c])) & bmask[h->size[c]]) == h->code[c]);
|
|
|
881 |
|
|
|
882 |
// convert the id to a symbol
|
|
|
883 |
j->code_bits -= k;
|
|
|
884 |
j->code_buffer <<= k;
|
|
|
885 |
return h->values[c];
|
|
|
886 |
}
|
|
|
887 |
|
|
|
888 |
// combined JPEG 'receive' and JPEG 'extend', since baseline
|
|
|
889 |
// always extends everything it receives.
|
|
|
890 |
__forceinline static int extend_receive(jpeg *j, int n)
|
|
|
891 |
{
|
|
|
892 |
unsigned int m = 1 << (n-1);
|
|
|
893 |
unsigned int k;
|
|
|
894 |
if (j->code_bits < n) grow_buffer_unsafe(j);
|
|
|
895 |
|
|
|
896 |
#if 1
|
|
|
897 |
k = stbi_lrot(j->code_buffer, n);
|
|
|
898 |
j->code_buffer = k & ~bmask[n];
|
|
|
899 |
k &= bmask[n];
|
|
|
900 |
j->code_bits -= n;
|
|
|
901 |
#else
|
|
|
902 |
k = (j->code_buffer >> (32 - n)) & bmask[n];
|
|
|
903 |
j->code_bits -= n;
|
|
|
904 |
j->code_buffer <<= n;
|
|
|
905 |
#endif
|
|
|
906 |
// the following test is probably a random branch that won't
|
|
|
907 |
// predict well. I tried to table accelerate it but failed.
|
|
|
908 |
// maybe it's compiling as a conditional move?
|
|
|
909 |
if (k < m)
|
|
|
910 |
return (-1 << n) + k + 1;
|
|
|
911 |
else
|
|
|
912 |
return k;
|
|
|
913 |
}
|
|
|
914 |
|
|
|
915 |
// given a value that's at position X in the zigzag stream,
|
|
|
916 |
// where does it appear in the 8x8 matrix coded as row-major?
|
|
|
917 |
static uint8 dezigzag[64+15] =
|
|
|
918 |
{
|
|
|
919 |
0, 1, 8, 16, 9, 2, 3, 10,
|
|
|
920 |
17, 24, 32, 25, 18, 11, 4, 5,
|
|
|
921 |
12, 19, 26, 33, 40, 48, 41, 34,
|
|
|
922 |
27, 20, 13, 6, 7, 14, 21, 28,
|
|
|
923 |
35, 42, 49, 56, 57, 50, 43, 36,
|
|
|
924 |
29, 22, 15, 23, 30, 37, 44, 51,
|
|
|
925 |
58, 59, 52, 45, 38, 31, 39, 46,
|
|
|
926 |
53, 60, 61, 54, 47, 55, 62, 63,
|
|
|
927 |
// let corrupt input sample past end
|
|
|
928 |
63, 63, 63, 63, 63, 63, 63, 63,
|
|
|
929 |
63, 63, 63, 63, 63, 63, 63
|
|
|
930 |
};
|
|
|
931 |
|
|
|
932 |
// decode one 64-entry block--
|
|
|
933 |
static int decode_block(jpeg *j, short data[64], huffman *hdc, huffman *hac, int b)
|
|
|
934 |
{
|
|
|
935 |
int diff,dc,k;
|
|
|
936 |
int t = decode(j, hdc);
|
|
|
937 |
if (t < 0) return e("bad huffman code","Corrupt JPEG");
|
|
|
938 |
|
|
|
939 |
// 0 all the ac values now so we can do it 32-bits at a time
|
|
|
940 |
memset(data,0,64*sizeof(data[0]));
|
|
|
941 |
|
|
|
942 |
diff = t ? extend_receive(j, t) : 0;
|
|
|
943 |
dc = j->img_comp[b].dc_pred + diff;
|
|
|
944 |
j->img_comp[b].dc_pred = dc;
|
|
|
945 |
data[0] = (short) dc;
|
|
|
946 |
|
|
|
947 |
// decode AC components, see JPEG spec
|
|
|
948 |
k = 1;
|
|
|
949 |
do {
|
|
|
950 |
int r,s;
|
|
|
951 |
int rs = decode(j, hac);
|
|
|
952 |
if (rs < 0) return e("bad huffman code","Corrupt JPEG");
|
|
|
953 |
s = rs & 15;
|
|
|
954 |
r = rs >> 4;
|
|
|
955 |
if (s == 0) {
|
|
|
956 |
if (rs != 0xf0) break; // end block
|
|
|
957 |
k += 16;
|
|
|
958 |
} else {
|
|
|
959 |
k += r;
|
|
|
960 |
// decode into unzigzag'd location
|
|
|
961 |
data[dezigzag[k++]] = (short) extend_receive(j,s);
|
|
|
962 |
}
|
|
|
963 |
} while (k < 64);
|
|
|
964 |
return 1;
|
|
|
965 |
}
|
|
|
966 |
|
|
|
967 |
// take a -128..127 value and clamp it and convert to 0..255
|
|
|
968 |
__forceinline static uint8 clamp(int x)
|
|
|
969 |
{
|
|
|
970 |
// trick to use a single test to catch both cases
|
|
|
971 |
if ((unsigned int) x > 255) {
|
|
|
972 |
if (x < 0) return 0;
|
|
|
973 |
if (x > 255) return 255;
|
|
|
974 |
}
|
|
|
975 |
return (uint8) x;
|
|
|
976 |
}
|
|
|
977 |
|
|
|
978 |
#define f2f(x) (int) (((x) * 4096 + 0.5))
|
|
|
979 |
#define fsh(x) ((x) << 12)
|
|
|
980 |
|
|
|
981 |
// derived from jidctint -- DCT_ISLOW
|
|
|
982 |
#define IDCT_1D(s0,s1,s2,s3,s4,s5,s6,s7) \
|
|
|
983 |
int t0,t1,t2,t3,p1,p2,p3,p4,p5,x0,x1,x2,x3; \
|
|
|
984 |
p2 = s2; \
|
|
|
985 |
p3 = s6; \
|
|
|
986 |
p1 = (p2+p3) * f2f(0.5411961f); \
|
|
|
987 |
t2 = p1 + p3*f2f(-1.847759065f); \
|
|
|
988 |
t3 = p1 + p2*f2f( 0.765366865f); \
|
|
|
989 |
p2 = s0; \
|
|
|
990 |
p3 = s4; \
|
|
|
991 |
t0 = fsh(p2+p3); \
|
|
|
992 |
t1 = fsh(p2-p3); \
|
|
|
993 |
x0 = t0+t3; \
|
|
|
994 |
x3 = t0-t3; \
|
|
|
995 |
x1 = t1+t2; \
|
|
|
996 |
x2 = t1-t2; \
|
|
|
997 |
t0 = s7; \
|
|
|
998 |
t1 = s5; \
|
|
|
999 |
t2 = s3; \
|
|
|
1000 |
t3 = s1; \
|
|
|
1001 |
p3 = t0+t2; \
|
|
|
1002 |
p4 = t1+t3; \
|
|
|
1003 |
p1 = t0+t3; \
|
|
|
1004 |
p2 = t1+t2; \
|
|
|
1005 |
p5 = (p3+p4)*f2f( 1.175875602f); \
|
|
|
1006 |
t0 = t0*f2f( 0.298631336f); \
|
|
|
1007 |
t1 = t1*f2f( 2.053119869f); \
|
|
|
1008 |
t2 = t2*f2f( 3.072711026f); \
|
|
|
1009 |
t3 = t3*f2f( 1.501321110f); \
|
|
|
1010 |
p1 = p5 + p1*f2f(-0.899976223f); \
|
|
|
1011 |
p2 = p5 + p2*f2f(-2.562915447f); \
|
|
|
1012 |
p3 = p3*f2f(-1.961570560f); \
|
|
|
1013 |
p4 = p4*f2f(-0.390180644f); \
|
|
|
1014 |
t3 += p1+p4; \
|
|
|
1015 |
t2 += p2+p3; \
|
|
|
1016 |
t1 += p2+p4; \
|
|
|
1017 |
t0 += p1+p3;
|
|
|
1018 |
|
|
|
1019 |
#ifdef STBI_SIMD
|
|
|
1020 |
typedef unsigned short stbi_dequantize_t;
|
|
|
1021 |
#else
|
|
|
1022 |
typedef uint8 stbi_dequantize_t;
|
|
|
1023 |
#endif
|
|
|
1024 |
|
|
|
1025 |
// .344 seconds on 3*anemones.jpg
|
|
|
1026 |
static void idct_block(uint8 *out, int out_stride, short data[64], stbi_dequantize_t *dequantize)
|
|
|
1027 |
{
|
|
|
1028 |
int i,val[64],*v=val;
|
|
|
1029 |
stbi_dequantize_t *dq = dequantize;
|
|
|
1030 |
uint8 *o;
|
|
|
1031 |
short *d = data;
|
|
|
1032 |
|
|
|
1033 |
// columns
|
|
|
1034 |
for (i=0; i < 8; ++i,++d,++dq, ++v) {
|
|
|
1035 |
// if all zeroes, shortcut -- this avoids dequantizing 0s and IDCTing
|
|
|
1036 |
if (d[ 8]==0 && d[16]==0 && d[24]==0 && d[32]==0
|
|
|
1037 |
&& d[40]==0 && d[48]==0 && d[56]==0) {
|
|
|
1038 |
// no shortcut 0 seconds
|
|
|
1039 |
// (1|2|3|4|5|6|7)==0 0 seconds
|
|
|
1040 |
// all separate -0.047 seconds
|
|
|
1041 |
// 1 && 2|3 && 4|5 && 6|7: -0.047 seconds
|
|
|
1042 |
int dcterm = d[0] * dq[0] << 2;
|
|
|
1043 |
v[0] = v[8] = v[16] = v[24] = v[32] = v[40] = v[48] = v[56] = dcterm;
|
|
|
1044 |
} else {
|
|
|
1045 |
IDCT_1D(d[ 0]*dq[ 0],d[ 8]*dq[ 8],d[16]*dq[16],d[24]*dq[24],
|
|
|
1046 |
d[32]*dq[32],d[40]*dq[40],d[48]*dq[48],d[56]*dq[56])
|
|
|
1047 |
// constants scaled things up by 1<<12; let's bring them back
|
|
|
1048 |
// down, but keep 2 extra bits of precision
|
|
|
1049 |
x0 += 512; x1 += 512; x2 += 512; x3 += 512;
|
|
|
1050 |
v[ 0] = (x0+t3) >> 10;
|
|
|
1051 |
v[56] = (x0-t3) >> 10;
|
|
|
1052 |
v[ 8] = (x1+t2) >> 10;
|
|
|
1053 |
v[48] = (x1-t2) >> 10;
|
|
|
1054 |
v[16] = (x2+t1) >> 10;
|
|
|
1055 |
v[40] = (x2-t1) >> 10;
|
|
|
1056 |
v[24] = (x3+t0) >> 10;
|
|
|
1057 |
v[32] = (x3-t0) >> 10;
|
|
|
1058 |
}
|
|
|
1059 |
}
|
|
|
1060 |
|
|
|
1061 |
for (i=0, v=val, o=out; i < 8; ++i,v+=8,o+=out_stride) {
|
|
|
1062 |
// no fast case since the first 1D IDCT spread components out
|
|
|
1063 |
IDCT_1D(v[0],v[1],v[2],v[3],v[4],v[5],v[6],v[7])
|
|
|
1064 |
// constants scaled things up by 1<<12, plus we had 1<<2 from first
|
|
|
1065 |
// loop, plus horizontal and vertical each scale by sqrt(8) so together
|
|
|
1066 |
// we've got an extra 1<<3, so 1<<17 total we need to remove.
|
|
|
1067 |
// so we want to round that, which means adding 0.5 * 1<<17,
|
|
|
1068 |
// aka 65536. Also, we'll end up with -128 to 127 that we want
|
|
|
1069 |
// to encode as 0..255 by adding 128, so we'll add that before the shift
|
|
|
1070 |
x0 += 65536 + (128<<17);
|
|
|
1071 |
x1 += 65536 + (128<<17);
|
|
|
1072 |
x2 += 65536 + (128<<17);
|
|
|
1073 |
x3 += 65536 + (128<<17);
|
|
|
1074 |
// tried computing the shifts into temps, or'ing the temps to see
|
|
|
1075 |
// if any were out of range, but that was slower
|
|
|
1076 |
o[0] = clamp((x0+t3) >> 17);
|
|
|
1077 |
o[7] = clamp((x0-t3) >> 17);
|
|
|
1078 |
o[1] = clamp((x1+t2) >> 17);
|
|
|
1079 |
o[6] = clamp((x1-t2) >> 17);
|
|
|
1080 |
o[2] = clamp((x2+t1) >> 17);
|
|
|
1081 |
o[5] = clamp((x2-t1) >> 17);
|
|
|
1082 |
o[3] = clamp((x3+t0) >> 17);
|
|
|
1083 |
o[4] = clamp((x3-t0) >> 17);
|
|
|
1084 |
}
|
|
|
1085 |
}
|
|
|
1086 |
|
|
|
1087 |
#ifdef STBI_SIMD
|
|
|
1088 |
static stbi_idct_8x8 stbi_idct_installed = idct_block;
|
|
|
1089 |
|
|
|
1090 |
extern void stbi_install_idct(stbi_idct_8x8 func)
|
|
|
1091 |
{
|
|
|
1092 |
stbi_idct_installed = func;
|
|
|
1093 |
}
|
|
|
1094 |
#endif
|
|
|
1095 |
|
|
|
1096 |
#define MARKER_none 0xff
|
|
|
1097 |
// if there's a pending marker from the entropy stream, return that
|
|
|
1098 |
// otherwise, fetch from the stream and get a marker. if there's no
|
|
|
1099 |
// marker, return 0xff, which is never a valid marker value
|
|
|
1100 |
static uint8 get_marker(jpeg *j)
|
|
|
1101 |
{
|
|
|
1102 |
uint8 x;
|
|
|
1103 |
if (j->marker != MARKER_none) { x = j->marker; j->marker = MARKER_none; return x; }
|
|
|
1104 |
x = get8u(&j->s);
|
|
|
1105 |
if (x != 0xff) return MARKER_none;
|
|
|
1106 |
while (x == 0xff)
|
|
|
1107 |
x = get8u(&j->s);
|
|
|
1108 |
return x;
|
|
|
1109 |
}
|
|
|
1110 |
|
|
|
1111 |
// in each scan, we'll have scan_n components, and the order
|
|
|
1112 |
// of the components is specified by order[]
|
|
|
1113 |
#define RESTART(x) ((x) >= 0xd0 && (x) <= 0xd7)
|
|
|
1114 |
|
|
|
1115 |
// after a restart interval, reset the entropy decoder and
|
|
|
1116 |
// the dc prediction
|
|
|
1117 |
static void reset(jpeg *j)
|
|
|
1118 |
{
|
|
|
1119 |
j->code_bits = 0;
|
|
|
1120 |
j->code_buffer = 0;
|
|
|
1121 |
j->nomore = 0;
|
|
|
1122 |
j->img_comp[0].dc_pred = j->img_comp[1].dc_pred = j->img_comp[2].dc_pred = 0;
|
|
|
1123 |
j->marker = MARKER_none;
|
|
|
1124 |
j->todo = j->restart_interval ? j->restart_interval : 0x7fffffff;
|
|
|
1125 |
// no more than 1<<31 MCUs if no restart_interal? that's plenty safe,
|
|
|
1126 |
// since we don't even allow 1<<30 pixels
|
|
|
1127 |
}
|
|
|
1128 |
|
|
|
1129 |
static int parse_entropy_coded_data(jpeg *z)
|
|
|
1130 |
{
|
|
|
1131 |
reset(z);
|
|
|
1132 |
if (z->scan_n == 1) {
|
|
|
1133 |
int i,j;
|
|
|
1134 |
#ifdef STBI_SIMD
|
|
|
1135 |
__declspec(align(16))
|
|
|
1136 |
#endif
|
|
|
1137 |
short data[64];
|
|
|
1138 |
int n = z->order[0];
|
|
|
1139 |
// non-interleaved data, we just need to process one block at a time,
|
|
|
1140 |
// in trivial scanline order
|
|
|
1141 |
// number of blocks to do just depends on how many actual "pixels" this
|
|
|
1142 |
// component has, independent of interleaved MCU blocking and such
|
|
|
1143 |
int w = (z->img_comp[n].x+7) >> 3;
|
|
|
1144 |
int h = (z->img_comp[n].y+7) >> 3;
|
|
|
1145 |
for (j=0; j < h; ++j) {
|
|
|
1146 |
for (i=0; i < w; ++i) {
|
|
|
1147 |
if (!decode_block(z, data, z->huff_dc+z->img_comp[n].hd, z->huff_ac+z->img_comp[n].ha, n)) return 0;
|
|
|
1148 |
#ifdef STBI_SIMD
|
|
|
1149 |
stbi_idct_installed(z->img_comp[n].data+z->img_comp[n].w2*j*8+i*8, z->img_comp[n].w2, data, z->dequant2[z->img_comp[n].tq]);
|
|
|
1150 |
#else
|
|
|
1151 |
idct_block(z->img_comp[n].data+z->img_comp[n].w2*j*8+i*8, z->img_comp[n].w2, data, z->dequant[z->img_comp[n].tq]);
|
|
|
1152 |
#endif
|
|
|
1153 |
// every data block is an MCU, so countdown the restart interval
|
|
|
1154 |
if (--z->todo <= 0) {
|
|
|
1155 |
if (z->code_bits < 24) grow_buffer_unsafe(z);
|
|
|
1156 |
// if it's NOT a restart, then just bail, so we get corrupt data
|
|
|
1157 |
// rather than no data
|
|
|
1158 |
if (!RESTART(z->marker)) return 1;
|
|
|
1159 |
reset(z);
|
|
|
1160 |
}
|
|
|
1161 |
}
|
|
|
1162 |
}
|
|
|
1163 |
} else { // interleaved!
|
|
|
1164 |
int i,j,k,x,y;
|
|
|
1165 |
short data[64];
|
|
|
1166 |
for (j=0; j < z->img_mcu_y; ++j) {
|
|
|
1167 |
for (i=0; i < z->img_mcu_x; ++i) {
|
|
|
1168 |
// scan an interleaved mcu... process scan_n components in order
|
|
|
1169 |
for (k=0; k < z->scan_n; ++k) {
|
|
|
1170 |
int n = z->order[k];
|
|
|
1171 |
// scan out an mcu's worth of this component; that's just determined
|
|
|
1172 |
// by the basic H and V specified for the component
|
|
|
1173 |
for (y=0; y < z->img_comp[n].v; ++y) {
|
|
|
1174 |
for (x=0; x < z->img_comp[n].h; ++x) {
|
|
|
1175 |
int x2 = (i*z->img_comp[n].h + x)*8;
|
|
|
1176 |
int y2 = (j*z->img_comp[n].v + y)*8;
|
|
|
1177 |
if (!decode_block(z, data, z->huff_dc+z->img_comp[n].hd, z->huff_ac+z->img_comp[n].ha, n)) return 0;
|
|
|
1178 |
#ifdef STBI_SIMD
|
|
|
1179 |
stbi_idct_installed(z->img_comp[n].data+z->img_comp[n].w2*y2+x2, z->img_comp[n].w2, data, z->dequant2[z->img_comp[n].tq]);
|
|
|
1180 |
#else
|
|
|
1181 |
idct_block(z->img_comp[n].data+z->img_comp[n].w2*y2+x2, z->img_comp[n].w2, data, z->dequant[z->img_comp[n].tq]);
|
|
|
1182 |
#endif
|
|
|
1183 |
}
|
|
|
1184 |
}
|
|
|
1185 |
}
|
|
|
1186 |
// after all interleaved components, that's an interleaved MCU,
|
|
|
1187 |
// so now count down the restart interval
|
|
|
1188 |
if (--z->todo <= 0) {
|
|
|
1189 |
if (z->code_bits < 24) grow_buffer_unsafe(z);
|
|
|
1190 |
// if it's NOT a restart, then just bail, so we get corrupt data
|
|
|
1191 |
// rather than no data
|
|
|
1192 |
if (!RESTART(z->marker)) return 1;
|
|
|
1193 |
reset(z);
|
|
|
1194 |
}
|
|
|
1195 |
}
|
|
|
1196 |
}
|
|
|
1197 |
}
|
|
|
1198 |
return 1;
|
|
|
1199 |
}
|
|
|
1200 |
|
|
|
1201 |
static int process_marker(jpeg *z, int m)
|
|
|
1202 |
{
|
|
|
1203 |
int L;
|
|
|
1204 |
switch (m) {
|
|
|
1205 |
case MARKER_none: // no marker found
|
|
|
1206 |
return e("expected marker","Corrupt JPEG");
|
|
|
1207 |
|
|
|
1208 |
case 0xC2: // SOF - progressive
|
|
|
1209 |
return e("progressive jpeg","JPEG format not supported (progressive)");
|
|
|
1210 |
|
|
|
1211 |
case 0xDD: // DRI - specify restart interval
|
|
|
1212 |
if (get16(&z->s) != 4) return e("bad DRI len","Corrupt JPEG");
|
|
|
1213 |
z->restart_interval = get16(&z->s);
|
|
|
1214 |
return 1;
|
|
|
1215 |
|
|
|
1216 |
case 0xDB: // DQT - define quantization table
|
|
|
1217 |
L = get16(&z->s)-2;
|
|
|
1218 |
while (L > 0) {
|
|
|
1219 |
int q = get8(&z->s);
|
|
|
1220 |
int p = q >> 4;
|
|
|
1221 |
int t = q & 15,i;
|
|
|
1222 |
if (p != 0) return e("bad DQT type","Corrupt JPEG");
|
|
|
1223 |
if (t > 3) return e("bad DQT table","Corrupt JPEG");
|
|
|
1224 |
for (i=0; i < 64; ++i)
|
|
|
1225 |
z->dequant[t][dezigzag[i]] = get8u(&z->s);
|
|
|
1226 |
#ifdef STBI_SIMD
|
|
|
1227 |
for (i=0; i < 64; ++i)
|
|
|
1228 |
z->dequant2[t][i] = z->dequant[t][i];
|
|
|
1229 |
#endif
|
|
|
1230 |
L -= 65;
|
|
|
1231 |
}
|
|
|
1232 |
return L==0;
|
|
|
1233 |
|
|
|
1234 |
case 0xC4: // DHT - define huffman table
|
|
|
1235 |
L = get16(&z->s)-2;
|
|
|
1236 |
while (L > 0) {
|
|
|
1237 |
uint8 *v;
|
|
|
1238 |
int sizes[16],i,m=0;
|
|
|
1239 |
int q = get8(&z->s);
|
|
|
1240 |
int tc = q >> 4;
|
|
|
1241 |
int th = q & 15;
|
|
|
1242 |
if (tc > 1 || th > 3) return e("bad DHT header","Corrupt JPEG");
|
|
|
1243 |
for (i=0; i < 16; ++i) {
|
|
|
1244 |
sizes[i] = get8(&z->s);
|
|
|
1245 |
m += sizes[i];
|
|
|
1246 |
}
|
|
|
1247 |
L -= 17;
|
|
|
1248 |
if (tc == 0) {
|
|
|
1249 |
if (!build_huffman(z->huff_dc+th, sizes)) return 0;
|
|
|
1250 |
v = z->huff_dc[th].values;
|
|
|
1251 |
} else {
|
|
|
1252 |
if (!build_huffman(z->huff_ac+th, sizes)) return 0;
|
|
|
1253 |
v = z->huff_ac[th].values;
|
|
|
1254 |
}
|
|
|
1255 |
for (i=0; i < m; ++i)
|
|
|
1256 |
v[i] = get8u(&z->s);
|
|
|
1257 |
L -= m;
|
|
|
1258 |
}
|
|
|
1259 |
return L==0;
|
|
|
1260 |
}
|
|
|
1261 |
// check for comment block or APP blocks
|
|
|
1262 |
if ((m >= 0xE0 && m <= 0xEF) || m == 0xFE) {
|
|
|
1263 |
skip(&z->s, get16(&z->s)-2);
|
|
|
1264 |
return 1;
|
|
|
1265 |
}
|
|
|
1266 |
return 0;
|
|
|
1267 |
}
|
|
|
1268 |
|
|
|
1269 |
// after we see SOS
|
|
|
1270 |
static int process_scan_header(jpeg *z)
|
|
|
1271 |
{
|
|
|
1272 |
int i;
|
|
|
1273 |
int Ls = get16(&z->s);
|
|
|
1274 |
z->scan_n = get8(&z->s);
|
|
|
1275 |
if (z->scan_n < 1 || z->scan_n > 4 || z->scan_n > (int) z->s.img_n) return e("bad SOS component count","Corrupt JPEG");
|
|
|
1276 |
if (Ls != 6+2*z->scan_n) return e("bad SOS len","Corrupt JPEG");
|
|
|
1277 |
for (i=0; i < z->scan_n; ++i) {
|
|
|
1278 |
int id = get8(&z->s), which;
|
|
|
1279 |
int q = get8(&z->s);
|
|
|
1280 |
for (which = 0; which < z->s.img_n; ++which)
|
|
|
1281 |
if (z->img_comp[which].id == id)
|
|
|
1282 |
break;
|
|
|
1283 |
if (which == z->s.img_n) return 0;
|
|
|
1284 |
z->img_comp[which].hd = q >> 4; if (z->img_comp[which].hd > 3) return e("bad DC huff","Corrupt JPEG");
|
|
|
1285 |
z->img_comp[which].ha = q & 15; if (z->img_comp[which].ha > 3) return e("bad AC huff","Corrupt JPEG");
|
|
|
1286 |
z->order[i] = which;
|
|
|
1287 |
}
|
|
|
1288 |
if (get8(&z->s) != 0) return e("bad SOS","Corrupt JPEG");
|
|
|
1289 |
get8(&z->s); // should be 63, but might be 0
|
|
|
1290 |
if (get8(&z->s) != 0) return e("bad SOS","Corrupt JPEG");
|
|
|
1291 |
|
|
|
1292 |
return 1;
|
|
|
1293 |
}
|
|
|
1294 |
|
|
|
1295 |
static int process_frame_header(jpeg *z, int scan)
|
|
|
1296 |
{
|
|
|
1297 |
stbi *s = &z->s;
|
|
|
1298 |
int Lf,p,i,q, h_max=1,v_max=1,c;
|
|
|
1299 |
Lf = get16(s); if (Lf < 11) return e("bad SOF len","Corrupt JPEG"); // JPEG
|
|
|
1300 |
p = get8(s); if (p != 8) return e("only 8-bit","JPEG format not supported: 8-bit only"); // JPEG baseline
|
|
|
1301 |
s->img_y = get16(s); if (s->img_y == 0) return e("no header height", "JPEG format not supported: delayed height"); // Legal, but we don't handle it--but neither does IJG
|
|
|
1302 |
s->img_x = get16(s); if (s->img_x == 0) return e("0 width","Corrupt JPEG"); // JPEG requires
|
|
|
1303 |
c = get8(s);
|
|
|
1304 |
if (c != 3 && c != 1) return e("bad component count","Corrupt JPEG"); // JFIF requires
|
|
|
1305 |
s->img_n = c;
|
|
|
1306 |
for (i=0; i < c; ++i) {
|
|
|
1307 |
z->img_comp[i].data = NULL;
|
|
|
1308 |
z->img_comp[i].linebuf = NULL;
|
|
|
1309 |
}
|
|
|
1310 |
|
|
|
1311 |
if (Lf != 8+3*s->img_n) return e("bad SOF len","Corrupt JPEG");
|
|
|
1312 |
|
|
|
1313 |
for (i=0; i < s->img_n; ++i) {
|
|
|
1314 |
z->img_comp[i].id = get8(s);
|
|
|
1315 |
if (z->img_comp[i].id != i+1) // JFIF requires
|
|
|
1316 |
if (z->img_comp[i].id != i) // some version of jpegtran outputs non-JFIF-compliant files!
|
|
|
1317 |
return e("bad component ID","Corrupt JPEG");
|
|
|
1318 |
q = get8(s);
|
|
|
1319 |
z->img_comp[i].h = (q >> 4); if (!z->img_comp[i].h || z->img_comp[i].h > 4) return e("bad H","Corrupt JPEG");
|
|
|
1320 |
z->img_comp[i].v = q & 15; if (!z->img_comp[i].v || z->img_comp[i].v > 4) return e("bad V","Corrupt JPEG");
|
|
|
1321 |
z->img_comp[i].tq = get8(s); if (z->img_comp[i].tq > 3) return e("bad TQ","Corrupt JPEG");
|
|
|
1322 |
}
|
|
|
1323 |
|
|
|
1324 |
if (scan != SCAN_load) return 1;
|
|
|
1325 |
|
|
|
1326 |
if ((1 << 30) / s->img_x / s->img_n < s->img_y) return e("too large", "Image too large to decode");
|
|
|
1327 |
|
|
|
1328 |
for (i=0; i < s->img_n; ++i) {
|
|
|
1329 |
if (z->img_comp[i].h > h_max) h_max = z->img_comp[i].h;
|
|
|
1330 |
if (z->img_comp[i].v > v_max) v_max = z->img_comp[i].v;
|
|
|
1331 |
}
|
|
|
1332 |
|
|
|
1333 |
// compute interleaved mcu info
|
|
|
1334 |
z->img_h_max = h_max;
|
|
|
1335 |
z->img_v_max = v_max;
|
|
|
1336 |
z->img_mcu_w = h_max * 8;
|
|
|
1337 |
z->img_mcu_h = v_max * 8;
|
|
|
1338 |
z->img_mcu_x = (s->img_x + z->img_mcu_w-1) / z->img_mcu_w;
|
|
|
1339 |
z->img_mcu_y = (s->img_y + z->img_mcu_h-1) / z->img_mcu_h;
|
|
|
1340 |
|
|
|
1341 |
for (i=0; i < s->img_n; ++i) {
|
|
|
1342 |
// number of effective pixels (e.g. for non-interleaved MCU)
|
|
|
1343 |
z->img_comp[i].x = (s->img_x * z->img_comp[i].h + h_max-1) / h_max;
|
|
|
1344 |
z->img_comp[i].y = (s->img_y * z->img_comp[i].v + v_max-1) / v_max;
|
|
|
1345 |
// to simplify generation, we'll allocate enough memory to decode
|
|
|
1346 |
// the bogus oversized data from using interleaved MCUs and their
|
|
|
1347 |
// big blocks (e.g. a 16x16 iMCU on an image of width 33); we won't
|
|
|
1348 |
// discard the extra data until colorspace conversion
|
|
|
1349 |
z->img_comp[i].w2 = z->img_mcu_x * z->img_comp[i].h * 8;
|
|
|
1350 |
z->img_comp[i].h2 = z->img_mcu_y * z->img_comp[i].v * 8;
|
|
|
1351 |
z->img_comp[i].raw_data = malloc(z->img_comp[i].w2 * z->img_comp[i].h2+15);
|
|
|
1352 |
if (z->img_comp[i].raw_data == NULL) {
|
|
|
1353 |
for(--i; i >= 0; --i) {
|
|
|
1354 |
free(z->img_comp[i].raw_data);
|
|
|
1355 |
z->img_comp[i].data = NULL;
|
|
|
1356 |
}
|
|
|
1357 |
return e("outofmem", "Out of memory");
|
|
|
1358 |
}
|
|
|
1359 |
// align blocks for installable-idct using mmx/sse
|
|
|
1360 |
z->img_comp[i].data = (uint8*) (((size_t) z->img_comp[i].raw_data + 15) & ~15);
|
|
|
1361 |
z->img_comp[i].linebuf = NULL;
|
|
|
1362 |
}
|
|
|
1363 |
|
|
|
1364 |
return 1;
|
|
|
1365 |
}
|
|
|
1366 |
|
|
|
1367 |
// use comparisons since in some cases we handle more than one case (e.g. SOF)
|
|
|
1368 |
#define DNL(x) ((x) == 0xdc)
|
|
|
1369 |
#define SOI(x) ((x) == 0xd8)
|
|
|
1370 |
#define EOI(x) ((x) == 0xd9)
|
|
|
1371 |
#define SOF(x) ((x) == 0xc0 || (x) == 0xc1)
|
|
|
1372 |
#define SOS(x) ((x) == 0xda)
|
|
|
1373 |
|
|
|
1374 |
static int decode_jpeg_header(jpeg *z, int scan)
|
|
|
1375 |
{
|
|
|
1376 |
int m;
|
|
|
1377 |
z->marker = MARKER_none; // initialize cached marker to empty
|
|
|
1378 |
m = get_marker(z);
|
|
|
1379 |
if (!SOI(m)) return e("no SOI","Corrupt JPEG");
|
|
|
1380 |
if (scan == SCAN_type) return 1;
|
|
|
1381 |
m = get_marker(z);
|
|
|
1382 |
while (!SOF(m)) {
|
|
|
1383 |
if (!process_marker(z,m)) return 0;
|
|
|
1384 |
m = get_marker(z);
|
|
|
1385 |
while (m == MARKER_none) {
|
|
|
1386 |
// some files have extra padding after their blocks, so ok, we'll scan
|
|
|
1387 |
if (at_eof(&z->s)) return e("no SOF", "Corrupt JPEG");
|
|
|
1388 |
m = get_marker(z);
|
|
|
1389 |
}
|
|
|
1390 |
}
|
|
|
1391 |
if (!process_frame_header(z, scan)) return 0;
|
|
|
1392 |
return 1;
|
|
|
1393 |
}
|
|
|
1394 |
|
|
|
1395 |
static int decode_jpeg_image(jpeg *j)
|
|
|
1396 |
{
|
|
|
1397 |
int m;
|
|
|
1398 |
j->restart_interval = 0;
|
|
|
1399 |
if (!decode_jpeg_header(j, SCAN_load)) return 0;
|
|
|
1400 |
m = get_marker(j);
|
|
|
1401 |
while (!EOI(m)) {
|
|
|
1402 |
if (SOS(m)) {
|
|
|
1403 |
if (!process_scan_header(j)) return 0;
|
|
|
1404 |
if (!parse_entropy_coded_data(j)) return 0;
|
|
|
1405 |
if (j->marker == MARKER_none ) {
|
|
|
1406 |
// handle 0s at the end of image data from IP Kamera 9060
|
|
|
1407 |
while (!at_eof(&j->s)) {
|
|
|
1408 |
int x = get8(&j->s);
|
|
|
1409 |
if (x == 255) {
|
|
|
1410 |
j->marker = get8u(&j->s);
|
|
|
1411 |
break;
|
|
|
1412 |
} else if (x != 0) {
|
|
|
1413 |
return 0;
|
|
|
1414 |
}
|
|
|
1415 |
}
|
|
|
1416 |
// if we reach eof without hitting a marker, get_marker() below will fail and we'll eventually return 0
|
|
|
1417 |
}
|
|
|
1418 |
} else {
|
|
|
1419 |
if (!process_marker(j, m)) return 0;
|
|
|
1420 |
}
|
|
|
1421 |
m = get_marker(j);
|
|
|
1422 |
}
|
|
|
1423 |
return 1;
|
|
|
1424 |
}
|
|
|
1425 |
|
|
|
1426 |
// static jfif-centered resampling (across block boundaries)
|
|
|
1427 |
|
|
|
1428 |
typedef uint8 *(*resample_row_func)(uint8 *out, uint8 *in0, uint8 *in1,
|
|
|
1429 |
int w, int hs);
|
|
|
1430 |
|
|
|
1431 |
#define div4(x) ((uint8) ((x) >> 2))
|
|
|
1432 |
|
|
|
1433 |
static uint8 *resample_row_1(uint8 *out, uint8 *in_near, uint8 *in_far, int w, int hs)
|
|
|
1434 |
{
|
|
|
1435 |
STBI_NOTUSED(out);
|
|
|
1436 |
STBI_NOTUSED(in_far);
|
|
|
1437 |
STBI_NOTUSED(w);
|
|
|
1438 |
STBI_NOTUSED(hs);
|
|
|
1439 |
return in_near;
|
|
|
1440 |
}
|
|
|
1441 |
|
|
|
1442 |
static uint8* resample_row_v_2(uint8 *out, uint8 *in_near, uint8 *in_far, int w, int hs)
|
|
|
1443 |
{
|
|
|
1444 |
// need to generate two samples vertically for every one in input
|
|
|
1445 |
int i;
|
|
|
1446 |
STBI_NOTUSED(hs);
|
|
|
1447 |
for (i=0; i < w; ++i)
|
|
|
1448 |
out[i] = div4(3*in_near[i] + in_far[i] + 2);
|
|
|
1449 |
return out;
|
|
|
1450 |
}
|
|
|
1451 |
|
|
|
1452 |
static uint8* resample_row_h_2(uint8 *out, uint8 *in_near, uint8 *in_far, int w, int hs)
|
|
|
1453 |
{
|
|
|
1454 |
// need to generate two samples horizontally for every one in input
|
|
|
1455 |
int i;
|
|
|
1456 |
uint8 *input = in_near;
|
|
|
1457 |
|
|
|
1458 |
if (w == 1) {
|
|
|
1459 |
// if only one sample, can't do any interpolation
|
|
|
1460 |
out[0] = out[1] = input[0];
|
|
|
1461 |
return out;
|
|
|
1462 |
}
|
|
|
1463 |
|
|
|
1464 |
out[0] = input[0];
|
|
|
1465 |
out[1] = div4(input[0]*3 + input[1] + 2);
|
|
|
1466 |
for (i=1; i < w-1; ++i) {
|
|
|
1467 |
int n = 3*input[i]+2;
|
|
|
1468 |
out[i*2+0] = div4(n+input[i-1]);
|
|
|
1469 |
out[i*2+1] = div4(n+input[i+1]);
|
|
|
1470 |
}
|
|
|
1471 |
out[i*2+0] = div4(input[w-2]*3 + input[w-1] + 2);
|
|
|
1472 |
out[i*2+1] = input[w-1];
|
|
|
1473 |
|
|
|
1474 |
STBI_NOTUSED(in_far);
|
|
|
1475 |
STBI_NOTUSED(hs);
|
|
|
1476 |
|
|
|
1477 |
return out;
|
|
|
1478 |
}
|
|
|
1479 |
|
|
|
1480 |
#define div16(x) ((uint8) ((x) >> 4))
|
|
|
1481 |
|
|
|
1482 |
static uint8 *resample_row_hv_2(uint8 *out, uint8 *in_near, uint8 *in_far, int w, int hs)
|
|
|
1483 |
{
|
|
|
1484 |
// need to generate 2x2 samples for every one in input
|
|
|
1485 |
int i,t0,t1;
|
|
|
1486 |
if (w == 1) {
|
|
|
1487 |
out[0] = out[1] = div4(3*in_near[0] + in_far[0] + 2);
|
|
|
1488 |
return out;
|
|
|
1489 |
}
|
|
|
1490 |
|
|
|
1491 |
t1 = 3*in_near[0] + in_far[0];
|
|
|
1492 |
out[0] = div4(t1+2);
|
|
|
1493 |
for (i=1; i < w; ++i) {
|
|
|
1494 |
t0 = t1;
|
|
|
1495 |
t1 = 3*in_near[i]+in_far[i];
|
|
|
1496 |
out[i*2-1] = div16(3*t0 + t1 + 8);
|
|
|
1497 |
out[i*2 ] = div16(3*t1 + t0 + 8);
|
|
|
1498 |
}
|
|
|
1499 |
out[w*2-1] = div4(t1+2);
|
|
|
1500 |
|
|
|
1501 |
STBI_NOTUSED(hs);
|
|
|
1502 |
|
|
|
1503 |
return out;
|
|
|
1504 |
}
|
|
|
1505 |
|
|
|
1506 |
static uint8 *resample_row_generic(uint8 *out, uint8 *in_near, uint8 *in_far, int w, int hs)
|
|
|
1507 |
{
|
|
|
1508 |
// resample with nearest-neighbor
|
|
|
1509 |
int i,j;
|
|
|
1510 |
in_far = in_far;
|
|
|
1511 |
for (i=0; i < w; ++i)
|
|
|
1512 |
for (j=0; j < hs; ++j)
|
|
|
1513 |
out[i*hs+j] = in_near[i];
|
|
|
1514 |
return out;
|
|
|
1515 |
}
|
|
|
1516 |
|
|
|
1517 |
#define float2fixed(x) ((int) ((x) * 65536 + 0.5))
|
|
|
1518 |
|
|
|
1519 |
// 0.38 seconds on 3*anemones.jpg (0.25 with processor = Pro)
|
|
|
1520 |
// VC6 without processor=Pro is generating multiple LEAs per multiply!
|
|
|
1521 |
static void YCbCr_to_RGB_row(uint8 *out, const uint8 *y, const uint8 *pcb, const uint8 *pcr, int count, int step)
|
|
|
1522 |
{
|
|
|
1523 |
int i;
|
|
|
1524 |
for (i=0; i < count; ++i) {
|
|
|
1525 |
int y_fixed = (y[i] << 16) + 32768; // rounding
|
|
|
1526 |
int r,g,b;
|
|
|
1527 |
int cr = pcr[i] - 128;
|
|
|
1528 |
int cb = pcb[i] - 128;
|
|
|
1529 |
r = y_fixed + cr*float2fixed(1.40200f);
|
|
|
1530 |
g = y_fixed - cr*float2fixed(0.71414f) - cb*float2fixed(0.34414f);
|
|
|
1531 |
b = y_fixed + cb*float2fixed(1.77200f);
|
|
|
1532 |
r >>= 16;
|
|
|
1533 |
g >>= 16;
|
|
|
1534 |
b >>= 16;
|
|
|
1535 |
if ((unsigned) r > 255) { if (r < 0) r = 0; else r = 255; }
|
|
|
1536 |
if ((unsigned) g > 255) { if (g < 0) g = 0; else g = 255; }
|
|
|
1537 |
if ((unsigned) b > 255) { if (b < 0) b = 0; else b = 255; }
|
|
|
1538 |
out[0] = (uint8)r;
|
|
|
1539 |
out[1] = (uint8)g;
|
|
|
1540 |
out[2] = (uint8)b;
|
|
|
1541 |
out[3] = 255;
|
|
|
1542 |
out += step;
|
|
|
1543 |
}
|
|
|
1544 |
}
|
|
|
1545 |
|
|
|
1546 |
#ifdef STBI_SIMD
|
|
|
1547 |
static stbi_YCbCr_to_RGB_run stbi_YCbCr_installed = YCbCr_to_RGB_row;
|
|
|
1548 |
|
|
|
1549 |
void stbi_install_YCbCr_to_RGB(stbi_YCbCr_to_RGB_run func)
|
|
|
1550 |
{
|
|
|
1551 |
stbi_YCbCr_installed = func;
|
|
|
1552 |
}
|
|
|
1553 |
#endif
|
|
|
1554 |
|
|
|
1555 |
|
|
|
1556 |
// clean up the temporary component buffers
|
|
|
1557 |
static void cleanup_jpeg(jpeg *j)
|
|
|
1558 |
{
|
|
|
1559 |
int i;
|
|
|
1560 |
for (i=0; i < j->s.img_n; ++i) {
|
|
|
1561 |
if (j->img_comp[i].data) {
|
|
|
1562 |
free(j->img_comp[i].raw_data);
|
|
|
1563 |
j->img_comp[i].data = NULL;
|
|
|
1564 |
}
|
|
|
1565 |
if (j->img_comp[i].linebuf) {
|
|
|
1566 |
free(j->img_comp[i].linebuf);
|
|
|
1567 |
j->img_comp[i].linebuf = NULL;
|
|
|
1568 |
}
|
|
|
1569 |
}
|
|
|
1570 |
}
|
|
|
1571 |
|
|
|
1572 |
typedef struct
|
|
|
1573 |
{
|
|
|
1574 |
resample_row_func resample;
|
|
|
1575 |
uint8 *line0,*line1;
|
|
|
1576 |
int hs,vs; // expansion factor in each axis
|
|
|
1577 |
int w_lores; // horizontal pixels pre-expansion
|
|
|
1578 |
int ystep; // how far through vertical expansion we are
|
|
|
1579 |
int ypos; // which pre-expansion row we're on
|
|
|
1580 |
} stbi_resample;
|
|
|
1581 |
|
|
|
1582 |
static uint8 *load_jpeg_image(jpeg *z, int *out_x, int *out_y, int *comp, int req_comp)
|
|
|
1583 |
{
|
|
|
1584 |
int n, decode_n;
|
|
|
1585 |
// validate req_comp
|
|
|
1586 |
if (req_comp < 0 || req_comp > 4) return epuc("bad req_comp", "Internal error");
|
|
|
1587 |
z->s.img_n = 0;
|
|
|
1588 |
|
|
|
1589 |
// load a jpeg image from whichever source
|
|
|
1590 |
if (!decode_jpeg_image(z)) { cleanup_jpeg(z); return NULL; }
|
|
|
1591 |
|
|
|
1592 |
// determine actual number of components to generate
|
|
|
1593 |
n = req_comp ? req_comp : z->s.img_n;
|
|
|
1594 |
|
|
|
1595 |
if (z->s.img_n == 3 && n < 3)
|
|
|
1596 |
decode_n = 1;
|
|
|
1597 |
else
|
|
|
1598 |
decode_n = z->s.img_n;
|
|
|
1599 |
|
|
|
1600 |
// resample and color-convert
|
|
|
1601 |
{
|
|
|
1602 |
int k;
|
|
|
1603 |
uint i,j;
|
|
|
1604 |
uint8 *output;
|
|
|
1605 |
uint8 *coutput[4];
|
|
|
1606 |
|
|
|
1607 |
stbi_resample res_comp[4];
|
|
|
1608 |
|
|
|
1609 |
for (k=0; k < decode_n; ++k) {
|
|
|
1610 |
stbi_resample *r = &res_comp[k];
|
|
|
1611 |
|
|
|
1612 |
// allocate line buffer big enough for upsampling off the edges
|
|
|
1613 |
// with upsample factor of 4
|
|
|
1614 |
z->img_comp[k].linebuf = (uint8 *) malloc(z->s.img_x + 3);
|
|
|
1615 |
if (!z->img_comp[k].linebuf) { cleanup_jpeg(z); return epuc("outofmem", "Out of memory"); }
|
|
|
1616 |
|
|
|
1617 |
r->hs = z->img_h_max / z->img_comp[k].h;
|
|
|
1618 |
r->vs = z->img_v_max / z->img_comp[k].v;
|
|
|
1619 |
r->ystep = r->vs >> 1;
|
|
|
1620 |
r->w_lores = (z->s.img_x + r->hs-1) / r->hs;
|
|
|
1621 |
r->ypos = 0;
|
|
|
1622 |
r->line0 = r->line1 = z->img_comp[k].data;
|
|
|
1623 |
|
|
|
1624 |
if (r->hs == 1 && r->vs == 1) r->resample = resample_row_1;
|
|
|
1625 |
else if (r->hs == 1 && r->vs == 2) r->resample = resample_row_v_2;
|
|
|
1626 |
else if (r->hs == 2 && r->vs == 1) r->resample = resample_row_h_2;
|
|
|
1627 |
else if (r->hs == 2 && r->vs == 2) r->resample = resample_row_hv_2;
|
|
|
1628 |
else r->resample = resample_row_generic;
|
|
|
1629 |
}
|
|
|
1630 |
|
|
|
1631 |
// can't error after this so, this is safe
|
|
|
1632 |
output = (uint8 *) malloc(n * z->s.img_x * z->s.img_y + 1);
|
|
|
1633 |
if (!output) { cleanup_jpeg(z); return epuc("outofmem", "Out of memory"); }
|
|
|
1634 |
|
|
|
1635 |
// now go ahead and resample
|
|
|
1636 |
for (j=0; j < z->s.img_y; ++j) {
|
|
|
1637 |
uint8 *out = output + n * z->s.img_x * j;
|
|
|
1638 |
for (k=0; k < decode_n; ++k) {
|
|
|
1639 |
stbi_resample *r = &res_comp[k];
|
|
|
1640 |
int y_bot = r->ystep >= (r->vs >> 1);
|
|
|
1641 |
coutput[k] = r->resample(z->img_comp[k].linebuf,
|
|
|
1642 |
y_bot ? r->line1 : r->line0,
|
|
|
1643 |
y_bot ? r->line0 : r->line1,
|
|
|
1644 |
r->w_lores, r->hs);
|
|
|
1645 |
if (++r->ystep >= r->vs) {
|
|
|
1646 |
r->ystep = 0;
|
|
|
1647 |
r->line0 = r->line1;
|
|
|
1648 |
if (++r->ypos < z->img_comp[k].y)
|
|
|
1649 |
r->line1 += z->img_comp[k].w2;
|
|
|
1650 |
}
|
|
|
1651 |
}
|
|
|
1652 |
if (n >= 3) {
|
|
|
1653 |
uint8 *y = coutput[0];
|
|
|
1654 |
if (z->s.img_n == 3) {
|
|
|
1655 |
#ifdef STBI_SIMD
|
|
|
1656 |
stbi_YCbCr_installed(out, y, coutput[1], coutput[2], z->s.img_x, n);
|
|
|
1657 |
#else
|
|
|
1658 |
YCbCr_to_RGB_row(out, y, coutput[1], coutput[2], z->s.img_x, n);
|
|
|
1659 |
#endif
|
|
|
1660 |
} else
|
|
|
1661 |
for (i=0; i < z->s.img_x; ++i) {
|
|
|
1662 |
out[0] = out[1] = out[2] = y[i];
|
|
|
1663 |
out[3] = 255; // not used if n==3
|
|
|
1664 |
out += n;
|
|
|
1665 |
}
|
|
|
1666 |
} else {
|
|
|
1667 |
uint8 *y = coutput[0];
|
|
|
1668 |
if (n == 1)
|
|
|
1669 |
for (i=0; i < z->s.img_x; ++i) out[i] = y[i];
|
|
|
1670 |
else
|
|
|
1671 |
for (i=0; i < z->s.img_x; ++i) *out++ = y[i], *out++ = 255;
|
|
|
1672 |
}
|
|
|
1673 |
}
|
|
|
1674 |
cleanup_jpeg(z);
|
|
|
1675 |
*out_x = z->s.img_x;
|
|
|
1676 |
*out_y = z->s.img_y;
|
|
|
1677 |
if (comp) *comp = z->s.img_n; // report original components, not output
|
|
|
1678 |
return output;
|
|
|
1679 |
}
|
|
|
1680 |
}
|
|
|
1681 |
|
|
|
1682 |
#ifndef STBI_NO_STDIO
|
|
|
1683 |
unsigned char *stbi_jpeg_load_from_file(FILE *f, int *x, int *y, int *comp, int req_comp)
|
|
|
1684 |
{
|
|
|
1685 |
jpeg j;
|
|
|
1686 |
start_file(&j.s, f);
|
|
|
1687 |
return load_jpeg_image(&j, x,y,comp,req_comp);
|
|
|
1688 |
}
|
|
|
1689 |
|
|
|
1690 |
unsigned char *stbi_jpeg_load(char const *filename, int *x, int *y, int *comp, int req_comp)
|
|
|
1691 |
{
|
|
|
1692 |
unsigned char *data;
|
|
|
1693 |
FILE *f = fopen(filename, "rb");
|
|
|
1694 |
if (!f) return NULL;
|
|
|
1695 |
data = stbi_jpeg_load_from_file(f,x,y,comp,req_comp);
|
|
|
1696 |
fclose(f);
|
|
|
1697 |
return data;
|
|
|
1698 |
}
|
|
|
1699 |
#endif
|
|
|
1700 |
|
|
|
1701 |
unsigned char *stbi_jpeg_load_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp)
|
|
|
1702 |
{
|
|
|
1703 |
#ifdef STBI_SMALL_STACK
|
|
|
1704 |
unsigned char *result;
|
|
|
1705 |
jpeg *j = (jpeg *) malloc(sizeof(*j));
|
|
|
1706 |
start_mem(&j->s, buffer, len);
|
|
|
1707 |
result = load_jpeg_image(j,x,y,comp,req_comp);
|
|
|
1708 |
free(j);
|
|
|
1709 |
return result;
|
|
|
1710 |
#else
|
|
|
1711 |
jpeg j;
|
|
|
1712 |
start_mem(&j.s, buffer,len);
|
|
|
1713 |
return load_jpeg_image(&j, x,y,comp,req_comp);
|
|
|
1714 |
#endif
|
|
|
1715 |
}
|
|
|
1716 |
|
|
|
1717 |
static int stbi_jpeg_info_raw(jpeg *j, int *x, int *y, int *comp)
|
|
|
1718 |
{
|
|
|
1719 |
if (!decode_jpeg_header(j, SCAN_header))
|
|
|
1720 |
return 0;
|
|
|
1721 |
if (x) *x = j->s.img_x;
|
|
|
1722 |
if (y) *y = j->s.img_y;
|
|
|
1723 |
if (comp) *comp = j->s.img_n;
|
|
|
1724 |
return 1;
|
|
|
1725 |
}
|
|
|
1726 |
|
|
|
1727 |
#ifndef STBI_NO_STDIO
|
|
|
1728 |
int stbi_jpeg_test_file(FILE *f)
|
|
|
1729 |
{
|
|
|
1730 |
int n,r;
|
|
|
1731 |
jpeg j;
|
|
|
1732 |
n = ftell(f);
|
|
|
1733 |
start_file(&j.s, f);
|
|
|
1734 |
r = decode_jpeg_header(&j, SCAN_type);
|
|
|
1735 |
fseek(f,n,SEEK_SET);
|
|
|
1736 |
return r;
|
|
|
1737 |
}
|
|
|
1738 |
|
|
|
1739 |
int stbi_jpeg_info_from_file(FILE *f, int *x, int *y, int *comp)
|
|
|
1740 |
{
|
|
|
1741 |
jpeg j;
|
|
|
1742 |
long n = ftell(f);
|
|
|
1743 |
int res;
|
|
|
1744 |
start_file(&j.s, f);
|
|
|
1745 |
res = stbi_jpeg_info_raw(&j, x, y, comp);
|
|
|
1746 |
fseek(f, n, SEEK_SET);
|
|
|
1747 |
return res;
|
|
|
1748 |
}
|
|
|
1749 |
|
|
|
1750 |
int stbi_jpeg_info(char const *filename, int *x, int *y, int *comp)
|
|
|
1751 |
{
|
|
|
1752 |
FILE *f = fopen(filename, "rb");
|
|
|
1753 |
int result;
|
|
|
1754 |
if (!f) return e("can't fopen", "Unable to open file");
|
|
|
1755 |
result = stbi_jpeg_info_from_file(f, x, y, comp);
|
|
|
1756 |
fclose(f);
|
|
|
1757 |
return result;
|
|
|
1758 |
}
|
|
|
1759 |
#endif
|
|
|
1760 |
|
|
|
1761 |
int stbi_jpeg_test_memory(stbi_uc const *buffer, int len)
|
|
|
1762 |
{
|
|
|
1763 |
jpeg j;
|
|
|
1764 |
start_mem(&j.s, buffer,len);
|
|
|
1765 |
return decode_jpeg_header(&j, SCAN_type);
|
|
|
1766 |
}
|
|
|
1767 |
|
|
|
1768 |
int stbi_jpeg_info_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp)
|
|
|
1769 |
{
|
|
|
1770 |
jpeg j;
|
|
|
1771 |
start_mem(&j.s, buffer, len);
|
|
|
1772 |
return stbi_jpeg_info_raw(&j, x, y, comp);
|
|
|
1773 |
}
|
|
|
1774 |
|
|
|
1775 |
#ifndef STBI_NO_STDIO
|
|
|
1776 |
extern int stbi_jpeg_info (char const *filename, int *x, int *y, int *comp);
|
|
|
1777 |
extern int stbi_jpeg_info_from_file (FILE *f, int *x, int *y, int *comp);
|
|
|
1778 |
#endif
|
|
|
1779 |
extern int stbi_jpeg_info_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp);
|
|
|
1780 |
|
|
|
1781 |
// public domain zlib decode v0.2 Sean Barrett 2006-11-18
|
|
|
1782 |
// simple implementation
|
|
|
1783 |
// - all input must be provided in an upfront buffer
|
|
|
1784 |
// - all output is written to a single output buffer (can malloc/realloc)
|
|
|
1785 |
// performance
|
|
|
1786 |
// - fast huffman
|
|
|
1787 |
|
|
|
1788 |
// fast-way is faster to check than jpeg huffman, but slow way is slower
|
|
|
1789 |
#define ZFAST_BITS 9 // accelerate all cases in default tables
|
|
|
1790 |
#define ZFAST_MASK ((1 << ZFAST_BITS) - 1)
|
|
|
1791 |
|
|
|
1792 |
// zlib-style huffman encoding
|
|
|
1793 |
// (jpegs packs from left, zlib from right, so can't share code)
|
|
|
1794 |
typedef struct
|
|
|
1795 |
{
|
|
|
1796 |
uint16 fast[1 << ZFAST_BITS];
|
|
|
1797 |
uint16 firstcode[16];
|
|
|
1798 |
int maxcode[17];
|
|
|
1799 |
uint16 firstsymbol[16];
|
|
|
1800 |
uint8 size[288];
|
|
|
1801 |
uint16 value[288];
|
|
|
1802 |
} zhuffman;
|
|
|
1803 |
|
|
|
1804 |
__forceinline static int bitreverse16(int n)
|
|
|
1805 |
{
|
|
|
1806 |
n = ((n & 0xAAAA) >> 1) | ((n & 0x5555) << 1);
|
|
|
1807 |
n = ((n & 0xCCCC) >> 2) | ((n & 0x3333) << 2);
|
|
|
1808 |
n = ((n & 0xF0F0) >> 4) | ((n & 0x0F0F) << 4);
|
|
|
1809 |
n = ((n & 0xFF00) >> 8) | ((n & 0x00FF) << 8);
|
|
|
1810 |
return n;
|
|
|
1811 |
}
|
|
|
1812 |
|
|
|
1813 |
__forceinline static int bit_reverse(int v, int bits)
|
|
|
1814 |
{
|
|
|
1815 |
assert(bits <= 16);
|
|
|
1816 |
// to bit reverse n bits, reverse 16 and shift
|
|
|
1817 |
// e.g. 11 bits, bit reverse and shift away 5
|
|
|
1818 |
return bitreverse16(v) >> (16-bits);
|
|
|
1819 |
}
|
|
|
1820 |
|
|
|
1821 |
static int zbuild_huffman(zhuffman *z, uint8 *sizelist, int num)
|
|
|
1822 |
{
|
|
|
1823 |
int i,k=0;
|
|
|
1824 |
int code, next_code[16], sizes[17];
|
|
|
1825 |
|
|
|
1826 |
// DEFLATE spec for generating codes
|
|
|
1827 |
memset(sizes, 0, sizeof(sizes));
|
|
|
1828 |
memset(z->fast, 255, sizeof(z->fast));
|
|
|
1829 |
for (i=0; i < num; ++i)
|
|
|
1830 |
++sizes[sizelist[i]];
|
|
|
1831 |
sizes[0] = 0;
|
|
|
1832 |
for (i=1; i < 16; ++i)
|
|
|
1833 |
assert(sizes[i] <= (1 << i));
|
|
|
1834 |
code = 0;
|
|
|
1835 |
for (i=1; i < 16; ++i) {
|
|
|
1836 |
next_code[i] = code;
|
|
|
1837 |
z->firstcode[i] = (uint16) code;
|
|
|
1838 |
z->firstsymbol[i] = (uint16) k;
|
|
|
1839 |
code = (code + sizes[i]);
|
|
|
1840 |
if (sizes[i])
|
|
|
1841 |
if (code-1 >= (1 << i)) return e("bad codelengths","Corrupt JPEG");
|
|
|
1842 |
z->maxcode[i] = code << (16-i); // preshift for inner loop
|
|
|
1843 |
code <<= 1;
|
|
|
1844 |
k += sizes[i];
|
|
|
1845 |
}
|
|
|
1846 |
z->maxcode[16] = 0x10000; // sentinel
|
|
|
1847 |
for (i=0; i < num; ++i) {
|
|
|
1848 |
int s = sizelist[i];
|
|
|
1849 |
if (s) {
|
|
|
1850 |
int c = next_code[s] - z->firstcode[s] + z->firstsymbol[s];
|
|
|
1851 |
z->size[c] = (uint8)s;
|
|
|
1852 |
z->value[c] = (uint16)i;
|
|
|
1853 |
if (s <= ZFAST_BITS) {
|
|
|
1854 |
int k = bit_reverse(next_code[s],s);
|
|
|
1855 |
while (k < (1 << ZFAST_BITS)) {
|
|
|
1856 |
z->fast[k] = (uint16) c;
|
|
|
1857 |
k += (1 << s);
|
|
|
1858 |
}
|
|
|
1859 |
}
|
|
|
1860 |
++next_code[s];
|
|
|
1861 |
}
|
|
|
1862 |
}
|
|
|
1863 |
return 1;
|
|
|
1864 |
}
|
|
|
1865 |
|
|
|
1866 |
// zlib-from-memory implementation for PNG reading
|
|
|
1867 |
// because PNG allows splitting the zlib stream arbitrarily,
|
|
|
1868 |
// and it's annoying structurally to have PNG call ZLIB call PNG,
|
|
|
1869 |
// we require PNG read all the IDATs and combine them into a single
|
|
|
1870 |
// memory buffer
|
|
|
1871 |
|
|
|
1872 |
typedef struct
|
|
|
1873 |
{
|
|
|
1874 |
uint8 *zbuffer, *zbuffer_end;
|
|
|
1875 |
int num_bits;
|
|
|
1876 |
uint32 code_buffer;
|
|
|
1877 |
|
|
|
1878 |
char *zout;
|
|
|
1879 |
char *zout_start;
|
|
|
1880 |
char *zout_end;
|
|
|
1881 |
int z_expandable;
|
|
|
1882 |
|
|
|
1883 |
zhuffman z_length, z_distance;
|
|
|
1884 |
} zbuf;
|
|
|
1885 |
|
|
|
1886 |
__forceinline static int zget8(zbuf *z)
|
|
|
1887 |
{
|
|
|
1888 |
if (z->zbuffer >= z->zbuffer_end) return 0;
|
|
|
1889 |
return *z->zbuffer++;
|
|
|
1890 |
}
|
|
|
1891 |
|
|
|
1892 |
static void fill_bits(zbuf *z)
|
|
|
1893 |
{
|
|
|
1894 |
do {
|
|
|
1895 |
assert(z->code_buffer < (1U << z->num_bits));
|
|
|
1896 |
z->code_buffer |= zget8(z) << z->num_bits;
|
|
|
1897 |
z->num_bits += 8;
|
|
|
1898 |
} while (z->num_bits <= 24);
|
|
|
1899 |
}
|
|
|
1900 |
|
|
|
1901 |
__forceinline static unsigned int zreceive(zbuf *z, int n)
|
|
|
1902 |
{
|
|
|
1903 |
unsigned int k;
|
|
|
1904 |
if (z->num_bits < n) fill_bits(z);
|
|
|
1905 |
k = z->code_buffer & ((1 << n) - 1);
|
|
|
1906 |
z->code_buffer >>= n;
|
|
|
1907 |
z->num_bits -= n;
|
|
|
1908 |
return k;
|
|
|
1909 |
}
|
|
|
1910 |
|
|
|
1911 |
__forceinline static int zhuffman_decode(zbuf *a, zhuffman *z)
|
|
|
1912 |
{
|
|
|
1913 |
int b,s,k;
|
|
|
1914 |
if (a->num_bits < 16) fill_bits(a);
|
|
|
1915 |
b = z->fast[a->code_buffer & ZFAST_MASK];
|
|
|
1916 |
if (b < 0xffff) {
|
|
|
1917 |
s = z->size[b];
|
|
|
1918 |
a->code_buffer >>= s;
|
|
|
1919 |
a->num_bits -= s;
|
|
|
1920 |
return z->value[b];
|
|
|
1921 |
}
|
|
|
1922 |
|
|
|
1923 |
// not resolved by fast table, so compute it the slow way
|
|
|
1924 |
// use jpeg approach, which requires MSbits at top
|
|
|
1925 |
k = bit_reverse(a->code_buffer, 16);
|
|
|
1926 |
for (s=ZFAST_BITS+1; ; ++s)
|
|
|
1927 |
if (k < z->maxcode[s])
|
|
|
1928 |
break;
|
|
|
1929 |
if (s == 16) return -1; // invalid code!
|
|
|
1930 |
// code size is s, so:
|
|
|
1931 |
b = (k >> (16-s)) - z->firstcode[s] + z->firstsymbol[s];
|
|
|
1932 |
assert(z->size[b] == s);
|
|
|
1933 |
a->code_buffer >>= s;
|
|
|
1934 |
a->num_bits -= s;
|
|
|
1935 |
return z->value[b];
|
|
|
1936 |
}
|
|
|
1937 |
|
|
|
1938 |
static int expand(zbuf *z, int n) // need to make room for n bytes
|
|
|
1939 |
{
|
|
|
1940 |
char *q;
|
|
|
1941 |
int cur, limit;
|
|
|
1942 |
if (!z->z_expandable) return e("output buffer limit","Corrupt PNG");
|
|
|
1943 |
cur = (int) (z->zout - z->zout_start);
|
|
|
1944 |
limit = (int) (z->zout_end - z->zout_start);
|
|
|
1945 |
while (cur + n > limit)
|
|
|
1946 |
limit *= 2;
|
|
|
1947 |
q = (char *) realloc(z->zout_start, limit);
|
|
|
1948 |
if (q == NULL) return e("outofmem", "Out of memory");
|
|
|
1949 |
z->zout_start = q;
|
|
|
1950 |
z->zout = q + cur;
|
|
|
1951 |
z->zout_end = q + limit;
|
|
|
1952 |
return 1;
|
|
|
1953 |
}
|
|
|
1954 |
|
|
|
1955 |
static int length_base[31] = {
|
|
|
1956 |
3,4,5,6,7,8,9,10,11,13,
|
|
|
1957 |
15,17,19,23,27,31,35,43,51,59,
|
|
|
1958 |
67,83,99,115,131,163,195,227,258,0,0 };
|
|
|
1959 |
|
|
|
1960 |
static int length_extra[31]=
|
|
|
1961 |
{ 0,0,0,0,0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,0,0,0 };
|
|
|
1962 |
|
|
|
1963 |
static int dist_base[32] = { 1,2,3,4,5,7,9,13,17,25,33,49,65,97,129,193,
|
|
|
1964 |
257,385,513,769,1025,1537,2049,3073,4097,6145,8193,12289,16385,24577,0,0};
|
|
|
1965 |
|
|
|
1966 |
static int dist_extra[32] =
|
|
|
1967 |
{ 0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13};
|
|
|
1968 |
|
|
|
1969 |
static int parse_huffman_block(zbuf *a)
|
|
|
1970 |
{
|
|
|
1971 |
for(;;) {
|
|
|
1972 |
int z = zhuffman_decode(a, &a->z_length);
|
|
|
1973 |
if (z < 256) {
|
|
|
1974 |
if (z < 0) return e("bad huffman code","Corrupt PNG"); // error in huffman codes
|
|
|
1975 |
if (a->zout >= a->zout_end) if (!expand(a, 1)) return 0;
|
|
|
1976 |
*a->zout++ = (char) z;
|
|
|
1977 |
} else {
|
|
|
1978 |
uint8 *p;
|
|
|
1979 |
int len,dist;
|
|
|
1980 |
if (z == 256) return 1;
|
|
|
1981 |
z -= 257;
|
|
|
1982 |
len = length_base[z];
|
|
|
1983 |
if (length_extra[z]) len += zreceive(a, length_extra[z]);
|
|
|
1984 |
z = zhuffman_decode(a, &a->z_distance);
|
|
|
1985 |
if (z < 0) return e("bad huffman code","Corrupt PNG");
|
|
|
1986 |
dist = dist_base[z];
|
|
|
1987 |
if (dist_extra[z]) dist += zreceive(a, dist_extra[z]);
|
|
|
1988 |
if (a->zout - a->zout_start < dist) return e("bad dist","Corrupt PNG");
|
|
|
1989 |
if (a->zout + len > a->zout_end) if (!expand(a, len)) return 0;
|
|
|
1990 |
p = (uint8 *) (a->zout - dist);
|
|
|
1991 |
while (len--)
|
|
|
1992 |
*a->zout++ = *p++;
|
|
|
1993 |
}
|
|
|
1994 |
}
|
|
|
1995 |
}
|
|
|
1996 |
|
|
|
1997 |
static int compute_huffman_codes(zbuf *a)
|
|
|
1998 |
{
|
|
|
1999 |
static uint8 length_dezigzag[19] = { 16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15 };
|
|
|
2000 |
zhuffman z_codelength;
|
|
|
2001 |
uint8 lencodes[286+32+137];//padding for maximum single op
|
|
|
2002 |
uint8 codelength_sizes[19];
|
|
|
2003 |
int i,n;
|
|
|
2004 |
|
|
|
2005 |
int hlit = zreceive(a,5) + 257;
|
|
|
2006 |
int hdist = zreceive(a,5) + 1;
|
|
|
2007 |
int hclen = zreceive(a,4) + 4;
|
|
|
2008 |
|
|
|
2009 |
memset(codelength_sizes, 0, sizeof(codelength_sizes));
|
|
|
2010 |
for (i=0; i < hclen; ++i) {
|
|
|
2011 |
int s = zreceive(a,3);
|
|
|
2012 |
codelength_sizes[length_dezigzag[i]] = (uint8) s;
|
|
|
2013 |
}
|
|
|
2014 |
if (!zbuild_huffman(&z_codelength, codelength_sizes, 19)) return 0;
|
|
|
2015 |
|
|
|
2016 |
n = 0;
|
|
|
2017 |
while (n < hlit + hdist) {
|
|
|
2018 |
int c = zhuffman_decode(a, &z_codelength);
|
|
|
2019 |
assert(c >= 0 && c < 19);
|
|
|
2020 |
if (c < 16)
|
|
|
2021 |
lencodes[n++] = (uint8) c;
|
|
|
2022 |
else if (c == 16) {
|
|
|
2023 |
c = zreceive(a,2)+3;
|
|
|
2024 |
memset(lencodes+n, lencodes[n-1], c);
|
|
|
2025 |
n += c;
|
|
|
2026 |
} else if (c == 17) {
|
|
|
2027 |
c = zreceive(a,3)+3;
|
|
|
2028 |
memset(lencodes+n, 0, c);
|
|
|
2029 |
n += c;
|
|
|
2030 |
} else {
|
|
|
2031 |
assert(c == 18);
|
|
|
2032 |
c = zreceive(a,7)+11;
|
|
|
2033 |
memset(lencodes+n, 0, c);
|
|
|
2034 |
n += c;
|
|
|
2035 |
}
|
|
|
2036 |
}
|
|
|
2037 |
if (n != hlit+hdist) return e("bad codelengths","Corrupt PNG");
|
|
|
2038 |
if (!zbuild_huffman(&a->z_length, lencodes, hlit)) return 0;
|
|
|
2039 |
if (!zbuild_huffman(&a->z_distance, lencodes+hlit, hdist)) return 0;
|
|
|
2040 |
return 1;
|
|
|
2041 |
}
|
|
|
2042 |
|
|
|
2043 |
static int parse_uncompressed_block(zbuf *a)
|
|
|
2044 |
{
|
|
|
2045 |
uint8 header[4];
|
|
|
2046 |
int len,nlen,k;
|
|
|
2047 |
if (a->num_bits & 7)
|
|
|
2048 |
zreceive(a, a->num_bits & 7); // discard
|
|
|
2049 |
// drain the bit-packed data into header
|
|
|
2050 |
k = 0;
|
|
|
2051 |
while (a->num_bits > 0) {
|
|
|
2052 |
header[k++] = (uint8) (a->code_buffer & 255); // wtf this warns?
|
|
|
2053 |
a->code_buffer >>= 8;
|
|
|
2054 |
a->num_bits -= 8;
|
|
|
2055 |
}
|
|
|
2056 |
assert(a->num_bits == 0);
|
|
|
2057 |
// now fill header the normal way
|
|
|
2058 |
while (k < 4)
|
|
|
2059 |
header[k++] = (uint8) zget8(a);
|
|
|
2060 |
len = header[1] * 256 + header[0];
|
|
|
2061 |
nlen = header[3] * 256 + header[2];
|
|
|
2062 |
if (nlen != (len ^ 0xffff)) return e("zlib corrupt","Corrupt PNG");
|
|
|
2063 |
if (a->zbuffer + len > a->zbuffer_end) return e("read past buffer","Corrupt PNG");
|
|
|
2064 |
if (a->zout + len > a->zout_end)
|
|
|
2065 |
if (!expand(a, len)) return 0;
|
|
|
2066 |
memcpy(a->zout, a->zbuffer, len);
|
|
|
2067 |
a->zbuffer += len;
|
|
|
2068 |
a->zout += len;
|
|
|
2069 |
return 1;
|
|
|
2070 |
}
|
|
|
2071 |
|
|
|
2072 |
static int parse_zlib_header(zbuf *a)
|
|
|
2073 |
{
|
|
|
2074 |
int cmf = zget8(a);
|
|
|
2075 |
int cm = cmf & 15;
|
|
|
2076 |
/* int cinfo = cmf >> 4; */
|
|
|
2077 |
int flg = zget8(a);
|
|
|
2078 |
if ((cmf*256+flg) % 31 != 0) return e("bad zlib header","Corrupt PNG"); // zlib spec
|
|
|
2079 |
if (flg & 32) return e("no preset dict","Corrupt PNG"); // preset dictionary not allowed in png
|
|
|
2080 |
if (cm != 8) return e("bad compression","Corrupt PNG"); // DEFLATE required for png
|
|
|
2081 |
// window = 1 << (8 + cinfo)... but who cares, we fully buffer output
|
|
|
2082 |
return 1;
|
|
|
2083 |
}
|
|
|
2084 |
|
|
|
2085 |
// @TODO: should statically initialize these for optimal thread safety
|
|
|
2086 |
static uint8 default_length[288], default_distance[32];
|
|
|
2087 |
static void init_defaults(void)
|
|
|
2088 |
{
|
|
|
2089 |
int i; // use <= to match clearly with spec
|
|
|
2090 |
for (i=0; i <= 143; ++i) default_length[i] = 8;
|
|
|
2091 |
for ( ; i <= 255; ++i) default_length[i] = 9;
|
|
|
2092 |
for ( ; i <= 279; ++i) default_length[i] = 7;
|
|
|
2093 |
for ( ; i <= 287; ++i) default_length[i] = 8;
|
|
|
2094 |
|
|
|
2095 |
for (i=0; i <= 31; ++i) default_distance[i] = 5;
|
|
|
2096 |
}
|
|
|
2097 |
|
|
|
2098 |
int stbi_png_partial; // a quick hack to only allow decoding some of a PNG... I should implement real streaming support instead
|
|
|
2099 |
static int parse_zlib(zbuf *a, int parse_header)
|
|
|
2100 |
{
|
|
|
2101 |
int final, type;
|
|
|
2102 |
if (parse_header)
|
|
|
2103 |
if (!parse_zlib_header(a)) return 0;
|
|
|
2104 |
a->num_bits = 0;
|
|
|
2105 |
a->code_buffer = 0;
|
|
|
2106 |
do {
|
|
|
2107 |
final = zreceive(a,1);
|
|
|
2108 |
type = zreceive(a,2);
|
|
|
2109 |
if (type == 0) {
|
|
|
2110 |
if (!parse_uncompressed_block(a)) return 0;
|
|
|
2111 |
} else if (type == 3) {
|
|
|
2112 |
return 0;
|
|
|
2113 |
} else {
|
|
|
2114 |
if (type == 1) {
|
|
|
2115 |
// use fixed code lengths
|
|
|
2116 |
if (!default_distance[31]) init_defaults();
|
|
|
2117 |
if (!zbuild_huffman(&a->z_length , default_length , 288)) return 0;
|
|
|
2118 |
if (!zbuild_huffman(&a->z_distance, default_distance, 32)) return 0;
|
|
|
2119 |
} else {
|
|
|
2120 |
if (!compute_huffman_codes(a)) return 0;
|
|
|
2121 |
}
|
|
|
2122 |
if (!parse_huffman_block(a)) return 0;
|
|
|
2123 |
}
|
|
|
2124 |
if (stbi_png_partial && a->zout - a->zout_start > 65536)
|
|
|
2125 |
break;
|
|
|
2126 |
} while (!final);
|
|
|
2127 |
return 1;
|
|
|
2128 |
}
|
|
|
2129 |
|
|
|
2130 |
static int do_zlib(zbuf *a, char *obuf, int olen, int exp, int parse_header)
|
|
|
2131 |
{
|
|
|
2132 |
a->zout_start = obuf;
|
|
|
2133 |
a->zout = obuf;
|
|
|
2134 |
a->zout_end = obuf + olen;
|
|
|
2135 |
a->z_expandable = exp;
|
|
|
2136 |
|
|
|
2137 |
return parse_zlib(a, parse_header);
|
|
|
2138 |
}
|
|
|
2139 |
|
|
|
2140 |
char *stbi_zlib_decode_malloc_guesssize(const char *buffer, int len, int initial_size, int *outlen)
|
|
|
2141 |
{
|
|
|
2142 |
zbuf a;
|
|
|
2143 |
char *p = (char *) malloc(initial_size);
|
|
|
2144 |
if (p == NULL) return NULL;
|
|
|
2145 |
a.zbuffer = (uint8 *) buffer;
|
|
|
2146 |
a.zbuffer_end = (uint8 *) buffer + len;
|
|
|
2147 |
if (do_zlib(&a, p, initial_size, 1, 1)) {
|
|
|
2148 |
if (outlen) *outlen = (int) (a.zout - a.zout_start);
|
|
|
2149 |
return a.zout_start;
|
|
|
2150 |
} else {
|
|
|
2151 |
free(a.zout_start);
|
|
|
2152 |
return NULL;
|
|
|
2153 |
}
|
|
|
2154 |
}
|
|
|
2155 |
|
|
|
2156 |
char *stbi_zlib_decode_malloc(char const *buffer, int len, int *outlen)
|
|
|
2157 |
{
|
|
|
2158 |
return stbi_zlib_decode_malloc_guesssize(buffer, len, 16384, outlen);
|
|
|
2159 |
}
|
|
|
2160 |
|
|
|
2161 |
char *stbi_zlib_decode_malloc_guesssize_headerflag(const char *buffer, int len, int initial_size, int *outlen, int parse_header)
|
|
|
2162 |
{
|
|
|
2163 |
zbuf a;
|
|
|
2164 |
char *p = (char *) malloc(initial_size);
|
|
|
2165 |
if (p == NULL) return NULL;
|
|
|
2166 |
a.zbuffer = (uint8 *) buffer;
|
|
|
2167 |
a.zbuffer_end = (uint8 *) buffer + len;
|
|
|
2168 |
if (do_zlib(&a, p, initial_size, 1, parse_header)) {
|
|
|
2169 |
if (outlen) *outlen = (int) (a.zout - a.zout_start);
|
|
|
2170 |
return a.zout_start;
|
|
|
2171 |
} else {
|
|
|
2172 |
free(a.zout_start);
|
|
|
2173 |
return NULL;
|
|
|
2174 |
}
|
|
|
2175 |
}
|
|
|
2176 |
|
|
|
2177 |
int stbi_zlib_decode_buffer(char *obuffer, int olen, char const *ibuffer, int ilen)
|
|
|
2178 |
{
|
|
|
2179 |
zbuf a;
|
|
|
2180 |
a.zbuffer = (uint8 *) ibuffer;
|
|
|
2181 |
a.zbuffer_end = (uint8 *) ibuffer + ilen;
|
|
|
2182 |
if (do_zlib(&a, obuffer, olen, 0, 1))
|
|
|
2183 |
return (int) (a.zout - a.zout_start);
|
|
|
2184 |
else
|
|
|
2185 |
return -1;
|
|
|
2186 |
}
|
|
|
2187 |
|
|
|
2188 |
char *stbi_zlib_decode_noheader_malloc(char const *buffer, int len, int *outlen)
|
|
|
2189 |
{
|
|
|
2190 |
zbuf a;
|
|
|
2191 |
char *p = (char *) malloc(16384);
|
|
|
2192 |
if (p == NULL) return NULL;
|
|
|
2193 |
a.zbuffer = (uint8 *) buffer;
|
|
|
2194 |
a.zbuffer_end = (uint8 *) buffer+len;
|
|
|
2195 |
if (do_zlib(&a, p, 16384, 1, 0)) {
|
|
|
2196 |
if (outlen) *outlen = (int) (a.zout - a.zout_start);
|
|
|
2197 |
return a.zout_start;
|
|
|
2198 |
} else {
|
|
|
2199 |
free(a.zout_start);
|
|
|
2200 |
return NULL;
|
|
|
2201 |
}
|
|
|
2202 |
}
|
|
|
2203 |
|
|
|
2204 |
int stbi_zlib_decode_noheader_buffer(char *obuffer, int olen, const char *ibuffer, int ilen)
|
|
|
2205 |
{
|
|
|
2206 |
zbuf a;
|
|
|
2207 |
a.zbuffer = (uint8 *) ibuffer;
|
|
|
2208 |
a.zbuffer_end = (uint8 *) ibuffer + ilen;
|
|
|
2209 |
if (do_zlib(&a, obuffer, olen, 0, 0))
|
|
|
2210 |
return (int) (a.zout - a.zout_start);
|
|
|
2211 |
else
|
|
|
2212 |
return -1;
|
|
|
2213 |
}
|
|
|
2214 |
|
|
|
2215 |
// public domain "baseline" PNG decoder v0.10 Sean Barrett 2006-11-18
|
|
|
2216 |
// simple implementation
|
|
|
2217 |
// - only 8-bit samples
|
|
|
2218 |
// - no CRC checking
|
|
|
2219 |
// - allocates lots of intermediate memory
|
|
|
2220 |
// - avoids problem of streaming data between subsystems
|
|
|
2221 |
// - avoids explicit window management
|
|
|
2222 |
// performance
|
|
|
2223 |
// - uses stb_zlib, a PD zlib implementation with fast huffman decoding
|
|
|
2224 |
|
|
|
2225 |
|
|
|
2226 |
typedef struct
|
|
|
2227 |
{
|
|
|
2228 |
uint32 length;
|
|
|
2229 |
uint32 type;
|
|
|
2230 |
} chunk;
|
|
|
2231 |
|
|
|
2232 |
#define PNG_TYPE(a,b,c,d) (((a) << 24) + ((b) << 16) + ((c) << 8) + (d))
|
|
|
2233 |
|
|
|
2234 |
static chunk get_chunk_header(stbi *s)
|
|
|
2235 |
{
|
|
|
2236 |
chunk c;
|
|
|
2237 |
c.length = get32(s);
|
|
|
2238 |
c.type = get32(s);
|
|
|
2239 |
return c;
|
|
|
2240 |
}
|
|
|
2241 |
|
|
|
2242 |
static int check_png_header(stbi *s)
|
|
|
2243 |
{
|
|
|
2244 |
static uint8 png_sig[8] = { 137,80,78,71,13,10,26,10 };
|
|
|
2245 |
int i;
|
|
|
2246 |
for (i=0; i < 8; ++i)
|
|
|
2247 |
if (get8(s) != png_sig[i]) return e("bad png sig","Not a PNG");
|
|
|
2248 |
return 1;
|
|
|
2249 |
}
|
|
|
2250 |
|
|
|
2251 |
typedef struct
|
|
|
2252 |
{
|
|
|
2253 |
stbi s;
|
|
|
2254 |
uint8 *idata, *expanded, *out;
|
|
|
2255 |
} png;
|
|
|
2256 |
|
|
|
2257 |
|
|
|
2258 |
enum {
|
|
|
2259 |
F_none=0, F_sub=1, F_up=2, F_avg=3, F_paeth=4,
|
|
|
2260 |
F_avg_first, F_paeth_first
|
|
|
2261 |
};
|
|
|
2262 |
|
|
|
2263 |
static uint8 first_row_filter[5] =
|
|
|
2264 |
{
|
|
|
2265 |
F_none, F_sub, F_none, F_avg_first, F_paeth_first
|
|
|
2266 |
};
|
|
|
2267 |
|
|
|
2268 |
static int paeth(int a, int b, int c)
|
|
|
2269 |
{
|
|
|
2270 |
int p = a + b - c;
|
|
|
2271 |
int pa = abs(p-a);
|
|
|
2272 |
int pb = abs(p-b);
|
|
|
2273 |
int pc = abs(p-c);
|
|
|
2274 |
if (pa <= pb && pa <= pc) return a;
|
|
|
2275 |
if (pb <= pc) return b;
|
|
|
2276 |
return c;
|
|
|
2277 |
}
|
|
|
2278 |
|
|
|
2279 |
// create the png data from post-deflated data
|
|
|
2280 |
static int create_png_image_raw(png *a, uint8 *raw, uint32 raw_len, int out_n, uint32 x, uint32 y)
|
|
|
2281 |
{
|
|
|
2282 |
stbi *s = &a->s;
|
|
|
2283 |
uint32 i,j,stride = x*out_n;
|
|
|
2284 |
int k;
|
|
|
2285 |
int img_n = s->img_n; // copy it into a local for later
|
|
|
2286 |
assert(out_n == s->img_n || out_n == s->img_n+1);
|
|
|
2287 |
if (stbi_png_partial) y = 1;
|
|
|
2288 |
a->out = (uint8 *) malloc(x * y * out_n);
|
|
|
2289 |
if (!a->out) return e("outofmem", "Out of memory");
|
|
|
2290 |
if (!stbi_png_partial) {
|
|
|
2291 |
if (s->img_x == x && s->img_y == y) {
|
|
|
2292 |
if (raw_len != (img_n * x + 1) * y) return e("not enough pixels","Corrupt PNG");
|
|
|
2293 |
} else { // interlaced:
|
|
|
2294 |
if (raw_len < (img_n * x + 1) * y) return e("not enough pixels","Corrupt PNG");
|
|
|
2295 |
}
|
|
|
2296 |
}
|
|
|
2297 |
for (j=0; j < y; ++j) {
|
|
|
2298 |
uint8 *cur = a->out + stride*j;
|
|
|
2299 |
uint8 *prior = cur - stride;
|
|
|
2300 |
int filter = *raw++;
|
|
|
2301 |
if (filter > 4) return e("invalid filter","Corrupt PNG");
|
|
|
2302 |
// if first row, use special filter that doesn't sample previous row
|
|
|
2303 |
if (j == 0) filter = first_row_filter[filter];
|
|
|
2304 |
// handle first pixel explicitly
|
|
|
2305 |
for (k=0; k < img_n; ++k) {
|
|
|
2306 |
switch (filter) {
|
|
|
2307 |
case F_none : cur[k] = raw[k]; break;
|
|
|
2308 |
case F_sub : cur[k] = raw[k]; break;
|
|
|
2309 |
case F_up : cur[k] = raw[k] + prior[k]; break;
|
|
|
2310 |
case F_avg : cur[k] = raw[k] + (prior[k]>>1); break;
|
|
|
2311 |
case F_paeth : cur[k] = (uint8) (raw[k] + paeth(0,prior[k],0)); break;
|
|
|
2312 |
case F_avg_first : cur[k] = raw[k]; break;
|
|
|
2313 |
case F_paeth_first: cur[k] = raw[k]; break;
|
|
|
2314 |
}
|
|
|
2315 |
}
|
|
|
2316 |
if (img_n != out_n) cur[img_n] = 255;
|
|
|
2317 |
raw += img_n;
|
|
|
2318 |
cur += out_n;
|
|
|
2319 |
prior += out_n;
|
|
|
2320 |
// this is a little gross, so that we don't switch per-pixel or per-component
|
|
|
2321 |
if (img_n == out_n) {
|
|
|
2322 |
#define CASE(f) \
|
|
|
2323 |
case f: \
|
|
|
2324 |
for (i=x-1; i >= 1; --i, raw+=img_n,cur+=img_n,prior+=img_n) \
|
|
|
2325 |
for (k=0; k < img_n; ++k)
|
|
|
2326 |
switch (filter) {
|
|
|
2327 |
CASE(F_none) cur[k] = raw[k]; break;
|
|
|
2328 |
CASE(F_sub) cur[k] = raw[k] + cur[k-img_n]; break;
|
|
|
2329 |
CASE(F_up) cur[k] = raw[k] + prior[k]; break;
|
|
|
2330 |
CASE(F_avg) cur[k] = raw[k] + ((prior[k] + cur[k-img_n])>>1); break;
|
|
|
2331 |
CASE(F_paeth) cur[k] = (uint8) (raw[k] + paeth(cur[k-img_n],prior[k],prior[k-img_n])); break;
|
|
|
2332 |
CASE(F_avg_first) cur[k] = raw[k] + (cur[k-img_n] >> 1); break;
|
|
|
2333 |
CASE(F_paeth_first) cur[k] = (uint8) (raw[k] + paeth(cur[k-img_n],0,0)); break;
|
|
|
2334 |
}
|
|
|
2335 |
#undef CASE
|
|
|
2336 |
} else {
|
|
|
2337 |
assert(img_n+1 == out_n);
|
|
|
2338 |
#define CASE(f) \
|
|
|
2339 |
case f: \
|
|
|
2340 |
for (i=x-1; i >= 1; --i, cur[img_n]=255,raw+=img_n,cur+=out_n,prior+=out_n) \
|
|
|
2341 |
for (k=0; k < img_n; ++k)
|
|
|
2342 |
switch (filter) {
|
|
|
2343 |
CASE(F_none) cur[k] = raw[k]; break;
|
|
|
2344 |
CASE(F_sub) cur[k] = raw[k] + cur[k-out_n]; break;
|
|
|
2345 |
CASE(F_up) cur[k] = raw[k] + prior[k]; break;
|
|
|
2346 |
CASE(F_avg) cur[k] = raw[k] + ((prior[k] + cur[k-out_n])>>1); break;
|
|
|
2347 |
CASE(F_paeth) cur[k] = (uint8) (raw[k] + paeth(cur[k-out_n],prior[k],prior[k-out_n])); break;
|
|
|
2348 |
CASE(F_avg_first) cur[k] = raw[k] + (cur[k-out_n] >> 1); break;
|
|
|
2349 |
CASE(F_paeth_first) cur[k] = (uint8) (raw[k] + paeth(cur[k-out_n],0,0)); break;
|
|
|
2350 |
}
|
|
|
2351 |
#undef CASE
|
|
|
2352 |
}
|
|
|
2353 |
}
|
|
|
2354 |
return 1;
|
|
|
2355 |
}
|
|
|
2356 |
|
|
|
2357 |
static int create_png_image(png *a, uint8 *raw, uint32 raw_len, int out_n, int interlaced)
|
|
|
2358 |
{
|
|
|
2359 |
uint8 *final;
|
|
|
2360 |
int p;
|
|
|
2361 |
int save;
|
|
|
2362 |
if (!interlaced)
|
|
|
2363 |
return create_png_image_raw(a, raw, raw_len, out_n, a->s.img_x, a->s.img_y);
|
|
|
2364 |
save = stbi_png_partial;
|
|
|
2365 |
stbi_png_partial = 0;
|
|
|
2366 |
|
|
|
2367 |
// de-interlacing
|
|
|
2368 |
final = (uint8 *) malloc(a->s.img_x * a->s.img_y * out_n);
|
|
|
2369 |
for (p=0; p < 7; ++p) {
|
|
|
2370 |
int xorig[] = { 0,4,0,2,0,1,0 };
|
|
|
2371 |
int yorig[] = { 0,0,4,0,2,0,1 };
|
|
|
2372 |
int xspc[] = { 8,8,4,4,2,2,1 };
|
|
|
2373 |
int yspc[] = { 8,8,8,4,4,2,2 };
|
|
|
2374 |
int i,j,x,y;
|
|
|
2375 |
// pass1_x[4] = 0, pass1_x[5] = 1, pass1_x[12] = 1
|
|
|
2376 |
x = (a->s.img_x - xorig[p] + xspc[p]-1) / xspc[p];
|
|
|
2377 |
y = (a->s.img_y - yorig[p] + yspc[p]-1) / yspc[p];
|
|
|
2378 |
if (x && y) {
|
|
|
2379 |
if (!create_png_image_raw(a, raw, raw_len, out_n, x, y)) {
|
|
|
2380 |
free(final);
|
|
|
2381 |
return 0;
|
|
|
2382 |
}
|
|
|
2383 |
for (j=0; j < y; ++j)
|
|
|
2384 |
for (i=0; i < x; ++i)
|
|
|
2385 |
memcpy(final + (j*yspc[p]+yorig[p])*a->s.img_x*out_n + (i*xspc[p]+xorig[p])*out_n,
|
|
|
2386 |
a->out + (j*x+i)*out_n, out_n);
|
|
|
2387 |
free(a->out);
|
|
|
2388 |
raw += (x*out_n+1)*y;
|
|
|
2389 |
raw_len -= (x*out_n+1)*y;
|
|
|
2390 |
}
|
|
|
2391 |
}
|
|
|
2392 |
a->out = final;
|
|
|
2393 |
|
|
|
2394 |
stbi_png_partial = save;
|
|
|
2395 |
return 1;
|
|
|
2396 |
}
|
|
|
2397 |
|
|
|
2398 |
static int compute_transparency(png *z, uint8 tc[3], int out_n)
|
|
|
2399 |
{
|
|
|
2400 |
stbi *s = &z->s;
|
|
|
2401 |
uint32 i, pixel_count = s->img_x * s->img_y;
|
|
|
2402 |
uint8 *p = z->out;
|
|
|
2403 |
|
|
|
2404 |
// compute color-based transparency, assuming we've
|
|
|
2405 |
// already got 255 as the alpha value in the output
|
|
|
2406 |
assert(out_n == 2 || out_n == 4);
|
|
|
2407 |
|
|
|
2408 |
if (out_n == 2) {
|
|
|
2409 |
for (i=0; i < pixel_count; ++i) {
|
|
|
2410 |
p[1] = (p[0] == tc[0] ? 0 : 255);
|
|
|
2411 |
p += 2;
|
|
|
2412 |
}
|
|
|
2413 |
} else {
|
|
|
2414 |
for (i=0; i < pixel_count; ++i) {
|
|
|
2415 |
if (p[0] == tc[0] && p[1] == tc[1] && p[2] == tc[2])
|
|
|
2416 |
p[3] = 0;
|
|
|
2417 |
p += 4;
|
|
|
2418 |
}
|
|
|
2419 |
}
|
|
|
2420 |
return 1;
|
|
|
2421 |
}
|
|
|
2422 |
|
|
|
2423 |
static int expand_palette(png *a, uint8 *palette, int len, int pal_img_n)
|
|
|
2424 |
{
|
|
|
2425 |
uint32 i, pixel_count = a->s.img_x * a->s.img_y;
|
|
|
2426 |
uint8 *p, *temp_out, *orig = a->out;
|
|
|
2427 |
|
|
|
2428 |
p = (uint8 *) malloc(pixel_count * pal_img_n);
|
|
|
2429 |
if (p == NULL) return e("outofmem", "Out of memory");
|
|
|
2430 |
|
|
|
2431 |
// between here and free(out) below, exitting would leak
|
|
|
2432 |
temp_out = p;
|
|
|
2433 |
|
|
|
2434 |
if (pal_img_n == 3) {
|
|
|
2435 |
for (i=0; i < pixel_count; ++i) {
|
|
|
2436 |
int n = orig[i]*4;
|
|
|
2437 |
p[0] = palette[n ];
|
|
|
2438 |
p[1] = palette[n+1];
|
|
|
2439 |
p[2] = palette[n+2];
|
|
|
2440 |
p += 3;
|
|
|
2441 |
}
|
|
|
2442 |
} else {
|
|
|
2443 |
for (i=0; i < pixel_count; ++i) {
|
|
|
2444 |
int n = orig[i]*4;
|
|
|
2445 |
p[0] = palette[n ];
|
|
|
2446 |
p[1] = palette[n+1];
|
|
|
2447 |
p[2] = palette[n+2];
|
|
|
2448 |
p[3] = palette[n+3];
|
|
|
2449 |
p += 4;
|
|
|
2450 |
}
|
|
|
2451 |
}
|
|
|
2452 |
free(a->out);
|
|
|
2453 |
a->out = temp_out;
|
|
|
2454 |
|
|
|
2455 |
STBI_NOTUSED(len);
|
|
|
2456 |
|
|
|
2457 |
return 1;
|
|
|
2458 |
}
|
|
|
2459 |
|
|
|
2460 |
static int stbi_unpremultiply_on_load = 0;
|
|
|
2461 |
static int stbi_de_iphone_flag = 0;
|
|
|
2462 |
|
|
|
2463 |
void stbi_set_unpremultiply_on_load(int flag_true_if_should_unpremultiply)
|
|
|
2464 |
{
|
|
|
2465 |
stbi_unpremultiply_on_load = flag_true_if_should_unpremultiply;
|
|
|
2466 |
}
|
|
|
2467 |
void stbi_convert_iphone_png_to_rgb(int flag_true_if_should_convert)
|
|
|
2468 |
{
|
|
|
2469 |
stbi_de_iphone_flag = flag_true_if_should_convert;
|
|
|
2470 |
}
|
|
|
2471 |
|
|
|
2472 |
static void stbi_de_iphone(png *z)
|
|
|
2473 |
{
|
|
|
2474 |
stbi *s = &z->s;
|
|
|
2475 |
uint32 i, pixel_count = s->img_x * s->img_y;
|
|
|
2476 |
uint8 *p = z->out;
|
|
|
2477 |
|
|
|
2478 |
if (s->img_out_n == 3) { // convert bgr to rgb
|
|
|
2479 |
for (i=0; i < pixel_count; ++i) {
|
|
|
2480 |
uint8 t = p[0];
|
|
|
2481 |
p[0] = p[2];
|
|
|
2482 |
p[2] = t;
|
|
|
2483 |
p += 3;
|
|
|
2484 |
}
|
|
|
2485 |
} else {
|
|
|
2486 |
assert(s->img_out_n == 4);
|
|
|
2487 |
if (stbi_unpremultiply_on_load) {
|
|
|
2488 |
// convert bgr to rgb and unpremultiply
|
|
|
2489 |
for (i=0; i < pixel_count; ++i) {
|
|
|
2490 |
uint8 a = p[3];
|
|
|
2491 |
uint8 t = p[0];
|
|
|
2492 |
if (a) {
|
|
|
2493 |
p[0] = p[2] * 255 / a;
|
|
|
2494 |
p[1] = p[1] * 255 / a;
|
|
|
2495 |
p[2] = t * 255 / a;
|
|
|
2496 |
} else {
|
|
|
2497 |
p[0] = p[2];
|
|
|
2498 |
p[2] = t;
|
|
|
2499 |
}
|
|
|
2500 |
p += 4;
|
|
|
2501 |
}
|
|
|
2502 |
} else {
|
|
|
2503 |
// convert bgr to rgb
|
|
|
2504 |
for (i=0; i < pixel_count; ++i) {
|
|
|
2505 |
uint8 t = p[0];
|
|
|
2506 |
p[0] = p[2];
|
|
|
2507 |
p[2] = t;
|
|
|
2508 |
p += 4;
|
|
|
2509 |
}
|
|
|
2510 |
}
|
|
|
2511 |
}
|
|
|
2512 |
}
|
|
|
2513 |
|
|
|
2514 |
static int parse_png_file(png *z, int scan, int req_comp)
|
|
|
2515 |
{
|
|
|
2516 |
uint8 palette[1024], pal_img_n=0;
|
|
|
2517 |
uint8 has_trans=0, tc[3];
|
|
|
2518 |
uint32 ioff=0, idata_limit=0, i, pal_len=0;
|
|
|
2519 |
int first=1,k,interlace=0, iphone=0;
|
|
|
2520 |
stbi *s = &z->s;
|
|
|
2521 |
|
|
|
2522 |
if (!check_png_header(s)) return 0;
|
|
|
2523 |
|
|
|
2524 |
if (scan == SCAN_type) return 1;
|
|
|
2525 |
|
|
|
2526 |
for (;;) {
|
|
|
2527 |
chunk c = get_chunk_header(s);
|
|
|
2528 |
switch (c.type) {
|
|
|
2529 |
case PNG_TYPE('C','g','B','I'):
|
|
|
2530 |
iphone = stbi_de_iphone_flag;
|
|
|
2531 |
skip(s, c.length);
|
|
|
2532 |
break;
|
|
|
2533 |
case PNG_TYPE('I','H','D','R'): {
|
|
|
2534 |
int depth,color,comp,filter;
|
|
|
2535 |
if (!first) return e("multiple IHDR","Corrupt PNG");
|
|
|
2536 |
first = 0;
|
|
|
2537 |
if (c.length != 13) return e("bad IHDR len","Corrupt PNG");
|
|
|
2538 |
s->img_x = get32(s); if (s->img_x > (1 << 24)) return e("too large","Very large image (corrupt?)");
|
|
|
2539 |
s->img_y = get32(s); if (s->img_y > (1 << 24)) return e("too large","Very large image (corrupt?)");
|
|
|
2540 |
depth = get8(s); if (depth != 8) return e("8bit only","PNG not supported: 8-bit only");
|
|
|
2541 |
color = get8(s); if (color > 6) return e("bad ctype","Corrupt PNG");
|
|
|
2542 |
if (color == 3) pal_img_n = 3; else if (color & 1) return e("bad ctype","Corrupt PNG");
|
|
|
2543 |
comp = get8(s); if (comp) return e("bad comp method","Corrupt PNG");
|
|
|
2544 |
filter= get8(s); if (filter) return e("bad filter method","Corrupt PNG");
|
|
|
2545 |
interlace = get8(s); if (interlace>1) return e("bad interlace method","Corrupt PNG");
|
|
|
2546 |
if (!s->img_x || !s->img_y) return e("0-pixel image","Corrupt PNG");
|
|
|
2547 |
if (!pal_img_n) {
|
|
|
2548 |
s->img_n = (color & 2 ? 3 : 1) + (color & 4 ? 1 : 0);
|
|
|
2549 |
if ((1 << 30) / s->img_x / s->img_n < s->img_y) return e("too large", "Image too large to decode");
|
|
|
2550 |
if (scan == SCAN_header) return 1;
|
|
|
2551 |
} else {
|
|
|
2552 |
// if paletted, then pal_n is our final components, and
|
|
|
2553 |
// img_n is # components to decompress/filter.
|
|
|
2554 |
s->img_n = 1;
|
|
|
2555 |
if ((1 << 30) / s->img_x / 4 < s->img_y) return e("too large","Corrupt PNG");
|
|
|
2556 |
// if SCAN_header, have to scan to see if we have a tRNS
|
|
|
2557 |
}
|
|
|
2558 |
break;
|
|
|
2559 |
}
|
|
|
2560 |
|
|
|
2561 |
case PNG_TYPE('P','L','T','E'): {
|
|
|
2562 |
if (first) return e("first not IHDR", "Corrupt PNG");
|
|
|
2563 |
if (c.length > 256*3) return e("invalid PLTE","Corrupt PNG");
|
|
|
2564 |
pal_len = c.length / 3;
|
|
|
2565 |
if (pal_len * 3 != c.length) return e("invalid PLTE","Corrupt PNG");
|
|
|
2566 |
for (i=0; i < pal_len; ++i) {
|
|
|
2567 |
palette[i*4+0] = get8u(s);
|
|
|
2568 |
palette[i*4+1] = get8u(s);
|
|
|
2569 |
palette[i*4+2] = get8u(s);
|
|
|
2570 |
palette[i*4+3] = 255;
|
|
|
2571 |
}
|
|
|
2572 |
break;
|
|
|
2573 |
}
|
|
|
2574 |
|
|
|
2575 |
case PNG_TYPE('t','R','N','S'): {
|
|
|
2576 |
if (first) return e("first not IHDR", "Corrupt PNG");
|
|
|
2577 |
if (z->idata) return e("tRNS after IDAT","Corrupt PNG");
|
|
|
2578 |
if (pal_img_n) {
|
|
|
2579 |
if (scan == SCAN_header) { s->img_n = 4; return 1; }
|
|
|
2580 |
if (pal_len == 0) return e("tRNS before PLTE","Corrupt PNG");
|
|
|
2581 |
if (c.length > pal_len) return e("bad tRNS len","Corrupt PNG");
|
|
|
2582 |
pal_img_n = 4;
|
|
|
2583 |
for (i=0; i < c.length; ++i)
|
|
|
2584 |
palette[i*4+3] = get8u(s);
|
|
|
2585 |
} else {
|
|
|
2586 |
if (!(s->img_n & 1)) return e("tRNS with alpha","Corrupt PNG");
|
|
|
2587 |
if (c.length != (uint32) s->img_n*2) return e("bad tRNS len","Corrupt PNG");
|
|
|
2588 |
has_trans = 1;
|
|
|
2589 |
for (k=0; k < s->img_n; ++k)
|
|
|
2590 |
tc[k] = (uint8) get16(s); // non 8-bit images will be larger
|
|
|
2591 |
}
|
|
|
2592 |
break;
|
|
|
2593 |
}
|
|
|
2594 |
|
|
|
2595 |
case PNG_TYPE('I','D','A','T'): {
|
|
|
2596 |
if (first) return e("first not IHDR", "Corrupt PNG");
|
|
|
2597 |
if (pal_img_n && !pal_len) return e("no PLTE","Corrupt PNG");
|
|
|
2598 |
if (scan == SCAN_header) { s->img_n = pal_img_n; return 1; }
|
|
|
2599 |
if (ioff + c.length > idata_limit) {
|
|
|
2600 |
uint8 *p;
|
|
|
2601 |
if (idata_limit == 0) idata_limit = c.length > 4096 ? c.length : 4096;
|
|
|
2602 |
while (ioff + c.length > idata_limit)
|
|
|
2603 |
idata_limit *= 2;
|
|
|
2604 |
p = (uint8 *) realloc(z->idata, idata_limit); if (p == NULL) return e("outofmem", "Out of memory");
|
|
|
2605 |
z->idata = p;
|
|
|
2606 |
}
|
|
|
2607 |
if (!getn(s, z->idata+ioff,c.length)) return e("outofdata","Corrupt PNG");
|
|
|
2608 |
ioff += c.length;
|
|
|
2609 |
break;
|
|
|
2610 |
}
|
|
|
2611 |
|
|
|
2612 |
case PNG_TYPE('I','E','N','D'): {
|
|
|
2613 |
uint32 raw_len;
|
|
|
2614 |
if (first) return e("first not IHDR", "Corrupt PNG");
|
|
|
2615 |
if (scan != SCAN_load) return 1;
|
|
|
2616 |
if (z->idata == NULL) return e("no IDAT","Corrupt PNG");
|
|
|
2617 |
z->expanded = (uint8 *) stbi_zlib_decode_malloc_guesssize_headerflag((char *) z->idata, ioff, 16384, (int *) &raw_len, !iphone);
|
|
|
2618 |
if (z->expanded == NULL) return 0; // zlib should set error
|
|
|
2619 |
free(z->idata); z->idata = NULL;
|
|
|
2620 |
if ((req_comp == s->img_n+1 && req_comp != 3 && !pal_img_n) || has_trans)
|
|
|
2621 |
s->img_out_n = s->img_n+1;
|
|
|
2622 |
else
|
|
|
2623 |
s->img_out_n = s->img_n;
|
|
|
2624 |
if (!create_png_image(z, z->expanded, raw_len, s->img_out_n, interlace)) return 0;
|
|
|
2625 |
if (has_trans)
|
|
|
2626 |
if (!compute_transparency(z, tc, s->img_out_n)) return 0;
|
|
|
2627 |
if (iphone && s->img_out_n > 2)
|
|
|
2628 |
stbi_de_iphone(z);
|
|
|
2629 |
if (pal_img_n) {
|
|
|
2630 |
// pal_img_n == 3 or 4
|
|
|
2631 |
s->img_n = pal_img_n; // record the actual colors we had
|
|
|
2632 |
s->img_out_n = pal_img_n;
|
|
|
2633 |
if (req_comp >= 3) s->img_out_n = req_comp;
|
|
|
2634 |
if (!expand_palette(z, palette, pal_len, s->img_out_n))
|
|
|
2635 |
return 0;
|
|
|
2636 |
}
|
|
|
2637 |
free(z->expanded); z->expanded = NULL;
|
|
|
2638 |
return 1;
|
|
|
2639 |
}
|
|
|
2640 |
|
|
|
2641 |
default:
|
|
|
2642 |
// if critical, fail
|
|
|
2643 |
if (first) return e("first not IHDR", "Corrupt PNG");
|
|
|
2644 |
if ((c.type & (1 << 29)) == 0) {
|
|
|
2645 |
#ifndef STBI_NO_FAILURE_STRINGS
|
|
|
2646 |
// not threadsafe
|
|
|
2647 |
static char invalid_chunk[] = "XXXX chunk not known";
|
|
|
2648 |
invalid_chunk[0] = (uint8) (c.type >> 24);
|
|
|
2649 |
invalid_chunk[1] = (uint8) (c.type >> 16);
|
|
|
2650 |
invalid_chunk[2] = (uint8) (c.type >> 8);
|
|
|
2651 |
invalid_chunk[3] = (uint8) (c.type >> 0);
|
|
|
2652 |
#endif
|
|
|
2653 |
return e(invalid_chunk, "PNG not supported: unknown chunk type");
|
|
|
2654 |
}
|
|
|
2655 |
skip(s, c.length);
|
|
|
2656 |
break;
|
|
|
2657 |
}
|
|
|
2658 |
// end of chunk, read and skip CRC
|
|
|
2659 |
get32(s);
|
|
|
2660 |
}
|
|
|
2661 |
}
|
|
|
2662 |
|
|
|
2663 |
static unsigned char *do_png(png *p, int *x, int *y, int *n, int req_comp)
|
|
|
2664 |
{
|
|
|
2665 |
unsigned char *result=NULL;
|
|
|
2666 |
p->expanded = NULL;
|
|
|
2667 |
p->idata = NULL;
|
|
|
2668 |
p->out = NULL;
|
|
|
2669 |
if (req_comp < 0 || req_comp > 4) return epuc("bad req_comp", "Internal error");
|
|
|
2670 |
if (parse_png_file(p, SCAN_load, req_comp)) {
|
|
|
2671 |
result = p->out;
|
|
|
2672 |
p->out = NULL;
|
|
|
2673 |
if (req_comp && req_comp != p->s.img_out_n) {
|
|
|
2674 |
result = convert_format(result, p->s.img_out_n, req_comp, p->s.img_x, p->s.img_y);
|
|
|
2675 |
p->s.img_out_n = req_comp;
|
|
|
2676 |
if (result == NULL) return result;
|
|
|
2677 |
}
|
|
|
2678 |
*x = p->s.img_x;
|
|
|
2679 |
*y = p->s.img_y;
|
|
|
2680 |
if (n) *n = p->s.img_n;
|
|
|
2681 |
}
|
|
|
2682 |
free(p->out); p->out = NULL;
|
|
|
2683 |
free(p->expanded); p->expanded = NULL;
|
|
|
2684 |
free(p->idata); p->idata = NULL;
|
|
|
2685 |
|
|
|
2686 |
return result;
|
|
|
2687 |
}
|
|
|
2688 |
|
|
|
2689 |
#ifndef STBI_NO_STDIO
|
|
|
2690 |
unsigned char *stbi_png_load_from_file(FILE *f, int *x, int *y, int *comp, int req_comp)
|
|
|
2691 |
{
|
|
|
2692 |
png p;
|
|
|
2693 |
start_file(&p.s, f);
|
|
|
2694 |
return do_png(&p, x,y,comp,req_comp);
|
|
|
2695 |
}
|
|
|
2696 |
|
|
|
2697 |
unsigned char *stbi_png_load(char const *filename, int *x, int *y, int *comp, int req_comp)
|
|
|
2698 |
{
|
|
|
2699 |
unsigned char *data;
|
|
|
2700 |
FILE *f = fopen(filename, "rb");
|
|
|
2701 |
if (!f) return NULL;
|
|
|
2702 |
data = stbi_png_load_from_file(f,x,y,comp,req_comp);
|
|
|
2703 |
fclose(f);
|
|
|
2704 |
return data;
|
|
|
2705 |
}
|
|
|
2706 |
#endif
|
|
|
2707 |
|
|
|
2708 |
unsigned char *stbi_png_load_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp)
|
|
|
2709 |
{
|
|
|
2710 |
png p;
|
|
|
2711 |
start_mem(&p.s, buffer,len);
|
|
|
2712 |
return do_png(&p, x,y,comp,req_comp);
|
|
|
2713 |
}
|
|
|
2714 |
|
|
|
2715 |
#ifndef STBI_NO_STDIO
|
|
|
2716 |
int stbi_png_test_file(FILE *f)
|
|
|
2717 |
{
|
|
|
2718 |
png p;
|
|
|
2719 |
int n,r;
|
|
|
2720 |
n = ftell(f);
|
|
|
2721 |
start_file(&p.s, f);
|
|
|
2722 |
r = parse_png_file(&p, SCAN_type,STBI_default);
|
|
|
2723 |
fseek(f,n,SEEK_SET);
|
|
|
2724 |
return r;
|
|
|
2725 |
}
|
|
|
2726 |
#endif
|
|
|
2727 |
|
|
|
2728 |
int stbi_png_test_memory(stbi_uc const *buffer, int len)
|
|
|
2729 |
{
|
|
|
2730 |
png p;
|
|
|
2731 |
start_mem(&p.s, buffer, len);
|
|
|
2732 |
return parse_png_file(&p, SCAN_type,STBI_default);
|
|
|
2733 |
}
|
|
|
2734 |
|
|
|
2735 |
static int stbi_png_info_raw(png *p, int *x, int *y, int *comp)
|
|
|
2736 |
{
|
|
|
2737 |
if (!parse_png_file(p, SCAN_header, 0))
|
|
|
2738 |
return 0;
|
|
|
2739 |
if (x) *x = p->s.img_x;
|
|
|
2740 |
if (y) *y = p->s.img_y;
|
|
|
2741 |
if (comp) *comp = p->s.img_n;
|
|
|
2742 |
return 1;
|
|
|
2743 |
}
|
|
|
2744 |
|
|
|
2745 |
#ifndef STBI_NO_STDIO
|
|
|
2746 |
int stbi_png_info (char const *filename, int *x, int *y, int *comp)
|
|
|
2747 |
{
|
|
|
2748 |
int res;
|
|
|
2749 |
FILE *f = fopen(filename, "rb");
|
|
|
2750 |
if (!f) return 0;
|
|
|
2751 |
res = stbi_png_info_from_file(f, x, y, comp);
|
|
|
2752 |
fclose(f);
|
|
|
2753 |
return res;
|
|
|
2754 |
}
|
|
|
2755 |
|
|
|
2756 |
int stbi_png_info_from_file(FILE *f, int *x, int *y, int *comp)
|
|
|
2757 |
{
|
|
|
2758 |
png p;
|
|
|
2759 |
int res;
|
|
|
2760 |
long n = ftell(f);
|
|
|
2761 |
start_file(&p.s, f);
|
|
|
2762 |
res = stbi_png_info_raw(&p, x, y, comp);
|
|
|
2763 |
fseek(f, n, SEEK_SET);
|
|
|
2764 |
return res;
|
|
|
2765 |
}
|
|
|
2766 |
#endif // !STBI_NO_STDIO
|
|
|
2767 |
|
|
|
2768 |
int stbi_png_info_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp)
|
|
|
2769 |
{
|
|
|
2770 |
png p;
|
|
|
2771 |
start_mem(&p.s, buffer, len);
|
|
|
2772 |
return stbi_png_info_raw(&p, x, y, comp);
|
|
|
2773 |
}
|
|
|
2774 |
|
|
|
2775 |
// Microsoft/Windows BMP image
|
|
|
2776 |
|
|
|
2777 |
static int bmp_test(stbi *s)
|
|
|
2778 |
{
|
|
|
2779 |
int sz;
|
|
|
2780 |
if (get8(s) != 'B') return 0;
|
|
|
2781 |
if (get8(s) != 'M') return 0;
|
|
|
2782 |
get32le(s); // discard filesize
|
|
|
2783 |
get16le(s); // discard reserved
|
|
|
2784 |
get16le(s); // discard reserved
|
|
|
2785 |
get32le(s); // discard data offset
|
|
|
2786 |
sz = get32le(s);
|
|
|
2787 |
if (sz == 12 || sz == 40 || sz == 56 || sz == 108) return 1;
|
|
|
2788 |
return 0;
|
|
|
2789 |
}
|
|
|
2790 |
|
|
|
2791 |
#ifndef STBI_NO_STDIO
|
|
|
2792 |
int stbi_bmp_test_file (FILE *f)
|
|
|
2793 |
{
|
|
|
2794 |
stbi s;
|
|
|
2795 |
int r,n = ftell(f);
|
|
|
2796 |
start_file(&s,f);
|
|
|
2797 |
r = bmp_test(&s);
|
|
|
2798 |
fseek(f,n,SEEK_SET);
|
|
|
2799 |
return r;
|
|
|
2800 |
}
|
|
|
2801 |
#endif
|
|
|
2802 |
|
|
|
2803 |
int stbi_bmp_test_memory (stbi_uc const *buffer, int len)
|
|
|
2804 |
{
|
|
|
2805 |
stbi s;
|
|
|
2806 |
start_mem(&s, buffer, len);
|
|
|
2807 |
return bmp_test(&s);
|
|
|
2808 |
}
|
|
|
2809 |
|
|
|
2810 |
// returns 0..31 for the highest set bit
|
|
|
2811 |
static int high_bit(unsigned int z)
|
|
|
2812 |
{
|
|
|
2813 |
int n=0;
|
|
|
2814 |
if (z == 0) return -1;
|
|
|
2815 |
if (z >= 0x10000) n += 16, z >>= 16;
|
|
|
2816 |
if (z >= 0x00100) n += 8, z >>= 8;
|
|
|
2817 |
if (z >= 0x00010) n += 4, z >>= 4;
|
|
|
2818 |
if (z >= 0x00004) n += 2, z >>= 2;
|
|
|
2819 |
if (z >= 0x00002) n += 1, z >>= 1;
|
|
|
2820 |
return n;
|
|
|
2821 |
}
|
|
|
2822 |
|
|
|
2823 |
static int bitcount(unsigned int a)
|
|
|
2824 |
{
|
|
|
2825 |
a = (a & 0x55555555) + ((a >> 1) & 0x55555555); // max 2
|
|
|
2826 |
a = (a & 0x33333333) + ((a >> 2) & 0x33333333); // max 4
|
|
|
2827 |
a = (a + (a >> 4)) & 0x0f0f0f0f; // max 8 per 4, now 8 bits
|
|
|
2828 |
a = (a + (a >> 8)); // max 16 per 8 bits
|
|
|
2829 |
a = (a + (a >> 16)); // max 32 per 8 bits
|
|
|
2830 |
return a & 0xff;
|
|
|
2831 |
}
|
|
|
2832 |
|
|
|
2833 |
static int shiftsigned(int v, int shift, int bits)
|
|
|
2834 |
{
|
|
|
2835 |
int result;
|
|
|
2836 |
int z=0;
|
|
|
2837 |
|
|
|
2838 |
if (shift < 0) v <<= -shift;
|
|
|
2839 |
else v >>= shift;
|
|
|
2840 |
result = v;
|
|
|
2841 |
|
|
|
2842 |
z = bits;
|
|
|
2843 |
while (z < 8) {
|
|
|
2844 |
result += v >> z;
|
|
|
2845 |
z += bits;
|
|
|
2846 |
}
|
|
|
2847 |
return result;
|
|
|
2848 |
}
|
|
|
2849 |
|
|
|
2850 |
static stbi_uc *bmp_load(stbi *s, int *x, int *y, int *comp, int req_comp)
|
|
|
2851 |
{
|
|
|
2852 |
uint8 *out;
|
|
|
2853 |
unsigned int mr=0,mg=0,mb=0,ma=0, fake_a=0;
|
|
|
2854 |
stbi_uc pal[256][4];
|
|
|
2855 |
int psize=0,i,j,compress=0,width;
|
|
|
2856 |
int bpp, flip_vertically, pad, target, offset, hsz;
|
|
|
2857 |
if (get8(s) != 'B' || get8(s) != 'M') return epuc("not BMP", "Corrupt BMP");
|
|
|
2858 |
get32le(s); // discard filesize
|
|
|
2859 |
get16le(s); // discard reserved
|
|
|
2860 |
get16le(s); // discard reserved
|
|
|
2861 |
offset = get32le(s);
|
|
|
2862 |
hsz = get32le(s);
|
|
|
2863 |
if (hsz != 12 && hsz != 40 && hsz != 56 && hsz != 108) return epuc("unknown BMP", "BMP type not supported: unknown");
|
|
|
2864 |
if (hsz == 12) {
|
|
|
2865 |
s->img_x = get16le(s);
|
|
|
2866 |
s->img_y = get16le(s);
|
|
|
2867 |
} else {
|
|
|
2868 |
s->img_x = get32le(s);
|
|
|
2869 |
s->img_y = get32le(s);
|
|
|
2870 |
}
|
|
|
2871 |
if (get16le(s) != 1) return epuc("bad BMP", "bad BMP");
|
|
|
2872 |
bpp = get16le(s);
|
|
|
2873 |
if (bpp == 1) return epuc("monochrome", "BMP type not supported: 1-bit");
|
|
|
2874 |
flip_vertically = ((int) s->img_y) > 0;
|
|
|
2875 |
s->img_y = abs((int) s->img_y);
|
|
|
2876 |
if (hsz == 12) {
|
|
|
2877 |
if (bpp < 24)
|
|
|
2878 |
psize = (offset - 14 - 24) / 3;
|
|
|
2879 |
} else {
|
|
|
2880 |
compress = get32le(s);
|
|
|
2881 |
if (compress == 1 || compress == 2) return epuc("BMP RLE", "BMP type not supported: RLE");
|
|
|
2882 |
get32le(s); // discard sizeof
|
|
|
2883 |
get32le(s); // discard hres
|
|
|
2884 |
get32le(s); // discard vres
|
|
|
2885 |
get32le(s); // discard colorsused
|
|
|
2886 |
get32le(s); // discard max important
|
|
|
2887 |
if (hsz == 40 || hsz == 56) {
|
|
|
2888 |
if (hsz == 56) {
|
|
|
2889 |
get32le(s);
|
|
|
2890 |
get32le(s);
|
|
|
2891 |
get32le(s);
|
|
|
2892 |
get32le(s);
|
|
|
2893 |
}
|
|
|
2894 |
if (bpp == 16 || bpp == 32) {
|
|
|
2895 |
mr = mg = mb = 0;
|
|
|
2896 |
if (compress == 0) {
|
|
|
2897 |
if (bpp == 32) {
|
|
|
2898 |
mr = 0xffu << 16;
|
|
|
2899 |
mg = 0xffu << 8;
|
|
|
2900 |
mb = 0xffu << 0;
|
|
|
2901 |
ma = 0xffu << 24;
|
|
|
2902 |
fake_a = 1; // @TODO: check for cases like alpha value is all 0 and switch it to 255
|
|
|
2903 |
} else {
|
|
|
2904 |
mr = 31u << 10;
|
|
|
2905 |
mg = 31u << 5;
|
|
|
2906 |
mb = 31u << 0;
|
|
|
2907 |
}
|
|
|
2908 |
} else if (compress == 3) {
|
|
|
2909 |
mr = get32le(s);
|
|
|
2910 |
mg = get32le(s);
|
|
|
2911 |
mb = get32le(s);
|
|
|
2912 |
// not documented, but generated by photoshop and handled by mspaint
|
|
|
2913 |
if (mr == mg && mg == mb) {
|
|
|
2914 |
// ?!?!?
|
|
|
2915 |
return epuc("bad BMP", "bad BMP");
|
|
|
2916 |
}
|
|
|
2917 |
} else
|
|
|
2918 |
return epuc("bad BMP", "bad BMP");
|
|
|
2919 |
}
|
|
|
2920 |
} else {
|
|
|
2921 |
assert(hsz == 108);
|
|
|
2922 |
mr = get32le(s);
|
|
|
2923 |
mg = get32le(s);
|
|
|
2924 |
mb = get32le(s);
|
|
|
2925 |
ma = get32le(s);
|
|
|
2926 |
get32le(s); // discard color space
|
|
|
2927 |
for (i=0; i < 12; ++i)
|
|
|
2928 |
get32le(s); // discard color space parameters
|
|
|
2929 |
}
|
|
|
2930 |
if (bpp < 16)
|
|
|
2931 |
psize = (offset - 14 - hsz) >> 2;
|
|
|
2932 |
}
|
|
|
2933 |
s->img_n = ma ? 4 : 3;
|
|
|
2934 |
if (req_comp && req_comp >= 3) // we can directly decode 3 or 4
|
|
|
2935 |
target = req_comp;
|
|
|
2936 |
else
|
|
|
2937 |
target = s->img_n; // if they want monochrome, we'll post-convert
|
|
|
2938 |
out = (stbi_uc *) malloc(target * s->img_x * s->img_y);
|
|
|
2939 |
if (!out) return epuc("outofmem", "Out of memory");
|
|
|
2940 |
if (bpp < 16) {
|
|
|
2941 |
int z=0;
|
|
|
2942 |
if (psize == 0 || psize > 256) { free(out); return epuc("invalid", "Corrupt BMP"); }
|
|
|
2943 |
for (i=0; i < psize; ++i) {
|
|
|
2944 |
pal[i][2] = get8u(s);
|
|
|
2945 |
pal[i][1] = get8u(s);
|
|
|
2946 |
pal[i][0] = get8u(s);
|
|
|
2947 |
if (hsz != 12) get8(s);
|
|
|
2948 |
pal[i][3] = 255;
|
|
|
2949 |
}
|
|
|
2950 |
skip(s, offset - 14 - hsz - psize * (hsz == 12 ? 3 : 4));
|
|
|
2951 |
if (bpp == 4) width = (s->img_x + 1) >> 1;
|
|
|
2952 |
else if (bpp == 8) width = s->img_x;
|
|
|
2953 |
else { free(out); return epuc("bad bpp", "Corrupt BMP"); }
|
|
|
2954 |
pad = (-width)&3;
|
|
|
2955 |
for (j=0; j < (int) s->img_y; ++j) {
|
|
|
2956 |
for (i=0; i < (int) s->img_x; i += 2) {
|
|
|
2957 |
int v=get8(s),v2=0;
|
|
|
2958 |
if (bpp == 4) {
|
|
|
2959 |
v2 = v & 15;
|
|
|
2960 |
v >>= 4;
|
|
|
2961 |
}
|
|
|
2962 |
out[z++] = pal[v][0];
|
|
|
2963 |
out[z++] = pal[v][1];
|
|
|
2964 |
out[z++] = pal[v][2];
|
|
|
2965 |
if (target == 4) out[z++] = 255;
|
|
|
2966 |
if (i+1 == (int) s->img_x) break;
|
|
|
2967 |
v = (bpp == 8) ? get8(s) : v2;
|
|
|
2968 |
out[z++] = pal[v][0];
|
|
|
2969 |
out[z++] = pal[v][1];
|
|
|
2970 |
out[z++] = pal[v][2];
|
|
|
2971 |
if (target == 4) out[z++] = 255;
|
|
|
2972 |
}
|
|
|
2973 |
skip(s, pad);
|
|
|
2974 |
}
|
|
|
2975 |
} else {
|
|
|
2976 |
int rshift=0,gshift=0,bshift=0,ashift=0,rcount=0,gcount=0,bcount=0,acount=0;
|
|
|
2977 |
int z = 0;
|
|
|
2978 |
int easy=0;
|
|
|
2979 |
skip(s, offset - 14 - hsz);
|
|
|
2980 |
if (bpp == 24) width = 3 * s->img_x;
|
|
|
2981 |
else if (bpp == 16) width = 2*s->img_x;
|
|
|
2982 |
else /* bpp = 32 and pad = 0 */ width=0;
|
|
|
2983 |
pad = (-width) & 3;
|
|
|
2984 |
if (bpp == 24) {
|
|
|
2985 |
easy = 1;
|
|
|
2986 |
} else if (bpp == 32) {
|
|
|
2987 |
if (mb == 0xff && mg == 0xff00 && mr == 0xff000000 && ma == 0xff000000)
|
|
|
2988 |
easy = 2;
|
|
|
2989 |
}
|
|
|
2990 |
if (!easy) {
|
|
|
2991 |
if (!mr || !mg || !mb) return epuc("bad masks", "Corrupt BMP");
|
|
|
2992 |
// right shift amt to put high bit in position #7
|
|
|
2993 |
rshift = high_bit(mr)-7; rcount = bitcount(mr);
|
|
|
2994 |
gshift = high_bit(mg)-7; gcount = bitcount(mr);
|
|
|
2995 |
bshift = high_bit(mb)-7; bcount = bitcount(mr);
|
|
|
2996 |
ashift = high_bit(ma)-7; acount = bitcount(mr);
|
|
|
2997 |
}
|
|
|
2998 |
for (j=0; j < (int) s->img_y; ++j) {
|
|
|
2999 |
if (easy) {
|
|
|
3000 |
for (i=0; i < (int) s->img_x; ++i) {
|
|
|
3001 |
int a;
|
|
|
3002 |
out[z+2] = get8u(s);
|
|
|
3003 |
out[z+1] = get8u(s);
|
|
|
3004 |
out[z+0] = get8u(s);
|
|
|
3005 |
z += 3;
|
|
|
3006 |
a = (easy == 2 ? get8(s) : 255);
|
|
|
3007 |
if (target == 4) out[z++] = (uint8) a;
|
|
|
3008 |
}
|
|
|
3009 |
} else {
|
|
|
3010 |
for (i=0; i < (int) s->img_x; ++i) {
|
|
|
3011 |
uint32 v = (bpp == 16 ? get16le(s) : get32le(s));
|
|
|
3012 |
int a;
|
|
|
3013 |
out[z++] = (uint8) shiftsigned(v & mr, rshift, rcount);
|
|
|
3014 |
out[z++] = (uint8) shiftsigned(v & mg, gshift, gcount);
|
|
|
3015 |
out[z++] = (uint8) shiftsigned(v & mb, bshift, bcount);
|
|
|
3016 |
a = (ma ? shiftsigned(v & ma, ashift, acount) : 255);
|
|
|
3017 |
if (target == 4) out[z++] = (uint8) a;
|
|
|
3018 |
}
|
|
|
3019 |
}
|
|
|
3020 |
skip(s, pad);
|
|
|
3021 |
}
|
|
|
3022 |
}
|
|
|
3023 |
if (flip_vertically) {
|
|
|
3024 |
stbi_uc t;
|
|
|
3025 |
for (j=0; j < (int) s->img_y>>1; ++j) {
|
|
|
3026 |
stbi_uc *p1 = out + j *s->img_x*target;
|
|
|
3027 |
stbi_uc *p2 = out + (s->img_y-1-j)*s->img_x*target;
|
|
|
3028 |
for (i=0; i < (int) s->img_x*target; ++i) {
|
|
|
3029 |
t = p1[i], p1[i] = p2[i], p2[i] = t;
|
|
|
3030 |
}
|
|
|
3031 |
}
|
|
|
3032 |
}
|
|
|
3033 |
|
|
|
3034 |
if (req_comp && req_comp != target) {
|
|
|
3035 |
out = convert_format(out, target, req_comp, s->img_x, s->img_y);
|
|
|
3036 |
if (out == NULL) return out; // convert_format frees input on failure
|
|
|
3037 |
}
|
|
|
3038 |
|
|
|
3039 |
*x = s->img_x;
|
|
|
3040 |
*y = s->img_y;
|
|
|
3041 |
if (comp) *comp = target;
|
|
|
3042 |
return out;
|
|
|
3043 |
}
|
|
|
3044 |
|
|
|
3045 |
#ifndef STBI_NO_STDIO
|
|
|
3046 |
stbi_uc *stbi_bmp_load (char const *filename, int *x, int *y, int *comp, int req_comp)
|
|
|
3047 |
{
|
|
|
3048 |
stbi_uc *data;
|
|
|
3049 |
FILE *f = fopen(filename, "rb");
|
|
|
3050 |
if (!f) return NULL;
|
|
|
3051 |
data = stbi_bmp_load_from_file(f, x,y,comp,req_comp);
|
|
|
3052 |
fclose(f);
|
|
|
3053 |
return data;
|
|
|
3054 |
}
|
|
|
3055 |
|
|
|
3056 |
stbi_uc *stbi_bmp_load_from_file (FILE *f, int *x, int *y, int *comp, int req_comp)
|
|
|
3057 |
{
|
|
|
3058 |
stbi s;
|
|
|
3059 |
start_file(&s, f);
|
|
|
3060 |
return bmp_load(&s, x,y,comp,req_comp);
|
|
|
3061 |
}
|
|
|
3062 |
#endif
|
|
|
3063 |
|
|
|
3064 |
stbi_uc *stbi_bmp_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp)
|
|
|
3065 |
{
|
|
|
3066 |
stbi s;
|
|
|
3067 |
start_mem(&s, buffer, len);
|
|
|
3068 |
return bmp_load(&s, x,y,comp,req_comp);
|
|
|
3069 |
}
|
|
|
3070 |
|
|
|
3071 |
// Targa Truevision - TGA
|
|
|
3072 |
// by Jonathan Dummer
|
|
|
3073 |
|
|
|
3074 |
static int tga_info(stbi *s, int *x, int *y, int *comp)
|
|
|
3075 |
{
|
|
|
3076 |
int tga_w, tga_h, tga_comp;
|
|
|
3077 |
int sz;
|
|
|
3078 |
get8u(s); // discard Offset
|
|
|
3079 |
sz = get8u(s); // color type
|
|
|
3080 |
if( sz > 1 ) return 0; // only RGB or indexed allowed
|
|
|
3081 |
sz = get8u(s); // image type
|
|
|
3082 |
// only RGB or grey allowed, +/- RLE
|
|
|
3083 |
if ((sz != 1) && (sz != 2) && (sz != 3) && (sz != 9) && (sz != 10) && (sz != 11)) return 0;
|
|
|
3084 |
get16le(s); // discard palette start
|
|
|
3085 |
get16le(s); // discard palette length
|
|
|
3086 |
get8(s); // discard bits per palette color entry
|
|
|
3087 |
get16le(s); // discard x origin
|
|
|
3088 |
get16le(s); // discard y origin
|
|
|
3089 |
tga_w = get16le(s);
|
|
|
3090 |
if( tga_w < 1 ) return 0; // test width
|
|
|
3091 |
tga_h = get16le(s);
|
|
|
3092 |
if( tga_h < 1 ) return 0; // test height
|
|
|
3093 |
sz = get8(s); // bits per pixel
|
|
|
3094 |
// only RGB or RGBA or grey allowed
|
|
|
3095 |
if ((sz != 8) && (sz != 16) && (sz != 24) && (sz != 32)) return 0;
|
|
|
3096 |
tga_comp = sz;
|
|
|
3097 |
if (x) *x = tga_w;
|
|
|
3098 |
if (y) *y = tga_h;
|
|
|
3099 |
if (comp) *comp = tga_comp / 8;
|
|
|
3100 |
return 1; // seems to have passed everything
|
|
|
3101 |
}
|
|
|
3102 |
|
|
|
3103 |
#ifndef STBI_NO_STDIO
|
|
|
3104 |
int stbi_tga_info_from_file(FILE *f, int *x, int *y, int *comp)
|
|
|
3105 |
{
|
|
|
3106 |
stbi s;
|
|
|
3107 |
int r;
|
|
|
3108 |
long n = ftell(f);
|
|
|
3109 |
start_file(&s, f);
|
|
|
3110 |
r = tga_info(&s, x, y, comp);
|
|
|
3111 |
fseek(f, n, SEEK_SET);
|
|
|
3112 |
return r;
|
|
|
3113 |
}
|
|
|
3114 |
#endif
|
|
|
3115 |
|
|
|
3116 |
int stbi_tga_info_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp)
|
|
|
3117 |
{
|
|
|
3118 |
stbi s;
|
|
|
3119 |
start_mem(&s, buffer, len);
|
|
|
3120 |
return tga_info(&s, x, y, comp);
|
|
|
3121 |
}
|
|
|
3122 |
|
|
|
3123 |
static int tga_test(stbi *s)
|
|
|
3124 |
{
|
|
|
3125 |
int sz;
|
|
|
3126 |
get8u(s); // discard Offset
|
|
|
3127 |
sz = get8u(s); // color type
|
|
|
3128 |
if ( sz > 1 ) return 0; // only RGB or indexed allowed
|
|
|
3129 |
sz = get8u(s); // image type
|
|
|
3130 |
if ( (sz != 1) && (sz != 2) && (sz != 3) && (sz != 9) && (sz != 10) && (sz != 11) ) return 0; // only RGB or grey allowed, +/- RLE
|
|
|
3131 |
get16(s); // discard palette start
|
|
|
3132 |
get16(s); // discard palette length
|
|
|
3133 |
get8(s); // discard bits per palette color entry
|
|
|
3134 |
get16(s); // discard x origin
|
|
|
3135 |
get16(s); // discard y origin
|
|
|
3136 |
if ( get16(s) < 1 ) return 0; // test width
|
|
|
3137 |
if ( get16(s) < 1 ) return 0; // test height
|
|
|
3138 |
sz = get8(s); // bits per pixel
|
|
|
3139 |
if ( (sz != 8) && (sz != 16) && (sz != 24) && (sz != 32) ) return 0; // only RGB or RGBA or grey allowed
|
|
|
3140 |
return 1; // seems to have passed everything
|
|
|
3141 |
}
|
|
|
3142 |
|
|
|
3143 |
#ifndef STBI_NO_STDIO
|
|
|
3144 |
int stbi_tga_test_file (FILE *f)
|
|
|
3145 |
{
|
|
|
3146 |
stbi s;
|
|
|
3147 |
int r,n = ftell(f);
|
|
|
3148 |
start_file(&s, f);
|
|
|
3149 |
r = tga_test(&s);
|
|
|
3150 |
fseek(f,n,SEEK_SET);
|
|
|
3151 |
return r;
|
|
|
3152 |
}
|
|
|
3153 |
#endif
|
|
|
3154 |
|
|
|
3155 |
int stbi_tga_test_memory (stbi_uc const *buffer, int len)
|
|
|
3156 |
{
|
|
|
3157 |
stbi s;
|
|
|
3158 |
start_mem(&s, buffer, len);
|
|
|
3159 |
return tga_test(&s);
|
|
|
3160 |
}
|
|
|
3161 |
|
|
|
3162 |
static stbi_uc *tga_load(stbi *s, int *x, int *y, int *comp, int req_comp)
|
|
|
3163 |
{
|
|
|
3164 |
// read in the TGA header stuff
|
|
|
3165 |
int tga_offset = get8u(s);
|
|
|
3166 |
int tga_indexed = get8u(s);
|
|
|
3167 |
int tga_image_type = get8u(s);
|
|
|
3168 |
int tga_is_RLE = 0;
|
|
|
3169 |
int tga_palette_start = get16le(s);
|
|
|
3170 |
int tga_palette_len = get16le(s);
|
|
|
3171 |
int tga_palette_bits = get8u(s);
|
|
|
3172 |
int tga_x_origin = get16le(s);
|
|
|
3173 |
int tga_y_origin = get16le(s);
|
|
|
3174 |
int tga_width = get16le(s);
|
|
|
3175 |
int tga_height = get16le(s);
|
|
|
3176 |
int tga_bits_per_pixel = get8u(s);
|
|
|
3177 |
int tga_inverted = get8u(s);
|
|
|
3178 |
// image data
|
|
|
3179 |
unsigned char *tga_data;
|
|
|
3180 |
unsigned char *tga_palette = NULL;
|
|
|
3181 |
int i, j;
|
|
|
3182 |
unsigned char raw_data[4];
|
|
|
3183 |
unsigned char trans_data[4];
|
|
|
3184 |
int RLE_count = 0;
|
|
|
3185 |
int RLE_repeating = 0;
|
|
|
3186 |
int read_next_pixel = 1;
|
|
|
3187 |
|
|
|
3188 |
// do a tiny bit of precessing
|
|
|
3189 |
if ( tga_image_type >= 8 )
|
|
|
3190 |
{
|
|
|
3191 |
tga_image_type -= 8;
|
|
|
3192 |
tga_is_RLE = 1;
|
|
|
3193 |
}
|
|
|
3194 |
/* int tga_alpha_bits = tga_inverted & 15; */
|
|
|
3195 |
tga_inverted = 1 - ((tga_inverted >> 5) & 1);
|
|
|
3196 |
|
|
|
3197 |
// error check
|
|
|
3198 |
if ( //(tga_indexed) ||
|
|
|
3199 |
(tga_width < 1) || (tga_height < 1) ||
|
|
|
3200 |
(tga_image_type < 1) || (tga_image_type > 3) ||
|
|
|
3201 |
((tga_bits_per_pixel != 8) && (tga_bits_per_pixel != 16) &&
|
|
|
3202 |
(tga_bits_per_pixel != 24) && (tga_bits_per_pixel != 32))
|
|
|
3203 |
)
|
|
|
3204 |
{
|
|
|
3205 |
return NULL;
|
|
|
3206 |
}
|
|
|
3207 |
|
|
|
3208 |
// If I'm paletted, then I'll use the number of bits from the palette
|
|
|
3209 |
if ( tga_indexed )
|
|
|
3210 |
{
|
|
|
3211 |
tga_bits_per_pixel = tga_palette_bits;
|
|
|
3212 |
}
|
|
|
3213 |
|
|
|
3214 |
// tga info
|
|
|
3215 |
*x = tga_width;
|
|
|
3216 |
*y = tga_height;
|
|
|
3217 |
if ( (req_comp < 1) || (req_comp > 4) )
|
|
|
3218 |
{
|
|
|
3219 |
// just use whatever the file was
|
|
|
3220 |
req_comp = tga_bits_per_pixel / 8;
|
|
|
3221 |
*comp = req_comp;
|
|
|
3222 |
} else
|
|
|
3223 |
{
|
|
|
3224 |
// force a new number of components
|
|
|
3225 |
*comp = tga_bits_per_pixel/8;
|
|
|
3226 |
}
|
|
|
3227 |
tga_data = (unsigned char*)malloc( tga_width * tga_height * req_comp );
|
|
|
3228 |
|
|
|
3229 |
// skip to the data's starting position (offset usually = 0)
|
|
|
3230 |
skip(s, tga_offset );
|
|
|
3231 |
// do I need to load a palette?
|
|
|
3232 |
if ( tga_indexed )
|
|
|
3233 |
{
|
|
|
3234 |
// any data to skip? (offset usually = 0)
|
|
|
3235 |
skip(s, tga_palette_start );
|
|
|
3236 |
// load the palette
|
|
|
3237 |
tga_palette = (unsigned char*)malloc( tga_palette_len * tga_palette_bits / 8 );
|
|
|
3238 |
if (!getn(s, tga_palette, tga_palette_len * tga_palette_bits / 8 ))
|
|
|
3239 |
return NULL;
|
|
|
3240 |
}
|
|
|
3241 |
// load the data
|
|
|
3242 |
trans_data[0] = trans_data[1] = trans_data[2] = trans_data[3] = 0;
|
|
|
3243 |
for (i=0; i < tga_width * tga_height; ++i)
|
|
|
3244 |
{
|
|
|
3245 |
// if I'm in RLE mode, do I need to get a RLE chunk?
|
|
|
3246 |
if ( tga_is_RLE )
|
|
|
3247 |
{
|
|
|
3248 |
if ( RLE_count == 0 )
|
|
|
3249 |
{
|
|
|
3250 |
// yep, get the next byte as a RLE command
|
|
|
3251 |
int RLE_cmd = get8u(s);
|
|
|
3252 |
RLE_count = 1 + (RLE_cmd & 127);
|
|
|
3253 |
RLE_repeating = RLE_cmd >> 7;
|
|
|
3254 |
read_next_pixel = 1;
|
|
|
3255 |
} else if ( !RLE_repeating )
|
|
|
3256 |
{
|
|
|
3257 |
read_next_pixel = 1;
|
|
|
3258 |
}
|
|
|
3259 |
} else
|
|
|
3260 |
{
|
|
|
3261 |
read_next_pixel = 1;
|
|
|
3262 |
}
|
|
|
3263 |
// OK, if I need to read a pixel, do it now
|
|
|
3264 |
if ( read_next_pixel )
|
|
|
3265 |
{
|
|
|
3266 |
// load however much data we did have
|
|
|
3267 |
if ( tga_indexed )
|
|
|
3268 |
{
|
|
|
3269 |
// read in 1 byte, then perform the lookup
|
|
|
3270 |
int pal_idx = get8u(s);
|
|
|
3271 |
if ( pal_idx >= tga_palette_len )
|
|
|
3272 |
{
|
|
|
3273 |
// invalid index
|
|
|
3274 |
pal_idx = 0;
|
|
|
3275 |
}
|
|
|
3276 |
pal_idx *= tga_bits_per_pixel / 8;
|
|
|
3277 |
for (j = 0; j*8 < tga_bits_per_pixel; ++j)
|
|
|
3278 |
{
|
|
|
3279 |
raw_data[j] = tga_palette[pal_idx+j];
|
|
|
3280 |
}
|
|
|
3281 |
} else
|
|
|
3282 |
{
|
|
|
3283 |
// read in the data raw
|
|
|
3284 |
for (j = 0; j*8 < tga_bits_per_pixel; ++j)
|
|
|
3285 |
{
|
|
|
3286 |
raw_data[j] = get8u(s);
|
|
|
3287 |
}
|
|
|
3288 |
}
|
|
|
3289 |
// convert raw to the intermediate format
|
|
|
3290 |
switch (tga_bits_per_pixel)
|
|
|
3291 |
{
|
|
|
3292 |
case 8:
|
|
|
3293 |
// Luminous => RGBA
|
|
|
3294 |
trans_data[0] = raw_data[0];
|
|
|
3295 |
trans_data[1] = raw_data[0];
|
|
|
3296 |
trans_data[2] = raw_data[0];
|
|
|
3297 |
trans_data[3] = 255;
|
|
|
3298 |
break;
|
|
|
3299 |
case 16:
|
|
|
3300 |
// Luminous,Alpha => RGBA
|
|
|
3301 |
trans_data[0] = raw_data[0];
|
|
|
3302 |
trans_data[1] = raw_data[0];
|
|
|
3303 |
trans_data[2] = raw_data[0];
|
|
|
3304 |
trans_data[3] = raw_data[1];
|
|
|
3305 |
break;
|
|
|
3306 |
case 24:
|
|
|
3307 |
// BGR => RGBA
|
|
|
3308 |
trans_data[0] = raw_data[2];
|
|
|
3309 |
trans_data[1] = raw_data[1];
|
|
|
3310 |
trans_data[2] = raw_data[0];
|
|
|
3311 |
trans_data[3] = 255;
|
|
|
3312 |
break;
|
|
|
3313 |
case 32:
|
|
|
3314 |
// BGRA => RGBA
|
|
|
3315 |
trans_data[0] = raw_data[2];
|
|
|
3316 |
trans_data[1] = raw_data[1];
|
|
|
3317 |
trans_data[2] = raw_data[0];
|
|
|
3318 |
trans_data[3] = raw_data[3];
|
|
|
3319 |
break;
|
|
|
3320 |
}
|
|
|
3321 |
// clear the reading flag for the next pixel
|
|
|
3322 |
read_next_pixel = 0;
|
|
|
3323 |
} // end of reading a pixel
|
|
|
3324 |
// convert to final format
|
|
|
3325 |
switch (req_comp)
|
|
|
3326 |
{
|
|
|
3327 |
case 1:
|
|
|
3328 |
// RGBA => Luminance
|
|
|
3329 |
tga_data[i*req_comp+0] = compute_y(trans_data[0],trans_data[1],trans_data[2]);
|
|
|
3330 |
break;
|
|
|
3331 |
case 2:
|
|
|
3332 |
// RGBA => Luminance,Alpha
|
|
|
3333 |
tga_data[i*req_comp+0] = compute_y(trans_data[0],trans_data[1],trans_data[2]);
|
|
|
3334 |
tga_data[i*req_comp+1] = trans_data[3];
|
|
|
3335 |
break;
|
|
|
3336 |
case 3:
|
|
|
3337 |
// RGBA => RGB
|
|
|
3338 |
tga_data[i*req_comp+0] = trans_data[0];
|
|
|
3339 |
tga_data[i*req_comp+1] = trans_data[1];
|
|
|
3340 |
tga_data[i*req_comp+2] = trans_data[2];
|
|
|
3341 |
break;
|
|
|
3342 |
case 4:
|
|
|
3343 |
// RGBA => RGBA
|
|
|
3344 |
tga_data[i*req_comp+0] = trans_data[0];
|
|
|
3345 |
tga_data[i*req_comp+1] = trans_data[1];
|
|
|
3346 |
tga_data[i*req_comp+2] = trans_data[2];
|
|
|
3347 |
tga_data[i*req_comp+3] = trans_data[3];
|
|
|
3348 |
break;
|
|
|
3349 |
}
|
|
|
3350 |
// in case we're in RLE mode, keep counting down
|
|
|
3351 |
--RLE_count;
|
|
|
3352 |
}
|
|
|
3353 |
// do I need to invert the image?
|
|
|
3354 |
if ( tga_inverted )
|
|
|
3355 |
{
|
|
|
3356 |
for (j = 0; j*2 < tga_height; ++j)
|
|
|
3357 |
{
|
|
|
3358 |
int index1 = j * tga_width * req_comp;
|
|
|
3359 |
int index2 = (tga_height - 1 - j) * tga_width * req_comp;
|
|
|
3360 |
for (i = tga_width * req_comp; i > 0; --i)
|
|
|
3361 |
{
|
|
|
3362 |
unsigned char temp = tga_data[index1];
|
|
|
3363 |
tga_data[index1] = tga_data[index2];
|
|
|
3364 |
tga_data[index2] = temp;
|
|
|
3365 |
++index1;
|
|
|
3366 |
++index2;
|
|
|
3367 |
}
|
|
|
3368 |
}
|
|
|
3369 |
}
|
|
|
3370 |
// clear my palette, if I had one
|
|
|
3371 |
if ( tga_palette != NULL )
|
|
|
3372 |
{
|
|
|
3373 |
free( tga_palette );
|
|
|
3374 |
}
|
|
|
3375 |
// the things I do to get rid of an error message, and yet keep
|
|
|
3376 |
// Microsoft's C compilers happy... [8^(
|
|
|
3377 |
tga_palette_start = tga_palette_len = tga_palette_bits =
|
|
|
3378 |
tga_x_origin = tga_y_origin = 0;
|
|
|
3379 |
// OK, done
|
|
|
3380 |
return tga_data;
|
|
|
3381 |
}
|
|
|
3382 |
|
|
|
3383 |
#ifndef STBI_NO_STDIO
|
|
|
3384 |
stbi_uc *stbi_tga_load (char const *filename, int *x, int *y, int *comp, int req_comp)
|
|
|
3385 |
{
|
|
|
3386 |
stbi_uc *data;
|
|
|
3387 |
FILE *f = fopen(filename, "rb");
|
|
|
3388 |
if (!f) return NULL;
|
|
|
3389 |
data = stbi_tga_load_from_file(f, x,y,comp,req_comp);
|
|
|
3390 |
fclose(f);
|
|
|
3391 |
return data;
|
|
|
3392 |
}
|
|
|
3393 |
|
|
|
3394 |
stbi_uc *stbi_tga_load_from_file (FILE *f, int *x, int *y, int *comp, int req_comp)
|
|
|
3395 |
{
|
|
|
3396 |
stbi s;
|
|
|
3397 |
start_file(&s, f);
|
|
|
3398 |
return tga_load(&s, x,y,comp,req_comp);
|
|
|
3399 |
}
|
|
|
3400 |
#endif
|
|
|
3401 |
|
|
|
3402 |
stbi_uc *stbi_tga_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp)
|
|
|
3403 |
{
|
|
|
3404 |
stbi s;
|
|
|
3405 |
start_mem(&s, buffer, len);
|
|
|
3406 |
return tga_load(&s, x,y,comp,req_comp);
|
|
|
3407 |
}
|
|
|
3408 |
|
|
|
3409 |
|
|
|
3410 |
// *************************************************************************************************
|
|
|
3411 |
// Photoshop PSD loader -- PD by Thatcher Ulrich, integration by Nicolas Schulz, tweaked by STB
|
|
|
3412 |
|
|
|
3413 |
static int psd_test(stbi *s)
|
|
|
3414 |
{
|
|
|
3415 |
if (get32(s) != 0x38425053) return 0; // "8BPS"
|
|
|
3416 |
else return 1;
|
|
|
3417 |
}
|
|
|
3418 |
|
|
|
3419 |
#ifndef STBI_NO_STDIO
|
|
|
3420 |
int stbi_psd_test_file(FILE *f)
|
|
|
3421 |
{
|
|
|
3422 |
stbi s;
|
|
|
3423 |
int r,n = ftell(f);
|
|
|
3424 |
start_file(&s, f);
|
|
|
3425 |
r = psd_test(&s);
|
|
|
3426 |
fseek(f,n,SEEK_SET);
|
|
|
3427 |
return r;
|
|
|
3428 |
}
|
|
|
3429 |
#endif
|
|
|
3430 |
|
|
|
3431 |
int stbi_psd_test_memory(stbi_uc const *buffer, int len)
|
|
|
3432 |
{
|
|
|
3433 |
stbi s;
|
|
|
3434 |
start_mem(&s, buffer, len);
|
|
|
3435 |
return psd_test(&s);
|
|
|
3436 |
}
|
|
|
3437 |
|
|
|
3438 |
static stbi_uc *psd_load(stbi *s, int *x, int *y, int *comp, int req_comp)
|
|
|
3439 |
{
|
|
|
3440 |
int pixelCount;
|
|
|
3441 |
int channelCount, compression;
|
|
|
3442 |
int channel, i, count, len;
|
|
|
3443 |
int w,h;
|
|
|
3444 |
uint8 *out;
|
|
|
3445 |
|
|
|
3446 |
// Check identifier
|
|
|
3447 |
if (get32(s) != 0x38425053) // "8BPS"
|
|
|
3448 |
return epuc("not PSD", "Corrupt PSD image");
|
|
|
3449 |
|
|
|
3450 |
// Check file type version.
|
|
|
3451 |
if (get16(s) != 1)
|
|
|
3452 |
return epuc("wrong version", "Unsupported version of PSD image");
|
|
|
3453 |
|
|
|
3454 |
// Skip 6 reserved bytes.
|
|
|
3455 |
skip(s, 6 );
|
|
|
3456 |
|
|
|
3457 |
// Read the number of channels (R, G, B, A, etc).
|
|
|
3458 |
channelCount = get16(s);
|
|
|
3459 |
if (channelCount < 0 || channelCount > 16)
|
|
|
3460 |
return epuc("wrong channel count", "Unsupported number of channels in PSD image");
|
|
|
3461 |
|
|
|
3462 |
// Read the rows and columns of the image.
|
|
|
3463 |
h = get32(s);
|
|
|
3464 |
w = get32(s);
|
|
|
3465 |
|
|
|
3466 |
// Make sure the depth is 8 bits.
|
|
|
3467 |
if (get16(s) != 8)
|
|
|
3468 |
return epuc("unsupported bit depth", "PSD bit depth is not 8 bit");
|
|
|
3469 |
|
|
|
3470 |
// Make sure the color mode is RGB.
|
|
|
3471 |
// Valid options are:
|
|
|
3472 |
// 0: Bitmap
|
|
|
3473 |
// 1: Grayscale
|
|
|
3474 |
// 2: Indexed color
|
|
|
3475 |
// 3: RGB color
|
|
|
3476 |
// 4: CMYK color
|
|
|
3477 |
// 7: Multichannel
|
|
|
3478 |
// 8: Duotone
|
|
|
3479 |
// 9: Lab color
|
|
|
3480 |
if (get16(s) != 3)
|
|
|
3481 |
return epuc("wrong color format", "PSD is not in RGB color format");
|
|
|
3482 |
|
|
|
3483 |
// Skip the Mode Data. (It's the palette for indexed color; other info for other modes.)
|
|
|
3484 |
skip(s,get32(s) );
|
|
|
3485 |
|
|
|
3486 |
// Skip the image resources. (resolution, pen tool paths, etc)
|
|
|
3487 |
skip(s, get32(s) );
|
|
|
3488 |
|
|
|
3489 |
// Skip the reserved data.
|
|
|
3490 |
skip(s, get32(s) );
|
|
|
3491 |
|
|
|
3492 |
// Find out if the data is compressed.
|
|
|
3493 |
// Known values:
|
|
|
3494 |
// 0: no compression
|
|
|
3495 |
// 1: RLE compressed
|
|
|
3496 |
compression = get16(s);
|
|
|
3497 |
if (compression > 1)
|
|
|
3498 |
return epuc("bad compression", "PSD has an unknown compression format");
|
|
|
3499 |
|
|
|
3500 |
// Create the destination image.
|
|
|
3501 |
out = (stbi_uc *) malloc(4 * w*h);
|
|
|
3502 |
if (!out) return epuc("outofmem", "Out of memory");
|
|
|
3503 |
pixelCount = w*h;
|
|
|
3504 |
|
|
|
3505 |
// Initialize the data to zero.
|
|
|
3506 |
//memset( out, 0, pixelCount * 4 );
|
|
|
3507 |
|
|
|
3508 |
// Finally, the image data.
|
|
|
3509 |
if (compression) {
|
|
|
3510 |
// RLE as used by .PSD and .TIFF
|
|
|
3511 |
// Loop until you get the number of unpacked bytes you are expecting:
|
|
|
3512 |
// Read the next source byte into n.
|
|
|
3513 |
// If n is between 0 and 127 inclusive, copy the next n+1 bytes literally.
|
|
|
3514 |
// Else if n is between -127 and -1 inclusive, copy the next byte -n+1 times.
|
|
|
3515 |
// Else if n is 128, noop.
|
|
|
3516 |
// Endloop
|
|
|
3517 |
|
|
|
3518 |
// The RLE-compressed data is preceeded by a 2-byte data count for each row in the data,
|
|
|
3519 |
// which we're going to just skip.
|
|
|
3520 |
skip(s, h * channelCount * 2 );
|
|
|
3521 |
|
|
|
3522 |
// Read the RLE data by channel.
|
|
|
3523 |
for (channel = 0; channel < 4; channel++) {
|
|
|
3524 |
uint8 *p;
|
|
|
3525 |
|
|
|
3526 |
p = out+channel;
|
|
|
3527 |
if (channel >= channelCount) {
|
|
|
3528 |
// Fill this channel with default data.
|
|
|
3529 |
for (i = 0; i < pixelCount; i++) *p = (channel == 3 ? 255 : 0), p += 4;
|
|
|
3530 |
} else {
|
|
|
3531 |
// Read the RLE data.
|
|
|
3532 |
count = 0;
|
|
|
3533 |
while (count < pixelCount) {
|
|
|
3534 |
len = get8(s);
|
|
|
3535 |
if (len == 128) {
|
|
|
3536 |
// No-op.
|
|
|
3537 |
} else if (len < 128) {
|
|
|
3538 |
// Copy next len+1 bytes literally.
|
|
|
3539 |
len++;
|
|
|
3540 |
count += len;
|
|
|
3541 |
while (len) {
|
|
|
3542 |
*p = get8u(s);
|
|
|
3543 |
p += 4;
|
|
|
3544 |
len--;
|
|
|
3545 |
}
|
|
|
3546 |
} else if (len > 128) {
|
|
|
3547 |
uint8 val;
|
|
|
3548 |
// Next -len+1 bytes in the dest are replicated from next source byte.
|
|
|
3549 |
// (Interpret len as a negative 8-bit int.)
|
|
|
3550 |
len ^= 0x0FF;
|
|
|
3551 |
len += 2;
|
|
|
3552 |
val = get8u(s);
|
|
|
3553 |
count += len;
|
|
|
3554 |
while (len) {
|
|
|
3555 |
*p = val;
|
|
|
3556 |
p += 4;
|
|
|
3557 |
len--;
|
|
|
3558 |
}
|
|
|
3559 |
}
|
|
|
3560 |
}
|
|
|
3561 |
}
|
|
|
3562 |
}
|
|
|
3563 |
|
|
|
3564 |
} else {
|
|
|
3565 |
// We're at the raw image data. It's each channel in order (Red, Green, Blue, Alpha, ...)
|
|
|
3566 |
// where each channel consists of an 8-bit value for each pixel in the image.
|
|
|
3567 |
|
|
|
3568 |
// Read the data by channel.
|
|
|
3569 |
for (channel = 0; channel < 4; channel++) {
|
|
|
3570 |
uint8 *p;
|
|
|
3571 |
|
|
|
3572 |
p = out + channel;
|
|
|
3573 |
if (channel > channelCount) {
|
|
|
3574 |
// Fill this channel with default data.
|
|
|
3575 |
for (i = 0; i < pixelCount; i++) *p = channel == 3 ? 255 : 0, p += 4;
|
|
|
3576 |
} else {
|
|
|
3577 |
// Read the data.
|
|
|
3578 |
for (i = 0; i < pixelCount; i++)
|
|
|
3579 |
*p = get8u(s), p += 4;
|
|
|
3580 |
}
|
|
|
3581 |
}
|
|
|
3582 |
}
|
|
|
3583 |
|
|
|
3584 |
if (req_comp && req_comp != 4) {
|
|
|
3585 |
out = convert_format(out, 4, req_comp, w, h);
|
|
|
3586 |
if (out == NULL) return out; // convert_format frees input on failure
|
|
|
3587 |
}
|
|
|
3588 |
|
|
|
3589 |
if (comp) *comp = channelCount;
|
|
|
3590 |
*y = h;
|
|
|
3591 |
*x = w;
|
|
|
3592 |
|
|
|
3593 |
return out;
|
|
|
3594 |
}
|
|
|
3595 |
|
|
|
3596 |
#ifndef STBI_NO_STDIO
|
|
|
3597 |
stbi_uc *stbi_psd_load(char const *filename, int *x, int *y, int *comp, int req_comp)
|
|
|
3598 |
{
|
|
|
3599 |
stbi_uc *data;
|
|
|
3600 |
FILE *f = fopen(filename, "rb");
|
|
|
3601 |
if (!f) return NULL;
|
|
|
3602 |
data = stbi_psd_load_from_file(f, x,y,comp,req_comp);
|
|
|
3603 |
fclose(f);
|
|
|
3604 |
return data;
|
|
|
3605 |
}
|
|
|
3606 |
|
|
|
3607 |
stbi_uc *stbi_psd_load_from_file(FILE *f, int *x, int *y, int *comp, int req_comp)
|
|
|
3608 |
{
|
|
|
3609 |
stbi s;
|
|
|
3610 |
start_file(&s, f);
|
|
|
3611 |
return psd_load(&s, x,y,comp,req_comp);
|
|
|
3612 |
}
|
|
|
3613 |
#endif
|
|
|
3614 |
|
|
|
3615 |
stbi_uc *stbi_psd_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp)
|
|
|
3616 |
{
|
|
|
3617 |
stbi s;
|
|
|
3618 |
start_mem(&s, buffer, len);
|
|
|
3619 |
return psd_load(&s, x,y,comp,req_comp);
|
|
|
3620 |
}
|
|
|
3621 |
|
|
|
3622 |
// *************************************************************************************************
|
|
|
3623 |
// Softimage PIC loader
|
|
|
3624 |
// by Tom Seddon
|
|
|
3625 |
//
|
|
|
3626 |
// See http://softimage.wiki.softimage.com/index.php/INFO:_PIC_file_format
|
|
|
3627 |
// See http://ozviz.wasp.uwa.edu.au/~pbourke/dataformats/softimagepic/
|
|
|
3628 |
|
|
|
3629 |
static int pic_is4(stbi *s,const char *str)
|
|
|
3630 |
{
|
|
|
3631 |
int i;
|
|
|
3632 |
for (i=0; i<4; ++i)
|
|
|
3633 |
if (get8(s) != (stbi_uc)str[i])
|
|
|
3634 |
return 0;
|
|
|
3635 |
|
|
|
3636 |
return 1;
|
|
|
3637 |
}
|
|
|
3638 |
|
|
|
3639 |
static int pic_test(stbi *s)
|
|
|
3640 |
{
|
|
|
3641 |
int i;
|
|
|
3642 |
|
|
|
3643 |
if (!pic_is4(s,"\x53\x80\xF6\x34"))
|
|
|
3644 |
return 0;
|
|
|
3645 |
|
|
|
3646 |
for(i=0;i<84;++i)
|
|
|
3647 |
get8(s);
|
|
|
3648 |
|
|
|
3649 |
if (!pic_is4(s,"PICT"))
|
|
|
3650 |
return 0;
|
|
|
3651 |
|
|
|
3652 |
return 1;
|
|
|
3653 |
}
|
|
|
3654 |
|
|
|
3655 |
typedef struct
|
|
|
3656 |
{
|
|
|
3657 |
stbi_uc size,type,channel;
|
|
|
3658 |
} pic_packet_t;
|
|
|
3659 |
|
|
|
3660 |
static stbi_uc *pic_readval(stbi *s, int channel, stbi_uc *dest)
|
|
|
3661 |
{
|
|
|
3662 |
int mask=0x80, i;
|
|
|
3663 |
|
|
|
3664 |
for (i=0; i<4; ++i, mask>>=1) {
|
|
|
3665 |
if (channel & mask) {
|
|
|
3666 |
if (at_eof(s)) return epuc("bad file","PIC file too short");
|
|
|
3667 |
dest[i]=get8u(s);
|
|
|
3668 |
}
|
|
|
3669 |
}
|
|
|
3670 |
|
|
|
3671 |
return dest;
|
|
|
3672 |
}
|
|
|
3673 |
|
|
|
3674 |
static void pic_copyval(int channel,stbi_uc *dest,const stbi_uc *src)
|
|
|
3675 |
{
|
|
|
3676 |
int mask=0x80,i;
|
|
|
3677 |
|
|
|
3678 |
for (i=0;i<4; ++i, mask>>=1)
|
|
|
3679 |
if (channel&mask)
|
|
|
3680 |
dest[i]=src[i];
|
|
|
3681 |
}
|
|
|
3682 |
|
|
|
3683 |
static stbi_uc *pic_load2(stbi *s,int width,int height,int *comp, stbi_uc *result)
|
|
|
3684 |
{
|
|
|
3685 |
int act_comp=0,num_packets=0,y,chained;
|
|
|
3686 |
pic_packet_t packets[10];
|
|
|
3687 |
|
|
|
3688 |
// this will (should...) cater for even some bizarre stuff like having data
|
|
|
3689 |
// for the same channel in multiple packets.
|
|
|
3690 |
do {
|
|
|
3691 |
pic_packet_t *packet;
|
|
|
3692 |
|
|
|
3693 |
if (num_packets==sizeof(packets)/sizeof(packets[0]))
|
|
|
3694 |
return epuc("bad format","too many packets");
|
|
|
3695 |
|
|
|
3696 |
packet = &packets[num_packets++];
|
|
|
3697 |
|
|
|
3698 |
chained = get8(s);
|
|
|
3699 |
packet->size = get8u(s);
|
|
|
3700 |
packet->type = get8u(s);
|
|
|
3701 |
packet->channel = get8u(s);
|
|
|
3702 |
|
|
|
3703 |
act_comp |= packet->channel;
|
|
|
3704 |
|
|
|
3705 |
if (at_eof(s)) return epuc("bad file","file too short (reading packets)");
|
|
|
3706 |
if (packet->size != 8) return epuc("bad format","packet isn't 8bpp");
|
|
|
3707 |
} while (chained);
|
|
|
3708 |
|
|
|
3709 |
*comp = (act_comp & 0x10 ? 4 : 3); // has alpha channel?
|
|
|
3710 |
|
|
|
3711 |
for(y=0; y<height; ++y) {
|
|
|
3712 |
int packet_idx;
|
|
|
3713 |
|
|
|
3714 |
for(packet_idx=0; packet_idx < num_packets; ++packet_idx) {
|
|
|
3715 |
pic_packet_t *packet = &packets[packet_idx];
|
|
|
3716 |
stbi_uc *dest = result+y*width*4;
|
|
|
3717 |
|
|
|
3718 |
switch (packet->type) {
|
|
|
3719 |
default:
|
|
|
3720 |
return epuc("bad format","packet has bad compression type");
|
|
|
3721 |
|
|
|
3722 |
case 0: {//uncompressed
|
|
|
3723 |
int x;
|
|
|
3724 |
|
|
|
3725 |
for(x=0;x<width;++x, dest+=4)
|
|
|
3726 |
if (!pic_readval(s,packet->channel,dest))
|
|
|
3727 |
return 0;
|
|
|
3728 |
break;
|
|
|
3729 |
}
|
|
|
3730 |
|
|
|
3731 |
case 1://Pure RLE
|
|
|
3732 |
{
|
|
|
3733 |
int left=width, i;
|
|
|
3734 |
|
|
|
3735 |
while (left>0) {
|
|
|
3736 |
stbi_uc count,value[4];
|
|
|
3737 |
|
|
|
3738 |
count=get8u(s);
|
|
|
3739 |
if (at_eof(s)) return epuc("bad file","file too short (pure read count)");
|
|
|
3740 |
|
|
|
3741 |
if (count > left)
|
|
|
3742 |
count = (uint8) left;
|
|
|
3743 |
|
|
|
3744 |
if (!pic_readval(s,packet->channel,value)) return 0;
|
|
|
3745 |
|
|
|
3746 |
for(i=0; i<count; ++i,dest+=4)
|
|
|
3747 |
pic_copyval(packet->channel,dest,value);
|
|
|
3748 |
left -= count;
|
|
|
3749 |
}
|
|
|
3750 |
}
|
|
|
3751 |
break;
|
|
|
3752 |
|
|
|
3753 |
case 2: {//Mixed RLE
|
|
|
3754 |
int left=width;
|
|
|
3755 |
while (left>0) {
|
|
|
3756 |
int count = get8(s), i;
|
|
|
3757 |
if (at_eof(s)) return epuc("bad file","file too short (mixed read count)");
|
|
|
3758 |
|
|
|
3759 |
if (count >= 128) { // Repeated
|
|
|
3760 |
stbi_uc value[4];
|
|
|
3761 |
int i;
|
|
|
3762 |
|
|
|
3763 |
if (count==128)
|
|
|
3764 |
count = get16(s);
|
|
|
3765 |
else
|
|
|
3766 |
count -= 127;
|
|
|
3767 |
if (count > left)
|
|
|
3768 |
return epuc("bad file","scanline overrun");
|
|
|
3769 |
|
|
|
3770 |
if (!pic_readval(s,packet->channel,value))
|
|
|
3771 |
return 0;
|
|
|
3772 |
|
|
|
3773 |
for(i=0;i<count;++i, dest += 4)
|
|
|
3774 |
pic_copyval(packet->channel,dest,value);
|
|
|
3775 |
} else { // Raw
|
|
|
3776 |
++count;
|
|
|
3777 |
if (count>left) return epuc("bad file","scanline overrun");
|
|
|
3778 |
|
|
|
3779 |
for(i=0;i<count;++i, dest+=4)
|
|
|
3780 |
if (!pic_readval(s,packet->channel,dest))
|
|
|
3781 |
return 0;
|
|
|
3782 |
}
|
|
|
3783 |
left-=count;
|
|
|
3784 |
}
|
|
|
3785 |
break;
|
|
|
3786 |
}
|
|
|
3787 |
}
|
|
|
3788 |
}
|
|
|
3789 |
}
|
|
|
3790 |
|
|
|
3791 |
return result;
|
|
|
3792 |
}
|
|
|
3793 |
|
|
|
3794 |
static stbi_uc *pic_load(stbi *s,int *px,int *py,int *comp,int req_comp)
|
|
|
3795 |
{
|
|
|
3796 |
stbi_uc *result;
|
|
|
3797 |
int i, x,y;
|
|
|
3798 |
|
|
|
3799 |
for (i=0; i<92; ++i)
|
|
|
3800 |
get8(s);
|
|
|
3801 |
|
|
|
3802 |
x = get16(s);
|
|
|
3803 |
y = get16(s);
|
|
|
3804 |
if (at_eof(s)) return epuc("bad file","file too short (pic header)");
|
|
|
3805 |
if ((1 << 28) / x < y) return epuc("too large", "Image too large to decode");
|
|
|
3806 |
|
|
|
3807 |
get32(s); //skip `ratio'
|
|
|
3808 |
get16(s); //skip `fields'
|
|
|
3809 |
get16(s); //skip `pad'
|
|
|
3810 |
|
|
|
3811 |
// intermediate buffer is RGBA
|
|
|
3812 |
result = (stbi_uc *) malloc(x*y*4);
|
|
|
3813 |
memset(result, 0xff, x*y*4);
|
|
|
3814 |
|
|
|
3815 |
if (!pic_load2(s,x,y,comp, result)) {
|
|
|
3816 |
free(result);
|
|
|
3817 |
result=0;
|
|
|
3818 |
}
|
|
|
3819 |
*px = x;
|
|
|
3820 |
*py = y;
|
|
|
3821 |
if (req_comp == 0) req_comp = *comp;
|
|
|
3822 |
result=convert_format(result,4,req_comp,x,y);
|
|
|
3823 |
|
|
|
3824 |
return result;
|
|
|
3825 |
}
|
|
|
3826 |
|
|
|
3827 |
int stbi_pic_test_memory(stbi_uc const *buffer, int len)
|
|
|
3828 |
{
|
|
|
3829 |
stbi s;
|
|
|
3830 |
start_mem(&s,buffer,len);
|
|
|
3831 |
return pic_test(&s);
|
|
|
3832 |
}
|
|
|
3833 |
|
|
|
3834 |
stbi_uc *stbi_pic_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp)
|
|
|
3835 |
{
|
|
|
3836 |
stbi s;
|
|
|
3837 |
start_mem(&s,buffer,len);
|
|
|
3838 |
return pic_load(&s,x,y,comp,req_comp);
|
|
|
3839 |
}
|
|
|
3840 |
|
|
|
3841 |
#ifndef STBI_NO_STDIO
|
|
|
3842 |
int stbi_pic_test_file(FILE *f)
|
|
|
3843 |
{
|
|
|
3844 |
int result;
|
|
|
3845 |
long l = ftell(f);
|
|
|
3846 |
stbi s;
|
|
|
3847 |
start_file(&s,f);
|
|
|
3848 |
result = pic_test(&s);
|
|
|
3849 |
fseek(f,l,SEEK_SET);
|
|
|
3850 |
return result;
|
|
|
3851 |
}
|
|
|
3852 |
|
|
|
3853 |
stbi_uc *stbi_pic_load(char const *filename,int *x, int *y, int *comp, int req_comp)
|
|
|
3854 |
{
|
|
|
3855 |
stbi_uc *result;
|
|
|
3856 |
FILE *f=fopen(filename,"rb");
|
|
|
3857 |
if (!f) return 0;
|
|
|
3858 |
result = stbi_pic_load_from_file(f,x,y,comp,req_comp);
|
|
|
3859 |
fclose(f);
|
|
|
3860 |
return result;
|
|
|
3861 |
}
|
|
|
3862 |
|
|
|
3863 |
stbi_uc *stbi_pic_load_from_file(FILE *f,int *x, int *y, int *comp, int req_comp)
|
|
|
3864 |
{
|
|
|
3865 |
stbi s;
|
|
|
3866 |
start_file(&s,f);
|
|
|
3867 |
return pic_load(&s,x,y,comp,req_comp);
|
|
|
3868 |
}
|
|
|
3869 |
#endif
|
|
|
3870 |
|
|
|
3871 |
// *************************************************************************************************
|
|
|
3872 |
// GIF loader -- public domain by Jean-Marc Lienher -- simplified/shrunk by stb
|
|
|
3873 |
typedef struct stbi_gif_lzw_struct {
|
|
|
3874 |
int16 prefix;
|
|
|
3875 |
uint8 first;
|
|
|
3876 |
uint8 suffix;
|
|
|
3877 |
} stbi_gif_lzw;
|
|
|
3878 |
|
|
|
3879 |
typedef struct stbi_gif_struct
|
|
|
3880 |
{
|
|
|
3881 |
int w,h;
|
|
|
3882 |
stbi_uc *out; // output buffer (always 4 components)
|
|
|
3883 |
int flags, bgindex, ratio, transparent, eflags;
|
|
|
3884 |
uint8 pal[256][4];
|
|
|
3885 |
uint8 lpal[256][4];
|
|
|
3886 |
stbi_gif_lzw codes[4096];
|
|
|
3887 |
uint8 *color_table;
|
|
|
3888 |
int parse, step;
|
|
|
3889 |
int lflags;
|
|
|
3890 |
int start_x, start_y;
|
|
|
3891 |
int max_x, max_y;
|
|
|
3892 |
int cur_x, cur_y;
|
|
|
3893 |
int line_size;
|
|
|
3894 |
} stbi_gif;
|
|
|
3895 |
|
|
|
3896 |
static int gif_test(stbi *s)
|
|
|
3897 |
{
|
|
|
3898 |
int sz;
|
|
|
3899 |
if (get8(s) != 'G' || get8(s) != 'I' || get8(s) != 'F' || get8(s) != '8') return 0;
|
|
|
3900 |
sz = get8(s);
|
|
|
3901 |
if (sz != '9' && sz != '7') return 0;
|
|
|
3902 |
if (get8(s) != 'a') return 0;
|
|
|
3903 |
return 1;
|
|
|
3904 |
}
|
|
|
3905 |
|
|
|
3906 |
#ifndef STBI_NO_STDIO
|
|
|
3907 |
int stbi_gif_test_file (FILE *f)
|
|
|
3908 |
{
|
|
|
3909 |
stbi s;
|
|
|
3910 |
int r,n = ftell(f);
|
|
|
3911 |
start_file(&s,f);
|
|
|
3912 |
r = gif_test(&s);
|
|
|
3913 |
fseek(f,n,SEEK_SET);
|
|
|
3914 |
return r;
|
|
|
3915 |
}
|
|
|
3916 |
#endif
|
|
|
3917 |
|
|
|
3918 |
int stbi_gif_test_memory (stbi_uc const *buffer, int len)
|
|
|
3919 |
{
|
|
|
3920 |
stbi s;
|
|
|
3921 |
start_mem(&s, buffer, len);
|
|
|
3922 |
return gif_test(&s);
|
|
|
3923 |
}
|
|
|
3924 |
|
|
|
3925 |
static void stbi_gif_parse_colortable(stbi *s, uint8 pal[256][4], int num_entries, int transp)
|
|
|
3926 |
{
|
|
|
3927 |
int i;
|
|
|
3928 |
for (i=0; i < num_entries; ++i) {
|
|
|
3929 |
pal[i][2] = get8u(s);
|
|
|
3930 |
pal[i][1] = get8u(s);
|
|
|
3931 |
pal[i][0] = get8u(s);
|
|
|
3932 |
pal[i][3] = transp ? 0 : 255;
|
|
|
3933 |
}
|
|
|
3934 |
}
|
|
|
3935 |
|
|
|
3936 |
static int stbi_gif_header(stbi *s, stbi_gif *g, int *comp, int is_info)
|
|
|
3937 |
{
|
|
|
3938 |
uint8 version;
|
|
|
3939 |
if (get8(s) != 'G' || get8(s) != 'I' || get8(s) != 'F' || get8(s) != '8')
|
|
|
3940 |
return e("not GIF", "Corrupt GIF");
|
|
|
3941 |
|
|
|
3942 |
version = get8u(s);
|
|
|
3943 |
if (version != '7' && version != '9') return e("not GIF", "Corrupt GIF");
|
|
|
3944 |
if (get8(s) != 'a') return e("not GIF", "Corrupt GIF");
|
|
|
3945 |
|
|
|
3946 |
failure_reason = "";
|
|
|
3947 |
g->w = get16le(s);
|
|
|
3948 |
g->h = get16le(s);
|
|
|
3949 |
g->flags = get8(s);
|
|
|
3950 |
g->bgindex = get8(s);
|
|
|
3951 |
g->ratio = get8(s);
|
|
|
3952 |
g->transparent = -1;
|
|
|
3953 |
|
|
|
3954 |
if (comp != 0) *comp = 4; // can't actually tell whether it's 3 or 4 until we parse the comments
|
|
|
3955 |
|
|
|
3956 |
if (is_info) return 1;
|
|
|
3957 |
|
|
|
3958 |
if (g->flags & 0x80)
|
|
|
3959 |
stbi_gif_parse_colortable(s,g->pal, 2 << (g->flags & 7), -1);
|
|
|
3960 |
|
|
|
3961 |
return 1;
|
|
|
3962 |
}
|
|
|
3963 |
|
|
|
3964 |
static int stbi_gif_info_raw(stbi *s, int *x, int *y, int *comp)
|
|
|
3965 |
{
|
|
|
3966 |
stbi_gif g;
|
|
|
3967 |
if (!stbi_gif_header(s, &g, comp, 1)) return 0;
|
|
|
3968 |
if (x) *x = g.w;
|
|
|
3969 |
if (y) *y = g.h;
|
|
|
3970 |
return 1;
|
|
|
3971 |
}
|
|
|
3972 |
|
|
|
3973 |
static void stbi_out_gif_code(stbi_gif *g, uint16 code)
|
|
|
3974 |
{
|
|
|
3975 |
uint8 *p, *c;
|
|
|
3976 |
|
|
|
3977 |
// recurse to decode the prefixes, since the linked-list is backwards,
|
|
|
3978 |
// and working backwards through an interleaved image would be nasty
|
|
|
3979 |
if (g->codes[code].prefix >= 0)
|
|
|
3980 |
stbi_out_gif_code(g, g->codes[code].prefix);
|
|
|
3981 |
|
|
|
3982 |
if (g->cur_y >= g->max_y) return;
|
|
|
3983 |
|
|
|
3984 |
p = &g->out[g->cur_x + g->cur_y];
|
|
|
3985 |
c = &g->color_table[g->codes[code].suffix * 4];
|
|
|
3986 |
|
|
|
3987 |
if (c[3] >= 128) {
|
|
|
3988 |
p[0] = c[2];
|
|
|
3989 |
p[1] = c[1];
|
|
|
3990 |
p[2] = c[0];
|
|
|
3991 |
p[3] = c[3];
|
|
|
3992 |
}
|
|
|
3993 |
g->cur_x += 4;
|
|
|
3994 |
|
|
|
3995 |
if (g->cur_x >= g->max_x) {
|
|
|
3996 |
g->cur_x = g->start_x;
|
|
|
3997 |
g->cur_y += g->step;
|
|
|
3998 |
|
|
|
3999 |
while (g->cur_y >= g->max_y && g->parse > 0) {
|
|
|
4000 |
g->step = (1 << g->parse) * g->line_size;
|
|
|
4001 |
g->cur_y = g->start_y + (g->step >> 1);
|
|
|
4002 |
--g->parse;
|
|
|
4003 |
}
|
|
|
4004 |
}
|
|
|
4005 |
}
|
|
|
4006 |
|
|
|
4007 |
static uint8 *stbi_process_gif_raster(stbi *s, stbi_gif *g)
|
|
|
4008 |
{
|
|
|
4009 |
uint8 lzw_cs;
|
|
|
4010 |
int32 len, code;
|
|
|
4011 |
uint32 first;
|
|
|
4012 |
int32 codesize, codemask, avail, oldcode, bits, valid_bits, clear;
|
|
|
4013 |
stbi_gif_lzw *p;
|
|
|
4014 |
|
|
|
4015 |
lzw_cs = get8u(s);
|
|
|
4016 |
clear = 1 << lzw_cs;
|
|
|
4017 |
first = 1;
|
|
|
4018 |
codesize = lzw_cs + 1;
|
|
|
4019 |
codemask = (1 << codesize) - 1;
|
|
|
4020 |
bits = 0;
|
|
|
4021 |
valid_bits = 0;
|
|
|
4022 |
for (code = 0; code < clear; code++) {
|
|
|
4023 |
g->codes[code].prefix = -1;
|
|
|
4024 |
g->codes[code].first = (uint8) code;
|
|
|
4025 |
g->codes[code].suffix = (uint8) code;
|
|
|
4026 |
}
|
|
|
4027 |
|
|
|
4028 |
// support no starting clear code
|
|
|
4029 |
avail = clear+2;
|
|
|
4030 |
oldcode = -1;
|
|
|
4031 |
|
|
|
4032 |
len = 0;
|
|
|
4033 |
for(;;) {
|
|
|
4034 |
if (valid_bits < codesize) {
|
|
|
4035 |
if (len == 0) {
|
|
|
4036 |
len = get8(s); // start new block
|
|
|
4037 |
if (len == 0)
|
|
|
4038 |
return g->out;
|
|
|
4039 |
}
|
|
|
4040 |
--len;
|
|
|
4041 |
bits |= (int32) get8(s) << valid_bits;
|
|
|
4042 |
valid_bits += 8;
|
|
|
4043 |
} else {
|
|
|
4044 |
int32 code = bits & codemask;
|
|
|
4045 |
bits >>= codesize;
|
|
|
4046 |
valid_bits -= codesize;
|
|
|
4047 |
// @OPTIMIZE: is there some way we can accelerate the non-clear path?
|
|
|
4048 |
if (code == clear) { // clear code
|
|
|
4049 |
codesize = lzw_cs + 1;
|
|
|
4050 |
codemask = (1 << codesize) - 1;
|
|
|
4051 |
avail = clear + 2;
|
|
|
4052 |
oldcode = -1;
|
|
|
4053 |
first = 0;
|
|
|
4054 |
} else if (code == clear + 1) { // end of stream code
|
|
|
4055 |
skip(s, len);
|
|
|
4056 |
while ((len = get8(s)) > 0)
|
|
|
4057 |
skip(s,len);
|
|
|
4058 |
return g->out;
|
|
|
4059 |
} else if (code <= avail) {
|
|
|
4060 |
if (first) return epuc("no clear code", "Corrupt GIF");
|
|
|
4061 |
|
|
|
4062 |
if (oldcode >= 0) {
|
|
|
4063 |
p = &g->codes[avail++];
|
|
|
4064 |
if (avail > 4096) return epuc("too many codes", "Corrupt GIF");
|
|
|
4065 |
p->prefix = (int16) oldcode;
|
|
|
4066 |
p->first = g->codes[oldcode].first;
|
|
|
4067 |
p->suffix = (code == avail) ? p->first : g->codes[code].first;
|
|
|
4068 |
} else if (code == avail)
|
|
|
4069 |
return epuc("illegal code in raster", "Corrupt GIF");
|
|
|
4070 |
|
|
|
4071 |
stbi_out_gif_code(g, (uint16) code);
|
|
|
4072 |
|
|
|
4073 |
if ((avail & codemask) == 0 && avail <= 0x0FFF) {
|
|
|
4074 |
codesize++;
|
|
|
4075 |
codemask = (1 << codesize) - 1;
|
|
|
4076 |
}
|
|
|
4077 |
|
|
|
4078 |
oldcode = code;
|
|
|
4079 |
} else {
|
|
|
4080 |
return epuc("illegal code in raster", "Corrupt GIF");
|
|
|
4081 |
}
|
|
|
4082 |
}
|
|
|
4083 |
}
|
|
|
4084 |
}
|
|
|
4085 |
|
|
|
4086 |
static void stbi_fill_gif_background(stbi_gif *g)
|
|
|
4087 |
{
|
|
|
4088 |
int i;
|
|
|
4089 |
uint8 *c = g->pal[g->bgindex];
|
|
|
4090 |
// @OPTIMIZE: write a dword at a time
|
|
|
4091 |
for (i = 0; i < g->w * g->h * 4; i += 4) {
|
|
|
4092 |
uint8 *p = &g->out[i];
|
|
|
4093 |
p[0] = c[2];
|
|
|
4094 |
p[1] = c[1];
|
|
|
4095 |
p[2] = c[0];
|
|
|
4096 |
p[3] = c[3];
|
|
|
4097 |
}
|
|
|
4098 |
}
|
|
|
4099 |
|
|
|
4100 |
// this function is designed to support animated gifs, although stb_image doesn't support it
|
|
|
4101 |
static uint8 *stbi_gif_load_next(stbi *s, stbi_gif *g, int *comp, int req_comp)
|
|
|
4102 |
{
|
|
|
4103 |
int i;
|
|
|
4104 |
uint8 *old_out = 0;
|
|
|
4105 |
|
|
|
4106 |
if (g->out == 0) {
|
|
|
4107 |
if (!stbi_gif_header(s, g, comp,0)) return 0; // failure_reason set by stbi_gif_header
|
|
|
4108 |
g->out = (uint8 *) malloc(4 * g->w * g->h);
|
|
|
4109 |
if (g->out == 0) return epuc("outofmem", "Out of memory");
|
|
|
4110 |
stbi_fill_gif_background(g);
|
|
|
4111 |
} else {
|
|
|
4112 |
// animated-gif-only path
|
|
|
4113 |
if (((g->eflags & 0x1C) >> 2) == 3) {
|
|
|
4114 |
old_out = g->out;
|
|
|
4115 |
g->out = (uint8 *) malloc(4 * g->w * g->h);
|
|
|
4116 |
if (g->out == 0) return epuc("outofmem", "Out of memory");
|
|
|
4117 |
memcpy(g->out, old_out, g->w*g->h*4);
|
|
|
4118 |
}
|
|
|
4119 |
}
|
|
|
4120 |
|
|
|
4121 |
for (;;) {
|
|
|
4122 |
switch (get8(s)) {
|
|
|
4123 |
case 0x2C: /* Image Descriptor */
|
|
|
4124 |
{
|
|
|
4125 |
int32 x, y, w, h;
|
|
|
4126 |
uint8 *o;
|
|
|
4127 |
|
|
|
4128 |
x = get16le(s);
|
|
|
4129 |
y = get16le(s);
|
|
|
4130 |
w = get16le(s);
|
|
|
4131 |
h = get16le(s);
|
|
|
4132 |
if (((x + w) > (g->w)) || ((y + h) > (g->h)))
|
|
|
4133 |
return epuc("bad Image Descriptor", "Corrupt GIF");
|
|
|
4134 |
|
|
|
4135 |
g->line_size = g->w * 4;
|
|
|
4136 |
g->start_x = x * 4;
|
|
|
4137 |
g->start_y = y * g->line_size;
|
|
|
4138 |
g->max_x = g->start_x + w * 4;
|
|
|
4139 |
g->max_y = g->start_y + h * g->line_size;
|
|
|
4140 |
g->cur_x = g->start_x;
|
|
|
4141 |
g->cur_y = g->start_y;
|
|
|
4142 |
|
|
|
4143 |
g->lflags = get8(s);
|
|
|
4144 |
|
|
|
4145 |
if (g->lflags & 0x40) {
|
|
|
4146 |
g->step = 8 * g->line_size; // first interlaced spacing
|
|
|
4147 |
g->parse = 3;
|
|
|
4148 |
} else {
|
|
|
4149 |
g->step = g->line_size;
|
|
|
4150 |
g->parse = 0;
|
|
|
4151 |
}
|
|
|
4152 |
|
|
|
4153 |
if (g->lflags & 0x80) {
|
|
|
4154 |
stbi_gif_parse_colortable(s,g->lpal, 2 << (g->lflags & 7), g->eflags & 0x01 ? g->transparent : -1);
|
|
|
4155 |
g->color_table = (uint8 *) g->lpal;
|
|
|
4156 |
} else if (g->flags & 0x80) {
|
|
|
4157 |
for (i=0; i < 256; ++i) // @OPTIMIZE: reset only the previous transparent
|
|
|
4158 |
g->pal[i][3] = 255;
|
|
|
4159 |
if (g->transparent >= 0 && (g->eflags & 0x01))
|
|
|
4160 |
g->pal[g->transparent][3] = 0;
|
|
|
4161 |
g->color_table = (uint8 *) g->pal;
|
|
|
4162 |
} else
|
|
|
4163 |
return epuc("missing color table", "Corrupt GIF");
|
|
|
4164 |
|
|
|
4165 |
o = stbi_process_gif_raster(s, g);
|
|
|
4166 |
if (o == NULL) return NULL;
|
|
|
4167 |
|
|
|
4168 |
if (req_comp && req_comp != 4)
|
|
|
4169 |
o = convert_format(o, 4, req_comp, g->w, g->h);
|
|
|
4170 |
return o;
|
|
|
4171 |
}
|
|
|
4172 |
|
|
|
4173 |
case 0x21: // Comment Extension.
|
|
|
4174 |
{
|
|
|
4175 |
int len;
|
|
|
4176 |
if (get8(s) == 0xF9) { // Graphic Control Extension.
|
|
|
4177 |
len = get8(s);
|
|
|
4178 |
if (len == 4) {
|
|
|
4179 |
g->eflags = get8(s);
|
|
|
4180 |
get16le(s); // delay
|
|
|
4181 |
g->transparent = get8(s);
|
|
|
4182 |
} else {
|
|
|
4183 |
skip(s, len);
|
|
|
4184 |
break;
|
|
|
4185 |
}
|
|
|
4186 |
}
|
|
|
4187 |
while ((len = get8(s)) != 0)
|
|
|
4188 |
skip(s, len);
|
|
|
4189 |
break;
|
|
|
4190 |
}
|
|
|
4191 |
|
|
|
4192 |
case 0x3B: // gif stream termination code
|
|
|
4193 |
return (uint8 *) 1;
|
|
|
4194 |
|
|
|
4195 |
default:
|
|
|
4196 |
return epuc("unknown code", "Corrupt GIF");
|
|
|
4197 |
}
|
|
|
4198 |
}
|
|
|
4199 |
}
|
|
|
4200 |
|
|
|
4201 |
#ifndef STBI_NO_STDIO
|
|
|
4202 |
stbi_uc *stbi_gif_load (char const *filename, int *x, int *y, int *comp, int req_comp)
|
|
|
4203 |
{
|
|
|
4204 |
uint8 *data;
|
|
|
4205 |
FILE *f = fopen(filename, "rb");
|
|
|
4206 |
if (!f) return NULL;
|
|
|
4207 |
data = stbi_gif_load_from_file(f, x,y,comp,req_comp);
|
|
|
4208 |
fclose(f);
|
|
|
4209 |
return data;
|
|
|
4210 |
}
|
|
|
4211 |
|
|
|
4212 |
stbi_uc *stbi_gif_load_from_file (FILE *f, int *x, int *y, int *comp, int req_comp)
|
|
|
4213 |
{
|
|
|
4214 |
uint8 *u = 0;
|
|
|
4215 |
stbi s;
|
|
|
4216 |
stbi_gif g={0};
|
|
|
4217 |
start_file(&s, f);
|
|
|
4218 |
|
|
|
4219 |
u = stbi_gif_load_next(&s, &g, comp, req_comp);
|
|
|
4220 |
if (u == (void *) 1) u = 0; // end of animated gif marker
|
|
|
4221 |
if (u) {
|
|
|
4222 |
*x = g.w;
|
|
|
4223 |
*y = g.h;
|
|
|
4224 |
}
|
|
|
4225 |
|
|
|
4226 |
return u;
|
|
|
4227 |
}
|
|
|
4228 |
#endif
|
|
|
4229 |
|
|
|
4230 |
stbi_uc *stbi_gif_load_from_memory (stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp)
|
|
|
4231 |
{
|
|
|
4232 |
uint8 *u = 0;
|
|
|
4233 |
stbi s;
|
|
|
4234 |
stbi_gif g={0};
|
|
|
4235 |
start_mem(&s, buffer, len);
|
|
|
4236 |
u = stbi_gif_load_next(&s, &g, comp, req_comp);
|
|
|
4237 |
if (u == (void *) 1) u = 0; // end of animated gif marker
|
|
|
4238 |
if (u) {
|
|
|
4239 |
*x = g.w;
|
|
|
4240 |
*y = g.h;
|
|
|
4241 |
}
|
|
|
4242 |
return u;
|
|
|
4243 |
}
|
|
|
4244 |
|
|
|
4245 |
#ifndef STBI_NO_STDIO
|
|
|
4246 |
int stbi_gif_info (char const *filename, int *x, int *y, int *comp)
|
|
|
4247 |
{
|
|
|
4248 |
int res;
|
|
|
4249 |
FILE *f = fopen(filename, "rb");
|
|
|
4250 |
if (!f) return 0;
|
|
|
4251 |
res = stbi_gif_info_from_file(f, x, y, comp);
|
|
|
4252 |
fclose(f);
|
|
|
4253 |
return res;
|
|
|
4254 |
}
|
|
|
4255 |
|
|
|
4256 |
int stbi_gif_info_from_file(FILE *f, int *x, int *y, int *comp)
|
|
|
4257 |
{
|
|
|
4258 |
stbi s;
|
|
|
4259 |
int res;
|
|
|
4260 |
long n = ftell(f);
|
|
|
4261 |
start_file(&s, f);
|
|
|
4262 |
res = stbi_gif_info_raw(&s, x, y, comp);
|
|
|
4263 |
fseek(f, n, SEEK_SET);
|
|
|
4264 |
return res;
|
|
|
4265 |
}
|
|
|
4266 |
#endif // !STBI_NO_STDIO
|
|
|
4267 |
|
|
|
4268 |
int stbi_gif_info_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp)
|
|
|
4269 |
{
|
|
|
4270 |
stbi s;
|
|
|
4271 |
start_mem(&s, buffer, len);
|
|
|
4272 |
return stbi_gif_info_raw(&s, x, y, comp);
|
|
|
4273 |
}
|
|
|
4274 |
|
|
|
4275 |
|
|
|
4276 |
|
|
|
4277 |
|
|
|
4278 |
// *************************************************************************************************
|
|
|
4279 |
// Radiance RGBE HDR loader
|
|
|
4280 |
// originally by Nicolas Schulz
|
|
|
4281 |
#ifndef STBI_NO_HDR
|
|
|
4282 |
static int hdr_test(stbi *s)
|
|
|
4283 |
{
|
|
|
4284 |
const char *signature = "#?RADIANCE\n";
|
|
|
4285 |
int i;
|
|
|
4286 |
for (i=0; signature[i]; ++i)
|
|
|
4287 |
if (get8(s) != signature[i])
|
|
|
4288 |
return 0;
|
|
|
4289 |
return 1;
|
|
|
4290 |
}
|
|
|
4291 |
|
|
|
4292 |
int stbi_hdr_test_memory(stbi_uc const *buffer, int len)
|
|
|
4293 |
{
|
|
|
4294 |
stbi s;
|
|
|
4295 |
start_mem(&s, buffer, len);
|
|
|
4296 |
return hdr_test(&s);
|
|
|
4297 |
}
|
|
|
4298 |
|
|
|
4299 |
#ifndef STBI_NO_STDIO
|
|
|
4300 |
int stbi_hdr_test_file(FILE *f)
|
|
|
4301 |
{
|
|
|
4302 |
stbi s;
|
|
|
4303 |
int r,n = ftell(f);
|
|
|
4304 |
start_file(&s, f);
|
|
|
4305 |
r = hdr_test(&s);
|
|
|
4306 |
fseek(f,n,SEEK_SET);
|
|
|
4307 |
return r;
|
|
|
4308 |
}
|
|
|
4309 |
#endif
|
|
|
4310 |
|
|
|
4311 |
#define HDR_BUFLEN 1024
|
|
|
4312 |
static char *hdr_gettoken(stbi *z, char *buffer)
|
|
|
4313 |
{
|
|
|
4314 |
int len=0;
|
|
|
4315 |
char c = '\0';
|
|
|
4316 |
|
|
|
4317 |
c = (char) get8(z);
|
|
|
4318 |
|
|
|
4319 |
while (!at_eof(z) && c != '\n') {
|
|
|
4320 |
buffer[len++] = c;
|
|
|
4321 |
if (len == HDR_BUFLEN-1) {
|
|
|
4322 |
// flush to end of line
|
|
|
4323 |
while (!at_eof(z) && get8(z) != '\n')
|
|
|
4324 |
;
|
|
|
4325 |
break;
|
|
|
4326 |
}
|
|
|
4327 |
c = (char) get8(z);
|
|
|
4328 |
}
|
|
|
4329 |
|
|
|
4330 |
buffer[len] = 0;
|
|
|
4331 |
return buffer;
|
|
|
4332 |
}
|
|
|
4333 |
|
|
|
4334 |
static void hdr_convert(float *output, stbi_uc *input, int req_comp)
|
|
|
4335 |
{
|
|
|
4336 |
if ( input[3] != 0 ) {
|
|
|
4337 |
float f1;
|
|
|
4338 |
// Exponent
|
|
|
4339 |
f1 = (float) ldexp(1.0f, input[3] - (int)(128 + 8));
|
|
|
4340 |
if (req_comp <= 2)
|
|
|
4341 |
output[0] = (input[0] + input[1] + input[2]) * f1 / 3;
|
|
|
4342 |
else {
|
|
|
4343 |
output[0] = input[0] * f1;
|
|
|
4344 |
output[1] = input[1] * f1;
|
|
|
4345 |
output[2] = input[2] * f1;
|
|
|
4346 |
}
|
|
|
4347 |
if (req_comp == 2) output[1] = 1;
|
|
|
4348 |
if (req_comp == 4) output[3] = 1;
|
|
|
4349 |
} else {
|
|
|
4350 |
switch (req_comp) {
|
|
|
4351 |
case 4: output[3] = 1; /* fallthrough */
|
|
|
4352 |
case 3: output[0] = output[1] = output[2] = 0;
|
|
|
4353 |
break;
|
|
|
4354 |
case 2: output[1] = 1; /* fallthrough */
|
|
|
4355 |
case 1: output[0] = 0;
|
|
|
4356 |
break;
|
|
|
4357 |
}
|
|
|
4358 |
}
|
|
|
4359 |
}
|
|
|
4360 |
|
|
|
4361 |
|
|
|
4362 |
static float *hdr_load(stbi *s, int *x, int *y, int *comp, int req_comp)
|
|
|
4363 |
{
|
|
|
4364 |
char buffer[HDR_BUFLEN];
|
|
|
4365 |
char *token;
|
|
|
4366 |
int valid = 0;
|
|
|
4367 |
int width, height;
|
|
|
4368 |
stbi_uc *scanline;
|
|
|
4369 |
float *hdr_data;
|
|
|
4370 |
int len;
|
|
|
4371 |
unsigned char count, value;
|
|
|
4372 |
int i, j, k, c1,c2, z;
|
|
|
4373 |
|
|
|
4374 |
|
|
|
4375 |
// Check identifier
|
|
|
4376 |
if (strcmp(hdr_gettoken(s,buffer), "#?RADIANCE") != 0)
|
|
|
4377 |
return epf("not HDR", "Corrupt HDR image");
|
|
|
4378 |
|
|
|
4379 |
// Parse header
|
|
|
4380 |
for(;;) {
|
|
|
4381 |
token = hdr_gettoken(s,buffer);
|
|
|
4382 |
if (token[0] == 0) break;
|
|
|
4383 |
if (strcmp(token, "FORMAT=32-bit_rle_rgbe") == 0) valid = 1;
|
|
|
4384 |
}
|
|
|
4385 |
|
|
|
4386 |
if (!valid) return epf("unsupported format", "Unsupported HDR format");
|
|
|
4387 |
|
|
|
4388 |
// Parse width and height
|
|
|
4389 |
// can't use sscanf() if we're not using stdio!
|
|
|
4390 |
token = hdr_gettoken(s,buffer);
|
|
|
4391 |
if (strncmp(token, "-Y ", 3)) return epf("unsupported data layout", "Unsupported HDR format");
|
|
|
4392 |
token += 3;
|
|
|
4393 |
height = strtol(token, &token, 10);
|
|
|
4394 |
while (*token == ' ') ++token;
|
|
|
4395 |
if (strncmp(token, "+X ", 3)) return epf("unsupported data layout", "Unsupported HDR format");
|
|
|
4396 |
token += 3;
|
|
|
4397 |
width = strtol(token, NULL, 10);
|
|
|
4398 |
|
|
|
4399 |
*x = width;
|
|
|
4400 |
*y = height;
|
|
|
4401 |
|
|
|
4402 |
*comp = 3;
|
|
|
4403 |
if (req_comp == 0) req_comp = 3;
|
|
|
4404 |
|
|
|
4405 |
// Read data
|
|
|
4406 |
hdr_data = (float *) malloc(height * width * req_comp * sizeof(float));
|
|
|
4407 |
|
|
|
4408 |
// Load image data
|
|
|
4409 |
// image data is stored as some number of sca
|
|
|
4410 |
if ( width < 8 || width >= 32768) {
|
|
|
4411 |
// Read flat data
|
|
|
4412 |
for (j=0; j < height; ++j) {
|
|
|
4413 |
for (i=0; i < width; ++i) {
|
|
|
4414 |
stbi_uc rgbe[4];
|
|
|
4415 |
main_decode_loop:
|
|
|
4416 |
getn(s, rgbe, 4);
|
|
|
4417 |
hdr_convert(hdr_data + j * width * req_comp + i * req_comp, rgbe, req_comp);
|
|
|
4418 |
}
|
|
|
4419 |
}
|
|
|
4420 |
} else {
|
|
|
4421 |
// Read RLE-encoded data
|
|
|
4422 |
scanline = NULL;
|
|
|
4423 |
|
|
|
4424 |
for (j = 0; j < height; ++j) {
|
|
|
4425 |
c1 = get8(s);
|
|
|
4426 |
c2 = get8(s);
|
|
|
4427 |
len = get8(s);
|
|
|
4428 |
if (c1 != 2 || c2 != 2 || (len & 0x80)) {
|
|
|
4429 |
// not run-length encoded, so we have to actually use THIS data as a decoded
|
|
|
4430 |
// pixel (note this can't be a valid pixel--one of RGB must be >= 128)
|
|
|
4431 |
uint8 rgbe[4];
|
|
|
4432 |
rgbe[0] = (uint8) c1;
|
|
|
4433 |
rgbe[1] = (uint8) c2;
|
|
|
4434 |
rgbe[2] = (uint8) len;
|
|
|
4435 |
rgbe[3] = (uint8) get8u(s);
|
|
|
4436 |
hdr_convert(hdr_data, rgbe, req_comp);
|
|
|
4437 |
i = 1;
|
|
|
4438 |
j = 0;
|
|
|
4439 |
free(scanline);
|
|
|
4440 |
goto main_decode_loop; // yes, this makes no sense
|
|
|
4441 |
}
|
|
|
4442 |
len <<= 8;
|
|
|
4443 |
len |= get8(s);
|
|
|
4444 |
if (len != width) { free(hdr_data); free(scanline); return epf("invalid decoded scanline length", "corrupt HDR"); }
|
|
|
4445 |
if (scanline == NULL) scanline = (stbi_uc *) malloc(width * 4);
|
|
|
4446 |
|
|
|
4447 |
for (k = 0; k < 4; ++k) {
|
|
|
4448 |
i = 0;
|
|
|
4449 |
while (i < width) {
|
|
|
4450 |
count = get8u(s);
|
|
|
4451 |
if (count > 128) {
|
|
|
4452 |
// Run
|
|
|
4453 |
value = get8u(s);
|
|
|
4454 |
count -= 128;
|
|
|
4455 |
for (z = 0; z < count; ++z)
|
|
|
4456 |
scanline[i++ * 4 + k] = value;
|
|
|
4457 |
} else {
|
|
|
4458 |
// Dump
|
|
|
4459 |
for (z = 0; z < count; ++z)
|
|
|
4460 |
scanline[i++ * 4 + k] = get8u(s);
|
|
|
4461 |
}
|
|
|
4462 |
}
|
|
|
4463 |
}
|
|
|
4464 |
for (i=0; i < width; ++i)
|
|
|
4465 |
hdr_convert(hdr_data+(j*width + i)*req_comp, scanline + i*4, req_comp);
|
|
|
4466 |
}
|
|
|
4467 |
free(scanline);
|
|
|
4468 |
}
|
|
|
4469 |
|
|
|
4470 |
return hdr_data;
|
|
|
4471 |
}
|
|
|
4472 |
|
|
|
4473 |
static stbi_uc *hdr_load_rgbe(stbi *s, int *x, int *y, int *comp, int req_comp)
|
|
|
4474 |
{
|
|
|
4475 |
char buffer[HDR_BUFLEN];
|
|
|
4476 |
char *token;
|
|
|
4477 |
int valid = 0;
|
|
|
4478 |
int width, height;
|
|
|
4479 |
stbi_uc *scanline;
|
|
|
4480 |
stbi_uc *rgbe_data;
|
|
|
4481 |
int len;
|
|
|
4482 |
unsigned char count, value;
|
|
|
4483 |
int i, j, k, c1,c2, z;
|
|
|
4484 |
|
|
|
4485 |
|
|
|
4486 |
// Check identifier
|
|
|
4487 |
if (strcmp(hdr_gettoken(s,buffer), "#?RADIANCE") != 0)
|
|
|
4488 |
return epuc("not HDR", "Corrupt HDR image");
|
|
|
4489 |
|
|
|
4490 |
// Parse header
|
|
|
4491 |
while(1) {
|
|
|
4492 |
token = hdr_gettoken(s,buffer);
|
|
|
4493 |
if (token[0] == 0) break;
|
|
|
4494 |
if (strcmp(token, "FORMAT=32-bit_rle_rgbe") == 0) valid = 1;
|
|
|
4495 |
}
|
|
|
4496 |
|
|
|
4497 |
if (!valid) return epuc("unsupported format", "Unsupported HDR format");
|
|
|
4498 |
|
|
|
4499 |
// Parse width and height
|
|
|
4500 |
// can't use sscanf() if we're not using stdio!
|
|
|
4501 |
token = hdr_gettoken(s,buffer);
|
|
|
4502 |
if (strncmp(token, "-Y ", 3)) return epuc("unsupported data layout", "Unsupported HDR format");
|
|
|
4503 |
token += 3;
|
|
|
4504 |
height = strtol(token, &token, 10);
|
|
|
4505 |
while (*token == ' ') ++token;
|
|
|
4506 |
if (strncmp(token, "+X ", 3)) return epuc("unsupported data layout", "Unsupported HDR format");
|
|
|
4507 |
token += 3;
|
|
|
4508 |
width = strtol(token, NULL, 10);
|
|
|
4509 |
|
|
|
4510 |
*x = width;
|
|
|
4511 |
*y = height;
|
|
|
4512 |
|
|
|
4513 |
// RGBE _MUST_ come out as 4 components
|
|
|
4514 |
*comp = 4;
|
|
|
4515 |
req_comp = 4;
|
|
|
4516 |
|
|
|
4517 |
// Read data
|
|
|
4518 |
rgbe_data = (stbi_uc *) malloc(height * width * req_comp * sizeof(stbi_uc));
|
|
|
4519 |
// point to the beginning
|
|
|
4520 |
scanline = rgbe_data;
|
|
|
4521 |
|
|
|
4522 |
// Load image data
|
|
|
4523 |
// image data is stored as some number of scan lines
|
|
|
4524 |
if( width < 8 || width >= 32768) {
|
|
|
4525 |
// Read flat data
|
|
|
4526 |
for (j=0; j < height; ++j) {
|
|
|
4527 |
for (i=0; i < width; ++i) {
|
|
|
4528 |
main_decode_loop:
|
|
|
4529 |
//getn(rgbe, 4);
|
|
|
4530 |
getn(s,scanline, 4);
|
|
|
4531 |
scanline += 4;
|
|
|
4532 |
}
|
|
|
4533 |
}
|
|
|
4534 |
} else {
|
|
|
4535 |
// Read RLE-encoded data
|
|
|
4536 |
for (j = 0; j < height; ++j) {
|
|
|
4537 |
c1 = get8(s);
|
|
|
4538 |
c2 = get8(s);
|
|
|
4539 |
len = get8(s);
|
|
|
4540 |
if (c1 != 2 || c2 != 2 || (len & 0x80)) {
|
|
|
4541 |
// not run-length encoded, so we have to actually use THIS data as a decoded
|
|
|
4542 |
// pixel (note this can't be a valid pixel--one of RGB must be >= 128)
|
|
|
4543 |
scanline[0] = c1;
|
|
|
4544 |
scanline[1] = c2;
|
|
|
4545 |
scanline[2] = len;
|
|
|
4546 |
scanline[3] = get8(s);
|
|
|
4547 |
scanline += 4;
|
|
|
4548 |
i = 1;
|
|
|
4549 |
j = 0;
|
|
|
4550 |
goto main_decode_loop; // yes, this is insane; blame the insane format
|
|
|
4551 |
}
|
|
|
4552 |
len <<= 8;
|
|
|
4553 |
len |= get8(s);
|
|
|
4554 |
if (len != width) { free(rgbe_data); return epuc("invalid decoded scanline length", "corrupt HDR"); }
|
|
|
4555 |
for (k = 0; k < 4; ++k) {
|
|
|
4556 |
i = 0;
|
|
|
4557 |
while (i < width) {
|
|
|
4558 |
count = get8(s);
|
|
|
4559 |
if (count > 128) {
|
|
|
4560 |
// Run
|
|
|
4561 |
value = get8(s);
|
|
|
4562 |
count -= 128;
|
|
|
4563 |
for (z = 0; z < count; ++z)
|
|
|
4564 |
scanline[i++ * 4 + k] = value;
|
|
|
4565 |
} else {
|
|
|
4566 |
// Dump
|
|
|
4567 |
for (z = 0; z < count; ++z)
|
|
|
4568 |
scanline[i++ * 4 + k] = get8(s);
|
|
|
4569 |
}
|
|
|
4570 |
}
|
|
|
4571 |
}
|
|
|
4572 |
// move the scanline on
|
|
|
4573 |
scanline += 4 * width;
|
|
|
4574 |
}
|
|
|
4575 |
}
|
|
|
4576 |
|
|
|
4577 |
return rgbe_data;
|
|
|
4578 |
}
|
|
|
4579 |
|
|
|
4580 |
#ifndef STBI_NO_STDIO
|
|
|
4581 |
float *stbi_hdr_load_from_file(FILE *f, int *x, int *y, int *comp, int req_comp)
|
|
|
4582 |
{
|
|
|
4583 |
stbi s;
|
|
|
4584 |
start_file(&s,f);
|
|
|
4585 |
return hdr_load(&s,x,y,comp,req_comp);
|
|
|
4586 |
}
|
|
|
4587 |
|
|
|
4588 |
stbi_uc *stbi_hdr_load_rgbe_file(FILE *f, int *x, int *y, int *comp, int req_comp)
|
|
|
4589 |
{
|
|
|
4590 |
stbi s;
|
|
|
4591 |
start_file(&s,f);
|
|
|
4592 |
return hdr_load_rgbe(&s,x,y,comp,req_comp);
|
|
|
4593 |
}
|
|
|
4594 |
|
|
|
4595 |
stbi_uc *stbi_hdr_load_rgbe (char const *filename, int *x, int *y, int *comp, int req_comp)
|
|
|
4596 |
{
|
|
|
4597 |
FILE *f = fopen(filename, "rb");
|
|
|
4598 |
unsigned char *result;
|
|
|
4599 |
if (!f) return epuc("can't fopen", "Unable to open file");
|
|
|
4600 |
result = stbi_hdr_load_rgbe_file(f,x,y,comp,req_comp);
|
|
|
4601 |
fclose(f);
|
|
|
4602 |
return result;
|
|
|
4603 |
}
|
|
|
4604 |
#endif
|
|
|
4605 |
|
|
|
4606 |
float *stbi_hdr_load_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp)
|
|
|
4607 |
{
|
|
|
4608 |
stbi s;
|
|
|
4609 |
start_mem(&s,buffer, len);
|
|
|
4610 |
return hdr_load(&s,x,y,comp,req_comp);
|
|
|
4611 |
}
|
|
|
4612 |
|
|
|
4613 |
stbi_uc *stbi_hdr_load_rgbe_memory(stbi_uc *buffer, int len, int *x, int *y, int *comp, int req_comp)
|
|
|
4614 |
{
|
|
|
4615 |
stbi s;
|
|
|
4616 |
start_mem(&s,buffer, len);
|
|
|
4617 |
return hdr_load_rgbe(&s,x,y,comp,req_comp);
|
|
|
4618 |
}
|
|
|
4619 |
|
|
|
4620 |
#endif // STBI_NO_HDR
|
|
|
4621 |
|
|
|
4622 |
|
|
|
4623 |
#ifndef STBI_NO_STDIO
|
|
|
4624 |
int stbi_info(char const *filename, int *x, int *y, int *comp)
|
|
|
4625 |
{
|
|
|
4626 |
FILE *f = fopen(filename, "rb");
|
|
|
4627 |
int result;
|
|
|
4628 |
if (!f) return e("can't fopen", "Unable to open file");
|
|
|
4629 |
result = stbi_info_from_file(f, x, y, comp);
|
|
|
4630 |
fclose(f);
|
|
|
4631 |
return result;
|
|
|
4632 |
}
|
|
|
4633 |
|
|
|
4634 |
int stbi_info_from_file(FILE *f, int *x, int *y, int *comp)
|
|
|
4635 |
{
|
|
|
4636 |
if (stbi_jpeg_info_from_file(f, x, y, comp))
|
|
|
4637 |
return 1;
|
|
|
4638 |
if (stbi_png_info_from_file(f, x, y, comp))
|
|
|
4639 |
return 1;
|
|
|
4640 |
if (stbi_gif_info_from_file(f, x, y, comp))
|
|
|
4641 |
return 1;
|
|
|
4642 |
// @TODO: stbi_bmp_info_from_file
|
|
|
4643 |
// @TODO: stbi_psd_info_from_file
|
|
|
4644 |
#ifndef STBI_NO_HDR
|
|
|
4645 |
// @TODO: stbi_hdr_info_from_file
|
|
|
4646 |
#endif
|
|
|
4647 |
// test tga last because it's a crappy test!
|
|
|
4648 |
if (stbi_tga_info_from_file(f, x, y, comp))
|
|
|
4649 |
return 1;
|
|
|
4650 |
return e("unknown image type", "Image not of any known type, or corrupt");
|
|
|
4651 |
}
|
|
|
4652 |
#endif // !STBI_NO_STDIO
|
|
|
4653 |
|
|
|
4654 |
int stbi_info_from_memory(stbi_uc const *buffer, int len, int *x, int *y, int *comp)
|
|
|
4655 |
{
|
|
|
4656 |
if (stbi_jpeg_info_from_memory(buffer, len, x, y, comp))
|
|
|
4657 |
return 1;
|
|
|
4658 |
if (stbi_png_info_from_memory(buffer, len, x, y, comp))
|
|
|
4659 |
return 1;
|
|
|
4660 |
if (stbi_gif_info_from_memory(buffer, len, x, y, comp))
|
|
|
4661 |
return 1;
|
|
|
4662 |
// @TODO: stbi_bmp_info_from_memory
|
|
|
4663 |
// @TODO: stbi_psd_info_from_memory
|
|
|
4664 |
#ifndef STBI_NO_HDR
|
|
|
4665 |
// @TODO: stbi_hdr_info_from_memory
|
|
|
4666 |
#endif
|
|
|
4667 |
// test tga last because it's a crappy test!
|
|
|
4668 |
if (stbi_tga_info_from_memory(buffer, len, x, y, comp))
|
|
|
4669 |
return 1;
|
|
|
4670 |
return e("unknown image type", "Image not of any known type, or corrupt");
|
|
|
4671 |
}
|
|
|
4672 |
|
|
|
4673 |
#endif // STBI_HEADER_FILE_ONLY
|
|
|
4674 |
|
|
|
4675 |
/*
|
|
|
4676 |
revision history:
|
|
|
4677 |
1.29 (2010-08-16) various warning fixes from Aurelien Pocheville
|
|
|
4678 |
1.28 (2010-08-01) fix bug in GIF palette transparency (SpartanJ)
|
|
|
4679 |
1.27 (2010-08-01)
|
|
|
4680 |
cast-to-uint8 to fix warnings
|
|
|
4681 |
1.26 (2010-07-24)
|
|
|
4682 |
fix bug in file buffering for PNG reported by SpartanJ
|
|
|
4683 |
1.25 (2010-07-17)
|
|
|
4684 |
refix trans_data warning (Won Chun)
|
|
|
4685 |
1.24 (2010-07-12)
|
|
|
4686 |
perf improvements reading from files on platforms with lock-heavy fgetc()
|
|
|
4687 |
minor perf improvements for jpeg
|
|
|
4688 |
deprecated type-specific functions so we'll get feedback if they're needed
|
|
|
4689 |
attempt to fix trans_data warning (Won Chun)
|
|
|
4690 |
1.23 fixed bug in iPhone support
|
|
|
4691 |
1.22 (2010-07-10)
|
|
|
4692 |
removed image *writing* support
|
|
|
4693 |
removed image *writing* support
|
|
|
4694 |
stbi_info support from Jetro Lauha
|
|
|
4695 |
GIF support from Jean-Marc Lienher
|
|
|
4696 |
iPhone PNG-extensions from James Brown
|
|
|
4697 |
warning-fixes from Nicolas Schulz and Janez Zemva (i.e. Janez (U+017D)emva)
|
|
|
4698 |
1.21 fix use of 'uint8' in header (reported by jon blow)
|
|
|
4699 |
1.20 added support for Softimage PIC, by Tom Seddon
|
|
|
4700 |
1.19 bug in interlaced PNG corruption check (found by ryg)
|
|
|
4701 |
1.18 2008-08-02
|
|
|
4702 |
fix a threading bug (local mutable static)
|
|
|
4703 |
1.17 support interlaced PNG
|
|
|
4704 |
1.16 major bugfix - convert_format converted one too many pixels
|
|
|
4705 |
1.15 initialize some fields for thread safety
|
|
|
4706 |
1.14 fix threadsafe conversion bug
|
|
|
4707 |
header-file-only version (#define STBI_HEADER_FILE_ONLY before including)
|
|
|
4708 |
1.13 threadsafe
|
|
|
4709 |
1.12 const qualifiers in the API
|
|
|
4710 |
1.11 Support installable IDCT, colorspace conversion routines
|
|
|
4711 |
1.10 Fixes for 64-bit (don't use "unsigned long")
|
|
|
4712 |
optimized upsampling by Fabian "ryg" Giesen
|
|
|
4713 |
1.09 Fix format-conversion for PSD code (bad global variables!)
|
|
|
4714 |
1.08 Thatcher Ulrich's PSD code integrated by Nicolas Schulz
|
|
|
4715 |
1.07 attempt to fix C++ warning/errors again
|
|
|
4716 |
1.06 attempt to fix C++ warning/errors again
|
|
|
4717 |
1.05 fix TGA loading to return correct *comp and use good luminance calc
|
|
|
4718 |
1.04 default float alpha is 1, not 255; use 'void *' for stbi_image_free
|
|
|
4719 |
1.03 bugfixes to STBI_NO_STDIO, STBI_NO_HDR
|
|
|
4720 |
1.02 support for (subset of) HDR files, float interface for preferred access to them
|
|
|
4721 |
1.01 fix bug: possible bug in handling right-side up bmps... not sure
|
|
|
4722 |
fix bug: the stbi_bmp_load() and stbi_tga_load() functions didn't work at all
|
|
|
4723 |
1.00 interface to zlib that skips zlib header
|
|
|
4724 |
0.99 correct handling of alpha in palette
|
|
|
4725 |
0.98 TGA loader by lonesock; dynamically add loaders (untested)
|
|
|
4726 |
0.97 jpeg errors on too large a file; also catch another malloc failure
|
|
|
4727 |
0.96 fix detection of invalid v value - particleman@mollyrocket forum
|
|
|
4728 |
0.95 during header scan, seek to markers in case of padding
|
|
|
4729 |
0.94 STBI_NO_STDIO to disable stdio usage; rename all #defines the same
|
|
|
4730 |
0.93 handle jpegtran output; verbose errors
|
|
|
4731 |
0.92 read 4,8,16,24,32-bit BMP files of several formats
|
|
|
4732 |
0.91 output 24-bit Windows 3.0 BMP files
|
|
|
4733 |
0.90 fix a few more warnings; bump version number to approach 1.0
|
|
|
4734 |
0.61 bugfixes due to Marc LeBlanc, Christopher Lloyd
|
|
|
4735 |
0.60 fix compiling as c++
|
|
|
4736 |
0.59 fix warnings: merge Dave Moore's -Wall fixes
|
|
|
4737 |
0.58 fix bug: zlib uncompressed mode len/nlen was wrong endian
|
|
|
4738 |
0.57 fix bug: jpg last huffman symbol before marker was >9 bits but less
|
|
|
4739 |
than 16 available
|
|
|
4740 |
0.56 fix bug: zlib uncompressed mode len vs. nlen
|
|
|
4741 |
0.55 fix bug: restart_interval not initialized to 0
|
|
|
4742 |
0.54 allow NULL for 'int *comp'
|
|
|
4743 |
0.53 fix bug in png 3->4; speedup png decoding
|
|
|
4744 |
0.52 png handles req_comp=3,4 directly; minor cleanup; jpeg comments
|
|
|
4745 |
0.51 obey req_comp requests, 1-component jpegs return as 1-component,
|
|
|
4746 |
on 'test' only check type, not whether we support this variant
|
|
|
4747 |
*/
|