~ubuntu-branches/ubuntu/warty/xplanet/warty

« back to all changes in this revision

Viewing changes to src/libimage/png.c

  • Committer: Bazaar Package Importer
  • Author(s): LaMont Jones
  • Date: 2004-08-24 07:14:00 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20040824071400-2dr4qnjbjmm8z3ia
Tags: 1.0.6-1ubuntu1
Build-depend: libtiff4-dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
    png.c - read and write png images using libpng routines.
 
3
    Distributed with Xplanet.
 
4
    Copyright (C) 2002 Hari Nair <hari@alumni.caltech.edu>
 
5
 
 
6
    This program is free software; you can redistribute it and/or modify
 
7
    it under the terms of the GNU General Public License as published by
 
8
    the Free Software Foundation; either version 2 of the License, or
 
9
    (at your option) any later version.
 
10
 
 
11
    This program is distributed in the hope that it will be useful,
 
12
    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
    GNU General Public License for more details.
 
15
 
 
16
    You should have received a copy of the GNU General Public License
 
17
    along with this program; if not, write to the Free Software
 
18
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
19
****************************************************************************/
 
20
 
 
21
#include <stdio.h>
 
22
#include <stdlib.h>
 
23
 
 
24
#include <png.h>
 
25
 
 
26
int
 
27
read_png(const char *filename, int *width, int *height, unsigned char **rgb, 
 
28
         unsigned char **alpha)
 
29
{
 
30
    FILE *infile = fopen(filename, "rb");
 
31
 
 
32
    png_structp png_ptr;
 
33
    png_infop info_ptr;
 
34
    png_bytepp row_pointers;
 
35
 
 
36
    unsigned char *ptr = NULL;
 
37
    png_uint_32 w, h;
 
38
    int bit_depth, color_type, interlace_type;
 
39
    int i;
 
40
 
 
41
    png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, 
 
42
                                     (png_voidp) NULL, 
 
43
                                     (png_error_ptr) NULL, 
 
44
                                     (png_error_ptr) NULL);
 
45
    if (!png_ptr) 
 
46
    {
 
47
        fclose(infile);
 
48
        return(0);
 
49
    }
 
50
  
 
51
    info_ptr = png_create_info_struct(png_ptr);
 
52
    if (!info_ptr)
 
53
    {
 
54
        png_destroy_read_struct(&png_ptr, (png_infopp) NULL, 
 
55
                                (png_infopp) NULL);
 
56
        fclose(infile);
 
57
        return(0);
 
58
    }
 
59
  
 
60
    if (setjmp(png_ptr->jmpbuf))
 
61
    {
 
62
        png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL);
 
63
        fclose(infile);
 
64
        return(0);
 
65
    }
 
66
  
 
67
    png_init_io(png_ptr, infile);
 
68
    png_read_info(png_ptr, info_ptr);
 
69
 
 
70
    png_get_IHDR(png_ptr, info_ptr, &w, &h, &bit_depth, &color_type,
 
71
                 &interlace_type, (int *) NULL, (int *) NULL);
 
72
 
 
73
    *width = (int) w;
 
74
    *height = (int) h;
 
