3 * Copyright (c) 2001 Lutz Mueller <lutz@users.sourceforge.net>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301 USA.
23 #include <libexif/exif-utils.h>
26 exif_array_set_byte_order (ExifFormat f, unsigned char *b, unsigned int n,
27 ExifByteOrder o_orig, ExifByteOrder o_new)
30 unsigned int fs = exif_format_get_size (f);
38 if (!b || !n || !fs) return;
41 case EXIF_FORMAT_SHORT:
42 for (j = 0; j < n; j++) {
43 s = exif_get_short (b + j * fs, o_orig);
44 exif_set_short (b + j * fs, o_new, s);
47 case EXIF_FORMAT_SSHORT:
48 for (j = 0; j < n; j++) {
49 ss = exif_get_sshort (b + j * fs, o_orig);
50 exif_set_sshort (b + j * fs, o_new, ss);
53 case EXIF_FORMAT_LONG:
54 for (j = 0; j < n; j++) {
55 l = exif_get_long (b + j * fs, o_orig);
56 exif_set_long (b + j * fs, o_new, l);
59 case EXIF_FORMAT_RATIONAL:
60 for (j = 0; j < n; j++) {
61 r = exif_get_rational (b + j * fs, o_orig);
62 exif_set_rational (b + j * fs, o_new, r);
65 case EXIF_FORMAT_SLONG:
66 for (j = 0; j < n; j++) {
67 sl = exif_get_slong (b + j * fs, o_orig);
68 exif_set_slong (b + j * fs, o_new, sl);
71 case EXIF_FORMAT_SRATIONAL:
72 for (j = 0; j < n; j++) {
73 sr = exif_get_srational (b + j * fs, o_orig);
74 exif_set_srational (b + j * fs, o_new, sr);
77 case EXIF_FORMAT_UNDEFINED:
78 case EXIF_FORMAT_BYTE:
79 case EXIF_FORMAT_ASCII:
87 exif_get_sshort (const unsigned char *buf, ExifByteOrder order)
91 case EXIF_BYTE_ORDER_MOTOROLA:
92 return ((buf[0] << 8) | buf[1]);
93 case EXIF_BYTE_ORDER_INTEL:
94 return ((buf[1] << 8) | buf[0]);
97 /* Won't be reached */
102 exif_get_short (const unsigned char *buf, ExifByteOrder order)
104 return (exif_get_sshort (buf, order) & 0xffff);
108 exif_set_sshort (unsigned char *b, ExifByteOrder order, ExifSShort value)
112 case EXIF_BYTE_ORDER_MOTOROLA:
113 b[0] = (unsigned char) (value >> 8);
114 b[1] = (unsigned char) value;
116 case EXIF_BYTE_ORDER_INTEL:
117 b[0] = (unsigned char) value;
118 b[1] = (unsigned char) (value >> 8);
124 exif_set_short (unsigned char *b, ExifByteOrder order, ExifShort value)
126 exif_set_sshort (b, order, value);
130 exif_get_slong (const unsigned char *b, ExifByteOrder order)
134 case EXIF_BYTE_ORDER_MOTOROLA:
135 return (((uint32_t)b[0] << 24) | ((uint32_t)b[1] << 16) | ((uint32_t)b[2] << 8) | (uint32_t)b[3]);
136 case EXIF_BYTE_ORDER_INTEL:
137 return (((uint32_t)b[3] << 24) | ((uint32_t)b[2] << 16) | ((uint32_t)b[1] << 8) | (uint32_t)b[0]);
140 /* Won't be reached */
145 exif_set_slong (unsigned char *b, ExifByteOrder order, ExifSLong value)
149 case EXIF_BYTE_ORDER_MOTOROLA:
150 b[0] = (unsigned char) (value >> 24);
151 b[1] = (unsigned char) (value >> 16);
152 b[2] = (unsigned char) (value >> 8);
153 b[3] = (unsigned char) value;
155 case EXIF_BYTE_ORDER_INTEL:
156 b[3] = (unsigned char) (value >> 24);
157 b[2] = (unsigned char) (value >> 16);
158 b[1] = (unsigned char) (value >> 8);
159 b[0] = (unsigned char) value;
165 exif_get_long (const unsigned char *buf, ExifByteOrder order)
167 return (exif_get_slong (buf, order) & 0xffffffff);
171 exif_set_long (unsigned char *b, ExifByteOrder order, ExifLong value)
173 exif_set_slong (b, order, value);
177 exif_get_srational (const unsigned char *buf, ExifByteOrder order)
181 r.numerator = buf ? exif_get_slong (buf, order) : 0;
182 r.denominator = buf ? exif_get_slong (buf + 4, order) : 0;
188 exif_get_rational (const unsigned char *buf, ExifByteOrder order)
192 r.numerator = buf ? exif_get_long (buf, order) : 0;
193 r.denominator = buf ? exif_get_long (buf + 4, order) : 0;
199 exif_set_rational (unsigned char *buf, ExifByteOrder order,
203 exif_set_long (buf, order, value.numerator);
204 exif_set_long (buf + 4, order, value.denominator);
208 exif_set_srational (unsigned char *buf, ExifByteOrder order,
212 exif_set_slong (buf, order, value.numerator);
213 exif_set_slong (buf + 4, order, value.denominator);
216 /*! This function converts rather UCS-2LE than UTF-16 to UTF-8.
217 * It should really be replaced by iconv().
220 exif_convert_utf16_to_utf8 (char *out, const unsigned char *in, int maxlen)
226 ExifShort v = exif_get_short(in, EXIF_BYTE_ORDER_INTEL);
236 } else if (v < 0x800) {
238 *out++ = ((v >> 6) & 0x1F) | 0xC0;
239 *out++ = (v & 0x3F) | 0x80;
246 *out++ = ((v >> 12) & 0x0F) | 0xE0;
247 *out++ = ((v >> 6) & 0x3F) | 0x80;
248 *out++ = (v & 0x3F) | 0x80;