Source for javax.imageio.plugins.jpeg.JPEGHuffmanTable

   1: /* JPEGHuffmanTable.java --
   2:  Copyright (C)  2006  Free Software Foundation, Inc.
   3: 
   4:  This file is part of GNU Classpath.
   5: 
   6:  GNU Classpath is free software; you can redistribute it and/or modify
   7:  it under the terms of the GNU General Public License as published by
   8:  the Free Software Foundation; either version 2, or (at your option)
   9:  any later version.
  10: 
  11:  GNU Classpath is distributed in the hope that it will be useful, but
  12:  WITHOUT ANY WARRANTY; without even the implied warranty of
  13:  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14:  General Public License for more details.
  15: 
  16:  You should have received a copy of the GNU General Public License
  17:  along with GNU Classpath; see the file COPYING.  If not, write to the
  18:  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19:  02110-1301 USA.
  20: 
  21:  Linking this library statically or dynamically with other modules is
  22:  making a combined work based on this library.  Thus, the terms and
  23:  conditions of the GNU General Public License cover the whole
  24:  combination.
  25: 
  26:  As a special exception, the copyright holders of this library give you
  27:  permission to link this library with independent modules to produce an
  28:  executable, regardless of the license terms of these independent
  29:  modules, and to copy and distribute the resulting executable under
  30:  terms of your choice, provided that you also meet, for each linked
  31:  independent module, the terms and conditions of the license of that
  32:  module.  An independent module is a module which is not derived from
  33:  or based on this library.  If you modify this library, you may extend
  34:  this exception to your version of the library, but you are not
  35:  obligated to do so.  If you do not wish to do so, delete this
  36:  exception statement from your version. */
  37: 
  38: 
  39: package javax.imageio.plugins.jpeg;
  40: 
  41: /**
  42:  * The JPEGHuffmanTable class represents a Huffman table read from a
  43:  * JPEG image file.  The standard JPEG AC and DC chrominance and
  44:  * luminance values are provided as static fields.
  45:  */
  46: public class JPEGHuffmanTable
  47: {
  48:   /**
  49:    * Huffman code lengths.
  50:    */
  51:   private short[] lengths;
  52: 
  53:   /**
  54:    * Huffman values.
  55:    */
  56:   private short[] values;
  57: 
  58:   // The private constructors are used for these final fields to avoid
  59:   // unnecessary copying.
  60:   /**
  61:    * The standard JPEG AC chrominance Huffman table.
  62:    */
  63:   public static final JPEGHuffmanTable StdACChrominance =
  64:       new JPEGHuffmanTable(new short[] { 0, 2, 1, 2, 4, 4, 3, 4, 7, 5,
  65:                                          4, 4, 0, 1, 2, 0x77 },
  66:                            new short[]  { 0x00, 0x01, 0x02, 0x03, 0x11,
  67:                                           0x04, 0x05, 0x21, 0x31, 0x06,
  68:                                           0x12, 0x41, 0x51, 0x07, 0x61,
  69:                                           0x71, 0x13, 0x22, 0x32, 0x81,
  70:                                           0x08, 0x14, 0x42, 0x91, 0xa1,
  71:                                           0xb1, 0xc1, 0x09, 0x23, 0x33,
  72:                                           0x52, 0xf0, 0x15, 0x62, 0x72,
  73:                                           0xd1, 0x0a, 0x16, 0x24, 0x34,
  74:                                           0xe1, 0x25, 0xf1, 0x17, 0x18,
  75:                                           0x19, 0x1a, 0x26, 0x27, 0x28,
  76:                                           0x29, 0x2a, 0x35, 0x36, 0x37,
  77:                                           0x38, 0x39, 0x3a, 0x43, 0x44,
  78:                                           0x45, 0x46, 0x47, 0x48, 0x49,
  79:                                           0x4a, 0x53, 0x54, 0x55, 0x56,
  80:                                           0x57, 0x58, 0x59, 0x5a, 0x63,
  81:                                           0x64, 0x65, 0x66, 0x67, 0x68,
  82:                                           0x69, 0x6a, 0x73, 0x74, 0x75,
  83:                                           0x76, 0x77, 0x78, 0x79, 0x7a,
  84:                                           0x82, 0x83, 0x84, 0x85, 0x86,
  85:                                           0x87, 0x88, 0x89, 0x8a, 0x92,
  86:                                           0x93, 0x94, 0x95, 0x96, 0x97,
  87:                                           0x98, 0x99, 0x9a, 0xa2, 0xa3,
  88:                                           0xa4, 0xa5, 0xa6, 0xa7, 0xa8,
  89:                                           0xa9, 0xaa, 0xb2, 0xb3, 0xb4,
  90:                                           0xb5, 0xb6, 0xb7, 0xb8, 0xb9,
  91:                                           0xba, 0xc2, 0xc3, 0xc4, 0xc5,
  92:                                           0xc6, 0xc7, 0xc8, 0xc9, 0xca,
  93:                                           0xd2, 0xd3, 0xd4, 0xd5, 0xd6,
  94:                                           0xd7, 0xd8, 0xd9, 0xda, 0xe2,
  95:                                           0xe3, 0xe4, 0xe5, 0xe6, 0xe7,
  96:                                           0xe8, 0xe9, 0xea, 0xf2, 0xf3,
  97:                                           0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
  98:                                           0xf9, 0xfa }, false);
  99: 
 100:   /**
 101:    * The standard JPEG AC luminance Huffman table.
 102:    */
 103:   public static final JPEGHuffmanTable StdACLuminance =
 104:       new JPEGHuffmanTable(new short[] { 0, 2, 1, 3, 3, 2, 4, 3, 5, 5,
 105:                                          4, 4, 0, 0, 1, 0x7d },
 106:                            new short[] { 0x01, 0x02, 0x03, 0x00, 0x04,
 107:                                          0x11, 0x05, 0x12, 0x21, 0x31,
 108:                                          0x41, 0x06, 0x13, 0x51, 0x61,
 109:                                          0x07, 0x22, 0x71, 0x14, 0x32,
 110:                                          0x81, 0x91, 0xa1, 0x08, 0x23,
 111:                                          0x42, 0xb1, 0xc1, 0x15, 0x52,
 112:                                          0xd1, 0xf0, 0x24, 0x33, 0x62,
 113:                                          0x72, 0x82, 0x09, 0x0a, 0x16,
 114:                                          0x17, 0x18, 0x19, 0x1a, 0x25,
 115:                                          0x26, 0x27, 0x28, 0x29, 0x2a,
 116:                                          0x34, 0x35, 0x36, 0x37, 0x38,
 117:                                          0x39, 0x3a, 0x43, 0x44, 0x45,
 118:                                          0x46, 0x47, 0x48, 0x49, 0x4a,
 119:                                          0x53, 0x54, 0x55, 0x56, 0x57,
 120:                                          0x58, 0x59, 0x5a, 0x63, 0x64,
 121:                                          0x65, 0x66, 0x67, 0x68, 0x69,
 122:                                          0x6a, 0x73, 0x74, 0x75, 0x76,
 123:                                          0x77, 0x78, 0x79, 0x7a, 0x83,
 124:                                          0x84, 0x85, 0x86, 0x87, 0x88,
 125:                                          0x89, 0x8a, 0x92, 0x93, 0x94,
 126:                                          0x95, 0x96, 0x97, 0x98, 0x99,
 127:                                          0x9a, 0xa2, 0xa3, 0xa4, 0xa5,
 128:                                          0xa6, 0xa7, 0xa8, 0xa9, 0xaa,
 129:                                          0xb2, 0xb3, 0xb4, 0xb5, 0xb6,
 130:                                          0xb7, 0xb8, 0xb9, 0xba, 0xc2,
 131:                                          0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
 132:                                          0xc8, 0xc9, 0xca, 0xd2, 0xd3,
 133:                                          0xd4, 0xd5, 0xd6, 0xd7, 0xd8,
 134:                                          0xd9, 0xda, 0xe1, 0xe2, 0xe3,
 135:                                          0xe4, 0xe5, 0xe6, 0xe7, 0xe8,
 136:                                          0xe9, 0xea, 0xf1, 0xf2, 0xf3,
 137:                                          0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
 138:                                          0xf9, 0xfa }, false);
 139: 
 140:   /**
 141:    * The standard JPEG DC chrominance Huffman table.
 142:    */
 143:   public static final JPEGHuffmanTable StdDCChrominance =
 144:       new JPEGHuffmanTable(new short[] { 0, 3, 1, 1, 1, 1, 1, 1, 1, 1,
 145:                                          1, 0, 0, 0, 0, 0 },
 146:                            new short[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
 147:                                          10, 11 }, false);
 148: 
 149:   /**
 150:    * The standard JPEG DC luminance Huffman table.
 151:    */
 152:   public static final JPEGHuffmanTable StdDCLuminance =
 153:       new JPEGHuffmanTable(new short[] { 0, 1, 5, 1, 1, 1, 1, 1, 1, 0,
 154:                                          0, 0, 0, 0, 0, 0 },
 155:                            new short[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
 156:                                          10, 11 }, false);
 157: 
 158:   /**
 159:    * Construct and initialize a Huffman table. Copies are created of
 160:    * the array arguments. lengths[index] stores the number of Huffman
 161:    * values with Huffman codes of length index + 1. The values array
 162:    * stores the Huffman values in order of increasing code length.
 163:    * 
 164:    * @param lengths an array of Huffman code lengths
 165:    * @param values a sorted array of Huffman values
 166:    * @throws IllegalArgumentException if either parameter is null, if
 167:    * lengths.length > 16 or values.length > 256, if any value in
 168:    * length or values is negative, or if the parameters do not
 169:    * describe a valid Huffman table
 170:    */
 171:   public JPEGHuffmanTable(short[] lengths, short[] values)
 172:   {
 173:     // Create copies of the lengths and values arguments.
 174:     this(checkLengths(lengths), checkValues(values, lengths), true);
 175:   }
 176: 
 177:   /**
 178:    * Private constructor that avoids unnecessary copying and argument
 179:    * checking.
 180:    *
 181:    * @param lengths an array of Huffman code lengths
 182:    * @param values a sorted array of Huffman values
 183:    * @param copy true if copies should be created of the given arrays
 184:    */
 185:   private JPEGHuffmanTable(short[] lengths, short[] values, boolean copy)
 186:   {
 187:     this.lengths = copy ? (short[]) lengths.clone() : lengths;
 188:     this.values = copy ? (short[]) values.clone() : values;
 189:   }
 190: 
 191:   private static short[] checkLengths(short[] lengths)
 192:   {
 193:     if (lengths == null || lengths.length > 16)
 194:       throw new IllegalArgumentException("invalid length array");
 195: 
 196:     for (int i = 0; i < lengths.length; i++)
 197:       {
 198:         if (lengths[i] < 0)
 199:           throw new IllegalArgumentException("negative length");
 200:       }
 201: 
 202:     int sum = 0;
 203:     for (int i = 0; i < lengths.length; i++)
 204:       {
 205:         if (lengths[i] > ((1 << (i + 1)) - 1))
 206:           throw new IllegalArgumentException("invalid number of codes"
 207:                                              + " for code length " + (i + 1));
 208:         sum += lengths[i];
 209:       }
 210: 
 211:     return lengths;
 212:   }
 213: 
 214:   private static short[] checkValues(short[] values, short[] lengths)
 215:   {
 216:     if (values == null || values.length > 256)
 217:       throw new IllegalArgumentException("invalid values array");
 218: 
 219:     for (int i = 0; i < values.length; i++)
 220:       {
 221:         if (values[i] < 0)
 222:           throw new IllegalArgumentException("negative value");
 223:       }
 224:     // lengths is known-valid by this point.
 225:     int sum = 0;
 226:     for (int i = 0; i < lengths.length; i++)
 227:       sum += lengths[i];
 228: 
 229:     if (values.length != sum)
 230:       throw new IllegalArgumentException("invalid number of values"
 231:                                          + " for number of codes");
 232: 
 233:     return values;
 234:   }
 235: 
 236:   /**
 237:    * Retrieve a copy of the array of Huffman code lengths.  If the
 238:    * returned array is called lengthcount, there are
 239:    * lengthcount[index] codes of length index + 1.
 240:    *
 241:    * @return a copy of the array of Huffman code lengths
 242:    */
 243:   public short[] getLengths()
 244:   {
 245:     return (short[]) lengths.clone();
 246:   }
 247: 
 248:   /**
 249:    * Retrieve a copy of the array of Huffman values, sorted in order
 250:    * of increasing code length.
 251:    *
 252:    * @return a copy of the array of Huffman values
 253:    */
 254:   public short[] getValues()
 255:   {
 256:     return (short[]) values.clone();
 257:   }
 258: 
 259:   /**
 260:    * Create a string representation of this JPEG Huffman table.
 261:    *
 262:    * @return a string representation of this JPEG Huffman table.
 263:    */
 264:   public String toString()
 265:   {
 266:     StringBuffer buffer = new StringBuffer();
 267:     
 268:     buffer.append("JPEGHuffmanTable:\nlengths:");
 269:     
 270:     for (int i = 0; i < lengths.length; i++)
 271:       buffer.append(" " + lengths[i]);
 272:     
 273:     buffer.append("\nvalues:");
 274:     
 275:     for (int i = 0; i < values.length; i++)
 276:       buffer.append(" " + values[i]);
 277:     
 278:     return buffer.toString();
 279:   }
 280: }