TJScalingFactor.java 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * Copyright (C)2011 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. public TJScalingFactor(int num, int denom) {
  35. if (num < 1 || denom < 1)
  36. throw new IllegalArgumentException("Numerator and denominator must be >= 1");
  37. this.num = num;
  38. this.denom = denom;
  39. }
  40. /**
  41. * Returns numerator
  42. *
  43. * @return numerator
  44. */
  45. public int getNum() {
  46. return num;
  47. }
  48. /**
  49. * Returns denominator
  50. *
  51. * @return denominator
  52. */
  53. public int getDenom() {
  54. return denom;
  55. }
  56. /**
  57. * Returns the scaled value of <code>dimension</code>. This function
  58. * performs the integer equivalent of
  59. * <code>ceil(dimension * scalingFactor)</code>.
  60. *
  61. * @return the scaled value of <code>dimension</code>.
  62. */
  63. public int getScaled(int dimension) {
  64. return (dimension * num + denom - 1) / denom;
  65. }
  66. /**
  67. * Returns true or false, depending on whether this instance and
  68. * <code>other</code> have the same numerator and denominator.
  69. *
  70. * @return true or false, depending on whether this instance and
  71. * <code>other</code> have the same numerator and denominator.
  72. */
  73. public boolean equals(TJScalingFactor other) {
  74. return this.num == other.num && this.denom == other.denom;
  75. }
  76. /**
  77. * Returns true or false, depending on whether this instance is equal to
  78. * 1/1.
  79. *
  80. * @return true or false, depending on whether this instance is equal to
  81. * 1/1.
  82. */
  83. public boolean isOne() {
  84. return num == 1 && denom == 1;
  85. }
  86. /**
  87. * Numerator
  88. */
  89. private int num = 1;
  90. /**
  91. * Denominator
  92. */
  93. private int denom = 1;
  94. }