comparison org.eclipse.swt.win32.win32.x86/src/org/eclipse/swt/internal/image/JPEGQuantizationTable.d @ 0:6dd524f61e62

add dwt win and basic java stuff
author Frank Benoit <benoit@tionex.de>
date Mon, 02 Mar 2009 14:44:16 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:6dd524f61e62
1 /*******************************************************************************
2 * Copyright (c) 2000, 2006 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 org.eclipse.swt.internal.image.JPEGQuantizationTable;
14
15 import org.eclipse.swt.internal.image.LEDataInputStream;
16 import org.eclipse.swt.internal.image.JPEGVariableSizeSegment;
17 import org.eclipse.swt.internal.image.JPEGFileFormat;
18
19 import java.lang.System;
20
21 final class JPEGQuantizationTable : JPEGVariableSizeSegment {
22 public static byte[] DefaultLuminanceQTable = [
23 cast(byte)255, cast(byte)219, 0, 67, 0,
24 16, 11, 10, 16, 24, 40, 51, 61,
25 12, 12, 14, 19, 26, 58, 60, 55,
26 14, 13, 16, 24, 40, 57, 69, 56,
27 14, 17, 22, 29, 51, 87, 80, 62,
28 18, 22, 37, 56, 68, 109, 103, 77,
29 24, 35, 55, 64, 81, 104, 113, 92,
30 49, 64, 78, 87, 103, 121, 120, 101,
31 72, 92, 95, 98, 112, 100, 103, 99
32 ];
33 public static byte[] DefaultChrominanceQTable = [
34 cast(byte)255, cast(byte)219, 0, 67, 1,
35 17, 18, 24, 47, 99, 99, 99, 99,
36 18, 21, 26, 66, 99, 99, 99, 99,
37 24, 26, 56, 99, 99, 99, 99, 99,
38 47, 66, 99, 99, 99, 99, 99, 99,
39 99, 99, 99, 99, 99, 99, 99, 99,
40 99, 99, 99, 99, 99, 99, 99, 99,
41 99, 99, 99, 99, 99, 99, 99, 99,
42 99, 99, 99, 99, 99, 99, 99, 99
43 ];
44
45 public this(byte[] reference) {
46 super(reference);
47 }
48
49 public this(LEDataInputStream byteStream) {
50 super(byteStream);
51 }
52
53 public static JPEGQuantizationTable defaultChrominanceTable() {
54 byte[] data = new byte[DefaultChrominanceQTable.length];
55 System.arraycopy(DefaultChrominanceQTable, 0, data, 0, data.length);
56 return new JPEGQuantizationTable(data);
57 }
58
59 public static JPEGQuantizationTable defaultLuminanceTable() {
60 byte[] data = new byte[DefaultLuminanceQTable.length];
61 System.arraycopy(DefaultLuminanceQTable, 0, data, 0, data.length);
62 return new JPEGQuantizationTable(data);
63 }
64
65 public int[] getQuantizationTablesKeys() {
66 int[] keys = new int[4];
67 int keysIndex = 0;
68 int totalLength = getSegmentLength() - 2;
69 int ofs = 4;
70 while (totalLength > 64) {
71 int tq = reference[ofs] & 0xF;
72 int pq = (reference[ofs] & 0xFF) >> 4;
73 if (pq is 0) {
74 ofs += 65;
75 totalLength -= 65;
76 } else {
77 ofs += 129;
78 totalLength -= 129;
79 }
80 if (keysIndex >= keys.length) {
81 int[] newKeys = new int[keys.length + 4];
82 System.arraycopy(keys, 0, newKeys, 0, keys.length);
83 keys = newKeys;
84 }
85 keys[keysIndex] = tq;
86 keysIndex++;
87 }
88 int[] newKeys = new int[keysIndex];
89 System.arraycopy(keys, 0, newKeys, 0, keysIndex);
90 return newKeys;
91 }
92
93 public int[][] getQuantizationTablesValues() {
94 int[][] values = new int[][](4);
95 int valuesIndex = 0;
96 int totalLength = getSegmentLength() - 2;
97 int ofs = 4;
98 while (totalLength > 64) {
99 int[] qk = new int[64];
100 int pq = (reference[ofs] & 0xFF) >> 4;
101 if (pq is 0) {
102 for (int i = 0; i < qk.length; i++) {
103 qk[i] = reference[ofs + i + 1] & 0xFF;
104 }
105 ofs += 65;
106 totalLength -= 65;
107 } else {
108 for (int i = 0; i < qk.length; i++) {
109 int idx = (i - 1) * 2 ;
110 qk[i] = (reference[ofs + idx + 1] & 0xFF) * 256 + (reference[ofs + idx + 2] & 0xFF);
111 }
112 ofs += 129;
113 totalLength -= 129;
114 }
115 if (valuesIndex >= values.length) {
116 int[][] newValues = new int[][](values.length + 4);
117 System.arraycopy(values, 0, newValues, 0, values.length);
118 values = newValues;
119 }
120 values[valuesIndex] = qk;
121 valuesIndex++;
122 }
123 int[][] newValues = new int[][](valuesIndex);
124 System.arraycopy(values, 0, newValues, 0, valuesIndex);
125 return newValues;
126 }
127
128 public void scaleBy(int qualityFactor) {
129 int qFactor = qualityFactor;
130 if (qFactor <= 0) {
131 qFactor = 1;
132 }
133 if (qFactor > 100) {
134 qFactor = 100;
135 }
136 if (qFactor < 50) {
137 qFactor = 5000 / qFactor;
138 } else {
139 qFactor = 200 - (qFactor * 2);
140 }
141 int totalLength = getSegmentLength() - 2;
142 int ofs = 4;
143 while (totalLength > 64) {
144 // int tq = reference[ofs] & 0xFF;
145 int pq = (reference[ofs] & 0xFF) >> 4;
146 if (pq is 0) {
147 for (int i = ofs + 1; i <= ofs + 64; i++) {
148 int temp = ((reference[i] & 0xFF) * qFactor + 50) / 100;
149 if (temp <= 0) temp = 1;
150 if (temp > 255) temp = 255;
151 reference[i] = cast(byte)temp;
152 }
153 ofs += 65;
154 totalLength -= 65;
155 } else {
156 for (int i = ofs + 1; i <= ofs + 128; i += 2) {
157 int temp = (((reference[i] & 0xFF) * 256 + (reference[i + 1] & 0xFF)) * qFactor + 50) / 100;
158 if (temp <= 0) temp = 1;
159 if (temp > 32767) temp = 32767;
160 reference[i] = cast(byte)(temp >> 8);
161 reference[i + 1] = cast(byte)(temp & 0xFF);
162 }
163 ofs += 129;
164 totalLength -= 129;
165 }
166 }
167 }
168
169 public override int signature() {
170 return JPEGFileFormat.DQT;
171 }
172 }