stb_image.h 40 KB

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