[7339b1]: jpeg.c Maximize Restore History

Download this file

jpeg.c    268 lines (201 with data), 6.8 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
#include <stdio.h>
//#include <jpeglib.h>
//#define __NO_SYSTEM__
#include "filter.h"
#include "metadata.h"
#include "file.h"
#include "jpegicc.h"
int writeJPEG(Image * im, fullPath * sfile, int quality, int progressive)
{
struct jpeg_compress_struct cinfo;
struct jpeg_error_mgr jerr;
FILE *outfile;
char filename[512];
int scanlines_written;
unsigned char *data, *buf;
#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
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_compress(&cinfo);
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;
}
TwoToOneByte(im);
jpeg_stdio_dest(&cinfo, outfile);
cinfo.image_width = im->width; /* image width and height, in pixels */
cinfo.image_height = im->height;
cinfo.input_components = 3; /* # of color components per pixel */
cinfo.in_color_space = JCS_RGB; /* colorspace of input image */
jpeg_set_defaults(&cinfo);
if (2 != (progressive & 2))
cinfo.optimize_coding = TRUE;
jpeg_set_quality(&cinfo, quality, TRUE);
if (1 == (progressive & 1))
jpeg_simple_progression(&cinfo);
jpeg_start_compress(&cinfo, TRUE);
// Write ICC Profile if it exists
if (im->metadata.iccProfile.size > 0) {
jpegICCWriteProfile(&cinfo,
(JOCTET *)im->metadata.iccProfile.data,
im->metadata.iccProfile.size);
}
scanlines_written = 0;
data = *(im->data);
buf = (unsigned char *) malloc((size_t) im->bytesPerLine);
if (buf == NULL)
{
PrintError("Not enough memory");
fclose(outfile);
return -1;
}
while (scanlines_written < im->height)
{
memcpy(buf, data, (size_t) im->bytesPerLine);
if (im->bitsPerPixel == 32) // Convert 4->3 samples
{
int x;
unsigned char *c1 = buf, *c2 = buf;
for (x = 0; x < im->width; x++)
{
c2++;
*c1++ = *c2++;
*c1++ = *c2++;
*c1++ = *c2++;
}
}
if (jpeg_write_scanlines(&cinfo, (JSAMPARRAY) & buf, 1))
{
scanlines_written++;
data += im->bytesPerLine;
}
}
jpeg_finish_compress(&cinfo);
jpeg_destroy_compress(&cinfo);
fclose(outfile);
free(buf);
return 0;
}
int readJPEG(Image * im, fullPath * sfile)
{
struct jpeg_decompress_struct cinfo;
struct jpeg_error_mgr jerr;
FILE *infile;
char filename[256];
int scan_lines_to_be_read, scan_lines_read;
unsigned int i, scanheight;
unsigned char *data;
JSAMPARRAY sarray;
JOCTET *ptr = NULL;
unsigned int size = 0;
#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
//PrintError("%s", sfile->name);
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_decompress(&cinfo);
// Prepare the library for reading the ICC profile, see its source code
// for further information
jpegICCSetupReadICCProfile(&cinfo);
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;
}
jpeg_stdio_src(&cinfo, infile);
jpeg_read_header(&cinfo, TRUE);
jpeg_start_decompress(&cinfo);
SetImageDefaults(im);
im->width = cinfo.output_width;
im->height = cinfo.output_height;
if (cinfo.output_components != 3)
{
PrintError("Image must be rgb");
fclose(infile);
return -1;
}
im->bitsPerPixel = 24;
im->bytesPerLine = im->width * 3;
im->dataSize = im->width * 4 * im->height;
im->data = (unsigned char **) mymalloc((size_t) im->dataSize);
if (im->data == NULL)
{
PrintError("Not enough memory");
fclose(infile);
return -1;
}
scanheight = cinfo.rec_outbuf_height;
sarray = (JSAMPARRAY) malloc(scanheight * sizeof(JSAMPROW));
scan_lines_to_be_read = im->height;
data = *(im->data);
while (scan_lines_to_be_read)
{
for (i = 0; i < scanheight; i++)
{
sarray[i] = (JSAMPROW) (data + i * im->bytesPerLine);
}
scan_lines_read = jpeg_read_scanlines(&cinfo, sarray, scanheight);
scan_lines_to_be_read -= scan_lines_read;
data += scan_lines_read * im->bytesPerLine;
}
// read ICC profile
if (jpegICCReadProfile(&cinfo, &ptr, &size)) {
im->metadata.iccProfile.size = size;
im->metadata.iccProfile.data = (char*) ptr;
}
jpeg_finish_decompress(&cinfo);
jpeg_destroy_decompress(&cinfo);
ThreeToFourBPP(im);
free(sarray);
fclose(infile);
return 0;
}
// this function is very likely to change
// so it should be considered a hack until
// we are able to read all the JPEG metadata
int panoJPEGRead(Image * im, fullPath * sfile)
{
if ( readJPEG(im, sfile) == 0) {
return panoMetadataUpdateFromImage(im);
} else
return FALSE;
}