6 * Original version 2005 Lars Ellenberg, base on dcraw by David coffin.
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!
21 #include <sys/types.h>
29 #include "format_raw.h"
31 #include "format_canon.h"
32 #include "format_fuji.h"
33 #include "format_nikon.h"
36 /* so that debugging is honored */
40 typedef struct _FormatRawEntry FormatRawEntry;
41 struct _FormatRawEntry {
42 const void *header_pattern;
43 const guint header_length;
44 const gchar *description;
45 FormatRawParseFunc func_parse;
48 static FormatRawEntry format_raw_list[] = {
52 { NULL, 0, NULL, NULL }
56 typedef struct _FormatExifEntry FormatExifEntry;
57 struct _FormatExifEntry {
58 FormatExifMatchType header_type;
59 const void *header_pattern;
60 const guint header_length;
61 const gchar *description;
62 FormatExifParseFunc func_parse;
65 static FormatExifEntry format_exif_list[] = {
73 static FormatRawEntry *format_raw_find(const void *data, const guint len)
78 while (format_raw_list[n].header_pattern)
80 if (format_raw_list[n].header_length <= len &&
81 memcmp(data, format_raw_list[n].header_pattern, format_raw_list[n].header_length) == 0)
83 return &format_raw_list[n];
91 static gint format_raw_parse(FormatRawEntry *entry,
92 const void *data, const guint len,
93 guint *image_offset, guint *exif_offset)
99 if (!entry || !entry->func_parse) return FALSE;
101 if (debug) printf("RAW using file parser for %s\n", entry->description);
103 found = entry->func_parse(data, len, &io, &eo);
112 if (image_offset) *image_offset = io;
113 if (exif_offset) *exif_offset = eo;
118 gint format_raw_img_exif_offsets(const void *data, const guint len,
119 guint *image_offset, guint *exif_offset)
121 FormatRawEntry *entry;
123 if (!data || len < 1) return FALSE;
125 entry = format_raw_find(data, len);
127 if (!entry || !entry->func_parse) return FALSE;
129 return format_raw_parse(entry, data, len, image_offset, exif_offset);
133 gint format_raw_img_exif_offsets_fd(int fd, const void *header_data, const guint header_len,
134 guint *image_offset, guint *exif_offset)
136 FormatRawEntry *entry;
137 void *map_data = NULL;
142 if (!header_data || fd < 0) return FALSE;
144 entry = format_raw_find(header_data, header_len);
146 if (!entry || !entry->func_parse) return FALSE;
148 if (fstat(fd, &st) == -1)
150 printf("Failed to stat file %d\n", fd);
153 map_len = st.st_size;
154 map_data = mmap(0, map_len, PROT_READ, MAP_PRIVATE, fd, 0);
155 if (map_data == MAP_FAILED)
157 printf("Failed to mmap file %d\n", fd);
161 success = format_raw_parse(entry, map_data, map_len, image_offset, exif_offset);
163 if (munmap(map_data, map_len) == -1)
165 printf("Failed to unmap file %d\n", fd);
168 if (success && image_offset)
170 if (lseek(fd, *image_offset, SEEK_SET) != *image_offset)
172 printf("Failed to seek to embedded image\n");
175 if (*exif_offset) *exif_offset = 0;
184 static FormatExifEntry *format_exif_makernote_find(ExifData *exif, unsigned char *tiff,
185 guint offset, guint size)
190 make = exif_get_item(exif, "Make");
193 while (format_exif_list[n].header_pattern)
195 switch (format_exif_list[n].header_type)
197 case FORMAT_EXIF_MATCH_MAKERNOTE:
198 if (format_exif_list[n].header_length + offset < size &&
199 memcmp(tiff + offset, format_exif_list[n].header_pattern,
200 format_exif_list[n].header_length) == 0)
202 return &format_exif_list[n];
205 case FORMAT_EXIF_MATCH_MAKE:
207 make->data_len >= format_exif_list[n].header_length &&
208 memcmp(make->data, format_exif_list[n].header_pattern,
209 format_exif_list[n].header_length) == 0)
211 return &format_exif_list[n];
221 gint format_exif_makernote_parse(ExifData *exif, unsigned char *tiff, guint offset,
222 guint size, ExifByteOrder byte_order)
224 FormatExifEntry *entry;
226 entry = format_exif_makernote_find(exif, tiff, offset, size);
228 if (!entry || !entry->func_parse) return FALSE;
230 if (debug) printf("EXIF using makernote parser for %s\n", entry->description);
232 return entry->func_parse(exif, tiff, offset, size, byte_order);