3 * Copyright (c) 2003 Fabrice Bellard.
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.
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.
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
25 #define PNG_COLOR_MASK_PALETTE 1
26 #define PNG_COLOR_MASK_COLOR 2
27 #define PNG_COLOR_MASK_ALPHA 4
29 #define PNG_COLOR_TYPE_GRAY 0
30 #define PNG_COLOR_TYPE_PALETTE (PNG_COLOR_MASK_COLOR | PNG_COLOR_MASK_PALETTE)
31 #define PNG_COLOR_TYPE_RGB (PNG_COLOR_MASK_COLOR)
32 #define PNG_COLOR_TYPE_RGB_ALPHA (PNG_COLOR_MASK_COLOR | PNG_COLOR_MASK_ALPHA)
33 #define PNG_COLOR_TYPE_GRAY_ALPHA (PNG_COLOR_MASK_ALPHA)
35 #define PNG_FILTER_VALUE_NONE 0
36 #define PNG_FILTER_VALUE_SUB 1
37 #define PNG_FILTER_VALUE_UP 2
38 #define PNG_FILTER_VALUE_AVG 3
39 #define PNG_FILTER_VALUE_PAETH 4
41 #define PNG_IHDR 0x0001
42 #define PNG_IDAT 0x0002
43 #define PNG_ALLIMAGE 0x0004
44 #define PNG_PLTE 0x0008
46 #define IOBUF_SIZE 4096
48 typedef struct PNGDecodeState {
62 uint32_t palette[256];
65 int crow_size; /* compressed row size (include filter type) */
66 int row_size; /* decompressed row size */
71 static const uint8_t pngsig[8] = {137, 80, 78, 71, 13, 10, 26, 10};
73 static int png_probe(AVProbeData *pd)
75 if (pd->buf_size >= 8 &&
76 memcmp(pd->buf, pngsig, 8) == 0)
77 return AVPROBE_SCORE_MAX;
82 static void *png_zalloc(void *opaque, unsigned int items, unsigned int size)
84 return av_malloc(items * size);
87 static void png_zfree(void *opaque, void *ptr)
93 static void png_filter_row(uint8_t *dst, int filter_type,
94 uint8_t *src, uint8_t *last, int size, int bpp)
99 case PNG_FILTER_VALUE_NONE:
100 memcpy(dst, src, size);
102 case PNG_FILTER_VALUE_SUB:
103 for(i = 0; i < bpp; i++) {
106 for(i = bpp; i < size; i++) {
111 case PNG_FILTER_VALUE_UP:
112 for(i = 0; i < size; i++) {
117 case PNG_FILTER_VALUE_AVG:
118 for(i = 0; i < bpp; i++) {
122 for(i = bpp; i < size; i++) {
123 p = ((dst[i - bpp] + last[i]) >> 1);
127 case PNG_FILTER_VALUE_PAETH:
128 for(i = 0; i < bpp; i++) {
132 for(i = bpp; i < size; i++) {
133 int a, b, c, pa, pb, pc;
146 if (pa <= pb && pa <= pc)
158 static void png_handle_row(PNGDecodeState *s)
160 uint8_t *ptr, *last_row;
161 ptr = s->image_buf + s->image_linesize * s->y;
163 last_row = s->empty_row;
165 last_row = ptr - s->image_linesize;
167 png_filter_row(ptr, s->crow_buf[0], s->crow_buf + 1,
168 last_row, s->row_size, s->bpp);
171 static int png_decode_idat(PNGDecodeState *s, ByteIOContext *f, int length)
173 uint8_t buf[IOBUF_SIZE];
177 /* read the buffer */
178 buf_size = IOBUF_SIZE;
179 if (buf_size > length)
181 ret = get_buffer(f, buf, buf_size);
184 s->zstream.avail_in = buf_size;
185 s->zstream.next_in = buf;
186 /* decode one line if possible */
187 while (s->zstream.avail_in > 0) {
188 ret = inflate(&s->zstream, Z_PARTIAL_FLUSH);
189 if (ret != Z_OK && ret != Z_STREAM_END) {
192 if (s->zstream.avail_out == 0) {
193 if (s->y < s->height) {
196 if (s->y == s->height)
197 s->state |= PNG_ALLIMAGE;
199 s->zstream.avail_out = s->crow_size;
200 s->zstream.next_out = s->crow_buf;
208 static int png_read(ByteIOContext *f,
209 int (*alloc_cb)(void *opaque, AVImageInfo *info), void *opaque)
211 AVImageInfo info1, *info = &info1;
212 PNGDecodeState s1, *s = &s1;
213 uint32_t tag, length;
217 /* check signature */
218 ret = get_buffer(f, buf, 8);
221 if (memcmp(buf, pngsig, 8) != 0)
223 memset(s, 0, sizeof(PNGDecodeState));
225 s->zstream.zalloc = png_zalloc;
226 s->zstream.zfree = png_zfree;
227 s->zstream.opaque = NULL;
228 ret = inflateInit(&s->zstream);
234 length = get_be32(f);
235 if (length > 0x7fffffff)
239 printf("png: tag=%c%c%c%c length=%u\n",
242 ((tag >> 16) & 0xff),
243 ((tag >> 24) & 0xff), length);
246 case MKTAG('I', 'H', 'D', 'R'):
249 s->width = get_be32(f);
250 s->height = get_be32(f);
251 s->bit_depth = get_byte(f);
252 s->color_type = get_byte(f);
253 s->compression_type = get_byte(f);
254 s->filter_type = get_byte(f);
255 s->interlace_type = get_byte(f);
257 s->state |= PNG_IHDR;
259 printf("width=%d height=%d depth=%d color_type=%d compression_type=%d filter_type=%d interlace_type=%d\n",
260 s->width, s->height, s->bit_depth, s->color_type,
261 s->compression_type, s->filter_type, s->interlace_type);
264 case MKTAG('I', 'D', 'A', 'T'):
265 if (!(s->state & PNG_IHDR))
267 if (!(s->state & PNG_IDAT)) {
268 /* init image info */
269 info->width = s->width;
270 info->height = s->height;
273 if ((s->color_type & (PNG_COLOR_MASK_COLOR | PNG_COLOR_MASK_PALETTE)) ==
274 PNG_COLOR_MASK_COLOR)
276 if (s->color_type & PNG_COLOR_MASK_ALPHA)
278 s->bits_per_pixel = s->bit_depth * s->channels;
279 s->bpp = (s->bits_per_pixel + 7) >> 3;
280 if (s->bit_depth == 8 &&
281 s->color_type == PNG_COLOR_TYPE_RGB) {
282 info->pix_fmt = PIX_FMT_RGB24;
283 s->row_size = s->width * 3;
284 } else if (s->bit_depth == 8 &&
285 s->color_type == PNG_COLOR_TYPE_GRAY) {
286 info->pix_fmt = PIX_FMT_GRAY8;
287 s->row_size = s->width;
288 } else if (s->bit_depth == 1 &&
289 s->color_type == PNG_COLOR_TYPE_GRAY) {
290 info->pix_fmt = PIX_FMT_MONOBLACK;
291 s->row_size = (s->width + 7) >> 3;
292 } else if (s->color_type == PNG_COLOR_TYPE_PALETTE) {
293 info->pix_fmt = PIX_FMT_PAL8;
294 s->row_size = s->width;;
298 /* compute the compressed row size */
299 if (!s->interlace_type) {
300 s->crow_size = s->row_size + 1;
302 /* XXX: handle interlacing */
305 ret = alloc_cb(opaque, info);
309 printf("row_size=%d crow_size =%d\n",
310 s->row_size, s->crow_size);
312 s->image_buf = info->pict.data[0];
313 s->image_linesize = info->pict.linesize[0];
314 /* copy the palette if needed */
315 if (s->color_type == PNG_COLOR_TYPE_PALETTE)
316 memcpy(info->pict.data[1], s->palette, 256 * sizeof(uint32_t));
317 /* empty row is used if differencing to the first row */
318 s->empty_row = av_mallocz(s->row_size);
322 s->crow_buf = av_malloc(s->crow_size);
325 s->zstream.avail_out = s->crow_size;
326 s->zstream.next_out = s->crow_buf;
328 s->state |= PNG_IDAT;
329 if (png_decode_idat(s, f, length) < 0)
334 case MKTAG('P', 'L', 'T', 'E'):
338 if ((length % 3) != 0 || length > 256 * 3)
340 /* read the palette */
346 s->palette[i] = (0xff << 24) | (r << 16) | (g << 8) | b;
349 s->palette[i] = (0xff << 24);
351 s->state |= PNG_PLTE;
355 case MKTAG('t', 'R', 'N', 'S'):
359 /* read the transparency. XXX: Only palette mode supported */
360 if (s->color_type != PNG_COLOR_TYPE_PALETTE ||
362 !(s->state & PNG_PLTE))
364 for(i=0;i<length;i++) {
366 s->palette[i] = (s->palette[i] & 0x00ffffff) | (v << 24);
371 case MKTAG('I', 'E', 'N', 'D'):
372 if (!(s->state & PNG_ALLIMAGE))
379 url_fskip(f, length + 4);
386 inflateEnd(&s->zstream);
387 av_free(s->crow_buf);
394 static void png_write_chunk(ByteIOContext *f, uint32_t tag,
395 const uint8_t *buf, int length)
401 crc = crc32(0, Z_NULL, 0);
403 tagbuf[1] = tag >> 8;
404 tagbuf[2] = tag >> 16;
405 tagbuf[3] = tag >> 24;
406 crc = crc32(crc, tagbuf, 4);
409 crc = crc32(crc, buf, length);
410 put_buffer(f, buf, length);
415 /* XXX: use avcodec generic function ? */
416 static void to_be32(uint8_t *p, uint32_t v)
424 static int png_write(ByteIOContext *f, AVImageInfo *info)
426 int bit_depth, color_type, y, len, row_size, ret;
428 uint8_t buf[IOBUF_SIZE];
429 uint8_t *crow_buf = NULL;
432 switch(info->pix_fmt) {
435 color_type = PNG_COLOR_TYPE_RGB;
436 row_size = info->width * 3;
440 color_type = PNG_COLOR_TYPE_GRAY;
441 row_size = info->width;
443 case PIX_FMT_MONOBLACK:
445 color_type = PNG_COLOR_TYPE_GRAY;
446 row_size = (info->width + 7) >> 3;
450 color_type = PNG_COLOR_TYPE_PALETTE;
451 row_size = info->width;
456 zstream.zalloc = png_zalloc;
457 zstream.zfree = png_zfree;
458 zstream.opaque = NULL;
459 ret = deflateInit2(&zstream, Z_DEFAULT_COMPRESSION,
460 Z_DEFLATED, 15, 8, Z_DEFAULT_STRATEGY);
463 crow_buf = av_malloc(row_size + 1);
467 /* write png header */
468 put_buffer(f, pngsig, 8);
470 to_be32(buf, info->width);
471 to_be32(buf + 4, info->height);
474 buf[10] = 0; /* compression type */
475 buf[11] = 0; /* filter type */
476 buf[12] = 0; /* interlace type */
478 png_write_chunk(f, MKTAG('I', 'H', 'D', 'R'), buf, 13);
480 /* put the palette if needed */
481 if (color_type == PNG_COLOR_TYPE_PALETTE) {
482 int has_alpha, alpha, i;
487 palette = (uint32_t *)info->pict.data[1];
489 alpha_ptr = buf + 256 * 3;
491 for(i = 0; i < 256; i++) {
496 *alpha_ptr++ = alpha;
502 png_write_chunk(f, MKTAG('P', 'L', 'T', 'E'), buf, 256 * 3);
504 png_write_chunk(f, MKTAG('t', 'R', 'N', 'S'), buf + 256 * 3, 256);
508 /* now put each row */
509 zstream.avail_out = IOBUF_SIZE;
510 zstream.next_out = buf;
511 for(y = 0;y < info->height; y++) {
512 /* XXX: do filtering */
513 ptr = info->pict.data[0] + y * info->pict.linesize[0];
514 memcpy(crow_buf + 1, ptr, row_size);
515 crow_buf[0] = PNG_FILTER_VALUE_NONE;
516 zstream.avail_in = row_size + 1;
517 zstream.next_in = crow_buf;
518 while (zstream.avail_in > 0) {
519 ret = deflate(&zstream, Z_NO_FLUSH);
522 if (zstream.avail_out == 0) {
523 png_write_chunk(f, MKTAG('I', 'D', 'A', 'T'), buf, IOBUF_SIZE);
524 zstream.avail_out = IOBUF_SIZE;
525 zstream.next_out = buf;
529 /* compress last bytes */
531 ret = deflate(&zstream, Z_FINISH);
532 if (ret == Z_OK || ret == Z_STREAM_END) {
533 len = IOBUF_SIZE - zstream.avail_out;
535 png_write_chunk(f, MKTAG('I', 'D', 'A', 'T'), buf, len);
537 zstream.avail_out = IOBUF_SIZE;
538 zstream.next_out = buf;
539 if (ret == Z_STREAM_END)
545 png_write_chunk(f, MKTAG('I', 'E', 'N', 'D'), NULL, 0);
551 deflateEnd(&zstream);
558 AVImageFormat png_image_format = {
563 (1 << PIX_FMT_RGB24) | (1 << PIX_FMT_GRAY8) | (1 << PIX_FMT_MONOBLACK) | (1 << PIX_FMT_PAL8),