Fri Jun 10 20:57:42 2005 John Ellis <johne@verizon.net>
[geeqie.git] / src / format_raw.c
1 /*
2  *  GQView
3  *  (C) 2005 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
18 #include <stdio.h>
19 #include <string.h>
20 #include <unistd.h>
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <sys/mman.h>
24
25 #include <glib.h>
26
27 #include "intl.h"
28
29 #include "format_raw.h"
30
31 #include "format_canon.h"
32 #include "format_fuji.h"
33 #include "format_nikon.h"
34 #include "format_olympus.h"
35
36
37 /* so that debugging is honored */
38 extern gint debug;
39
40
41 typedef struct _FormatRawEntry FormatRawEntry;
42 struct _FormatRawEntry {
43         const gchar *extension;
44         FormatRawMatchType magic_type;
45         const guint magic_offset;
46         const void *magic_pattern;
47         const guint magic_length;
48         const gchar *description;
49         FormatRawParseFunc func_parse;
50 };
51
52 static FormatRawEntry format_raw_list[] = {
53         FORMAT_RAW_CANON,
54         FORMAT_RAW_FUJI,
55         FORMAT_RAW_NIKON,
56         { NULL, 0, 0, NULL, 0, NULL, NULL }
57 };
58
59
60 typedef struct _FormatExifEntry FormatExifEntry;
61 struct _FormatExifEntry {
62         FormatExifMatchType header_type;
63         const void *header_pattern;
64         const guint header_length;
65         const gchar *description;
66         FormatExifParseFunc func_parse;
67 };
68
69 static FormatExifEntry format_exif_list[] = {
70         FORMAT_EXIF_CANON,
71         FORMAT_EXIF_FUJI,
72         FORMAT_EXIF_NIKON,
73         FORMAT_EXIF_OLYMPUS,
74         { 0, NULL, 0, NULL }
75 };
76
77
78 static guint tiff_table(unsigned char *data, const guint len, guint offset, ExifByteOrder bo,
79                         guint tag, ExifFormatType type,
80                         guint *result_offset, guint *result_count)
81 {
82         guint count;
83         guint i;
84
85         if (len < offset + 2) return 0;
86         if (type < 0 || type > EXIF_FORMAT_COUNT) return 0;
87
88         count = exif_byte_get_int16(data + offset, bo);
89         offset += 2;
90         if (len < offset + count * 12 + 4) return 0;
91
92         for (i = 0; i < count; i++)
93                 {
94                 guint segment;
95
96                 segment = offset + i * 12;
97                 if (exif_byte_get_int16(data + segment, bo) == tag &&
98                     exif_byte_get_int16(data + segment + 2, bo) == type)
99                         {
100                         guint chunk_count;
101                         guint chunk_offset;
102                         guint chunk_length;
103
104                         chunk_count = exif_byte_get_int32(data + segment + 4, bo);
105                         chunk_length = ExifFormatList[type].size * chunk_count;
106
107                         if (chunk_length > 4)
108                                 {
109                                 chunk_offset = exif_byte_get_int32(data + segment + 8, bo);
110                                 }
111                         else
112                                 {
113                                 chunk_offset = segment + 8;
114                                 }
115
116                         if (chunk_offset + chunk_length <= len)
117                                 {
118                                 *result_offset = chunk_offset;
119                                 *result_count = chunk_count;
120                                 }
121
122                         return 0;
123                         }
124                 }
125
126         return exif_byte_get_int32(data + offset + count * 12, bo);
127 }
128
129 static gint format_tiff_find_tag_data(unsigned char *data, const guint len,
130                                       guint tag, ExifFormatType type,
131                                       guint *result_offset, guint *result_count)
132 {
133         ExifByteOrder bo;
134         guint offset;
135
136         if (len < 8) return FALSE;
137
138         if (memcmp(data, "II", 2) == 0)
139                 {
140                 bo = EXIF_BYTE_ORDER_INTEL;
141                 }
142         else if (memcmp(data, "MM", 2) == 0)
143                 {
144                 bo = EXIF_BYTE_ORDER_MOTOROLA;
145                 }
146         else
147                 {
148                 return FALSE;
149                 }
150
151         if (exif_byte_get_int16(data + 2, bo) != 0x002A)
152                 {
153                 return FALSE;
154                 }
155
156         offset = exif_byte_get_int32(data + 4, bo);
157
158         while (offset != 0)
159                 {
160                 guint ro = 0;
161                 guint rc = 0;
162
163                 offset = tiff_table(data, len, offset, bo, tag, type, &ro, &rc);
164                 if (ro != 0)
165                         {
166                         *result_offset = ro;
167                         *result_count = rc;
168                         return TRUE;
169                         }
170                 }
171
172         return FALSE;
173 }
174
175 static FormatRawEntry *format_raw_find(unsigned char *data, const guint len)
176 {
177         gint n;
178         gint tiff;
179         guint make_count = 0;
180         guint make_offset = 0;
181
182         tiff = (len > 8 &&
183                 (memcmp(data, "II\x2a\x00", 4) == 0 ||
184                  memcmp(data, "MM\x00\x2a", 4) == 0));
185
186         n = 0;
187         while (format_raw_list[n].magic_pattern)
188                 {
189                 FormatRawEntry *entry = &format_raw_list[n];
190
191                 switch (entry->magic_type)
192                         {
193                         case FORMAT_RAW_MATCH_MAGIC:
194                                 if (entry->magic_length + entry->magic_offset <= len &&
195                                     memcmp(data + entry->magic_offset,
196                                            entry->magic_pattern, entry->magic_length) == 0)
197                                         {
198                                         return entry;
199                                         }
200                                 break;
201                         case FORMAT_RAW_MATCH_TIFF_MAKE:
202                                 if (tiff &&
203                                     make_offset == 0 &&
204                                     !format_tiff_find_tag_data(data, len, 0x10f, EXIF_FORMAT_STRING,
205                                                                &make_offset, &make_count))
206                                         {
207                                         tiff = FALSE;
208                                         }
209                                 if (make_offset != 0 &&
210                                     make_count >= entry->magic_offset + entry->magic_length &&
211                                     memcmp(entry->magic_pattern,
212                                            data + make_offset + entry->magic_offset, entry->magic_length) == 0)
213                                         {
214                                         return entry;
215                                         }
216                                 break;
217                         default:
218                                 break;
219                         }
220                 n++;
221                 }
222
223         return NULL;
224 }
225
226 static gint format_raw_parse(FormatRawEntry *entry,
227                              unsigned char *data, const guint len,
228                              guint *image_offset, guint *exif_offset)
229 {
230         guint io = 0;
231         guint eo = 0;
232         gint found;
233
234         if (!entry || !entry->func_parse) return FALSE;
235
236         if (debug) printf("RAW using file parser for %s\n", entry->description);
237
238         found = entry->func_parse(data, len, &io, &eo);
239
240         if (!found ||
241             io >= len - 4 ||
242             eo >= len)
243                 {
244                 return FALSE;
245                 }
246
247         if (image_offset) *image_offset = io;
248         if (exif_offset) *exif_offset = eo;
249
250         return TRUE;
251 }
252
253 gint format_raw_img_exif_offsets(unsigned char *data, const guint len,
254                                  guint *image_offset, guint *exif_offset)
255 {
256         FormatRawEntry *entry;
257
258         if (!data || len < 1) return FALSE;
259
260         entry = format_raw_find(data, len);
261
262         if (!entry || !entry->func_parse) return FALSE;
263
264         return format_raw_parse(entry, data, len, image_offset, exif_offset);
265 }
266
267
268 gint format_raw_img_exif_offsets_fd(int fd, const gchar *path,
269                                     unsigned char *header_data, const guint header_len,
270                                     guint *image_offset, guint *exif_offset)
271 {
272         FormatRawEntry *entry;
273         void *map_data = NULL;
274         size_t map_len = 0;
275         struct stat st;
276         gint success;
277
278         if (!header_data || fd < 0) return FALSE;
279
280         /* given image pathname, first do simple (and fast) file extension test */
281         if (path)
282                 {
283                 const gchar *ext;
284                 gint match = FALSE;
285                 gint i;
286
287                 ext = strrchr(path, '.');
288                 if (!ext) return FALSE;
289                 ext++;
290
291                 i = 0;
292                 while (!match && format_raw_list[i].magic_pattern)
293                         {
294                         if (format_raw_list[i].extension &&
295                             strcasecmp(format_raw_list[i].extension, ext) == 0)
296                                 {
297                                 match = TRUE;
298                                 }
299                         i++;
300                         }
301
302                 if (!match) return FALSE;
303
304                 if (debug) printf("RAW file parser extension match\n");
305                 }
306
307         entry = format_raw_find(header_data, header_len);
308
309         if (!entry || !entry->func_parse) return FALSE;
310
311         if (fstat(fd, &st) == -1)
312                 {
313                 printf("Failed to stat file %d\n", fd);
314                 return FALSE;
315                 }
316         map_len = st.st_size;
317         map_data = mmap(0, map_len, PROT_READ, MAP_PRIVATE, fd, 0);
318         if (map_data == MAP_FAILED)
319                 {
320                 printf("Failed to mmap file %d\n", fd);
321                 return FALSE;
322                 }
323
324         success = format_raw_parse(entry, map_data, map_len, image_offset, exif_offset);
325
326         if (munmap(map_data, map_len) == -1)
327                 {
328                 printf("Failed to unmap file %d\n", fd);
329                 }
330
331         if (success && image_offset)
332                 {
333                 if (lseek(fd, *image_offset, SEEK_SET) != *image_offset)
334                         {
335                         printf("Failed to seek to embedded image\n");
336
337                         *image_offset = 0;
338                         if (*exif_offset) *exif_offset = 0;
339                         success = FALSE;
340                         }
341                 }
342
343         return success;
344 }
345
346
347 static FormatExifEntry *format_exif_makernote_find(ExifData *exif, unsigned char *tiff,
348                                                    guint offset, guint size)
349 {
350         ExifItem *make;
351         gint n;
352
353         make = exif_get_item(exif, "Make");
354
355         n = 0;
356         while (format_exif_list[n].header_pattern)
357                 {
358                 switch (format_exif_list[n].header_type)
359                         {
360                         case FORMAT_EXIF_MATCH_MAKERNOTE:
361                                 if (format_exif_list[n].header_length + offset < size &&
362                                     memcmp(tiff + offset, format_exif_list[n].header_pattern,
363                                                           format_exif_list[n].header_length) == 0)
364                                         {
365                                         return &format_exif_list[n];
366                                         }
367                                 break;
368                         case FORMAT_EXIF_MATCH_MAKE:
369                                 if (make &&
370                                     make->data_len >= format_exif_list[n].header_length &&
371                                     memcmp(make->data, format_exif_list[n].header_pattern,
372                                                        format_exif_list[n].header_length) == 0)
373                                         {
374                                         return &format_exif_list[n];
375                                         }
376                                 break;
377                         }
378                 n++;
379                 }
380
381         return FALSE;
382 }
383
384 gint format_exif_makernote_parse(ExifData *exif, unsigned char *tiff, guint offset,
385                                  guint size, ExifByteOrder bo)
386 {
387         FormatExifEntry *entry;
388
389         entry = format_exif_makernote_find(exif, tiff, offset, size);
390
391         if (!entry || !entry->func_parse) return FALSE;
392
393         if (debug) printf("EXIF using makernote parser for %s\n", entry->description);
394
395         return entry->func_parse(exif, tiff, offset, size, bo);
396 }
397
398