TJScalingFactor.java 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. package org.libjpegturbo.turbojpeg;
  30. /**
  31. * Fractional scaling factor
  32. */
  33. public class TJScalingFactor {
  34. /**
  35. * Create a TurboJPEG scaling factor instance.
  36. *
  37. * @param num numerator
  38. * @param denom denominator
  39. */
  40. @SuppressWarnings("checkstyle:HiddenField")
  41. public TJScalingFactor(int num, int denom) {
  42. if (num < 1 || denom < 1)
  43. throw new IllegalArgumentException("Numerator and denominator must be >= 1");
  44. this.num = num;
  45. this.denom = denom;
  46. }
  47. /**
  48. * Returns numerator
  49. *
  50. * @return numerator
  51. */
  52. public int getNum() {
  53. return num;
  54. }
  55. /**
  56. * Returns denominator
  57. *
  58. * @return denominator
  59. */
  60. public int getDenom() {
  61. return denom;
  62. }
  63. /**
  64. * Returns the scaled value of <code>dimension</code>. This function
  65. * performs the integer equivalent of
  66. * <code>ceil(dimension * scalingFactor)</code>.
  67. *
  68. * @param dimension width or height to multiply by this scaling factor
  69. *
  70. * @return the scaled value of <code>dimension</code>.
  71. */
  72. public int getScaled(int dimension) {
  73. return (dimension * num + denom - 1) / denom;
  74. }
  75. /**
  76. * Returns true or false, depending on whether this instance and
  77. * <code>other</code> have the same numerator and denominator.
  78. *
  79. * @param other the scaling factor against which to compare this one
  80. *
  81. * @return true or false, depending on whether this instance and
  82. * <code>other</code> have the same numerator and denominator.
  83. */
  84. public boolean equals(TJScalingFactor other) {
  85. return this.num == other.num && this.denom == other.denom;
  86. }
  87. /**
  88. * Returns true or false, depending on whether this instance is equal to
  89. * 1/1.
  90. *
  91. * @return true or false, depending on whether this instance is equal to
  92. * 1/1.
  93. */
  94. public boolean isOne() {
  95. return num == 1 && denom == 1;
  96. }
  97. /**
  98. * Numerator
  99. */
  100. private int num = 1;
  101. /**
  102. * Denominator
  103. */
  104. private int denom = 1;
  105. }