annotate png.c @ 1065:fab79efcd476 libavformat

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