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