1 /* 2 * Copyright (C) 2010 Google Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY 15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 17 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY 18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 20 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 21 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 */ 25 26 #ifndef ImageOrientation_h 27 #define ImageOrientation_h 28 29 #include "platform/PlatformExport.h" 30 31 namespace WebCore { 32 33 class AffineTransform; 34 class FloatSize; 35 36 // This enum intentionally matches the orientation values from the EXIF spec. 37 // See JEITA CP-3451, page 18. http://www.exif.org/Exif2-2.PDF 38 enum ImageOrientationEnum { 39 // "TopLeft" means that the 0 row starts at the Top, the 0 column starts at the Left. 40 OriginTopLeft = 1, // default 41 OriginTopRight = 2, // mirror along y-axis 42 OriginBottomRight = 3, // 180 degree rotation 43 OriginBottomLeft = 4, // mirror along the x-axis 44 OriginLeftTop = 5, // mirror along x-axis + 270 degree CW rotation 45 OriginRightTop = 6, // 90 degree CW rotation 46 OriginRightBottom = 7, // mirror along x-axis + 90 degree CW rotation 47 OriginLeftBottom = 8, // 270 degree CW rotation 48 // All other values are "reserved" as of EXIF 2.2 49 DefaultImageOrientation = OriginTopLeft, 50 }; 51 52 enum RespectImageOrientationEnum { 53 DoNotRespectImageOrientation = 0, 54 RespectImageOrientation = 1 55 }; 56 57 class PLATFORM_EXPORT ImageOrientation { 58 public: 59 ImageOrientation(ImageOrientationEnum orientation = DefaultImageOrientation) 60 : m_orientation(orientation) 61 { 62 } 63 64 bool usesWidthAsHeight() const 65 { 66 // Values 5 through 8 all flip the width/height. 67 return m_orientation >= OriginLeftTop; 68 } 69 70 // ImageOrientationEnum currently matches EXIF values, however code outside 71 // this function should never assume that. 72 static ImageOrientation fromEXIFValue(int exifValue) 73 { 74 // Values direct from images may be invalid, in which case we use the default. 75 if (exifValue < OriginTopLeft || exifValue > OriginLeftBottom) 76 return DefaultImageOrientation; 77 return static_cast<ImageOrientationEnum>(exifValue); 78 } 79 80 // This transform can be used for drawing an image according to the orientation. 81 // It should be used in a right-handed coordinate system. 82 AffineTransform transformFromDefault(const FloatSize& drawnSize) const; 83 84 inline bool operator==(const ImageOrientation& other) const { return other.m_orientation == m_orientation; } 85 inline bool operator!=(const ImageOrientation& other) const { return !(*this == other); } 86 87 private: 88 // FIXME: This only needs to be one byte. 89 ImageOrientationEnum m_orientation; 90 }; 91 92 } // namespace WebCore 93 94 #endif // ImageOrientation_h 95