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