tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

Orientation.h (3503B)


      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/. */
      5 
      6 #ifndef mozilla_image_Orientation_h
      7 #define mozilla_image_Orientation_h
      8 
      9 #include <stdint.h>
     10 #include "mozilla/gfx/Rect.h"
     11 
     12 namespace mozilla {
     13 
     14 // Pixel values in an image considering orientation metadata, such as the size
     15 // of an image as seen by consumers of the image.
     16 //
     17 // Any public methods on RasterImage that use untyped units are interpreted as
     18 // oriented pixels.
     19 struct OrientedPixel {};
     20 template <>
     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;
     25 
     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 {};
     29 template <>
     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;
     34 
     35 namespace image {
     36 
     37 enum class Angle : uint8_t { D0, D90, D180, D270 };
     38 
     39 enum class Flip : uint8_t { Unflipped, Horizontal };
     40 
     41 /**
     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.
     45 *
     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.)
     49 */
     50 struct Orientation {
     51  explicit Orientation(Angle aRotation = Angle::D0,
     52                       Flip aFlip = Flip::Unflipped, bool aFlipFirst = false)
     53      : rotation(aRotation), flip(aFlip), flipFirst(aFlipFirst) {}
     54 
     55  Orientation Reversed() const {
     56    return Orientation(InvertAngle(rotation), flip, !flipFirst);
     57  }
     58 
     59  bool IsIdentity() const {
     60    return (rotation == Angle::D0) && (flip == Flip::Unflipped);
     61  }
     62 
     63  bool SwapsWidthAndHeight() const {
     64    return (rotation == Angle::D90) || (rotation == Angle::D270);
     65  }
     66 
     67  bool operator==(const Orientation& aOther) const {
     68    return rotation == aOther.rotation && flip == aOther.flip &&
     69           flipFirst == aOther.flipFirst;
     70  }
     71 
     72  bool operator!=(const Orientation& aOther) const {
     73    return !(*this == aOther);
     74  }
     75 
     76  OrientedIntSize ToOriented(const UnorientedIntSize& aSize) const {
     77    if (SwapsWidthAndHeight()) {
     78      return OrientedIntSize(aSize.height, aSize.width);
     79    } else {
     80      return OrientedIntSize(aSize.width, aSize.height);
     81    }
     82  }
     83 
     84  UnorientedIntSize ToUnoriented(const OrientedIntSize& aSize) const {
     85    if (SwapsWidthAndHeight()) {
     86      return UnorientedIntSize(aSize.height, aSize.width);
     87    } else {
     88      return UnorientedIntSize(aSize.width, aSize.height);
     89    }
     90  }
     91 
     92  static Angle InvertAngle(Angle aAngle) {
     93    switch (aAngle) {
     94      case Angle::D90:
     95        return Angle::D270;
     96      case Angle::D270:
     97        return Angle::D90;
     98      default:
     99        return aAngle;
    100    }
    101  }
    102 
    103  Angle rotation;
    104  Flip flip;
    105  bool flipFirst;
    106 };
    107 
    108 }  // namespace image
    109 }  // namespace mozilla
    110 
    111 #endif  // mozilla_image_Orientation_h