2 * Copyright Samsung Electronics Co.,LTD.
3 * Copyright (C) 2015 The Android Open Source Project
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef __HARDWARE_SAMSUNG_SLSI_EXYNOS_APPMARKER_WRITER_H__
18 #define __HARDWARE_SAMSUNG_SLSI_EXYNOS_APPMARKER_WRITER_H__
20 #include <ExynosExif.h>
22 #define JPEG_MAX_SEGMENT_SIZE ((1 << 16) - 1 - 100)
23 #define JPEG_MARKER_SIZE 2
24 #define JPEG_SEGMENT_LENFIELD_SIZE 2
26 #define IFD_FIELDCOUNT_SIZE 2
27 #define IFD_NEXTIFDOFFSET_SIZE 4
29 #define IFD_TAG_SIZE 2
30 #define IFD_TYPE_SIZE 2
31 #define IFD_COUNT_SIZE 4
32 #define IFD_VALOFF_SIZE 4
34 #define IFD_FIELD_SIZE \
35 (IFD_TAG_SIZE + IFD_TYPE_SIZE + IFD_COUNT_SIZE + IFD_VALOFF_SIZE)
37 #define EXTRA_APPMARKER_MIN 4
38 #define EXTRA_APPMARKER_LIMIT 10
40 #define MAX_GPS_PROCESSINGMETHOD_SIZE 108
42 #define EXIF_SUBSECTIME_LENGTH 5
43 #define EXIF_DATETIME_LENGTH 20
44 #define EXIF_GPSDATESTAMP_LENGTH 11
46 class CAppMarkerWriter
{
49 size_t m_szMaxThumbSize
; // Maximum available thumbnail stream size minus JPEG_MARKER_SIZE
50 uint16_t m_szApp1
; // The size of APP1 segment without marker
51 uint16_t m_szApp11
; // The size of APP11 segment without marker
52 uint16_t m_n0thIFDFields
;
53 uint16_t m_n1stIFDFields
;
54 uint16_t m_nExifIFDFields
;
55 uint16_t m_nGPSIFDFields
;
56 exif_attribute_t
*m_pExif
;
57 debug_attribute_t
*m_pDebug
;
60 uint32_t m_szSoftware
;
62 uint32_t m_szDateTime __unused
;
63 uint32_t m_szSubsecTime __unused
;
64 uint32_t m_szUniqueID
;
67 // The address to write compressed stream of the thumbnail image
69 // points to the "ValueOffset" field of JPEGInterchangeFormatLen tag of 1st IFD
70 // This should be updated after compression of thumbnail image.
71 // Note that the address may not be aligned by 32-bit.
72 char *m_pThumbSizePlaceholder
;
76 char *WriteAPP1(char *base
, bool reserve_thumbnail_space
, bool updating
= false);
77 char *WriteAPPX(char *base
, bool just_reserve
);
78 char *WriteAPP11(char *current
, size_t dummy
, size_t align
);
80 // dummy: number of dummy bytes written by the compressor of the main image
81 // this dummy size should be added to the APP1 length. Howerver, this dummy area
82 // for the main image stream should not be written by neither of appwriter nor
83 // thumbnail compressor.
85 CAppMarkerWriter(char *base
, exif_attribute_t
*exif
, debug_attribute_t
*debug
);
87 ~CAppMarkerWriter() { }
89 void PrepareAppWriter(char *base
, exif_attribute_t
*exif
, debug_attribute_t
*debug
);
91 char *GetMainStreamBase() { return m_pMainBase
; }
92 char *GetThumbStreamBase() { return m_pThumbBase
; }
93 char *GetThumbStreamSizeAddr() {
94 char *p
= m_pThumbSizePlaceholder
;
95 m_pThumbSizePlaceholder
= NULL
;
98 size_t GetMaxThumbnailSize() { return m_szMaxThumbSize
; }
99 // CalculateAPPSize() is valid after Write() is successful.
100 size_t CalculateAPPSize(size_t thumblen
= JPEG_MAX_SEGMENT_SIZE
) {
103 appsize
+= m_szApp1
+ JPEG_MARKER_SIZE
;
105 for (int idx
= 0; idx
< m_pDebug
->num_of_appmarker
; idx
++)
106 appsize
+= m_pDebug
->debugSize
[m_pDebug
->idx
[idx
][0]]
107 + JPEG_MARKER_SIZE
+ JPEG_SEGMENT_LENFIELD_SIZE
;
109 if (IsThumbSpaceReserved())
110 appsize
+= m_szMaxThumbSize
;
112 appsize
+= min(m_szMaxThumbSize
, thumblen
);
114 return appsize
+ m_szApp11
;
117 char *GetApp1End() { return m_pApp1End
; }
119 void Write(bool reserve_thumbnail_space
, size_t dummy
, size_t align
, bool reserve_debug
= false) {
120 m_pApp1End
= WriteAPP1(m_pAppBase
, reserve_thumbnail_space
);
121 char *appXend
= WriteAPPX(m_pApp1End
, reserve_debug
);
122 char *app11end
= WriteAPP11(appXend
, dummy
, align
);
123 m_szApp11
= PTR_DIFF(appXend
, app11end
);
124 m_pMainBase
= app11end
- dummy
;
127 void Update() { WriteAPP1(m_pAppBase
, false, true); }
129 bool IsThumbSpaceReserved() {
130 return PTR_DIFF(m_pAppBase
, m_pApp1End
) == (m_szApp1
+ m_szMaxThumbSize
+ JPEG_MARKER_SIZE
);
133 void Finalize(size_t thumbsize
);
135 void UpdateApp1Size(size_t amount
);
137 #endif //__HARDWARE_SAMSUNG_SLSI_EXYNOS_APPMARKER_WRITER_H__