2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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.
18 #include "ContentExif.h"
25 ContentExif::ContentExif(std::string path)
27 fp = fopen(path.c_str(), "rb+");
31 ContentExif::~ContentExif()
43 unsigned char ContentExif::nextByte()
48 int ContentExif::read_1_byte (void)
58 /* Read 2 bytes, convert to unsigned int */
59 /* All 2-byte quantities in JPEG markers are MSB first */
60 unsigned int ContentExif::read_2_bytes (void)
67 return (((unsigned int) c1) << 8) + ((unsigned int) c2);
70 int ContentExif::LodExif()
74 if(fp == NULL) return 0;
76 for (int i = 0; i < sizeof(soi); i++)
77 soi[i] = (unsigned char) read_1_byte();
79 //check soi tag for jpeg
80 if (soi[0]!=0xFF || soi[1]!=0xD8)
82 LogDebug("can not find SOI tag");
90 for( int i = 0; i < sizeof(app); i++)
91 app[i] = (unsigned char) read_1_byte();
93 printf("marker:%02x %02x \n",app[0],app[1]);
94 if(app[0] == 0xFF && app[1] == 0xE1)
96 else if(app[0] != 0xFF || !(app[1]==0xEE ||app[1]==0xE0))
98 LogDebug("Not supported image type.");
102 unsigned int marker_size = read_2_bytes();
104 for( int i =0 ; i < marker_size - 2; i++)
108 unsigned int len = read_2_bytes() - 8;
111 LogDebug("Wrong Image");
116 unsigned char exif[6];
117 for (int i = 0; i < 6; i++)
118 exif[i] = (unsigned char) read_1_byte();
120 if (exif[0] != 0x45 || exif[1] != 0x78 || exif[2] != 0x69 ||exif[3] != 0x66)
122 LogDebug("can not find EXIF tag");
126 m_exifBuf = (unsigned char*)calloc(len,sizeof(unsigned char));
128 fread(m_exifBuf, 1, len,fp);
130 /* check endian type */
131 if (m_exifBuf[0] == 0x49 && m_exifBuf[1] == 0x49)
133 m_endianType = false;
135 else if (m_exifBuf[0] == 0x4D && m_exifBuf[1] == 0x4D)
141 LogDebug("can not find endian tag");
145 /* Get first IFD offset (offset to IFD0) */
148 if (m_exifBuf[4] != 0) return 0;
149 if (m_exifBuf[5] != 0) return 0;
150 m_offset = m_exifBuf[6];
152 m_offset += m_exifBuf[7];
156 if (m_exifBuf[7] != 0) return 0;
157 if (m_exifBuf[6] != 0) return 0;
158 m_offset = m_exifBuf[5];
160 m_offset += m_exifBuf[4];
163 if (m_offset > len - 2) return 0; /* check end of data segment */
165 /* Get the number of directory entries contained in this IFD */
168 m_tagCount = m_exifBuf[m_offset];
170 m_tagCount += m_exifBuf[m_offset+1];
174 m_tagCount = m_exifBuf[m_offset+1];
176 m_tagCount += m_exifBuf[m_offset];
178 if (m_tagCount == 0) return 0;
184 bool ContentExif::SetExifChar(unsigned int tag, unsigned char value)
187 bool isfound = false;
188 for(unsigned int i = 0; i < m_tagCount; i++)
192 _tag = m_exifBuf[m_offset];
194 _tag += m_exifBuf[m_offset+1];
198 _tag = m_exifBuf[m_offset+1];
200 _tag += m_exifBuf[m_offset];
214 m_exifBuf[m_offset+2] = 0; /* Format = unsigned short (2 octets) */
215 m_exifBuf[m_offset+3] = 3;
216 m_exifBuf[m_offset+4] = 0; /* Number Of Components = 1 */
217 m_exifBuf[m_offset+5] = 0;
218 m_exifBuf[m_offset+6] = 0;
219 m_exifBuf[m_offset+7] = 1;
220 m_exifBuf[m_offset+8] = 0;
221 m_exifBuf[m_offset+9] = value;
222 m_exifBuf[m_offset+10] = 0;
223 m_exifBuf[m_offset+11] = 0;
225 m_exifBuf[m_offset+2] = 3; /* Format = unsigned short (2 octets) */
226 m_exifBuf[m_offset+3] = 0;
227 m_exifBuf[m_offset+4] = 1; /* Number Of Components = 1 */
228 m_exifBuf[m_offset+5] = 0;
229 m_exifBuf[m_offset+6] = 0;
230 m_exifBuf[m_offset+7] = 0;
231 m_exifBuf[m_offset+8] = value;
232 m_exifBuf[m_offset+9] = 0;
233 m_exifBuf[m_offset+10] = 0;
234 m_exifBuf[m_offset+11] = 0;
237 fseek(fp, (4 + 2 + 6 + 2) + m_offset, SEEK_SET);
238 fwrite(m_exifBuf + 2 + m_offset, 1, 10, fp);