comparison dwt/internal/image/JPEGFrameHeader.d @ 0:380af2bdd8e5

Upload of whole dwt tree
author Jacob Carlborg <doob@me.com> <jacob.carlborg@gmail.com>
date Sat, 09 Aug 2008 17:00:02 +0200
parents
children 1a8b3cb347e0
comparison
equal deleted inserted replaced
-1:000000000000 0:380af2bdd8e5
1 /*******************************************************************************
2 * Copyright (c) 2000, 2005 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 module dwt.internal.image;
12
13
14 import dwt.DWT;
15
16 final class JPEGFrameHeader : JPEGVariableSizeSegment {
17 int maxVFactor;
18 int maxHFactor;
19 public int[] componentIdentifiers;
20 public int[][] componentParameters;
21
22 public JPEGFrameHeader(byte[] reference) {
23 super(reference);
24 }
25
26 public JPEGFrameHeader(LEDataInputStream byteStream) {
27 super(byteStream);
28 initializeComponentParameters();
29 }
30
31 public int getSamplePrecision() {
32 return reference[4] & 0xFF;
33 }
34
35 public int getNumberOfLines() {
36 return (reference[5] & 0xFF) << 8 | (reference[6] & 0xFF);
37 }
38
39 public int getSamplesPerLine() {
40 return (reference[7] & 0xFF) << 8 | (reference[8] & 0xFF);
41 }
42
43 public int getNumberOfImageComponents() {
44 return reference[9] & 0xFF;
45 }
46
47 public void setSamplePrecision(int precision) {
48 reference[4] = (byte)(precision & 0xFF);
49 }
50
51 public void setNumberOfLines(int anInteger) {
52 reference[5] = (byte)((anInteger & 0xFF00) >> 8);
53 reference[6] = (byte)(anInteger & 0xFF);
54 }
55
56 public void setSamplesPerLine(int samples) {
57 reference[7] = (byte)((samples & 0xFF00) >> 8);
58 reference[8] = (byte)(samples & 0xFF);
59 }
60
61 public void setNumberOfImageComponents(int anInteger) {
62 reference[9] = (byte)(anInteger & 0xFF);
63 }
64
65 public int getMaxHFactor() {
66 return maxHFactor;
67 }
68
69 public int getMaxVFactor() {
70 return maxVFactor;
71 }
72
73 public void setMaxHFactor(int anInteger) {
74 maxHFactor = anInteger;
75 }
76
77 public void setMaxVFactor(int anInteger) {
78 maxVFactor = anInteger;
79 }
80
81 /* Used when decoding. */
82 void initializeComponentParameters() {
83 int nf = getNumberOfImageComponents();
84 componentIdentifiers = new int[nf];
85 int[][] compSpecParams = new int[0][];
86 int hmax = 1;
87 int vmax = 1;
88 for (int i = 0; i < nf; i++) {
89 int ofs = i * 3 + 10;
90 int ci = reference[ofs] & 0xFF;
91 componentIdentifiers[i] = ci;
92 int hi = (reference[ofs + 1] & 0xFF) >> 4;
93 int vi = reference[ofs + 1] & 0xF;
94 int tqi = reference[ofs + 2] & 0xFF;
95 if (hi > hmax) {
96 hmax = hi;
97 }
98 if (vi > vmax) {
99 vmax = vi;
100 }
101 int[] compParam = new int[5];
102 compParam[0] = tqi;
103 compParam[1] = hi;
104 compParam[2] = vi;
105 if (compSpecParams.length <= ci) {
106 int[][] newParams = new int[ci + 1][];
107 System.arraycopy(compSpecParams, 0, newParams, 0, compSpecParams.length);
108 compSpecParams = newParams;
109 }
110 compSpecParams[ci] = compParam;
111 }
112 int x = getSamplesPerLine();
113 int y = getNumberOfLines();
114 int[] multiples = new int[] { 8, 16, 24, 32 };
115 for (int i = 0; i < nf; i++) {
116 int[] compParam = compSpecParams[componentIdentifiers[i]];
117 int hi = compParam[1];
118 int vi = compParam[2];
119 int compWidth = (x * hi + hmax - 1) / hmax;
120 int compHeight = (y * vi + vmax - 1) / vmax;
121 int dsWidth = roundUpToMultiple(compWidth, multiples[hi - 1]);
122 int dsHeight = roundUpToMultiple(compHeight, multiples[vi - 1]);
123 compParam[3] = dsWidth;
124 compParam[4] = dsHeight;
125 }
126 setMaxHFactor(hmax);
127 setMaxVFactor(vmax);
128 componentParameters = compSpecParams;
129 }
130
131 /* Used when encoding. */
132 public void initializeContents() {
133 int nf = getNumberOfImageComponents();
134 if (nf is 0 || nf !is componentParameters.length) {
135 DWT.error(DWT.ERROR_INVALID_IMAGE);
136 }
137 int hmax = 0;
138 int vmax = 0;
139 int[][] compSpecParams = componentParameters;
140 for (int i = 0; i < nf; i++) {
141 int ofs = i * 3 + 10;
142 int[] compParam = compSpecParams[componentIdentifiers[i]];
143 int hi = compParam[1];
144 int vi = compParam[2];
145 if (hi * vi > 4) {
146 DWT.error(DWT.ERROR_INVALID_IMAGE);
147 }
148 reference[ofs] = (byte)(i + 1);
149 reference[ofs + 1] = (byte)(hi * 16 + vi);
150 reference[ofs + 2] = (byte)(compParam[0]);
151 if (hi > hmax) hmax = hi;
152 if (vi > vmax) vmax = vi;
153 }
154 int x = getSamplesPerLine();
155 int y = getNumberOfLines();
156 int[] multiples = new int[] {8, 16, 24, 32};
157 for (int i = 0; i < nf; i++) {
158 int[] compParam = compSpecParams[componentIdentifiers[i]];
159 int hi = compParam[1];
160 int vi = compParam[2];
161 int compWidth = (x * hi + hmax - 1) / hmax;
162 int compHeight = (y * vi + vmax - 1) / vmax;
163 int dsWidth = roundUpToMultiple(compWidth, multiples[hi - 1]);
164 int dsHeight = roundUpToMultiple(compHeight, multiples[vi - 1]);
165 compParam[3] = dsWidth;
166 compParam[4] = dsHeight;
167 }
168 setMaxHFactor(hmax);
169 setMaxVFactor(vmax);
170 }
171
172 int roundUpToMultiple(int anInteger, int mInteger) {
173 int a = anInteger + mInteger - 1;
174 return a - (a % mInteger);
175 }
176
177 /*
178 * Verify the information contained in the receiver is correct.
179 * Answer true if the header contains a valid marker. Otherwise,
180 * answer false. Valid Start Of Frame markers are:
181 * SOF_0 - Baseline DCT, Huffman coding
182 * SOF_1 - Extended sequential DCT, Huffman coding
183 * SOF_2 - Progressive DCT, Huffman coding
184 * SOF_3 - Lossless (sequential), Huffman coding
185 * SOF_5 - Differential sequential, Huffman coding
186 * SOF_6 - Differential progressive, Huffman coding
187 * SOF_7 - Differential lossless, Huffman coding
188 * SOF_9 - Extended sequential DCT, arithmetic coding
189 * SOF_10 - Progressive DCT, arithmetic coding
190 * SOF_11 - Lossless (sequential), arithmetic coding
191 * SOF_13 - Differential sequential, arithmetic coding
192 * SOF_14 - Differential progressive, arithmetic coding
193 * SOF_15 - Differential lossless, arithmetic coding
194 */
195 public bool verify() {
196 int marker = getSegmentMarker();
197 return (marker >= JPEGFileFormat.SOF0 && marker <= JPEGFileFormat.SOF3) ||
198 (marker >= JPEGFileFormat.SOF5 && marker <= JPEGFileFormat.SOF7) ||
199 (marker >= JPEGFileFormat.SOF9 && marker <= JPEGFileFormat.SOF11) ||
200 (marker >= JPEGFileFormat.SOF13 && marker <= JPEGFileFormat.SOF15);
201 }
202
203 public bool isProgressive() {
204 int marker = getSegmentMarker();
205 return marker is JPEGFileFormat.SOF2
206 || marker is JPEGFileFormat.SOF6
207 || marker is JPEGFileFormat.SOF10
208 || marker is JPEGFileFormat.SOF14;
209 }
210
211 public bool isArithmeticCoding() {
212 return getSegmentMarker() >= JPEGFileFormat.SOF9;
213 }
214 }