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_Resolution_h 7 #define mozilla_image_Resolution_h 8 9 #include "mozilla/Assertions.h" 10 #include <cmath> 11 12 namespace mozilla { 13 namespace image { 14 15 /** 16 * The resolution of an image, in dppx. 17 */ 18 struct Resolution { 19 Resolution() = default; ResolutionResolution20 Resolution(float aX, float aY) : mX(aX), mY(aY) { 21 MOZ_ASSERT(mX != 0.0f); 22 MOZ_ASSERT(mY != 0.0f); 23 } 24 25 bool operator==(const Resolution& aOther) const { 26 return mX == aOther.mX && mY == aOther.mY; 27 } 28 bool operator!=(const Resolution& aOther) const { return !(*this == aOther); } 29 30 float mX = 1.0f; 31 float mY = 1.0f; 32 ScaleByResolution33 void ScaleBy(float aScale) { 34 if (MOZ_LIKELY(aScale != 0.0f)) { 35 mX *= aScale; 36 mY *= aScale; 37 } 38 } 39 ApplyXToResolution40 void ApplyXTo(int32_t& aWidth) const { 41 if (mX != 1.0f) { 42 aWidth = std::round(float(aWidth) / mX); 43 } 44 } 45 ApplyXToResolution46 void ApplyXTo(float& aWidth) const { 47 if (mX != 1.0f) { 48 aWidth /= mX; 49 } 50 } 51 ApplyYToResolution52 void ApplyYTo(int32_t& aHeight) const { 53 if (mY != 1.0f) { 54 aHeight = std::round(float(aHeight) / mY); 55 } 56 } 57 ApplyYToResolution58 void ApplyYTo(float& aHeight) const { 59 if (mY != 1.0f) { 60 aHeight /= mY; 61 } 62 } 63 ApplyToResolution64 void ApplyTo(int32_t& aWidth, int32_t& aHeight) const { 65 ApplyXTo(aWidth); 66 ApplyYTo(aHeight); 67 } 68 ApplyToResolution69 void ApplyTo(float& aWidth, float& aHeight) const { 70 ApplyXTo(aWidth); 71 ApplyYTo(aHeight); 72 } 73 ApplyInverseToResolution74 void ApplyInverseTo(int32_t& aWidth, int32_t& aHeight) { 75 if (mX != 1.0f) { 76 aWidth = std::round(float(aWidth) * mX); 77 } 78 if (mY != 1.0f) { 79 aHeight = std::round(float(aHeight) * mY); 80 } 81 } 82 }; 83 84 } // namespace image 85 86 using ImageResolution = image::Resolution; 87 88 } // namespace mozilla 89 90 #endif 91