forked from Matthias-Wandel/imgcomp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjpgfile.c
executable file
·219 lines (161 loc) · 5.42 KB
/
jpgfile.c
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
//--------------------------------------------------------------------------
// Module to interface with exif.c from jhead project.
// Matthias Wandel 2015
//
// Imgcomp is licensed under GPL v2 (see README.txt)
//--------------------------------------------------------------------------
#include "jhead.h"
#ifdef VIEWCGI
#define Log stdout
#else
#include "imgcomp.h"
#endif
// Storage for simplified info extracted from file.
ImageInfo_t ImageInfo;
int ShowTags = 0;
int SupressNonFatalErrors = 0;
//--------------------------------------------------------------------------
// Report non fatal errors. Now that microsoft.net modifies exif headers,
// there's corrupted ones, and there could be more in the future.
//--------------------------------------------------------------------------
void ErrNonfatal(const char * msg, int a1, int a2)
{
if (SupressNonFatalErrors) return;
fprintf(Log,"\nNonfatal Error : ");
fprintf(Log, msg, a1, a2);
fprintf(Log, "\n");
}
//--------------------------------------------------------------------------
// Get 16 bits motorola order (always) for jpeg header stuff.
//--------------------------------------------------------------------------
static int Get16m(const void * Short)
{
return (((uchar *)Short)[0] << 8) | ((uchar *)Short)[1];
}
//--------------------------------------------------------------------------
// Process a SOFn marker. This is useful for the image dimensions
//--------------------------------------------------------------------------
static void process_SOFn (const uchar * Data, int marker)
{
int data_precision, num_components;
data_precision = Data[2];
ImageInfo.Height = Get16m(Data+3);
ImageInfo.Width = Get16m(Data+5);
num_components = Data[7];
if (num_components == 3){
ImageInfo.IsColor = 1;
}else{
ImageInfo.IsColor = 0;
}
ImageInfo.Process = marker;
if (ShowTags){
printf("JPEG image is %uw * %uh, %d color components, %d bits per sample\n",
ImageInfo.Width, ImageInfo.Height, num_components, data_precision);
}
}
//--------------------------------------------------------------------------
// Parse the marker stream until SOS or EOI is seen;
//--------------------------------------------------------------------------
static int FindExifInFile (FILE * infile)
{
int a;
int have_exif = 0, have_sof= 0;
int SectionsRead = 0;
a = fgetc(infile);
if (a != 0xff || fgetc(infile) != M_SOI){
return FALSE;
}
for(;;){
int itemlen;
int marker = 0;
int prev;
int ll,lh, got;
uchar * Data;
for (a=0;;a++){
prev = marker;
marker = fgetc(infile);
if (marker != 0xff && prev == 0xff) break;
if (marker == EOF){
fprintf(Log, "Unexpected end of file");
return 0;
}
}
if (a > 10){
fprintf(Log, "Extraneous %d padding bytes before section %02X",a-1,marker);
return 0;
}
// Read the length of the section.
lh = fgetc(infile);
ll = fgetc(infile);
if (lh == EOF || ll == EOF){
fprintf(Log, "Unexpected end of file at %ld",ftell(infile));
return 0;
}
itemlen = (lh << 8) | ll;
if (itemlen < 2){
fprintf(Log, "invalid jpg marker");
return 0;
}
Data = (uchar *)malloc(itemlen);
if (Data == NULL){
fprintf(Log, "Could not allocate memory");
return 0;
}
// Store first two pre-read bytes.
Data[0] = (uchar)lh;
Data[1] = (uchar)ll;
got = fread(Data+2, 1, itemlen-2, infile); // Read the whole section.
if (got != itemlen-2){
fprintf(Log, "Premature end of file?");
return 0;
}
SectionsRead += 1;
switch(marker){
case M_EXIF:
if (memcmp(Data+2, "Exif", 4) == 0){
process_EXIF(Data, itemlen);
have_exif = 1;
}
break;
#ifdef VIEWCGI // We only care to guess jpg quality fiew view.cgi program.
case M_DQT:
// Use for jpeg quality guessing
process_DQT(Data, itemlen);
break;
#endif
case M_SOF0:
case M_SOF1:
case M_SOF2:
case M_SOF3:
case M_SOF5:
case M_SOF6:
case M_SOF7:
case M_SOF9:
case M_SOF10:
case M_SOF11:
case M_SOF13:
case M_SOF14:
case M_SOF15:
process_SOFn(Data, marker);
have_sof = 1;
break;
case M_SOS:
SectionsRead += 100; //Stop reading stuff!
break;
}
free(Data);
if (have_exif && have_sof) break;
if (SectionsRead > 4) break; // Don't read too far!
}
return 0;
}
//--------------------------------------------------------------------------
// Parse the marker stream until SOS or EOI is seen;
//--------------------------------------------------------------------------
int ReadExifPart(FILE * infile)
{
int a;
a = FindExifInFile(infile);
rewind(infile); // go back to start for libexif to read the image.
return a;
}