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
26 #define PNG_COLOR_MASK_PALETTE 1
27 #define PNG_COLOR_MASK_COLOR 2
28 #define PNG_COLOR_MASK_ALPHA 4
30 #define PNG_COLOR_TYPE_GRAY 0
31 #define PNG_COLOR_TYPE_PALETTE (PNG_COLOR_MASK_COLOR | PNG_COLOR_MASK_PALETTE)
32 #define PNG_COLOR_TYPE_RGB (PNG_COLOR_MASK_COLOR)
33 #define PNG_COLOR_TYPE_RGB_ALPHA (PNG_COLOR_MASK_COLOR | PNG_COLOR_MASK_ALPHA)
34 #define PNG_COLOR_TYPE_GRAY_ALPHA (PNG_COLOR_MASK_ALPHA)
36 #define PNG_FILTER_VALUE_NONE 0
37 #define PNG_FILTER_VALUE_SUB 1
38 #define PNG_FILTER_VALUE_UP 2
39 #define PNG_FILTER_VALUE_AVG 3
40 #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
47 #define IOBUF_SIZE 4096
49 typedef struct PNGDecodeState {
63 uint32_t palette[256];
66 int crow_size; /* compressed row size (include filter type) */
67 int row_size; /* decompressed row size */
72 static const uint8_t pngsig[8] = {137, 80, 78, 71, 13, 10, 26, 10};
74 static int png_probe(AVProbeData *pd)
76 if (pd->buf_size >= 8 &&
77 memcmp(pd->buf, pngsig, 8) == 0)
78 return AVPROBE_SCORE_MAX;
83 static void *png_zalloc(void *opaque, unsigned int items, unsigned int size)
85 return av_malloc(items * size);
88 static void png_zfree(void *opaque, void *ptr)
94 static void png_filter_row(uint8_t *dst, int filter_type,
95 uint8_t *src, uint8_t *last, int size, int bpp)
100 case PNG_FILTER_VALUE_NONE:
101 memcpy(dst, src, size);
103 case PNG_FILTER_VALUE_SUB:
104 for(i = 0; i < bpp; i++) {
107 for(i = bpp; i < size; i++) {
112 case PNG_FILTER_VALUE_UP:
113 for(i = 0; i < size; i++) {
118 case PNG_FILTER_VALUE_AVG:
119 for(i = 0; i < bpp; i++) {
123 for(i = bpp; i < size; i++) {
124 p = ((dst[i - bpp] + last[i]) >> 1);
128 case PNG_FILTER_VALUE_PAETH:
129 for(i = 0; i < bpp; i++) {
133 for(i = bpp; i < size; i++) {
134 int a, b, c, pa, pb, pc;
147 if (pa <= pb && pa <= pc)
159 static void png_handle_row(PNGDecodeState *s)
161 uint8_t *ptr, *last_row;
162 ptr = s->image_buf + s->image_linesize * s->y;
164 last_row = s->empty_row;
166 last_row = ptr - s->image_linesize;
168 png_filter_row(ptr, s->crow_buf[0], s->crow_buf + 1,
169 last_row, s->row_size, s->bpp);
172 static int png_decode_idat(PNGDecodeState *s, ByteIOContext *f, int length)
174 uint8_t buf[IOBUF_SIZE];
178 /* read the buffer */
179 buf_size = IOBUF_SIZE;
180 if (buf_size > length)
182 ret = get_buffer(f, buf, buf_size);
185 s->zstream.avail_in = buf_size;
186 s->zstream.next_in = buf;
187 /* decode one line if possible */
188 while (s->zstream.avail_in > 0) {
189 ret = inflate(&s->zstream, Z_PARTIAL_FLUSH);
190 if (ret != Z_OK && ret != Z_STREAM_END) {
193 if (s->zstream.avail_out == 0) {
194 if (s->y < s->height) {
197 if (s->y == s->height)
198 s->state |= PNG_ALLIMAGE;
200 s->zstream.avail_out = s->crow_size;
201 s->zstream.next_out = s->crow_buf;
209 static int png_read(ByteIOContext *f,
210 int (*alloc_cb)(void *opaque, AVImageInfo *info), void *opaque)
212 AVImageInfo info1, *info = &info1;
213 PNGDecodeState s1, *s = &s1;
214 uint32_t tag, length;
218 /* check signature */
219 ret = get_buffer(f, buf, 8);
222 if (memcmp(buf, pngsig, 8) != 0)
224 memset(s, 0, sizeof(PNGDecodeState));
226 s->zstream.zalloc = png_zalloc;
227 s->zstream.zfree = png_zfree;
228 s->zstream.opaque = NULL;
229 ret = inflateInit(&s->zstream);
235 length = get_be32(f);
236 if (length > 0x7fffffff)
240 printf("png: tag=%c%c%c%c length=%u\n",
243 ((tag >> 16) & 0xff),
244 ((tag >> 24) & 0xff), length);
247 case MKTAG('I', 'H', 'D', 'R'):
250 s->width = get_be32(f);
251 s->height = get_be32(f);
252 s->bit_depth = get_byte(f);
253 s->color_type = get_byte(f);
254 s->compression_type = get_byte(f);
255 s->filter_type = get_byte(f);
256 s->interlace_type = get_byte(f);
258 s->state |= PNG_IHDR;
260 printf("width=%d height=%d depth=%d color_type=%d compression_type=%d filter_type=%d interlace_type=%d\n",
261 s->width, s->height, s->bit_depth, s->color_type,
262 s->compression_type, s->filter_type, s->interlace_type);
265 case MKTAG('I', 'D', 'A', 'T'):
266 if (!(s->state & PNG_IHDR))
268 if (!(s->state & PNG_IDAT)) {
269 /* init image info */
270 info->width = s->width;
271 info->height = s->height;
274 if ((s->color_type & (PNG_COLOR_MASK_COLOR | PNG_COLOR_MASK_PALETTE)) ==
275 PNG_COLOR_MASK_COLOR)
277 if (s->color_type & PNG_COLOR_MASK_ALPHA)
279 s->bits_per_pixel = s->bit_depth * s->channels;
280 s->bpp = (s->bits_per_pixel + 7) >> 3;
281 if (s->bit_depth == 8 &&
282 s->color_type == PNG_COLOR_TYPE_RGB) {
283 info->pix_fmt = PIX_FMT_RGB24;
284 s->row_size = s->width * 3;
285 } else if (s->bit_depth == 8 &&
286 s->color_type == PNG_COLOR_TYPE_GRAY) {
287 info->pix_fmt = PIX_FMT_GRAY8;
288 s->row_size = s->width;
289 } else if (s->bit_depth == 1 &&
290 s->color_type == PNG_COLOR_TYPE_GRAY) {
291 info->pix_fmt = PIX_FMT_MONOBLACK;
292 s->row_size = (s->width + 7) >> 3;
293 } else if (s->color_type == PNG_COLOR_TYPE_PALETTE) {
294 info->pix_fmt = PIX_FMT_PAL8;
295 s->row_size = s->width;;
299 /* compute the compressed row size */
300 if (!s->interlace_type) {
301 s->crow_size = s->row_size + 1;
303 /* XXX: handle interlacing */
306 ret = alloc_cb(opaque, info);
310 printf("row_size=%d crow_size =%d\n",
311 s->row_size, s->crow_size);
313 s->image_buf = info->pict.data[0];
314 s->image_linesize = info->pict.linesize[0];
315 /* copy the palette if needed */
316 if (s->color_type == PNG_COLOR_TYPE_PALETTE)
317 memcpy(info->pict.data[1], s->palette, 256 * sizeof(uint32_t));
318 /* empty row is used if differencing to the first row */
319 s->empty_row = av_mallocz(s->row_size);
323 s->crow_buf = av_malloc(s->crow_size);
326 s->zstream.avail_out = s->crow_size;
327 s->zstream.next_out = s->crow_buf;
329 s->state |= PNG_IDAT;
330 if (png_decode_idat(s, f, length) < 0)
335 case MKTAG('P', 'L', 'T', 'E'):
339 if ((length % 3) != 0 || length > 256 * 3)
341 /* read the palette */
347 s->palette[i] = (0xff << 24) | (r << 16) | (g << 8) | b;
350 s->palette[i] = (0xff << 24);
352 s->state |= PNG_PLTE;
356 case MKTAG('t', 'R', 'N', 'S'):
360 /* read the transparency. XXX: Only palette mode supported */
361 if (s->color_type != PNG_COLOR_TYPE_PALETTE ||
363 !(s->state & PNG_PLTE))
365 for(i=0;i<length;i++) {
367 s->palette[i] = (s->palette[i] & 0x00ffffff) | (v << 24);
372 case MKTAG('I', 'E', 'N', 'D'):
373 if (!(s->state & PNG_ALLIMAGE))
380 url_fskip(f, length + 4);
387 inflateEnd(&s->zstream);
388 av_free(s->crow_buf);
395 static void png_write_chunk(ByteIOContext *f, uint32_t tag,
396 const uint8_t *buf, int length)
402 crc = crc32(0, Z_NULL, 0);
404 tagbuf[1] = tag >> 8;
405 tagbuf[2] = tag >> 16;
406 tagbuf[3] = tag >> 24;
407 crc = crc32(crc, tagbuf, 4);
410 crc = crc32(crc, buf, length);
411 put_buffer(f, buf, length);
416 /* XXX: use avcodec generic function ? */
417 static void to_be32(uint8_t *p, uint32_t v)
425 static int png_write(ByteIOContext *f, AVImageInfo *info)
427 int bit_depth, color_type, y, len, row_size, ret;
429 uint8_t buf[IOBUF_SIZE];
430 uint8_t *crow_buf = NULL;
433 switch(info->pix_fmt) {
436 color_type = PNG_COLOR_TYPE_RGB;
437 row_size = info->width * 3;
441 color_type = PNG_COLOR_TYPE_GRAY;
442 row_size = info->width;
444 case PIX_FMT_MONOBLACK:
446 color_type = PNG_COLOR_TYPE_GRAY;
447 row_size = (info->width + 7) >> 3;
451 color_type = PNG_COLOR_TYPE_PALETTE;
452 row_size = info->width;
457 zstream.zalloc = png_zalloc;
458 zstream.zfree = png_zfree;
459 zstream.opaque = NULL;
460 ret = deflateInit2(&zstream, Z_DEFAULT_COMPRESSION,
461 Z_DEFLATED, 15, 8, Z_DEFAULT_STRATEGY);
464 crow_buf = av_malloc(row_size + 1);
468 /* write png header */
469 put_buffer(f, pngsig, 8);
471 to_be32(buf, info->width);
472 to_be32(buf + 4, info->height);
475 buf[10] = 0; /* compression type */
476 buf[11] = 0; /* filter type */
477 buf[12] = 0; /* interlace type */
479 png_write_chunk(f, MKTAG('I', 'H', 'D', 'R'), buf, 13);
481 /* put the palette if needed */
482 if (color_type == PNG_COLOR_TYPE_PALETTE) {
483 int has_alpha, alpha, i;
488 palette = (uint32_t *)info->pict.data[1];
490 alpha_ptr = buf + 256 * 3;
492 for(i = 0; i < 256; i++) {
497 *alpha_ptr++ = alpha;
503 png_write_chunk(f, MKTAG('P', 'L', 'T', 'E'), buf, 256 * 3);
505 png_write_chunk(f, MKTAG('t', 'R', 'N', 'S'), buf + 256 * 3, 256);
509 /* now put each row */
510 zstream.avail_out = IOBUF_SIZE;
511 zstream.next_out = buf;
512 for(y = 0;y < info->height; y++) {
513 /* XXX: do filtering */
514 ptr = info->pict.data[0] + y * info->pict.linesize[0];
515 memcpy(crow_buf + 1, ptr, row_size);
516 crow_buf[0] = PNG_FILTER_VALUE_NONE;
517 zstream.avail_in = row_size + 1;
518 zstream.next_in = crow_buf;
519 while (zstream.avail_in > 0) {
520 ret = deflate(&zstream, Z_NO_FLUSH);
523 if (zstream.avail_out == 0) {
524 png_write_chunk(f, MKTAG('I', 'D', 'A', 'T'), buf, IOBUF_SIZE);
525 zstream.avail_out = IOBUF_SIZE;
526 zstream.next_out = buf;
530 /* compress last bytes */
532 ret = deflate(&zstream, Z_FINISH);
533 if (ret == Z_OK || ret == Z_STREAM_END) {
534 len = IOBUF_SIZE - zstream.avail_out;
536 png_write_chunk(f, MKTAG('I', 'D', 'A', 'T'), buf, len);
538 zstream.avail_out = IOBUF_SIZE;
539 zstream.next_out = buf;
540 if (ret == Z_STREAM_END)
546 png_write_chunk(f, MKTAG('I', 'E', 'N', 'D'), NULL, 0);
552 deflateEnd(&zstream);
559 AVImageFormat png_image_format = {
564 (1 << PIX_FMT_RGB24) | (1 << PIX_FMT_GRAY8) | (1 << PIX_FMT_MONOBLACK) | (1 << PIX_FMT_PAL8),