384b56eff9de86936aa45104f4928edf362b2004
[platform/upstream/libexif.git] / libexif / exif-utils.c
1 /* exif-utils.c
2  *
3  * Copyright © 2001 Lutz Müller <lutz@users.sourceforge.net>
4  *
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.
9  *
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. 
14  *
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., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 #include <config.h>
22
23 #include <libexif/exif-utils.h>
24
25 typedef signed short ExifSShort;
26
27 static ExifSShort
28 exif_get_sshort (const unsigned char *buf, ExifByteOrder order)
29 {
30         if (!buf) return 0;
31         switch (order) {
32         case EXIF_BYTE_ORDER_MOTOROLA:
33                 return ((buf[0] << 8) | buf[1]);
34         case EXIF_BYTE_ORDER_INTEL:
35                 return ((buf[1] << 8) | buf[0]);
36         }
37
38         /* Won't be reached */
39         return (0);
40 }
41
42 ExifShort
43 exif_get_short (const unsigned char *buf, ExifByteOrder order)
44 {
45         return (exif_get_sshort (buf, order) & 0xffff);
46 }
47
48 void
49 exif_set_short (unsigned char *b, ExifByteOrder order, ExifShort value)
50 {
51         if (!b) return;
52         switch (order) {
53         case EXIF_BYTE_ORDER_MOTOROLA:
54                 b[0] = (unsigned char) (value >> 8);
55                 b[1] = (unsigned char) value;
56                 break;
57         case EXIF_BYTE_ORDER_INTEL:
58                 b[0] = (unsigned char) value;
59                 b[1] = (unsigned char) (value >> 8);
60                 break;
61         }
62 }
63
64 ExifSLong
65 exif_get_slong (const unsigned char *b, ExifByteOrder order)
66 {
67         if (!b) return 0;
68         switch (order) {
69         case EXIF_BYTE_ORDER_MOTOROLA:
70                 return ((b[0] << 24) | (b[1] << 16) | (b[2] << 8) | b[3]);
71         case EXIF_BYTE_ORDER_INTEL:
72                 return ((b[3] << 24) | (b[2] << 16) | (b[1] << 8) | b[0]);
73         }
74
75         /* Won't be reached */
76         return (0);
77 }
78
79 void
80 exif_set_slong (unsigned char *b, ExifByteOrder order, ExifSLong value)
81 {
82         if (!b) return;
83         switch (order) {
84         case EXIF_BYTE_ORDER_MOTOROLA:
85                 b[0] = (unsigned char) (value >> 24);
86                 b[1] = (unsigned char) (value >> 16);
87                 b[2] = (unsigned char) (value >> 8);
88                 b[3] = (unsigned char) value;
89                 break;
90         case EXIF_BYTE_ORDER_INTEL:
91                 b[3] = (unsigned char) (value >> 24);
92                 b[2] = (unsigned char) (value >> 16);
93                 b[1] = (unsigned char) (value >> 8);
94                 b[0] = (unsigned char) value;
95                 break;
96         }
97 }
98
99 ExifLong
100 exif_get_long (const unsigned char *buf, ExifByteOrder order)
101 {
102         return (exif_get_slong (buf, order) & 0xffffffff);
103 }
104
105 void
106 exif_set_long (unsigned char *b, ExifByteOrder order, ExifLong value)
107 {
108         exif_set_slong (b, order, value);
109 }
110
111 ExifSRational
112 exif_get_srational (const unsigned char *buf, ExifByteOrder order)
113 {
114         ExifSRational r;
115
116         r.numerator   = buf ? exif_get_slong (buf, order) : 0;
117         r.denominator = buf ? exif_get_slong (buf + 4, order) : 0;
118
119         return (r);
120 }
121
122 ExifRational
123 exif_get_rational (const unsigned char *buf, ExifByteOrder order)
124 {
125         ExifRational r;
126
127         r.numerator   = buf ? exif_get_long (buf, order) : 0;
128         r.denominator = buf ? exif_get_long (buf + 4, order) : 0;
129
130         return (r);
131 }
132
133 void
134 exif_set_rational (unsigned char *buf, ExifByteOrder order,
135                    ExifRational value)
136 {
137         if (!buf) return;
138         exif_set_long (buf, order, value.numerator);
139         exif_set_long (buf + 4, order, value.denominator);
140 }
141
142 void
143 exif_set_srational (unsigned char *buf, ExifByteOrder order,
144                     ExifSRational value)
145 {
146         if (!buf) return;
147         exif_set_slong (buf, order, value.numerator);
148         exif_set_slong (buf + 4, order, value.denominator);
149 }