HuffmanTable.java 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /* HuffmanTable.java --
  2. Copyright (C) 2006 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package gnu.javax.imageio.jpeg;
  32. import java.io.IOException;
  33. import javax.imageio.plugins.jpeg.JPEGHuffmanTable;
  34. /**
  35. * This Object construct a JPEGHuffmanTable which can be used to encode/decode
  36. * a scan from a JPEG codec stream. The table must be initalized with either a
  37. * BITS byte amount and a Huffman Table Value for decoding or a Huffman Size
  38. * and Huffman Code table for encoding.
  39. */
  40. public class HuffmanTable
  41. {
  42. public final static int HUFFMAN_MAX_TABLES = 4;
  43. private short[] huffcode = new short[256];
  44. private short[] huffsize = new short[256];
  45. private short[] EHUFCO;
  46. private short[] EHUFSI;
  47. private short[] valptr = new short[16];
  48. private short[] mincode = {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  49. -1,-1,-1};
  50. private short[] maxcode = {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  51. -1, -1, -1};
  52. private short[] huffval;
  53. private short[] bits;
  54. static byte JPEG_DC_TABLE = 0;
  55. static byte JPEG_AC_TABLE = 1;
  56. private short lastk = 0;
  57. public HuffmanTable(JPEGHuffmanTable table)
  58. {
  59. huffcode = table.getValues();
  60. bits = table.getLengths();
  61. }
  62. /**
  63. * Generated from FIGURE C.1 - Generation of table of Huffman code sizes on
  64. * ISO DIS 10918-1. Requirements and Guidelines
  65. */
  66. private void generateSizeTable()
  67. {
  68. short index=0;
  69. for(short i=0; i < bits.length ; i++)
  70. {
  71. for(short j=0; j < bits[i] ; j++)
  72. {
  73. huffsize[index] = (short) (i+1);
  74. index++;
  75. }
  76. }
  77. lastk = index;
  78. }
  79. /**
  80. * Generated from FIGURE C.2 - Generation of table of Huffman codes on
  81. * ISO DIS 10918-1. Requirements and Guidelines
  82. */
  83. private void generateCodeTable()
  84. {
  85. short k=0;
  86. short si = huffsize[0];
  87. short code = 0;
  88. for(short i=0; i < huffsize.length ; i++)
  89. {
  90. while(huffsize[k]==si)
  91. {
  92. huffcode[k] = code;
  93. code++;
  94. k++;
  95. }
  96. code <<= 1;
  97. si++;
  98. }
  99. }
  100. /**
  101. * Generated from FIGURE F.15 - Generation of decode table generation on
  102. * ISO DIS 10918-1. Requirements and Guidelines
  103. */
  104. private void generateDecoderTables()
  105. {
  106. short bitcount = 0;
  107. for(int i=0; i < 16 ; i++)
  108. {
  109. if(bits[i]!=0)
  110. valptr[i] = bitcount;
  111. for(int j=0 ; j < bits[i] ; j++)
  112. {
  113. if(huffcode[j+bitcount] < mincode[i] || mincode[i] == -1)
  114. mincode[i] = huffcode[j+bitcount];
  115. if(huffcode[j+bitcount] > maxcode[i])
  116. maxcode[i] = huffcode[j+bitcount];
  117. }
  118. if(mincode[i]!=-1)
  119. valptr[i] = (short) (valptr[i] - mincode[i]);
  120. bitcount += bits[i];
  121. }
  122. }
  123. /**
  124. * Generated from FIGURE C.3 - Generation of Order Codes and tables EHUFCO
  125. * and EHUFSI from the ISO DIS 10918-1. Requirements and Guidelines
  126. */
  127. public void orderCodes(boolean isDC)
  128. {
  129. EHUFCO = new short[isDC ? 15 : 255];
  130. EHUFSI = new short[isDC ? 15 : 255];
  131. for (int p=0; p < lastk ; p++)
  132. {
  133. int i = huffval[p];
  134. if(i < 0 || i > EHUFCO.length || EHUFSI[i]!=0)
  135. System.err.println("Error, bad huffman table.");
  136. EHUFCO[i] = huffcode[p];
  137. EHUFSI[i] = huffsize[p];
  138. }
  139. }
  140. /**
  141. * Generated from FIGURE F.12 - Extending the sign bit of a decoded value in on
  142. * ISO DIS 10918-1. Requirements and Guidelines<p>
  143. *
  144. * @param diff TODO
  145. * @param t TODO
  146. * @return TODO
  147. */
  148. public static int extend(int diff, int t)
  149. {
  150. int Vt = (int)Math.pow(2,(t-1));
  151. if(diff<Vt)
  152. {
  153. Vt=(-1 << t)+1;
  154. diff=diff+Vt;
  155. }
  156. return diff;
  157. }
  158. /**
  159. * Generated from FIGURE F.16 - Procedure for DECODE on
  160. * ISO DIS 10918-1. Requirements and Guidelines<p>
  161. *
  162. * This function takes in a dynamic amount of bits and using the Huffman
  163. * table returns information on how many bits must be read in to a byte in
  164. * order to reconstruct said byte.
  165. *
  166. * @param JPEGStream the bits of the data stream.
  167. */
  168. public int decode(JPEGImageInputStream JPEGStream)
  169. throws IOException, JPEGException
  170. {
  171. int i=0;
  172. short code = (short) JPEGStream.readBits(1);
  173. while(code > maxcode[i])
  174. {
  175. i++;
  176. code <<= 1;
  177. code |= JPEGStream.readBits(1);
  178. }
  179. int val = huffval[code+(valptr[i])];
  180. if(val < 0)
  181. val = 256 + val;
  182. return val;
  183. }
  184. }