summaryrefslogtreecommitdiff
path: root/graphics/jpeg/files/patch-transupp.c
blob: 48927aa80bc0b6e600238cef26f6ef7d3d1e5269 (plain) (blame)
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
--- transupp.c.exif	1997-08-10 02:15:26.000000000 +0200
+++ transupp.c	2003-09-29 22:28:42.000000000 +0200
@@ -717,6 +717,194 @@
 }
 
 
+/* Adjust Exif image parameters.
+ *
+ * We try to adjust the Tags ExifImageWidth, ExifImageHeight and
+ * ExifOrientation if possible. If the given new_* value is zero the
+ * corresponding tag is not adjusted.
+ */
+
+LOCAL(void)
+adjust_exif_parameters (JOCTET FAR * data, unsigned int length,
+			JDIMENSION new_width, JDIMENSION new_height,
+                        unsigned int new_orient)
+{
+  boolean is_motorola; /* Flag for byte order */
+  unsigned int number_of_tags, tagnum;
+  unsigned int firstoffset, offset, exifsuboffset;
+  JDIMENSION new_value;
+
+  if (length < 12) return; /* Length of an IFD entry */
+
+  /* Discover byte order */
+  if (GETJOCTET(data[0]) == 0x49 && GETJOCTET(data[1]) == 0x49)
+    is_motorola = FALSE;
+  else if (GETJOCTET(data[0]) == 0x4D && GETJOCTET(data[1]) == 0x4D)
+    is_motorola = TRUE;
+  else
+    return;
+
+  /* Check Tag Mark */
+  if (is_motorola) {
+    if (GETJOCTET(data[2]) != 0) return;
+    if (GETJOCTET(data[3]) != 0x2A) return;
+  } else {
+    if (GETJOCTET(data[3]) != 0) return;
+    if (GETJOCTET(data[2]) != 0x2A) return;
+  }
+
+  /* Get first IFD offset (offset to IFD0) */
+  if (is_motorola) {
+    if (GETJOCTET(data[4]) != 0) return;
+    if (GETJOCTET(data[5]) != 0) return;
+    firstoffset = GETJOCTET(data[6]);
+    firstoffset <<= 8;
+    firstoffset += GETJOCTET(data[7]);
+  } else {
+    if (GETJOCTET(data[7]) != 0) return;
+    if (GETJOCTET(data[6]) != 0) return;
+    firstoffset = GETJOCTET(data[5]);
+    firstoffset <<= 8;
+    firstoffset += GETJOCTET(data[4]);
+  }
+  if (firstoffset > length - 2) return; /* check end of data segment */
+
+  /* Get the number of directory entries contained in this IFD */
+  if (is_motorola) {
+    number_of_tags = GETJOCTET(data[firstoffset]);
+    number_of_tags <<= 8;
+    number_of_tags += GETJOCTET(data[firstoffset+1]);
+  } else {
+    number_of_tags = GETJOCTET(data[firstoffset+1]);
+    number_of_tags <<= 8;
+    number_of_tags += GETJOCTET(data[firstoffset]);
+  }
+  if (number_of_tags == 0) return;
+  firstoffset += 2;
+
+  /* Search for ExifSubIFD offset and ExifOrient Tag in IFD0 */
+  exifsuboffset = 0;
+  for (;;) {
+    if (firstoffset > length - 12) break; /* check end of data segment */
+    /* Get Tag number */
+    if (is_motorola) {
+      tagnum = GETJOCTET(data[firstoffset]);
+      tagnum <<= 8;
+      tagnum += GETJOCTET(data[firstoffset+1]);
+    } else {
+      tagnum = GETJOCTET(data[firstoffset+1]);
+      tagnum <<= 8;
+      tagnum += GETJOCTET(data[firstoffset]);
+    }
+    if (tagnum == 0x0112 && new_orient > 0) { /* found ExifOrientation */
+      if (is_motorola) {
+        data[firstoffset+2] = 0; /* Format = unsigned short (2 octets) */
+        data[firstoffset+3] = 3;
+        data[firstoffset+4] = 0; /* Number Of Components = 1 */
+        data[firstoffset+5] = 0;
+        data[firstoffset+6] = 0;
+        data[firstoffset+7] = 1;
+        data[firstoffset+8] = 0;
+        data[firstoffset+9] = (JOCTET)new_orient;
+        data[firstoffset+10] = 0;
+        data[firstoffset+11] = 0;
+      } else {
+        data[firstoffset+2] = 3; /* Format = unsigned short (2 octets) */
+        data[firstoffset+3] = 0;
+        data[firstoffset+4] = 1; /* Number Of Components = 1 */
+        data[firstoffset+5] = 0;
+        data[firstoffset+6] = 0;
+        data[firstoffset+7] = 0;
+        data[firstoffset+8] = (JOCTET)new_orient;
+        data[firstoffset+9] = 0;
+        data[firstoffset+10] = 0;
+        data[firstoffset+11] = 0;
+      }
+    } else if (tagnum == 0x8769) {
+      exifsuboffset = firstoffset; /* found ExifSubIFD offset Tag */
+    }
+    if (--number_of_tags == 0) break;
+    firstoffset += 12;
+  }
+  if (exifsuboffset == 0) return;
+
+  /* Get the ExifSubIFD offset */
+  if (is_motorola) {
+    if (GETJOCTET(data[exifsuboffset+8]) != 0) return;
+    if (GETJOCTET(data[exifsuboffset+9]) != 0) return;
+    offset = GETJOCTET(data[exifsuboffset+10]);
+    offset <<= 8;
+    offset += GETJOCTET(data[exifsuboffset+11]);
+  } else {
+    if (GETJOCTET(data[exifsuboffset+11]) != 0) return;
+    if (GETJOCTET(data[exifsuboffset+10]) != 0) return;
+    offset = GETJOCTET(data[exifsuboffset+9]);
+    offset <<= 8;
+    offset += GETJOCTET(data[exifsuboffset+8]);
+  }
+  if (offset > length - 2) return; /* check end of data segment */
+
+  /* Get the number of directory entries contained in this SubIFD */
+  if (is_motorola) {
+    number_of_tags = GETJOCTET(data[offset]);
+    number_of_tags <<= 8;
+    number_of_tags += GETJOCTET(data[offset+1]);
+  } else {
+    number_of_tags = GETJOCTET(data[offset+1]);
+    number_of_tags <<= 8;
+    number_of_tags += GETJOCTET(data[offset]);
+  }
+  if (number_of_tags < 2) return;
+  offset += 2;
+
+  /* Search for ExifImageWidth and ExifImageHeight Tags in this SubIFD */
+  do {
+    if (offset > length - 12) return; /* check end of data segment */
+    /* Get Tag number */
+    if (is_motorola) {
+      tagnum = GETJOCTET(data[offset]);
+      tagnum <<= 8;
+      tagnum += GETJOCTET(data[offset+1]);
+    } else {
+      tagnum = GETJOCTET(data[offset+1]);
+      tagnum <<= 8;
+      tagnum += GETJOCTET(data[offset]);
+    }
+    if ((tagnum == 0xA002 && new_width > 0) ||
+        (tagnum == 0xA003 && new_height > 0)) {
+      if (tagnum == 0xA002)
+	new_value = new_width; /* ExifImageWidth Tag */
+      else
+	new_value = new_height; /* ExifImageHeight Tag */
+      if (is_motorola) {
+	data[offset+2] = 0; /* Format = unsigned long (4 octets) */
+	data[offset+3] = 4;
+	data[offset+4] = 0; /* Number Of Components = 1 */
+	data[offset+5] = 0;
+	data[offset+6] = 0;
+	data[offset+7] = 1;
+	data[offset+8] = 0;
+	data[offset+9] = 0;
+	data[offset+10] = (JOCTET)((new_value >> 8) & 0xFF);
+	data[offset+11] = (JOCTET)(new_value & 0xFF);
+      } else {
+	data[offset+2] = 4; /* Format = unsigned long (4 octets) */
+	data[offset+3] = 0;
+	data[offset+4] = 1; /* Number Of Components = 1 */
+	data[offset+5] = 0;
+	data[offset+6] = 0;
+	data[offset+7] = 0;
+	data[offset+8] = (JOCTET)(new_value & 0xFF);
+	data[offset+9] = (JOCTET)((new_value >> 8) & 0xFF);
+	data[offset+10] = 0;
+	data[offset+11] = 0;
+      }
+    }
+    offset += 12;
+  } while (--number_of_tags);
+}
+
+
 /* Adjust output image parameters as needed.
  *
  * This must be called after jpeg_copy_critical_parameters()
@@ -734,6 +922,8 @@
 			      jvirt_barray_ptr *src_coef_arrays,
 			      jpeg_transform_info *info)
 {
+  jpeg_saved_marker_ptr cur_marker; /* ptr to walk the marker list */
+
   /* If force-to-grayscale is requested, adjust destination parameters */
   if (info->force_grayscale) {
     /* We use jpeg_set_colorspace to make sure subsidiary settings get fixed
@@ -799,6 +989,28 @@
     break;
   }
 
+  /* Adjust Exif properties. Exif requires its APP marker to be the first
+   * one, but we allow other locations for mixed JFIF/Exif files. */
+  cur_marker = srcinfo->marker_list;
+  while (cur_marker != NULL) {
+    if (cur_marker->marker == JPEG_APP0+1 &&
+        cur_marker->data_length >= 6 &&
+        GETJOCTET(cur_marker->data[0]) == 0x45 &&
+        GETJOCTET(cur_marker->data[1]) == 0x78 &&
+        GETJOCTET(cur_marker->data[2]) == 0x69 &&
+        GETJOCTET(cur_marker->data[3]) == 0x66 &&
+        GETJOCTET(cur_marker->data[4]) == 0 &&
+        GETJOCTET(cur_marker->data[5]) == 0) {
+      /* Adjust Exif image parameters */
+      if (info->transform != JXFORM_NONE)
+        /* Align data segment to start of TIFF structure for parsing */
+        adjust_exif_parameters(cur_marker->data + 6,
+                               cur_marker->data_length - 6,
+                               dstinfo->image_width, dstinfo->image_height, 1);
+    }
+    cur_marker = cur_marker->next;
+  }
+
   /* Return the appropriate output data set */
   if (info->workspace_coef_arrays != NULL)
     return info->workspace_coef_arrays;
@@ -854,6 +1066,8 @@
 
 
 /* Setup decompression object to save desired markers in memory.
+ * Unless JCOPYOPT_NONE is given, the COM and potential EXIF markers (APP1)
+ * are always saved.
  * This must be called before jpeg_read_header() to have the desired effect.
  */
 
@@ -871,6 +1085,8 @@
   if (option == JCOPYOPT_ALL) {
     for (m = 0; m < 16; m++)
       jpeg_save_markers(srcinfo, JPEG_APP0 + m, 0xFFFF);
+  } else if (option != JCOPYOPT_NONE) {
+    jpeg_save_markers(srcinfo, JPEG_APP0 + 1, 0xFFFF);
   }
 #endif /* SAVE_MARKERS_SUPPORTED */
 }
