You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

566 lines
17KB

  1. /*
  2. * PNG image format
  3. * Copyright (c) 2003 Fabrice Bellard.
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #include "avformat.h"
  20. #include <zlib.h>
  21. //#define DEBUG
  22. #define PNG_COLOR_MASK_PALETTE 1
  23. #define PNG_COLOR_MASK_COLOR 2
  24. #define PNG_COLOR_MASK_ALPHA 4
  25. #define PNG_COLOR_TYPE_GRAY 0
  26. #define PNG_COLOR_TYPE_PALETTE (PNG_COLOR_MASK_COLOR | PNG_COLOR_MASK_PALETTE)
  27. #define PNG_COLOR_TYPE_RGB (PNG_COLOR_MASK_COLOR)
  28. #define PNG_COLOR_TYPE_RGB_ALPHA (PNG_COLOR_MASK_COLOR | PNG_COLOR_MASK_ALPHA)
  29. #define PNG_COLOR_TYPE_GRAY_ALPHA (PNG_COLOR_MASK_ALPHA)
  30. #define PNG_FILTER_VALUE_NONE 0
  31. #define PNG_FILTER_VALUE_SUB 1
  32. #define PNG_FILTER_VALUE_UP 2
  33. #define PNG_FILTER_VALUE_AVG 3
  34. #define PNG_FILTER_VALUE_PAETH 4
  35. #define PNG_IHDR 0x0001
  36. #define PNG_IDAT 0x0002
  37. #define PNG_ALLIMAGE 0x0004
  38. #define PNG_PLTE 0x0008
  39. #define IOBUF_SIZE 4096
  40. typedef struct PNGDecodeState {
  41. int state;
  42. int width, height;
  43. int bit_depth;
  44. int color_type;
  45. int compression_type;
  46. int interlace_type;
  47. int filter_type;
  48. int channels;
  49. int bits_per_pixel;
  50. int bpp;
  51. uint8_t *image_buf;
  52. int image_linesize;
  53. uint32_t palette[256];
  54. uint8_t *crow_buf;
  55. uint8_t *empty_row;
  56. int crow_size; /* compressed row size (include filter type) */
  57. int row_size; /* decompressed row size */
  58. int y;
  59. z_stream zstream;
  60. } PNGDecodeState;
  61. static const uint8_t pngsig[8] = {137, 80, 78, 71, 13, 10, 26, 10};
  62. static int png_probe(AVProbeData *pd)
  63. {
  64. if (pd->buf_size >= 8 &&
  65. memcmp(pd->buf, pngsig, 8) == 0)
  66. return AVPROBE_SCORE_MAX;
  67. else
  68. return 0;
  69. }
  70. static void *png_zalloc(void *opaque, unsigned int items, unsigned int size)
  71. {
  72. return av_malloc(items * size);
  73. }
  74. static void png_zfree(void *opaque, void *ptr)
  75. {
  76. av_free(ptr);
  77. }
  78. /* XXX: optimize */
  79. static void png_filter_row(uint8_t *dst, int filter_type,
  80. uint8_t *src, uint8_t *last, int size, int bpp)
  81. {
  82. int i, p;
  83. switch(filter_type) {
  84. case PNG_FILTER_VALUE_NONE:
  85. memcpy(dst, src, size);
  86. break;
  87. case PNG_FILTER_VALUE_SUB:
  88. for(i = 0; i < bpp; i++) {
  89. dst[i] = src[i];
  90. }
  91. for(i = bpp; i < size; i++) {
  92. p = dst[i - bpp];
  93. dst[i] = p + src[i];
  94. }
  95. break;
  96. case PNG_FILTER_VALUE_UP:
  97. for(i = 0; i < size; i++) {
  98. p = last[i];
  99. dst[i] = p + src[i];
  100. }
  101. break;
  102. case PNG_FILTER_VALUE_AVG:
  103. for(i = 0; i < bpp; i++) {
  104. p = (last[i] >> 1);
  105. dst[i] = p + src[i];
  106. }
  107. for(i = bpp; i < size; i++) {
  108. p = ((dst[i - bpp] + last[i]) >> 1);
  109. dst[i] = p + src[i];
  110. }
  111. break;
  112. case PNG_FILTER_VALUE_PAETH:
  113. for(i = 0; i < bpp; i++) {
  114. p = last[i];
  115. dst[i] = p + src[i];
  116. }
  117. for(i = bpp; i < size; i++) {
  118. int a, b, c, pa, pb, pc;
  119. a = dst[i - bpp];
  120. b = last[i];
  121. c = last[i - bpp];
  122. p = b - c;
  123. pc = a - c;
  124. pa = abs(p);
  125. pb = abs(pc);
  126. pc = abs(p + pc);
  127. if (pa <= pb && pa <= pc)
  128. p = a;
  129. else if (pb <= pc)
  130. p = b;
  131. else
  132. p = c;
  133. dst[i] = p + src[i];
  134. }
  135. break;
  136. }
  137. }
  138. static void png_handle_row(PNGDecodeState *s)
  139. {
  140. uint8_t *ptr, *last_row;
  141. ptr = s->image_buf + s->image_linesize * s->y;
  142. if (s->y == 0)
  143. last_row = s->empty_row;
  144. else
  145. last_row = ptr - s->image_linesize;
  146. png_filter_row(ptr, s->crow_buf[0], s->crow_buf + 1,
  147. last_row, s->row_size, s->bpp);
  148. }
  149. static int png_decode_idat(PNGDecodeState *s, ByteIOContext *f, int length)
  150. {
  151. uint8_t buf[IOBUF_SIZE];
  152. int buf_size;
  153. int ret;
  154. while (length > 0) {
  155. /* read the buffer */
  156. buf_size = IOBUF_SIZE;
  157. if (buf_size > length)
  158. buf_size = length;
  159. ret = get_buffer(f, buf, buf_size);
  160. if (ret != buf_size)
  161. return -1;
  162. s->zstream.avail_in = buf_size;
  163. s->zstream.next_in = buf;
  164. /* decode one line if possible */
  165. while (s->zstream.avail_in > 0) {
  166. ret = inflate(&s->zstream, Z_PARTIAL_FLUSH);
  167. if (ret != Z_OK && ret != Z_STREAM_END) {
  168. return -1;
  169. }
  170. if (s->zstream.avail_out == 0) {
  171. if (s->y < s->height) {
  172. png_handle_row(s);
  173. s->y++;
  174. if (s->y == s->height)
  175. s->state |= PNG_ALLIMAGE;
  176. }
  177. s->zstream.avail_out = s->crow_size;
  178. s->zstream.next_out = s->crow_buf;
  179. }
  180. }
  181. length -= buf_size;
  182. }
  183. return 0;
  184. }
  185. static int png_read(ByteIOContext *f,
  186. int (*alloc_cb)(void *opaque, AVImageInfo *info), void *opaque)
  187. {
  188. AVImageInfo info1, *info = &info1;
  189. PNGDecodeState s1, *s = &s1;
  190. uint32_t tag, length;
  191. int ret, crc;
  192. uint8_t buf[8];
  193. /* check signature */
  194. ret = get_buffer(f, buf, 8);
  195. if (ret != 8)
  196. return -1;
  197. if (memcmp(buf, pngsig, 8) != 0)
  198. return -1;
  199. memset(s, 0, sizeof(PNGDecodeState));
  200. /* init the zlib */
  201. s->zstream.zalloc = png_zalloc;
  202. s->zstream.zfree = png_zfree;
  203. s->zstream.opaque = NULL;
  204. ret = inflateInit(&s->zstream);
  205. if (ret != Z_OK)
  206. return -1;
  207. for(;;) {
  208. if (url_feof(f))
  209. goto fail;
  210. length = get_be32(f);
  211. if (length > 0x7fffffff)
  212. goto fail;
  213. tag = get_le32(f);
  214. #ifdef DEBUG
  215. printf("png: tag=%c%c%c%c length=%u\n",
  216. (tag & 0xff),
  217. ((tag >> 8) & 0xff),
  218. ((tag >> 16) & 0xff),
  219. ((tag >> 24) & 0xff), length);
  220. #endif
  221. switch(tag) {
  222. case MKTAG('I', 'H', 'D', 'R'):
  223. if (length != 13)
  224. goto fail;
  225. s->width = get_be32(f);
  226. s->height = get_be32(f);
  227. s->bit_depth = get_byte(f);
  228. s->color_type = get_byte(f);
  229. s->compression_type = get_byte(f);
  230. s->filter_type = get_byte(f);
  231. s->interlace_type = get_byte(f);
  232. crc = get_be32(f);
  233. s->state |= PNG_IHDR;
  234. #ifdef DEBUG
  235. printf("width=%d height=%d depth=%d color_type=%d compression_type=%d filter_type=%d interlace_type=%d\n",
  236. s->width, s->height, s->bit_depth, s->color_type,
  237. s->compression_type, s->filter_type, s->interlace_type);
  238. #endif
  239. break;
  240. case MKTAG('I', 'D', 'A', 'T'):
  241. if (!(s->state & PNG_IHDR))
  242. goto fail;
  243. if (!(s->state & PNG_IDAT)) {
  244. /* init image info */
  245. info->width = s->width;
  246. info->height = s->height;
  247. s->channels = 1;
  248. if ((s->color_type & (PNG_COLOR_MASK_COLOR | PNG_COLOR_MASK_PALETTE)) ==
  249. PNG_COLOR_MASK_COLOR)
  250. s->channels = 3;
  251. if (s->color_type & PNG_COLOR_MASK_ALPHA)
  252. s->channels++;
  253. s->bits_per_pixel = s->bit_depth * s->channels;
  254. s->bpp = (s->bits_per_pixel + 7) >> 3;
  255. if (s->bit_depth == 8 &&
  256. s->color_type == PNG_COLOR_TYPE_RGB) {
  257. info->pix_fmt = PIX_FMT_RGB24;
  258. s->row_size = s->width * 3;
  259. } else if (s->bit_depth == 8 &&
  260. s->color_type == PNG_COLOR_TYPE_GRAY) {
  261. info->pix_fmt = PIX_FMT_GRAY8;
  262. s->row_size = s->width;
  263. } else if (s->bit_depth == 1 &&
  264. s->color_type == PNG_COLOR_TYPE_GRAY) {
  265. info->pix_fmt = PIX_FMT_MONOBLACK;
  266. s->row_size = (s->width + 7) >> 3;
  267. } else if (s->color_type == PNG_COLOR_TYPE_PALETTE) {
  268. info->pix_fmt = PIX_FMT_PAL8;
  269. s->row_size = s->width;;
  270. } else {
  271. goto fail;
  272. }
  273. /* compute the compressed row size */
  274. if (!s->interlace_type) {
  275. s->crow_size = s->row_size + 1;
  276. } else {
  277. /* XXX: handle interlacing */
  278. goto fail;
  279. }
  280. ret = alloc_cb(opaque, info);
  281. if (ret)
  282. goto the_end;
  283. #ifdef DEBUG
  284. printf("row_size=%d crow_size =%d\n",
  285. s->row_size, s->crow_size);
  286. #endif
  287. s->image_buf = info->pict.data[0];
  288. s->image_linesize = info->pict.linesize[0];
  289. /* copy the palette if needed */
  290. if (s->color_type == PNG_COLOR_TYPE_PALETTE)
  291. memcpy(info->pict.data[1], s->palette, 256 * sizeof(uint32_t));
  292. /* empty row is used if differencing to the first row */
  293. s->empty_row = av_mallocz(s->row_size);
  294. if (!s->empty_row)
  295. goto fail;
  296. /* compressed row */
  297. s->crow_buf = av_malloc(s->crow_size);
  298. if (!s->crow_buf)
  299. goto fail;
  300. s->zstream.avail_out = s->crow_size;
  301. s->zstream.next_out = s->crow_buf;
  302. }
  303. s->state |= PNG_IDAT;
  304. if (png_decode_idat(s, f, length) < 0)
  305. goto fail;
  306. /* skip crc */
  307. crc = get_be32(f);
  308. break;
  309. case MKTAG('P', 'L', 'T', 'E'):
  310. {
  311. int n, i, r, g, b;
  312. if ((length % 3) != 0 || length > 256 * 3)
  313. goto skip_tag;
  314. /* read the palette */
  315. n = length / 3;
  316. for(i=0;i<n;i++) {
  317. r = get_byte(f);
  318. g = get_byte(f);
  319. b = get_byte(f);
  320. s->palette[i] = (0xff << 24) | (r << 16) | (g << 8) | b;
  321. }
  322. for(;i<256;i++) {
  323. s->palette[i] = (0xff << 24);
  324. }
  325. s->state |= PNG_PLTE;
  326. crc = get_be32(f);
  327. }
  328. break;
  329. case MKTAG('t', 'R', 'N', 'S'):
  330. {
  331. int v, i;
  332. /* read the transparency. XXX: Only palette mode supported */
  333. if (s->color_type != PNG_COLOR_TYPE_PALETTE ||
  334. length > 256 ||
  335. !(s->state & PNG_PLTE))
  336. goto skip_tag;
  337. for(i=0;i<length;i++) {
  338. v = get_byte(f);
  339. s->palette[i] = (s->palette[i] & 0x00ffffff) | (v << 24);
  340. }
  341. crc = get_be32(f);
  342. }
  343. break;
  344. case MKTAG('I', 'E', 'N', 'D'):
  345. if (!(s->state & PNG_ALLIMAGE))
  346. goto fail;
  347. crc = get_be32(f);
  348. goto exit_loop;
  349. default:
  350. /* skip tag */
  351. skip_tag:
  352. url_fskip(f, length + 4);
  353. break;
  354. }
  355. }
  356. exit_loop:
  357. ret = 0;
  358. the_end:
  359. inflateEnd(&s->zstream);
  360. av_free(s->crow_buf);
  361. return ret;
  362. fail:
  363. ret = -1;
  364. goto the_end;
  365. }
  366. static void png_write_chunk(ByteIOContext *f, uint32_t tag,
  367. const uint8_t *buf, int length)
  368. {
  369. uint32_t crc;
  370. uint8_t tagbuf[4];
  371. put_be32(f, length);
  372. crc = crc32(0, Z_NULL, 0);
  373. tagbuf[0] = tag;
  374. tagbuf[1] = tag >> 8;
  375. tagbuf[2] = tag >> 16;
  376. tagbuf[3] = tag >> 24;
  377. crc = crc32(crc, tagbuf, 4);
  378. put_le32(f, tag);
  379. if (length > 0) {
  380. crc = crc32(crc, buf, length);
  381. put_buffer(f, buf, length);
  382. }
  383. put_be32(f, crc);
  384. }
  385. /* XXX: use avcodec generic function ? */
  386. static void to_be32(uint8_t *p, uint32_t v)
  387. {
  388. p[0] = v >> 24;
  389. p[1] = v >> 16;
  390. p[2] = v >> 8;
  391. p[3] = v;
  392. }
  393. static int png_write(ByteIOContext *f, AVImageInfo *info)
  394. {
  395. int bit_depth, color_type, y, len, row_size, ret;
  396. uint8_t *ptr;
  397. uint8_t buf[IOBUF_SIZE];
  398. uint8_t *crow_buf = NULL;
  399. z_stream zstream;
  400. switch(info->pix_fmt) {
  401. case PIX_FMT_RGB24:
  402. bit_depth = 8;
  403. color_type = PNG_COLOR_TYPE_RGB;
  404. row_size = info->width * 3;
  405. break;
  406. case PIX_FMT_GRAY8:
  407. bit_depth = 8;
  408. color_type = PNG_COLOR_TYPE_GRAY;
  409. row_size = info->width;
  410. break;
  411. case PIX_FMT_MONOBLACK:
  412. bit_depth = 1;
  413. color_type = PNG_COLOR_TYPE_GRAY;
  414. row_size = (info->width + 7) >> 3;
  415. break;
  416. case PIX_FMT_PAL8:
  417. bit_depth = 8;
  418. color_type = PNG_COLOR_TYPE_PALETTE;
  419. row_size = info->width;
  420. break;
  421. default:
  422. return -1;
  423. }
  424. zstream.zalloc = png_zalloc;
  425. zstream.zfree = png_zfree;
  426. zstream.opaque = NULL;
  427. ret = deflateInit2(&zstream, Z_DEFAULT_COMPRESSION,
  428. Z_DEFLATED, 15, 8, Z_DEFAULT_STRATEGY);
  429. if (ret != Z_OK)
  430. return -1;
  431. crow_buf = av_malloc(row_size + 1);
  432. if (!crow_buf)
  433. goto fail;
  434. /* write png header */
  435. put_buffer(f, pngsig, 8);
  436. to_be32(buf, info->width);
  437. to_be32(buf + 4, info->height);
  438. buf[8] = bit_depth;
  439. buf[9] = color_type;
  440. buf[10] = 0; /* compression type */
  441. buf[11] = 0; /* filter type */
  442. buf[12] = 0; /* interlace type */
  443. png_write_chunk(f, MKTAG('I', 'H', 'D', 'R'), buf, 13);
  444. /* put the palette if needed */
  445. if (color_type == PNG_COLOR_TYPE_PALETTE) {
  446. int has_alpha, alpha, i;
  447. unsigned int v;
  448. uint32_t *palette;
  449. uint8_t *alpha_ptr;
  450. palette = (uint32_t *)info->pict.data[1];
  451. ptr = buf;
  452. alpha_ptr = buf + 256 * 3;
  453. has_alpha = 0;
  454. for(i = 0; i < 256; i++) {
  455. v = palette[i];
  456. alpha = v >> 24;
  457. if (alpha != 0xff)
  458. has_alpha = 1;
  459. *alpha_ptr++ = alpha;
  460. ptr[0] = v >> 16;
  461. ptr[1] = v >> 8;
  462. ptr[2] = v;
  463. ptr += 3;
  464. }
  465. png_write_chunk(f, MKTAG('P', 'L', 'T', 'E'), buf, 256 * 3);
  466. if (has_alpha) {
  467. png_write_chunk(f, MKTAG('t', 'R', 'N', 'S'), buf + 256 * 3, 256);
  468. }
  469. }
  470. /* now put each row */
  471. zstream.avail_out = IOBUF_SIZE;
  472. zstream.next_out = buf;
  473. for(y = 0;y < info->height; y++) {
  474. /* XXX: do filtering */
  475. ptr = info->pict.data[0] + y * info->pict.linesize[0];
  476. memcpy(crow_buf + 1, ptr, row_size);
  477. crow_buf[0] = PNG_FILTER_VALUE_NONE;
  478. zstream.avail_in = row_size + 1;
  479. zstream.next_in = crow_buf;
  480. while (zstream.avail_in > 0) {
  481. ret = deflate(&zstream, Z_NO_FLUSH);
  482. if (ret != Z_OK)
  483. goto fail;
  484. if (zstream.avail_out == 0) {
  485. png_write_chunk(f, MKTAG('I', 'D', 'A', 'T'), buf, IOBUF_SIZE);
  486. zstream.avail_out = IOBUF_SIZE;
  487. zstream.next_out = buf;
  488. }
  489. }
  490. }
  491. /* compress last bytes */
  492. for(;;) {
  493. ret = deflate(&zstream, Z_FINISH);
  494. if (ret == Z_OK || ret == Z_STREAM_END) {
  495. len = IOBUF_SIZE - zstream.avail_out;
  496. if (len > 0) {
  497. png_write_chunk(f, MKTAG('I', 'D', 'A', 'T'), buf, len);
  498. }
  499. zstream.avail_out = IOBUF_SIZE;
  500. zstream.next_out = buf;
  501. if (ret == Z_STREAM_END)
  502. break;
  503. } else {
  504. goto fail;
  505. }
  506. }
  507. png_write_chunk(f, MKTAG('I', 'E', 'N', 'D'), NULL, 0);
  508. put_flush_packet(f);
  509. ret = 0;
  510. the_end:
  511. av_free(crow_buf);
  512. deflateEnd(&zstream);
  513. return ret;
  514. fail:
  515. ret = -1;
  516. goto the_end;
  517. }
  518. AVImageFormat png_image_format = {
  519. "png",
  520. "png",
  521. png_probe,
  522. png_read,
  523. (1 << PIX_FMT_RGB24) | (1 << PIX_FMT_GRAY8) | (1 << PIX_FMT_MONOBLACK) | (1 << PIX_FMT_PAL8),
  524. png_write,
  525. };