aboutsummaryrefslogtreecommitdiffstats
path: root/libavformat/png.c
blob: 79efdec755a2ba17522cedf08062e01886b4a88f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*
 * PNG image format
 * Copyright (c) 2003 Fabrice Bellard.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
#include "avformat.h"

#include "libpng/png.h"

extern const uint8_t png_sig[];

static int png_probe(AVProbeData *pd)
{
    if (pd->buf_size >= 8 &&
        memcmp(pd->buf, png_sig, 8) == 0)
        return AVPROBE_SCORE_MAX;
    else
        return 0;
}

png_voidp PNGAPI
png_memset_check (png_structp png_ptr, png_voidp s1, int value,
   png_uint_32 length)
{
   png_size_t size;

   size = (png_size_t)length;
   if ((png_uint_32)size != length)
      png_error(png_ptr,"Overflow in png_memset_check.");

   return (png_memset (s1, value, size));

}

png_voidp PNGAPI
png_memcpy_check (png_structp png_ptr, png_voidp s1, png_voidp s2,
   png_uint_32 length)
{
   png_size_t size;

   size = (png_size_t)length;
   if ((png_uint_32)size != length)
      png_error(png_ptr,"Overflow in png_memcpy_check.");

   return(png_memcpy (s1, s2, size));
}

void png_error (png_struct *png_ptr, const char *message)
{
    longjmp(png_ptr->jmpbuf, 0);
}

void png_warning (png_struct *png_ptr, const char *message)
{
}

void PNGAPI
png_chunk_error(png_structp png_ptr, png_const_charp error_message)
{
   char msg[18+64];
   //   png_format_buffer(png_ptr, msg, error_message);
   png_error(png_ptr, msg);
}

void PNGAPI
png_chunk_warning(png_structp png_ptr, png_const_charp warning_message)
{
   char msg[18+64];
   //   png_format_buffer(png_ptr, msg, warning_message);
   png_warning(png_ptr, msg);
}

void png_read_data (png_struct *png_ptr, png_byte *data, png_uint_32 length)
{
    int ret;

    ret = get_buffer(png_ptr->io_ptr, data, length);
    if (ret != length)
	png_error (png_ptr, "Read Error");
}

void *png_malloc (png_struct *png_ptr, png_uint_32 size)
{
    return av_malloc(size);
}

void png_free (png_struct *png_ptr, void *ptr)
{
    return av_free(ptr);
}

static int png_read(ByteIOContext *f, 
                    int (*alloc_cb)(void *opaque, AVImageInfo *info), void *opaque)
{
    png_struct png_struct1, *png_ptr;
    png_info png_info1, png_info2, *info_ptr, *end_info;
    AVImageInfo info1, *info = &info1;
    int y, height, ret, row_size;
    uint8_t *ptr;

    png_ptr = &png_struct1;
    png_read_init(png_ptr);

    info_ptr = &png_info1;
    end_info = &png_info2;
    
    png_info_init(info_ptr);
    png_info_init(end_info);
    
    png_ptr->io_ptr = f;

    if (setjmp(png_jmpbuf(png_ptr))) {
        png_read_destroy (png_ptr, info_ptr, NULL);
        return -1;
    }
    
    png_read_info (png_ptr, info_ptr);
    
    /* init image info */
    info->width = info_ptr->width;
    info->height = info_ptr->height;
    
    if (info_ptr->bit_depth == 8 && 
        info_ptr->color_type == PNG_COLOR_TYPE_RGB) {
        info->pix_fmt = PIX_FMT_RGB24;
        row_size = info_ptr->width * 3;
    } else if (info_ptr->bit_depth == 8 && 
               info_ptr->color_type == PNG_COLOR_TYPE_GRAY) {
        info->pix_fmt = PIX_FMT_GRAY8;
        row_size = info_ptr->width;
    } else if (info_ptr->bit_depth == 1 && 
               info_ptr->color_type == PNG_COLOR_TYPE_GRAY) {
        info->pix_fmt = PIX_FMT_MONOBLACK;
        row_size = (info_ptr->width + 7) >> 3;
    } else {
        png_read_destroy (png_ptr, info_ptr, NULL);
        return -1;
    }
    ret = alloc_cb(opaque, info);
    if (ret) {
        png_read_destroy (png_ptr, info_ptr, NULL);
        return ret;
    }
    
    /* now read the whole image */
    png_start_read_image (png_ptr);

    height = info->height;
    ptr = info->pict.data[0];
    for (y = 0; y < height; y++) {
        png_read_row (png_ptr, NULL, NULL);
        memcpy(ptr, png_ptr->row_buf + 1, row_size);
        ptr += info->pict.linesize[0];
    }
    
    png_read_destroy (png_ptr, info_ptr, NULL);
    return 0;
}

void
png_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
{
    put_buffer(png_ptr->io_ptr, data, length);
}

static int png_write(ByteIOContext *pb, AVImageInfo *info)
{
    png_struct png_struct1, *png_ptr;
    png_info png_info1, *info_ptr;
    int w, h, y;
    uint8_t *ptr;

    png_ptr = &png_struct1;
    info_ptr = &png_info1;

    png_write_init(png_ptr);
    png_info_init(info_ptr);
    
    png_ptr->io_ptr = pb;

    if (setjmp(png_ptr->jmpbuf)) {
        png_write_destroy(png_ptr);
        return -1;
    }

    w = info->width;
    h = info->height;

    info_ptr->width = w;
    info_ptr->height = h;
    switch(info->pix_fmt) {
    case PIX_FMT_RGB24:
        info_ptr->bit_depth = 8;
        info_ptr->color_type = PNG_COLOR_TYPE_RGB;
        break;
    case PIX_FMT_GRAY8:
        info_ptr->bit_depth = 8;
        info_ptr->color_type = PNG_COLOR_TYPE_GRAY;
        break;
    case PIX_FMT_MONOBLACK:
        info_ptr->bit_depth = 1;
        info_ptr->color_type = PNG_COLOR_TYPE_GRAY;
        break;
    default:
        return -1;
    }
    png_write_info(png_ptr, info_ptr);

    ptr = info->pict.data[0];
    for(y=0;y<h;y++) {
        png_write_row(png_ptr, ptr);
        ptr += info->pict.linesize[0];
    }
    png_write_end(png_ptr, info_ptr);
    png_write_destroy(png_ptr);
    put_flush_packet(pb);
    return 0;
}

AVImageFormat png_image_format = {
    "png",
    "png",
    png_probe,
    png_read,
    (1 << PIX_FMT_RGB24) | (1 << PIX_FMT_GRAY8) | (1 << PIX_FMT_MONOBLACK),
    png_write,
};