[0c73d8]: png.c Maximize Restore History

Download this file

png.c    306 lines (238 with data), 8.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#include "filter.h"
#include "png.h"
#include "file.h"
#include "metadata.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;
#ifdef __Mac__
unsigned char the_pcUnixFilePath[512];//added by Kekus Digital
Str255 the_cString;
Boolean the_bReturnValue;
CFStringRef the_FilePath;
CFURLRef the_Url;//till here
#endif
if( GetFullPath (sfile, filename))
return -1;
#ifdef __Mac__
CopyCStringToPascal(filename,the_cString);//added by Kekus Digital
the_FilePath = CFStringCreateWithPascalString(kCFAllocatorDefault, the_cString, kCFStringEncodingUTF8);
the_Url = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, the_FilePath, kCFURLHFSPathStyle, false);
the_bReturnValue = CFURLGetFileSystemRepresentation(the_Url, true, the_pcUnixFilePath, 512);
strcpy(filename, the_pcUnixFilePath);//till here
#endif
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_jmpbuf(png_ptr)))
{
/* 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 );
png_set_IHDR(png_ptr, info_ptr, im->width, im->height,
(im->bitsPerPixel > 32 ? 16 : 8), PNG_COLOR_TYPE_RGB,
0, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
#if 0
info_ptr->channels = (png_byte)(im->bitsPerPixel / info_ptr->bit_depth);
info_ptr->pixel_depth = (png_byte)(im->bitsPerPixel);
info_ptr->rowbytes = im->bytesPerLine;
#endif
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;
unsigned long dataSize;
int color_type;
#ifdef __Mac__
unsigned char the_pcUnixFilePath[256];//added by Kekus Digital
Str255 the_cString;
Boolean the_bReturnValue;
CFStringRef the_FilePath;
CFURLRef the_Url;//till here
#endif
if( GetFullPath (sfile, filename))
return -1;
#ifdef __Mac__
CopyCStringToPascal(filename,the_cString);//added by Kekus Digital
the_FilePath = CFStringCreateWithPascalString(kCFAllocatorDefault, the_cString, kCFStringEncodingUTF8);
the_Url = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, the_FilePath, kCFURLHFSPathStyle, false);
the_bReturnValue = CFURLGetFileSystemRepresentation(the_Url, true, the_pcUnixFilePath, 256);
strcpy(filename, the_pcUnixFilePath);//till here
#endif
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_jmpbuf(png_ptr)))
{
/* 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);
color_type = png_get_color_type(png_ptr, info_ptr);
if( color_type != PNG_COLOR_TYPE_RGB &&
color_type != PNG_COLOR_TYPE_PALETTE &&
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 (color_type == PNG_COLOR_TYPE_PALETTE)
png_set_expand(png_ptr);
SetImageDefaults( im );
im->width = png_get_image_width(png_ptr, info_ptr);
im->height = png_get_image_height(png_ptr, info_ptr);
im->bytesPerLine = png_get_rowbytes(png_ptr, info_ptr);
im->dataSize = im->height * im->bytesPerLine;
im->bitsPerPixel = 8 * im->bytesPerLine / im->width;
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
uint8_t pix, *p;
LOOP_IMAGE( im, p = (uint8_t*)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
uint16_t pix, *p;
LOOP_IMAGE( im, p = (uint16_t*)idata; pix=p[0]; p[0]=p[3];p[3]=p[2];p[2]=p[1];p[1]=pix; )
}
#ifndef PT_BIGENDIAN
// Swap bytes in shorts
if(im->bitsPerPixel == 48){
uint8_t b,*id;
// the original construct of *id = *(++id) results in:
//.L80:
// movb (%edx), %al
// incl %ecx
// movb %al, 1(%edx)
// movb 2(%edx), %al
// movb %al, 3(%edx)
// movb 4(%edx), %al
// movb %al, 5(%edx)
// movb 6(%edx), %al
// movb %al, 7(%edx)
// addl %edi, %edx
// movl 8(%ebp), %eax
// cmpl %ecx, (%eax)
// jg .L80
//
// when compiled with -O2 which is incorrect since it does not swap the bytes.
// Force explicit ordering with *id=*(id+1); id++ for correct output
LOOP_IMAGE( im, id = idata; \
b = *id; *id = *(id + 1); id++; *(id++)=b; \
b = *id; *id = *(id + 1); id++; *(id++)=b; \
b = *id; *id = *(id + 1); id++; *(id)=b; )
}
if(im->bitsPerPixel == 64){
uint8_t b,*id;
LOOP_IMAGE( im, id = idata; \
b = *id; *id = *(id + 1); id++; *(id++)=b; \
b = *id; *id = *(id + 1); id++; *(id++)=b; \
b = *id; *id = *(id + 1); id++; *(id++)=b; \
b = *id; *id = *(id + 1); 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;
}
int panoPNGRead(Image *im, fullPath *sfile )
{
if (readPNG(im, sfile) == 0) {
return panoMetadataUpdateFromImage(im);
} else
return FALSE;
}