1. /*
  2. * @(#)JPEGQTable.java 1.9 03/12/19
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. /**********************************************************************
  8. **********************************************************************
  9. **********************************************************************
  10. *** COPYRIGHT (c) 1997-1998 Eastman Kodak Company. ***
  11. *** As an unpublished work pursuant to Title 17 of the United ***
  12. *** States Code. All rights reserved. ***
  13. **********************************************************************
  14. **********************************************************************
  15. **********************************************************************/
  16. package com.sun.image.codec.jpeg;
  17. /** Class to encapsulate the JPEG quantization tables.
  18. * <p>
  19. * Note that the classes in the com.sun.image.codec.jpeg package are not
  20. * part of the core Java APIs. They are a part of Sun's JDK and JRE
  21. * distributions. Although other licensees may choose to distribute these
  22. * classes, developers cannot depend on their availability in non-Sun
  23. * implementations. We expect that equivalent functionality will eventually
  24. * be available in a core API or standard extension.
  25. * <p>
  26. */
  27. public class JPEGQTable {
  28. /** Quantization step for each coefficient in zig-zag order */
  29. private int quantval[];
  30. /** The number of coefficients in a DCT block */
  31. private static final byte QTABLESIZE = 64;
  32. /**
  33. * This is the sample luminance quantization table given in the
  34. * JPEG spec section K.1, expressed in zigzag order. The spec says
  35. * that the values given produce "good" quality, and when divided
  36. * by 2, "very good" quality.
  37. */
  38. public static final JPEGQTable StdLuminance = new JPEGQTable();
  39. static {
  40. int [] lumVals = {
  41. 16, 11, 12, 14, 12, 10, 16, 14,
  42. 13, 14, 18, 17, 16, 19, 24, 40,
  43. 26, 24, 22, 22, 24, 49, 35, 37,
  44. 29, 40, 58, 51, 61, 60, 57, 51,
  45. 56, 55, 64, 72, 92, 78, 64, 68,
  46. 87, 69, 55, 56, 80, 109, 81, 87,
  47. 95, 98, 103, 104, 103, 62, 77, 113,
  48. 121, 112, 100, 120, 92, 101, 103, 99
  49. };
  50. StdLuminance.quantval = lumVals;
  51. }
  52. /**
  53. * This is the sample luminance quantization table given in the
  54. * JPEG spec section K.1, expressed in zigzag order. The spec says
  55. * that the values given produce "good" quality, and when divided
  56. * by 2, "very good" quality.
  57. */
  58. public static final JPEGQTable StdChrominance = new JPEGQTable();
  59. static {
  60. int [] chromVals = {
  61. 17, 18, 18, 24, 21, 24, 47, 26,
  62. 26, 47, 99, 66, 56, 66, 99, 99,
  63. 99, 99, 99, 99, 99, 99, 99, 99,
  64. 99, 99, 99, 99, 99, 99, 99, 99,
  65. 99, 99, 99, 99, 99, 99, 99, 99,
  66. 99, 99, 99, 99, 99, 99, 99, 99,
  67. 99, 99, 99, 99, 99, 99, 99, 99,
  68. 99, 99, 99, 99, 99, 99, 99, 99
  69. };
  70. StdChrominance.quantval = chromVals;
  71. }
  72. /**
  73. * Constructs an empty quantization table. This is used to create
  74. * the Std Q-Tables.
  75. */
  76. private JPEGQTable() {
  77. quantval = new int[QTABLESIZE];
  78. }
  79. /**
  80. * Constructs an quantization table from the array that was
  81. * passed. The coefficents must be in zig-zag order. The array
  82. * must be of length 64.
  83. * @param table the quantization table (this is copied).
  84. */
  85. public JPEGQTable( int table[] ) {
  86. if ( table.length != QTABLESIZE ) {
  87. throw new IllegalArgumentException
  88. ("Quantization table is the wrong size.");
  89. } else {
  90. quantval = new int[QTABLESIZE];
  91. System.arraycopy( table, 0, quantval, 0, QTABLESIZE );
  92. }
  93. }
  94. /**
  95. * Returns the current quantization table as an array of ints in
  96. * zig zag order.
  97. * @return A copy of the contained quantization table.
  98. */
  99. public int[] getTable() {
  100. int[] table = new int[QTABLESIZE];
  101. System.arraycopy( quantval, 0, table, 0, QTABLESIZE );
  102. return table;
  103. }
  104. /**
  105. * Returns a new Quantization table where the values are
  106. * multiplied by scaleFactor and then clamped to the range
  107. * 1..32767 (or to 1..255 if forceBaseline is 'true'). <P>
  108. * Values less than one tend to improve the quality level of the
  109. * table, and values greater than one degrade the quality level of
  110. * the table.
  111. * @param scaleFactor the multiplication factor for the table
  112. * @param forceBaseline if true the values will be clamped
  113. * to the range [1 .. 255]
  114. * @return A new Q-Table that is a linear multiple of this Q-Table
  115. */
  116. public JPEGQTable getScaledInstance(float scaleFactor,
  117. boolean forceBaseline ) {
  118. long max = (forceBaseline)?255L:32767L;
  119. int []ret = new int[QTABLESIZE];
  120. for (int i=0; i<QTABLESIZE; i++ ) {
  121. long holder = (long)((quantval[i] * scaleFactor) + 0.5);
  122. // limit to valid range
  123. if (holder <= 0L) holder = 1L;
  124. // Max quantizer for 12 bits
  125. if (holder > max ) holder = max;
  126. ret[i] = (int)holder;
  127. }
  128. return new JPEGQTable(ret);
  129. }
  130. }