root/dwt/internal/image/JPEGFrameHeader.d

Revision 213:36f5cb12e1a2, 7.6 kB (checked in by Frank Benoit <benoit@tionex.de>, 8 months ago)

Update to SWT 3.4M7

Line 
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  * Port to the D programming language:
11  *     Frank Benoit <benoit@tionex.de>
12  *******************************************************************************/
13 module dwt.internal.image.JPEGFrameHeader;
14
15
16 import dwt.DWT;
17 import dwt.internal.image.JPEGVariableSizeSegment;
18 import dwt.internal.image.JPEGFileFormat;
19 import dwt.internal.image.LEDataInputStream;
20 import dwt.dwthelper.utils;
21
22 final class JPEGFrameHeader : JPEGVariableSizeSegment {
23     int maxVFactor;
24     int maxHFactor;
25     public int[] componentIdentifiers;
26     public int[][] componentParameters;
27
28     public this(byte[] reference) {
29         super(reference);
30     }
31
32     public this(LEDataInputStream byteStream) {
33         super(byteStream);
34         initializeComponentParameters();
35     }
36
37     public int getSamplePrecision() {
38         return reference[4] & 0xFF;
39     }
40
41     public int getNumberOfLines() {
42         return (reference[5] & 0xFF) << 8 | (reference[6] & 0xFF);
43     }
44
45     public int getSamplesPerLine() {
46         return (reference[7] & 0xFF) << 8 | (reference[8] & 0xFF);
47     }
48
49     public int getNumberOfImageComponents() {
50         return reference[9] & 0xFF;
51     }
52
53     public void setSamplePrecision(int precision) {
54         reference[4] = cast(byte)(precision & 0xFF);
55     }
56
57     public void setNumberOfLines(int anInteger) {
58         reference[5] = cast(byte)((anInteger & 0xFF00) >> 8);
59         reference[6] = cast(byte)(anInteger & 0xFF);
60     }
61
62     public void setSamplesPerLine(int samples) {
63         reference[7] = cast(byte)((samples & 0xFF00) >> 8);
64         reference[8] = cast(byte)(samples & 0xFF);
65     }
66
67     public void setNumberOfImageComponents(int anInteger) {
68         reference[9] = cast(byte)(anInteger & 0xFF);
69     }
70
71     public int getMaxHFactor() {
72         return maxHFactor;
73     }
74
75     public int getMaxVFactor() {
76         return maxVFactor;
77     }
78
79     public void setMaxHFactor(int anInteger) {
80         maxHFactor = anInteger;
81     }
82
83     public void setMaxVFactor(int anInteger) {
84         maxVFactor = anInteger;
85     }
86
87     /* Used when decoding. */
88     void initializeComponentParameters() {
89         int nf = getNumberOfImageComponents();
90         componentIdentifiers = new int[nf];
91         int[][] compSpecParams;
92         int hmax = 1;
93         int vmax = 1;
94         for (int i = 0; i < nf; i++) {
95             int ofs = i * 3 + 10;
96             int ci = reference[ofs] & 0xFF;
97             componentIdentifiers[i] = ci;
98             int hi = (reference[ofs + 1] & 0xFF) >> 4;
99             int vi = reference[ofs + 1] & 0xF;
100             int tqi = reference[ofs + 2] & 0xFF;
101             if (hi > hmax) {
102                 hmax = hi;
103             }
104             if (vi > vmax) {
105                 vmax = vi;
106             }
107             int[] compParam = new int[5];
108             compParam[0] = tqi;
109             compParam[1] = hi;
110             compParam[2] = vi;
111             if (compSpecParams.length <= ci) {
112                 int[][] newParams = new int[][](ci + 1);
113                 System.arraycopy(compSpecParams, 0, newParams, 0, compSpecParams.length);
114                 compSpecParams = newParams;
115             }
116             compSpecParams[ci] = compParam;
117         }
118         int x = getSamplesPerLine();
119         int y = getNumberOfLines();
120         int[] multiples = [ 8, 16, 24, 32 ];
121         for (int i = 0; i < nf; i++) {
122             int[] compParam = compSpecParams[componentIdentifiers[i]];
123             int hi = compParam[1];
124             int vi = compParam[2];
125             int compWidth = (x * hi + hmax - 1) / hmax;
126             int compHeight = (y * vi + vmax - 1) / vmax;
127             int dsWidth = roundUpToMultiple(compWidth, multiples[hi - 1]);
128             int dsHeight = roundUpToMultiple(compHeight, multiples[vi - 1]);
129             compParam[3] = dsWidth;
130             compParam[4] = dsHeight;
131         }
132         setMaxHFactor(hmax);
133         setMaxVFactor(vmax);
134         componentParameters = compSpecParams;
135     }
136
137     /* Used when encoding. */
138     public void initializeContents() {
139         int nf = getNumberOfImageComponents();
140         if (nf is 0 || nf !is componentParameters.length) {
141             DWT.error(DWT.ERROR_INVALID_IMAGE);
142         }
143         int hmax = 0;
144         int vmax = 0;
145         int[][] compSpecParams = componentParameters;
146         for (int i = 0; i < nf; i++) {
147             int ofs = i * 3 + 10;
148             int[] compParam = compSpecParams[componentIdentifiers[i]];
149             int hi = compParam[1];
150             int vi = compParam[2];
151             if (hi * vi > 4) {
152                 DWT.error(DWT.ERROR_INVALID_IMAGE);
153             }
154             reference[ofs] = cast(byte)(i + 1);
155             reference[ofs + 1] = cast(byte)(hi * 16 + vi);
156             reference[ofs + 2] = cast(byte)(compParam[0]);
157             if (hi > hmax) hmax = hi;
158             if (vi > vmax) vmax = vi;
159         }
160         int x = getSamplesPerLine();
161         int y = getNumberOfLines();
162         int[] multiples = [8, 16, 24, 32];
163         for (int i = 0; i < nf; i++) {
164             int[] compParam = compSpecParams[componentIdentifiers[i]];
165             int hi = compParam[1];
166             int vi = compParam[2];
167             int compWidth = (x * hi + hmax - 1) / hmax;
168             int compHeight = (y * vi + vmax - 1) / vmax;
169             int dsWidth = roundUpToMultiple(compWidth, multiples[hi - 1]);
170             int dsHeight = roundUpToMultiple(compHeight, multiples[vi - 1]);
171             compParam[3] = dsWidth;
172             compParam[4] = dsHeight;
173         }
174         setMaxHFactor(hmax);
175         setMaxVFactor(vmax);
176     }
177
178     int roundUpToMultiple(int anInteger, int mInteger) {
179         int a = anInteger + mInteger - 1;
180         return a - (a % mInteger);
181     }
182
183     /*
184      * Verify the information contained in the receiver is correct.
185      * Answer true if the header contains a valid marker. Otherwise,
186      * answer false. Valid Start Of Frame markers are:
187      *  SOF_0  - Baseline DCT, Huffman coding
188      *  SOF_1  - Extended sequential DCT, Huffman coding
189      *  SOF_2  - Progressive DCT, Huffman coding
190      *  SOF_3  - Lossless (sequential), Huffman coding
191      *  SOF_5  - Differential sequential, Huffman coding
192      *  SOF_6  - Differential progressive, Huffman coding
193      *  SOF_7  - Differential lossless, Huffman coding
194      *  SOF_9  - Extended sequential DCT, arithmetic coding
195      *  SOF_10 - Progressive DCT, arithmetic coding
196      *  SOF_11 - Lossless (sequential), arithmetic coding
197      *  SOF_13 - Differential sequential, arithmetic coding
198      *  SOF_14 - Differential progressive, arithmetic coding
199      *  SOF_15 - Differential lossless, arithmetic coding
200      */
201     public override bool verify() {
202         int marker = getSegmentMarker();
203         return (marker >= JPEGFileFormat.SOF0 && marker <= JPEGFileFormat.SOF3) ||
204             (marker >= JPEGFileFormat.SOF5 && marker <= JPEGFileFormat.SOF7) ||
205             (marker >= JPEGFileFormat.SOF9 && marker <= JPEGFileFormat.SOF11) ||
206             (marker >= JPEGFileFormat.SOF13 && marker <= JPEGFileFormat.SOF15);
207     }
208
209     public bool isProgressive() {
210         int marker = getSegmentMarker();
211         return marker is JPEGFileFormat.SOF2
212             || marker is JPEGFileFormat.SOF6
213             || marker is JPEGFileFormat.SOF10
214             || marker is JPEGFileFormat.SOF14;
215     }
216
217     public bool isArithmeticCoding() {
218         return getSegmentMarker() >= JPEGFileFormat.SOF9;
219     }
220 }
Note: See TracBrowser for help on using the browser.