[2a97a8]: png.c Maximize Restore History

Download this file

png.c    253 lines (187 with data), 6.1 kB

  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
241
242
243
244
245
246
247
248
249
#include "filter.h"
#include "png.h"
int writePNG( Image *im, fullPath *sfile )
{
FILE * outfile;
png_structp png_ptr;
png_infop info_ptr;
char filename[512];
png_bytep *row_pointers;
int row;
if( GetFullPath (sfile, filename))
return -1;
if ((outfile = fopen(filename, "wb")) == NULL)
{
PrintError("can't open %s", filename);
return -1;
}
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,
(void *)NULL, NULL, NULL);
if (!png_ptr)
{
fclose(outfile);
return -1;
}
info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr)
{
fclose(outfile);
png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
return -1;
}
/* set error handling */
if (setjmp(png_ptr->jmpbuf))
{
/* If we get here, we had a problem reading the file */
fclose(outfile);
png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
return -1;
}
/* set up the output control if you are using standard C streams */
png_init_io(png_ptr, outfile);
FourToThreeBPP( im );
info_ptr->width = im->width;
info_ptr->height = im->height;
info_ptr->bit_depth = (im->bitsPerPixel > 32 ? 16 : 8);
info_ptr->color_type = PNG_COLOR_TYPE_RGB;
info_ptr->channels = im->bitsPerPixel / info_ptr->bit_depth;
info_ptr->pixel_depth = im->bitsPerPixel;
info_ptr->rowbytes = im->bytesPerLine;
info_ptr->interlace_type= 0;
png_write_info(png_ptr, info_ptr);
/* the easiest way to read the image */
row_pointers = (png_bytep*) malloc( im->height * sizeof(png_bytep) );
if( row_pointers == NULL ) return -1;
for (row = 0; row < im->height; row++)
{
row_pointers[row] = (png_bytep)(*im->data) + row * im->bytesPerLine;
}
png_write_image(png_ptr, row_pointers);
png_write_end(png_ptr, info_ptr);
/* clean up after the write, and free any memory allocated */
png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
free( row_pointers );
/* close the file */
fclose(outfile);
/* that's it */
return 0;
}
int readPNG ( Image *im, fullPath *sfile )
{
FILE * infile;
char filename[256];
png_structp png_ptr;
png_infop info_ptr;
png_bytep *row_pointers;
int row;
long dataSize;
if( GetFullPath (sfile, filename))
return -1;
if ((infile = fopen(filename, "rb")) == NULL)
{
PrintError("can't open %s", filename);
return -1;
}
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,
NULL, NULL, NULL);
if (!png_ptr)
{
fclose(infile);
return-1;
}
info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr)
{
fclose(infile);
png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL);
return -1;
}
/* set error handling if you are using the setjmp/longjmp method */
if (setjmp(png_ptr->jmpbuf))
{
/* Free all of the memory associated with the png_ptr and info_ptr */
png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
fclose(infile);
PrintError("Could not read png file");
/* If we get here, we had a problem reading the file */
return -1;
}
/* set up the input control if you are using standard C streams */
png_init_io(png_ptr, infile);
/* read the file information */
png_read_info(png_ptr, info_ptr);
if( info_ptr->color_type != PNG_COLOR_TYPE_RGB &&
info_ptr->color_type != PNG_COLOR_TYPE_PALETTE &&
info_ptr->color_type != PNG_COLOR_TYPE_RGB_ALPHA)
{
PrintError(" Only rgb images supported");
png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
fclose( infile );
return -1;
}
/* expand paletted colors into true RGB triplets */
if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
png_set_expand(png_ptr);
SetImageDefaults( im );
im->width = info_ptr->width;
im->height = info_ptr->height;
im->bytesPerLine = info_ptr->rowbytes;
im->bitsPerPixel = info_ptr->pixel_depth;
im->dataSize = im->height * im->bytesPerLine;
if( im->bitsPerPixel == 24 )
dataSize = im->width * im->height * 4;
else if( im->bitsPerPixel == 48 )
dataSize = im->width * im->height * 8;
else
dataSize = im->width * im->height * im->bitsPerPixel/8;
im->data = (unsigned char**)mymalloc( (dataSize > im->dataSize ? dataSize : im->dataSize) );
if( im->data == NULL ){
PrintError("Not enough memory");
png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
fclose( infile );
return -1;
}
/* the easiest way to read the image */
row_pointers = (png_bytep*) malloc( im->height * sizeof(png_bytep) );
if( row_pointers == NULL ) return -1;
for (row = 0; row < im->height; row++){
row_pointers[row] = (png_bytep)(*im->data + row * im->bytesPerLine);
}
png_read_image(png_ptr, row_pointers);
if(im->bitsPerPixel == 32){ // rgba; switch to argb
UCHAR pix, *p;
LOOP_IMAGE( im, p = (UCHAR*)idata; pix=p[0]; p[0]=p[3];p[3]=p[2];p[2]=p[1];p[1]=pix; )
}
if(im->bitsPerPixel == 64){ // rgba; switch to argb
USHORT pix, *p;
LOOP_IMAGE( im, p = (USHORT*)idata; pix=p[0]; p[0]=p[3];p[3]=p[2];p[2]=p[1];p[1]=pix; )
}
#ifndef BIGENDIAN
// Swap bytes in shorts
if(im->bitsPerPixel == 48){
UCHAR b,*id;
LOOP_IMAGE( im, id = idata; \
b = *id; *id = *(++id); *(id++)=b;\
b = *id; *id = *(++id); *(id++)=b;\
b = *id; *id = *(++id); *id=b; )
}
if(im->bitsPerPixel == 64){
UCHAR b,*id;
LOOP_IMAGE( im, id = idata; \
b = *id; *id = *(++id); *(id++)=b;\
b = *id; *id = *(++id); *(id++)=b;\
b = *id; *id = *(++id); *(id++)=b;\
b = *id; *id = *(++id); *id=b; )
}
#endif
ThreeToFourBPP( im );
/* read the rest of the file, getting any additional chunks in info_ptr */
png_read_end(png_ptr, info_ptr);
/* clean up after the read, and free any memory allocated */
png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
free(row_pointers);
/* close the file */
fclose(infile);
/* that's it */
return 0;
}