1/* mnote-canon-entry.c
2 *
3 * Copyright (c) 2002 Lutz Mueller <lutz@users.sourceforge.net>
4 * Copyright (c) 2003 Matthieu Castet <mat-c@users.sourceforge.net>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA  02110-1301  USA.
20 */
21
22#include "config.h"
23#include "mnote-canon-entry.h"
24
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <math.h>
29
30#include <libexif/exif-format.h>
31#include <libexif/exif-utils.h>
32#include <libexif/i18n.h>
33
34/* #define DEBUG */
35
36#define CF(format,target,v,maxlen)                              \
37{                                                               \
38        if (format != target) {                                 \
39                snprintf (v, maxlen,                            \
40                        _("Invalid format '%s', "               \
41                        "expected '%s'."),                      \
42                        exif_format_get_name (format),          \
43                        exif_format_get_name (target));         \
44                break;                                          \
45        }                                                       \
46}
47
48#define CC(number,target,v,maxlen)                                      \
49{                                                                       \
50        if (number != target) {                                         \
51                snprintf (v, maxlen,                                    \
52                        _("Invalid number of components (%i, "          \
53                        "expected %i)."), (int) number, (int) target);  \
54                break;                                                  \
55        }                                                               \
56}
57#define CC2(number,t1,t2,v,maxlen)                                      \
58{                                                                       \
59	if ((number != t1) && (number != t2)) {                         \
60		snprintf (v, maxlen,                                    \
61			_("Invalid number of components (%i, "          \
62			"expected %i or %i)."), (int) number,		\
63			(int) t1, (int) t2);  				\
64		break;                                                  \
65	}                                                               \
66}
67
68#define UNDEFINED 0xFF
69
70static const struct canon_entry_table_t {
71  unsigned int subtag;
72  ExifShort value;
73  const char *name;
74} entries_settings_1 [] = {
75#ifndef NO_VERBOSE_TAG_DATA
76  { 0,  1, N_("Macro")},
77  { 0,  2, N_("Normal")},
78  { 2,  1, N_("Economy")},
79  { 2,  2, N_("Normal")},
80  { 2,  3, N_("Fine")},
81  { 2,  4, N_("RAW")},
82  { 2,  5, N_("Superfine")},
83  { 3,  0, N_("Off")},
84  { 3,  1, N_("Auto")},
85  { 3,  2, N_("On")},
86  { 3,  3, N_("Red-eye reduction")},
87  { 3,  4, N_("Slow synchro")},
88  { 3,  5, N_("Auto + Red-eye reduction")},
89  { 3,  6, N_("On + Red-eye reduction")},
90  { 3, 16, N_("External flash")},
91  { 4,  0, N_("Single")},
92  { 4,  1, N_("Continuous")},
93  { 4,  2, N_("Movie")},
94  { 4,  3, N_("Continuous, speed priority")},
95  { 4,  4, N_("Continuous, low")},
96  { 4,  5, N_("Continuous, high")},
97  { 6,  0, N_("One-shot AF")},
98  { 6,  1, N_("AI servo AF")},
99  { 6,  2, N_("AI focus AF")},
100  { 6,  3, N_("Manual focus")},
101  { 6,  4, N_("Single")},
102  { 6,  5, N_("Continuous")},
103  { 6,  6, N_("Manual focus")},
104  { 6,  16, N_("Pan focus")},
105  { 8,  1, N_("JPEG")},
106  { 8,  2, N_("CRW+THM")},
107  { 8,  3, N_("AVI+THM")},
108  { 8,  4, N_("TIF")},
109  { 8,  5, N_("TIF+JPEG")},
110  { 8,  6, N_("CR2")},
111  { 8,  7, N_("CR2+JPEG")},
112  { 9,  0, N_("Large")},
113  { 9,  1, N_("Medium")},
114  { 9,  2, N_("Small")},
115  { 9,  5, N_("Medium 1")},
116  { 9,  6, N_("Medium 2")},
117  { 9,  7, N_("Medium 3")},
118  { 9,  8, N_("Postcard")},
119  { 9,  9, N_("Widescreen")},
120  {10,  0, N_("Full auto")},
121  {10,  1, N_("Manual")},
122  {10,  2, N_("Landscape")},
123  {10,  3, N_("Fast shutter")},
124  {10,  4, N_("Slow shutter")},
125  {10,  5, N_("Night")},
126  {10,  6, N_("Grayscale")},
127  {10,  7, N_("Sepia")},
128  {10,  8, N_("Portrait")},
129  {10,  9, N_("Sports")},
130  {10, 10, N_("Macro")},
131  {10, 11, N_("Black & white")},
132  {10, 12, N_("Pan focus")},
133  {10, 13, N_("Vivid")},
134  {10, 14, N_("Neutral")},
135  {10, 15, N_("Flash off")},
136  {10, 16, N_("Long shutter")},
137  {10, 17, N_("Super macro")},
138  {10, 18, N_("Foliage")},
139  {10, 19, N_("Indoor")},
140  {10, 20, N_("Fireworks")},
141  {10, 21, N_("Beach")},
142  {10, 22, N_("Underwater")},
143  {10, 23, N_("Snow")},
144  {10, 24, N_("Kids & pets")},
145  {10, 25, N_("Night snapshot")},
146  {10, 26, N_("Digital macro")},
147  {10, 27, N_("My colors")},
148  {10, 28, N_("Still image")},
149  {10, 30, N_("Color accent")},
150  {10, 31, N_("Color swap")},
151  {10, 32, N_("Aquarium")},
152  {10, 33, N_("ISO 3200")},
153  {11, 0, N_("None")},
154  {11, 1, N_("2x")},
155  {11, 2, N_("4x")},
156  {11, 3, N_("Other")},
157  {12, 0x0000, N_("Normal")},
158  {12, 0x0001, N_("High")},
159  {12, 0xffff, N_("Low")},
160  {13, 0x0000, N_("Normal")},
161  {13, 0x0001, N_("High")},
162  {13, 0xffff, N_("Low")},
163  {14, 0x0000, N_("Normal")},
164  {14, 0x0001, N_("High")},
165  {14, 0xffff, N_("Low")},
166  {15, 14, N_("Auto high")},
167  {15, 15, N_("Auto")},
168  {15, 16, N_("50")},
169  {15, 17, N_("100")},
170  {15, 18, N_("200")},
171  {15, 19, N_("400")},
172  {15, 20, N_("800")},
173  {16,  0, N_("Default")},
174  {16,  1, N_("Spot")},
175  {16,  2, N_("Average")},
176  {16,  3, N_("Evaluative")},
177  {16,  4, N_("Partial")},
178  {16,  5, N_("Center-weighted average")},
179  {17,  0, N_("Manual")},
180  {17,  1, N_("Auto")},
181  {17,  2, N_("Not known")},
182  {17,  3, N_("Macro")},
183  {17,  4, N_("Very close")},
184  {17,  5, N_("Close")},
185  {17,  6, N_("Middle range")},
186  {17,  7, N_("Far range")},
187  {17,  8, N_("Pan focus")},
188  {17,  9, N_("Super macro")},
189  {17,  10, N_("Infinity")},
190  {18, 0x2005, N_("Manual AF point selection")},
191  {18, 0x3000, N_("None (MF)")},
192  {18, 0x3001, N_("Auto-selected")},
193  {18, 0x3002, N_("Right")},
194  {18, 0x3003, N_("Center")},
195  {18, 0x3004, N_("Left")},
196  {18, 0x4001, N_("Auto AF point selection")},
197  {19,  0, N_("Easy shooting")},
198  {19,  1, N_("Program")},
199  {19,  2, N_("Tv-priority")},
200  {19,  3, N_("Av-priority")},
201  {19,  4, N_("Manual")},
202  {19,  5, N_("A-DEP")},
203  {19,  6, N_("M-DEP")},
204  {21,   1, N_("Canon EF 50mm f/1.8")},
205  {21,   2, N_("Canon EF 28mm f/2.8")},
206  {21,   4, N_("Sigma UC Zoom 35-135mm f/4-5.6")},
207  {21,   6, N_("Tokina AF193-2 19-35mm f/3.5-4.5")},
208  {21,   7, N_("Canon EF 100-300mm F5.6L")},
209  {21,  10, N_("Sigma 50mm f/2.8 EX or 28mm f/1.8")},
210  {21,  11, N_("Canon EF 35mm f/2")},
211  {21,  13, N_("Canon EF 15mm f/2.8")},
212  {21,  21, N_("Canon EF 80-200mm f/2.8L")},
213  {21,  22, N_("Tokina AT-X280AF PRO 28-80mm F2.8 ASPHERICAL")},
214  {21,  26, N_("Cosina 100mm f/3.5 Macro AF")},
215  {21,  28, N_("Tamron AF Aspherical 28-200mm f/3.8-5.6")},
216  {21,  29, N_("Canon EF 50mm f/1.8 MkII")},
217  {21,  31, N_("Tamron SP AF 300mm f/2.8 LD IF")},
218  {21,  32, N_("Canon EF 24mm f/2.8 or Sigma 15mm f/2.8 EX Fisheye")},
219  {21,  39, N_("Canon EF 75-300mm f/4-5.6")},
220  {21,  40, N_("Canon EF 28-80mm f/3.5-5.6")},
221  {21,  43, N_("Canon EF 28-105mm f/4-5.6")},
222  {21,  45, N_("Canon EF-S 18-55mm f/3.5-5.6")},
223  {21, 124, N_("Canon MP-E 65mm f/2.8 1-5x Macro Photo")},
224  {21, 125, N_("Canon TS-E 24mm f/3.5L")},
225  {21, 126, N_("Canon TS-E 45mm f/2.8")},
226  {21, 127, N_("Canon TS-E 90mm f/2.8")},
227  {21, 130, N_("Canon EF 50mm f/1.0L")},
228  {21, 131, N_("Sigma 17-35mm f2.8-4 EX Aspherical HSM")},
229  {21, 134, N_("Canon EF 600mm f/4L IS")},
230  {21, 135, N_("Canon EF 200mm f/1.8L")},
231  {21, 136, N_("Canon EF 300mm f/2.8L")},
232  {21, 137, N_("Canon EF 85mm f/1.2L")},
233  {21, 139, N_("Canon EF 400mm f/2.8L")},
234  {21, 141, N_("Canon EF 500mm f/4.5L")},
235  {21, 142, N_("Canon EF 300mm f/2.8L IS")},
236  {21, 143, N_("Canon EF 500mm f/4L IS")},
237  {21, 149, N_("Canon EF 100mm f/2")},
238  {21, 150, N_("Sigma 20mm EX f/1.8")},
239  {21, 151, N_("Canon EF 200mm f/2.8L")},
240  {21, 152, N_("Sigma 10-20mm F4-5.6 or 12-24mm f/4.5-5.6 or 14mm f/2.8")},
241  {21, 153, N_("Canon EF 35-350mm f/3.5-5.6L")},
242  {21, 155, N_("Canon EF 85mm f/1.8 USM")},
243  {21, 156, N_("Canon EF 28-105mm f/3.5-4.5 USM")},
244  {21, 160, N_("Canon EF 20-35mm f/3.5-4.5 USM")},
245  {21, 161, N_("Canon EF 28-70mm f/2.8L or Sigma 24-70mm EX f/2.8")},
246  {21, 165, N_("Canon EF 70-200mm f/2.8 L")},
247  {21, 166, N_("Canon EF 70-200mm f/2.8 L + x1.4")},
248  {21, 167, N_("Canon EF 70-200mm f/2.8 L + x2")},
249  {21, 168, N_("Canon EF 28mm f/1.8 USM")},
250  {21, 169, N_("Sigma 15-30mm f/3.5-4.5 EX DG Aspherical")},
251  {21, 170, N_("Canon EF 200mm f/2.8L II")},
252  {21, 173, N_("Canon EF 180mm Macro f/3.5L or Sigma 180mm EX HSM Macro f/3.5")},
253  {21, 174, N_("Canon EF 135mm f/2L")},
254  {21, 176, N_("Canon EF 24-85mm f/3.5-4.5 USM")},
255  {21, 177, N_("Canon EF 300mm f/4L IS")},
256  {21, 178, N_("Canon EF 28-135mm f/3.5-5.6 IS")},
257  {21, 180, N_("Canon EF 35mm f/1.4L")},
258  {21, 181, N_("Canon EF 100-400mm f/4.5-5.6L IS + x1.4")},
259  {21, 182, N_("Canon EF 100-400mm f/4.5-5.6L IS + x2")},
260  {21, 183, N_("Canon EF 100-400mm f/4.5-5.6L IS")},
261  {21, 184, N_("Canon EF 400mm f/2.8L + x2")},
262  {21, 186, N_("Canon EF 70-200mm f/4L")},
263  {21, 190, N_("Canon EF 100mm f/2.8 Macro")},
264  {21, 191, N_("Canon EF 400mm f/4 DO IS")},
265  {21, 197, N_("Canon EF 75-300mm f/4-5.6 IS")},
266  {21, 198, N_("Canon EF 50mm f/1.4")},
267  {21, 202, N_("Canon EF 28-80 f/3.5-5.6 USM IV")},
268  {21, 211, N_("Canon EF 28-200mm f/3.5-5.6")},
269  {21, 213, N_("Canon EF 90-300mm f/4.5-5.6")},
270  {21, 214, N_("Canon EF-S 18-55mm f/3.5-4.5 USM")},
271  {21, 224, N_("Canon EF 70-200mm f/2.8L IS USM")},
272  {21, 225, N_("Canon EF 70-200mm f/2.8L IS USM + x1.4")},
273  {21, 226, N_("Canon EF 70-200mm f/2.8L IS USM + x2")},
274  {21, 229, N_("Canon EF 16-35mm f/2.8L")},
275  {21, 230, N_("Canon EF 24-70mm f/2.8L")},
276  {21, 231, N_("Canon EF 17-40mm f/4L")},
277  {21, 232, N_("Canon EF 70-300mm f/4.5-5.6 DO IS USM")},
278  {21, 234, N_("Canon EF-S 17-85mm f4-5.6 IS USM")},
279  {21, 235, N_("Canon EF-S10-22mm F3.5-4.5 USM")},
280  {21, 236, N_("Canon EF-S60mm F2.8 Macro USM")},
281  {21, 237, N_("Canon EF 24-105mm f/4L IS")},
282  {21, 238, N_("Canon EF 70-300mm F4-5.6 IS USM")},
283  {21, 241, N_("Canon EF 50mm F1.2L USM")},
284  {21, 242, N_("Canon EF 70-200mm f/4L IS USM")},
285  {28, 0, N_("Manual")},
286  {28, 1, N_("TTL")},
287  {28, 2, N_("A-TTL")},
288  {28, 3, N_("E-TTL")},
289  {28, 4, N_("FP sync enabled")},
290  {28, 7, N_("2nd-curtain sync used")},
291  {28, 11, N_("FP sync used")},
292  {28, 13, N_("Internal")},
293  {28, 14, N_("External")},
294  {31,  0, N_("Single")},
295  {31,  1, N_("Continuous")},
296  {32, 0, N_("Normal AE")},
297  {32, 1, N_("Exposure compensation")},
298  {32, 2, N_("AE lock")},
299  {32, 3, N_("AE lock + Exposure compensation")},
300  {32, 4, N_("No AE")},
301  {33, 0, N_("Off")},
302  {33, 1, N_("On")},
303  {33, 2, N_("On, shot only")},
304  {39, 0, N_("Off")},
305  {39, 1, N_("Vivid")},
306  {39, 2, N_("Neutral")},
307  {39, 3, N_("Smooth")},
308  {39, 4, N_("Sepia")},
309  {39, 5, N_("Black & white")},
310  {39, 6, N_("Custom")},
311  {39, 100, N_("My color data")},
312  {40, 0, N_("Off")},
313  {40, 0x0500, N_("Full")},
314  {40, 0x0502, N_("2/3")},
315  {40, 0x0504, N_("1/3")},
316#endif
317  { 0,  0, NULL}
318},
319entries_focal_length [] = {
320#ifndef NO_VERBOSE_TAG_DATA
321	{0, 1, N_("Fixed")},
322	{0, 2, N_("Zoom")},
323#endif
324	{0, 0, NULL}
325},
326entries_settings_2 [] = {
327#ifndef NO_VERBOSE_TAG_DATA
328  { 6,  0, N_("Auto")},
329  { 6,  1, N_("Sunny")},
330  { 6,  2, N_("Cloudy")},
331  { 6,  3, N_("Tungsten")},
332  { 6,  4, N_("Fluorescent")},
333  { 6,  5, N_("Flash")},
334  { 6,  6, N_("Custom")},
335  { 6,  7, N_("Black & white")},
336  { 6,  8, N_("Shade")},
337  { 6,  9, N_("Manual temperature (Kelvin)")},
338  { 6,  10, N_("PC set 1")},
339  { 6,  11, N_("PC set 2")},
340  { 6,  12, N_("PC set 3")},
341  { 6,  14, N_("Daylight fluorescent")},
342  { 6,  15, N_("Custom 1")},
343  { 6,  16, N_("Custom 2")},
344  { 6,  17, N_("Underwater")},
345  { 7,  0, N_("Off")},
346  { 7,  1, N_("Night scene")},
347  { 7,  2, N_("On")},
348  { 7,  3, N_("None")},
349  { 13,  0x3000, N_("None (MF)")},
350  { 13,  0x3001, N_("Right")},
351  { 13,  0x3002, N_("Center")},
352  { 13,  0x3003, N_("Center + Right")},
353  { 13,  0x3004, N_("Left")},
354  { 13,  0x3005, N_("Left + Right")},
355  { 13,  0x3006, N_("Left + Center")},
356  { 13,  0x3007, N_("All")},
357  { 15,  0, N_("Off")},
358  { 15,  1, N_("On (shot 1)")},
359  { 15,  2, N_("On (shot 2)")},
360  { 15,  3, N_("On (shot 3)")},
361  { 15,  0xffff, N_("On")},
362  { 25,  248, N_("EOS high-end")},
363  { 25,  250, N_("Compact")},
364  { 25,  252, N_("EOS mid-range")},
365  { 26,  0, N_("None")},
366  { 26,  1, N_("Rotate 90 CW")},
367  { 26,  2, N_("Rotate 180")},
368  { 26,  3, N_("Rotate 270 CW")},
369  { 26,  0xffff, N_("Rotated by software")},
370  { 27,  0, N_("Off")},
371  { 27,  1, N_("On")},
372  { 32,  0, N_("Off")},
373  { 32,  0x0014, N_("1/3")},
374  { 32,  0x008c, N_("2/3")},
375  { 32,  0x07d0, N_("Full")},
376#endif
377  {0, 0, NULL}
378},
379entries_panorama [] = {
380#ifndef NO_VERBOSE_TAG_DATA
381	{0, 0, N_("Left to right")},
382	{0, 1, N_("Right to left")},
383	{0, 2, N_("Bottom to top")},
384	{0, 3, N_("Top to bottom")},
385	{0, 4, N_("2x2 matrix (clockwise)")},
386#endif
387	{0, 0, NULL}
388},
389color_information [] = {
390#ifndef NO_VERBOSE_TAG_DATA
391  {0, 0, N_("Standard")},
392  {0, 1, N_("Manual")},
393  {0, 2, N_("Custom")},
394  {2, 0, N_("N/A")},
395  {2, 1, N_("Lowest")},
396  {2, 2, N_("Low")},
397  {2, 3, N_("Standard")},
398  {2, 4, N_("High")},
399  {2, 5, N_("Highest")},
400  {7,  0, N_("Auto")},
401  {7,  1, N_("Daylight")},
402  {7,  2, N_("Cloudy")},
403  {7,  3, N_("Tungsten")},
404  {7,  4, N_("Fluorescent")},
405  {7,  5, N_("Flash")},
406  {7,  6, N_("Custom")},
407  {7,  7, N_("Black & White")},
408  {7,  8, N_("Shade")},
409  {7,  9, N_("Manual Temperature (Kelvin)")},
410  {7, 10, N_("PC Set1")},
411  {7, 11, N_("PC Set2")},
412  {7, 12, N_("PC Set3")},
413  {7, 14, N_("Daylight Fluorescent")},
414  {7, 15, N_("Custom 1")},
415  {7, 16, N_("Custom 2")},
416  {7, 17, N_("Underwater")},
417  {9, 0x00, N_("None")},
418  {9, 0x01, N_("Standard")},
419  {9, 0x02, N_("Set 1")},
420  {9, 0x03, N_("Set 2")},
421  {9, 0x04, N_("Set 3")},
422  {9, 0x21, N_("User Def. 1")},
423  {9, 0x22, N_("User Def. 2")},
424  {9, 0x23, N_("User Def. 3")},
425  {9, 0x41, N_("External 1")},
426  {9, 0x42, N_("External 2")},
427  {9, 0x43, N_("External 3")},
428  {9, 0x81, N_("Standard")},
429  {9, 0x82, N_("Portrait")},
430  {9, 0x83, N_("Landscape")},
431  {9, 0x84, N_("Neutral")},
432  {9, 0x85, N_("Faithful")},
433  {9, 0x86, N_("Monochrome")},
434#endif
435  {0, 0, NULL}
436};
437
438static void
439canon_search_table_value (const struct canon_entry_table_t table[],
440    unsigned int t, ExifShort vs, char *val, unsigned int maxlen)
441{
442	unsigned int j;
443
444	/* Search the table for the first matching subtag and value. */
445	for (j = 0; table[j].name && ((table[j].subtag < t) ||
446			((table[j].subtag == t) && table[j].value <= vs)); j++) {
447		if ((table[j].subtag == t) && (table[j].value == vs)) {
448			break;
449		}
450	}
451	if ((table[j].subtag == t) && (table[j].value == vs) && table[j].name) {
452		/* Matching subtag and value found. */
453		strncpy (val, _(table[j].name), maxlen);
454	} else {
455		/* No matching subtag and/or value found. */
456		snprintf (val, maxlen, "0x%04x", vs);
457	}
458}
459
460static void
461canon_search_table_bitfield (const struct canon_entry_table_t table[],
462    unsigned int t, ExifShort vs, char *val, unsigned int maxlen)
463{
464	unsigned int j;
465
466	/* Search the table for the first matching subtag. */
467	for (j = 0; table[j].name && (table[j].subtag <= t); j++) {
468		if (table[j].subtag == t) {
469			break;
470		}
471	}
472	if ((table[j].subtag == t) && table[j].name) {
473		unsigned int i, bit, lastbit = 0;
474
475		/*
476		 * Search the table for the last matching bit, because
477		 * that one needs no additional comma appended.
478		 */
479		for (i = j; table[i].name && (table[i].subtag == t); i++) {
480			bit = table[i].value;
481			if ((vs >> bit) & 1) {
482				lastbit = bit;
483			}
484		}
485		/* Search the table for all matching bits. */
486		for (i = j; table[i].name && (table[i].subtag == t); i++) {
487			bit = table[i].value;
488			if ((vs >> bit) & 1) {
489				strncat(val, _(table[i].name), maxlen - strlen (val));
490				if (bit != lastbit)
491					strncat (val, _(", "), maxlen - strlen (val));
492			}
493		}
494	} else {
495		/* No matching subtag found. */
496		snprintf (val, maxlen, "0x%04x", vs);
497	}
498}
499
500unsigned int
501mnote_canon_entry_count_values (const MnoteCanonEntry *entry)
502{
503	unsigned int  val;
504
505	if (!entry) return 0;
506
507	switch (entry->tag) {
508	case MNOTE_CANON_TAG_FOCAL_LENGTH:
509	case MNOTE_CANON_TAG_PANORAMA:
510		return entry->components;
511	case MNOTE_CANON_TAG_SETTINGS_1:
512	case MNOTE_CANON_TAG_SETTINGS_2:
513	case MNOTE_CANON_TAG_CUSTOM_FUNCS:
514	case MNOTE_CANON_TAG_COLOR_INFORMATION:
515		if (entry->format != EXIF_FORMAT_SHORT) return 0;
516
517		val = exif_get_short (entry->data, entry->order);
518		/* val is buffer size, i.e. # of values plus 1 */
519		return MIN (entry->size - 2, val) / 2;
520	default:
521		return 1;
522	}
523}
524
525/*
526 * For reference, see Exif 2.1 specification (Appendix C),
527 * or http://en.wikipedia.org/wiki/APEX_system
528 */
529static double
530apex_value_to_aperture (double x)
531{
532	return pow (2, x / 2.);
533}
534
535static double
536apex_value_to_shutter_speed(double x)
537{
538	return 1.0 / pow (2, x);
539}
540
541static double
542apex_value_to_iso_speed (double x)
543{
544	return 3.125 * pow (2, x);
545}
546
547char *
548mnote_canon_entry_get_value (const MnoteCanonEntry *entry, unsigned int t, char *val, unsigned int maxlen)
549{
550	char buf[128];
551	ExifLong vl;
552	ExifShort vs, n;
553	unsigned char *data;
554	double d;
555
556	if (!entry)
557		return NULL;
558
559	data = entry->data;
560
561	memset (val, 0, maxlen);
562	maxlen--;
563
564	switch (entry->tag) {
565	case MNOTE_CANON_TAG_SETTINGS_1:
566		CF (entry->format, EXIF_FORMAT_SHORT, val, maxlen);
567		n = exif_get_short (data, entry->order) / 2;
568		if (t >= n) return NULL;
569		CC (entry->components, n, val, maxlen);
570		vs = exif_get_short (data + 2 + t * 2, entry->order);
571		switch (t) {
572		case 1:
573			if (!vs) {
574				strncpy(val, _("Off"), maxlen);
575				break;
576			}
577			snprintf (val, maxlen, _("%i (ms)"), vs * 100);
578			break;
579		case 15:
580			if (((vs & 0xC000) == 0x4000) && (vs != 0x7FFF)) {
581				/* Canon S3 IS - directly specified value */
582				snprintf (val, maxlen, "%i", vs & ~0x4000);
583			} else {
584				/* Standard Canon - index into lookup table */
585				canon_search_table_value (entries_settings_1, t, vs, val, maxlen);
586			}
587			break;
588		case 22:
589		case 23:
590		case 24:
591			snprintf (val, maxlen, "%u", vs);
592			break;
593		case 25:
594		case 26:
595			snprintf (val, maxlen, "%.2f", apex_value_to_aperture (vs / 32.0));
596			break;
597		case 28:
598			canon_search_table_bitfield(entries_settings_1, t, vs, val, maxlen);
599			break;
600		case 34:
601			snprintf (val, maxlen, "%.2f", vs / 10.0);
602			break;
603		case 35:
604		case 36:
605			snprintf (val, maxlen, "%u", vs);
606			break;
607		default:
608			canon_search_table_value (entries_settings_1, t, vs, val, maxlen);
609		}
610		break;
611
612	case MNOTE_CANON_TAG_FOCAL_LENGTH:
613		CF (entry->format, EXIF_FORMAT_SHORT, val, maxlen);
614		vs = exif_get_short (data + t * 2, entry->order);
615		switch (t) {
616		case 1:
617			snprintf (val, maxlen, "%u", vs);
618			break;
619		case 2:
620		case 3:
621			snprintf (val, maxlen, _("%.2f mm"), vs * 25.4 / 1000);
622			break;
623		default:
624			canon_search_table_value (entries_focal_length, t, vs, val, maxlen);
625		}
626		break;
627
628	case MNOTE_CANON_TAG_SETTINGS_2:
629		CF (entry->format, EXIF_FORMAT_SHORT, val, maxlen);
630		n = exif_get_short (data, entry->order) / 2;
631		if (t >= n) return NULL;
632		CC (entry->components, n, val, maxlen);
633		vs = exif_get_short (data + 2 + t * 2, entry->order);
634		switch (t) {
635		case 0:
636			snprintf (val, maxlen, "%.3f", pow (2, (ExifSShort)vs / 32.0));
637			break;
638		case 1:
639			snprintf (val, maxlen, "%.0f", apex_value_to_iso_speed ((ExifSShort)vs / 32.0));
640			break;
641		case 2:
642		case 5:
643		case 14:
644		case 16:
645			snprintf (val, maxlen, _("%.2f EV"), (ExifSShort)vs / 32.0);
646			break;
647		case 3:
648		case 20:
649			snprintf (val, maxlen, "%.2f", apex_value_to_aperture (vs / 32.0));
650			break;
651		case 4:
652		case 21:
653			d = apex_value_to_shutter_speed ((ExifSShort)vs / 32.0);
654			if (d < 1)
655				snprintf (val, maxlen, _("1/%i"),(int)(1.0 / d));
656			else
657				snprintf (val, maxlen, "%i", (int) d);
658			break;
659		case 8:
660			snprintf (val, maxlen, "%u", vs);
661			break;
662		case 12:
663			snprintf (val, maxlen, "%.2f", vs / 32.0);
664			break;
665		case 18:
666		case 19:
667			snprintf (val, maxlen, _("%u mm"), vs);
668			break;
669		case 28:
670			if ((ExifSShort)vs <= 0) {
671				strncpy(val, _("Off"), maxlen);
672				break;
673			}
674			snprintf (val, maxlen, _("%i (ms)"), vs * 100);
675			break;
676		default:
677			canon_search_table_value (entries_settings_2, t, vs, val, maxlen);
678		}
679		break;
680
681	case MNOTE_CANON_TAG_PANORAMA:
682		CF (entry->format, EXIF_FORMAT_SHORT, val, maxlen);
683		vs = exif_get_short (data + t * 2, entry->order);
684		canon_search_table_value (entries_panorama, t, vs, val, maxlen);
685		break;
686
687	case MNOTE_CANON_TAG_OWNER:
688		CC (entry->components, 32, val, maxlen);
689		/* Fall through; ImageType can have many sizes */
690	case MNOTE_CANON_TAG_IMAGE_TYPE:
691		CF (entry->format, EXIF_FORMAT_ASCII, val, maxlen);
692		strncpy (val, (char *)data, MIN (entry->size, maxlen));
693		break;
694
695	case MNOTE_CANON_TAG_FIRMWARE:
696		CF (entry->format, EXIF_FORMAT_ASCII, val, maxlen);
697/*		CC2 (entry->components, 24, 32, val, maxlen); Can also be 22 */
698		strncpy (val, (char *)data, MIN (entry->size, maxlen));
699		break;
700
701	case MNOTE_CANON_TAG_IMAGE_NUMBER:
702		CF (entry->format, EXIF_FORMAT_LONG, val, maxlen);
703		CC (entry->components, 1, val, maxlen);
704		vl = exif_get_long (data, entry->order);
705		snprintf (val, maxlen, "%03lu-%04lu",
706				(unsigned long) vl/10000,
707				(unsigned long) vl%10000);
708		break;
709
710	case MNOTE_CANON_TAG_SERIAL_NUMBER:
711		CF (entry->format, EXIF_FORMAT_LONG, val, maxlen);
712		CC (entry->components, 1, val, maxlen);
713		vl = exif_get_long (data, entry->order);
714		snprintf (val, maxlen, "%04X-%05d", (int)vl>>16,(int)vl&0xffff);
715		break;
716
717	case MNOTE_CANON_TAG_CUSTOM_FUNCS:
718		CF (entry->format, EXIF_FORMAT_SHORT, val, maxlen);
719		n = exif_get_short (data, entry->order) / 2;
720		if (t >= n) return NULL;
721		CC (entry->components, n, val, maxlen);
722		vs = exif_get_short (data + 2 + t * 2, entry->order);
723		snprintf (buf, sizeof (buf), "%u", vs);
724		strncat (val, buf, maxlen - strlen (val));
725		break;
726
727	case MNOTE_CANON_TAG_COLOR_INFORMATION:
728		CF (entry->format, EXIF_FORMAT_SHORT, val, maxlen);
729		n = exif_get_short (data, entry->order) / 2;
730		if (t >= n) return NULL;
731		CC (entry->components, n, val, maxlen);
732		vs = exif_get_short (data + 2 + t * 2, entry->order);
733		canon_search_table_value (color_information, t, vs, val, maxlen);
734		break;
735
736	default:
737#ifdef DEBUG
738	  {
739		int i;
740		if (entry->format == EXIF_FORMAT_SHORT)
741		for(i=0;i<entry->components;i++) {
742			vs = exif_get_short (data, entry->order);
743			data+=2;
744			printf ("Value%d=%d\n", i, vs);
745		}
746		else if (entry->format == EXIF_FORMAT_LONG)
747		for(i=0;i<entry->components;i++) {
748			vl = exif_get_long (data, entry->order);
749			data+=4;
750			printf ("Value%d=%d\n", i, vs);
751		}
752		else if (entry->format == EXIF_FORMAT_ASCII)
753		    strncpy (val, data, MIN (entry->size, maxlen));
754	  }
755#endif
756		break;
757	}
758	return val;
759}
760