pixelmed
MarkerSegmentDHT.java
Go to the documentation of this file.
1 /* Copyright (c) 2014, David A. Clunie DBA Pixelmed Publishing. All rights reserved. */
2 
3 package com.pixelmed.codec.jpeg;
4 
5 import java.util.Map;
6 
12 public class MarkerSegmentDHT {
13 
14  private static final String identString = "@(#) $Header: /userland/cvs/codec/com/pixelmed/codec/jpeg/MarkerSegmentDHT.java,v 1.2 2014/03/21 21:46:20 dclunie Exp $";
15 
16  private int nTables;
17 
18  private int[] TableClass;
19  private int[] HuffmanTableIdentifier;
20  private int[][] nHuffmanCodesOfLengthI;
21  private int[][][] ValueOfHuffmanCodeIJ;
22 
23  public MarkerSegmentDHT(byte[] b,int length) throws Exception {
24  TableClass = new int [4];
25  HuffmanTableIdentifier = new int [4];
26  nHuffmanCodesOfLengthI = new int [4][];
27  ValueOfHuffmanCodeIJ = new int [4][][];
28 
29  nTables=0;
30  int offset=0;
31  while (length > 0) {
32  if (nTables >= 4) {
33  throw new Exception("Only 4 tables are permitted");
34  }
35  TableClass[nTables] = Utilities.extract8(b,offset) >> 4;
36  HuffmanTableIdentifier[nTables] = Utilities.extract8(b,offset) & 0x0f;
37  ++offset; --length;
38 
39  nHuffmanCodesOfLengthI[nTables] = new int[16];
40  for (int i=0; i<16; ++i) {
41  nHuffmanCodesOfLengthI[nTables][i] = Utilities.extract8(b,offset);
42  ++offset; --length;
43  }
44 
45  ValueOfHuffmanCodeIJ[nTables] = new int[16][];
46  for (int i=0; i<16; ++i) {
47  ValueOfHuffmanCodeIJ[nTables][i] = new int[nHuffmanCodesOfLengthI[nTables][i]];
48  for (int j=0; j<nHuffmanCodesOfLengthI[nTables][i]; ++j) {
49  ValueOfHuffmanCodeIJ[nTables][i][j] = Utilities.extract8(b,offset);
50  ++offset; --length;
51  }
52  }
53  ++nTables;
54  }
55  }
56 
57  public void addToMapByClassAndIdentifier(Map<String,HuffmanTable> htByClassAndIdentifer) {
58  for (int t=0; t<nTables; ++t) {
59  int cl = TableClass[t];
60  int id = HuffmanTableIdentifier[t];
61  String key = Integer.toString(cl) + "+" + Integer.toString(id);
62  htByClassAndIdentifer.put(key,new HuffmanTable(cl,id,nHuffmanCodesOfLengthI[t],ValueOfHuffmanCodeIJ[t]));
63  }
64  }
65 
66  public String toString() {
67  StringBuffer buf = new StringBuffer();
68  buf.append("\n\tDHT:\n");
69  for (int t=0; t<nTables; ++t) {
70  buf.append("\t\t TableClass = " +TableClass[t]+"\n");
71  buf.append("\t\t HuffmanTableIdentifier = "+HuffmanTableIdentifier[t]+"\n");
72  for (int i=0; i<16; ++i) {
73  buf.append("\t\t\t nHuffmanCodesOfLength "+i+" = "+nHuffmanCodesOfLengthI[t][i]+"\n");
74  for (int j=0; j<nHuffmanCodesOfLengthI[t][i];++j) {
75  buf.append("\t\t\t\t ValueOfHuffmanCode "+j+" = "+ValueOfHuffmanCodeIJ[t][i][j]+"\n");
76  }
77  }
78  }
79  return buf.toString();
80  }
81 
82 }
83 
com.pixelmed.codec.jpeg.MarkerSegmentDHT.toString
String toString()
Definition: MarkerSegmentDHT.java:66
com.pixelmed.codec.jpeg.Utilities.extract8
static final int extract8(byte[] b, int offset)
Definition: Utilities.java:42
com.pixelmed.codec.jpeg.HuffmanTable
Definition: HuffmanTable.java:10
com.pixelmed.codec.jpeg.MarkerSegmentDHT.MarkerSegmentDHT
MarkerSegmentDHT(byte[] b, int length)
Definition: MarkerSegmentDHT.java:23
com.pixelmed.codec.jpeg.Utilities
Definition: Utilities.java:13
com.pixelmed.codec.jpeg.MarkerSegmentDHT
Definition: MarkerSegmentDHT.java:12
com.pixelmed.codec.jpeg.MarkerSegmentDHT.addToMapByClassAndIdentifier
void addToMapByClassAndIdentifier(Map< String, HuffmanTable > htByClassAndIdentifer)
Definition: MarkerSegmentDHT.java:57