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.

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