• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C)2011, 2018 D. R. Commander.  All Rights Reserved.
3  * Copyright (C)2015 Viktor Szathmáry.  All Rights Reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * - Redistributions of source code must retain the above copyright notice,
9  *   this list of conditions and the following disclaimer.
10  * - Redistributions in binary form must reproduce the above copyright notice,
11  *   this list of conditions and the following disclaimer in the documentation
12  *   and/or other materials provided with the distribution.
13  * - Neither the name of the libjpeg-turbo Project nor the names of its
14  *   contributors may be used to endorse or promote products derived from this
15  *   software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS",
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 package org.libjpegturbo.turbojpeg;
31 
32 /**
33  * Fractional scaling factor
34  */
35 public class TJScalingFactor {
36 
37   /**
38    * Create a TurboJPEG scaling factor instance.
39    *
40    * @param num numerator
41    * @param denom denominator
42    */
43   @SuppressWarnings("checkstyle:HiddenField")
TJScalingFactor(int num, int denom)44   public TJScalingFactor(int num, int denom) {
45     if (num < 1 || denom < 1)
46       throw new IllegalArgumentException("Numerator and denominator must be >= 1");
47     this.num = num;
48     this.denom = denom;
49   }
50 
51   /**
52    * Returns numerator
53    *
54    * @return numerator
55    */
getNum()56   public int getNum() {
57     return num;
58   }
59 
60   /**
61    * Returns denominator
62    *
63    * @return denominator
64    */
getDenom()65   public int getDenom() {
66     return denom;
67   }
68 
69   /**
70    * Returns the scaled value of <code>dimension</code>.  This function
71    * performs the integer equivalent of
72    * <code>ceil(dimension * scalingFactor)</code>.
73    *
74    * @param dimension width or height to multiply by this scaling factor
75    *
76    * @return the scaled value of <code>dimension</code>.
77    */
getScaled(int dimension)78   public int getScaled(int dimension) {
79     return (dimension * num + denom - 1) / denom;
80   }
81 
82   /**
83    * Returns true or false, depending on whether this instance and
84    * <code>other</code> have the same numerator and denominator.
85    *
86    * @param other the scaling factor against which to compare this one
87    *
88    * @return true or false, depending on whether this instance and
89    * <code>other</code> have the same numerator and denominator.
90    */
equals(TJScalingFactor other)91   public boolean equals(TJScalingFactor other) {
92     return this.num == other.num && this.denom == other.denom;
93   }
94 
95   /**
96    * Returns true or false, depending on whether this instance is equal to
97    * 1/1.
98    *
99    * @return true or false, depending on whether this instance is equal to
100    * 1/1.
101    */
isOne()102   public boolean isOne() {
103     return num == 1 && denom == 1;
104   }
105 
106   /**
107    * Numerator
108    */
109   private int num = 1;
110 
111   /**
112    * Denominator
113    */
114   private int denom = 1;
115 }
116