@@ -888,12 +1104,27 @@
 {
   jpeg_saved_marker_ptr marker;
 
-  /* In the current implementation, we don't actually need to examine the
-   * option flag here; we just copy everything that got saved.
-   * But to avoid confusion, we do not output JFIF and Adobe APP14 markers
+  /* NOTE: to avoid confusion, we do not output JFIF and Adobe APP14 markers
    * if the encoder library already wrote one.
    */
+  if (option == JCOPYOPT_NONE) return;
   for (marker = srcinfo->marker_list; marker != NULL; marker = marker->next) {
+    if (option == JCOPYOPT_COMMENTS &&
+        marker->marker != JPEG_COM) {
+      continue;         /* only COM accpeted if JCOPYOPT_COMMENTS */
+    }
+    if (option == JCOPYOPT_EXIF &&
+        marker->marker != JPEG_COM &&
+        !(marker->marker == JPEG_APP0+1 &&
+          marker->data_length >= 6 &&
+          GETJOCTET(marker->data[0]) == 0x45 &&
+          GETJOCTET(marker->data[1]) == 0x78 &&
+          GETJOCTET(marker->data[2]) == 0x69 &&
+          GETJOCTET(marker->data[3]) == 0x66 &&
+          GETJOCTET(marker->data[4]) == 0 &&
+          GETJOCTET(marker->data[5]) == 0)) {
+      continue;         /* only COM and APP1-EXIF if JCOPYOPT_EXIF */
+    }
     if (dstinfo->write_JFIF_header &&
 	marker->marker == JPEG_APP0 &&
 	marker->data_length >= 5 &&