KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > swt > internal > image > JPEGScanHeader


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