| 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 | namespace WebCore { |
| 30 | |
| 31 | class AffineTransform; |
| 32 | class FloatSize; |
| 33 | |
| 34 | // This enum intentionally matches the orientation values from the EXIF spec. |
| 35 | // See JEITA CP-3451, page 18. http://www.exif.org/Exif2-2.PDF |
| 36 | enum ImageOrientationEnum { |
| 37 | // "TopLeft" means that the 0 row starts at the Top, the 0 column starts at the Left. |
| 38 | OriginTopLeft = 1, // default |
| 39 | OriginTopRight = 2, // mirror along y-axis |
| 40 | OriginBottomRight = 3, // 180 degree rotation |
| 41 | OriginBottomLeft = 4, // mirror along the x-axis |
| 42 | OriginLeftTop = 5, // mirror along x-axis + 270 degree CW rotation |
| 43 | OriginRightTop = 6, // 90 degree CW rotation |
| 44 | OriginRightBottom = 7, // mirror along x-axis + 90 degree CW rotation |
| 45 | OriginLeftBottom = 8, // 270 degree CW rotation |
| 46 | // All other values are "reserved" as of EXIF 2.2 |
| 47 | DefaultImageOrientation = OriginTopLeft, |
| 48 | }; |
| 49 | |
| 50 | enum RespectImageOrientationEnum { |
| 51 | DoNotRespectImageOrientation = 0, |
| 52 | RespectImageOrientation = 1 |
| 53 | }; |
| 54 | |
| 55 | struct ImageOrientationDescription { |
| 56 | ImageOrientationDescription(RespectImageOrientationEnum shouldRespectImageOrientation, |
| 57 | ImageOrientationEnum orientation) |
| 58 | : m_respectOrientation(shouldRespectImageOrientation) |
| 59 | , m_orientation(orientation) |
| 60 | { |
| 61 | } |
| 62 | |
| 63 | ImageOrientationDescription() |
| 64 | : m_respectOrientation(DoNotRespectImageOrientation) |
| 65 | , m_orientation(DefaultImageOrientation) |
| 66 | { |
| 67 | } |
| 68 | |
| 69 | ImageOrientationDescription(RespectImageOrientationEnum shouldRespectImageOrientation) |
| 70 | : m_respectOrientation(shouldRespectImageOrientation) |
| 71 | , m_orientation(DefaultImageOrientation) |
| 72 | { |
| 73 | } |
| 74 | |
| 75 | void setRespectImageOrientation(RespectImageOrientationEnum shouldRespectImageOrientation) { m_respectOrientation = shouldRespectImageOrientation; } |
| 76 | RespectImageOrientationEnum respectImageOrientation() { return m_respectOrientation; } |
| 77 | |
| 78 | void setImageOrientationEnum(ImageOrientationEnum orientation) { m_orientation = orientation; } |
| 79 | ImageOrientationEnum imageOrientation() { return m_orientation; } |
| 80 | |
| 81 | RespectImageOrientationEnum m_respectOrientation; |
| 82 | ImageOrientationEnum m_orientation; |
| 83 | }; |
| 84 | |
| 85 | class ImageOrientation { |
| 86 | public: |
| 87 | ImageOrientation() |
| 88 | : m_orientation(DefaultImageOrientation) |
| 89 | { |
| 90 | } |
| 91 | |
| 92 | explicit ImageOrientation(ImageOrientationEnum orientation) |
| 93 | : m_orientation(orientation) |
| 94 | { |
| 95 | } |
| 96 | |
| 97 | bool usesWidthAsHeight() const |
| 98 | { |
| 99 | // Values 5 through 8 all flip the width/height. |
| 100 | return m_orientation >= OriginLeftTop; |
| 101 | } |
| 102 | |
| 103 | // ImageOrientationEnum currently matches EXIF values, however code outside |
| 104 | // this function should never assume that. |
| 105 | static ImageOrientation fromEXIFValue(int exifValue) |
| 106 | { |
| 107 | // Values direct from images may be invalid, in which case we use the default. |
| 108 | if (exifValue < OriginTopLeft || exifValue > OriginLeftBottom) |
| 109 | return ImageOrientation(); |
| 110 | return ImageOrientation(static_cast<ImageOrientationEnum>(exifValue)); |
| 111 | } |
| 112 | |
| 113 | // This transform can be used for drawing an image according to the orientation. |
| 114 | // It should be used in a right-handed coordinate system. |
| 115 | AffineTransform transformFromDefault(const FloatSize& drawnSize) const; |
| 116 | |
| 117 | inline operator ImageOrientationEnum() const { return m_orientation; } |
| 118 | |
| 119 | inline bool operator==(const ImageOrientation& other) const { return other.m_orientation == m_orientation; } |
| 120 | inline bool operator!=(const ImageOrientation& other) const { return !(*this == other); } |
| 121 | |
| 122 | private: |
| 123 | // FIXME: This only needs to be one byte. |
| 124 | ImageOrientationEnum m_orientation; |
| 125 | }; |
| 126 | |
| 127 | } // namespace WebCore |
| 128 | |
| 129 | #endif // ImageOrientation_h |
| 130 | |