tor-browser

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

ImageRegion.h (8600B)


      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_ImageRegion_h
      7 #define mozilla_image_ImageRegion_h
      8 
      9 #include "gfxMatrix.h"
     10 #include "gfxPoint.h"
     11 #include "gfxRect.h"
     12 #include "gfxTypes.h"
     13 #include "mozilla/gfx/Matrix.h"
     14 #include "mozilla/gfx/Types.h"
     15 #include "nsSize.h"
     16 #include "PLDHashTable.h"  // for PLDHashNumber
     17 
     18 namespace mozilla {
     19 namespace image {
     20 
     21 /**
     22 * An axis-aligned rectangle in tiled image space, with an optional sampling
     23 * restriction rect. The drawing code ensures that if a sampling restriction
     24 * rect is present, any pixels sampled during the drawing process are found
     25 * within that rect.
     26 *
     27 * The sampling restriction rect exists primarily for callers which perform
     28 * pixel snapping. Other callers should generally use one of the Create()
     29 * overloads.
     30 */
     31 class ImageRegion {
     32  typedef mozilla::gfx::ExtendMode ExtendMode;
     33 
     34 public:
     35  static ImageRegion Empty() {
     36    return ImageRegion(gfxRect(), ExtendMode::CLAMP);
     37  }
     38 
     39  static ImageRegion Create(const gfxRect& aRect,
     40                            ExtendMode aExtendMode = ExtendMode::CLAMP) {
     41    return ImageRegion(aRect, aExtendMode);
     42  }
     43 
     44  static ImageRegion Create(const gfxSize& aSize,
     45                            ExtendMode aExtendMode = ExtendMode::CLAMP) {
     46    return ImageRegion(gfxRect(0, 0, aSize.width, aSize.height), aExtendMode);
     47  }
     48 
     49  static ImageRegion Create(const nsIntSize& aSize,
     50                            ExtendMode aExtendMode = ExtendMode::CLAMP) {
     51    return ImageRegion(gfxRect(0, 0, aSize.width, aSize.height), aExtendMode);
     52  }
     53 
     54  static ImageRegion CreateWithSamplingRestriction(
     55      const gfxRect& aRect, const gfxRect& aRestriction,
     56      ExtendMode aExtendMode = ExtendMode::CLAMP) {
     57    return ImageRegion(aRect, aRestriction, aExtendMode);
     58  }
     59 
     60  bool IsRestricted() const { return mIsRestricted; }
     61  const gfxRect& Rect() const { return mRect; }
     62 
     63  const gfxRect& Restriction() const {
     64    MOZ_ASSERT(mIsRestricted);
     65    return mRestriction;
     66  }
     67 
     68  bool RestrictionContains(const gfxRect& aRect) const {
     69    if (!mIsRestricted) {
     70      return true;
     71    }
     72    return mRestriction.Contains(aRect);
     73  }
     74 
     75  ImageRegion Intersect(const gfxRect& aRect) const {
     76    if (mIsRestricted) {
     77      return CreateWithSamplingRestriction(aRect.Intersect(mRect),
     78                                           aRect.Intersect(mRestriction));
     79    }
     80    return Create(aRect.Intersect(mRect));
     81  }
     82 
     83  gfxRect IntersectAndRestrict(const gfxRect& aRect) const {
     84    gfxRect intersection = mRect.Intersect(aRect);
     85    if (mIsRestricted) {
     86      intersection = mRestriction.Intersect(intersection);
     87    }
     88    return intersection;
     89  }
     90 
     91  void MoveBy(gfxFloat dx, gfxFloat dy) {
     92    mRect.MoveBy(dx, dy);
     93    if (mIsRestricted) {
     94      mRestriction.MoveBy(dx, dy);
     95    }
     96  }
     97 
     98  void Scale(gfxFloat sx, gfxFloat sy) {
     99    mRect.Scale(sx, sy);
    100    if (mIsRestricted) {
    101      mRestriction.Scale(sx, sy);
    102    }
    103  }
    104 
    105  void TransformBy(const gfxMatrix& aMatrix) {
    106    mRect = aMatrix.TransformRect(mRect);
    107    if (mIsRestricted) {
    108      mRestriction = aMatrix.TransformRect(mRestriction);
    109    }
    110  }
    111 
    112  void TransformBoundsBy(const gfxMatrix& aMatrix) {
    113    mRect = aMatrix.TransformBounds(mRect);
    114    if (mIsRestricted) {
    115      mRestriction = aMatrix.TransformBounds(mRestriction);
    116    }
    117  }
    118 
    119  ImageRegion operator-(const gfxPoint& aPt) const {
    120    if (mIsRestricted) {
    121      return CreateWithSamplingRestriction(mRect - aPt, mRestriction - aPt);
    122    }
    123    return Create(mRect - aPt);
    124  }
    125 
    126  ImageRegion operator+(const gfxPoint& aPt) const {
    127    if (mIsRestricted) {
    128      return CreateWithSamplingRestriction(mRect + aPt, mRestriction + aPt);
    129    }
    130    return Create(mRect + aPt);
    131  }
    132 
    133  gfx::ExtendMode GetExtendMode() const { return mExtendMode; }
    134 
    135  /* ImageRegion() : mIsRestricted(false) { } */
    136 
    137 private:
    138  explicit ImageRegion(const gfxRect& aRect, ExtendMode aExtendMode)
    139      : mRect(aRect), mExtendMode(aExtendMode), mIsRestricted(false) {}
    140 
    141  ImageRegion(const gfxRect& aRect, const gfxRect& aRestriction,
    142              ExtendMode aExtendMode)
    143      : mRect(aRect),
    144        mRestriction(aRestriction),
    145        mExtendMode(aExtendMode),
    146        mIsRestricted(true) {}
    147 
    148  gfxRect mRect;
    149  gfxRect mRestriction;
    150  ExtendMode mExtendMode;
    151  bool mIsRestricted;
    152 };
    153 
    154 /**
    155 * An axis-aligned rectangle in tiled image space, with an optional sampling
    156 * restriction rect. The drawing code ensures that if a sampling restriction
    157 * rect is present, any pixels sampled during the drawing process are found
    158 * within that rect.
    159 *
    160 * The sampling restriction rect exists primarily for callers which perform
    161 * pixel snapping. Other callers should generally use one of the Create()
    162 * overloads.
    163 */
    164 class ImageIntRegion {
    165  typedef mozilla::gfx::ExtendMode ExtendMode;
    166 
    167 public:
    168  static ImageIntRegion Empty() {
    169    return ImageIntRegion(mozilla::gfx::IntRect(), ExtendMode::CLAMP);
    170  }
    171 
    172  static ImageIntRegion Create(const mozilla::gfx::IntRect& aRect,
    173                               ExtendMode aExtendMode = ExtendMode::CLAMP) {
    174    return ImageIntRegion(aRect, aExtendMode);
    175  }
    176 
    177  static ImageIntRegion Create(const mozilla::gfx::IntSize& aSize,
    178                               ExtendMode aExtendMode = ExtendMode::CLAMP) {
    179    return ImageIntRegion(
    180        mozilla::gfx::IntRect(0, 0, aSize.width, aSize.height), aExtendMode);
    181  }
    182 
    183  static ImageIntRegion CreateWithSamplingRestriction(
    184      const mozilla::gfx::IntRect& aRect,
    185      const mozilla::gfx::IntRect& aRestriction,
    186      ExtendMode aExtendMode = ExtendMode::CLAMP) {
    187    return ImageIntRegion(aRect, aRestriction, aExtendMode);
    188  }
    189 
    190  bool IsRestricted() const { return mIsRestricted; }
    191  const mozilla::gfx::IntRect& Rect() const { return mRect; }
    192 
    193  const mozilla::gfx::IntRect& Restriction() const {
    194    MOZ_ASSERT(mIsRestricted);
    195    return mRestriction;
    196  }
    197 
    198  bool RestrictionContains(const mozilla::gfx::IntRect& aRect) const {
    199    if (!mIsRestricted) {
    200      return true;
    201    }
    202    return mRestriction.Contains(aRect);
    203  }
    204 
    205  ImageIntRegion Intersect(const mozilla::gfx::IntRect& aRect) const {
    206    if (mIsRestricted) {
    207      return CreateWithSamplingRestriction(aRect.Intersect(mRect),
    208                                           aRect.Intersect(mRestriction));
    209    }
    210    return Create(aRect.Intersect(mRect));
    211  }
    212 
    213  mozilla::gfx::IntRect IntersectAndRestrict(
    214      const mozilla::gfx::IntRect& aRect) const {
    215    mozilla::gfx::IntRect intersection = mRect.Intersect(aRect);
    216    if (mIsRestricted) {
    217      intersection = mRestriction.Intersect(intersection);
    218    }
    219    return intersection;
    220  }
    221 
    222  gfx::ExtendMode GetExtendMode() const { return mExtendMode; }
    223 
    224  ImageRegion ToImageRegion() const {
    225    if (mIsRestricted) {
    226      return ImageRegion::CreateWithSamplingRestriction(
    227          gfxRect(mRect.x, mRect.y, mRect.width, mRect.height),
    228          gfxRect(mRestriction.x, mRestriction.y, mRestriction.width,
    229                  mRestriction.height),
    230          mExtendMode);
    231    }
    232    return ImageRegion::Create(
    233        gfxRect(mRect.x, mRect.y, mRect.width, mRect.height), mExtendMode);
    234  }
    235 
    236  bool operator==(const ImageIntRegion& aOther) const {
    237    return mExtendMode == aOther.mExtendMode &&
    238           mIsRestricted == aOther.mIsRestricted &&
    239           mRect.IsEqualEdges(aOther.mRect) &&
    240           (!mIsRestricted || mRestriction.IsEqualEdges(aOther.mRestriction));
    241  }
    242 
    243  PLDHashNumber Hash() const {
    244    return HashGeneric(mRect.x, mRect.y, mRect.width, mRect.height,
    245                       mRestriction.x, mRestriction.y, mRestriction.width,
    246                       mRestriction.height, mExtendMode, mIsRestricted);
    247  }
    248 
    249  /* ImageIntRegion() : mIsRestricted(false) { } */
    250 
    251 private:
    252  explicit ImageIntRegion(const mozilla::gfx::IntRect& aRect,
    253                          ExtendMode aExtendMode)
    254      : mRect(aRect), mExtendMode(aExtendMode), mIsRestricted(false) {}
    255 
    256  ImageIntRegion(const mozilla::gfx::IntRect& aRect,
    257                 const mozilla::gfx::IntRect& aRestriction,
    258                 ExtendMode aExtendMode)
    259      : mRect(aRect),
    260        mRestriction(aRestriction),
    261        mExtendMode(aExtendMode),
    262        mIsRestricted(true) {}
    263 
    264  mozilla::gfx::IntRect mRect;
    265  mozilla::gfx::IntRect mRestriction;
    266  ExtendMode mExtendMode;
    267  bool mIsRestricted;
    268 };
    269 
    270 }  // namespace image
    271 }  // namespace mozilla
    272 
    273 #endif  // mozilla_image_ImageRegion_h