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

Generated by: LCOV version 1.10