LCOV - code coverage report
Current view: top level - vcl/source/filter/jpeg - Exif.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 0 124 0.0 %
Date: 2014-04-11 Functions: 0 13 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       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/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      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 .
      18             :  */
      19             : 
      20             : #include "Exif.hxx"
      21             : #include <boost/scoped_array.hpp>
      22             : 
      23           0 : Exif::Exif() :
      24             :     maOrientation(TOP_LEFT),
      25           0 :     mbExifPresent(false)
      26           0 : {}
      27             : 
      28           0 : Exif::~Exif()
      29           0 : {}
      30             : 
      31           0 : Orientation Exif::getOrientation() {
      32           0 :     return maOrientation;
      33             : }
      34             : 
      35           0 : void Exif::setOrientation(Orientation aOrientation) {
      36           0 :     maOrientation = aOrientation;
      37           0 : }
      38             : 
      39           0 : Orientation Exif::convertToOrientation(sal_Int32 value)
      40             : {
      41           0 :     switch(value) {
      42           0 :         case 1: return TOP_LEFT;
      43           0 :         case 2: return TOP_RIGHT;
      44           0 :         case 3: return BOTTOM_RIGHT;
      45           0 :         case 4: return BOTTOM_LEFT;
      46           0 :         case 5: return LEFT_TOP;
      47           0 :         case 6: return RIGHT_TOP;
      48           0 :         case 7: return RIGHT_BOTTOM;
      49           0 :         case 8: return LEFT_BOTTOM;
      50             :     }
      51           0 :     return TOP_LEFT;
      52             : }
      53             : 
      54           0 : sal_Int32 Exif::getRotation()
      55             : {
      56           0 :     switch(maOrientation) {
      57             :         case TOP_LEFT:
      58           0 :             return 0;
      59             :         case BOTTOM_RIGHT:
      60           0 :             return 1800;
      61             :         case RIGHT_TOP:
      62           0 :             return 2700;
      63             :         case LEFT_BOTTOM:
      64           0 :             return 900;
      65             :         default:
      66           0 :             break;
      67             :     }
      68           0 :     return 0;
      69             : }
      70             : 
      71           0 : bool Exif::hasExif()
      72             : {
      73           0 :     return mbExifPresent;
      74             : }
      75             : 
      76           0 : bool Exif::read(SvStream& rStream)
      77             : {
      78           0 :     sal_Int32 nStreamPosition = rStream.Tell();
      79           0 :     bool result = processJpeg(rStream, false);
      80           0 :     rStream.Seek( nStreamPosition );
      81             : 
      82           0 :     return result;
      83             : }
      84             : 
      85           0 : bool Exif::write(SvStream& rStream)
      86             : {
      87           0 :     sal_Int32 nStreamPosition = rStream.Tell();
      88           0 :     bool result = processJpeg(rStream, true);
      89           0 :     rStream.Seek( nStreamPosition );
      90             : 
      91           0 :     return result;
      92             : }
      93             : 
      94           0 : bool Exif::processJpeg(SvStream& rStream, bool bSetValue)
      95             : {
      96             :     sal_uInt16  aMagic16;
      97             :     sal_uInt16  aLength;
      98             : 
      99           0 :     rStream.Seek(STREAM_SEEK_TO_END);
     100           0 :     sal_uInt32 aSize = rStream.Tell();
     101           0 :     rStream.Seek(STREAM_SEEK_TO_BEGIN);
     102             : 
     103           0 :     rStream.SetNumberFormatInt( NUMBERFORMAT_INT_BIGENDIAN );
     104           0 :     rStream.ReadUInt16( aMagic16 );
     105             : 
     106             :     // Compare JPEG magic bytes
     107           0 :     if( 0xFFD8 != aMagic16 )
     108             :     {
     109           0 :         return false;
     110             :     }
     111             : 
     112           0 :     sal_uInt32 aPreviousPosition = STREAM_SEEK_TO_BEGIN;
     113             : 
     114             :     while(true)
     115             :     {
     116           0 :         sal_uInt8 aMarker = 0xD9;
     117             :         sal_Int32 aCount;
     118             : 
     119           0 :         for (aCount = 0; aCount < 7; aCount++)
     120             :         {
     121           0 :             rStream.ReadUChar( aMarker );
     122           0 :             if (aMarker != 0xFF)
     123             :             {
     124           0 :                 break;
     125             :             }
     126           0 :             if (aCount >= 6)
     127             :             {
     128           0 :                 return false;
     129             :             }
     130             :         }
     131             : 
     132           0 :         rStream.ReadUInt16( aLength );
     133             : 
     134           0 :         if (aLength < 8)
     135             :         {
     136           0 :             return false;
     137             :         }
     138             : 
     139           0 :         if (aMarker == 0xE1)
     140             :         {
     141           0 :             return processExif(rStream, aLength, bSetValue);
     142             :         }
     143           0 :         else if (aMarker == 0xD9)
     144             :         {
     145           0 :             return false;
     146             :         }
     147             :         else
     148             :         {
     149           0 :             sal_uInt32 aCurrentPosition = rStream.SeekRel(aLength-1);
     150           0 :             if (aCurrentPosition == aPreviousPosition || aCurrentPosition > aSize)
     151             :             {
     152           0 :                 return false;
     153             :             }
     154           0 :             aPreviousPosition = aCurrentPosition;
     155             :         }
     156             :     }
     157           0 :     return false;
     158             : }
     159             : 
     160           0 : bool Exif::processIFD(sal_uInt8* pExifData, sal_uInt16 aLength, sal_uInt16 aOffset, sal_uInt16 aNumberOfTags, bool bSetValue, bool bSwap)
     161             : {
     162           0 :     ExifIFD* ifd = NULL;
     163             : 
     164           0 :     while (aOffset <= aLength - 12 && aNumberOfTags > 0)
     165             :     {
     166           0 :         ifd = (ExifIFD*) &pExifData[aOffset];
     167           0 :         sal_uInt16 tag = ifd->tag;
     168           0 :         if (bSwap)
     169             :         {
     170           0 :             tag = OSL_SWAPWORD(ifd->tag);
     171             :         }
     172             : 
     173           0 :         if (tag == ORIENTATION)
     174             :         {
     175           0 :             if(bSetValue)
     176             :             {
     177           0 :                 ifd->tag = ORIENTATION;
     178           0 :                 ifd->type = 3;
     179           0 :                 ifd->count = 1;
     180           0 :                 ifd->offset = maOrientation;
     181           0 :                 if (bSwap)
     182             :                 {
     183           0 :                     ifd->tag = OSL_SWAPWORD(ifd->tag);
     184           0 :                     ifd->offset = OSL_SWAPWORD(ifd->offset);
     185             :                 }
     186             :             }
     187             :             else
     188             :             {
     189           0 :                 sal_uInt32 nIfdOffset = ifd->offset;
     190           0 :                 if (bSwap)
     191           0 :                     nIfdOffset = OSL_SWAPWORD(ifd->offset);
     192           0 :                 maOrientation = convertToOrientation(nIfdOffset);
     193             :             }
     194             :         }
     195             : 
     196           0 :         aNumberOfTags--;
     197           0 :         aOffset += 12;
     198             :     }
     199           0 :     return true;
     200             : }
     201             : 
     202           0 : bool Exif::processExif(SvStream& rStream, sal_uInt16 aSectionLength, bool bSetValue)
     203             : {
     204             :     sal_uInt32  aMagic32;
     205             :     sal_uInt16  aMagic16;
     206             : 
     207           0 :     rStream.ReadUInt32( aMagic32 );
     208           0 :     rStream.ReadUInt16( aMagic16 );
     209             : 
     210             :     // Compare EXIF magic bytes
     211           0 :     if( 0x45786966 != aMagic32 || 0x0000 != aMagic16)
     212             :     {
     213           0 :         return false;
     214             :     }
     215             : 
     216           0 :     sal_uInt16 aLength = aSectionLength - 6; // Length = Section - Header
     217             : 
     218           0 :     boost::scoped_array<sal_uInt8> aExifData(new sal_uInt8[aLength]);
     219           0 :     sal_uInt32 aExifDataBeginPosition = rStream.Tell();
     220             : 
     221           0 :     rStream.Read(aExifData.get(), aLength);
     222             : 
     223             :     // Exif detected
     224           0 :     mbExifPresent = true;
     225             : 
     226           0 :     TiffHeader* aTiffHeader = (TiffHeader*) &aExifData[0];
     227             : 
     228           0 :     bool bIntel = aTiffHeader->byteOrder == 0x4949;      //big-endian
     229           0 :     bool bMotorola = aTiffHeader->byteOrder == 0x4D4D;   //little-endian
     230             : 
     231           0 :     if (!bIntel && !bMotorola)
     232             :     {
     233           0 :         return false;
     234             :     }
     235             : 
     236           0 :     bool bSwap = false;
     237             : 
     238             : #ifdef OSL_BIGENDIAN
     239             :     if (bIntel)
     240             :         bSwap = true;
     241             : #else
     242           0 :     if (bMotorola)
     243           0 :         bSwap = true;
     244             : #endif
     245             : 
     246           0 :     if (bSwap)
     247             :     {
     248           0 :         aTiffHeader->tagAlign = OSL_SWAPWORD(aTiffHeader->tagAlign);
     249           0 :         aTiffHeader->offset = OSL_SWAPDWORD(aTiffHeader->offset);
     250             :     }
     251             : 
     252           0 :     if (aTiffHeader->tagAlign != 0x002A) // TIFF tag
     253             :     {
     254           0 :         return false;
     255             :     }
     256             : 
     257           0 :     sal_uInt16 aOffset = aTiffHeader->offset;
     258             : 
     259           0 :     sal_uInt16 aNumberOfTags = aExifData[aOffset];
     260           0 :     if (bSwap)
     261             :     {
     262           0 :         aNumberOfTags = ((aExifData[aOffset] << 8) | aExifData[aOffset+1]);
     263             :     }
     264             : 
     265           0 :     processIFD(aExifData.get(), aLength, aOffset+2, aNumberOfTags, bSetValue, bSwap);
     266             : 
     267           0 :     if (bSetValue)
     268             :     {
     269           0 :         rStream.Seek(aExifDataBeginPosition);
     270           0 :         rStream.Write(aExifData.get(), aLength);
     271             :     }
     272             : 
     273           0 :     return true;
     274             : }
     275             : 
     276             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10