1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #ifndef mozilla_image_Orientation_h
7 #define mozilla_image_Orientation_h
10 #include "mozilla/gfx/Rect.h"
14 // Pixel values in an image considering orientation metadata, such as the size
15 // of an image as seen by consumers of the image.
17 // Any public methods on RasterImage that use untyped units are interpreted as
19 struct OrientedPixel
{};
21 struct IsPixel
<OrientedPixel
> : std::true_type
{};
22 typedef gfx::IntPointTyped
<OrientedPixel
> OrientedIntPoint
;
23 typedef gfx::IntSizeTyped
<OrientedPixel
> OrientedIntSize
;
24 typedef gfx::IntRectTyped
<OrientedPixel
> OrientedIntRect
;
26 // Pixel values in an image ignoring orientation metadata, such as are stored
27 // in surfaces and the raw pixel data in the image.
28 struct UnorientedPixel
{};
30 struct IsPixel
<UnorientedPixel
> : std::true_type
{};
31 typedef gfx::IntPointTyped
<UnorientedPixel
> UnorientedIntPoint
;
32 typedef gfx::IntSizeTyped
<UnorientedPixel
> UnorientedIntSize
;
33 typedef gfx::IntRectTyped
<UnorientedPixel
> UnorientedIntRect
;
37 enum class Angle
: uint8_t { D0
, D90
, D180
, D270
};
39 enum class Flip
: uint8_t { Unflipped
, Horizontal
};
42 * A struct that describes an image's orientation as a rotation optionally
43 * followed by a reflection. This may be used to be indicate an image's inherent
44 * orientation or a desired orientation for the image.
46 * When flipFirst = true, this indicates that the reflection is applied before
47 * the rotation. (This is used by OrientedImage to represent the inverse of an
48 * underlying image's Orientation.)
51 explicit Orientation(Angle aRotation
= Angle::D0
,
52 Flip aFlip
= Flip::Unflipped
, bool aFlipFirst
= false)
53 : rotation(aRotation
), flip(aFlip
), flipFirst(aFlipFirst
) {}
55 Orientation
Reversed() const {
56 return Orientation(InvertAngle(rotation
), flip
, !flipFirst
);
59 bool IsIdentity() const {
60 return (rotation
== Angle::D0
) && (flip
== Flip::Unflipped
);
63 bool SwapsWidthAndHeight() const {
64 return (rotation
== Angle::D90
) || (rotation
== Angle::D270
);
67 bool operator==(const Orientation
& aOther
) const {
68 return rotation
== aOther
.rotation
&& flip
== aOther
.flip
&&
69 flipFirst
== aOther
.flipFirst
;
72 bool operator!=(const Orientation
& aOther
) const {
73 return !(*this == aOther
);
76 OrientedIntSize
ToOriented(const UnorientedIntSize
& aSize
) const {
77 if (SwapsWidthAndHeight()) {
78 return OrientedIntSize(aSize
.height
, aSize
.width
);
80 return OrientedIntSize(aSize
.width
, aSize
.height
);
84 UnorientedIntSize
ToUnoriented(const OrientedIntSize
& aSize
) const {
85 if (SwapsWidthAndHeight()) {
86 return UnorientedIntSize(aSize
.height
, aSize
.width
);
88 return UnorientedIntSize(aSize
.width
, aSize
.height
);
92 static Angle
InvertAngle(Angle aAngle
) {
109 } // namespace mozilla
111 #endif // mozilla_image_Orientation_h