1 /*
2  * Copyright Samsung Electronics Co.,LTD.
3  * Copyright (C) 2015 The Android Open Source Project
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 // To prevent build conflict with the previous libhwjpeg
19 #ifndef __HARDWARE_EXYNOS_EXYNOS_EXIF_H__
20 #define __HARDWARE_EXYNOS_EXYNOS_EXIF_H__
21 
22 #include <math.h>
23 
24 #include <sys/types.h>
25 
26 #define EXIF_LOG2(x)                    (log((double)(x)) / log(2.0))
27 #define APEX_FNUM_TO_APERTURE(x)        ((EXIF_LOG2((double)(x))) * 2.0)
28 #define APEX_EXPOSURE_TO_SHUTTER(x)     ((x) >= 1 ?                                 \
29                                         (int)(-(EXIF_LOG2((double)(x)) + 0.5)) :    \
30                                         (int)(-(EXIF_LOG2((double)(x)) - 0.5)))
31 #define APEX_ISO_TO_FILMSENSITIVITY(x)  ((int)(EXIF_LOG2((x) / 3.125) + 0.5))
32 
33 #define NUM_SIZE                    2
34 #define IFD_SIZE                    12
35 #define OFFSET_SIZE                 4
36 #define EXIF_INFO_LIMIT_SIZE        12288
37 
38 #define NUM_0TH_IFD_TIFF            10
39 #define NUM_0TH_IFD_EXIF            29
40 #define NUM_0TH_IFD_INTEROPERABILITY (2)
41 #define NUM_0TH_IFD_GPS             10
42 #define NUM_1TH_IFD_TIFF            9
43 
44 /* Type */
45 #define EXIF_TYPE_BYTE              1
46 #define EXIF_TYPE_ASCII             2
47 #define EXIF_TYPE_SHORT             3
48 #define EXIF_TYPE_LONG              4
49 #define EXIF_TYPE_RATIONAL          5
50 #define EXIF_TYPE_UNDEFINED         7
51 #define EXIF_TYPE_SLONG             9
52 #define EXIF_TYPE_SRATIONAL         10
53 
54 #define EXIF_FILE_SIZE              28800
55 
56 /* 0th IFD TIFF Tags */
57 #define EXIF_TAG_IMAGE_WIDTH                    0x0100
58 #define EXIF_TAG_IMAGE_HEIGHT                   0x0101
59 #define EXIF_TAG_MAKE                           0x010f
60 #define EXIF_TAG_MODEL                          0x0110
61 #define EXIF_TAG_ORIENTATION                    0x0112
62 #define EXIF_TAG_SOFTWARE                       0x0131
63 #define EXIF_TAG_DATE_TIME                      0x0132
64 #define EXIF_TAG_YCBCR_POSITIONING              0x0213
65 #define EXIF_TAG_EXIF_IFD_POINTER               0x8769
66 #define EXIF_TAG_GPS_IFD_POINTER                0x8825
67 
68 /* 0th IFD Exif Private Tags */
69 #define EXIF_TAG_EXPOSURE_TIME                  0x829A
70 #define EXIF_TAG_FNUMBER                        0x829D
71 #define EXIF_TAG_EXPOSURE_PROGRAM               0x8822
72 #define EXIF_TAG_ISO_SPEED_RATING               0x8827
73 #define EXIF_TAG_EXIF_VERSION                   0x9000
74 #define EXIF_TAG_DATE_TIME_ORG                  0x9003
75 #define EXIF_TAG_DATE_TIME_DIGITIZE             0x9004
76 #define EXIF_TAG_COMPONENTS_CONFIGURATION       0x9101
77 #define EXIF_TAG_SHUTTER_SPEED                  0x9201
78 #define EXIF_TAG_APERTURE                       0x9202
79 #define EXIF_TAG_BRIGHTNESS                     0x9203
80 #define EXIF_TAG_EXPOSURE_BIAS                  0x9204
81 #define EXIF_TAG_MAX_APERTURE                   0x9205
82 #define EXIF_TAG_METERING_MODE                  0x9207
83 #define EXIF_TAG_FLASH                          0x9209
84 #define EXIF_TAG_FOCAL_LENGTH                   0x920A
85 #define EXIF_TAG_MAKER_NOTE                     0x927C
86 #define EXIF_TAG_USER_COMMENT                   0x9286
87 #define EXIF_TAG_SUBSEC_TIME                    0x9290
88 #define EXIF_TAG_SUBSEC_TIME_ORIG               0x9291
89 #define EXIF_TAG_SUBSEC_TIME_DIG                0x9292
90 #define EXIF_TAG_FLASHPIX_VERSION               0xA000
91 #define EXIF_TAG_COLOR_SPACE                    0xA001
92 #define EXIF_TAG_PIXEL_X_DIMENSION              0xA002
93 #define EXIF_TAG_PIXEL_Y_DIMENSION              0xA003
94 #define EXIF_TAG_RELATED_SOUND_FILE             0xA004
95 #define EXIF_TAG_INTEROPERABILITY               0xA005
96 #define EXIF_TAG_SCENE_TYPE                     0xA301
97 #define EXIF_TAG_CUSTOM_RENDERED                0xA401
98 #define EXIF_TAG_EXPOSURE_MODE                  0xA402
99 #define EXIF_TAG_WHITE_BALANCE                  0xA403
100 #define EXIF_TAG_DIGITAL_ZOOM_RATIO             0xA404
101 #define EXIF_TAG_FOCA_LENGTH_IN_35MM_FILM       0xA405
102 #define EXIF_TAG_SCENCE_CAPTURE_TYPE            0xA406
103 #define EXIF_TAG_CONTRAST                       0xA408
104 #define EXIF_TAG_SATURATION                     0xA409
105 #define EXIF_TAG_SHARPNESS                      0xA40A
106 #define EXIF_TAG_IMAGE_UNIQUE_ID                0xA420
107 
108 /* 0th IFD Interoperability Info Tags */
109 #define EXIF_TAG_INTEROPERABILITY_INDEX         0x0001
110 #define EXIF_TAG_INTEROPERABILITY_VERSION       0x0002
111 
112 /* 0th IFD GPS Info Tags */
113 #define EXIF_TAG_GPS_VERSION_ID                 0x0000
114 #define EXIF_TAG_GPS_LATITUDE_REF               0x0001
115 #define EXIF_TAG_GPS_LATITUDE                   0x0002
116 #define EXIF_TAG_GPS_LONGITUDE_REF              0x0003
117 #define EXIF_TAG_GPS_LONGITUDE                  0x0004
118 #define EXIF_TAG_GPS_ALTITUDE_REF               0x0005
119 #define EXIF_TAG_GPS_ALTITUDE                   0x0006
120 #define EXIF_TAG_GPS_TIMESTAMP                  0x0007
121 #define EXIF_TAG_GPS_PROCESSING_METHOD          0x001B
122 #define EXIF_TAG_GPS_DATESTAMP                  0x001D
123 
124 /* 1th IFD TIFF Tags */
125 #define EXIF_TAG_COMPRESSION_SCHEME             0x0103
126 #define EXIF_TAG_X_RESOLUTION                   0x011A
127 #define EXIF_TAG_Y_RESOLUTION                   0x011B
128 #define EXIF_TAG_RESOLUTION_UNIT                0x0128
129 #define EXIF_TAG_JPEG_INTERCHANGE_FORMAT        0x0201
130 #define EXIF_TAG_JPEG_INTERCHANGE_FORMAT_LEN    0x0202
131 
132 typedef enum {
133     EXIF_ORIENTATION_UP     = 1,
134     EXIF_ORIENTATION_90     = 6,
135     EXIF_ORIENTATION_180    = 3,
136     EXIF_ORIENTATION_270    = 8,
137 } ExifOrientationType;
138 
139 typedef enum {
140     EXIF_SCENE_STANDARD,
141     EXIF_SCENE_LANDSCAPE,
142     EXIF_SCENE_PORTRAIT,
143     EXIF_SCENE_NIGHT,
144 } CamExifSceneCaptureType;
145 
146 typedef enum {
147     EXIF_METERING_UNKNOWN,
148     EXIF_METERING_AVERAGE,
149     EXIF_METERING_CENTER,
150     EXIF_METERING_SPOT,
151     EXIF_METERING_MULTISPOT,
152     EXIF_METERING_PATTERN,
153     EXIF_METERING_PARTIAL,
154     EXIF_METERING_OTHER     = 255,
155 } CamExifMeteringModeType;
156 
157 typedef enum {
158     EXIF_EXPOSURE_AUTO,
159     EXIF_EXPOSURE_MANUAL,
160     EXIF_EXPOSURE_AUTO_BRACKET,
161 } CamExifExposureModeType;
162 
163 typedef enum {
164     EXIF_WB_AUTO,
165     EXIF_WB_MANUAL,
166     EXIF_WB_INCANDESCENT,
167     EXIF_WB_FLUORESCENT,
168     EXIF_WB_DAYLIGHT,
169     EXIF_WB_CLOUDY,
170 } CamExifWhiteBalanceType;
171 
172 /* Values */
173 #define EXIF_DEF_MAKER          "Samsung Electronics Co., Ltd."    /* testJpegExif on the CTS test. This should match Build.MANUFACTURER  */
174 #define EXIF_DEF_MODEL          "SAMSUNG"
175 #define EXIF_DEF_SOFTWARE       "SAMSUNG"
176 #define EXIF_DEF_EXIF_VERSION   "0220"
177 #define EXIF_DEF_USERCOMMENTS   "User comments"
178 
179 #define EXIF_DEF_YCBCR_POSITIONING  1   /* centered */
180 #define EXIF_DEF_EXPOSURE_MANUAL    1   /* manual program */
181 #define EXIF_DEF_EXPOSURE_PROGRAM   2   /* normal program */
182 #define EXIF_DEF_FLASH              0   /* O: off, 1: on*/
183 #define EXIF_DEF_COLOR_SPACE        1
184 #define EXIF_DEF_INTEROPERABILITY  (0)
185 #define EXIF_DEF_EXPOSURE_MODE      EXIF_EXPOSURE_AUTO
186 #define EXIF_DEF_APEX_DEN          (100)
187 
188 #define EXIF_DEF_COMPRESSION        6
189 #define EXIF_DEF_RESOLUTION_NUM     72
190 #define EXIF_DEF_RESOLUTION_DEN     1
191 #define EXIF_DEF_RESOLUTION_UNIT    2   /* inches */
192 
193 #define APP_MARKER_2                2
194 #define APP_MARKER_3                3
195 #define APP_MARKER_4                4
196 #define APP_MARKER_5                5
197 #define APP_MARKER_6                6
198 
199 typedef struct {
200     uint32_t num;
201     uint32_t den;
202 } rational_t;
203 
204 typedef struct {
205     int32_t num;
206     int32_t den;
207 } srational_t;
208 
209 typedef struct {
210     bool enableGps;
211     bool enableThumb;
212 
213     char maker[32];
214     char model[32];
215     char software[32];
216     char exif_version[4];
217     char date_time[20];
218     char sec_time[5];
219     unsigned int  maker_note_size;
220     unsigned char *maker_note;
221     unsigned int  user_comment_size;
222     unsigned char *user_comment;
223 
224     uint32_t width;
225     uint32_t height;
226     uint32_t widthThumb;
227     uint32_t heightThumb;
228 
229     uint16_t orientation;
230     uint16_t ycbcr_positioning;
231     uint16_t exposure_program;
232     uint16_t iso_speed_rating;
233     uint16_t metering_mode;
234     uint16_t flash;
235     uint16_t color_space;
236     uint16_t interoperability_index;
237     uint16_t custom_rendered;
238     uint16_t contrast;
239     uint16_t saturation;
240     uint16_t sharpness;
241 
242     uint16_t exposure_mode;
243     uint16_t white_balance;
244     uint16_t focal_length_in_35mm_length;
245     uint16_t scene_capture_type;
246     char unique_id[30];
247 
248     rational_t exposure_time;
249     rational_t fnumber;
250     rational_t aperture;
251     rational_t max_aperture;
252     rational_t focal_length;
253     rational_t digital_zoom_ratio;
254 
255     srational_t shutter_speed;
256     srational_t brightness;
257     srational_t exposure_bias;
258 
259     char gps_latitude_ref[2];
260     char gps_longitude_ref[2];
261 
262     uint8_t gps_version_id[4];
263     uint8_t gps_altitude_ref;
264 
265     rational_t gps_latitude[3];
266     rational_t gps_longitude[3];
267     rational_t gps_altitude;
268     rational_t gps_timestamp[3];
269     char gps_datestamp[11];
270     char gps_processing_method[100];
271 
272     rational_t x_resolution;
273     rational_t y_resolution;
274     uint16_t resolution_unit;
275     uint16_t compression_scheme;
276 } exif_attribute_t;
277 
278 typedef struct {
279     int num_of_appmarker; /* number of app marker */
280     int idx[15][1]; /* idx[number_of_appmarker][appmarker_number] */
281     char *debugData[15]; /* 0-base */
282     unsigned int debugSize[15];
283 } debug_attribute_t;
284 
285 bool UpdateDebugData(char *jpeg, size_t jpeglen, debug_attribute_t *debug);
286 bool UpdateExif(char *jpeg, size_t jpeglen, exif_attribute_t *exif);
287 
288 #endif /* __HARDWARE_EXYNOS_EXYNOS_EXIF_H__ */
289