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