gqview.h -> main.h
[geeqie.git] / src / format_raw.c
1 /*
2  *  GQView
3  *  (C) 2006 John Ellis
4  *
5  *  Authors:
6  *    Original version 2005 Lars Ellenberg, base on dcraw by David coffin.
7  *
8  * This software is released under the GNU General Public License (GNU GPL).
9  * Please read the included file COPYING for more information.
10  * This software comes with no warranty of any kind, use at your own risk!
11  */
12
13 #ifdef HAVE_CONFIG_H
14 #  include "config.h"
15 #endif
16
17 #ifndef HAVE_EXIV2
18
19 #include <stdio.h>
20 #include <string.h>
21 #include <unistd.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <sys/mman.h>
25
26 #include <glib.h>
27
28 #include "intl.h"
29
30 #include "main.h"
31 #include "format_raw.h"
32
33 #include "format_canon.h"
34 #include "format_fuji.h"
35 #include "format_nikon.h"
36 #include "format_olympus.h"
37
38 #ifdef DEBUG
39 /* so that debugging is honored */
40 extern gint debug;
41 #endif
42
43 typedef struct _FormatRawEntry FormatRawEntry;
44 struct _FormatRawEntry {
45         const gchar *extension;
46         FormatRawMatchType magic_type;
47         const guint magic_offset;
48         const void *magic_pattern;
49         const guint magic_length;
50         const FormatRawExifType exif_type;
51         FormatRawExifParseFunc exif_func;
52         const gchar *description;
53         FormatRawParseFunc func_parse;
54 };
55
56 static FormatRawEntry format_raw_list[] = {
57 #if DEBUG_RAW_TIFF
58         FORMAT_RAW_DEBUG_TIFF,
59 #endif
60         FORMAT_RAW_CANON,
61         FORMAT_RAW_FUJI,
62         FORMAT_RAW_NIKON,
63         FORMAT_RAW_OLYMPUS,
64         FORMAT_RAW_PENTAX,
65         FORMAT_RAW_SAMSUNG,
66         { NULL, 0, 0, NULL, 0, 0, NULL, NULL, NULL }
67 };
68
69
70 typedef struct _FormatExifEntry FormatExifEntry;
71 struct _FormatExifEntry {
72         FormatExifMatchType header_type;
73         const void *header_pattern;
74         const guint header_length;
75         const gchar *description;
76         FormatExifParseFunc func_parse;
77 };
78
79 static FormatExifEntry format_exif_list[] = {
80         FORMAT_EXIF_CANON,
81         FORMAT_EXIF_FUJI,
82         FORMAT_EXIF_NIKON,
83         FORMAT_EXIF_OLYMPUS,
84         { 0, NULL, 0, NULL }
85 };
86
87
88 static guint tiff_table(unsigned char *data, const guint len, guint offset, ExifByteOrder bo,
89                         guint tag, ExifFormatType type,
90                         guint *result_offset, guint *result_count)
91 {
92         guint count;
93         guint i;
94
95         if (len < offset + 2) return 0;
96         if (type < 0 || type > EXIF_FORMAT_COUNT) return 0;
97
98         count = exif_byte_get_int16(data + offset, bo);
99         offset += 2;
100         if (len < offset + count * 12 + 4) return 0;
101
102         for (i = 0; i < count; i++)
103                 {
104                 guint segment;
105
106                 segment = offset + i * 12;
107                 if (exif_byte_get_int16(data + segment, bo) == tag &&
108                     exif_byte_get_int16(data + segment + 2, bo) == type)
109                         {
110                         guint chunk_count;
111                         guint chunk_offset;
112                         guint chunk_length;
113
114                         chunk_count = exif_byte_get_int32(data + segment + 4, bo);
115                         chunk_length = ExifFormatList[type].size * chunk_count;
116
117                         if (chunk_length > 4)
118                                 {
119                                 chunk_offset = exif_byte_get_int32(data + segment + 8, bo);
120                                 }
121                         else
122                                 {
123                                 chunk_offset = segment + 8;
124                                 }
125
126                         if (chunk_offset + chunk_length <= len)
127                                 {
128                                 *result_offset = chunk_offset;
129                                 *result_count = chunk_count;
130                                 }
131
132                         return 0;
133                         }
134                 }
135
136         return exif_byte_get_int32(data + offset + count * 12, bo);
137 }
138
139 static gint format_tiff_find_tag_data(unsigned char *data, const guint len,
140                                       guint tag, ExifFormatType type,
141                                       guint *result_offset, guint *result_count)
142 {
143         ExifByteOrder bo;
144         guint offset;
145
146         if (len < 8) return FALSE;
147
148         if (memcmp(data, "II", 2) == 0)
149                 {
150                 bo = EXIF_BYTE_ORDER_INTEL;
151                 }
152         else if (memcmp(data, "MM", 2) == 0)
153                 {
154                 bo = EXIF_BYTE_ORDER_MOTOROLA;
155                 }
156         else
157                 {
158                 return FALSE;
159                 }
160
161         if (exif_byte_get_int16(data + 2, bo) != 0x002A)
162                 {
163                 return FALSE;
164                 }
165
166         offset = exif_byte_get_int32(data + 4, bo);
167
168         while (offset != 0)
169                 {
170                 guint ro = 0;
171                 guint rc = 0;
172
173                 offset = tiff_table(data, len, offset, bo, tag, type, &ro, &rc);
174                 if (ro != 0)
175                         {
176                         *result_offset = ro;
177                         *result_count = rc;
178                         return TRUE;
179                         }
180                 }
181
182         return FALSE;
183 }
184
185 static FormatRawEntry *format_raw_find(unsigned char *data, const guint len)
186 {
187         gint n;
188         gint tiff;
189         guint make_count = 0;
190         guint make_offset = 0;
191
192         tiff = (len > 8 &&
193                 (memcmp(data, "II\x2a\x00", 4) == 0 ||
194                  memcmp(data, "MM\x00\x2a", 4) == 0));
195
196         n = 0;
197         while (format_raw_list[n].magic_pattern)
198                 {
199                 FormatRawEntry *entry = &format_raw_list[n];
200
201                 switch (entry->magic_type)
202                         {
203                         case FORMAT_RAW_MATCH_MAGIC:
204                                 if (entry->magic_length + entry->magic_offset <= len &&
205                                     memcmp(data + entry->magic_offset,
206                                            entry->magic_pattern, entry->magic_length) == 0)
207                                         {
208                                         return entry;
209                                         }
210                                 break;
211                         case FORMAT_RAW_MATCH_TIFF_MAKE:
212                                 if (tiff &&
213                                     make_offset == 0 &&
214                                     !format_tiff_find_tag_data(data, len, 0x10f, EXIF_FORMAT_STRING,
215                                                                &make_offset, &make_count))
216                                         {
217                                         tiff = FALSE;
218                                         }
219                                 if (make_offset != 0 &&
220                                     make_count >= entry->magic_offset + entry->magic_length &&
221                                     memcmp(entry->magic_pattern,
222                                            data + make_offset + entry->magic_offset, entry->magic_length) == 0)
223                                         {
224                                         return entry;
225                                         }
226                                 break;
227                         default:
228                                 break;
229                         }
230                 n++;
231                 }
232
233         return NULL;
234 }
235
236 static gint format_raw_parse(FormatRawEntry *entry,
237                              unsigned char *data, const guint len,
238                              guint *image_offset, guint *exif_offset)
239 {
240         guint io = 0;
241         guint eo = 0;
242         gint found;
243
244         if (!entry || !entry->func_parse) return FALSE;
245
246         if (debug) printf("RAW using file parser for %s\n", entry->description);
247
248         found = entry->func_parse(data, len, &io, &eo);
249
250         if (!found ||
251             io >= len - 4 ||
252             eo >= len)
253                 {
254                 return FALSE;
255                 }
256
257         if (image_offset) *image_offset = io;
258         if (exif_offset) *exif_offset = eo;
259
260         return TRUE;
261 }
262
263 gint format_raw_img_exif_offsets(unsigned char *data, const guint len,
264                                  guint *image_offset, guint *exif_offset)
265 {
266         FormatRawEntry *entry;
267
268         if (!data || len < 1) return FALSE;
269
270         entry = format_raw_find(data, len);
271
272         if (!entry || !entry->func_parse) return FALSE;
273
274         return format_raw_parse(entry, data, len, image_offset, exif_offset);
275 }
276
277
278 FormatRawExifType format_raw_exif_offset(unsigned char *data, const guint len, guint *exif_offset,
279                                          FormatRawExifParseFunc *exif_parse_func)
280 {
281         FormatRawEntry *entry;
282
283         if (!data || len < 1) return FALSE;
284
285         entry = format_raw_find(data, len);
286
287         if (!entry || !entry->func_parse) return FALSE;
288
289         if (!format_raw_parse(entry, data, len, NULL, exif_offset)) return FORMAT_RAW_EXIF_NONE;
290
291         if (entry->exif_type == FORMAT_RAW_EXIF_PROPRIETARY && exif_parse_func)
292                 {
293                 *exif_parse_func = entry->exif_func;
294                 }
295
296         return entry->exif_type;
297 }
298
299
300 gint format_raw_img_exif_offsets_fd(int fd, const gchar *path,
301                                     unsigned char *header_data, const guint header_len,
302                                     guint *image_offset, guint *exif_offset)
303 {
304         FormatRawEntry *entry;
305         void *map_data = NULL;
306         size_t map_len = 0;
307         struct stat st;
308         gint success;
309
310         if (!header_data || fd < 0) return FALSE;
311
312         /* given image pathname, first do simple (and fast) file extension test */
313         if (path)
314                 {
315                 const gchar *ext;
316                 gint match = FALSE;
317                 gint i;
318
319                 ext = strrchr(path, '.');
320                 if (!ext) return FALSE;
321                 ext++;
322
323                 i = 0;
324                 while (!match && format_raw_list[i].magic_pattern)
325                         {
326                         if (format_raw_list[i].extension &&
327                             strcasecmp(format_raw_list[i].extension, ext) == 0)
328                                 {
329                                 match = TRUE;
330                                 }
331                         i++;
332                         }
333
334                 if (!match) return FALSE;
335
336                 if (debug) printf("RAW file parser extension match\n");
337                 }
338
339         /* FIXME:
340          * when the target is a tiff file it should be mmaped prior to format_raw_find as
341          * the make field data may not always be within header_data + header_len
342          */ 
343         entry = format_raw_find(header_data, header_len);
344
345         if (!entry || !entry->func_parse) return FALSE;
346
347         if (fstat(fd, &st) == -1)
348                 {
349                 printf("Failed to stat file %d\n", fd);
350                 return FALSE;
351                 }
352         map_len = st.st_size;
353         map_data = mmap(0, map_len, PROT_READ, MAP_PRIVATE, fd, 0);
354         if (map_data == MAP_FAILED)
355                 {
356                 printf("Failed to mmap file %d\n", fd);
357                 return FALSE;
358                 }
359
360         success = format_raw_parse(entry, map_data, map_len, image_offset, exif_offset);
361
362         if (munmap(map_data, map_len) == -1)
363                 {
364                 printf("Failed to unmap file %d\n", fd);
365                 }
366
367         if (success && image_offset)
368                 {
369                 if (lseek(fd, *image_offset, SEEK_SET) != *image_offset)
370                         {
371                         printf("Failed to seek to embedded image\n");
372
373                         *image_offset = 0;
374                         if (*exif_offset) *exif_offset = 0;
375                         success = FALSE;
376                         }
377                 }
378
379         return success;
380 }
381
382
383 static FormatExifEntry *format_exif_makernote_find(ExifData *exif, unsigned char *tiff,
384                                                    guint offset, guint size)
385 {
386         ExifItem *make;
387         gint n;
388
389         make = exif_get_item(exif, "Exif.Image.Make");
390
391         n = 0;
392         while (format_exif_list[n].header_pattern)
393                 {
394                 switch (format_exif_list[n].header_type)
395                         {
396                         case FORMAT_EXIF_MATCH_MAKERNOTE:
397                                 if (format_exif_list[n].header_length + offset < size &&
398                                     memcmp(tiff + offset, format_exif_list[n].header_pattern,
399                                                           format_exif_list[n].header_length) == 0)
400                                         {
401                                         return &format_exif_list[n];
402                                         }
403                                 break;
404                         case FORMAT_EXIF_MATCH_MAKE:
405                                 if (make &&
406                                     make->data_len >= format_exif_list[n].header_length &&
407                                     memcmp(make->data, format_exif_list[n].header_pattern,
408                                                        format_exif_list[n].header_length) == 0)
409                                         {
410                                         return &format_exif_list[n];
411                                         }
412                                 break;
413                         }
414                 n++;
415                 }
416
417         return FALSE;
418 }
419
420 gint format_exif_makernote_parse(ExifData *exif, unsigned char *tiff, guint offset,
421                                  guint size, ExifByteOrder bo)
422 {
423         FormatExifEntry *entry;
424
425         entry = format_exif_makernote_find(exif, tiff, offset, size);
426
427         if (!entry || !entry->func_parse) return FALSE;
428
429         if (debug) printf("EXIF using makernote parser for %s\n", entry->description);
430
431         return entry->func_parse(exif, tiff, offset, size, bo);
432 }
433
434 /*
435  *-----------------------------------------------------------------------------
436  * Basic TIFF debugger, prints all IFD entries within tiff file
437  *-----------------------------------------------------------------------------
438  */
439 #if DEBUG_RAW_TIFF
440
441 static guint format_debug_tiff_table(unsigned char *data, const guint len, guint offset,
442                                      ExifByteOrder bo, gint level);
443
444 static void format_debug_tiff_entry(unsigned char *data, const guint len, guint offset,
445                                     ExifByteOrder bo, gint level)
446 {
447         guint tag;
448         guint type;
449         guint count;
450         guint segment;
451         guint seg_len;
452
453         tag = exif_byte_get_int16(data + offset + EXIF_TIFD_OFFSET_TAG, bo);
454         type = exif_byte_get_int16(data + offset + EXIF_TIFD_OFFSET_FORMAT, bo);
455         count = exif_byte_get_int32(data + offset + EXIF_TIFD_OFFSET_COUNT, bo);
456
457         seg_len = ExifFormatList[type].size * count;
458         if (seg_len > 4)
459                 {
460                 segment = exif_byte_get_int32(data + offset + EXIF_TIFD_OFFSET_DATA, bo);
461                 if (segment + seg_len > len) return;
462                 }
463         else
464                 {
465                 segment = offset + EXIF_TIFD_OFFSET_DATA;
466                 }
467
468         printf("%*stag:0x%04X (%05d), type:%2d %9s, len:%6d [%02X %02X %02X %02X] @ offset:%d\n",
469                 level, "", tag, tag, type,
470                 (type < EXIF_FORMAT_COUNT) ? ExifFormatList[type].short_name : "???", count,
471                 data[segment], data[segment + 1], data[segment + 2], data[segment + 3], segment);
472
473         if (tag == 0x8769 || tag == 0x14a)
474                 {
475                 gint i;
476
477                 printf("%*s~~~ found %s table\n", level, "", (tag == 0x14a) ? "subIFD" : "EXIF" );
478
479                 for (i = 0; i < count; i++)
480                         {
481                         guint subset;
482
483                         subset = exif_byte_get_int32(data + segment + i * 4, bo);
484                         format_debug_tiff_table(data, len, subset, bo, level + 1);
485                         }
486                 }
487         else if (tag == 0x8773 && type == EXIF_FORMAT_UNDEFINED)
488                 {
489                 printf("%*s~~~ found ICC color profile at offset %d, length %d\n", level, "", segment, seg_len);
490                 }
491         else if (tag == 0x201 && (type == EXIF_FORMAT_LONG_UNSIGNED || type == EXIF_FORMAT_LONG))
492                 {
493                 guint subset = exif_byte_get_int32(data + segment, bo);
494                 printf("%*s~~~ found jpeg data at offset %d\n", level, "", subset);
495                 }
496         else if (tag == 0x202 && (type == EXIF_FORMAT_LONG_UNSIGNED || type == EXIF_FORMAT_LONG))
497                 {
498                 guint subset = exif_byte_get_int32(data + segment, bo);
499                 printf("%*s~~~ found jpeg data length of %d\n", level, "", subset);
500                 }
501 }
502
503 static guint format_debug_tiff_table(unsigned char *data, const guint len, guint offset,
504                                      ExifByteOrder bo, gint level)
505 {
506         guint count;
507         guint i;
508
509         if (level > EXIF_TIFF_MAX_LEVELS) return 0;
510
511         if (len < offset + 2) return FALSE;
512
513         count = exif_byte_get_int16(data + offset, bo);
514         offset += 2;
515         if (len < offset + count * EXIF_TIFD_SIZE + 4) return 0;
516
517         printf("%*s== tiff table #%d has %d entries ==\n", level, "", level, count);
518
519         for (i = 0; i < count; i++)
520                 {
521                 format_debug_tiff_entry(data, len, offset + i * EXIF_TIFD_SIZE, bo, level);
522                 }
523
524         printf("%*s----------- end of #%d ------------\n", level, "", level);
525
526         return exif_byte_get_int32(data + offset + count * EXIF_TIFD_SIZE, bo);
527 }
528
529 gint format_debug_tiff_raw(unsigned char *data, const guint len,
530                            guint *image_offset, guint *exif_offset)
531 {
532         ExifByteOrder bo;
533         gint level;
534         guint offset;
535
536         if (len < 8) return FALSE;
537
538         /* for debugging, we are more relaxed as to magic header */
539         if (memcmp(data, "II", 2) == 0)
540                 {
541                 bo = EXIF_BYTE_ORDER_INTEL;
542                 }
543         else if (memcmp(data, "MM", 2) == 0)
544                 {
545                 bo = EXIF_BYTE_ORDER_MOTOROLA;
546                 }
547         else
548                 {
549                 return FALSE;
550                 }
551
552         printf("*** debug parsing tiff\n");
553
554         offset = exif_byte_get_int32(data + 4, bo);
555         level = 0;
556         while (offset && level < EXIF_TIFF_MAX_LEVELS)
557                 {
558                 offset = format_debug_tiff_table(data, len, offset, bo, 0);
559                 level++;
560                 }
561
562         printf("*** end\n");
563
564         /* we are debugging, not trying to return any data */
565         return FALSE;
566 }
567 #endif
568
569 #endif 
570 /* not HAVE_EXIV2 */
571