1: 
  37: 
  38: 
  39: package ;
  40: 
  41: import ;
  42: 
  43: 
  48: public class JPEGHuffmanTable
  49: {
  50:   
  53:   private short[] lengths;
  54: 
  55:   
  58:   private short[] values;
  59: 
  60:   
  61:   
  62:   
  65:   public static final JPEGHuffmanTable StdACChrominance =
  66:       new JPEGHuffmanTable(new short[] { 0, 2, 1, 2, 4, 4, 3, 4, 7, 5,
  67:                                          4, 4, 0, 1, 2, 0x77 },
  68:                            new short[]  { 0x00, 0x01, 0x02, 0x03, 0x11,
  69:                                           0x04, 0x05, 0x21, 0x31, 0x06,
  70:                                           0x12, 0x41, 0x51, 0x07, 0x61,
  71:                                           0x71, 0x13, 0x22, 0x32, 0x81,
  72:                                           0x08, 0x14, 0x42, 0x91, 0xa1,
  73:                                           0xb1, 0xc1, 0x09, 0x23, 0x33,
  74:                                           0x52, 0xf0, 0x15, 0x62, 0x72,
  75:                                           0xd1, 0x0a, 0x16, 0x24, 0x34,
  76:                                           0xe1, 0x25, 0xf1, 0x17, 0x18,
  77:                                           0x19, 0x1a, 0x26, 0x27, 0x28,
  78:                                           0x29, 0x2a, 0x35, 0x36, 0x37,
  79:                                           0x38, 0x39, 0x3a, 0x43, 0x44,
  80:                                           0x45, 0x46, 0x47, 0x48, 0x49,
  81:                                           0x4a, 0x53, 0x54, 0x55, 0x56,
  82:                                           0x57, 0x58, 0x59, 0x5a, 0x63,
  83:                                           0x64, 0x65, 0x66, 0x67, 0x68,
  84:                                           0x69, 0x6a, 0x73, 0x74, 0x75,
  85:                                           0x76, 0x77, 0x78, 0x79, 0x7a,
  86:                                           0x82, 0x83, 0x84, 0x85, 0x86,
  87:                                           0x87, 0x88, 0x89, 0x8a, 0x92,
  88:                                           0x93, 0x94, 0x95, 0x96, 0x97,
  89:                                           0x98, 0x99, 0x9a, 0xa2, 0xa3,
  90:                                           0xa4, 0xa5, 0xa6, 0xa7, 0xa8,
  91:                                           0xa9, 0xaa, 0xb2, 0xb3, 0xb4,
  92:                                           0xb5, 0xb6, 0xb7, 0xb8, 0xb9,
  93:                                           0xba, 0xc2, 0xc3, 0xc4, 0xc5,
  94:                                           0xc6, 0xc7, 0xc8, 0xc9, 0xca,
  95:                                           0xd2, 0xd3, 0xd4, 0xd5, 0xd6,
  96:                                           0xd7, 0xd8, 0xd9, 0xda, 0xe2,
  97:                                           0xe3, 0xe4, 0xe5, 0xe6, 0xe7,
  98:                                           0xe8, 0xe9, 0xea, 0xf2, 0xf3,
  99:                                           0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
 100:                                           0xf9, 0xfa }, false);
 101: 
 102:   
 105:   public static final JPEGHuffmanTable StdACLuminance =
 106:       new JPEGHuffmanTable(new short[] { 0, 2, 1, 3, 3, 2, 4, 3, 5, 5,
 107:                                          4, 4, 0, 0, 1, 0x7d },
 108:                            new short[] { 0x01, 0x02, 0x03, 0x00, 0x04,
 109:                                          0x11, 0x05, 0x12, 0x21, 0x31,
 110:                                          0x41, 0x06, 0x13, 0x51, 0x61,
 111:                                          0x07, 0x22, 0x71, 0x14, 0x32,
 112:                                          0x81, 0x91, 0xa1, 0x08, 0x23,
 113:                                          0x42, 0xb1, 0xc1, 0x15, 0x52,
 114:                                          0xd1, 0xf0, 0x24, 0x33, 0x62,
 115:                                          0x72, 0x82, 0x09, 0x0a, 0x16,
 116:                                          0x17, 0x18, 0x19, 0x1a, 0x25,
 117:                                          0x26, 0x27, 0x28, 0x29, 0x2a,
 118:                                          0x34, 0x35, 0x36, 0x37, 0x38,
 119:                                          0x39, 0x3a, 0x43, 0x44, 0x45,
 120:                                          0x46, 0x47, 0x48, 0x49, 0x4a,
 121:                                          0x53, 0x54, 0x55, 0x56, 0x57,
 122:                                          0x58, 0x59, 0x5a, 0x63, 0x64,
 123:                                          0x65, 0x66, 0x67, 0x68, 0x69,
 124:                                          0x6a, 0x73, 0x74, 0x75, 0x76,
 125:                                          0x77, 0x78, 0x79, 0x7a, 0x83,
 126:                                          0x84, 0x85, 0x86, 0x87, 0x88,
 127:                                          0x89, 0x8a, 0x92, 0x93, 0x94,
 128:                                          0x95, 0x96, 0x97, 0x98, 0x99,
 129:                                          0x9a, 0xa2, 0xa3, 0xa4, 0xa5,
 130:                                          0xa6, 0xa7, 0xa8, 0xa9, 0xaa,
 131:                                          0xb2, 0xb3, 0xb4, 0xb5, 0xb6,
 132:                                          0xb7, 0xb8, 0xb9, 0xba, 0xc2,
 133:                                          0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
 134:                                          0xc8, 0xc9, 0xca, 0xd2, 0xd3,
 135:                                          0xd4, 0xd5, 0xd6, 0xd7, 0xd8,
 136:                                          0xd9, 0xda, 0xe1, 0xe2, 0xe3,
 137:                                          0xe4, 0xe5, 0xe6, 0xe7, 0xe8,
 138:                                          0xe9, 0xea, 0xf1, 0xf2, 0xf3,
 139:                                          0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
 140:                                          0xf9, 0xfa }, false);
 141: 
 142:   
 145:   public static final JPEGHuffmanTable StdDCChrominance =
 146:       new JPEGHuffmanTable(new short[] { 0, 3, 1, 1, 1, 1, 1, 1, 1, 1,
 147:                                          1, 0, 0, 0, 0, 0 },
 148:                            new short[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
 149:                                          10, 11 }, false);
 150: 
 151:   
 154:   public static final JPEGHuffmanTable StdDCLuminance =
 155:       new JPEGHuffmanTable(new short[] { 0, 1, 5, 1, 1, 1, 1, 1, 1, 0,
 156:                                          0, 0, 0, 0, 0, 0 },
 157:                            new short[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
 158:                                          10, 11 }, false);
 159: 
 160:   
 173:   public JPEGHuffmanTable(short[] lengths, short[] values)
 174:   {
 175:     
 176:     this(checkLengths(lengths), checkValues(values, lengths), true);
 177:   }
 178: 
 179:   
 187:   private JPEGHuffmanTable(short[] lengths, short[] values, boolean copy)
 188:   {
 189:     this.lengths = copy ? (short[]) lengths.clone() : lengths;
 190:     this.values = copy ? (short[]) values.clone() : values;
 191:   }
 192: 
 193:   private static short[] checkLengths(short[] lengths)
 194:   {
 195:     if (lengths == null || lengths.length > 16)
 196:       throw new IllegalArgumentException("invalid length array");
 197: 
 198:     for (int i = 0; i < lengths.length; i++)
 199:       {
 200:         if (lengths[i] < 0)
 201:           throw new IllegalArgumentException("negative length");
 202:       }
 203: 
 204:     int sum = 0;
 205:     for (int i = 0; i < lengths.length; i++)
 206:       {
 207:         if (lengths[i] > ((1 << (i + 1)) - 1))
 208:           throw new IllegalArgumentException("invalid number of codes"
 209:                                              + " for code length " + (i + 1));
 210:         sum += lengths[i];
 211:       }
 212: 
 213:     return lengths;
 214:   }
 215: 
 216:   private static short[] checkValues(short[] values, short[] lengths)
 217:   {
 218:     if (values == null || values.length > 256)
 219:       throw new IllegalArgumentException("invalid values array");
 220: 
 221:     for (int i = 0; i < values.length; i++)
 222:       {
 223:         if (values[i] < 0)
 224:           throw new IllegalArgumentException("negative value");
 225:       }
 226:     
 227:     int sum = 0;
 228:     for (int i = 0; i < lengths.length; i++)
 229:       sum += lengths[i];
 230: 
 231:     if (values.length != sum)
 232:       throw new IllegalArgumentException("invalid number of values"
 233:                                          + " for number of codes");
 234: 
 235:     return values;
 236:   }
 237: 
 238:   
 245:   public short[] getLengths()
 246:   {
 247:     return (short[]) lengths.clone();
 248:   }
 249: 
 250:   
 256:   public short[] getValues()
 257:   {
 258:     return (short[]) values.clone();
 259:   }
 260: 
 261:   
 266:   public String toString()
 267:   {
 268:     CPStringBuilder buffer = new CPStringBuilder();
 269: 
 270:     buffer.append("JPEGHuffmanTable:\nlengths:");
 271: 
 272:     for (int i = 0; i < lengths.length; i++)
 273:       buffer.append(" " + lengths[i]);
 274: 
 275:     buffer.append("\nvalues:");
 276: 
 277:     for (int i = 0; i < values.length; i++)
 278:       buffer.append(" " + values[i]);
 279: 
 280:     return buffer.toString();
 281:   }
 282: }