Line | Count | Source |
1 | | /* jpegmpf.c - JPEG MPF marker related functions |
2 | | * |
3 | | * Copyright (c) 2026 Timo Kokkonen |
4 | | * All Rights Reserved. |
5 | | * |
6 | | * |
7 | | * SPDX-License-Identifier: GPL-3.0-or-later |
8 | | * |
9 | | * This file is part of JPEGinfo. |
10 | | * |
11 | | * JPEGinfo is free software: you can redistribute it and/or modify |
12 | | * it under the terms of the GNU General Public License as published by |
13 | | * the Free Software Foundation, either version 3 of the License, or |
14 | | * (at your option) any later version. |
15 | | * |
16 | | * JPEGinfo is distributed in the hope that it will be useful, |
17 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
18 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
19 | | * GNU General Public License for more details. |
20 | | * |
21 | | * You should have received a copy of the GNU General Public License |
22 | | * along with JPEGinfo. If not, see <https://www.gnu.org/licenses/>. |
23 | | */ |
24 | | |
25 | | #ifdef HAVE_CONFIG_H |
26 | | #include "config.h" |
27 | | #endif |
28 | | |
29 | | #include <stdio.h> |
30 | | #include <inttypes.h> |
31 | | #include <string.h> |
32 | | #include <jpeglib.h> |
33 | | #include "easy-endian.h" |
34 | | #include "jpegmpf.h" |
35 | | |
36 | | //#define DEBUG 1 |
37 | | |
38 | | #if DEBUG > 0 |
39 | | #define DEBUG_PRINT(fmt, ...) { \ |
40 | | printf("%s:%d %s(): " fmt, __FILE__, __LINE__, __func__, ##__VA_ARGS__); \ |
41 | | } |
42 | | #else |
43 | | #define DEBUG_PRINT(...) |
44 | | #endif |
45 | | |
46 | | |
47 | | /* These come from CIPA DC-007-2009 standard */ |
48 | | |
49 | | static const char *mp_format_identifier = "MPF\0"; |
50 | | static const int mp_format_identifier_len = 4; |
51 | | |
52 | | static const char *mp_little_endian_magic = "II*\0"; |
53 | | static const char *mp_big_endian_magic = "MM\0*"; |
54 | | static const int mp_endian_magic_len = 4; |
55 | | |
56 | | |
57 | | enum mpf_idf_types { |
58 | | MP_Unknown_IDF = 0, |
59 | | MP_Index_IDF, |
60 | | MP_Attribute_IDF, |
61 | | }; |
62 | | |
63 | | enum mpf_tag_ids { |
64 | | MPFVersion = 0xb000, |
65 | | NumberOfImages = 0xb001, |
66 | | MPEntry = 0xb002, |
67 | | ImageUIDList = 0xb003, |
68 | | TotalFrames = 0xb004, |
69 | | |
70 | | MPIndividualNum = 0xb101, |
71 | | |
72 | | PanOrientation = 0xb201, |
73 | | PanOverlap_H = 0xb202, |
74 | | PanOverlap_V = 0xb203, |
75 | | BaseViewpointNum = 0xb204, |
76 | | ConvergenceAngle = 0xb205, |
77 | | BaselineLength = 0xb206, |
78 | | VerticalDivergence = 0xb207, |
79 | | AxisDistance_X = 0xb208, |
80 | | AxisDistance_Y = 0xb209, |
81 | | AxisDistance_Z = 0xb20a, |
82 | | YawAngle = 0xb20b, |
83 | | PitchAngle = 0xb20c, |
84 | | RollAngle = 0xb20d, |
85 | | }; |
86 | | |
87 | | enum mpf_data_types { |
88 | | MP_LONG = 4, |
89 | | MP_UNDEFINED = 7, |
90 | | }; |
91 | | |
92 | | enum mpf_image_attributes { |
93 | | /* Flag bits */ |
94 | | DependentParentImage_Flag = (1UL << 31), |
95 | | DependentChildImage_Flag = (1UL << 30), |
96 | | RepresentativeImage_Flag = (1UL << 29), |
97 | | |
98 | | /* MP Type codes */ |
99 | | MPPrimaryImage = 0x030000, |
100 | | LargeThumbnail_Class1 = 0x010001, |
101 | | LargeThumbnail_Class2 = 0x010002, |
102 | | MultiFrameImage_Panorama = 0x020001, |
103 | | MultiFrameImage_Disparity = 0x020002, |
104 | | MultiFrameImage_MultiAngle = 0x020003, |
105 | | Undefined = 0x000000, |
106 | | |
107 | | MP_Type_Mask = 0x03ffff, |
108 | | }; |
109 | | |
110 | | |
111 | | |
112 | | static uint32_t read32(const void *ptr, mpf_marker_t *mpf) |
113 | 0 | { |
114 | 0 | uint32_t val; |
115 | |
|
116 | 0 | memcpy(&val, ptr, sizeof(val)); |
117 | |
|
118 | 0 | if (mpf->big_endian) |
119 | 0 | return be32toh(val); |
120 | | |
121 | 0 | return le32toh(val); |
122 | 0 | } |
123 | | |
124 | | |
125 | | static uint16_t read16(const void *ptr, mpf_marker_t *mpf) |
126 | 0 | { |
127 | 0 | uint16_t val; |
128 | |
|
129 | 0 | memcpy(&val, ptr, sizeof(val)); |
130 | |
|
131 | 0 | if (mpf->big_endian) |
132 | 0 | return be16toh(val); |
133 | | |
134 | 0 | return le16toh(val); |
135 | 0 | } |
136 | | |
137 | | |
138 | | static void write32(void *ptr, uint32_t val, mpf_marker_t *mpf) |
139 | 0 | { |
140 | 0 | uint32_t n_val; |
141 | |
|
142 | 0 | if (mpf->big_endian) |
143 | 0 | n_val = htobe32(val); |
144 | 0 | else |
145 | 0 | n_val = htole32(val); |
146 | |
|
147 | 0 | memcpy(ptr, &n_val, sizeof(n_val)); |
148 | 0 | } |
149 | | |
150 | | |
151 | | |
152 | | static int parse_idf(const char *marker, uint32_t len, uint32_t offset, mpf_marker_t *mpf, enum mpf_idf_types idf_type) |
153 | 0 | { |
154 | 0 | if (!marker || len <= 0 || offset < 0 || offset >= len || !mpf || idf_type == MP_Unknown_IDF) |
155 | 0 | return -1; |
156 | | |
157 | | /* Get count of entries in this IDF */ |
158 | 0 | uint32_t o = offset; |
159 | 0 | if (o + 2 > len) |
160 | 0 | return -2; |
161 | 0 | uint16_t count = read16(marker + o, mpf); |
162 | 0 | o += 2; |
163 | 0 | uint32_t idf_len = 2 + (count * 12) + 4; |
164 | 0 | DEBUG_PRINT("IDF tag_count=%u, len=%u\n", count, idf_len); |
165 | 0 | if (idf_len > len - offset) |
166 | 0 | return -3; |
167 | | |
168 | | |
169 | | /* Parse tags in the IFD */ |
170 | 0 | uint16_t previous_tag = 0; |
171 | |
|
172 | 0 | for(int i = 0; i < count; i++) { |
173 | 0 | const char *m = marker + o; |
174 | 0 | uint16_t tag = read16(m, mpf); |
175 | 0 | uint16_t type = read16(m + 2, mpf); |
176 | 0 | uint32_t count = read32(m + 4, mpf); |
177 | 0 | uint32_t val = read32(m + 8, mpf); |
178 | |
|
179 | 0 | DEBUG_PRINT("IDFtag=%04x, type=%u, count=%u, default=%u\n", tag, type, count, val); |
180 | | |
181 | | /* Tags must be specified in the order of their tag IDs */ |
182 | 0 | if (tag <= previous_tag) { |
183 | 0 | DEBUG_PRINT("Invalid tag order (%04x after %04x)\n", tag, previous_tag); |
184 | 0 | return -4; |
185 | 0 | } |
186 | 0 | previous_tag = tag; |
187 | |
|
188 | 0 | switch(tag) { |
189 | 0 | case MPFVersion: |
190 | 0 | if (type != MP_UNDEFINED || count !=4) |
191 | 0 | return -5; |
192 | 0 | memcpy(mpf->version, m + 8, 4); |
193 | 0 | mpf->version[4] = 0; |
194 | 0 | DEBUG_PRINT(" MPVersion: '%s'\n", mpf->version); |
195 | 0 | break; |
196 | | |
197 | 0 | case NumberOfImages: |
198 | 0 | if (idf_type != MP_Index_IDF || type != MP_LONG || count != 1 || val < 2) |
199 | 0 | return -6; |
200 | 0 | mpf->number_of_images = val; |
201 | 0 | DEBUG_PRINT(" NumberOfImages: %u\n", mpf->number_of_images); |
202 | 0 | break; |
203 | | |
204 | 0 | case MPEntry: |
205 | 0 | val += 4; |
206 | 0 | if (idf_type != MP_Index_IDF || type != MP_UNDEFINED || |
207 | 0 | count != (uint64_t)16 * mpf->number_of_images || |
208 | 0 | val < (uint64_t)offset + idf_len || |
209 | 0 | (uint64_t)val + count > len) |
210 | 0 | return -7; |
211 | 0 | mpf->mpentry_list_offset = val; |
212 | 0 | DEBUG_PRINT(" MPEntry list offset: %d (len=%u)\n", mpf->mpentry_list_offset, len); |
213 | | #if DEBUG > 0 |
214 | | for(uint32_t img = 0; img < mpf->number_of_images; img++) { |
215 | | const char *e = marker + mpf->mpentry_list_offset + (img * 16); |
216 | | uint32_t attr = read32(e, mpf); |
217 | | uint32_t imgsize = read32(e + 4, mpf); |
218 | | uint32_t imgoffset = read32(e + 8, mpf); |
219 | | uint16_t dep1 = read16(e + 12, mpf); |
220 | | uint16_t dep2 = read16(e + 14, mpf); |
221 | | uint32_t imgoffset2 = imgoffset > 0 ? imgoffset + mpf->marker_offset + 4 : 0; |
222 | | |
223 | | DEBUG_PRINT(" MPentry%d: attr=%08x, imgsize=%u, imgoffset=%u (%u), dep1=%u, dep2=%u\n", |
224 | | img, attr, imgsize, imgoffset, imgoffset2, dep1, dep2); |
225 | | } |
226 | | #endif |
227 | 0 | break; |
228 | | |
229 | 0 | case ImageUIDList: |
230 | 0 | val += 4; |
231 | 0 | if (idf_type != MP_Index_IDF || type != MP_UNDEFINED || |
232 | 0 | count != (uint64_t)33 * mpf->number_of_images || |
233 | 0 | val < (uint64_t)offset + idf_len || |
234 | 0 | (uint64_t)val + count > len) |
235 | 0 | return -8; |
236 | 0 | mpf->imageuid_list_offset = val; |
237 | 0 | DEBUG_PRINT(" ImageUIDList offset: %u\n", mpf->imageuid_list_offset); |
238 | 0 | break; |
239 | | |
240 | 0 | case TotalFrames: |
241 | 0 | if (idf_type != MP_Index_IDF || count != 1 || type != MP_LONG) |
242 | 0 | return -9; |
243 | 0 | mpf->total_frames = val; |
244 | 0 | DEBUG_PRINT(" TotalFrames: %u\n", mpf->total_frames); |
245 | 0 | break; |
246 | | |
247 | 0 | case MPIndividualNum: |
248 | 0 | if (idf_type != MP_Attribute_IDF || count != 1 || type != MP_LONG) |
249 | 0 | return -10; |
250 | 0 | mpf->individual_image_number = val; |
251 | 0 | break; |
252 | | |
253 | 0 | case PanOrientation: |
254 | 0 | case PanOverlap_H: |
255 | 0 | case PanOverlap_V: |
256 | 0 | case BaseViewpointNum: |
257 | 0 | case ConvergenceAngle: |
258 | 0 | case BaselineLength: |
259 | 0 | case VerticalDivergence: |
260 | 0 | case AxisDistance_X: |
261 | 0 | case AxisDistance_Y: |
262 | 0 | case AxisDistance_Z: |
263 | 0 | case YawAngle: |
264 | 0 | case PitchAngle: |
265 | 0 | case RollAngle: |
266 | 0 | if (idf_type != MP_Attribute_IDF || count != 1) |
267 | 0 | return -11; |
268 | | /* These are currently ignored... */ |
269 | 0 | break; |
270 | | |
271 | 0 | default: |
272 | 0 | DEBUG_PRINT(" Unknown tag!\n"); |
273 | |
|
274 | 0 | } |
275 | | |
276 | 0 | o += 12; |
277 | 0 | } |
278 | | |
279 | | |
280 | | /* Offset of Next IFD */ |
281 | 0 | if (o + 4 > len) |
282 | 0 | return -100; |
283 | 0 | uint32_t next_ifd = read32(marker + o, mpf); |
284 | 0 | DEBUG_PRINT(" Offset of Next IFD: %u\n", next_ifd); |
285 | | |
286 | | /* Check that offset is within the marker */ |
287 | 0 | if (next_ifd + 4 >= len) |
288 | 0 | return -101; |
289 | | |
290 | 0 | return next_ifd; |
291 | 0 | } |
292 | | |
293 | | |
294 | | static int read_mpf_marker(const char *data, unsigned int len, mpf_marker_t *mpf, size_t marker_offset, bool first_image) |
295 | 0 | { |
296 | 0 | const char *m = data; |
297 | |
|
298 | 0 | if (!data || len <= 0 || !mpf) |
299 | 0 | return -1; |
300 | | |
301 | 0 | DEBUG_PRINT("MPF Maker offset: %lu\n", marker_offset); |
302 | |
|
303 | 0 | memset(mpf, 0, sizeof(mpf_marker_t)); |
304 | 0 | mpf->marker_offset = marker_offset; |
305 | 0 | mpf->data = data; |
306 | 0 | mpf->data_len = len; |
307 | |
|
308 | 0 | if (len < mp_format_identifier_len + mp_endian_magic_len + 4) |
309 | 0 | return 1; |
310 | | |
311 | | /* Check MP Format Identifier (4 bytes) */ |
312 | 0 | if (memcmp(m, mp_format_identifier, mp_format_identifier_len)) |
313 | 0 | return 2; |
314 | 0 | m += mp_format_identifier_len; |
315 | | |
316 | | /* Check MP Endian Tag (4 bytes) */ |
317 | 0 | if (!memcmp(m, mp_little_endian_magic, mp_endian_magic_len)) { |
318 | 0 | mpf->big_endian = false; |
319 | 0 | DEBUG_PRINT("MP Endian: Little Endian\n"); |
320 | 0 | } |
321 | 0 | else if (!memcmp(m, mp_big_endian_magic, mp_endian_magic_len)) { |
322 | 0 | mpf->big_endian = true; |
323 | 0 | DEBUG_PRINT("MP Endian: Big Endian\n"); |
324 | 0 | } |
325 | 0 | else { |
326 | 0 | return 3; |
327 | 0 | } |
328 | 0 | m += mp_endian_magic_len; |
329 | | |
330 | | /* Offset to First IFD (4 bytes) */ |
331 | 0 | mpf->first_ifd_offset = read32(m, mpf); |
332 | 0 | DEBUG_PRINT("MP First IFD offset: %u\n", mpf->first_ifd_offset); |
333 | 0 | if (mpf->first_ifd_offset < 8 || mpf->first_ifd_offset >= len - 4) |
334 | 0 | return 4; |
335 | | |
336 | | |
337 | | /* Parse IFDs */ |
338 | 0 | int64_t offset = mpf->first_ifd_offset; |
339 | 0 | if (first_image) { |
340 | 0 | DEBUG_PRINT("Parse Index IFD at %"PRId64"\n", offset); |
341 | | /* Note, offset is from start of "MP HEader", so we must add 4 |
342 | | to convert to offset from beginning of the marker data. */ |
343 | 0 | if ((offset = parse_idf(data, len, offset + 4, mpf, MP_Index_IDF)) < 0) |
344 | 0 | return 5; |
345 | 0 | } |
346 | 0 | if (offset > 0) { |
347 | 0 | DEBUG_PRINT("Parse Attribute IFD at %"PRId64"\n", offset); |
348 | | /* Note, offset is from start of "MP HEader", so we must add 4 |
349 | | to convert to offset from beginning of the marker data. */ |
350 | 0 | if ((offset = parse_idf(data, len, offset + 4, mpf, MP_Attribute_IDF)) < 0) |
351 | 0 | return 6; |
352 | 0 | } |
353 | 0 | if (offset > 0) { |
354 | 0 | DEBUG_PRINT("Ignoring addtional/unknown IFD(s) found\n"); |
355 | 0 | } |
356 | | |
357 | | /* MP Entry list is required for patching the image offsets/sizes */ |
358 | 0 | if (mpf->mpentry_list_offset == 0 || mpf->number_of_images < 2) |
359 | 0 | return 7; |
360 | | |
361 | 0 | return 0; |
362 | 0 | } |
363 | | |
364 | | |
365 | | int valid_mpf_marker(const jpeg_saved_marker_ptr marker, size_t offset, mpf_marker_t *mpf, bool first_image) |
366 | 0 | { |
367 | 0 | if (!marker || !mpf) |
368 | 0 | return -1; |
369 | | |
370 | 0 | return (read_mpf_marker((const char*)marker->data, marker->data_length, mpf, offset, first_image) == 0); |
371 | 0 | } |
372 | | |
373 | | |
374 | | int patch_mpf_marker(unsigned char *image, size_t image_len, mpf_marker_t *mpf, size_t new_offset, size_t in_image_size) |
375 | 0 | { |
376 | 0 | if (!image || image_len <= 0 || !mpf || new_offset <= 0 || in_image_size <= 0) |
377 | 0 | return -1; |
378 | | |
379 | 0 | DEBUG_PRINT("New MPF offset: %lu (new image size: %lu)\n", new_offset, image_len); |
380 | | |
381 | | /* Check that the new MPF marker is actually at 'new_offset' */ |
382 | 0 | if (memcmp(image + new_offset, mpf->data, mpf->data_len)) |
383 | 0 | return -2; |
384 | | |
385 | | |
386 | 0 | DEBUG_PRINT("Checking %u records in MP Entry list\n", mpf->number_of_images); |
387 | 0 | for(int img = 0; img < mpf->number_of_images; img++) { |
388 | 0 | unsigned char *e = image + new_offset + mpf->mpentry_list_offset + (img * 16); |
389 | 0 | uint32_t imgsize = read32(e + 4, mpf); |
390 | 0 | uint32_t imgoffset = read32(e + 8, mpf); |
391 | | #if DEBUG >0 |
392 | | uint32_t attr = read32(e, mpf); |
393 | | uint16_t dep1 = read16(e + 12, mpf); |
394 | | uint16_t dep2 = read16(e + 14, mpf); |
395 | | #endif |
396 | |
|
397 | 0 | DEBUG_PRINT(" MPentry%d: attr=%08x, imgsize=%u, imgoffset=%u, dep1=%u, dep2=%u\n", |
398 | 0 | img, attr, imgsize, imgoffset, dep1, dep2); |
399 | |
|
400 | 0 | if (imgoffset > 0) { |
401 | | /* Update offset of additional image(s) */ |
402 | | |
403 | | /* Calculate relative offset from end of original image */ |
404 | 0 | int64_t rel_o = (int64_t)imgoffset + (mpf->marker_offset + 4) - in_image_size; |
405 | 0 | if (rel_o < 0) { |
406 | 0 | DEBUG_PRINT("Invalid relative offset!\n"); |
407 | 0 | return -3; |
408 | 0 | } |
409 | | /* Calculate new offset (as MPF marker may have moved and image size may have changed) */ |
410 | 0 | int64_t new_o = (int64_t)image_len - (new_offset + 4) + rel_o; |
411 | 0 | if (new_o < 0) { |
412 | 0 | DEBUG_PRINT("Invalid new offset!\n"); |
413 | 0 | return -4; |
414 | 0 | } |
415 | 0 | if (imgoffset != new_o) { |
416 | 0 | DEBUG_PRINT(" update image offset %u -> %"PRId64" (relative offset: %"PRId64")\n\n", |
417 | 0 | imgoffset, new_o, rel_o); |
418 | 0 | write32(e + 8, new_o, mpf); |
419 | 0 | } |
420 | 0 | } else { |
421 | | /* Update size of primary image */ |
422 | 0 | if (imgsize != image_len) { |
423 | 0 | DEBUG_PRINT(" update image size %u -> %lu\n", imgsize, image_len); |
424 | 0 | write32(e + 4, image_len, mpf); |
425 | 0 | } |
426 | 0 | } |
427 | 0 | } |
428 | | |
429 | 0 | return 0; |
430 | 0 | } |
431 | | |
432 | | |
433 | | int find_mpf_marker(const unsigned char *image, size_t image_len, size_t *offset) |
434 | 0 | { |
435 | 0 | if (!image || image_len < 4) |
436 | 0 | return -1; |
437 | | |
438 | | /* Check that image has JPEG signature */ |
439 | 0 | if (image[0] != 0xff || image[1] != 0xd8) |
440 | 0 | return -2; |
441 | 0 | DEBUG_PRINT("JPEG marker found at %p (len=%lu)\n", image, image_len); |
442 | |
|
443 | 0 | int found = 0; |
444 | 0 | size_t pos = 2; |
445 | | |
446 | | /* Walk the JPEG segmet markers and look for APP2 "MPF" segments. */ |
447 | 0 | while (pos + 4 <= image_len && image[pos] == 0xff) { |
448 | 0 | unsigned char marker = image[pos + 1]; |
449 | |
|
450 | 0 | if (marker == 0xff) { |
451 | | /* Skip any padding (fill bytes) */ |
452 | 0 | pos++; |
453 | 0 | continue; |
454 | 0 | } |
455 | 0 | if (marker == 0x01 || (marker >= 0xd0 && marker <= 0xd7)) { |
456 | | /* Skip stand-alone markers (no data payload): |
457 | | TEM (0x01), RST0-RST7 (0xd0-0xd7) */ |
458 | 0 | DEBUG_PRINT("Skip stand-alone marker 0x%02x\n", marker); |
459 | 0 | pos += 2; |
460 | 0 | continue; |
461 | 0 | } |
462 | 0 | if (marker == 0xda || marker == 0xd8 || marker == 0xd9) { |
463 | | /* Stop when we see SOS (0xda), SOI (0xd8) or EOI (0xd9) */ |
464 | 0 | DEBUG_PRINT("Stoping search at marker 0x%02x\n", marker); |
465 | 0 | break; |
466 | 0 | } |
467 | | |
468 | | /* Get marker length */ |
469 | 0 | size_t len = ((size_t)image[pos + 2] << 8) | image[pos + 3]; |
470 | 0 | if (len < 2 || pos + 2 + len > image_len) { |
471 | | /* Stop if length is not valid */ |
472 | 0 | DEBUG_PRINT("Marker with bad length 0x%02x (length=%lu)\n", marker, len); |
473 | 0 | break; |
474 | 0 | } |
475 | 0 | DEBUG_PRINT("Found marker 0x%02x at offset %lu (length=%lu)\n", marker, pos, len); |
476 | |
|
477 | 0 | if (marker == 0xe2 && len >= 6 |
478 | 0 | && !memcmp(image + pos + 4, mp_format_identifier, mp_format_identifier_len)) { |
479 | | /* Found a APP2 marker that has MPF signature */ |
480 | 0 | DEBUG_PRINT("Found MPF marker (data offset=%lu)\n", pos + 4); |
481 | 0 | if (!found && offset) |
482 | 0 | *offset = pos + 4; |
483 | 0 | found++; |
484 | 0 | } |
485 | |
|
486 | 0 | pos += 2 + len; |
487 | 0 | } |
488 | |
|
489 | 0 | DEBUG_PRINT("%d MPF marker(s) found\n", found); |
490 | |
|
491 | 0 | return found; |
492 | 0 | } |
493 | | |
494 | | /* eof :-) */ |