root/dwt/internal/image/JPEGScanHeader.d

Revision 213:36f5cb12e1a2, 4.0 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.JPEGScanHeader;
14
15 import dwt.DWT;
16 import dwt.internal.image.JPEGVariableSizeSegment;
17 import dwt.internal.image.LEDataInputStream;
18 import dwt.internal.image.JPEGFileFormat;
19 import dwt.dwthelper.utils;
20
21 final class JPEGScanHeader : JPEGVariableSizeSegment {
22     public int[][] componentParameters;
23
24 public this(byte[] reference) {
25     super(reference);
26 }
27
28 public this(LEDataInputStream byteStream) {
29     super(byteStream);
30     initializeComponentParameters();
31 }
32
33 public int getApproxBitPositionHigh() {
34     return reference[(2 * getNumberOfImageComponents()) + 7] >> 4;
35 }
36
37 public int getApproxBitPositionLow() {
38     return reference[(2 * getNumberOfImageComponents()) + 7] & 0xF;
39 }
40
41 public int getEndOfSpectralSelection() {
42     return reference[(2 * getNumberOfImageComponents()) + 6];
43 }
44
45 public int getNumberOfImageComponents() {
46     return reference[4];
47 }
48
49 public int getStartOfSpectralSelection() {
50     return reference[(2 * getNumberOfImageComponents()) + 5];
51 }
52
53 /* Used when decoding. */
54 void initializeComponentParameters() {
55     int compCount = getNumberOfImageComponents();
56     componentParameters = null;
57     for (int i = 0; i < compCount; i++) {
58         int ofs = 5 + i * 2;
59         int cid = reference[ofs] & 0xFF;
60         int dc = (reference[ofs + 1] & 0xFF) >> 4;
61         int ac = reference[ofs + 1] & 0xF;
62         if (componentParameters.length <= cid) {
63             int[][] newParams = new int[][](cid + 1);
64             System.arraycopy(componentParameters, 0, newParams, 0, componentParameters.length);
65             componentParameters = newParams;
66         }
67         componentParameters[cid] = [ dc, ac ];
68     }
69 }
70
71 /* Used when encoding. */
72 public void initializeContents() {
73     int compCount = getNumberOfImageComponents();
74     int[][] compSpecParams = componentParameters;
75     if (compCount is 0 || compCount !is compSpecParams.length) {
76         DWT.error(DWT.ERROR_INVALID_IMAGE);
77     }
78     for (int i = 0; i < compCount; i++) {
79         int ofs = i * 2 + 5;
80         int[] compParams = compSpecParams[i];
81         reference[ofs] = cast(byte)(i + 1);
82         reference[ofs + 1] = cast(byte)(compParams[0] * 16 + compParams[1]);
83     }
84 }
85
86 public void setEndOfSpectralSelection(int anInteger) {
87     reference[(2 * getNumberOfImageComponents()) + 6] = cast(byte)anInteger;
88 }
89
90 public void setNumberOfImageComponents(int anInteger) {
91     reference[4] = cast(byte)(anInteger & 0xFF);
92 }
93
94 public void setStartOfSpectralSelection(int anInteger) {
95     reference[(2 * getNumberOfImageComponents()) + 5] = cast(byte)anInteger;
96 }
97
98 public override int signature() {
99     return JPEGFileFormat.SOS;
100 }
101
102 public bool verifyProgressiveScan() {
103     int start = getStartOfSpectralSelection();
104     int end = getEndOfSpectralSelection();
105     int low = getApproxBitPositionLow();
106     int high = getApproxBitPositionHigh();
107     int count = getNumberOfImageComponents();
108     if ((start is 0 && end is 00) || (start <= end && end <= 63)) {
109         if (low <= 13 && high <= 13 && (high is 0 || high is low + 1)) {
110             return start is 0 || (start > 0 && count is 1);
111         }
112     }
113     return false;
114 }
115
116 public bool isACProgressiveScan() {
117     return getStartOfSpectralSelection() !is 0 && getEndOfSpectralSelection() !is 0;
118 }
119
120 public bool isDCProgressiveScan() {
121     return getStartOfSpectralSelection() is 0 && getEndOfSpectralSelection() is 0;
122 }
123
124 public bool isFirstScan() {
125     return getApproxBitPositionHigh() is 0;
126 }
127
128 }
Note: See TracBrowser for help on using the browser.