stb_png.h 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201
  1. /* PNG decompressor from
  2. stb_image - v2.23 - public domain image loader - http://nothings.org/stb_image.h
  3. */
  4. #ifndef STBI_INCLUDE_STB_IMAGE_H
  5. #define STBI_INCLUDE_STB_IMAGE_H
  6. #include <stdlib.h>
  7. #include <string.h>
  8. enum
  9. {
  10. STBI_default = 0,
  11. STBI_grey = 1,
  12. STBI_grey_alpha = 2,
  13. STBI_rgb = 3,
  14. STBI_rgb_alpha = 4
  15. };
  16. enum
  17. {
  18. STBI__SCAN_load=0,
  19. STBI__SCAN_type,
  20. STBI__SCAN_header
  21. };
  22. typedef unsigned short stbi_us;
  23. typedef uint16_t stbi__uint16;
  24. typedef int16_t stbi__int16;
  25. typedef uint32_t stbi__uint32;
  26. typedef int32_t stbi__int32;
  27. typedef struct
  28. {
  29. stbi__uint32 img_x, img_y;
  30. int img_n, img_out_n;
  31. void *io_user_data;
  32. int read_from_callbacks;
  33. int buflen;
  34. unsigned char buffer_start[128];
  35. unsigned char *img_buffer, *img_buffer_end;
  36. unsigned char *img_buffer_original, *img_buffer_original_end;
  37. } stbi__context;
  38. typedef struct
  39. {
  40. int bits_per_channel;
  41. int num_channels;
  42. int channel_order;
  43. } stbi__result_info;
  44. void *stbi__png_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri);
  45. char *stbi_zlib_decode_malloc_guesssize_headerflag(const char *buffer, int len, int initial_size, int *outlen, int parse_header);
  46. #ifdef STBI_IMPLEMENTATION
  47. static const char *stbi__g_failure_reason;
  48. #define STBI_ASSERT(v)
  49. #define STBI_NOTUSED(v) (void)sizeof(v)
  50. #define STBI__BYTECAST(x) ((unsigned char) ((x) & 255))
  51. #define STBI_MALLOC(sz) malloc(sz)
  52. #define STBI_REALLOC(p,newsz) realloc(p,newsz)
  53. #define STBI_FREE(p) free(p)
  54. #define STBI_REALLOC_SIZED(p,oldsz,newsz) realloc(p,newsz)
  55. __inline__ static unsigned char stbi__get8(stbi__context *s)
  56. {
  57. if (s->img_buffer < s->img_buffer_end)
  58. return *s->img_buffer++;
  59. return 0;
  60. }
  61. __inline__ static int stbi__at_eof(stbi__context *s)
  62. {
  63. return s->img_buffer >= s->img_buffer_end;
  64. }
  65. static void stbi__skip(stbi__context *s, int n)
  66. {
  67. if (n < 0) {
  68. s->img_buffer = s->img_buffer_end;
  69. return;
  70. }
  71. s->img_buffer += n;
  72. }
  73. static int stbi__getn(stbi__context *s, unsigned char *buffer, int n)
  74. {
  75. if (s->img_buffer+n <= s->img_buffer_end) {
  76. memcpy(buffer, s->img_buffer, n);
  77. s->img_buffer += n;
  78. return 1;
  79. } else
  80. return 0;
  81. }
  82. static int stbi__get16be(stbi__context *s)
  83. {
  84. int z = stbi__get8(s);
  85. return (z << 8) + stbi__get8(s);
  86. }
  87. static stbi__uint32 stbi__get32be(stbi__context *s)
  88. {
  89. stbi__uint32 z = stbi__get16be(s);
  90. return (z << 16) + stbi__get16be(s);
  91. }
  92. #define stbi__err(x,y) stbi__errstr(y)
  93. static int stbi__errstr(const char *str)
  94. {
  95. stbi__g_failure_reason = str;
  96. return 0;
  97. }
  98. __inline__ static void *stbi__malloc(size_t size)
  99. {
  100. return STBI_MALLOC(size);
  101. }
  102. static int stbi__addsizes_valid(int a, int b)
  103. {
  104. if (b < 0) return 0;
  105. return a <= 2147483647 - b;
  106. }
  107. static int stbi__mul2sizes_valid(int a, int b)
  108. {
  109. if (a < 0 || b < 0) return 0;
  110. if (b == 0) return 1;
  111. return a <= 2147483647/b;
  112. }
  113. static int stbi__mad2sizes_valid(int a, int b, int add)
  114. {
  115. return stbi__mul2sizes_valid(a, b) && stbi__addsizes_valid(a*b, add);
  116. }
  117. static int stbi__mad3sizes_valid(int a, int b, int c, int add)
  118. {
  119. return stbi__mul2sizes_valid(a, b) && stbi__mul2sizes_valid(a*b, c) &&
  120. stbi__addsizes_valid(a*b*c, add);
  121. }
  122. static void *stbi__malloc_mad2(int a, int b, int add)
  123. {
  124. if (!stbi__mad2sizes_valid(a, b, add)) return NULL;
  125. return stbi__malloc(a*b + add);
  126. }
  127. static void *stbi__malloc_mad3(int a, int b, int c, int add)
  128. {
  129. if (!stbi__mad3sizes_valid(a, b, c, add)) return NULL;
  130. return stbi__malloc(a*b*c + add);
  131. }
  132. static unsigned char stbi__compute_y(int r, int g, int b)
  133. {
  134. return (unsigned char) (((r*77) + (g*150) + (29*b)) >> 8);
  135. }
  136. static unsigned char *stbi__convert_format(unsigned char *data, int img_n, int req_comp, unsigned int x, unsigned int y)
  137. {
  138. int i,j;
  139. unsigned char *good;
  140. if (req_comp == img_n) return data;
  141. STBI_ASSERT(req_comp >= 1 && req_comp <= 4);
  142. good = (unsigned char *) stbi__malloc_mad3(req_comp, x, y, 0);
  143. if (good == NULL) {
  144. STBI_FREE(data);
  145. stbi__err("outofmem", "Out of memory");
  146. return NULL;
  147. }
  148. for (j=0; j < (int) y; ++j) {
  149. unsigned char *src = data + j * x * img_n ;
  150. unsigned char *dest = good + j * x * req_comp;
  151. #define STBI__COMBO(a,b) ((a)*8+(b))
  152. #define STBI__CASE(a,b) case STBI__COMBO(a,b): for(i=x-1; i >= 0; --i, src += a, dest += b)
  153. switch (STBI__COMBO(img_n, req_comp)) {
  154. STBI__CASE(1,2) { dest[0]=src[0], dest[1]=255; } break;
  155. STBI__CASE(1,3) { dest[0]=dest[1]=dest[2]=src[0]; } break;
  156. STBI__CASE(1,4) { dest[0]=dest[1]=dest[2]=src[0], dest[3]=255; } break;
  157. STBI__CASE(2,1) { dest[0]=src[0]; } break;
  158. STBI__CASE(2,3) { dest[0]=dest[1]=dest[2]=src[0]; } break;
  159. STBI__CASE(2,4) { dest[0]=dest[1]=dest[2]=src[0], dest[3]=src[1]; } break;
  160. STBI__CASE(3,4) { dest[0]=src[0],dest[1]=src[1],dest[2]=src[2],dest[3]=255; } break;
  161. STBI__CASE(3,1) { dest[0]=stbi__compute_y(src[0],src[1],src[2]); } break;
  162. STBI__CASE(3,2) { dest[0]=stbi__compute_y(src[0],src[1],src[2]), dest[1] = 255; } break;
  163. STBI__CASE(4,1) { dest[0]=stbi__compute_y(src[0],src[1],src[2]); } break;
  164. STBI__CASE(4,2) { dest[0]=stbi__compute_y(src[0],src[1],src[2]), dest[1] = src[3]; } break;
  165. STBI__CASE(4,3) { dest[0]=src[0],dest[1]=src[1],dest[2]=src[2]; } break;
  166. default: STBI_ASSERT(0);
  167. }
  168. #undef STBI__CASE
  169. }
  170. STBI_FREE(data);
  171. return good;
  172. }
  173. static stbi__uint16 stbi__compute_y_16(int r, int g, int b)
  174. {
  175. return (stbi__uint16) (((r*77) + (g*150) + (29*b)) >> 8);
  176. }
  177. static stbi__uint16 *stbi__convert_format16(stbi__uint16 *data, int img_n, int req_comp, unsigned int x, unsigned int y)
  178. {
  179. int i,j;
  180. stbi__uint16 *good;
  181. if (req_comp == img_n) return data;
  182. STBI_ASSERT(req_comp >= 1 && req_comp <= 4);
  183. good = (stbi__uint16 *) stbi__malloc(req_comp * x * y * 2);
  184. if (good == NULL) {
  185. STBI_FREE(data);
  186. stbi__err("outofmem", "Out of memory");
  187. return NULL;
  188. }
  189. for (j=0; j < (int) y; ++j) {
  190. stbi__uint16 *src = data + j * x * img_n ;
  191. stbi__uint16 *dest = good + j * x * req_comp;
  192. #define STBI__COMBO(a,b) ((a)*8+(b))
  193. #define STBI__CASE(a,b) case STBI__COMBO(a,b): for(i=x-1; i >= 0; --i, src += a, dest += b)
  194. switch (STBI__COMBO(img_n, req_comp)) {
  195. STBI__CASE(1,2) { dest[0]=src[0], dest[1]=0xffff; } break;
  196. STBI__CASE(1,3) { dest[0]=dest[1]=dest[2]=src[0]; } break;
  197. STBI__CASE(1,4) { dest[0]=dest[1]=dest[2]=src[0], dest[3]=0xffff; } break;
  198. STBI__CASE(2,1) { dest[0]=src[0]; } break;
  199. STBI__CASE(2,3) { dest[0]=dest[1]=dest[2]=src[0]; } break;
  200. STBI__CASE(2,4) { dest[0]=dest[1]=dest[2]=src[0], dest[3]=src[1]; } break;
  201. STBI__CASE(3,4) { dest[0]=src[0],dest[1]=src[1],dest[2]=src[2],dest[3]=0xffff; } break;
  202. STBI__CASE(3,1) { dest[0]=stbi__compute_y_16(src[0],src[1],src[2]); } break;
  203. STBI__CASE(3,2) { dest[0]=stbi__compute_y_16(src[0],src[1],src[2]), dest[1] = 0xffff; } break;
  204. STBI__CASE(4,1) { dest[0]=stbi__compute_y_16(src[0],src[1],src[2]); } break;
  205. STBI__CASE(4,2) { dest[0]=stbi__compute_y_16(src[0],src[1],src[2]), dest[1] = src[3]; } break;
  206. STBI__CASE(4,3) { dest[0]=src[0],dest[1]=src[1],dest[2]=src[2]; } break;
  207. default: STBI_ASSERT(0);
  208. }
  209. #undef STBI__CASE
  210. }
  211. STBI_FREE(data);
  212. return good;
  213. }
  214. #define STBI__ZFAST_BITS 9
  215. #define STBI__ZFAST_MASK ((1 << STBI__ZFAST_BITS) - 1)
  216. typedef struct
  217. {
  218. stbi__uint16 fast[1 << STBI__ZFAST_BITS];
  219. stbi__uint16 firstcode[16];
  220. int maxcode[17];
  221. stbi__uint16 firstsymbol[16];
  222. unsigned char size[288];
  223. stbi__uint16 value[288];
  224. } stbi__zhuffman;
  225. __inline__ static int stbi__bitreverse16(int n)
  226. {
  227. n = ((n & 0xAAAA) >> 1) | ((n & 0x5555) << 1);
  228. n = ((n & 0xCCCC) >> 2) | ((n & 0x3333) << 2);
  229. n = ((n & 0xF0F0) >> 4) | ((n & 0x0F0F) << 4);
  230. n = ((n & 0xFF00) >> 8) | ((n & 0x00FF) << 8);
  231. return n;
  232. }
  233. __inline__ static int stbi__bit_reverse(int v, int bits)
  234. {
  235. STBI_ASSERT(bits <= 16);
  236. return stbi__bitreverse16(v) >> (16-bits);
  237. }
  238. static int stbi__zbuild_huffman(stbi__zhuffman *z, unsigned char *sizelist, int num)
  239. {
  240. int i,k=0;
  241. int code, next_code[16], sizes[17];
  242. memset(sizes, 0, sizeof(sizes));
  243. memset(z->fast, 0, sizeof(z->fast));
  244. for (i=0; i < num; ++i)
  245. ++sizes[sizelist[i]];
  246. sizes[0] = 0;
  247. for (i=1; i < 16; ++i)
  248. if (sizes[i] > (1 << i))
  249. return stbi__err("bad sizes", "Corrupt PNG");
  250. code = 0;
  251. for (i=1; i < 16; ++i) {
  252. next_code[i] = code;
  253. z->firstcode[i] = (stbi__uint16) code;
  254. z->firstsymbol[i] = (stbi__uint16) k;
  255. code = (code + sizes[i]);
  256. if (sizes[i])
  257. if (code-1 >= (1 << i)) return stbi__err("bad codelengths","Corrupt PNG");
  258. z->maxcode[i] = code << (16-i);
  259. code <<= 1;
  260. k += sizes[i];
  261. }
  262. z->maxcode[16] = 0x10000;
  263. for (i=0; i < num; ++i) {
  264. int s = sizelist[i];
  265. if (s) {
  266. int c = next_code[s] - z->firstcode[s] + z->firstsymbol[s];
  267. stbi__uint16 fastv = (stbi__uint16) ((s << 9) | i);
  268. z->size [c] = (unsigned char ) s;
  269. z->value[c] = (stbi__uint16) i;
  270. if (s <= STBI__ZFAST_BITS) {
  271. int j = stbi__bit_reverse(next_code[s],s);
  272. while (j < (1 << STBI__ZFAST_BITS)) {
  273. z->fast[j] = fastv;
  274. j += (1 << s);
  275. }
  276. }
  277. ++next_code[s];
  278. }
  279. }
  280. return 1;
  281. }
  282. typedef struct
  283. {
  284. unsigned char *zbuffer, *zbuffer_end;
  285. int num_bits;
  286. stbi__uint32 code_buffer;
  287. char *zout;
  288. char *zout_start;
  289. char *zout_end;
  290. int z_expandable;
  291. stbi__zhuffman z_length, z_distance;
  292. } stbi__zbuf;
  293. __inline__ static unsigned char stbi__zget8(stbi__zbuf *z)
  294. {
  295. if (z->zbuffer >= z->zbuffer_end) return 0;
  296. return *z->zbuffer++;
  297. }
  298. static void stbi__fill_bits(stbi__zbuf *z)
  299. {
  300. do {
  301. STBI_ASSERT(z->code_buffer < (1U << z->num_bits));
  302. z->code_buffer |= (unsigned int) stbi__zget8(z) << z->num_bits;
  303. z->num_bits += 8;
  304. } while (z->num_bits <= 24);
  305. }
  306. __inline__ static unsigned int stbi__zreceive(stbi__zbuf *z, int n)
  307. {
  308. unsigned int k;
  309. if (z->num_bits < n) stbi__fill_bits(z);
  310. k = z->code_buffer & ((1 << n) - 1);
  311. z->code_buffer >>= n;
  312. z->num_bits -= n;
  313. return k;
  314. }
  315. static int stbi__zhuffman_decode_slowpath(stbi__zbuf *a, stbi__zhuffman *z)
  316. {
  317. int b,s,k;
  318. k = stbi__bit_reverse(a->code_buffer, 16);
  319. for (s=STBI__ZFAST_BITS+1; ; ++s)
  320. if (k < z->maxcode[s])
  321. break;
  322. if (s == 16) return -1;
  323. b = (k >> (16-s)) - z->firstcode[s] + z->firstsymbol[s];
  324. STBI_ASSERT(z->size[b] == s);
  325. a->code_buffer >>= s;
  326. a->num_bits -= s;
  327. return z->value[b];
  328. }
  329. __inline__ static int stbi__zhuffman_decode(stbi__zbuf *a, stbi__zhuffman *z)
  330. {
  331. int b,s;
  332. if (a->num_bits < 16) stbi__fill_bits(a);
  333. b = z->fast[a->code_buffer & STBI__ZFAST_MASK];
  334. if (b) {
  335. s = b >> 9;
  336. a->code_buffer >>= s;
  337. a->num_bits -= s;
  338. return b & 511;
  339. }
  340. return stbi__zhuffman_decode_slowpath(a, z);
  341. }
  342. static int stbi__zexpand(stbi__zbuf *z, char *zout, int n)
  343. {
  344. char *q;
  345. int cur, limit, old_limit;
  346. z->zout = zout;
  347. if (!z->z_expandable) return stbi__err("output buffer limit","Corrupt PNG");
  348. cur = (int) (z->zout - z->zout_start);
  349. limit = old_limit = (int) (z->zout_end - z->zout_start);
  350. while (cur + n > limit)
  351. limit *= 2;
  352. q = (char *) STBI_REALLOC_SIZED(z->zout_start, old_limit, limit);
  353. STBI_NOTUSED(old_limit);
  354. if (q == NULL) return stbi__err("outofmem", "Out of memory");
  355. z->zout_start = q;
  356. z->zout = q + cur;
  357. z->zout_end = q + limit;
  358. return 1;
  359. }
  360. static int stbi__zlength_base[31] = {
  361. 3,4,5,6,7,8,9,10,11,13,
  362. 15,17,19,23,27,31,35,43,51,59,
  363. 67,83,99,115,131,163,195,227,258,0,0 };
  364. static int stbi__zlength_extra[31]=
  365. { 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 };
  366. static int stbi__zdist_base[32] = { 1,2,3,4,5,7,9,13,17,25,33,49,65,97,129,193,
  367. 257,385,513,769,1025,1537,2049,3073,4097,6145,8193,12289,16385,24577,0,0};
  368. static int stbi__zdist_extra[32] =
  369. { 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};
  370. static int stbi__parse_huffman_block(stbi__zbuf *a)
  371. {
  372. char *zout = a->zout;
  373. for(;;) {
  374. int z = stbi__zhuffman_decode(a, &a->z_length);
  375. if (z < 256) {
  376. if (z < 0) return stbi__err("bad huffman code","Corrupt PNG");
  377. if (zout >= a->zout_end) {
  378. if (!stbi__zexpand(a, zout, 1)) return 0;
  379. zout = a->zout;
  380. }
  381. *zout++ = (char) z;
  382. } else {
  383. unsigned char *p;
  384. int len,dist;
  385. if (z == 256) {
  386. a->zout = zout;
  387. return 1;
  388. }
  389. z -= 257;
  390. len = stbi__zlength_base[z];
  391. if (stbi__zlength_extra[z]) len += stbi__zreceive(a, stbi__zlength_extra[z]);
  392. z = stbi__zhuffman_decode(a, &a->z_distance);
  393. if (z < 0) return stbi__err("bad huffman code","Corrupt PNG");
  394. dist = stbi__zdist_base[z];
  395. if (stbi__zdist_extra[z]) dist += stbi__zreceive(a, stbi__zdist_extra[z]);
  396. if (zout - a->zout_start < dist) return stbi__err("bad dist","Corrupt PNG");
  397. if (zout + len > a->zout_end) {
  398. if (!stbi__zexpand(a, zout, len)) return 0;
  399. zout = a->zout;
  400. }
  401. p = (unsigned char *) (zout - dist);
  402. if (dist == 1) {
  403. unsigned char v = *p;
  404. if (len) { do *zout++ = v; while (--len); }
  405. } else {
  406. if (len) { do *zout++ = *p++; while (--len); }
  407. }
  408. }
  409. }
  410. }
  411. static int stbi__compute_huffman_codes(stbi__zbuf *a)
  412. {
  413. static unsigned char length_dezigzag[19] = { 16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15 };
  414. stbi__zhuffman z_codelength;
  415. unsigned char lencodes[286+32+137];
  416. unsigned char codelength_sizes[19];
  417. int i,n;
  418. int hlit = stbi__zreceive(a,5) + 257;
  419. int hdist = stbi__zreceive(a,5) + 1;
  420. int hclen = stbi__zreceive(a,4) + 4;
  421. int ntot = hlit + hdist;
  422. memset(codelength_sizes, 0, sizeof(codelength_sizes));
  423. for (i=0; i < hclen; ++i) {
  424. int s = stbi__zreceive(a,3);
  425. codelength_sizes[length_dezigzag[i]] = (unsigned char) s;
  426. }
  427. if (!stbi__zbuild_huffman(&z_codelength, codelength_sizes, 19)) return 0;
  428. n = 0;
  429. while (n < ntot) {
  430. int c = stbi__zhuffman_decode(a, &z_codelength);
  431. if (c < 0 || c >= 19) return stbi__err("bad codelengths", "Corrupt PNG");
  432. if (c < 16)
  433. lencodes[n++] = (unsigned char) c;
  434. else {
  435. unsigned char fill = 0;
  436. if (c == 16) {
  437. c = stbi__zreceive(a,2)+3;
  438. if (n == 0) return stbi__err("bad codelengths", "Corrupt PNG");
  439. fill = lencodes[n-1];
  440. } else if (c == 17)
  441. c = stbi__zreceive(a,3)+3;
  442. else {
  443. STBI_ASSERT(c == 18);
  444. c = stbi__zreceive(a,7)+11;
  445. }
  446. if (ntot - n < c) return stbi__err("bad codelengths", "Corrupt PNG");
  447. memset(lencodes+n, fill, c);
  448. n += c;
  449. }
  450. }
  451. if (n != ntot) return stbi__err("bad codelengths","Corrupt PNG");
  452. if (!stbi__zbuild_huffman(&a->z_length, lencodes, hlit)) return 0;
  453. if (!stbi__zbuild_huffman(&a->z_distance, lencodes+hlit, hdist)) return 0;
  454. return 1;
  455. }
  456. __inline__ static int stbi__parse_uncompressed_block(stbi__zbuf *a)
  457. {
  458. unsigned char header[4];
  459. int len,nlen,k;
  460. if (a->num_bits & 7)
  461. stbi__zreceive(a, a->num_bits & 7);
  462. k = 0;
  463. while (a->num_bits > 0) {
  464. header[k++] = (unsigned char) (a->code_buffer & 255);
  465. a->code_buffer >>= 8;
  466. a->num_bits -= 8;
  467. }
  468. STBI_ASSERT(a->num_bits == 0);
  469. while (k < 4)
  470. header[k++] = stbi__zget8(a);
  471. len = header[1] * 256 + header[0];
  472. nlen = header[3] * 256 + header[2];
  473. if (nlen != (len ^ 0xffff)) return stbi__err("zlib corrupt","Corrupt PNG");
  474. if (a->zbuffer + len > a->zbuffer_end) return stbi__err("read past buffer","Corrupt PNG");
  475. if (a->zout + len > a->zout_end)
  476. if (!stbi__zexpand(a, a->zout, len)) return 0;
  477. memcpy(a->zout, a->zbuffer, len);
  478. a->zbuffer += len;
  479. a->zout += len;
  480. return 1;
  481. }
  482. static int stbi__parse_zlib_header(stbi__zbuf *a)
  483. {
  484. int cmf = stbi__zget8(a);
  485. int cm = cmf & 15;
  486. /* int cinfo = cmf >> 4; */
  487. int flg = stbi__zget8(a);
  488. if ((cmf*256+flg) % 31 != 0) return stbi__err("bad zlib header","Corrupt PNG");
  489. if (flg & 32) return stbi__err("no preset dict","Corrupt PNG");
  490. if (cm != 8) return stbi__err("bad compression","Corrupt PNG");
  491. return 1;
  492. }
  493. static unsigned char stbi__zdefault_length[288], stbi__zdefault_distance[32];
  494. static void stbi__init_zdefaults(void)
  495. {
  496. int i;
  497. for (i=0; i <= 143; ++i) stbi__zdefault_length[i] = 8;
  498. for ( ; i <= 255; ++i) stbi__zdefault_length[i] = 9;
  499. for ( ; i <= 279; ++i) stbi__zdefault_length[i] = 7;
  500. for ( ; i <= 287; ++i) stbi__zdefault_length[i] = 8;
  501. for (i=0; i <= 31; ++i) stbi__zdefault_distance[i] = 5;
  502. }
  503. static int stbi__parse_zlib(stbi__zbuf *a, int parse_header)
  504. {
  505. int final, type;
  506. if (parse_header)
  507. if (!stbi__parse_zlib_header(a)) return 0;
  508. a->num_bits = 0;
  509. a->code_buffer = 0;
  510. do {
  511. final = stbi__zreceive(a,1);
  512. type = stbi__zreceive(a,2);
  513. if (type == 0) {
  514. if (!stbi__parse_uncompressed_block(a)) return 0;
  515. } else if (type == 3) {
  516. return 0;
  517. } else {
  518. if (type == 1) {
  519. if (!stbi__zbuild_huffman(&a->z_length , stbi__zdefault_length , 288)) return 0;
  520. if (!stbi__zbuild_huffman(&a->z_distance, stbi__zdefault_distance, 32)) return 0;
  521. } else {
  522. if (!stbi__compute_huffman_codes(a)) return 0;
  523. }
  524. if (!stbi__parse_huffman_block(a)) return 0;
  525. }
  526. } while (!final);
  527. return 1;
  528. }
  529. static int stbi__do_zlib(stbi__zbuf *a, char *obuf, int olen, int exp, int parse_header)
  530. {
  531. a->zout_start = obuf;
  532. a->zout = obuf;
  533. a->zout_end = obuf + olen;
  534. a->z_expandable = exp;
  535. stbi__init_zdefaults();
  536. return stbi__parse_zlib(a, parse_header);
  537. }
  538. char *stbi_zlib_decode_malloc_guesssize_headerflag(const char *buffer, int len, int initial_size, int *outlen, int parse_header)
  539. {
  540. stbi__zbuf a;
  541. char *p = (char *) stbi__malloc(initial_size);
  542. if (p == NULL) return NULL;
  543. a.zbuffer = (unsigned char *) buffer;
  544. a.zbuffer_end = (unsigned char *) buffer + len;
  545. if (stbi__do_zlib(&a, p, initial_size, 1, parse_header)) {
  546. if (outlen) *outlen = (int) (a.zout - a.zout_start);
  547. return a.zout_start;
  548. } else {
  549. STBI_FREE(a.zout_start);
  550. return NULL;
  551. }
  552. }
  553. typedef struct
  554. {
  555. stbi__uint32 length;
  556. stbi__uint32 type;
  557. } stbi__pngchunk;
  558. static stbi__pngchunk stbi__get_chunk_header(stbi__context *s)
  559. {
  560. stbi__pngchunk c;
  561. c.length = stbi__get32be(s);
  562. c.type = stbi__get32be(s);
  563. return c;
  564. }
  565. __inline__ static int stbi__check_png_header(stbi__context *s)
  566. {
  567. static unsigned char png_sig[8] = { 137,80,78,71,13,10,26,10 };
  568. int i;
  569. for (i=0; i < 8; ++i)
  570. if (stbi__get8(s) != png_sig[i]) return stbi__err("bad png sig","Not a PNG");
  571. return 1;
  572. }
  573. typedef struct
  574. {
  575. stbi__context *s;
  576. unsigned char *idata, *expanded, *out;
  577. int depth;
  578. } stbi__png;
  579. enum {
  580. STBI__F_none=0,
  581. STBI__F_sub=1,
  582. STBI__F_up=2,
  583. STBI__F_avg=3,
  584. STBI__F_paeth=4,
  585. STBI__F_avg_first,
  586. STBI__F_paeth_first
  587. };
  588. static unsigned char first_row_filter[5] =
  589. {
  590. STBI__F_none,
  591. STBI__F_sub,
  592. STBI__F_none,
  593. STBI__F_avg_first,
  594. STBI__F_paeth_first
  595. };
  596. static int stbi__paeth(int a, int b, int c)
  597. {
  598. int p = a + b - c;
  599. int pa = abs(p-a);
  600. int pb = abs(p-b);
  601. int pc = abs(p-c);
  602. if (pa <= pb && pa <= pc) return a;
  603. if (pb <= pc) return b;
  604. return c;
  605. }
  606. static unsigned char stbi__depth_scale_table[9] = { 0, 0xff, 0x55, 0, 0x11, 0,0,0, 0x01 };
  607. static int stbi__create_png_image_raw(stbi__png *a, unsigned char *raw, stbi__uint32 raw_len, int out_n, stbi__uint32 x, stbi__uint32 y, int depth, int color)
  608. {
  609. int bytes = (depth == 16? 2 : 1);
  610. stbi__context *s = a->s;
  611. stbi__uint32 i,j,stride = x*out_n*bytes;
  612. stbi__uint32 img_len, img_width_bytes;
  613. int k;
  614. int img_n = s->img_n;
  615. int output_bytes = out_n*bytes;
  616. int filter_bytes = img_n*bytes;
  617. int width = x;
  618. STBI_ASSERT(out_n == s->img_n || out_n == s->img_n+1);
  619. a->out = (unsigned char *) stbi__malloc_mad3(x, y, output_bytes, 0);
  620. if (!a->out) return stbi__err("outofmem", "Out of memory");
  621. if (!stbi__mad3sizes_valid(img_n, x, depth, 7)) return stbi__err("too large", "Corrupt PNG");
  622. img_width_bytes = (((img_n * x * depth) + 7) >> 3);
  623. img_len = (img_width_bytes + 1) * y;
  624. if (s->img_x == x && s->img_y == y) {
  625. if (raw_len != img_len) return stbi__err("not enough pixels","Corrupt PNG");
  626. } else {
  627. if (raw_len < img_len) return stbi__err("not enough pixels","Corrupt PNG");
  628. }
  629. for (j=0; j < y; ++j) {
  630. unsigned char *cur = a->out + stride*j;
  631. unsigned char *prior = cur - stride;
  632. int filter = *raw++;
  633. if (filter > 4)
  634. return stbi__err("invalid filter","Corrupt PNG");
  635. if (depth < 8) {
  636. STBI_ASSERT(img_width_bytes <= x);
  637. cur += x*out_n - img_width_bytes;
  638. filter_bytes = 1;
  639. width = img_width_bytes;
  640. }
  641. prior = cur - stride;
  642. if (j == 0) filter = first_row_filter[filter];
  643. for (k=0; k < filter_bytes; ++k) {
  644. switch (filter) {
  645. case STBI__F_none : cur[k] = raw[k]; break;
  646. case STBI__F_sub : cur[k] = raw[k]; break;
  647. case STBI__F_up : cur[k] = STBI__BYTECAST(raw[k] + prior[k]); break;
  648. case STBI__F_avg : cur[k] = STBI__BYTECAST(raw[k] + (prior[k]>>1)); break;
  649. case STBI__F_paeth : cur[k] = STBI__BYTECAST(raw[k] + stbi__paeth(0,prior[k],0)); break;
  650. case STBI__F_avg_first : cur[k] = raw[k]; break;
  651. case STBI__F_paeth_first: cur[k] = raw[k]; break;
  652. }
  653. }
  654. if (depth == 8) {
  655. if (img_n != out_n)
  656. cur[img_n] = 255;
  657. raw += img_n;
  658. cur += out_n;
  659. prior += out_n;
  660. } else if (depth == 16) {
  661. if (img_n != out_n) {
  662. cur[filter_bytes] = 255;
  663. cur[filter_bytes+1] = 255;
  664. }
  665. raw += filter_bytes;
  666. cur += output_bytes;
  667. prior += output_bytes;
  668. } else {
  669. raw += 1;
  670. cur += 1;
  671. prior += 1;
  672. }
  673. if (depth < 8 || img_n == out_n) {
  674. int nk = (width - 1)*filter_bytes;
  675. #define STBI__CASE(f) \
  676. case f: \
  677. for (k=0; k < nk; ++k)
  678. switch (filter) {
  679. case STBI__F_none: memcpy(cur, raw, nk); break;
  680. STBI__CASE(STBI__F_sub) { cur[k] = STBI__BYTECAST(raw[k] + cur[k-filter_bytes]); } break;
  681. STBI__CASE(STBI__F_up) { cur[k] = STBI__BYTECAST(raw[k] + prior[k]); } break;
  682. STBI__CASE(STBI__F_avg) { cur[k] = STBI__BYTECAST(raw[k] + ((prior[k] + cur[k-filter_bytes])>>1)); } break;
  683. STBI__CASE(STBI__F_paeth) { cur[k] = STBI__BYTECAST(raw[k] + stbi__paeth(cur[k-filter_bytes],prior[k],prior[k-filter_bytes])); } break;
  684. STBI__CASE(STBI__F_avg_first) { cur[k] = STBI__BYTECAST(raw[k] + (cur[k-filter_bytes] >> 1)); } break;
  685. STBI__CASE(STBI__F_paeth_first) { cur[k] = STBI__BYTECAST(raw[k] + stbi__paeth(cur[k-filter_bytes],0,0)); } break;
  686. }
  687. #undef STBI__CASE
  688. raw += nk;
  689. } else {
  690. STBI_ASSERT(img_n+1 == out_n);
  691. #define STBI__CASE(f) \
  692. case f: \
  693. for (i=x-1; i >= 1; --i, cur[filter_bytes]=255,raw+=filter_bytes,cur+=output_bytes,prior+=output_bytes) \
  694. for (k=0; k < filter_bytes; ++k)
  695. switch (filter) {
  696. STBI__CASE(STBI__F_none) { cur[k] = raw[k]; } break;
  697. STBI__CASE(STBI__F_sub) { cur[k] = STBI__BYTECAST(raw[k] + cur[k- output_bytes]); } break;
  698. STBI__CASE(STBI__F_up) { cur[k] = STBI__BYTECAST(raw[k] + prior[k]); } break;
  699. STBI__CASE(STBI__F_avg) { cur[k] = STBI__BYTECAST(raw[k] + ((prior[k] + cur[k- output_bytes])>>1)); } break;
  700. STBI__CASE(STBI__F_paeth) { cur[k] = STBI__BYTECAST(raw[k] + stbi__paeth(cur[k- output_bytes],prior[k],prior[k- output_bytes])); } break;
  701. STBI__CASE(STBI__F_avg_first) { cur[k] = STBI__BYTECAST(raw[k] + (cur[k- output_bytes] >> 1)); } break;
  702. STBI__CASE(STBI__F_paeth_first) { cur[k] = STBI__BYTECAST(raw[k] + stbi__paeth(cur[k- output_bytes],0,0)); } break;
  703. }
  704. #undef STBI__CASE
  705. if (depth == 16) {
  706. cur = a->out + stride*j;
  707. for (i=0; i < x; ++i,cur+=output_bytes) {
  708. cur[filter_bytes+1] = 255;
  709. }
  710. }
  711. }
  712. }
  713. if (depth < 8) {
  714. for (j=0; j < y; ++j) {
  715. unsigned char *cur = a->out + stride*j;
  716. unsigned char *in = a->out + stride*j + x*out_n - img_width_bytes;
  717. unsigned char scale = (color == 0) ? stbi__depth_scale_table[depth] : 1;
  718. if (depth == 4) {
  719. for (k=x*img_n; k >= 2; k-=2, ++in) {
  720. *cur++ = scale * ((*in >> 4) );
  721. *cur++ = scale * ((*in ) & 0x0f);
  722. }
  723. if (k > 0) *cur++ = scale * ((*in >> 4) );
  724. } else if (depth == 2) {
  725. for (k=x*img_n; k >= 4; k-=4, ++in) {
  726. *cur++ = scale * ((*in >> 6) );
  727. *cur++ = scale * ((*in >> 4) & 0x03);
  728. *cur++ = scale * ((*in >> 2) & 0x03);
  729. *cur++ = scale * ((*in ) & 0x03);
  730. }
  731. if (k > 0) *cur++ = scale * ((*in >> 6) );
  732. if (k > 1) *cur++ = scale * ((*in >> 4) & 0x03);
  733. if (k > 2) *cur++ = scale * ((*in >> 2) & 0x03);
  734. } else if (depth == 1) {
  735. for (k=x*img_n; k >= 8; k-=8, ++in) {
  736. *cur++ = scale * ((*in >> 7) );
  737. *cur++ = scale * ((*in >> 6) & 0x01);
  738. *cur++ = scale * ((*in >> 5) & 0x01);
  739. *cur++ = scale * ((*in >> 4) & 0x01);
  740. *cur++ = scale * ((*in >> 3) & 0x01);
  741. *cur++ = scale * ((*in >> 2) & 0x01);
  742. *cur++ = scale * ((*in >> 1) & 0x01);
  743. *cur++ = scale * ((*in ) & 0x01);
  744. }
  745. if (k > 0) *cur++ = scale * ((*in >> 7) );
  746. if (k > 1) *cur++ = scale * ((*in >> 6) & 0x01);
  747. if (k > 2) *cur++ = scale * ((*in >> 5) & 0x01);
  748. if (k > 3) *cur++ = scale * ((*in >> 4) & 0x01);
  749. if (k > 4) *cur++ = scale * ((*in >> 3) & 0x01);
  750. if (k > 5) *cur++ = scale * ((*in >> 2) & 0x01);
  751. if (k > 6) *cur++ = scale * ((*in >> 1) & 0x01);
  752. }
  753. if (img_n != out_n) {
  754. int q;
  755. cur = a->out + stride*j;
  756. if (img_n == 1) {
  757. for (q=x-1; q >= 0; --q) {
  758. cur[q*2+1] = 255;
  759. cur[q*2+0] = cur[q];
  760. }
  761. } else {
  762. STBI_ASSERT(img_n == 3);
  763. for (q=x-1; q >= 0; --q) {
  764. cur[q*4+3] = 255;
  765. cur[q*4+2] = cur[q*3+2];
  766. cur[q*4+1] = cur[q*3+1];
  767. cur[q*4+0] = cur[q*3+0];
  768. }
  769. }
  770. }
  771. }
  772. } else if (depth == 16) {
  773. unsigned char *cur = a->out;
  774. stbi__uint16 *cur16 = (stbi__uint16*)cur;
  775. for(i=0; i < x*y*out_n; ++i,cur16++,cur+=2) {
  776. *cur16 = (cur[0] << 8) | cur[1];
  777. }
  778. }
  779. return 1;
  780. }
  781. static int stbi__create_png_image(stbi__png *a, unsigned char *image_data, stbi__uint32 image_data_len, int out_n, int depth, int color, int interlaced)
  782. {
  783. int bytes = (depth == 16 ? 2 : 1);
  784. int out_bytes = out_n * bytes;
  785. unsigned char *final;
  786. int p;
  787. if (!interlaced)
  788. return stbi__create_png_image_raw(a, image_data, image_data_len, out_n, a->s->img_x, a->s->img_y, depth, color);
  789. final = (unsigned char *) stbi__malloc_mad3(a->s->img_x, a->s->img_y, out_bytes, 0);
  790. for (p=0; p < 7; ++p) {
  791. int xorig[] = { 0,4,0,2,0,1,0 };
  792. int yorig[] = { 0,0,4,0,2,0,1 };
  793. int xspc[] = { 8,8,4,4,2,2,1 };
  794. int yspc[] = { 8,8,8,4,4,2,2 };
  795. int i,j,x,y;
  796. x = (a->s->img_x - xorig[p] + xspc[p]-1) / xspc[p];
  797. y = (a->s->img_y - yorig[p] + yspc[p]-1) / yspc[p];
  798. if (x && y) {
  799. stbi__uint32 img_len = ((((a->s->img_n * x * depth) + 7) >> 3) + 1) * y;
  800. if (!stbi__create_png_image_raw(a, image_data, image_data_len, out_n, x, y, depth, color)) {
  801. STBI_FREE(final);
  802. return 0;
  803. }
  804. for (j=0; j < y; ++j) {
  805. for (i=0; i < x; ++i) {
  806. int out_y = j*yspc[p]+yorig[p];
  807. int out_x = i*xspc[p]+xorig[p];
  808. memcpy(final + out_y*a->s->img_x*out_bytes + out_x*out_bytes,
  809. a->out + (j*x+i)*out_bytes, out_bytes);
  810. }
  811. }
  812. STBI_FREE(a->out);
  813. image_data += img_len;
  814. image_data_len -= img_len;
  815. }
  816. }
  817. a->out = final;
  818. return 1;
  819. }
  820. static int stbi__compute_transparency(stbi__png *z, unsigned char tc[3], int out_n)
  821. {
  822. stbi__context *s = z->s;
  823. stbi__uint32 i, pixel_count = s->img_x * s->img_y;
  824. unsigned char *p = z->out;
  825. STBI_ASSERT(out_n == 2 || out_n == 4);
  826. if (out_n == 2) {
  827. for (i=0; i < pixel_count; ++i) {
  828. p[1] = (p[0] == tc[0] ? 0 : 255);
  829. p += 2;
  830. }
  831. } else {
  832. for (i=0; i < pixel_count; ++i) {
  833. if (p[0] == tc[0] && p[1] == tc[1] && p[2] == tc[2])
  834. p[3] = 0;
  835. p += 4;
  836. }
  837. }
  838. return 1;
  839. }
  840. static int stbi__compute_transparency16(stbi__png *z, stbi__uint16 tc[3], int out_n)
  841. {
  842. stbi__context *s = z->s;
  843. stbi__uint32 i, pixel_count = s->img_x * s->img_y;
  844. stbi__uint16 *p = (stbi__uint16*) z->out;
  845. STBI_ASSERT(out_n == 2 || out_n == 4);
  846. if (out_n == 2) {
  847. for (i = 0; i < pixel_count; ++i) {
  848. p[1] = (p[0] == tc[0] ? 0 : 65535);
  849. p += 2;
  850. }
  851. } else {
  852. for (i = 0; i < pixel_count; ++i) {
  853. if (p[0] == tc[0] && p[1] == tc[1] && p[2] == tc[2])
  854. p[3] = 0;
  855. p += 4;
  856. }
  857. }
  858. return 1;
  859. }
  860. static int stbi__expand_png_palette(stbi__png *a, unsigned char *palette, int len, int pal_img_n)
  861. {
  862. stbi__uint32 i, pixel_count = a->s->img_x * a->s->img_y;
  863. unsigned char *p, *temp_out, *orig = a->out;
  864. p = (unsigned char *) stbi__malloc_mad2(pixel_count, pal_img_n, 0);
  865. if (p == NULL) return stbi__err("outofmem", "Out of memory");
  866. temp_out = p;
  867. if (pal_img_n == 3) {
  868. for (i=0; i < pixel_count; ++i) {
  869. int n = orig[i]*4;
  870. p[0] = palette[n ];
  871. p[1] = palette[n+1];
  872. p[2] = palette[n+2];
  873. p += 3;
  874. }
  875. } else {
  876. for (i=0; i < pixel_count; ++i) {
  877. int n = orig[i]*4;
  878. p[0] = palette[n ];
  879. p[1] = palette[n+1];
  880. p[2] = palette[n+2];
  881. p[3] = palette[n+3];
  882. p += 4;
  883. }
  884. }
  885. STBI_FREE(a->out);
  886. a->out = temp_out;
  887. STBI_NOTUSED(len);
  888. return 1;
  889. }
  890. #define STBI__PNG_TYPE(a,b,c,d) (((unsigned) (a) << 24) + ((unsigned) (b) << 16) + ((unsigned) (c) << 8) + (unsigned) (d))
  891. static int stbi__parse_png_file(stbi__png *z, int scan, int req_comp)
  892. {
  893. unsigned char palette[1024], pal_img_n=0;
  894. unsigned char has_trans=0, tc[3]={0,0,0};
  895. stbi__uint16 tc16[3]={0,0,0};
  896. stbi__uint32 ioff=0, idata_limit=0, i, pal_len=0;
  897. int first=1,k,interlace=0, color=0;
  898. stbi__context *s = z->s;
  899. z->expanded = NULL;
  900. z->idata = NULL;
  901. z->out = NULL;
  902. if (!stbi__check_png_header(s)) return 0;
  903. if (scan == STBI__SCAN_type) return 1;
  904. for (;;) {
  905. stbi__pngchunk c = stbi__get_chunk_header(s);
  906. switch (c.type) {
  907. case STBI__PNG_TYPE('C','g','B','I'):
  908. stbi__skip(s, c.length);
  909. break;
  910. case STBI__PNG_TYPE('I','H','D','R'): {
  911. int comp,filter;
  912. if (!first) return stbi__err("multiple IHDR","Corrupt PNG");
  913. first = 0;
  914. if (c.length != 13) return stbi__err("bad IHDR len","Corrupt PNG");
  915. s->img_x = stbi__get32be(s); if (s->img_x > (1 << 24)) return stbi__err("too large","Very large image (corrupt?)");
  916. s->img_y = stbi__get32be(s); if (s->img_y > (1 << 24)) return stbi__err("too large","Very large image (corrupt?)");
  917. z->depth = stbi__get8(s); if (z->depth != 1 && z->depth != 2 && z->depth != 4 && z->depth != 8 && z->depth != 16) return stbi__err("1/2/4/8/16-bit only","PNG not supported: 1/2/4/8/16-bit only");
  918. color = stbi__get8(s); if (color > 6) return stbi__err("bad ctype","Corrupt PNG");
  919. if (color == 3 && z->depth == 16) return stbi__err("bad ctype","Corrupt PNG");
  920. if (color == 3) pal_img_n = 3; else if (color & 1) return stbi__err("bad ctype","Corrupt PNG");
  921. comp = stbi__get8(s); if (comp) return stbi__err("bad comp method","Corrupt PNG");
  922. filter= stbi__get8(s); if (filter) return stbi__err("bad filter method","Corrupt PNG");
  923. interlace = stbi__get8(s); if (interlace>1) return stbi__err("bad interlace method","Corrupt PNG");
  924. if (!s->img_x || !s->img_y) return stbi__err("0-pixel image","Corrupt PNG");
  925. if (!pal_img_n) {
  926. s->img_n = (color & 2 ? 3 : 1) + (color & 4 ? 1 : 0);
  927. if ((1 << 30) / s->img_x / s->img_n < s->img_y) return stbi__err("too large", "Image too large to decode");
  928. if (scan == STBI__SCAN_header) return 1;
  929. } else {
  930. s->img_n = 1;
  931. if ((1 << 30) / s->img_x / 4 < s->img_y) return stbi__err("too large","Corrupt PNG");
  932. }
  933. break;
  934. }
  935. case STBI__PNG_TYPE('P','L','T','E'): {
  936. if (first) return stbi__err("first not IHDR", "Corrupt PNG");
  937. if (c.length > 256*3) return stbi__err("invalid PLTE","Corrupt PNG");
  938. pal_len = c.length / 3;
  939. if (pal_len * 3 != c.length) return stbi__err("invalid PLTE","Corrupt PNG");
  940. for (i=0; i < pal_len; ++i) {
  941. palette[i*4+0] = stbi__get8(s);
  942. palette[i*4+1] = stbi__get8(s);
  943. palette[i*4+2] = stbi__get8(s);
  944. palette[i*4+3] = 255;
  945. }
  946. break;
  947. }
  948. case STBI__PNG_TYPE('t','R','N','S'): {
  949. if (first) return stbi__err("first not IHDR", "Corrupt PNG");
  950. if (z->idata) return stbi__err("tRNS after IDAT","Corrupt PNG");
  951. if (pal_img_n) {
  952. if (scan == STBI__SCAN_header) { s->img_n = 4; return 1; }
  953. if (pal_len == 0) return stbi__err("tRNS before PLTE","Corrupt PNG");
  954. if (c.length > pal_len) return stbi__err("bad tRNS len","Corrupt PNG");
  955. pal_img_n = 4;
  956. for (i=0; i < c.length; ++i)
  957. palette[i*4+3] = stbi__get8(s);
  958. } else {
  959. if (!(s->img_n & 1)) return stbi__err("tRNS with alpha","Corrupt PNG");
  960. if (c.length != (stbi__uint32) s->img_n*2) return stbi__err("bad tRNS len","Corrupt PNG");
  961. has_trans = 1;
  962. if (z->depth == 16) {
  963. for (k = 0; k < s->img_n && k < (int)sizeof(tc16); ++k) tc16[k] = (stbi__uint16)stbi__get16be(s);
  964. } else {
  965. for (k = 0; k < s->img_n && k < (int)sizeof(tc); ++k) tc[k] =
  966. (unsigned char)(stbi__get16be(s) & 255) * stbi__depth_scale_table[z->depth];
  967. }
  968. }
  969. break;
  970. }
  971. case STBI__PNG_TYPE('I','D','A','T'): {
  972. if (first) return stbi__err("first not IHDR", "Corrupt PNG");
  973. if (pal_img_n && !pal_len) return stbi__err("no PLTE","Corrupt PNG");
  974. if (scan == STBI__SCAN_header) { s->img_n = pal_img_n; return 1; }
  975. if ((int)(ioff + c.length) < (int)ioff) return 0;
  976. if (ioff + c.length > idata_limit) {
  977. stbi__uint32 idata_limit_old = idata_limit;
  978. unsigned char *p;
  979. if (idata_limit == 0) idata_limit = c.length > 4096 ? c.length : 4096;
  980. while (ioff + c.length > idata_limit)
  981. idata_limit *= 2;
  982. STBI_NOTUSED(idata_limit_old);
  983. p = (unsigned char *) STBI_REALLOC_SIZED(z->idata, idata_limit_old, idata_limit); if (p == NULL) return stbi__err("outofmem", "Out of memory");
  984. z->idata = p;
  985. }
  986. if (!stbi__getn(s, z->idata+ioff,c.length)) return stbi__err("outofdata","Corrupt PNG");
  987. ioff += c.length;
  988. break;
  989. }
  990. case STBI__PNG_TYPE('I','E','N','D'): {
  991. stbi__uint32 raw_len, bpl;
  992. if (first) return stbi__err("first not IHDR", "Corrupt PNG");
  993. if (scan != STBI__SCAN_load) return 1;
  994. if (z->idata == NULL) return stbi__err("no IDAT","Corrupt PNG");
  995. bpl = (s->img_x * z->depth + 7) / 8;
  996. raw_len = bpl * s->img_y * s->img_n /* pixels */ + s->img_y /* filter mode per row */;
  997. z->expanded = (unsigned char *) stbi_zlib_decode_malloc_guesssize_headerflag((char *) z->idata, ioff, raw_len, (int *) &raw_len, 1);
  998. if (z->expanded == NULL) return 0;
  999. STBI_FREE(z->idata); z->idata = NULL;
  1000. if ((req_comp == s->img_n+1 && req_comp != 3 && !pal_img_n) || has_trans)
  1001. s->img_out_n = s->img_n+1;
  1002. else
  1003. s->img_out_n = s->img_n;
  1004. if (!stbi__create_png_image(z, z->expanded, raw_len, s->img_out_n, z->depth, color, interlace)) return 0;
  1005. if (has_trans) {
  1006. if (z->depth == 16) {
  1007. if (!stbi__compute_transparency16(z, tc16, s->img_out_n)) return 0;
  1008. } else {
  1009. if (!stbi__compute_transparency(z, tc, s->img_out_n)) return 0;
  1010. }
  1011. }
  1012. if (pal_img_n) {
  1013. s->img_n = pal_img_n;
  1014. s->img_out_n = pal_img_n;
  1015. if (req_comp >= 3) s->img_out_n = req_comp;
  1016. if (!stbi__expand_png_palette(z, palette, pal_len, s->img_out_n))
  1017. return 0;
  1018. } else if (has_trans) {
  1019. ++s->img_n;
  1020. }
  1021. STBI_FREE(z->expanded); z->expanded = NULL;
  1022. return 1;
  1023. }
  1024. default:
  1025. if (first) return stbi__err("first not IHDR", "Corrupt PNG");
  1026. if ((c.type & (1 << 29)) == 0) {
  1027. return stbi__err("invalid_chunk", "PNG not supported: unknown PNG chunk type");
  1028. }
  1029. stbi__skip(s, c.length);
  1030. break;
  1031. }
  1032. stbi__get32be(s);
  1033. }
  1034. }
  1035. static void *stbi__do_png(stbi__png *p, int *x, int *y, int *n, int req_comp, stbi__result_info *ri)
  1036. {
  1037. void *result=NULL;
  1038. if (req_comp < 0 || req_comp > 4) { stbi__err("bad req_comp", "Internal error"); return NULL; }
  1039. if (stbi__parse_png_file(p, STBI__SCAN_load, req_comp)) {
  1040. if (p->depth < 8)
  1041. ri->bits_per_channel = 8;
  1042. else
  1043. ri->bits_per_channel = p->depth;
  1044. result = p->out;
  1045. p->out = NULL;
  1046. if (req_comp && req_comp != p->s->img_out_n) {
  1047. if (ri->bits_per_channel == 8)
  1048. result = stbi__convert_format((unsigned char *) result, p->s->img_out_n, req_comp, p->s->img_x, p->s->img_y);
  1049. else
  1050. result = stbi__convert_format16((stbi__uint16 *) result, p->s->img_out_n, req_comp, p->s->img_x, p->s->img_y);
  1051. p->s->img_out_n = req_comp;
  1052. if (result == NULL) return result;
  1053. }
  1054. *x = p->s->img_x;
  1055. *y = p->s->img_y;
  1056. if (n) *n = p->s->img_n;
  1057. }
  1058. STBI_FREE(p->out); p->out = NULL;
  1059. STBI_FREE(p->expanded); p->expanded = NULL;
  1060. STBI_FREE(p->idata); p->idata = NULL;
  1061. return result;
  1062. }
  1063. void *stbi__png_load(stbi__context *s, int *x, int *y, int *comp, int req_comp, stbi__result_info *ri)
  1064. {
  1065. stbi__png p;
  1066. p.s = s;
  1067. return stbi__do_png(&p, x,y,comp,req_comp, ri);
  1068. }
  1069. #endif
  1070. #endif /* STBI_INCLUDE_STB_IMAGE_H */