tizen 2.3.1 release
[framework/web/wearable/wrt-plugins-tizen.git] / src / Exif / JpegFile.h
1 //
2 // Tizen Web Device API
3 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
19 #ifndef __TIZEN_EXIF_JPEG_FILE_H_
20 #define __TIZEN_EXIF_JPEG_FILE_H_
21
22 #include <memory>
23 #include <string>
24 #include <vector>
25 #include <stdio.h>
26 #include <map>
27 #include <libexif/exif-data.h>
28 #include <libexif/exif-entry.h>
29 #include <libexif/exif-utils.h>
30
31 namespace DeviceAPI {
32 namespace Exif {
33
34 enum JpegMarker{
35     JPEG_MARKER_UNKNOWN     = 0x00,
36     JPEG_MARKER_LOWEST_ID   = 0xc0,
37     JPEG_MARKER_SOI         = 0xd8, //Start Of Image
38     JPEG_MARKER_EOI         = 0xd9, //End Of Image
39     JPEG_MARKER_SOS         = 0xda, //Start Of Stream
40     JPEG_MARKER_APP1        = 0xe1, //Application Data 1 - for Exif
41     JPEG_MARKER_HIGHEST_ID  = 0xfe
42 };
43
44 struct JpegFileSection;
45 typedef std::shared_ptr<JpegFileSection> JpegFileSectionPtr;
46
47 struct JpegFileSection
48 {
49     JpegFileSection() :
50         type(JPEG_MARKER_UNKNOWN),
51         data_ptr(NULL),
52         size(0),
53         exif_data(NULL) {};
54
55     JpegMarker type;
56     unsigned char* data_ptr;
57     unsigned short size;
58
59     ExifData* exif_data;
60 };
61
62
63 class JpegFile;
64 typedef std::shared_ptr<JpegFile> JpegFilePtr;
65
66 class JpegFile {
67 public:
68     static JpegFilePtr loadFile(const std::string& path);
69     ~JpegFile();
70
71     void setNewExifData(ExifData* new_exif_data);
72
73     /**
74      * You are responsible to unreference returned data with: exif_data_unref(...)
75      * Example:
76      *     ExifData* ed = jpeg.getExifData();
77      *     if(ed) {
78      *         doSth(ed);
79      *         exif_data_unref(ed);
80      *     }
81      */
82     ExifData* getExifData();
83
84     void saveToFile(const std::string& out_path);
85
86 private:
87     JpegFile();
88     void load(const std::string& path);
89     void generateListOfSections();
90
91     std::string getPartOfFile(const size_t offset,
92             const size_t num_bytes_before = 10,
93             const size_t num_bytes_after = 10);
94
95     JpegFileSectionPtr getExifSection();
96     void saveToFilePriv(const std::string& out_path);
97
98     /**
99      * Search for first occurence of specific tag inside buffer.
100      *
101      * buffer_end is the first byte that should not be checked:
102      * [buffer_start ... buffer_end)
103      *
104      * For example EOI - search for first 'ffd9' in buffer
105      */
106     static bool searchForTagInBuffer(const unsigned char* buffer_start,
107             const unsigned char* buffer_end,
108             const JpegMarker marker,
109             size_t& out_index);
110
111     std::string m_source_file_path;
112
113     unsigned char* m_in_data;
114     size_t m_in_data_size;
115
116     unsigned char* m_image_data;
117     size_t m_image_size;
118
119     /**
120      * This contains any bytes after EOI.
121      * Usually there should be no extra bytes after EOI unfortunately
122      * some cameras saves extra bytes (for example Android).
123      */
124     unsigned char* m_padding_data;
125     size_t m_padding_data_size;
126
127     FILE* m_in_file;
128     FILE* m_out_file;
129
130     typedef std::vector<JpegFileSectionPtr> SectionsVec;
131     SectionsVec m_sections;
132 };
133
134
135 } // namespace Exif
136 } // namespace DeviceApi
137
138 #endif // __TIZEN_EXIF_JPEG_FILE_H_