File: JPEGHuffmanTable.java

package info (click to toggle)
java-imaging-utilities 0.14.2%2B3-2
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 2,280 kB
  • ctags: 3,725
  • sloc: java: 31,190; sh: 238; makefile: 53; xml: 30
file content (160 lines) | stat: -rw-r--r-- 2,576 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
 * JPEGHuffmanTable
 *
 * Copyright (c) 2005, 2006 Marco Schmidt.
 * All rights reserved.
 */

package net.sourceforge.jiu.codecs.jpeg;

/**
 * Data class that stores a single Huffman table, including class 
 * (AC or DC), ID and codes for the 16 possible bit lengths from 1 to 16.
 * @author Marco Schmidt
 * @since 0.13.0
 */
public class JPEGHuffmanTable
{
	public static final int TABLE_CLASS_AC = 1;
	public static final int TABLE_CLASS_DC = 0;

	private int id;
	private int classAcDc;
	private int[][] codes;

	private int[] huffCode;
	private int[] huffSize;
	private int lastK;

	public void createDecoderTables()
	{
		generateSizeTable();
		generateCodeTable();
		// TODO: F.15
	}

	/**
	 * Initialize huffCode from huffSize.
	 * P&M figure C.2, p. 406f.
	 */
	private void generateCodeTable()
	{
		huffCode = new int[257];
		int k = 0;
		int code = 0;
		int si = huffSize[0];
		while (true)
		{
			while (true)
			{
				huffCode[k] = code;
				code++;
				k++;
				if (huffSize[k] != si)
				{
					break;
				}
			}
			if (huffSize[k] == 0)
			{
				break;
			}
			while (true)
			{
				code <<= 1;
				si++;
				if (huffSize[k] == si)
				{
					break;
				}
			}
		}
	}

	/**
	 * Initialize huffSize and lastK from codes.
	 * P&M figure C.1, p. 405f.
	 */
	private void generateSizeTable()
	{
		huffSize = new int[257];
		int i = 1;
		int j = 1;
		int k = 0;
		while (true)
		{
			while (true)
			{
				if (j > codes[i].length)
				{
					break;
				}
				huffSize[k] = i;
				k++;
				j++;
			}
			i++;
			j = 1;
			if (i > JPEGConstants.MAX_HUFFMAN_CODE_LENGTH)
			{
				break;
			}
		}
		huffSize[k] = 0;
		lastK = k;
	}

	public int getClassAcDc()
	{
		return classAcDc;
	}

	public int[][] getCodes()
	{
		return codes;
	}

	public int getId()
	{
		return id;
	}

	public void setClassAcDc(int i)
	{
		classAcDc = i;
	}

	public void setCodes(int[][] is)
	{
		codes = is;
	}

	public void setId(int i)
	{
		id = i;
	}

	public String toString()
	{
		StringBuffer sb = new StringBuffer();
		sb.append("id=");
		sb.append(id);
		sb.append("/class=");
		sb.append(classAcDc == TABLE_CLASS_AC ? "AC" : "DC");
		if (codes != null)
		{
			sb.append("/codes(length,number)=");
			for (int i = 0; i < codes.length; i++)
			{
				if (codes[i].length > 0)
				{
					sb.append(" ");
					sb.append((i+1));
					sb.append(":");
					sb.append(codes[i].length);
				}
			}
		}
		return sb.toString();
	}
}