Skip to content
Myr

rename integer types to avoid collisions

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Sean Barrett · committed by Sean Barrett · parent ab33a72 · Browse files · 1 file+214 added−208 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+214 added−208 removed

  • other:+214 added−208 removed

1 file+214 added−208 removed

modified:stb_image.c+214 added-208 removed
20 unmodified lines
21 - decode from arbitrary I/O callbacks22 - overridable dequantizing-IDCT, YCbCr-to-RGB conversion (define STBI_SIMD)2324 Latest revisions:25 1.34 (unknown ) warning fix26 1.33 (2011-07-14) minor fixes suggested by Dave Moore27 1.32 (2011-07-13) info support for all filetypes (SpartanJ)28 1.31 (2011-06-19) a few more leak fixes, bug in PNG handling (SpartanJ)29 1.30 (2011-06-11) added ability to load files via io callbacks (Ben Wenger)30 1.29 (2010-08-16) various warning fixes from Aurelien Pocheville 31 1.28 (2010-08-01) fix bug in GIF palette transparency (SpartanJ)31 1.27 (2010-08-01) cast-to-uint8 to fix warnings (Laurent Gomila)32 1.27 (2010-08-01) cast-to-stbi__uint8 to fix warnings (Laurent Gomila)33 allow trailing 0s at end of image data (Laurent Gomila)34 1.26 (2010-07-24) fix bug in file buffering for PNG reported by SpartanJ3536 See end of file for full revision history.
18 unmodified lines
55 James "moose2000" Brown (iPhone PNG) Laurent Gomila 56 Ben "Disch" Wenger (io callbacks) Aruelien Pocheville57 Martin "SpartanJ" Golini Ryamond Barbiero58 David Woo58 596061 If your name should be here but isn't, let Sean know.6263*/64
293 unmodified lines
358#endif359360361// implementation:361typedef unsigned char uint8;362typedef unsigned short uint16;363typedef signed short int16;364typedef unsigned int uint32;365typedef signed int int32;366typedef unsigned int uint;362typedef unsigned char stbi__uint8;363typedef unsigned short stbi__uint16;364typedef signed short stbi__int16;365typedef unsigned int stbi__uint32;366typedef signed int stbi__int32;367368// should produce compiler error if size is wrong369typedef unsigned char validate_uint32[sizeof(uint32)==4 ? 1 : -1];369typedef unsigned char validate_uint32[sizeof(stbi__uint32)==4 ? 1 : -1];370371#if defined(STBI_NO_STDIO) && !defined(STBI_NO_WRITE)372#define STBI_NO_WRITE373#endif
17 unmodified lines
391// stbi structure is our basic context used by all images, so it392// contains all the IO context, plus some basic image information393typedef struct394{395 uint32 img_x, img_y;395 stbi__uint32 img_x, img_y;396 int img_n, img_out_n;397 398 stbi_io_callbacks io;399 void *io_user_data;400401 int read_from_callbacks;402 int buflen;403 uint8 buffer_start[128];403 stbi__uint8 buffer_start[128];404405 uint8 *img_buffer, *img_buffer_end;406 uint8 *img_buffer_original;405 stbi__uint8 *img_buffer, *img_buffer_end;406 stbi__uint8 *img_buffer_original;407} stbi;408409410static void refill_buffer(stbi *s);411412// initialize a memory-decode context413static void start_mem(stbi *s, uint8 const *buffer, int len)413static void start_mem(stbi *s, stbi__uint8 const *buffer, int len)414{415 s->io.read = NULL;416 s->read_from_callbacks = 0;417 s->img_buffer = s->img_buffer_original = (uint8 *) buffer;418 s->img_buffer_end = (uint8 *) buffer+len;417 s->img_buffer = s->img_buffer_original = (stbi__uint8 *) buffer;418 s->img_buffer_end = (stbi__uint8 *) buffer+len;419}420421// initialize a callback-based context422static void start_callbacks(stbi *s, stbi_io_callbacks *c, void *user)
325 unmodified lines
748749 return s->img_buffer >= s->img_buffer_end; 750}751752stbi_inline static uint8 get8u(stbi *s)752stbi_inline static stbi__uint8 get8u(stbi *s)753{754 return (uint8) get8(s);754 return (stbi__uint8) get8(s);755}756757static void skip(stbi *s, int n)758{
37 unmodified lines
796 int z = get8(s);797 return (z << 8) + get8(s);798}799800static uint32 get32(stbi *s)800static stbi__uint32 get32(stbi *s)801{802 uint32 z = get16(s);802 stbi__uint32 z = get16(s);803 return (z << 16) + get16(s);804}805806static int get16le(stbi *s)
1 unmodified line
808 int z = get8(s);809 return z + (get8(s) << 8);810}811812static uint32 get32le(stbi *s)812static stbi__uint32 get32le(stbi *s)813{814 uint32 z = get16le(s);814 stbi__uint32 z = get16le(s);815 return z + (get16le(s) << 16);816}817818//////////////////////////////////////////////////////////////////////////////
6 unmodified lines
825//826// assume data buffer is malloced, so malloc a new one and free that one827// only failure mode is malloc failing828829static uint8 compute_y(int r, int g, int b)829static stbi__uint8 compute_y(int r, int g, int b)830{831 return (uint8) (((r*77) + (g*150) + (29*b)) >> 8);831 return (stbi__uint8) (((r*77) + (g*150) + (29*b)) >> 8);832}833834static unsigned char *convert_format(unsigned char *data, int img_n, int req_comp, uint x, uint y)834static unsigned char *convert_format(unsigned char *data, int img_n, int req_comp, unsigned int x, unsigned int y)835{836 int i,j;837 unsigned char *good;838
66 unmodified lines
905 for (k=0; k < n; ++k) {906 float z = (float) pow(data[i*comp+k]*h2l_scale_i, h2l_gamma_i) * 255 + 0.5f;907 if (z < 0) z = 0;908 if (z > 255) z = 255;909 output[i*comp + k] = (uint8) float2int(z);909 output[i*comp + k] = (stbi__uint8) float2int(z);910 }911 if (k < comp) {912 float z = data[i*comp+k] * 255 + 0.5f;913 if (z < 0) z = 0;914 if (z > 255) z = 255;915 output[i*comp + k] = (uint8) float2int(z);915 output[i*comp + k] = (stbi__uint8) float2int(z);916 }917 }918 free(data);919 return output;
31 unmodified lines
951#define FAST_BITS 9 // larger handles more cases; smaller stomps less cache952953typedef struct954{955 uint8 fast[1 << FAST_BITS];955 stbi__uint8 fast[1 << FAST_BITS];956 // weirdly, repacking this into AoS is a 10% speed loss, instead of a win957 uint16 code[256];958 uint8 values[256];959 uint8 size[257];957 stbi__uint16 code[256];958 stbi__uint8 values[256];959 stbi__uint8 size[257];960 unsigned int maxcode[18];961 int delta[17]; // old 'firstsymbol' - old 'firstcode'962} huffman;963
4 unmodified lines
968 #endif969 stbi *s;970 huffman huff_dc[4];971 huffman huff_ac[4];972 uint8 dequant[4][64];972 stbi__uint8 dequant[4][64];973974// sizes for components, interleaved MCUs975 int img_h_max, img_v_max;976 int img_mcu_x, img_mcu_y;
8 unmodified lines
985 int hd,ha;986 int dc_pred;987988 int x,y,w2,h2;989 uint8 *data;989 stbi__uint8 *data;990 void *raw_data;991 uint8 *linebuf;991 stbi__uint8 *linebuf;992 } img_comp[4];993994 uint32 code_buffer; // jpeg entropy-coded buffer994 stbi__uint32 code_buffer; // jpeg entropy-coded buffer995 int code_bits; // number of valid bits996 unsigned char marker; // marker seen while filling entropy buffer997 int nomore; // flag if we saw a marker so must stop998
6 unmodified lines
1005 int i,j,k=0,code;1006 // build size list for each symbol (from JPEG spec)1007 for (i=0; i < 16; ++i)1008 for (j=0; j < count[i]; ++j)1009 h->size[k++] = (uint8) (i+1);1009 h->size[k++] = (stbi__uint8) (i+1);1010 h->size[k] = 0;10111012 // compute actual symbols (from jpeg spec)1013 code = 0;
2 unmodified lines
1016 // compute delta to add to code to compute symbol id1017 h->delta[j] = k - code;1018 if (h->size[k] == j) {1019 while (h->size[k] == j)1020 h->code[k++] = (uint16) (code++);1020 h->code[k++] = (stbi__uint16) (code++);1021 if (code-1 >= (1 << j)) return e("bad code lengths","Corrupt JPEG");1022 }1023 // compute largest code + 1 for this size, preshifted as needed later1024 h->maxcode[j] = code << (16-j);
8 unmodified lines
1033 if (s <= FAST_BITS) {1034 int c = h->code[i] << (FAST_BITS-s);1035 int m = 1 << (FAST_BITS-s);1036 for (j=0; j < m; ++j) {1037 h->fast[c+j] = (uint8) i;1037 h->fast[c+j] = (stbi__uint8) i;1038 }1039 }1040 }1041 return 1;
16 unmodified lines
1058 } while (j->code_bits <= 24);1059}10601061// (1 << n) - 11062static uint32 bmask[17]={0,1,3,7,15,31,63,127,255,511,1023,2047,4095,8191,16383,32767,65535};1062static stbi__uint32 bmask[17]={0,1,3,7,15,31,63,127,255,511,1023,2047,4095,8191,16383,32767,65535};10631064// decode a jpeg huffman value from the bitstream1065stbi_inline static int decode(jpeg *j, huffman *h)1066{
72 unmodified lines
1139}11401141// given a value that's at position X in the zigzag stream,1142// where does it appear in the 8x8 matrix coded as row-major?1143static uint8 dezigzag[64+15] =1143static stbi__uint8 dezigzag[64+15] =1144{1145 0, 1, 8, 16, 9, 2, 3, 10,1146 17, 24, 32, 25, 18, 11, 4, 5,1147 12, 19, 26, 33, 40, 48, 41, 34,
42 unmodified lines
1190 return 1;1191}11921193// take a -128..127 value and clamp it and convert to 0..2551194stbi_inline static uint8 clamp(int x)1194stbi_inline static stbi__uint8 clamp(int x)1195{1196 // trick to use a single test to catch both cases1197 if ((unsigned int) x > 255) {1198 if (x < 0) return 0;1199 if (x > 255) return 255;1200 }1201 return (uint8) x;1201 return (stbi__uint8) x;1202}12031204#define f2f(x) (int) (((x) * 4096 + 0.5))1205#define fsh(x) ((x) << 12)
38 unmodified lines
12441245#ifdef STBI_SIMD1246typedef unsigned short stbi_dequantize_t;1247#else1248typedef uint8 stbi_dequantize_t;1248typedef stbi__uint8 stbi_dequantize_t;1249#endif12501251// .344 seconds on 3*anemones.jpg1252static void idct_block(uint8 *out, int out_stride, short data[64], stbi_dequantize_t *dequantize)1252static void idct_block(stbi__uint8 *out, int out_stride, short data[64], stbi_dequantize_t *dequantize)1253{1254 int i,val[64],*v=val;1255 stbi_dequantize_t *dq = dequantize;1256 uint8 *o;1256 stbi__uint8 *o;1257 short *d = data;12581259 // columns1260 for (i=0; i < 8; ++i,++d,++dq, ++v) {
61 unmodified lines
1322#define MARKER_none 0xff1323// if there's a pending marker from the entropy stream, return that1324// otherwise, fetch from the stream and get a marker. if there's no1325// marker, return 0xff, which is never a valid marker value1326static uint8 get_marker(jpeg *j)1326static stbi__uint8 get_marker(jpeg *j)1327{1328 uint8 x;1328 stbi__uint8 x;1329 if (j->marker != MARKER_none) { x = j->marker; j->marker = MARKER_none; return x; }1330 x = get8u(j->s);1331 if (x != 0xff) return MARKER_none;1332 while (x == 0xff)
126 unmodified lines
14591460 case 0xC4: // DHT - define huffman table1461 L = get16(z->s)-2;1462 while (L > 0) {1463 uint8 *v;1464 int sizes[16],i,m=0;1463 stbi__uint8 *v;1464 int sizes[16],i,n=0;1465 int q = get8(z->s);1466 int tc = q >> 4;1467 int th = q & 15;1468 if (tc > 1 || th > 3) return e("bad DHT header","Corrupt JPEG");1469 for (i=0; i < 16; ++i) {1470 sizes[i] = get8(z->s);1471 m += sizes[i];1471 n += sizes[i];1472 }1473 L -= 17;1474 if (tc == 0) {1475 if (!build_huffman(z->huff_dc+th, sizes)) return 0;
1 unmodified line
1477 } else {1478 if (!build_huffman(z->huff_ac+th, sizes)) return 0;1479 v = z->huff_ac[th].values;1480 }1481 for (i=0; i < m; ++i)1481 for (i=0; i < n; ++i)1482 v[i] = get8u(z->s);1483 L -= m;1483 L -= n;1484 }1485 return L==0;1486 }1487 // check for comment block or APP blocks
94 unmodified lines
1582 }1583 return e("outofmem", "Out of memory");1584 }1585 // align blocks for installable-idct using mmx/sse1586 z->img_comp[i].data = (uint8*) (((size_t) z->img_comp[i].raw_data + 15) & ~15);1586 z->img_comp[i].data = (stbi__uint8*) (((size_t) z->img_comp[i].raw_data + 15) & ~15);1587 z->img_comp[i].linebuf = NULL;1588 }15891590 return 1;
59 unmodified lines
1650}16511652// static jfif-centered resampling (across block boundaries)16531654typedef uint8 *(*resample_row_func)(uint8 *out, uint8 *in0, uint8 *in1,1654typedef stbi__uint8 *(*resample_row_func)(stbi__uint8 *out, stbi__uint8 *in0, stbi__uint8 *in1,1655 int w, int hs);16561657#define div4(x) ((uint8) ((x) >> 2))1657#define div4(x) ((stbi__uint8) ((x) >> 2))16581659static uint8 *resample_row_1(uint8 *out, uint8 *in_near, uint8 *in_far, int w, int hs)1659static stbi__uint8 *resample_row_1(stbi__uint8 *out, stbi__uint8 *in_near, stbi__uint8 *in_far, int w, int hs)1660{1661 STBI_NOTUSED(out);1662 STBI_NOTUSED(in_far);1663 STBI_NOTUSED(w);1664 STBI_NOTUSED(hs);1665 return in_near;1666}16671668static uint8* resample_row_v_2(uint8 *out, uint8 *in_near, uint8 *in_far, int w, int hs)1668static stbi__uint8* resample_row_v_2(stbi__uint8 *out, stbi__uint8 *in_near, stbi__uint8 *in_far, int w, int hs)1669{1670 // need to generate two samples vertically for every one in input1671 int i;1672 STBI_NOTUSED(hs);
1 unmodified line
1674 out[i] = div4(3*in_near[i] + in_far[i] + 2);1675 return out;1676}16771678static uint8* resample_row_h_2(uint8 *out, uint8 *in_near, uint8 *in_far, int w, int hs)1678static stbi__uint8* resample_row_h_2(stbi__uint8 *out, stbi__uint8 *in_near, stbi__uint8 *in_far, int w, int hs)1679{1680 // need to generate two samples horizontally for every one in input1681 int i;1682 uint8 *input = in_near;1682 stbi__uint8 *input = in_near;16831684 if (w == 1) {1685 // if only one sample, can't do any interpolation1686 out[0] = out[1] = input[0];
15 unmodified lines
17021703 return out;1704}17051706#define div16(x) ((uint8) ((x) >> 4))1706#define div16(x) ((stbi__uint8) ((x) >> 4))17071708static uint8 *resample_row_hv_2(uint8 *out, uint8 *in_near, uint8 *in_far, int w, int hs)1708static stbi__uint8 *resample_row_hv_2(stbi__uint8 *out, stbi__uint8 *in_near, stbi__uint8 *in_far, int w, int hs)1709{1710 // need to generate 2x2 samples for every one in input1711 int i,t0,t1;1712 if (w == 1) {
15 unmodified lines
17281729 return out;1730}17311732static uint8 *resample_row_generic(uint8 *out, uint8 *in_near, uint8 *in_far, int w, int hs)1732static stbi__uint8 *resample_row_generic(stbi__uint8 *out, stbi__uint8 *in_near, stbi__uint8 *in_far, int w, int hs)1733{1734 // resample with nearest-neighbor1735 int i,j;1736 in_far = in_far;1736 STBI_NOTUSED(in_far);1737 for (i=0; i < w; ++i)1738 for (j=0; j < hs; ++j)1739 out[i*hs+j] = in_near[i];1740 return out;
2 unmodified lines
1743#define float2fixed(x) ((int) ((x) * 65536 + 0.5))17441745// 0.38 seconds on 3*anemones.jpg (0.25 with processor = Pro)1746// VC6 without processor=Pro is generating multiple LEAs per multiply!1747static void YCbCr_to_RGB_row(uint8 *out, const uint8 *y, const uint8 *pcb, const uint8 *pcr, int count, int step)1747static void YCbCr_to_RGB_row(stbi__uint8 *out, const stbi__uint8 *y, const stbi__uint8 *pcb, const stbi__uint8 *pcr, int count, int step)1748{1749 int i;1750 for (i=0; i < count; ++i) {1751 int y_fixed = (y[i] << 16) + 32768; // rounding
8 unmodified lines
1760 b >>= 16;1761 if ((unsigned) r > 255) { if (r < 0) r = 0; else r = 255; }1762 if ((unsigned) g > 255) { if (g < 0) g = 0; else g = 255; }1763 if ((unsigned) b > 255) { if (b < 0) b = 0; else b = 255; }1764 out[0] = (uint8)r;1765 out[1] = (uint8)g;1766 out[2] = (uint8)b;1764 out[0] = (stbi__uint8)r;1765 out[1] = (stbi__uint8)g;1766 out[2] = (stbi__uint8)b;1767 out[3] = 255;1768 out += step;1769 }1770}
26 unmodified lines
17971798typedef struct1799{1800 resample_row_func resample;1801 uint8 *line0,*line1;1801 stbi__uint8 *line0,*line1;1802 int hs,vs; // expansion factor in each axis1803 int w_lores; // horizontal pixels pre-expansion 1804 int ystep; // how far through vertical expansion we are1805 int ypos; // which pre-expansion row we're on1806} stbi_resample;18071808static uint8 *load_jpeg_image(jpeg *z, int *out_x, int *out_y, int *comp, int req_comp)1808static stbi__uint8 *load_jpeg_image(jpeg *z, int *out_x, int *out_y, int *comp, int req_comp)1809{1810 int n, decode_n;1811 // validate req_comp1812 if (req_comp < 0 || req_comp > 4) return epuc("bad req_comp", "Internal error");
12 unmodified lines
18251826 // resample and color-convert1827 {1828 int k;1829 uint i,j;1830 uint8 *output;1831 uint8 *coutput[4];1829 unsigned int i,j;1830 stbi__uint8 *output;1831 stbi__uint8 *coutput[4];18321833 stbi_resample res_comp[4];18341835 for (k=0; k < decode_n; ++k) {1836 stbi_resample *r = &res_comp[k];18371838 // allocate line buffer big enough for upsampling off the edges1839 // with upsample factor of 41840 z->img_comp[k].linebuf = (uint8 *) malloc(z->s->img_x + 3);1840 z->img_comp[k].linebuf = (stbi__uint8 *) malloc(z->s->img_x + 3);1841 if (!z->img_comp[k].linebuf) { cleanup_jpeg(z); return epuc("outofmem", "Out of memory"); }18421843 r->hs = z->img_h_max / z->img_comp[k].h;1844 r->vs = z->img_v_max / z->img_comp[k].v;
9 unmodified lines
1854 else r->resample = resample_row_generic;1855 }18561857 // can't error after this so, this is safe1858 output = (uint8 *) malloc(n * z->s->img_x * z->s->img_y + 1);1858 output = (stbi__uint8 *) malloc(n * z->s->img_x * z->s->img_y + 1);1859 if (!output) { cleanup_jpeg(z); return epuc("outofmem", "Out of memory"); }18601861 // now go ahead and resample1862 for (j=0; j < z->s->img_y; ++j) {1863 uint8 *out = output + n * z->s->img_x * j;1863 stbi__uint8 *out = output + n * z->s->img_x * j;1864 for (k=0; k < decode_n; ++k) {1865 stbi_resample *r = &res_comp[k];1866 int y_bot = r->ystep >= (r->vs >> 1);1867 coutput[k] = r->resample(z->img_comp[k].linebuf,
7 unmodified lines
1875 r->line1 += z->img_comp[k].w2;1876 }1877 }1878 if (n >= 3) {1879 uint8 *y = coutput[0];1879 stbi__uint8 *y = coutput[0];1880 if (z->s->img_n == 3) {1881 #ifdef STBI_SIMD1882 stbi_YCbCr_installed(out, y, coutput[1], coutput[2], z->s.img_x, n);1883 #else
5 unmodified lines
1889 out[3] = 255; // not used if n==31890 out += n;1891 }1892 } else {1893 uint8 *y = coutput[0];1893 stbi__uint8 *y = coutput[0];1894 if (n == 1)1895 for (i=0; i < z->s->img_x; ++i) out[i] = y[i];1896 else1897 for (i=0; i < z->s->img_x; ++i) *out++ = y[i], *out++ = 255;
57 unmodified lines
1955// zlib-style huffman encoding1956// (jpegs packs from left, zlib from right, so can't share code)1957typedef struct1958{1959 uint16 fast[1 << ZFAST_BITS];1960 uint16 firstcode[16];1959 stbi__uint16 fast[1 << ZFAST_BITS];1960 stbi__uint16 firstcode[16];1961 int maxcode[17];1962 uint16 firstsymbol[16];1963 uint8 size[288];1964 uint16 value[288]; 1962 stbi__uint16 firstsymbol[16];1963 stbi__uint8 size[288];1964 stbi__uint16 value[288]; 1965} zhuffman;19661967stbi_inline static int bitreverse16(int n)1968{
11 unmodified lines
1980 // e.g. 11 bits, bit reverse and shift away 51981 return bitreverse16(v) >> (16-bits);1982}19831984static int zbuild_huffman(zhuffman *z, uint8 *sizelist, int num)1984static int zbuild_huffman(zhuffman *z, stbi__uint8 *sizelist, int num)1985{1986 int i,k=0;1987 int code, next_code[16], sizes[17];1988
7 unmodified lines
1996 assert(sizes[i] <= (1 << i));1997 code = 0;1998 for (i=1; i < 16; ++i) {1999 next_code[i] = code;2000 z->firstcode[i] = (uint16) code;2001 z->firstsymbol[i] = (uint16) k;2000 z->firstcode[i] = (stbi__uint16) code;2001 z->firstsymbol[i] = (stbi__uint16) k;2002 code = (code + sizes[i]);2003 if (sizes[i])2004 if (code-1 >= (1 << i)) return e("bad codelengths","Corrupt JPEG");2005 z->maxcode[i] = code << (16-i); // preshift for inner loop
4 unmodified lines
2010 for (i=0; i < num; ++i) {2011 int s = sizelist[i];2012 if (s) {2013 int c = next_code[s] - z->firstcode[s] + z->firstsymbol[s];2014 z->size[c] = (uint8)s;2015 z->value[c] = (uint16)i;2014 z->size[c] = (stbi__uint8)s;2015 z->value[c] = (stbi__uint16)i;2016 if (s <= ZFAST_BITS) {2017 int k = bit_reverse(next_code[s],s);2018 while (k < (1 << ZFAST_BITS)) {2019 z->fast[k] = (uint16) c;2019 z->fast[k] = (stbi__uint16) c;2020 k += (1 << s);2021 }2022 }2023 ++next_code[s];
9 unmodified lines
2033// memory buffer20342035typedef struct2036{2037 uint8 *zbuffer, *zbuffer_end;2037 stbi__uint8 *zbuffer, *zbuffer_end;2038 int num_bits;2039 uint32 code_buffer;2039 stbi__uint32 code_buffer;20402041 char *zout;2042 char *zout_start;2043 char *zout_end;
93 unmodified lines
2137 if (z < 0) return e("bad huffman code","Corrupt PNG"); // error in huffman codes2138 if (a->zout >= a->zout_end) if (!expand(a, 1)) return 0;2139 *a->zout++ = (char) z;2140 } else {2141 uint8 *p;2141 stbi__uint8 *p;2142 int len,dist;2143 if (z == 256) return 1;2144 z -= 257;2145 len = length_base[z];
3 unmodified lines
2149 dist = dist_base[z];2150 if (dist_extra[z]) dist += zreceive(a, dist_extra[z]);2151 if (a->zout - a->zout_start < dist) return e("bad dist","Corrupt PNG");2152 if (a->zout + len > a->zout_end) if (!expand(a, len)) return 0;2153 p = (uint8 *) (a->zout - dist);2153 p = (stbi__uint8 *) (a->zout - dist);2154 while (len--)2155 *a->zout++ = *p++;2156 }2157 }2158}21592160static int compute_huffman_codes(zbuf *a)2161{2162 static uint8 length_dezigzag[19] = { 16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15 };2162 static stbi__uint8 length_dezigzag[19] = { 16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15 };2163 zhuffman z_codelength;2164 uint8 lencodes[286+32+137];//padding for maximum single op2165 uint8 codelength_sizes[19];2164 stbi__uint8 lencodes[286+32+137];//padding for maximum single op2165 stbi__uint8 codelength_sizes[19];2166 int i,n;21672168 int hlit = zreceive(a,5) + 257;2169 int hdist = zreceive(a,5) + 1;
1 unmodified line
21712172 memset(codelength_sizes, 0, sizeof(codelength_sizes));2173 for (i=0; i < hclen; ++i) {2174 int s = zreceive(a,3);2175 codelength_sizes[length_dezigzag[i]] = (uint8) s;2175 codelength_sizes[length_dezigzag[i]] = (stbi__uint8) s;2176 }2177 if (!zbuild_huffman(&z_codelength, codelength_sizes, 19)) return 0;21782179 n = 0;2180 while (n < hlit + hdist) {2181 int c = zhuffman_decode(a, &z_codelength);2182 assert(c >= 0 && c < 19);2183 if (c < 16)2184 lencodes[n++] = (uint8) c;2184 lencodes[n++] = (stbi__uint8) c;2185 else if (c == 16) {2186 c = zreceive(a,2)+3;2187 memset(lencodes+n, lencodes[n-1], c);2188 n += c;
15 unmodified lines
2204}22052206static int parse_uncompressed_block(zbuf *a)2207{2208 uint8 header[4];2208 stbi__uint8 header[4];2209 int len,nlen,k;2210 if (a->num_bits & 7)2211 zreceive(a, a->num_bits & 7); // discard2212 // drain the bit-packed data into header2213 k = 0;2214 while (a->num_bits > 0) {2215 header[k++] = (uint8) (a->code_buffer & 255); // wtf this warns?2215 header[k++] = (stbi__uint8) (a->code_buffer & 255); // wtf this warns?2216 a->code_buffer >>= 8;2217 a->num_bits -= 8;2218 }2219 assert(a->num_bits == 0);2220 // now fill header the normal way2221 while (k < 4)2222 header[k++] = (uint8) zget8(a);2222 header[k++] = (stbi__uint8) zget8(a);2223 len = header[1] * 256 + header[0];2224 nlen = header[3] * 256 + header[2];2225 if (nlen != (len ^ 0xffff)) return e("zlib corrupt","Corrupt PNG");2226 if (a->zbuffer + len > a->zbuffer_end) return e("read past buffer","Corrupt PNG");
18 unmodified lines
2245 return 1;2246}22472248// @TODO: should statically initialize these for optimal thread safety2249static uint8 default_length[288], default_distance[32];2249static stbi__uint8 default_length[288], default_distance[32];2250static void init_defaults(void)2251{2252 int i; // use <= to match clearly with spec2253 for (i=0; i <= 143; ++i) default_length[i] = 8;
50 unmodified lines
2304{2305 zbuf a;2306 char *p = (char *) malloc(initial_size);2307 if (p == NULL) return NULL;2308 a.zbuffer = (uint8 *) buffer;2309 a.zbuffer_end = (uint8 *) buffer + len;2308 a.zbuffer = (stbi__uint8 *) buffer;2309 a.zbuffer_end = (stbi__uint8 *) buffer + len;2310 if (do_zlib(&a, p, initial_size, 1, 1)) {2311 if (outlen) *outlen = (int) (a.zout - a.zout_start);2312 return a.zout_start;2313 } else {
11 unmodified lines
2325{2326 zbuf a;2327 char *p = (char *) malloc(initial_size);2328 if (p == NULL) return NULL;2329 a.zbuffer = (uint8 *) buffer;2330 a.zbuffer_end = (uint8 *) buffer + len;2329 a.zbuffer = (stbi__uint8 *) buffer;2330 a.zbuffer_end = (stbi__uint8 *) buffer + len;2331 if (do_zlib(&a, p, initial_size, 1, parse_header)) {2332 if (outlen) *outlen = (int) (a.zout - a.zout_start);2333 return a.zout_start;2334 } else {
4 unmodified lines
23392340int stbi_zlib_decode_buffer(char *obuffer, int olen, char const *ibuffer, int ilen)2341{2342 zbuf a;2343 a.zbuffer = (uint8 *) ibuffer;2344 a.zbuffer_end = (uint8 *) ibuffer + ilen;2343 a.zbuffer = (stbi__uint8 *) ibuffer;2344 a.zbuffer_end = (stbi__uint8 *) ibuffer + ilen;2345 if (do_zlib(&a, obuffer, olen, 0, 1))2346 return (int) (a.zout - a.zout_start);2347 else2348 return -1;
3 unmodified lines
2352{2353 zbuf a;2354 char *p = (char *) malloc(16384);2355 if (p == NULL) return NULL;2356 a.zbuffer = (uint8 *) buffer;2357 a.zbuffer_end = (uint8 *) buffer+len;2356 a.zbuffer = (stbi__uint8 *) buffer;2357 a.zbuffer_end = (stbi__uint8 *) buffer+len;2358 if (do_zlib(&a, p, 16384, 1, 0)) {2359 if (outlen) *outlen = (int) (a.zout - a.zout_start);2360 return a.zout_start;2361 } else {
4 unmodified lines
23662367int stbi_zlib_decode_noheader_buffer(char *obuffer, int olen, const char *ibuffer, int ilen)2368{2369 zbuf a;2370 a.zbuffer = (uint8 *) ibuffer;2371 a.zbuffer_end = (uint8 *) ibuffer + ilen;2370 a.zbuffer = (stbi__uint8 *) ibuffer;2371 a.zbuffer_end = (stbi__uint8 *) ibuffer + ilen;2372 if (do_zlib(&a, obuffer, olen, 0, 0))2373 return (int) (a.zout - a.zout_start);2374 else2375 return -1;
11 unmodified lines
238723882389typedef struct2390{2391 uint32 length;2392 uint32 type;2391 stbi__uint32 length;2392 stbi__uint32 type;2393} chunk;23942395#define PNG_TYPE(a,b,c,d) (((a) << 24) + ((b) << 16) + ((c) << 8) + (d))2396
6 unmodified lines
2403}24042405static int check_png_header(stbi *s)2406{2407 static uint8 png_sig[8] = { 137,80,78,71,13,10,26,10 };2407 static stbi__uint8 png_sig[8] = { 137,80,78,71,13,10,26,10 };2408 int i;2409 for (i=0; i < 8; ++i)2410 if (get8u(s) != png_sig[i]) return e("bad png sig","Not a PNG");2411 return 1;
1 unmodified line
24132414typedef struct2415{2416 stbi *s;2417 uint8 *idata, *expanded, *out;2417 stbi__uint8 *idata, *expanded, *out;2418} png;241924202421enum {2422 F_none=0, F_sub=1, F_up=2, F_avg=3, F_paeth=4,2423 F_avg_first, F_paeth_first2424};24252426static uint8 first_row_filter[5] =2426static stbi__uint8 first_row_filter[5] =2427{2428 F_none, F_sub, F_none, F_avg_first, F_paeth_first2429};2430
8 unmodified lines
2439 return c;2440}24412442// create the png data from post-deflated data2443static int create_png_image_raw(png *a, uint8 *raw, uint32 raw_len, int out_n, uint32 x, uint32 y)2443static int create_png_image_raw(png *a, stbi__uint8 *raw, stbi__uint32 raw_len, int out_n, stbi__uint32 x, stbi__uint32 y)2444{2445 stbi *s = a->s;2446 uint32 i,j,stride = x*out_n;2446 stbi__uint32 i,j,stride = x*out_n;2447 int k;2448 int img_n = s->img_n; // copy it into a local for later2449 assert(out_n == s->img_n || out_n == s->img_n+1);2450 if (stbi_png_partial) y = 1;2451 a->out = (uint8 *) malloc(x * y * out_n);2451 a->out = (stbi__uint8 *) malloc(x * y * out_n);2452 if (!a->out) return e("outofmem", "Out of memory");2453 if (!stbi_png_partial) {2454 if (s->img_x == x && s->img_y == y) {2455 if (raw_len != (img_n * x + 1) * y) return e("not enough pixels","Corrupt PNG");
1 unmodified line
2457 if (raw_len < (img_n * x + 1) * y) return e("not enough pixels","Corrupt PNG");2458 }2459 }2460 for (j=0; j < y; ++j) {2461 uint8 *cur = a->out + stride*j;2462 uint8 *prior = cur - stride;2461 stbi__uint8 *cur = a->out + stride*j;2462 stbi__uint8 *prior = cur - stride;2463 int filter = *raw++;2464 if (filter > 4) return e("invalid filter","Corrupt PNG");2465 // if first row, use special filter that doesn't sample previous row2466 if (j == 0) filter = first_row_filter[filter];
3 unmodified lines
2470 case F_none : cur[k] = raw[k]; break;2471 case F_sub : cur[k] = raw[k]; break;2472 case F_up : cur[k] = raw[k] + prior[k]; break;2473 case F_avg : cur[k] = raw[k] + (prior[k]>>1); break;2474 case F_paeth : cur[k] = (uint8) (raw[k] + paeth(0,prior[k],0)); break;2474 case F_paeth : cur[k] = (stbi__uint8) (raw[k] + paeth(0,prior[k],0)); break;2475 case F_avg_first : cur[k] = raw[k]; break;2476 case F_paeth_first: cur[k] = raw[k]; break;2477 }2478 }
11 unmodified lines
2490 CASE(F_none) cur[k] = raw[k]; break;2491 CASE(F_sub) cur[k] = raw[k] + cur[k-img_n]; break;2492 CASE(F_up) cur[k] = raw[k] + prior[k]; break;2493 CASE(F_avg) cur[k] = raw[k] + ((prior[k] + cur[k-img_n])>>1); break;2494 CASE(F_paeth) cur[k] = (uint8) (raw[k] + paeth(cur[k-img_n],prior[k],prior[k-img_n])); break;2494 CASE(F_paeth) cur[k] = (stbi__uint8) (raw[k] + paeth(cur[k-img_n],prior[k],prior[k-img_n])); break;2495 CASE(F_avg_first) cur[k] = raw[k] + (cur[k-img_n] >> 1); break;2496 CASE(F_paeth_first) cur[k] = (uint8) (raw[k] + paeth(cur[k-img_n],0,0)); break;2496 CASE(F_paeth_first) cur[k] = (stbi__uint8) (raw[k] + paeth(cur[k-img_n],0,0)); break;2497 }2498 #undef CASE2499 } else {2500 assert(img_n+1 == out_n);
5 unmodified lines
2506 CASE(F_none) cur[k] = raw[k]; break;2507 CASE(F_sub) cur[k] = raw[k] + cur[k-out_n]; break;2508 CASE(F_up) cur[k] = raw[k] + prior[k]; break;2509 CASE(F_avg) cur[k] = raw[k] + ((prior[k] + cur[k-out_n])>>1); break;2510 CASE(F_paeth) cur[k] = (uint8) (raw[k] + paeth(cur[k-out_n],prior[k],prior[k-out_n])); break;2510 CASE(F_paeth) cur[k] = (stbi__uint8) (raw[k] + paeth(cur[k-out_n],prior[k],prior[k-out_n])); break;2511 CASE(F_avg_first) cur[k] = raw[k] + (cur[k-out_n] >> 1); break;2512 CASE(F_paeth_first) cur[k] = (uint8) (raw[k] + paeth(cur[k-out_n],0,0)); break;2512 CASE(F_paeth_first) cur[k] = (stbi__uint8) (raw[k] + paeth(cur[k-out_n],0,0)); break;2513 }2514 #undef CASE2515 }2516 }2517 return 1;2518}25192520static int create_png_image(png *a, uint8 *raw, uint32 raw_len, int out_n, int interlaced)2520static int create_png_image(png *a, stbi__uint8 *raw, stbi__uint32 raw_len, int out_n, int interlaced)2521{2522 uint8 *final;2522 stbi__uint8 *final;2523 int p;2524 int save;2525 if (!interlaced)2526 return create_png_image_raw(a, raw, raw_len, out_n, a->s->img_x, a->s->img_y);2527 save = stbi_png_partial;2528 stbi_png_partial = 0;25292530 // de-interlacing2531 final = (uint8 *) malloc(a->s->img_x * a->s->img_y * out_n);2531 final = (stbi__uint8 *) malloc(a->s->img_x * a->s->img_y * out_n);2532 for (p=0; p < 7; ++p) {2533 int xorig[] = { 0,4,0,2,0,1,0 };2534 int yorig[] = { 0,0,4,0,2,0,1 };2535 int xspc[] = { 8,8,4,4,2,2,1 };
21 unmodified lines
2557 stbi_png_partial = save;2558 return 1;2559}25602561static int compute_transparency(png *z, uint8 tc[3], int out_n)2561static int compute_transparency(png *z, stbi__uint8 tc[3], int out_n)2562{2563 stbi *s = z->s;2564 uint32 i, pixel_count = s->img_x * s->img_y;2565 uint8 *p = z->out;2564 stbi__uint32 i, pixel_count = s->img_x * s->img_y;2565 stbi__uint8 *p = z->out;25662567 // compute color-based transparency, assuming we've2568 // already got 255 as the alpha value in the output2569 assert(out_n == 2 || out_n == 4);
12 unmodified lines
2582 }2583 return 1;2584}25852586static int expand_palette(png *a, uint8 *palette, int len, int pal_img_n)2586static int expand_palette(png *a, stbi__uint8 *palette, int len, int pal_img_n)2587{2588 uint32 i, pixel_count = a->s->img_x * a->s->img_y;2589 uint8 *p, *temp_out, *orig = a->out;2588 stbi__uint32 i, pixel_count = a->s->img_x * a->s->img_y;2589 stbi__uint8 *p, *temp_out, *orig = a->out;25902591 p = (uint8 *) malloc(pixel_count * pal_img_n);2591 p = (stbi__uint8 *) malloc(pixel_count * pal_img_n);2592 if (p == NULL) return e("outofmem", "Out of memory");25932594 // between here and free(out) below, exitting would leak2595 temp_out = p;
38 unmodified lines
26342635static void stbi_de_iphone(png *z)2636{2637 stbi *s = z->s;2638 uint32 i, pixel_count = s->img_x * s->img_y;2639 uint8 *p = z->out;2638 stbi__uint32 i, pixel_count = s->img_x * s->img_y;2639 stbi__uint8 *p = z->out;26402641 if (s->img_out_n == 3) { // convert bgr to rgb2642 for (i=0; i < pixel_count; ++i) {2643 uint8 t = p[0];2643 stbi__uint8 t = p[0];2644 p[0] = p[2];2645 p[2] = t;2646 p += 3;2647 }
1 unmodified line
2649 assert(s->img_out_n == 4);2650 if (stbi_unpremultiply_on_load) {2651 // convert bgr to rgb and unpremultiply2652 for (i=0; i < pixel_count; ++i) {2653 uint8 a = p[3];2654 uint8 t = p[0];2653 stbi__uint8 a = p[3];2654 stbi__uint8 t = p[0];2655 if (a) {2656 p[0] = p[2] * 255 / a;2657 p[1] = p[1] * 255 / a;2658 p[2] = t * 255 / a;
5 unmodified lines
2664 }2665 } else {2666 // convert bgr to rgb2667 for (i=0; i < pixel_count; ++i) {2668 uint8 t = p[0];2668 stbi__uint8 t = p[0];2669 p[0] = p[2];2670 p[2] = t;2671 p += 4;2672 }
2 unmodified lines
2675}26762677static int parse_png_file(png *z, int scan, int req_comp)2678{2679 uint8 palette[1024], pal_img_n=0;2680 uint8 has_trans=0, tc[3];2681 uint32 ioff=0, idata_limit=0, i, pal_len=0;2679 stbi__uint8 palette[1024], pal_img_n=0;2680 stbi__uint8 has_trans=0, tc[3];2681 stbi__uint32 ioff=0, idata_limit=0, i, pal_len=0;2682 int first=1,k,interlace=0, iphone=0;2683 stbi *s = z->s;26842685 z->expanded = NULL;
64 unmodified lines
2750 for (i=0; i < c.length; ++i)2751 palette[i*4+3] = get8u(s);2752 } else {2753 if (!(s->img_n & 1)) return e("tRNS with alpha","Corrupt PNG");2754 if (c.length != (uint32) s->img_n*2) return e("bad tRNS len","Corrupt PNG");2754 if (c.length != (stbi__uint32) s->img_n*2) return e("bad tRNS len","Corrupt PNG");2755 has_trans = 1;2756 for (k=0; k < s->img_n; ++k)2757 tc[k] = (uint8) get16(s); // non 8-bit images will be larger2757 tc[k] = (stbi__uint8) get16(s); // non 8-bit images will be larger2758 }2759 break;2760 }2761
1 unmodified line
2763 if (first) return e("first not IHDR", "Corrupt PNG");2764 if (pal_img_n && !pal_len) return e("no PLTE","Corrupt PNG");2765 if (scan == SCAN_header) { s->img_n = pal_img_n; return 1; }2766 if (ioff + c.length > idata_limit) {2767 uint8 *p;2767 stbi__uint8 *p;2768 if (idata_limit == 0) idata_limit = c.length > 4096 ? c.length : 4096;2769 while (ioff + c.length > idata_limit)2770 idata_limit *= 2;2771 p = (uint8 *) realloc(z->idata, idata_limit); if (p == NULL) return e("outofmem", "Out of memory");2771 p = (stbi__uint8 *) realloc(z->idata, idata_limit); if (p == NULL) return e("outofmem", "Out of memory");2772 z->idata = p;2773 }2774 if (!getn(s, z->idata+ioff,c.length)) return e("outofdata","Corrupt PNG");2775 ioff += c.length;2776 break;2777 }27782779 case PNG_TYPE('I','E','N','D'): {2780 uint32 raw_len;2780 stbi__uint32 raw_len;2781 if (first) return e("first not IHDR", "Corrupt PNG");2782 if (scan != SCAN_load) return 1;2783 if (z->idata == NULL) return e("no IDAT","Corrupt PNG");2784 z->expanded = (uint8 *) stbi_zlib_decode_malloc_guesssize_headerflag((char *) z->idata, ioff, 16384, (int *) &raw_len, !iphone);2784 z->expanded = (stbi__uint8 *) stbi_zlib_decode_malloc_guesssize_headerflag((char *) z->idata, ioff, 16384, (int *) &raw_len, !iphone);2785 if (z->expanded == NULL) return 0; // zlib should set error2786 free(z->idata); z->idata = NULL;2787 if ((req_comp == s->img_n+1 && req_comp != 3 && !pal_img_n) || has_trans)2788 s->img_out_n = s->img_n+1;
22 unmodified lines
2811 if ((c.type & (1 << 29)) == 0) {2812 #ifndef STBI_NO_FAILURE_STRINGS2813 // not threadsafe2814 static char invalid_chunk[] = "XXXX chunk not known";2815 invalid_chunk[0] = (uint8) (c.type >> 24);2816 invalid_chunk[1] = (uint8) (c.type >> 16);2817 invalid_chunk[2] = (uint8) (c.type >> 8);2818 invalid_chunk[3] = (uint8) (c.type >> 0);2815 invalid_chunk[0] = (stbi__uint8) (c.type >> 24);2816 invalid_chunk[1] = (stbi__uint8) (c.type >> 16);2817 invalid_chunk[2] = (stbi__uint8) (c.type >> 8);2818 invalid_chunk[3] = (stbi__uint8) (c.type >> 0);2819 #endif2820 return e(invalid_chunk, "PNG not supported: unknown chunk type");2821 }2822 skip(s, c.length);
126 unmodified lines
2949}29502951static stbi_uc *bmp_load(stbi *s, int *x, int *y, int *comp, int req_comp)2952{2953 uint8 *out;2953 stbi__uint8 *out;2954 unsigned int mr=0,mg=0,mb=0,ma=0, fake_a=0;2955 stbi_uc pal[256][4];2956 int psize=0,i,j,compress=0,width;2957 int bpp, flip_vertically, pad, target, offset, hsz;
146 unmodified lines
3104 out[z+1] = get8u(s);3105 out[z+0] = get8u(s);3106 z += 3;3107 a = (easy == 2 ? get8(s) : 255);3108 if (target == 4) out[z++] = (uint8) a;3108 if (target == 4) out[z++] = (stbi__uint8) a;3109 }3110 } else {3111 for (i=0; i < (int) s->img_x; ++i) {3112 uint32 v = (bpp == 16 ? get16le(s) : get32le(s));3112 stbi__uint32 v = (bpp == 16 ? get16le(s) : get32le(s));3113 int a;3114 out[z++] = (uint8) shiftsigned(v & mr, rshift, rcount);3115 out[z++] = (uint8) shiftsigned(v & mg, gshift, gcount);3116 out[z++] = (uint8) shiftsigned(v & mb, bshift, bcount);3114 out[z++] = (stbi__uint8) shiftsigned(v & mr, rshift, rcount);3115 out[z++] = (stbi__uint8) shiftsigned(v & mg, gshift, gcount);3116 out[z++] = (stbi__uint8) shiftsigned(v & mb, bshift, bcount);3117 a = (ma ? shiftsigned(v & ma, ashift, acount) : 255);3118 if (target == 4) out[z++] = (uint8) a; 3118 if (target == 4) out[z++] = (stbi__uint8) a; 3119 }3120 }3121 skip(s, pad);3122 }
174 unmodified lines
3297 // any data to skip? (offset usually = 0)3298 skip(s, tga_palette_start );3299 // load the palette3300 tga_palette = (unsigned char*)malloc( tga_palette_len * tga_palette_bits / 8 );3301 if (!tga_palette) return epuc("outofmem", "Out of memory");3301 if (!tga_palette) {3302 free(tga_data);3303 return epuc("outofmem", "Out of memory");3304 }3305 if (!getn(s, tga_palette, tga_palette_len * tga_palette_bits / 8 )) {3306 free(tga_data);3307 free(tga_palette);3308 return epuc("bad palette", "Corrupt TGA");
168 unmodified lines
3477 int pixelCount;3478 int channelCount, compression;3479 int channel, i, count, len;3480 int w,h;3478 uint8 *out;3481 stbi__uint8 *out;34823483 // Check identifier3484 if (get32(s) != 0x38425053) // "8BPS"3485 return epuc("not PSD", "Corrupt PSD image");
71 unmodified lines
3557 skip(s, h * channelCount * 2 );35583559 // Read the RLE data by channel.3560 for (channel = 0; channel < 4; channel++) {3558 uint8 *p;3561 stbi__uint8 *p;3562 3563 p = out+channel;3564 if (channel >= channelCount) {3565 // Fill this channel with default data.
14 unmodified lines
3580 p += 4;3581 len--;3582 }3583 } else if (len > 128) {3581 uint8 val;3584 stbi__uint8 val;3585 // Next -len+1 bytes in the dest are replicated from next source byte.3586 // (Interpret len as a negative 8-bit int.)3587 len ^= 0x0FF;3588 len += 2;
14 unmodified lines
3603 // where each channel consists of an 8-bit value for each pixel in the image.3604 3605 // Read the data by channel.3606 for (channel = 0; channel < 4; channel++) {3604 uint8 *p;3607 stbi__uint8 *p;3608 3609 p = out + channel;3610 if (channel > channelCount) {3611 // Fill this channel with default data.
142 unmodified lines
3754 count=get8u(s);3755 if (at_eof(s)) return epuc("bad file","file too short (pure read count)");37563757 if (count > left)3755 count = (uint8) left;3758 count = (stbi__uint8) left;37593760 if (!pic_readval(s,packet->channel,value)) return 0;37613762 for(i=0; i<count; ++i,dest+=4)
91 unmodified lines
38543855// *************************************************************************************************3856// GIF loader -- public domain by Jean-Marc Lienher -- simplified/shrunk by stb3857typedef struct stbi_gif_lzw_struct {3855 int16 prefix;3856 uint8 first;3857 uint8 suffix;3858 stbi__int16 prefix;3859 stbi__uint8 first;3860 stbi__uint8 suffix;3861} stbi_gif_lzw;38623863typedef struct stbi_gif_struct3864{3865 int w,h;3866 stbi_uc *out; // output buffer (always 4 components)3867 int flags, bgindex, ratio, transparent, eflags;3865 uint8 pal[256][4];3866 uint8 lpal[256][4];3868 stbi__uint8 pal[256][4];3869 stbi__uint8 lpal[256][4];3870 stbi_gif_lzw codes[4096];3868 uint8 *color_table;3871 stbi__uint8 *color_table;3872 int parse, step;3873 int lflags;3874 int start_x, start_y;3875 int max_x, max_y;
17 unmodified lines
3893 stbi_rewind(s);3894 return r;3895}38963894static void stbi_gif_parse_colortable(stbi *s, uint8 pal[256][4], int num_entries, int transp)3897static void stbi_gif_parse_colortable(stbi *s, stbi__uint8 pal[256][4], int num_entries, int transp)3898{3899 int i;3900 for (i=0; i < num_entries; ++i) {3901 pal[i][2] = get8u(s);
4 unmodified lines
3906}39073908static int stbi_gif_header(stbi *s, stbi_gif *g, int *comp, int is_info)3909{3907 uint8 version;3910 stbi__uint8 version;3911 if (get8(s) != 'G' || get8(s) != 'I' || get8(s) != 'F' || get8(s) != '8')3912 return e("not GIF", "Corrupt GIF");39133914 version = get8u(s);
29 unmodified lines
3944 if (y) *y = g.h;3945 return 1;3946}39473945static void stbi_out_gif_code(stbi_gif *g, uint16 code)3948static void stbi_out_gif_code(stbi_gif *g, stbi__uint16 code)3949{3947 uint8 *p, *c;3950 stbi__uint8 *p, *c;39513952 // recurse to decode the prefixes, since the linked-list is backwards,3953 // and working backwards through an interleaved image would be nasty3954 if (g->codes[code].prefix >= 0)
23 unmodified lines
3978 }3979 }3980}39813979static uint8 *stbi_process_gif_raster(stbi *s, stbi_gif *g)3982static stbi__uint8 *stbi_process_gif_raster(stbi *s, stbi_gif *g)3983{3981 uint8 lzw_cs;3982 int32 len, code;3983 uint32 first;3984 int32 codesize, codemask, avail, oldcode, bits, valid_bits, clear;3984 stbi__uint8 lzw_cs;3985 stbi__int32 len, code;3986 stbi__uint32 first;3987 stbi__int32 codesize, codemask, avail, oldcode, bits, valid_bits, clear;3988 stbi_gif_lzw *p;39893990 lzw_cs = get8u(s);3991 clear = 1 << lzw_cs;
3 unmodified lines
3995 bits = 0;3996 valid_bits = 0;3997 for (code = 0; code < clear; code++) {3998 g->codes[code].prefix = -1;3996 g->codes[code].first = (uint8) code;3997 g->codes[code].suffix = (uint8) code;3999 g->codes[code].first = (stbi__uint8) code;4000 g->codes[code].suffix = (stbi__uint8) code;4001 }40024003 // support no starting clear code4004 avail = clear+2;
7 unmodified lines
4012 if (len == 0) 4013 return g->out;4014 }4015 --len;4013 bits |= (int32) get8(s) << valid_bits;4016 bits |= (stbi__int32) get8(s) << valid_bits;4017 valid_bits += 8;4018 } else {4016 int32 code = bits & codemask;4019 stbi__int32 code = bits & codemask;4020 bits >>= codesize;4021 valid_bits -= codesize;4022 // @OPTIMIZE: is there some way we can accelerate the non-clear path?4023 if (code == clear) { // clear code
12 unmodified lines
40364037 if (oldcode >= 0) {4038 p = &g->codes[avail++];4039 if (avail > 4096) return epuc("too many codes", "Corrupt GIF");4037 p->prefix = (int16) oldcode;4040 p->prefix = (stbi__int16) oldcode;4041 p->first = g->codes[oldcode].first;4042 p->suffix = (code == avail) ? p->first : g->codes[code].first;4043 } else if (code == avail)4044 return epuc("illegal code in raster", "Corrupt GIF");40454043 stbi_out_gif_code(g, (uint16) code);4046 stbi_out_gif_code(g, (stbi__uint16) code);40474048 if ((avail & codemask) == 0 && avail <= 0x0FFF) {4049 codesize++;4050 codemask = (1 << codesize) - 1;
9 unmodified lines
40604061static void stbi_fill_gif_background(stbi_gif *g)4062{4063 int i;4061 uint8 *c = g->pal[g->bgindex];4064 stbi__uint8 *c = g->pal[g->bgindex];4065 // @OPTIMIZE: write a dword at a time4066 for (i = 0; i < g->w * g->h * 4; i += 4) {4064 uint8 *p = &g->out[i];4067 stbi__uint8 *p = &g->out[i];4068 p[0] = c[2];4069 p[1] = c[1];4070 p[2] = c[0];4071 p[3] = c[3];4072 }4073}40744075// this function is designed to support animated gifs, although stb_image doesn't support it4073static uint8 *stbi_gif_load_next(stbi *s, stbi_gif *g, int *comp, int req_comp)4076static stbi__uint8 *stbi_gif_load_next(stbi *s, stbi_gif *g, int *comp, int req_comp)4077{4078 int i;4076 uint8 *old_out = 0;4079 stbi__uint8 *old_out = 0;40804081 if (g->out == 0) {4082 if (!stbi_gif_header(s, g, comp,0)) return 0; // failure_reason set by stbi_gif_header4080 g->out = (uint8 *) malloc(4 * g->w * g->h);4083 g->out = (stbi__uint8 *) malloc(4 * g->w * g->h);4084 if (g->out == 0) return epuc("outofmem", "Out of memory");4085 stbi_fill_gif_background(g);4086 } else {4087 // animated-gif-only path4088 if (((g->eflags & 0x1C) >> 2) == 3) {4089 old_out = g->out;4087 g->out = (uint8 *) malloc(4 * g->w * g->h);4090 g->out = (stbi__uint8 *) malloc(4 * g->w * g->h);4091 if (g->out == 0) return epuc("outofmem", "Out of memory");4092 memcpy(g->out, old_out, g->w*g->h*4);4093 }4094 }
1 unmodified line
4096 for (;;) {4097 switch (get8(s)) {4098 case 0x2C: /* Image Descriptor */4099 {4097 int32 x, y, w, h;4098 uint8 *o;4100 stbi__int32 x, y, w, h;4101 stbi__uint8 *o;41024103 x = get16le(s);4104 y = get16le(s);4105 w = get16le(s);
20 unmodified lines
4126 }41274128 if (g->lflags & 0x80) {4129 stbi_gif_parse_colortable(s,g->lpal, 2 << (g->lflags & 7), g->eflags & 0x01 ? g->transparent : -1);4127 g->color_table = (uint8 *) g->lpal; 4130 g->color_table = (stbi__uint8 *) g->lpal; 4131 } else if (g->flags & 0x80) {4132 for (i=0; i < 256; ++i) // @OPTIMIZE: reset only the previous transparent4133 g->pal[i][3] = 255; 4134 if (g->transparent >= 0 && (g->eflags & 0x01))4135 g->pal[g->transparent][3] = 0;4133 g->color_table = (uint8 *) g->pal;4136 g->color_table = (stbi__uint8 *) g->pal;4137 } else4138 return epuc("missing color table", "Corrupt GIF");4139 4140 o = stbi_process_gif_raster(s, g);
23 unmodified lines
4164 break;4165 }41664167 case 0x3B: // gif stream termination code4165 return (uint8 *) 1;4168 return (stbi__uint8 *) 1;41694170 default:4171 return epuc("unknown code", "Corrupt GIF");4172 }
1 unmodified line
4174}41754176static stbi_uc *stbi_gif_load(stbi *s, int *x, int *y, int *comp, int req_comp)4177{4175 uint8 *u = 0;4178 stbi__uint8 *u = 0;4179 stbi_gif g={0};41804181 u = stbi_gif_load_next(s, &g, comp, req_comp);4182 if (u == (void *) 1) u = 0; // end of animated gif marker
150 unmodified lines
4333 len = get8(s);4334 if (c1 != 2 || c2 != 2 || (len & 0x80)) {4335 // not run-length encoded, so we have to actually use THIS data as a decoded4336 // pixel (note this can't be a valid pixel--one of RGB must be >= 128)4334 uint8 rgbe[4];4335 rgbe[0] = (uint8) c1;4336 rgbe[1] = (uint8) c2;4337 rgbe[2] = (uint8) len;4338 rgbe[3] = (uint8) get8u(s);4337 stbi__uint8 rgbe[4];4338 rgbe[0] = (stbi__uint8) c1;4339 rgbe[1] = (stbi__uint8) c2;4340 rgbe[2] = (stbi__uint8) len;4341 rgbe[3] = (stbi__uint8) get8u(s);4342 hdr_convert(hdr_data, rgbe, req_comp);4343 i = 1;4344 j = 0;4345 free(scanline);
243 unmodified lines
4589#endif // STBI_HEADER_FILE_ONLY45904591/*4592 revision history:4593 1.35 (2014-05-27)4594 1.34 (unknown)4595 use STBI_NOTUSED in resample_row_generic(), fix one more leak in tga failure case4596 1.33 (2011-07-14)4597 make stbi_is_hdr work in STBI_NO_HDR (as specified), minor compiler-friendly improvements4598 1.32 (2011-07-13)4599 support for "info" function for all supported filetypes (SpartanJ)
9 unmodified lines
4609 various warning fixes from Aurelien Pocheville 4610 1.28 (2010-08-01)4611 fix bug in GIF palette transparency (SpartanJ)4612 1.27 (2010-08-01)4607 cast-to-uint8 to fix warnings4613 cast-to-stbi__uint8 to fix warnings4614 1.26 (2010-07-24)4615 fix bug in file buffering for PNG reported by SpartanJ4616 1.25 (2010-07-17)4617 refix trans_data warning (Won Chun)
8 unmodified lines
4626 stbi_info support from Jetro Lauha4627 GIF support from Jean-Marc Lienher4628 iPhone PNG-extensions from James Brown4629 warning-fixes from Nicolas Schulz and Janez Zemva (i.e. Janez (U+017D)emva)4624 1.21 fix use of 'uint8' in header (reported by jon blow)4630 1.21 fix use of 'stbi__uint8' in header (reported by jon blow)4631 1.20 added support for Softimage PIC, by Tom Seddon4632 1.19 bug in interlaced PNG corruption check (found by ryg)4633 1.18 2008-08-024634 fix a threading bug (local mutable static)
45 unmodified lines