75
    
 
76
    if (color_type == PNG_COLOR_TYPE_RGB_ALPHA
 
77
        || color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
 
78
    {
 
79
        alpha[0] = malloc(*width * *height);
 
80
        if (alpha[0] == NULL)
 
81
        {
 
82
            fprintf(stderr, "Can't allocate memory for alpha channel in PNG file.\n");
 
83
            return(0); 
 
84
        }
 
85
    }
 
86
 
 
87
    /* Change a paletted/grayscale image to RGB */
 
88
    if (color_type == PNG_COLOR_TYPE_PALETTE && bit_depth <= 8) 
 
89
        png_set_expand(png_ptr);
 
90
 
 
91
    /* Change a grayscale image to RGB */
 
92
    if (color_type == PNG_COLOR_TYPE_GRAY 
 
93
        || color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
 
94
        png_set_gray_to_rgb(png_ptr);
 
95
 
 
96
    /* If the PNG file has 16 bits per channel, strip them down to 8 */
 
97
    if (bit_depth == 16) png_set_strip_16(png_ptr);
 
98
 
 
99
    /* use 1 byte per pixel */
 
100
    png_set_packing(png_ptr);
 
101
 
 
102
    row_pointers = malloc(*height * sizeof(png_bytep));
 
103
    if (row_pointers == NULL)
 
104
    {
 
105
        fprintf(stderr, "Can't allocate memory for PNG file.\n");
 
106
        return(0);
 
107
    }
 
108
 
 
109
    for (i = 0; i < *height; i++)
 
110
    {
 
111
        row_pointers[i] = malloc(4 * *width);
 
112
        if (row_pointers == NULL)
 
113
        {
 
114
            fprintf(stderr, "Can't allocate memory for PNG line.\n");
 
115
            return(0);
 
116
        }
 
117
    }
 
118
 
 
119
    png_read_image(png_ptr, row_pointers);
 
120
 
 
121
    rgb[0] = malloc(3 * *width * *height);
 
122
    if (rgb[0] == NULL)
 
123
    {
 
124
        fprintf(stderr, "Can't allocate memory for PNG file.\n");
 
125
        return(0);
 
126
    }
 
127
 
 
128
    if (alpha[0] == NULL)
 
129
    {
 
130
        ptr = rgb[0];
 
131
        for (i = 0; i < *height; i++)
 
132
        {
 
133
            memcpy(ptr, row_pointers[i], 3 * *width);
 
134
            ptr += 3 * *width;
 
135
        }
 
136
    }
 
137
    else
 
138
    {
 
139
        int j;
 
140
        ptr = rgb[0];
 
141
        for (i = 0; i < *height; i++)
 
142
        {
 
143
            int ipos = 0;
 
144
            for (j = 0; j < *width; j++)
 
145
            {
 
146
                *ptr++ = row_pointers[i][ipos++];
 
147
                *ptr++ = row_pointers[i][ipos++];
 
148
                *ptr++ = row_pointers[i][ipos++];
 
149
                alpha[0][i * *width + j] = row_pointers[i][ipos++];
 
150
            }
 
151
        }
 
152
    }
 
153
 
 
154
    png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL);
 
155
 
 
156
    for (i = 0; i < *height; i++) free(row_pointers[i]);
 
157
    free(row_pointers);
 
158
 
 
159
    fclose(infile);
 
160
    return(1);
 
161
}
 
162
 
 
163
int
 
164
write_png(FILE *outfile, int width, int height, unsigned char *rgb,
 
165
          unsigned char *alpha)
 
166
{
 
167
    png_structp png_ptr;
 
168
    png_infop info_ptr;
 
169
    png_bytep row_ptr;
 
170
 
 
171
    int i;
 
172
 
 
173
    png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, 
 
174
                                      (png_voidp) NULL, 
 
175
                                      (png_error_ptr) NULL, 
 
176
                                      (png_error_ptr) NULL);
 
177
 
 
178
    if (!png_ptr) return(0);
 
179
  
 
180
    info_ptr = png_create_info_struct(png_ptr);
 
181
    if (!info_ptr)
 
182
    {
 
183
        png_destroy_write_struct(&png_ptr, (png_infopp) NULL);
 
184
        return(0);
 
185
    }
 
186
  
 
187
    png_init_io(png_ptr, outfile);
 
188
 
 
189
    if (alpha == NULL)
 
190
    {
 
191
        png_set_IHDR(png_ptr, info_ptr, width, height, 8, PNG_COLOR_TYPE_RGB,
 
192
                     PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT,
 
193
                     PNG_FILTER_TYPE_DEFAULT);
 
194
        
 
195
        png_write_info(png_ptr, info_ptr);
 
196
        
 
197
        for (i = 0; i < height; i++) 
 
198
        {
 
199
            row_ptr = rgb + 3 * i * width;
 
200
            png_write_rows(png_ptr, &row_ptr, 1);
 
201
        }
 
202
    }
 
203
    else
 
204
    {
 
205
        int irgb = 0;
 
206
        int irgba = 0;
 
207
 
 
208
        int area = width * height;
 
209
        unsigned char *rgba = malloc(4 * area);
 
210
 
 
211
        png_set_IHDR(png_ptr, info_ptr, width, height, 8, 
 
212
                     PNG_COLOR_TYPE_RGB_ALPHA,
 
213
                     PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT,
 
214
                     PNG_FILTER_TYPE_DEFAULT);
 
215
        
 
216
        png_write_info(png_ptr, info_ptr);
 
217
        
 
218
        for (i = 0; i < area; i++)
 
219
        {
 
220
            rgba[irgba++] = rgb[irgb++];
 
221
            rgba[irgba++] = rgb[irgb++];
 
222
            rgba[irgba++] = rgb[irgb++];
 
223
            rgba[irgba++] = alpha[i];
 
224
        }
 
225
        
 
226
        for (i = 0; i < height; i++) 
 
227
        {
 
228
            row_ptr = rgba + 4 * i * width;
 
229
            png_write_rows(png_ptr, &row_ptr, 1);
 
230
        }
 
231
 
 
232
        free(rgba);
 
233
    }
 
234
 
 
235
    png_write_end(png_ptr, info_ptr);
 
236
    png_destroy_write_struct(&png_ptr, &info_ptr);
 
237
 
 
238
    return(1);
 
239
}