replaced gchar* path with FileData *fd
[geeqie.git] / src / format_fuji.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
22 #include <glib.h>
23
24 #include "intl.h"
25
26 #include "gqview.h"
27 #include "format_fuji.h"
28 #include "format_raw.h"
29
30 #include "exif.h"
31
32
33 /*
34  *-----------------------------------------------------------------------------
35  * Raw (RAF) embedded jpeg extraction for Fujifilm
36  *-----------------------------------------------------------------------------
37  */
38
39
40 gint format_fuji_raw(unsigned char *data, const guint len,
41                      guint *image_offset, guint *exif_offset)
42 {
43         guint io;
44         guint eo;
45
46         if (len < 128 ||
47             memcmp(data, "FUJIFILM", 8) != 0)
48                 {
49                 return FALSE;
50                 }
51
52         /* offset to jpeg is embedded at bytes 84-87 */
53         io = exif_byte_get_int32(data + 84, EXIF_BYTE_ORDER_MOTOROLA);
54         if (io + 4 > len) return FALSE;
55
56         /* verify jpeg marker */
57         if (memcmp(data + io, "\xff\xd8\xff\xe1", 4) != 0)
58                 {
59                 return FALSE;
60                 }
61
62         /* Exif is stored in the jpeg, so use the same offset */
63         eo=io;
64
65         if (image_offset) *image_offset = io;
66         if (exif_offset) *exif_offset = eo;
67
68         return TRUE;
69 }
70
71
72 /*
73  *-----------------------------------------------------------------------------
74  * EXIF Makernote for Fujifilm
75  *-----------------------------------------------------------------------------
76  */
77
78 static ExifTextList FujiTagSharpness[] = {
79         { 1,    "soft" },
80         { 2,    "soft" },
81         { 3,    "normal" },
82         { 4,    "hard" },
83         { 5,    "hard" },
84         EXIF_TEXT_LIST_END
85 };
86
87 static ExifTextList FujiTagWhiteBalance[]= {
88         { 0,    "auto" },
89         { 256,  "daylight" },
90         { 512,  "cloudy" },
91         { 768,  "daylight color-fluorescence" },
92         { 769,  "daywhite color-fluorescence" },
93         { 770,  "white-fluorescence" },
94         { 1024, "incandescent" },
95         { 3840, "custom" },
96         EXIF_TEXT_LIST_END
97 };
98
99 static ExifTextList FujiTagColorTone[]= {
100         { 0,    "normal" },
101         { 256,  "high" },
102         { 512,  "low" },
103         EXIF_TEXT_LIST_END
104 };
105
106 static ExifTextList FujiTagFlashMode[]= {
107         { 0,    "auto" },
108         { 1,    "on" },
109         { 2,    "off" },
110         { 3,    "red-eye reduction" },
111         EXIF_TEXT_LIST_END
112 };
113
114 static ExifTextList FujiTagOffOn[]= {
115         { 0,    "off" },
116         { 1,    "on" },
117         EXIF_TEXT_LIST_END
118 };
119
120 static ExifTextList FujiTagFocusMode[]= {
121         { 0,    "auto" },
122         { 1,    "manual" },
123         EXIF_TEXT_LIST_END
124 };
125
126 static ExifTextList FujiTagPictureMode[]= {
127         { 0,    "auto" },
128         { 1,    "portrait" },
129         { 2,    "landscape" },
130         { 4,    "sports" },
131         { 5,    "night" },
132         { 6,    "program AE" },
133         { 256,  "aperture priority AE" },
134         { 512,  "shutter priority AE" },
135         { 768,  "manual" },
136         EXIF_TEXT_LIST_END
137 };
138
139 static ExifTextList FujiTagNoYes[]= {
140         { 0,    "no" },
141         { 1,    "yes" },
142         EXIF_TEXT_LIST_END
143 };
144
145 #if 0
146 static ExifTextList FujiTag[]= {
147         { ,     "" },
148         { ,     "" },
149         EXIF_TEXT_LIST_END
150 };
151 #endif
152
153
154 static ExifMarker FujiExifMarkersList[] = {
155 { 0x1000,       EXIF_FORMAT_STRING, 8,          "Fuji.Quality",         "Quality",      NULL },
156 { 0x1001,       EXIF_FORMAT_SHORT_UNSIGNED, 1,  "Fuji.Sharpness",       "Sharpness",    FujiTagSharpness },
157 { 0x1002,       EXIF_FORMAT_SHORT_UNSIGNED, 1,  "Fuji.WhiteBalance",    "White balance",FujiTagWhiteBalance },
158 { 0x1003,       EXIF_FORMAT_SHORT_UNSIGNED, 1,  "Fuji.Color",           "Color",        FujiTagColorTone },
159 { 0x1004,       EXIF_FORMAT_SHORT_UNSIGNED, 1,  "Fuji.Tone",            "Tone",         FujiTagColorTone },
160 { 0x1010,       EXIF_FORMAT_SHORT_UNSIGNED, 1,  "Fuji.FlashMode",       "Flash mode",   FujiTagFlashMode },
161 { 0x1011,       EXIF_FORMAT_RATIONAL, 1,        "Fuji.FlashStrength",   "Flash strength", NULL },
162 { 0x1020,       EXIF_FORMAT_SHORT_UNSIGNED, 1,  "Fuji.Macro",           "Macro",        FujiTagOffOn },
163 { 0x1021,       EXIF_FORMAT_SHORT_UNSIGNED, 1,  "Fuji.FocusMode",       "Focus mode",   FujiTagFocusMode },
164 { 0x1030,       EXIF_FORMAT_SHORT_UNSIGNED, 1,  "Fuji.SlowSync",        "Slow synchro", FujiTagOffOn },
165 { 0x1031,       EXIF_FORMAT_SHORT_UNSIGNED, 1,  "Fuji.PictureMode",     "Picture mode", FujiTagPictureMode },
166 { 0x1100,       EXIF_FORMAT_SHORT_UNSIGNED, 1,  "Fuji.ContTake/Bracket",
167                                                         "Continuous / Auto bracket",    FujiTagOffOn },
168 { 0x1300,       EXIF_FORMAT_SHORT_UNSIGNED, 1,  "Fuji.BlurWarning",     "Blue warning", FujiTagNoYes },
169 { 0x1301,       EXIF_FORMAT_SHORT_UNSIGNED, 1,  "Fuji.FocusWarning",    "Focus warning",FujiTagNoYes },
170 { 0x1302,       EXIF_FORMAT_SHORT_UNSIGNED, 1,  "Fuji.AEWarning",       "AE warning",   FujiTagNoYes },
171 EXIF_MARKER_LIST_END
172 };
173
174
175
176 gint format_fuji_makernote(ExifData *exif, unsigned char *tiff, guint offset,
177                            guint size, ExifByteOrder bo)
178 {
179         unsigned char *data;
180         guint ifdstart;
181
182         if (offset + 8 + 4 >= size) return FALSE;
183
184         data = tiff + offset;
185
186         /* Fuji tag format starts with "FUJIFILM",
187          * followed by 4 bytes indicating offset to IFD directory using Fuji tags,
188          * byte order is always little endian (II).
189          */
190         if (memcmp(data, "FUJIFILM", 8) != 0) return FALSE;
191
192         ifdstart = exif_byte_get_int32(data + 8, EXIF_BYTE_ORDER_INTEL);
193         if (offset + ifdstart >= size) return FALSE;
194
195         if (exif_parse_IFD_table(exif, tiff + offset, ifdstart, size - offset,
196                                  EXIF_BYTE_ORDER_INTEL, 0, FujiExifMarkersList) != 0)
197                 {
198                 return FALSE;
199                 }
200
201         return TRUE;
202 }
203