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 THIRD_PARTY_BLINK_RENDERER_PLATFORM_GRAPHICS_IMAGE_ORIENTATION_H_
27 #define THIRD_PARTY_BLINK_RENDERER_PLATFORM_GRAPHICS_IMAGE_ORIENTATION_H_
28 
29 #include "third_party/blink/renderer/platform/platform_export.h"
30 #include "third_party/blink/renderer/platform/wtf/allocator/allocator.h"
31 
32 namespace blink {
33 
34 class AffineTransform;
35 class FloatSize;
36 
37 // This enum intentionally matches the orientation values from the EXIF spec.
38 // See JEITA CP-3451, page 18. http://www.exif.org/Exif2-2.PDF
39 enum ImageOrientationEnum {
40   // "TopLeft" means that the 0 row starts at the Top, the 0 column starts at
41   // the Left.
42   kOriginTopLeft = 1,      // default
43   kOriginTopRight = 2,     // mirror along y-axis
44   kOriginBottomRight = 3,  // 180 degree rotation
45   kOriginBottomLeft = 4,   // mirror along the x-axis
46   kOriginLeftTop = 5,      // mirror along x-axis + 270 degree CW rotation
47   kOriginRightTop = 6,     // 90 degree CW rotation
48   kOriginRightBottom = 7,  // mirror along x-axis + 90 degree CW rotation
49   kOriginLeftBottom = 8,   // 270 degree CW rotation
50   // All other values are "reserved" as of EXIF 2.2
51   kDefaultImageOrientation = kOriginTopLeft,
52   kImageOrientationEnumEnd = kOriginLeftBottom + 1,
53 };
54 
55 enum RespectImageOrientationEnum {
56   kDoNotRespectImageOrientation = 0,
57   kRespectImageOrientation = 1
58 };
59 
60 class PLATFORM_EXPORT ImageOrientation final {
61   DISALLOW_NEW();
62 
63  public:
64   ImageOrientation(ImageOrientationEnum orientation = kDefaultImageOrientation)
orientation_(orientation)65       : orientation_(orientation) {}
66 
UsesWidthAsHeight()67   bool UsesWidthAsHeight() const {
68     // Values 5 through 8 all flip the width/height.
69     return orientation_ >= kOriginLeftTop;
70   }
71 
72   // ImageOrientationEnum currently matches EXIF values, however code outside
73   // this function should never assume that.
FromEXIFValue(int exif_value)74   static ImageOrientation FromEXIFValue(int exif_value) {
75     // Values direct from images may be invalid, in which case we use the
76     // default.
77     if (exif_value < kOriginTopLeft || exif_value > kOriginLeftBottom)
78       return kDefaultImageOrientation;
79     return static_cast<ImageOrientationEnum>(exif_value);
80   }
81 
82   // This transform can be used for drawing an image according to the
83   // orientation. It should be used in a right-handed coordinate system.
84   AffineTransform TransformFromDefault(const FloatSize& drawn_size) const;
85 
86   inline bool operator==(const ImageOrientation& other) const {
87     return other.orientation_ == orientation_;
88   }
89   inline bool operator!=(const ImageOrientation& other) const {
90     return !(*this == other);
91   }
92 
Orientation()93   ImageOrientationEnum Orientation() const { return orientation_; }
94 
95  private:
96   // FIXME: This only needs to be one byte.
97   ImageOrientationEnum orientation_;
98 };
99 
100 }  // namespace blink
101 
102 #endif  // THIRD_PARTY_BLINK_RENDERER_PLATFORM_GRAPHICS_IMAGE_ORIENTATION_H_
103