Branch libreoffice-5-0-4
[LibreOffice.git] / vcl / source / filter / jpeg / Exif.cxx
blobfc59664192cb3ed1dae418465a9925e523e95148
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include "Exif.hxx"
21 #include <boost/scoped_array.hpp>
23 Exif::Exif() :
24 maOrientation(TOP_LEFT),
25 mbExifPresent(false)
28 Exif::~Exif()
32 void Exif::setOrientation(Orientation aOrientation) {
33 maOrientation = aOrientation;
36 Orientation Exif::convertToOrientation(sal_Int32 value)
38 switch(value) {
39 case 1: return TOP_LEFT;
40 case 2: return TOP_RIGHT;
41 case 3: return BOTTOM_RIGHT;
42 case 4: return BOTTOM_LEFT;
43 case 5: return LEFT_TOP;
44 case 6: return RIGHT_TOP;
45 case 7: return RIGHT_BOTTOM;
46 case 8: return LEFT_BOTTOM;
48 return TOP_LEFT;
51 sal_Int32 Exif::getRotation()
53 switch(maOrientation) {
54 case TOP_LEFT:
55 return 0;
56 case BOTTOM_RIGHT:
57 return 1800;
58 case RIGHT_TOP:
59 return 2700;
60 case LEFT_BOTTOM:
61 return 900;
62 default:
63 break;
65 return 0;
69 bool Exif::read(SvStream& rStream)
71 sal_Int32 nStreamPosition = rStream.Tell();
72 bool result = processJpeg(rStream, false);
73 rStream.Seek( nStreamPosition );
75 return result;
78 bool Exif::write(SvStream& rStream)
80 sal_Int32 nStreamPosition = rStream.Tell();
81 bool result = processJpeg(rStream, true);
82 rStream.Seek( nStreamPosition );
84 return result;
87 bool Exif::processJpeg(SvStream& rStream, bool bSetValue)
89 sal_uInt16 aMagic16;
90 sal_uInt16 aLength;
92 rStream.Seek(STREAM_SEEK_TO_END);
93 sal_uInt32 aSize = rStream.Tell();
94 rStream.Seek(STREAM_SEEK_TO_BEGIN);
96 rStream.SetEndian( SvStreamEndian::BIG );
97 rStream.ReadUInt16( aMagic16 );
99 // Compare JPEG magic bytes
100 if( 0xFFD8 != aMagic16 )
102 return false;
105 sal_uInt32 aPreviousPosition = STREAM_SEEK_TO_BEGIN;
107 while(true)
109 sal_uInt8 aMarker = 0xD9;
110 sal_Int32 aCount;
112 for (aCount = 0; aCount < 7; aCount++)
114 rStream.ReadUChar( aMarker );
115 if (aMarker != 0xFF)
117 break;
119 if (aCount >= 6)
121 return false;
125 rStream.ReadUInt16( aLength );
127 if (aLength < 8 || aLength > rStream.remainingSize())
129 return false;
132 if (aMarker == 0xE1)
134 return processExif(rStream, aLength, bSetValue);
136 else if (aMarker == 0xD9)
138 return false;
140 else
142 sal_uInt32 aCurrentPosition = rStream.SeekRel(aLength-1);
143 if (aCurrentPosition == aPreviousPosition || aCurrentPosition > aSize)
145 return false;
147 aPreviousPosition = aCurrentPosition;
150 return false;
153 bool Exif::processIFD(sal_uInt8* pExifData, sal_uInt16 aLength, sal_uInt16 aOffset, sal_uInt16 aNumberOfTags, bool bSetValue, bool bSwap)
155 ExifIFD* ifd = NULL;
157 while (aOffset <= aLength - 12 && aNumberOfTags > 0)
159 ifd = reinterpret_cast<ExifIFD*>(&pExifData[aOffset]);
160 sal_uInt16 tag = ifd->tag;
161 if (bSwap)
163 tag = OSL_SWAPWORD(ifd->tag);
166 if (tag == ORIENTATION)
168 if(bSetValue)
170 ifd->tag = ORIENTATION;
171 ifd->type = 3;
172 ifd->count = 1;
173 ifd->offset = maOrientation;
174 if (bSwap)
176 ifd->tag = OSL_SWAPWORD(ifd->tag);
177 ifd->offset = OSL_SWAPWORD(ifd->offset);
180 else
182 sal_uInt32 nIfdOffset = ifd->offset;
183 if (bSwap)
184 nIfdOffset = OSL_SWAPWORD(ifd->offset);
185 maOrientation = convertToOrientation(nIfdOffset);
189 aNumberOfTags--;
190 aOffset += 12;
192 return true;
195 bool Exif::processExif(SvStream& rStream, sal_uInt16 aSectionLength, bool bSetValue)
197 sal_uInt32 aMagic32;
198 sal_uInt16 aMagic16;
200 rStream.ReadUInt32( aMagic32 );
201 rStream.ReadUInt16( aMagic16 );
203 // Compare EXIF magic bytes
204 if( 0x45786966 != aMagic32 || 0x0000 != aMagic16)
206 return false;
209 sal_uInt16 aLength = aSectionLength - 6; // Length = Section - Header
211 boost::scoped_array<sal_uInt8> aExifData(new sal_uInt8[aLength]);
212 sal_uInt32 aExifDataBeginPosition = rStream.Tell();
214 rStream.Read(aExifData.get(), aLength);
216 // Exif detected
217 mbExifPresent = true;
219 TiffHeader* aTiffHeader = reinterpret_cast<TiffHeader*>(&aExifData[0]);
221 bool bIntel = aTiffHeader->byteOrder == 0x4949; //big-endian
222 bool bMotorola = aTiffHeader->byteOrder == 0x4D4D; //little-endian
224 if (!bIntel && !bMotorola)
226 return false;
229 bool bSwap = false;
231 #ifdef OSL_BIGENDIAN
232 if (bIntel)
233 bSwap = true;
234 #else
235 if (bMotorola)
236 bSwap = true;
237 #endif
239 if (bSwap)
241 aTiffHeader->tagAlign = OSL_SWAPWORD(aTiffHeader->tagAlign);
242 aTiffHeader->offset = OSL_SWAPDWORD(aTiffHeader->offset);
245 if (aTiffHeader->tagAlign != 0x002A) // TIFF tag
247 return false;
250 sal_uInt16 aOffset = aTiffHeader->offset;
252 sal_uInt16 aNumberOfTags = aExifData[aOffset];
253 if (bSwap)
255 aNumberOfTags = ((aExifData[aOffset] << 8) | aExifData[aOffset+1]);
258 processIFD(aExifData.get(), aLength, aOffset+2, aNumberOfTags, bSetValue, bSwap);
260 if (bSetValue)
262 rStream.Seek(aExifDataBeginPosition);
263 rStream.Write(aExifData.get(), aLength);
266 return true;
269 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */