001: /*******************************************************************************
002: * Copyright (c) 2000, 2005 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.swt.internal.image;
011:
012: import org.eclipse.swt.*;
013:
014: final class JPEGFrameHeader extends JPEGVariableSizeSegment {
015: int maxVFactor;
016: int maxHFactor;
017: public int[] componentIdentifiers;
018: public int[][] componentParameters;
019:
020: public JPEGFrameHeader(byte[] reference) {
021: super (reference);
022: }
023:
024: public JPEGFrameHeader(LEDataInputStream byteStream) {
025: super (byteStream);
026: initializeComponentParameters();
027: }
028:
029: public int getSamplePrecision() {
030: return reference[4] & 0xFF;
031: }
032:
033: public int getNumberOfLines() {
034: return (reference[5] & 0xFF) << 8 | (reference[6] & 0xFF);
035: }
036:
037: public int getSamplesPerLine() {
038: return (reference[7] & 0xFF) << 8 | (reference[8] & 0xFF);
039: }
040:
041: public int getNumberOfImageComponents() {
042: return reference[9] & 0xFF;
043: }
044:
045: public void setSamplePrecision(int precision) {
046: reference[4] = (byte) (precision & 0xFF);
047: }
048:
049: public void setNumberOfLines(int anInteger) {
050: reference[5] = (byte) ((anInteger & 0xFF00) >> 8);
051: reference[6] = (byte) (anInteger & 0xFF);
052: }
053:
054: public void setSamplesPerLine(int samples) {
055: reference[7] = (byte) ((samples & 0xFF00) >> 8);
056: reference[8] = (byte) (samples & 0xFF);
057: }
058:
059: public void setNumberOfImageComponents(int anInteger) {
060: reference[9] = (byte) (anInteger & 0xFF);
061: }
062:
063: public int getMaxHFactor() {
064: return maxHFactor;
065: }
066:
067: public int getMaxVFactor() {
068: return maxVFactor;
069: }
070:
071: public void setMaxHFactor(int anInteger) {
072: maxHFactor = anInteger;
073: }
074:
075: public void setMaxVFactor(int anInteger) {
076: maxVFactor = anInteger;
077: }
078:
079: /* Used when decoding. */
080: void initializeComponentParameters() {
081: int nf = getNumberOfImageComponents();
082: componentIdentifiers = new int[nf];
083: int[][] compSpecParams = new int[0][];
084: int hmax = 1;
085: int vmax = 1;
086: for (int i = 0; i < nf; i++) {
087: int ofs = i * 3 + 10;
088: int ci = reference[ofs] & 0xFF;
089: componentIdentifiers[i] = ci;
090: int hi = (reference[ofs + 1] & 0xFF) >> 4;
091: int vi = reference[ofs + 1] & 0xF;
092: int tqi = reference[ofs + 2] & 0xFF;
093: if (hi > hmax) {
094: hmax = hi;
095: }
096: if (vi > vmax) {
097: vmax = vi;
098: }
099: int[] compParam = new int[5];
100: compParam[0] = tqi;
101: compParam[1] = hi;
102: compParam[2] = vi;
103: if (compSpecParams.length <= ci) {
104: int[][] newParams = new int[ci + 1][];
105: System.arraycopy(compSpecParams, 0, newParams, 0,
106: compSpecParams.length);
107: compSpecParams = newParams;
108: }
109: compSpecParams[ci] = compParam;
110: }
111: int x = getSamplesPerLine();
112: int y = getNumberOfLines();
113: int[] multiples = new int[] { 8, 16, 24, 32 };
114: for (int i = 0; i < nf; i++) {
115: int[] compParam = compSpecParams[componentIdentifiers[i]];
116: int hi = compParam[1];
117: int vi = compParam[2];
118: int compWidth = (x * hi + hmax - 1) / hmax;
119: int compHeight = (y * vi + vmax - 1) / vmax;
120: int dsWidth = roundUpToMultiple(compWidth,
121: multiples[hi - 1]);
122: int dsHeight = roundUpToMultiple(compHeight,
123: multiples[vi - 1]);
124: compParam[3] = dsWidth;
125: compParam[4] = dsHeight;
126: }
127: setMaxHFactor(hmax);
128: setMaxVFactor(vmax);
129: componentParameters = compSpecParams;
130: }
131:
132: /* Used when encoding. */
133: public void initializeContents() {
134: int nf = getNumberOfImageComponents();
135: if (nf == 0 || nf != componentParameters.length) {
136: SWT.error(SWT.ERROR_INVALID_IMAGE);
137: }
138: int hmax = 0;
139: int vmax = 0;
140: int[][] compSpecParams = componentParameters;
141: for (int i = 0; i < nf; i++) {
142: int ofs = i * 3 + 10;
143: int[] compParam = compSpecParams[componentIdentifiers[i]];
144: int hi = compParam[1];
145: int vi = compParam[2];
146: if (hi * vi > 4) {
147: SWT.error(SWT.ERROR_INVALID_IMAGE);
148: }
149: reference[ofs] = (byte) (i + 1);
150: reference[ofs + 1] = (byte) (hi * 16 + vi);
151: reference[ofs + 2] = (byte) (compParam[0]);
152: if (hi > hmax)
153: hmax = hi;
154: if (vi > vmax)
155: vmax = vi;
156: }
157: int x = getSamplesPerLine();
158: int y = getNumberOfLines();
159: int[] multiples = new int[] { 8, 16, 24, 32 };
160: for (int i = 0; i < nf; i++) {
161: int[] compParam = compSpecParams[componentIdentifiers[i]];
162: int hi = compParam[1];
163: int vi = compParam[2];
164: int compWidth = (x * hi + hmax - 1) / hmax;
165: int compHeight = (y * vi + vmax - 1) / vmax;
166: int dsWidth = roundUpToMultiple(compWidth,
167: multiples[hi - 1]);
168: int dsHeight = roundUpToMultiple(compHeight,
169: multiples[vi - 1]);
170: compParam[3] = dsWidth;
171: compParam[4] = dsHeight;
172: }
173: setMaxHFactor(hmax);
174: setMaxVFactor(vmax);
175: }
176:
177: int roundUpToMultiple(int anInteger, int mInteger) {
178: int a = anInteger + mInteger - 1;
179: return a - (a % mInteger);
180: }
181:
182: /*
183: * Verify the information contained in the receiver is correct.
184: * Answer true if the header contains a valid marker. Otherwise,
185: * answer false. Valid Start Of Frame markers are:
186: * SOF_0 - Baseline DCT, Huffman coding
187: * SOF_1 - Extended sequential DCT, Huffman coding
188: * SOF_2 - Progressive DCT, Huffman coding
189: * SOF_3 - Lossless (sequential), Huffman coding
190: * SOF_5 - Differential sequential, Huffman coding
191: * SOF_6 - Differential progressive, Huffman coding
192: * SOF_7 - Differential lossless, Huffman coding
193: * SOF_9 - Extended sequential DCT, arithmetic coding
194: * SOF_10 - Progressive DCT, arithmetic coding
195: * SOF_11 - Lossless (sequential), arithmetic coding
196: * SOF_13 - Differential sequential, arithmetic coding
197: * SOF_14 - Differential progressive, arithmetic coding
198: * SOF_15 - Differential lossless, arithmetic coding
199: */
200: public boolean verify() {
201: int marker = getSegmentMarker();
202: return (marker >= JPEGFileFormat.SOF0 && marker <= JPEGFileFormat.SOF3)
203: || (marker >= JPEGFileFormat.SOF5 && marker <= JPEGFileFormat.SOF7)
204: || (marker >= JPEGFileFormat.SOF9 && marker <= JPEGFileFormat.SOF11)
205: || (marker >= JPEGFileFormat.SOF13 && marker <= JPEGFileFormat.SOF15);
206: }
207:
208: public boolean isProgressive() {
209: int marker = getSegmentMarker();
210: return marker == JPEGFileFormat.SOF2
211: || marker == JPEGFileFormat.SOF6
212: || marker == JPEGFileFormat.SOF10
213: || marker == JPEGFileFormat.SOF14;
214: }
215:
216: public boolean isArithmeticCoding() {
217: return getSegmentMarker() >= JPEGFileFormat.SOF9;
218: }
219: }
|