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.

890 lines
26KB

  1. /*
  2. * PNG image format
  3. * Copyright (c) 2003 Fabrice Bellard.
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "avformat.h"
  22. /* TODO:
  23. * - add 2, 4 and 16 bit depth support
  24. * - use filters when generating a png (better compression)
  25. */
  26. #ifdef CONFIG_ZLIB
  27. #include <zlib.h>
  28. //#define DEBUG
  29. #define PNG_COLOR_MASK_PALETTE 1
  30. #define PNG_COLOR_MASK_COLOR 2
  31. #define PNG_COLOR_MASK_ALPHA 4
  32. #define PNG_COLOR_TYPE_GRAY 0
  33. #define PNG_COLOR_TYPE_PALETTE (PNG_COLOR_MASK_COLOR | PNG_COLOR_MASK_PALETTE)
  34. #define PNG_COLOR_TYPE_RGB (PNG_COLOR_MASK_COLOR)
  35. #define PNG_COLOR_TYPE_RGB_ALPHA (PNG_COLOR_MASK_COLOR | PNG_COLOR_MASK_ALPHA)
  36. #define PNG_COLOR_TYPE_GRAY_ALPHA (PNG_COLOR_MASK_ALPHA)
  37. #define PNG_FILTER_VALUE_NONE 0
  38. #define PNG_FILTER_VALUE_SUB 1
  39. #define PNG_FILTER_VALUE_UP 2
  40. #define PNG_FILTER_VALUE_AVG 3
  41. #define PNG_FILTER_VALUE_PAETH 4
  42. #define PNG_IHDR 0x0001
  43. #define PNG_IDAT 0x0002
  44. #define PNG_ALLIMAGE 0x0004
  45. #define PNG_PLTE 0x0008
  46. #define NB_PASSES 7
  47. #define IOBUF_SIZE 4096
  48. typedef struct PNGDecodeState {
  49. int state;
  50. int width, height;
  51. int bit_depth;
  52. int color_type;
  53. int compression_type;
  54. int interlace_type;
  55. int filter_type;
  56. int channels;
  57. int bits_per_pixel;
  58. int bpp;
  59. uint8_t *image_buf;
  60. int image_linesize;
  61. uint32_t palette[256];
  62. uint8_t *crow_buf;
  63. uint8_t *last_row;
  64. uint8_t *tmp_row;
  65. int pass;
  66. int crow_size; /* compressed row size (include filter type) */
  67. int row_size; /* decompressed row size */
  68. int pass_row_size; /* decompress row size of the current pass */
  69. int y;
  70. z_stream zstream;
  71. } PNGDecodeState;
  72. static const uint8_t pngsig[8] = {137, 80, 78, 71, 13, 10, 26, 10};
  73. /* Mask to determine which y pixels are valid in a pass */
  74. static const uint8_t png_pass_ymask[NB_PASSES] = {
  75. 0x80, 0x80, 0x08, 0x88, 0x22, 0xaa, 0x55,
  76. };
  77. /* Mask to determine which y pixels can be written in a pass */
  78. static const uint8_t png_pass_dsp_ymask[NB_PASSES] = {
  79. 0xff, 0xff, 0x0f, 0xcc, 0x33, 0xff, 0x55,
  80. };
  81. /* minimum x value */
  82. static const uint8_t png_pass_xmin[NB_PASSES] = {
  83. 0, 4, 0, 2, 0, 1, 0
  84. };
  85. /* x shift to get row width */
  86. static const uint8_t png_pass_xshift[NB_PASSES] = {
  87. 3, 3, 2, 2, 1, 1, 0
  88. };
  89. /* Mask to determine which pixels are valid in a pass */
  90. static const uint8_t png_pass_mask[NB_PASSES] = {
  91. 0x80, 0x08, 0x88, 0x22, 0xaa, 0x55, 0xff
  92. };
  93. /* Mask to determine which pixels to overwrite while displaying */
  94. static const uint8_t png_pass_dsp_mask[NB_PASSES] = {
  95. 0xff, 0x0f, 0xff, 0x33, 0xff, 0x55, 0xff
  96. };
  97. static int png_probe(AVProbeData *pd)
  98. {
  99. if (pd->buf_size >= 8 &&
  100. memcmp(pd->buf, pngsig, 8) == 0)
  101. return AVPROBE_SCORE_MAX;
  102. else
  103. return 0;
  104. }
  105. static void *png_zalloc(void *opaque, unsigned int items, unsigned int size)
  106. {
  107. return av_malloc(items * size);
  108. }
  109. static void png_zfree(void *opaque, void *ptr)
  110. {
  111. av_free(ptr);
  112. }
  113. static int png_get_nb_channels(int color_type)
  114. {
  115. int channels;
  116. channels = 1;
  117. if ((color_type & (PNG_COLOR_MASK_COLOR | PNG_COLOR_MASK_PALETTE)) ==
  118. PNG_COLOR_MASK_COLOR)
  119. channels = 3;
  120. if (color_type & PNG_COLOR_MASK_ALPHA)
  121. channels++;
  122. return channels;
  123. }
  124. /* compute the row size of an interleaved pass */
  125. static int png_pass_row_size(int pass, int bits_per_pixel, int width)
  126. {
  127. int shift, xmin, pass_width;
  128. xmin = png_pass_xmin[pass];
  129. if (width <= xmin)
  130. return 0;
  131. shift = png_pass_xshift[pass];
  132. pass_width = (width - xmin + (1 << shift) - 1) >> shift;
  133. return (pass_width * bits_per_pixel + 7) >> 3;
  134. }
  135. /* NOTE: we try to construct a good looking image at each pass. width
  136. is the original image width. We also do pixel format convertion at
  137. this stage */
  138. static void png_put_interlaced_row(uint8_t *dst, int width,
  139. int bits_per_pixel, int pass,
  140. int color_type, const uint8_t *src)
  141. {
  142. int x, mask, dsp_mask, j, src_x, b, bpp;
  143. uint8_t *d;
  144. const uint8_t *s;
  145. mask = png_pass_mask[pass];
  146. dsp_mask = png_pass_dsp_mask[pass];
  147. switch(bits_per_pixel) {
  148. case 1:
  149. /* we must intialize the line to zero before writing to it */
  150. if (pass == 0)
  151. memset(dst, 0, (width + 7) >> 3);
  152. src_x = 0;
  153. for(x = 0; x < width; x++) {
  154. j = (x & 7);
  155. if ((dsp_mask << j) & 0x80) {
  156. b = (src[src_x >> 3] >> (7 - (src_x & 7))) & 1;
  157. dst[x >> 3] |= b << (7 - j);
  158. }
  159. if ((mask << j) & 0x80)
  160. src_x++;
  161. }
  162. break;
  163. default:
  164. bpp = bits_per_pixel >> 3;
  165. d = dst;
  166. s = src;
  167. if (color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
  168. for(x = 0; x < width; x++) {
  169. j = x & 7;
  170. if ((dsp_mask << j) & 0x80) {
  171. *(uint32_t *)d = (s[3] << 24) | (s[0] << 16) | (s[1] << 8) | s[2];
  172. }
  173. d += bpp;
  174. if ((mask << j) & 0x80)
  175. s += bpp;
  176. }
  177. } else {
  178. for(x = 0; x < width; x++) {
  179. j = x & 7;
  180. if ((dsp_mask << j) & 0x80) {
  181. memcpy(d, s, bpp);
  182. }
  183. d += bpp;
  184. if ((mask << j) & 0x80)
  185. s += bpp;
  186. }
  187. }
  188. break;
  189. }
  190. }
  191. static void png_get_interlaced_row(uint8_t *dst, int row_size,
  192. int bits_per_pixel, int pass,
  193. const uint8_t *src, int width)
  194. {
  195. int x, mask, dst_x, j, b, bpp;
  196. uint8_t *d;
  197. const uint8_t *s;
  198. mask = png_pass_mask[pass];
  199. switch(bits_per_pixel) {
  200. case 1:
  201. memset(dst, 0, row_size);
  202. dst_x = 0;
  203. for(x = 0; x < width; x++) {
  204. j = (x & 7);
  205. if ((mask << j) & 0x80) {
  206. b = (src[x >> 3] >> (7 - j)) & 1;
  207. dst[dst_x >> 3] |= b << (7 - (dst_x & 7));
  208. dst_x++;
  209. }
  210. }
  211. break;
  212. default:
  213. bpp = bits_per_pixel >> 3;
  214. d = dst;
  215. s = src;
  216. for(x = 0; x < width; x++) {
  217. j = x & 7;
  218. if ((mask << j) & 0x80) {
  219. memcpy(d, s, bpp);
  220. d += bpp;
  221. }
  222. s += bpp;
  223. }
  224. break;
  225. }
  226. }
  227. /* XXX: optimize */
  228. /* NOTE: 'dst' can be equal to 'last' */
  229. static void png_filter_row(uint8_t *dst, int filter_type,
  230. uint8_t *src, uint8_t *last, int size, int bpp)
  231. {
  232. int i, p;
  233. switch(filter_type) {
  234. case PNG_FILTER_VALUE_NONE:
  235. memcpy(dst, src, size);
  236. break;
  237. case PNG_FILTER_VALUE_SUB:
  238. for(i = 0; i < bpp; i++) {
  239. dst[i] = src[i];
  240. }
  241. for(i = bpp; i < size; i++) {
  242. p = dst[i - bpp];
  243. dst[i] = p + src[i];
  244. }
  245. break;
  246. case PNG_FILTER_VALUE_UP:
  247. for(i = 0; i < size; i++) {
  248. p = last[i];
  249. dst[i] = p + src[i];
  250. }
  251. break;
  252. case PNG_FILTER_VALUE_AVG:
  253. for(i = 0; i < bpp; i++) {
  254. p = (last[i] >> 1);
  255. dst[i] = p + src[i];
  256. }
  257. for(i = bpp; i < size; i++) {
  258. p = ((dst[i - bpp] + last[i]) >> 1);
  259. dst[i] = p + src[i];
  260. }
  261. break;
  262. case PNG_FILTER_VALUE_PAETH:
  263. for(i = 0; i < bpp; i++) {
  264. p = last[i];
  265. dst[i] = p + src[i];
  266. }
  267. for(i = bpp; i < size; i++) {
  268. int a, b, c, pa, pb, pc;
  269. a = dst[i - bpp];
  270. b = last[i];
  271. c = last[i - bpp];
  272. p = b - c;
  273. pc = a - c;
  274. pa = abs(p);
  275. pb = abs(pc);
  276. pc = abs(p + pc);
  277. if (pa <= pb && pa <= pc)
  278. p = a;
  279. else if (pb <= pc)
  280. p = b;
  281. else
  282. p = c;
  283. dst[i] = p + src[i];
  284. }
  285. break;
  286. }
  287. }
  288. static void convert_from_rgba32(uint8_t *dst, const uint8_t *src, int width)
  289. {
  290. uint8_t *d;
  291. int j;
  292. unsigned int v;
  293. d = dst;
  294. for(j = 0; j < width; j++) {
  295. v = ((const uint32_t *)src)[j];
  296. d[0] = v >> 16;
  297. d[1] = v >> 8;
  298. d[2] = v;
  299. d[3] = v >> 24;
  300. d += 4;
  301. }
  302. }
  303. static void convert_to_rgba32(uint8_t *dst, const uint8_t *src, int width)
  304. {
  305. int j;
  306. unsigned int r, g, b, a;
  307. for(j = 0;j < width; j++) {
  308. r = src[0];
  309. g = src[1];
  310. b = src[2];
  311. a = src[3];
  312. *(uint32_t *)dst = (a << 24) | (r << 16) | (g << 8) | b;
  313. dst += 4;
  314. src += 4;
  315. }
  316. }
  317. /* process exactly one decompressed row */
  318. static void png_handle_row(PNGDecodeState *s)
  319. {
  320. uint8_t *ptr, *last_row;
  321. int got_line;
  322. if (!s->interlace_type) {
  323. ptr = s->image_buf + s->image_linesize * s->y;
  324. /* need to swap bytes correctly for RGB_ALPHA */
  325. if (s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
  326. png_filter_row(s->tmp_row, s->crow_buf[0], s->crow_buf + 1,
  327. s->last_row, s->row_size, s->bpp);
  328. memcpy(s->last_row, s->tmp_row, s->row_size);
  329. convert_to_rgba32(ptr, s->tmp_row, s->width);
  330. } else {
  331. /* in normal case, we avoid one copy */
  332. if (s->y == 0)
  333. last_row = s->last_row;
  334. else
  335. last_row = ptr - s->image_linesize;
  336. png_filter_row(ptr, s->crow_buf[0], s->crow_buf + 1,
  337. last_row, s->row_size, s->bpp);
  338. }
  339. s->y++;
  340. if (s->y == s->height) {
  341. s->state |= PNG_ALLIMAGE;
  342. }
  343. } else {
  344. got_line = 0;
  345. for(;;) {
  346. ptr = s->image_buf + s->image_linesize * s->y;
  347. if ((png_pass_ymask[s->pass] << (s->y & 7)) & 0x80) {
  348. /* if we already read one row, it is time to stop to
  349. wait for the next one */
  350. if (got_line)
  351. break;
  352. png_filter_row(s->tmp_row, s->crow_buf[0], s->crow_buf + 1,
  353. s->last_row, s->pass_row_size, s->bpp);
  354. memcpy(s->last_row, s->tmp_row, s->pass_row_size);
  355. got_line = 1;
  356. }
  357. if ((png_pass_dsp_ymask[s->pass] << (s->y & 7)) & 0x80) {
  358. /* NOTE: rgba32 is handled directly in png_put_interlaced_row */
  359. png_put_interlaced_row(ptr, s->width, s->bits_per_pixel, s->pass,
  360. s->color_type, s->last_row);
  361. }
  362. s->y++;
  363. if (s->y == s->height) {
  364. for(;;) {
  365. if (s->pass == NB_PASSES - 1) {
  366. s->state |= PNG_ALLIMAGE;
  367. goto the_end;
  368. } else {
  369. s->pass++;
  370. s->y = 0;
  371. s->pass_row_size = png_pass_row_size(s->pass,
  372. s->bits_per_pixel,
  373. s->width);
  374. s->crow_size = s->pass_row_size + 1;
  375. if (s->pass_row_size != 0)
  376. break;
  377. /* skip pass if empty row */
  378. }
  379. }
  380. }
  381. }
  382. the_end: ;
  383. }
  384. }
  385. static int png_decode_idat(PNGDecodeState *s, ByteIOContext *f, int length)
  386. {
  387. uint8_t buf[IOBUF_SIZE];
  388. int buf_size;
  389. int ret;
  390. while (length > 0) {
  391. /* read the buffer */
  392. buf_size = IOBUF_SIZE;
  393. if (buf_size > length)
  394. buf_size = length;
  395. ret = get_buffer(f, buf, buf_size);
  396. if (ret != buf_size)
  397. return -1;
  398. s->zstream.avail_in = buf_size;
  399. s->zstream.next_in = buf;
  400. /* decode one line if possible */
  401. while (s->zstream.avail_in > 0) {
  402. ret = inflate(&s->zstream, Z_PARTIAL_FLUSH);
  403. if (ret != Z_OK && ret != Z_STREAM_END) {
  404. return -1;
  405. }
  406. if (s->zstream.avail_out == 0) {
  407. if (!(s->state & PNG_ALLIMAGE)) {
  408. png_handle_row(s);
  409. }
  410. s->zstream.avail_out = s->crow_size;
  411. s->zstream.next_out = s->crow_buf;
  412. }
  413. }
  414. length -= buf_size;
  415. }
  416. return 0;
  417. }
  418. static int png_read(ByteIOContext *f,
  419. int (*alloc_cb)(void *opaque, AVImageInfo *info), void *opaque)
  420. {
  421. AVImageInfo info1, *info = &info1;
  422. PNGDecodeState s1, *s = &s1;
  423. uint32_t tag, length;
  424. int ret, crc;
  425. uint8_t buf[8];
  426. /* check signature */
  427. ret = get_buffer(f, buf, 8);
  428. if (ret != 8)
  429. return -1;
  430. if (memcmp(buf, pngsig, 8) != 0)
  431. return -1;
  432. memset(s, 0, sizeof(PNGDecodeState));
  433. /* init the zlib */
  434. s->zstream.zalloc = png_zalloc;
  435. s->zstream.zfree = png_zfree;
  436. s->zstream.opaque = NULL;
  437. ret = inflateInit(&s->zstream);
  438. if (ret != Z_OK)
  439. return -1;
  440. for(;;) {
  441. if (url_feof(f))
  442. goto fail;
  443. length = get_be32(f);
  444. if (length > 0x7fffffff)
  445. goto fail;
  446. tag = get_le32(f);
  447. #ifdef DEBUG
  448. printf("png: tag=%c%c%c%c length=%u\n",
  449. (tag & 0xff),
  450. ((tag >> 8) & 0xff),
  451. ((tag >> 16) & 0xff),
  452. ((tag >> 24) & 0xff), length);
  453. #endif
  454. switch(tag) {
  455. case MKTAG('I', 'H', 'D', 'R'):
  456. if (length != 13)
  457. goto fail;
  458. s->width = get_be32(f);
  459. s->height = get_be32(f);
  460. s->bit_depth = get_byte(f);
  461. s->color_type = get_byte(f);
  462. s->compression_type = get_byte(f);
  463. s->filter_type = get_byte(f);
  464. s->interlace_type = get_byte(f);
  465. crc = get_be32(f);
  466. s->state |= PNG_IHDR;
  467. #ifdef DEBUG
  468. printf("width=%d height=%d depth=%d color_type=%d compression_type=%d filter_type=%d interlace_type=%d\n",
  469. s->width, s->height, s->bit_depth, s->color_type,
  470. s->compression_type, s->filter_type, s->interlace_type);
  471. #endif
  472. break;
  473. case MKTAG('I', 'D', 'A', 'T'):
  474. if (!(s->state & PNG_IHDR))
  475. goto fail;
  476. if (!(s->state & PNG_IDAT)) {
  477. /* init image info */
  478. info->width = s->width;
  479. info->height = s->height;
  480. info->interleaved = (s->interlace_type != 0);
  481. s->channels = png_get_nb_channels(s->color_type);
  482. s->bits_per_pixel = s->bit_depth * s->channels;
  483. s->bpp = (s->bits_per_pixel + 7) >> 3;
  484. s->row_size = (info->width * s->bits_per_pixel + 7) >> 3;
  485. if (s->bit_depth == 8 &&
  486. s->color_type == PNG_COLOR_TYPE_RGB) {
  487. info->pix_fmt = PIX_FMT_RGB24;
  488. } else if (s->bit_depth == 8 &&
  489. s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
  490. info->pix_fmt = PIX_FMT_RGBA32;
  491. } else if (s->bit_depth == 8 &&
  492. s->color_type == PNG_COLOR_TYPE_GRAY) {
  493. info->pix_fmt = PIX_FMT_GRAY8;
  494. } else if (s->bit_depth == 1 &&
  495. s->color_type == PNG_COLOR_TYPE_GRAY) {
  496. info->pix_fmt = PIX_FMT_MONOBLACK;
  497. } else if (s->color_type == PNG_COLOR_TYPE_PALETTE) {
  498. info->pix_fmt = PIX_FMT_PAL8;
  499. } else {
  500. goto fail;
  501. }
  502. ret = alloc_cb(opaque, info);
  503. if (ret)
  504. goto the_end;
  505. /* compute the compressed row size */
  506. if (!s->interlace_type) {
  507. s->crow_size = s->row_size + 1;
  508. } else {
  509. s->pass = 0;
  510. s->pass_row_size = png_pass_row_size(s->pass,
  511. s->bits_per_pixel,
  512. s->width);
  513. s->crow_size = s->pass_row_size + 1;
  514. }
  515. #ifdef DEBUG
  516. printf("row_size=%d crow_size =%d\n",
  517. s->row_size, s->crow_size);
  518. #endif
  519. s->image_buf = info->pict.data[0];
  520. s->image_linesize = info->pict.linesize[0];
  521. /* copy the palette if needed */
  522. if (s->color_type == PNG_COLOR_TYPE_PALETTE)
  523. memcpy(info->pict.data[1], s->palette, 256 * sizeof(uint32_t));
  524. /* empty row is used if differencing to the first row */
  525. s->last_row = av_mallocz(s->row_size);
  526. if (!s->last_row)
  527. goto fail;
  528. if (s->interlace_type ||
  529. s->color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
  530. s->tmp_row = av_malloc(s->row_size);
  531. if (!s->tmp_row)
  532. goto fail;
  533. }
  534. /* compressed row */
  535. s->crow_buf = av_malloc(s->row_size + 1);
  536. if (!s->crow_buf)
  537. goto fail;
  538. s->zstream.avail_out = s->crow_size;
  539. s->zstream.next_out = s->crow_buf;
  540. }
  541. s->state |= PNG_IDAT;
  542. if (png_decode_idat(s, f, length) < 0)
  543. goto fail;
  544. /* skip crc */
  545. crc = get_be32(f);
  546. break;
  547. case MKTAG('P', 'L', 'T', 'E'):
  548. {
  549. int n, i, r, g, b;
  550. if ((length % 3) != 0 || length > 256 * 3)
  551. goto skip_tag;
  552. /* read the palette */
  553. n = length / 3;
  554. for(i=0;i<n;i++) {
  555. r = get_byte(f);
  556. g = get_byte(f);
  557. b = get_byte(f);
  558. s->palette[i] = (0xff << 24) | (r << 16) | (g << 8) | b;
  559. }
  560. for(;i<256;i++) {
  561. s->palette[i] = (0xff << 24);
  562. }
  563. s->state |= PNG_PLTE;
  564. crc = get_be32(f);
  565. }
  566. break;
  567. case MKTAG('t', 'R', 'N', 'S'):
  568. {
  569. int v, i;
  570. /* read the transparency. XXX: Only palette mode supported */
  571. if (s->color_type != PNG_COLOR_TYPE_PALETTE ||
  572. length > 256 ||
  573. !(s->state & PNG_PLTE))
  574. goto skip_tag;
  575. for(i=0;i<length;i++) {
  576. v = get_byte(f);
  577. s->palette[i] = (s->palette[i] & 0x00ffffff) | (v << 24);
  578. }
  579. crc = get_be32(f);
  580. }
  581. break;
  582. case MKTAG('I', 'E', 'N', 'D'):
  583. if (!(s->state & PNG_ALLIMAGE))
  584. goto fail;
  585. crc = get_be32(f);
  586. goto exit_loop;
  587. default:
  588. /* skip tag */
  589. skip_tag:
  590. url_fskip(f, length + 4);
  591. break;
  592. }
  593. }
  594. exit_loop:
  595. ret = 0;
  596. the_end:
  597. inflateEnd(&s->zstream);
  598. av_free(s->crow_buf);
  599. av_free(s->last_row);
  600. av_free(s->tmp_row);
  601. return ret;
  602. fail:
  603. ret = -1;
  604. goto the_end;
  605. }
  606. static void png_write_chunk(ByteIOContext *f, uint32_t tag,
  607. const uint8_t *buf, int length)
  608. {
  609. uint32_t crc;
  610. uint8_t tagbuf[4];
  611. put_be32(f, length);
  612. crc = crc32(0, Z_NULL, 0);
  613. tagbuf[0] = tag;
  614. tagbuf[1] = tag >> 8;
  615. tagbuf[2] = tag >> 16;
  616. tagbuf[3] = tag >> 24;
  617. crc = crc32(crc, tagbuf, 4);
  618. put_le32(f, tag);
  619. if (length > 0) {
  620. crc = crc32(crc, buf, length);
  621. put_buffer(f, buf, length);
  622. }
  623. put_be32(f, crc);
  624. }
  625. /* XXX: use avcodec generic function ? */
  626. static void to_be32(uint8_t *p, uint32_t v)
  627. {
  628. p[0] = v >> 24;
  629. p[1] = v >> 16;
  630. p[2] = v >> 8;
  631. p[3] = v;
  632. }
  633. typedef struct PNGEncodeState {
  634. ByteIOContext *f;
  635. z_stream zstream;
  636. uint8_t buf[IOBUF_SIZE];
  637. } PNGEncodeState;
  638. /* XXX: do filtering */
  639. static int png_write_row(PNGEncodeState *s, const uint8_t *data, int size)
  640. {
  641. int ret;
  642. s->zstream.avail_in = size;
  643. s->zstream.next_in = (uint8_t *)data;
  644. while (s->zstream.avail_in > 0) {
  645. ret = deflate(&s->zstream, Z_NO_FLUSH);
  646. if (ret != Z_OK)
  647. return -1;
  648. if (s->zstream.avail_out == 0) {
  649. png_write_chunk(s->f, MKTAG('I', 'D', 'A', 'T'), s->buf, IOBUF_SIZE);
  650. s->zstream.avail_out = IOBUF_SIZE;
  651. s->zstream.next_out = s->buf;
  652. }
  653. }
  654. return 0;
  655. }
  656. static int png_write(ByteIOContext *f, AVImageInfo *info)
  657. {
  658. PNGEncodeState s1, *s = &s1;
  659. int bit_depth, color_type, y, len, row_size, ret, is_progressive;
  660. int bits_per_pixel, pass_row_size;
  661. uint8_t *ptr;
  662. uint8_t *crow_buf = NULL;
  663. uint8_t *tmp_buf = NULL;
  664. s->f = f;
  665. is_progressive = info->interleaved;
  666. switch(info->pix_fmt) {
  667. case PIX_FMT_RGBA32:
  668. bit_depth = 8;
  669. color_type = PNG_COLOR_TYPE_RGB_ALPHA;
  670. break;
  671. case PIX_FMT_RGB24:
  672. bit_depth = 8;
  673. color_type = PNG_COLOR_TYPE_RGB;
  674. break;
  675. case PIX_FMT_GRAY8:
  676. bit_depth = 8;
  677. color_type = PNG_COLOR_TYPE_GRAY;
  678. break;
  679. case PIX_FMT_MONOBLACK:
  680. bit_depth = 1;
  681. color_type = PNG_COLOR_TYPE_GRAY;
  682. break;
  683. case PIX_FMT_PAL8:
  684. bit_depth = 8;
  685. color_type = PNG_COLOR_TYPE_PALETTE;
  686. break;
  687. default:
  688. return -1;
  689. }
  690. bits_per_pixel = png_get_nb_channels(color_type) * bit_depth;
  691. row_size = (info->width * bits_per_pixel + 7) >> 3;
  692. s->zstream.zalloc = png_zalloc;
  693. s->zstream.zfree = png_zfree;
  694. s->zstream.opaque = NULL;
  695. ret = deflateInit2(&s->zstream, Z_DEFAULT_COMPRESSION,
  696. Z_DEFLATED, 15, 8, Z_DEFAULT_STRATEGY);
  697. if (ret != Z_OK)
  698. return -1;
  699. crow_buf = av_malloc(row_size + 1);
  700. if (!crow_buf)
  701. goto fail;
  702. if (is_progressive) {
  703. tmp_buf = av_malloc(row_size + 1);
  704. if (!tmp_buf)
  705. goto fail;
  706. }
  707. /* write png header */
  708. put_buffer(f, pngsig, 8);
  709. to_be32(s->buf, info->width);
  710. to_be32(s->buf + 4, info->height);
  711. s->buf[8] = bit_depth;
  712. s->buf[9] = color_type;
  713. s->buf[10] = 0; /* compression type */
  714. s->buf[11] = 0; /* filter type */
  715. s->buf[12] = is_progressive; /* interlace type */
  716. png_write_chunk(f, MKTAG('I', 'H', 'D', 'R'), s->buf, 13);
  717. /* put the palette if needed */
  718. if (color_type == PNG_COLOR_TYPE_PALETTE) {
  719. int has_alpha, alpha, i;
  720. unsigned int v;
  721. uint32_t *palette;
  722. uint8_t *alpha_ptr;
  723. palette = (uint32_t *)info->pict.data[1];
  724. ptr = s->buf;
  725. alpha_ptr = s->buf + 256 * 3;
  726. has_alpha = 0;
  727. for(i = 0; i < 256; i++) {
  728. v = palette[i];
  729. alpha = v >> 24;
  730. if (alpha != 0xff)
  731. has_alpha = 1;
  732. *alpha_ptr++ = alpha;
  733. ptr[0] = v >> 16;
  734. ptr[1] = v >> 8;
  735. ptr[2] = v;
  736. ptr += 3;
  737. }
  738. png_write_chunk(f, MKTAG('P', 'L', 'T', 'E'), s->buf, 256 * 3);
  739. if (has_alpha) {
  740. png_write_chunk(f, MKTAG('t', 'R', 'N', 'S'), s->buf + 256 * 3, 256);
  741. }
  742. }
  743. /* now put each row */
  744. s->zstream.avail_out = IOBUF_SIZE;
  745. s->zstream.next_out = s->buf;
  746. if (is_progressive) {
  747. uint8_t *ptr1;
  748. int pass;
  749. for(pass = 0; pass < NB_PASSES; pass++) {
  750. /* NOTE: a pass is completely omited if no pixels would be
  751. output */
  752. pass_row_size = png_pass_row_size(pass, bits_per_pixel, info->width);
  753. if (pass_row_size > 0) {
  754. for(y = 0; y < info->height; y++) {
  755. if ((png_pass_ymask[pass] << (y & 7)) & 0x80) {
  756. ptr = info->pict.data[0] + y * info->pict.linesize[0];
  757. if (color_type == PNG_COLOR_TYPE_RGB_ALPHA) {
  758. convert_from_rgba32(tmp_buf, ptr, info->width);
  759. ptr1 = tmp_buf;
  760. } else {
  761. ptr1 = ptr;
  762. }
  763. png_get_interlaced_row(crow_buf + 1, pass_row_size,
  764. bits_per_pixel, pass,
  765. ptr1, info->width);
  766. crow_buf[0] = PNG_FILTER_VALUE_NONE;
  767. png_write_row(s, crow_buf, pass_row_size + 1);
  768. }
  769. }
  770. }
  771. }
  772. } else {
  773. for(y = 0; y < info->height; y++) {
  774. ptr = info->pict.data[0] + y * info->pict.linesize[0];
  775. if (color_type == PNG_COLOR_TYPE_RGB_ALPHA)
  776. convert_from_rgba32(crow_buf + 1, ptr, info->width);
  777. else
  778. memcpy(crow_buf + 1, ptr, row_size);
  779. crow_buf[0] = PNG_FILTER_VALUE_NONE;
  780. png_write_row(s, crow_buf, row_size + 1);
  781. }
  782. }
  783. /* compress last bytes */
  784. for(;;) {
  785. ret = deflate(&s->zstream, Z_FINISH);
  786. if (ret == Z_OK || ret == Z_STREAM_END) {
  787. len = IOBUF_SIZE - s->zstream.avail_out;
  788. if (len > 0) {
  789. png_write_chunk(f, MKTAG('I', 'D', 'A', 'T'), s->buf, len);
  790. }
  791. s->zstream.avail_out = IOBUF_SIZE;
  792. s->zstream.next_out = s->buf;
  793. if (ret == Z_STREAM_END)
  794. break;
  795. } else {
  796. goto fail;
  797. }
  798. }
  799. png_write_chunk(f, MKTAG('I', 'E', 'N', 'D'), NULL, 0);
  800. put_flush_packet(f);
  801. ret = 0;
  802. the_end:
  803. av_free(crow_buf);
  804. av_free(tmp_buf);
  805. deflateEnd(&s->zstream);
  806. return ret;
  807. fail:
  808. ret = -1;
  809. goto the_end;
  810. }
  811. AVImageFormat png_image_format = {
  812. "png",
  813. "png",
  814. png_probe,
  815. png_read,
  816. (1 << PIX_FMT_RGBA32) | (1 << PIX_FMT_RGB24) | (1 << PIX_FMT_GRAY8) |
  817. (1 << PIX_FMT_MONOBLACK) | (1 << PIX_FMT_PAL8),
  818. png_write,
  819. AVIMAGE_INTERLEAVED,
  820. };
  821. #endif