File: MarkerSegmentDQT.java

package info (click to toggle)
pixelmed-codec 20141206-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 268 kB
  • ctags: 329
  • sloc: java: 1,270; makefile: 164
file content (75 lines) | stat: -rw-r--r-- 2,353 bytes parent folder | download | duplicates (5)
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
/* Copyright (c) 2014, David A. Clunie DBA Pixelmed Publishing. All rights reserved. */

package com.pixelmed.codec.jpeg;

import java.util.Map;

/**
 * <p>A JPEG DQT Marker Segment.</p>
 *
 * @author	dclunie
 */
public class MarkerSegmentDQT {

	private static final String identString = "@(#) $Header: /userland/cvs/codec/com/pixelmed/codec/jpeg/MarkerSegmentDQT.java,v 1.2 2014/03/21 21:46:20 dclunie Exp $";
	
	private int nTables;

	private int[] QuantizationTableElementPrecision;
	private int[] QuantizationTableIdentifier;
	private int[][] QuantizationTableElement;

	public MarkerSegmentDQT(byte[] b,int length) throws Exception {
		QuantizationTableElementPrecision = new int [4];
		QuantizationTableIdentifier       = new int [4];
		QuantizationTableElement          = new int [4][];
	
		nTables=0;
		int offset=0;
		while (length > 0) {
			if (nTables >= 4) {
				throw new Exception("Only 4 tables are permitted");
			}
			
			QuantizationTableElementPrecision[nTables] = Utilities.extract8(b,offset) >> 4;
			QuantizationTableIdentifier[nTables]       = Utilities.extract8(b,offset) & 0x0f;
			QuantizationTableElement[nTables]          = new int[64];
			++offset; --length;
			
			for (int i=0; i<64; ++i) {
				if (QuantizationTableElementPrecision[nTables] > 0) {
					QuantizationTableElement[nTables][i] = Utilities.extract16be(b,offset);
					offset+=2; length-=2;
				}
				else {
					QuantizationTableElement[nTables][i] = Utilities.extract8(b,offset);
					++offset; --length;
				}
			}
			++nTables;
		}
	}

	public void addToMapByIdentifier(Map<String,QuantizationTable> qtByIdentifer) {
		for (int t=0; t<nTables; ++t) {
			int id = QuantizationTableIdentifier[t];
			String key = Integer.toString(id);
			qtByIdentifer.put(key,new QuantizationTable(id,QuantizationTableElementPrecision[t],QuantizationTableElement[t]));
		}
	}

	public String toString() {
		StringBuffer buf = new StringBuffer();
		buf.append("\n\tDQT:\n");
		for (int t=0; t<nTables; ++t) {
			buf.append("\t\t QuantizationTableElementPrecision = "+QuantizationTableElementPrecision[t]+"\n");
			buf.append("\t\t QuantizationTableIdentifier = "      +QuantizationTableIdentifier[t]+"\n");
			for (int i=0; i<64; ++i) {
				buf.append("\t\t\t QuantizationTableElement "+i+" = "+QuantizationTableElement[t][i]+"\n");
			}
		}
		return buf.toString();
	}

}