25
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2000, 2008 IBM Corporation and others.
|
|
3 * All rights reserved. This source file is made available under the terms contained in the README file
|
|
4 * accompanying this program. The README file should be located in the about_files directory of the
|
|
5 * plug-in that contains this source file.
|
|
6 *
|
|
7 * Contributors:
|
|
8 * IBM Corporation - initial API and implementation
|
|
9 * Port to the D programming language:
|
|
10 * Frank Benoit <benoit@tionex.de>
|
|
11 *******************************************************************************/
|
|
12 module org.eclipse.swt.internal.image.JPEGFileFormat;
|
|
13
|
|
14 import org.eclipse.swt.SWT;
|
|
15 import org.eclipse.swt.internal.image.JPEGFrameHeader;
|
|
16 import org.eclipse.swt.internal.image.JPEGScanHeader;
|
|
17 import org.eclipse.swt.internal.image.JPEGHuffmanTable;
|
|
18 import org.eclipse.swt.internal.image.JPEGAppn;
|
|
19 import org.eclipse.swt.internal.image.JPEGSegment;
|
|
20 import org.eclipse.swt.internal.image.FileFormat;
|
|
21 import org.eclipse.swt.internal.image.JPEGComment;
|
|
22 import org.eclipse.swt.internal.image.JPEGArithmeticConditioningTable;
|
|
23 import org.eclipse.swt.internal.image.JPEGRestartInterval;
|
|
24 import org.eclipse.swt.internal.image.JPEGQuantizationTable;
|
|
25 import org.eclipse.swt.internal.image.JPEGStartOfImage;
|
|
26 import org.eclipse.swt.internal.image.JPEGDecoder;
|
|
27 import org.eclipse.swt.internal.image.JPEGEndOfImage;
|
|
28 import java.lang.all;
|
|
29
|
|
30 import org.eclipse.swt.graphics.RGB;
|
|
31 import org.eclipse.swt.graphics.PaletteData;
|
|
32
|
|
33
|
|
34 final class JPEGFileFormat : FileFormat {
|
|
35 int restartInterval;
|
|
36 JPEGFrameHeader frameHeader;
|
|
37 int imageWidth, imageHeight;
|
|
38 int interleavedMcuCols, interleavedMcuRows;
|
|
39 int maxV, maxH;
|
|
40 bool progressive;
|
|
41 int samplePrecision;
|
|
42 int nComponents;
|
|
43 int[][] frameComponents;
|
|
44 int[] componentIds;
|
|
45 byte[][] imageComponents;
|
|
46 int[] dataUnit;
|
|
47 int[][][] dataUnits;
|
|
48 int[] precedingDCs;
|
|
49 JPEGScanHeader scanHeader;
|
|
50 byte[] dataBuffer;
|
|
51 int currentBitCount;
|
|
52 int bufferCurrentPosition;
|
|
53 int restartsToGo;
|
|
54 int nextRestartNumber;
|
|
55 JPEGHuffmanTable[] acHuffmanTables;
|
|
56 JPEGHuffmanTable[] dcHuffmanTables;
|
|
57 int[][] quantizationTables;
|
|
58 int currentByte;
|
|
59 int encoderQFactor = 75;
|
|
60 int eobrun = 0;
|
|
61 /* JPEGConstants */
|
|
62 public static const int DCTSIZE = 8;
|
|
63 public static const int DCTSIZESQR = 64;
|
|
64 /* JPEGFixedPointConstants */
|
|
65 public static const int FIX_0_899976223 = 7373;
|
|
66 public static const int FIX_1_961570560 = 16069;
|
|
67 public static const int FIX_2_053119869 = 16819;
|
|
68 public static const int FIX_0_298631336 = 2446;
|
|
69 public static const int FIX_1_847759065 = 15137;
|
|
70 public static const int FIX_1_175875602 = 9633;
|
|
71 public static const int FIX_3_072711026 = 25172;
|
|
72 public static const int FIX_0_765366865 = 6270;
|
|
73 public static const int FIX_2_562915447 = 20995;
|
|
74 public static const int FIX_0_541196100 = 4433;
|
|
75 public static const int FIX_0_390180644 = 3196;
|
|
76 public static const int FIX_1_501321110 = 12299;
|
|
77 /* JPEGMarkerCodes */
|
|
78 public static const int APP0 = 0xFFE0;
|
|
79 public static const int APP15 = 0xFFEF;
|
|
80 public static const int COM = 0xFFFE;
|
|
81 public static const int DAC = 0xFFCC;
|
|
82 public static const int DHP = 0xFFDE;
|
|
83 public static const int DHT = 0xFFC4;
|
|
84 public static const int DNL = 0xFFDC;
|
|
85 public static const int DRI = 0xFFDD;
|
|
86 public static const int DQT = 0xFFDB;
|
|
87 public static const int EOI = 0xFFD9;
|
|
88 public static const int EXP = 0xFFDF;
|
|
89 public static const int JPG = 0xFFC8;
|
|
90 public static const int JPG0 = 0xFFF0;
|
|
91 public static const int JPG13 = 0xFFFD;
|
|
92 public static const int RST0 = 0xFFD0;
|
|
93 public static const int RST1 = 0xFFD1;
|
|
94 public static const int RST2 = 0xFFD2;
|
|
95 public static const int RST3 = 0xFFD3;
|
|
96 public static const int RST4 = 0xFFD4;
|
|
97 public static const int RST5 = 0xFFD5;
|
|
98 public static const int RST6 = 0xFFD6;
|
|
99 public static const int RST7 = 0xFFD7;
|
|
100 public static const int SOF0 = 0xFFC0;
|
|
101 public static const int SOF1 = 0xFFC1;
|
|
102 public static const int SOF2 = 0xFFC2;
|
|
103 public static const int SOF3 = 0xFFC3;
|
|
104 public static const int SOF5 = 0xFFC5;
|
|
105 public static const int SOF6 = 0xFFC6;
|
|
106 public static const int SOF7 = 0xFFC7;
|
|
107 public static const int SOF9 = 0xFFC9;
|
|
108 public static const int SOF10 = 0xFFCA;
|
|
109 public static const int SOF11 = 0xFFCB;
|
|
110 public static const int SOF13 = 0xFFCD;
|
|
111 public static const int SOF14 = 0xFFCE;
|
|
112 public static const int SOF15 = 0xFFCF;
|
|
113 public static const int SOI = 0xFFD8;
|
|
114 public static const int SOS = 0xFFDA;
|
|
115 public static const int TEM = 0xFF01;
|
|
116 /* JPEGFrameComponentParameterConstants */
|
|
117 public static const int TQI = 0;
|
|
118 public static const int HI = 1;
|
|
119 public static const int VI = 2;
|
|
120 public static const int CW = 3;
|
|
121 public static const int CH = 4;
|
|
122 /* JPEGScanComponentParameterConstants */
|
|
123 public static const int DC = 0;
|
|
124 public static const int AC = 1;
|
|
125 /* JFIF Component Constants */
|
|
126 public static const int ID_Y = 1 - 1;
|
|
127 public static const int ID_CB = 2 - 1;
|
|
128 public static const int ID_CR = 3 - 1;
|
|
129 public static /*const*/ RGB[] RGB16;
|
|
130 public static const int[] ExtendTest = [
|
|
131 0, 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048,
|
|
132 4096, 8192, 16384, 32768, 65536, 131072, 262144
|
|
133 ];
|
|
134 public static const int[] ExtendOffset = [
|
|
135 0, -1, -3, -7, -15, -31, -63, -127, -255, -511, -1023, -2047,
|
|
136 -4095, -8191, -16383, -32767, -65535, -131071, -262143
|
|
137 ];
|
|
138 public static const int[] ZigZag8x8 = [
|
|
139 0, 1, 8, 16, 9, 2, 3, 10,
|
|
140 17, 24, 32, 25, 18, 11, 4, 5,
|
|
141 12, 19, 26, 33, 40, 48, 41, 34,
|
|
142 27, 20, 13, 6, 7, 14, 21, 28,
|
|
143 35, 42, 49, 56, 57, 50, 43, 36,
|
|
144 29, 22, 15, 23, 30, 37, 44, 51,
|
|
145 58, 59, 52, 45, 38, 31, 39, 46,
|
|
146 53, 60, 61, 54, 47, 55, 62, 63
|
|
147 ];
|
|
148
|
|
149 public static int[] CrRTable, CbBTable, CrGTable, CbGTable;
|
|
150 public static int[] RYTable, GYTable, BYTable,
|
|
151 RCbTable, GCbTable, BCbTable, RCrTable, GCrTable, BCrTable, NBitsTable;
|
|
152 //public static void static_this() {
|
|
153 static this() {
|
|
154
|
|
155 RGB16 = [
|
|
156 new RGB(0,0,0),
|
|
157 new RGB(0x80,0,0),
|
|
158 new RGB(0,0x80,0),
|
|
159 new RGB(0x80,0x80,0),
|
|
160 new RGB(0,0,0x80),
|
|
161 new RGB(0x80,0,0x80),
|
|
162 new RGB(0,0x80,0x80),
|
|
163 new RGB(0xC0,0xC0,0xC0),
|
|
164 new RGB(0x80,0x80,0x80),
|
|
165 new RGB(0xFF,0,0),
|
|
166 new RGB(0,0xFF,0),
|
|
167 new RGB(0xFF,0xFF,0),
|
|
168 new RGB(0,0,0xFF),
|
|
169 new RGB(0xFF,0,0xFF),
|
|
170 new RGB(0,0xFF,0xFF),
|
|
171 new RGB(0xFF,0xFF,0xFF)
|
|
172 ];
|
|
173 int [] rYTable = new int[256];
|
|
174 int [] gYTable = new int[256];
|
|
175 int [] bYTable = new int[256];
|
|
176 int [] rCbTable = new int[256];
|
|
177 int [] gCbTable = new int[256];
|
|
178 int [] bCbTable = new int[256];
|
|
179 int [] rCrTable = new int[256];
|
|
180 int [] gCrTable = new int[256];
|
|
181 int [] bCrTable = new int[256];
|
|
182 for (int i = 0; i < 256; i++) {
|
|
183 rYTable[i] = i * 19595;
|
|
184 gYTable[i] = i * 38470;
|
|
185 bYTable[i] = i * 7471 + 32768;
|
|
186 rCbTable[i] = i * -11059;
|
|
187 gCbTable[i] = i * -21709;
|
|
188 bCbTable[i] = i * 32768 + 8388608;
|
|
189 gCrTable[i] = i * -27439;
|
|
190 bCrTable[i] = i * -5329;
|
|
191 }
|
|
192 RYTable = rYTable;
|
|
193 GYTable = gYTable;
|
|
194 BYTable = bYTable;
|
|
195 RCbTable = rCbTable;
|
|
196 GCbTable = gCbTable;
|
|
197 BCbTable = bCbTable;
|
|
198 RCrTable = bCbTable;
|
|
199 GCrTable = gCrTable;
|
|
200 BCrTable = bCrTable;
|
|
201
|
|
202 /* Initialize YCbCr-RGB Tables */
|
|
203 int [] crRTable = new int[256];
|
|
204 int [] cbBTable = new int[256];
|
|
205 int [] crGTable = new int[256];
|
|
206 int [] cbGTable = new int[256];
|
|
207 for (int i = 0; i < 256; i++) {
|
|
208 int x2 = 2 * i - 255;
|
|
209 crRTable[i] = (45941 * x2 + 32768) >> 16;
|
|
210 cbBTable[i] = (58065 * x2 + 32768) >> 16;
|
|
211 crGTable[i] = -23401 * x2;
|
|
212 cbGTable[i] = -11277 * x2 + 32768;
|
|
213 }
|
|
214 CrRTable = crRTable;
|
|
215 CbBTable = cbBTable;
|
|
216 CrGTable = crGTable;
|
|
217 CbGTable = cbGTable;
|
|
218
|
|
219 /* Initialize BitCount Table */
|
|
220 int nBits = 1;
|
|
221 int power2 = 2;
|
|
222 int [] nBitsTable = new int[2048];
|
|
223 nBitsTable[0] = 0;
|
|
224 for (int i = 1; i < nBitsTable.length; i++) {
|
|
225 if (!(i < power2)) {
|
|
226 nBits++;
|
|
227 power2 *= 2;
|
|
228 }
|
|
229 nBitsTable[i] = nBits;
|
|
230 }
|
|
231 NBitsTable = nBitsTable;
|
|
232 }
|
|
233 void compress(ImageData image, byte[] dataYComp, byte[] dataCbComp, byte[] dataCrComp) {
|
|
234 int srcWidth = image.width;
|
|
235 int srcHeight = image.height;
|
|
236 int vhFactor = maxV * maxH;
|
|
237 int[] frameComponent;
|
|
238 imageComponents = new byte[][](nComponents);
|
|
239 for (int i = 0; i < nComponents; i++) {
|
|
240 frameComponent = frameComponents[componentIds[i]];
|
|
241 imageComponents[i] = new byte[frameComponent[CW] * frameComponent[CH]];
|
|
242 }
|
|
243 frameComponent = frameComponents[componentIds[ID_Y]];
|
|
244 for (int yPos = 0; yPos < srcHeight; yPos++) {
|
|
245 int srcOfs = yPos * srcWidth;
|
|
246 int dstOfs = yPos * frameComponent[CW];
|
|
247 System.arraycopy(dataYComp, srcOfs, imageComponents[ID_Y], dstOfs, srcWidth);
|
|
248 }
|
|
249 frameComponent = frameComponents[componentIds[ID_CB]];
|
|
250 for (int yPos = 0; yPos < srcHeight / maxV; yPos++) {
|
|
251 int destRowIndex = yPos * frameComponent[CW];
|
|
252 for (int xPos = 0; xPos < srcWidth / maxH; xPos++) {
|
|
253 int sum = 0;
|
|
254 for (int iv = 0; iv < maxV; iv++) {
|
|
255 int srcIndex = (yPos * maxV + iv) * srcWidth + (xPos * maxH);
|
|
256 for (int ih = 0; ih < maxH; ih++) {
|
|
257 sum += dataCbComp[srcIndex + ih] & 0xFF;
|
|
258 }
|
|
259 }
|
|
260 imageComponents[ID_CB][destRowIndex + xPos] = cast(byte)(sum / vhFactor);
|
|
261 }
|
|
262 }
|
|
263 frameComponent = frameComponents[componentIds[ID_CR]];
|
|
264 for (int yPos = 0; yPos < srcHeight / maxV; yPos++) {
|
|
265 int destRowIndex = yPos * frameComponent[CW];
|
|
266 for (int xPos = 0; xPos < srcWidth / maxH; xPos++) {
|
|
267 int sum = 0;
|
|
268 for (int iv = 0; iv < maxV; iv++) {
|
|
269 int srcIndex = (yPos * maxV + iv) * srcWidth + (xPos * maxH);
|
|
270 for (int ih = 0; ih < maxH; ih++) {
|
|
271 sum += dataCrComp[srcIndex + ih] & 0xFF;
|
|
272 }
|
|
273 }
|
|
274 imageComponents[ID_CR][destRowIndex + xPos] = cast(byte)(sum / vhFactor);
|
|
275 }
|
|
276 }
|
|
277 for (int iComp = 0; iComp < nComponents; iComp++) {
|
|
278 byte[] imageComponent = imageComponents[iComp];
|
|
279 frameComponent = frameComponents[componentIds[iComp]];
|
|
280 int hFactor = frameComponent[HI];
|
|
281 int vFactor = frameComponent[VI];
|
|
282 int componentWidth = frameComponent[CW];
|
|
283 int componentHeight = frameComponent[CH];
|
|
284 int compressedWidth = srcWidth / (maxH / hFactor);
|
|
285 int compressedHeight = srcHeight / (maxV / vFactor);
|
|
286 if (compressedWidth < componentWidth) {
|
|
287 int delta = componentWidth - compressedWidth;
|
|
288 for (int yPos = 0; yPos < compressedHeight; yPos++) {
|
|
289 int dstOfs = ((yPos + 1) * componentWidth - delta);
|
|
290 int dataValue = imageComponent[(dstOfs > 0) ? dstOfs - 1 : 0] & 0xFF;
|
|
291 for (int i = 0; i < delta; i++) {
|
|
292 imageComponent[dstOfs + i] = cast(byte)dataValue;
|
|
293 }
|
|
294 }
|
|
295 }
|
|
296 if (compressedHeight < componentHeight) {
|
|
297 int srcOfs = (compressedHeight > 0) ? (compressedHeight - 1) * componentWidth : 1;
|
|
298 for (int yPos = (compressedHeight > 0) ? compressedHeight : 1; yPos <= componentHeight; yPos++) {
|
|
299 int dstOfs = (yPos - 1) * componentWidth;
|
|
300 System.arraycopy(imageComponent, srcOfs, imageComponent, dstOfs, componentWidth);
|
|
301 }
|
|
302 }
|
|
303 }
|
|
304 }
|
|
305 void convert4BitRGBToYCbCr(ImageData image) {
|
|
306 RGB[] rgbs = image.getRGBs();
|
|
307 int paletteSize = rgbs.length;
|
|
308 byte[] yComp = new byte[paletteSize];
|
|
309 byte[] cbComp = new byte[paletteSize];
|
|
310 byte[] crComp = new byte[paletteSize];
|
|
311 int srcWidth = image.width;
|
|
312 int srcHeight = image.height;
|
|
313 for (int i = 0; i < paletteSize; i++) {
|
|
314 RGB color = rgbs[i];
|
|
315 int r = color.red;
|
|
316 int g = color.green;
|
|
317 int b = color.blue;
|
|
318 int n = RYTable[r] + GYTable[g] + BYTable[b];
|
|
319 yComp[i] = cast(byte)(n >> 16);
|
|
320 if ((n < 0) && ((n & 0xFFFF) !is 0)) yComp[i]--;
|
|
321 n = RCbTable[r] + GCbTable[g] + BCbTable[b];
|
|
322 cbComp[i] = cast(byte)(n >> 16);
|
|
323 if ((n < 0) && ((n & 0xFFFF) !is 0)) cbComp[i]--;
|
|
324 n = RCrTable[r] + GCrTable[g] + BCrTable[b];
|
|
325 crComp[i] = cast(byte)(n >> 16);
|
|
326 if ((n < 0) && ((n & 0xFFFF) !is 0)) crComp[i]--;
|
|
327 }
|
|
328 int bSize = srcWidth * srcHeight;
|
|
329 byte[] dataYComp = new byte[bSize];
|
|
330 byte[] dataCbComp = new byte[bSize];
|
|
331 byte[] dataCrComp = new byte[bSize];
|
|
332 byte[] origData = image.data;
|
|
333 int bytesPerLine = image.bytesPerLine;
|
|
334 int maxScanlineByte = srcWidth >> 1;
|
|
335 for (int yPos = 0; yPos < srcHeight; yPos++) {
|
|
336 for (int xPos = 0; xPos < maxScanlineByte; xPos++) {
|
|
337 int srcIndex = yPos * bytesPerLine + xPos;
|
|
338 int dstIndex = yPos * srcWidth + (xPos * 2);
|
|
339 int value2 = origData[srcIndex] & 0xFF;
|
|
340 int value1 = value2 >> 4;
|
|
341 value2 &= 0x0F;
|
|
342 dataYComp[dstIndex] = yComp[value1];
|
|
343 dataCbComp[dstIndex] = cbComp[value1];
|
|
344 dataCrComp[dstIndex] = crComp[value1];
|
|
345 dataYComp[dstIndex + 1] = yComp[value2];
|
|
346 dataCbComp[dstIndex + 1] = cbComp[value2];
|
|
347 dataCrComp[dstIndex + 1] = crComp[value2];
|
|
348 }
|
|
349 }
|
|
350 compress(image, dataYComp, dataCbComp, dataCrComp);
|
|
351 }
|
|
352 void convert8BitRGBToYCbCr(ImageData image) {
|
|
353 RGB[] rgbs = image.getRGBs();
|
|
354 int paletteSize = rgbs.length;
|
|
355 byte[] yComp = new byte[paletteSize];
|
|
356 byte[] cbComp = new byte[paletteSize];
|
|
357 byte[] crComp = new byte[paletteSize];
|
|
358 int srcWidth = image.width;
|
|
359 int srcHeight = image.height;
|
|
360 for (int i = 0; i < paletteSize; i++) {
|
|
361 RGB color = rgbs[i];
|
|
362 int r = color.red;
|
|
363 int g = color.green;
|
|
364 int b = color.blue;
|
|
365 int n = RYTable[r] + GYTable[g] + BYTable[b];
|
|
366 yComp[i] = cast(byte)(n >> 16);
|
|
367 if ((n < 0) && ((n & 0xFFFF) !is 0)) yComp[i]--;
|
|
368 n = RCbTable[r] + GCbTable[g] + BCbTable[b];
|
|
369 cbComp[i] = cast(byte)(n >> 16);
|
|
370 if ((n < 0) && ((n & 0xFFFF) !is 0)) cbComp[i]--;
|
|
371 n = RCrTable[r] + GCrTable[g] + BCrTable[b];
|
|
372 crComp[i] = cast(byte)(n >> 16);
|
|
373 if ((n < 0) && ((n & 0xFFFF) !is 0)) crComp[i]--;
|
|
374 }
|
|
375 int dstWidth = image.width;
|
|
376 int dstHeight = srcHeight;
|
|
377 int stride = ((srcWidth + 3) >> 2) << 2;
|
|
378 int bSize = dstWidth * dstHeight;
|
|
379 byte[] dataYComp = new byte[bSize];
|
|
380 byte[] dataCbComp = new byte[bSize];
|
|
381 byte[] dataCrComp = new byte[bSize];
|
|
382 byte[] origData = image.data;
|
|
383 for (int yPos = 0; yPos < srcHeight; yPos++) {
|
|
384 int srcRowIndex = yPos * stride;
|
|
385 int dstRowIndex = yPos * dstWidth;
|
|
386 for (int xPos = 0; xPos < srcWidth; xPos++) {
|
|
387 int value = origData[srcRowIndex + xPos] & 0xFF;
|
|
388 int dstIndex = dstRowIndex + xPos;
|
|
389 dataYComp[dstIndex] = yComp[value];
|
|
390 dataCbComp[dstIndex] = cbComp[value];
|
|
391 dataCrComp[dstIndex] = crComp[value];
|
|
392 }
|
|
393 }
|
|
394 compress(image, dataYComp, dataCbComp, dataCrComp);
|
|
395 }
|
|
396 byte[] convertCMYKToRGB() {
|
|
397 /* Unsupported CMYK format. Answer an empty byte array. */
|
|
398 return new byte[0];
|
|
399 }
|
|
400 void convertImageToYCbCr(ImageData image) {
|
|
401 switch (image.depth) {
|
|
402 case 4:
|
|
403 convert4BitRGBToYCbCr(image);
|
|
404 return;
|
|
405 case 8:
|
|
406 convert8BitRGBToYCbCr(image);
|
|
407 return;
|
|
408 case 16:
|
|
409 case 24:
|
|
410 case 32:
|
|
411 convertMultiRGBToYCbCr(image);
|
|
412 return;
|
|
413 default:
|
|
414 SWT.error(SWT.ERROR_UNSUPPORTED_DEPTH);
|
|
415 }
|
|
416 return;
|
|
417 }
|
|
418 void convertMultiRGBToYCbCr(ImageData image) {
|
|
419 int srcWidth = image.width;
|
|
420 int srcHeight = image.height;
|
|
421 int bSize = srcWidth * srcHeight;
|
|
422 byte[] dataYComp = new byte[bSize];
|
|
423 byte[] dataCbComp = new byte[bSize];
|
|
424 byte[] dataCrComp = new byte[bSize];
|
|
425 PaletteData palette = image.palette;
|
|
426 int[] buffer = new int[srcWidth];
|
|
427 if (palette.isDirect) {
|
|
428 int redMask = palette.redMask;
|
|
429 int greenMask = palette.greenMask;
|
|
430 int blueMask = palette.blueMask;
|
|
431 int redShift = palette.redShift;
|
|
432 int greenShift = palette.greenShift;
|
|
433 int blueShift = palette.blueShift;
|
|
434 for (int yPos = 0; yPos < srcHeight; yPos++) {
|
|
435 image.getPixels(0, yPos, srcWidth, buffer, 0);
|
|
436 int dstRowIndex = yPos * srcWidth;
|
|
437 for (int xPos = 0; xPos < srcWidth; xPos++) {
|
|
438 int pixel = buffer[xPos];
|
|
439 int dstDataIndex = dstRowIndex + xPos;
|
|
440 int r = pixel & redMask;
|
|
441 r = (redShift < 0) ? r >>> -redShift : r << redShift;
|
|
442 int g = pixel & greenMask;
|
|
443 g = (greenShift < 0) ? g >>> -greenShift : g << greenShift;
|
|
444 int b = pixel & blueMask;
|
|
445 b = (blueShift < 0) ? b >>> -blueShift : b << blueShift;
|
|
446 dataYComp[dstDataIndex] = cast(byte)((RYTable[r] + GYTable[g] + BYTable[b]) >> 16);
|
|
447 dataCbComp[dstDataIndex] = cast(byte)((RCbTable[r] + GCbTable[g] + BCbTable[b]) >> 16);
|
|
448 dataCrComp[dstDataIndex] = cast(byte)((RCrTable[r] + GCrTable[g] + BCrTable[b]) >> 16);
|
|
449 }
|
|
450 }
|
|
451 } else {
|
|
452 for (int yPos = 0; yPos < srcHeight; yPos++) {
|
|
453 image.getPixels(0, yPos, srcWidth, buffer, 0);
|
|
454 int dstRowIndex = yPos * srcWidth;
|
|
455 for (int xPos = 0; xPos < srcWidth; xPos++) {
|
|
456 int pixel = buffer[xPos];
|
|
457 int dstDataIndex = dstRowIndex + xPos;
|
|
458 RGB rgb = palette.getRGB(pixel);
|
|
459 int r = rgb.red;
|
|
460 int g = rgb.green;
|
|
461 int b = rgb.blue;
|
|
462 dataYComp[dstDataIndex] = cast(byte)((RYTable[r] + GYTable[g] + BYTable[b]) >> 16);
|
|
463 dataCbComp[dstDataIndex] = cast(byte)((RCbTable[r] + GCbTable[g] + BCbTable[b]) >> 16);
|
|
464 dataCrComp[dstDataIndex] = cast(byte)((RCrTable[r] + GCrTable[g] + BCrTable[b]) >> 16);
|
|
465 }
|
|
466 }
|
|
467 }
|
|
468 compress(image, dataYComp, dataCbComp, dataCrComp);
|
|
469 }
|
|
470 byte[] convertYToRGB() {
|
|
471 int compWidth = frameComponents[componentIds[ID_Y]][CW];
|
|
472 int bytesPerLine = (((imageWidth * 8 + 7) / 8) + 3) / 4 * 4;
|
|
473 byte[] data = new byte[bytesPerLine * imageHeight];
|
|
474 byte[] yComp = imageComponents[ID_Y];
|
|
475 int destIndex = 0;
|
|
476 for (int i = 0; i < imageHeight; i++) {
|
|
477 int srcIndex = i * compWidth;
|
|
478 for (int j = 0; j < bytesPerLine; j++) {
|
|
479 int y = yComp[srcIndex] & 0xFF;
|
|
480 if (y < 0) {
|
|
481 y = 0;
|
|
482 } else {
|
|
483 if (y > 255) y = 255;
|
|
484 }
|
|
485 if (j >= imageWidth) {
|
|
486 y = 0;
|
|
487 }
|
|
488 data[destIndex] = cast(byte)y;
|
|
489 srcIndex++;
|
|
490 destIndex++;
|
|
491 }
|
|
492 }
|
|
493 return data;
|
|
494 }
|
|
495 byte[] convertYCbCrToRGB() {
|
|
496 /**
|
|
497 * Convert existing image components into an RGB format.
|
|
498 * YCbCr is defined per CCIR 601-1, except that Cb and Cr are
|
|
499 * normalized to the range 0..MAXJSAMPLE rather than -0.5 .. 0.5.
|
|
500 * The conversion equations to be implemented are therefore
|
|
501 * R = Y + 1.40200 * Cr
|
|
502 * G = Y - 0.34414 * Cb - 0.71414 * Cr
|
|
503 * B = Y + 1.77200 * Cb
|
|
504 * where Cb and Cr represent the incoming values less MAXJSAMPLE/2.
|
|
505 * (These numbers are derived from TIFF 6.0 section 21, dated 3-June-92.)
|
|
506 *
|
|
507 * To avoid floating-point arithmetic, we represent the fractional constants
|
|
508 * as integers scaled up by 2^16 (about 4 digits precision); we have to divide
|
|
509 * the products by 2^16, with appropriate rounding, to get the correct answer.
|
|
510 * Notice that Y, being an integral input, does not contribute any fraction
|
|
511 * so it need not participate in the rounding.
|
|
512 *
|
|
513 * For even more speed, we avoid doing any multiplications in the inner loop
|
|
514 * by precalculating the constants times Cb and Cr for all possible values.
|
|
515 * For 8-bit JSAMPLEs this is very reasonable (only 256 entries per table);
|
|
516 * for 12-bit samples it is still acceptable. It's not very reasonable for
|
|
517 * 16-bit samples, but if you want lossless storage you shouldn't be changing
|
|
518 * colorspace anyway.
|
|
519 * The Cr=>R and Cb=>B values can be rounded to integers in advance; the
|
|
520 * values for the G calculation are left scaled up, since we must add them
|
|
521 * together before rounding.
|
|
522 */
|
|
523 int bSize = imageWidth * imageHeight * nComponents;
|
|
524 byte[] rgbData = new byte[bSize];
|
|
525 int destIndex = 0;
|
|
526 expandImageComponents();
|
|
527 byte[] yComp = imageComponents[ID_Y];
|
|
528 byte[] cbComp = imageComponents[ID_CB];
|
|
529 byte[] crComp = imageComponents[ID_CR];
|
|
530 int compWidth = frameComponents[componentIds[ID_Y]][CW];
|
|
531 for (int v = 0; v < imageHeight; v++) {
|
|
532 int srcIndex = v * compWidth;
|
|
533 for (int i = 0; i < imageWidth; i++) {
|
|
534 int y = yComp[srcIndex] & 0xFF;
|
|
535 int cb = cbComp[srcIndex] & 0xFF;
|
|
536 int cr = crComp[srcIndex] & 0xFF;
|
|
537 int r = y + CrRTable[cr];
|
|
538 int g = y + ((CbGTable[cb] + CrGTable[cr]) >> 16);
|
|
539 int b = y + CbBTable[cb];
|
|
540 if (r < 0) {
|
|
541 r = 0;
|
|
542 } else {
|
|
543 if (r > 255) r = 255;
|
|
544 }
|
|
545 if (g < 0) {
|
|
546 g = 0;
|
|
547 } else {
|
|
548 if (g > 255) g = 255;
|
|
549 }
|
|
550 if (b < 0) {
|
|
551 b = 0;
|
|
552 } else {
|
|
553 if (b > 255) b = 255;
|
|
554 }
|
|
555 rgbData[destIndex] = cast(byte)b;
|
|
556 rgbData[destIndex + 1] = cast(byte)g;
|
|
557 rgbData[destIndex + 2] = cast(byte)r;
|
|
558 destIndex += 3;
|
|
559 srcIndex++;
|
|
560 }
|
|
561 }
|
|
562 return rgbData;
|
|
563 }
|
|
564 void decodeACCoefficients(int[] dataUnit, int iComp) {
|
|
565 int[] sParams = scanHeader.componentParameters[componentIds[iComp]];
|
|
566 JPEGHuffmanTable acTable = acHuffmanTables[sParams[AC]];
|
|
567 int k = 1;
|
|
568 while (k < 64) {
|
|
569 int rs = decodeUsingTable(acTable);
|
|
570 int r = rs >> 4;
|
|
571 int s = rs & 0xF;
|
|
572 if (s is 0) {
|
|
573 if (r is 15) {
|
|
574 k += 16;
|
|
575 } else {
|
|
576 break;
|
|
577 }
|
|
578 } else {
|
|
579 k += r;
|
|
580 int bits = receive(s);
|
|
581 dataUnit[ZigZag8x8[k]] = extendBy(bits, s);
|
|
582 k++;
|
|
583 }
|
|
584 }
|
|
585 }
|
|
586 void decodeACFirstCoefficients(int[] dataUnit, int iComp, int start, int end, int approxBit) {
|
|
587 if (eobrun > 0) {
|
|
588 eobrun--;
|
|
589 return;
|
|
590 }
|
|
591 int[] sParams = scanHeader.componentParameters[componentIds[iComp]];
|
|
592 JPEGHuffmanTable acTable = acHuffmanTables[sParams[AC]];
|
|
593 int k = start;
|
|
594 while (k <= end) {
|
|
595 int rs = decodeUsingTable(acTable);
|
|
596 int r = rs >> 4;
|
|
597 int s = rs & 0xF;
|
|
598 if (s is 0) {
|
|
599 if (r is 15) {
|
|
600 k += 16;
|
|
601 } else {
|
|
602 eobrun = (1 << r) + receive(r) - 1;
|
|
603 break;
|
|
604 }
|
|
605 } else {
|
|
606 k += r;
|
|
607 int bits = receive(s);
|
|
608 dataUnit[ZigZag8x8[k]] = extendBy(bits, s) << approxBit;
|
|
609 k++;
|
|
610 }
|
|
611 }
|
|
612 }
|
|
613 void decodeACRefineCoefficients(int[] dataUnit, int iComp, int start, int end, int approxBit) {
|
|
614 int[] sParams = scanHeader.componentParameters[componentIds[iComp]];
|
|
615 JPEGHuffmanTable acTable = acHuffmanTables[sParams[AC]];
|
|
616 int k = start;
|
|
617 while (k <= end) {
|
|
618 if (eobrun > 0) {
|
|
619 while (k <= end) {
|
|
620 int zzIndex = ZigZag8x8[k];
|
|
621 if (dataUnit[zzIndex] !is 0) {
|
|
622 dataUnit[zzIndex] = refineAC(dataUnit[zzIndex], approxBit);
|
|
623 }
|
|
624 k++;
|
|
625 }
|
|
626 eobrun--;
|
|
627 } else {
|
|
628 int rs = decodeUsingTable(acTable);
|
|
629 int r = rs >> 4;
|
|
630 int s = rs & 0xF;
|
|
631 if (s is 0) {
|
|
632 if (r is 15) {
|
|
633 int zeros = 0;
|
|
634 while (zeros < 16 && k <= end) {
|
|
635 int zzIndex = ZigZag8x8[k];
|
|
636 if (dataUnit[zzIndex] !is 0) {
|
|
637 dataUnit[zzIndex] = refineAC(dataUnit[zzIndex], approxBit);
|
|
638 } else {
|
|
639 zeros++;
|
|
640 }
|
|
641 k++;
|
|
642 }
|
|
643 } else {
|
|
644 eobrun = (1 << r) + receive(r);
|
|
645 }
|
|
646 } else {
|
|
647 int bit = receive(s);
|
|
648 int zeros = 0;
|
|
649 int zzIndex = ZigZag8x8[k];
|
|
650 while ((zeros < r || dataUnit[zzIndex] !is 0) && k <= end) {
|
|
651 if (dataUnit[zzIndex] !is 0) {
|
|
652 dataUnit[zzIndex] = refineAC(dataUnit[zzIndex], approxBit);
|
|
653 } else {
|
|
654 zeros++;
|
|
655 }
|
|
656 k++;
|
|
657 zzIndex = ZigZag8x8[k];
|
|
658 }
|
|
659 if (bit !is 0) {
|
|
660 dataUnit[zzIndex] = 1 << approxBit;
|
|
661 } else {
|
|
662 dataUnit[zzIndex] = -1 << approxBit;
|
|
663 }
|
|
664 k++;
|
|
665 }
|
|
666 }
|
|
667 }
|
|
668 }
|
|
669 int refineAC(int ac, int approxBit) {
|
|
670 if (ac > 0) {
|
|
671 int bit = nextBit();
|
|
672 if (bit !is 0) {
|
|
673 ac += 1 << approxBit;
|
|
674 }
|
|
675 } else if (ac < 0) {
|
|
676 int bit = nextBit();
|
|
677 if (bit !is 0) {
|
|
678 ac += -1 << approxBit;
|
|
679 }
|
|
680 }
|
|
681 return ac;
|
|
682 }
|
|
683 void decodeDCCoefficient(int[] dataUnit, int iComp, bool first, int approxBit) {
|
|
684 int[] sParams = scanHeader.componentParameters[componentIds[iComp]];
|
|
685 JPEGHuffmanTable dcTable = dcHuffmanTables[sParams[DC]];
|
|
686 int lastDC = 0;
|
|
687 if (progressive && !first) {
|
|
688 int bit = nextBit();
|
|
689 lastDC = dataUnit[0] + (bit << approxBit);
|
|
690 } else {
|
|
691 lastDC = precedingDCs[iComp];
|
|
692 int nBits = decodeUsingTable(dcTable);
|
|
693 if (nBits !is 0) {
|
|
694 int bits = receive(nBits);
|
|
695 int diff = extendBy(bits, nBits);
|
|
696 lastDC += diff;
|
|
697 precedingDCs[iComp] = lastDC;
|
|
698 }
|
|
699 if (progressive) {
|
|
700 lastDC = lastDC << approxBit;
|
|
701 }
|
|
702 }
|
|
703 dataUnit[0] = lastDC;
|
|
704 }
|
|
705 void dequantize(int[] dataUnit, int iComp) {
|
|
706 int[] qTable = quantizationTables[frameComponents[componentIds[iComp]][TQI]];
|
|
707 for (int i = 0; i < dataUnit.length; i++) {
|
|
708 int zzIndex = ZigZag8x8[i];
|
|
709 dataUnit[zzIndex] = dataUnit[zzIndex] * qTable[i];
|
|
710 }
|
|
711 }
|
|
712 byte[] decodeImageComponents() {
|
|
713 if (nComponents is 3) { // compIds 1, 2, 3
|
|
714 return convertYCbCrToRGB();
|
|
715 }
|
|
716 // if (nComponents is 3) { // compIds 1, 4, 5
|
|
717 // Unsupported CMYK format.
|
|
718 // return convertYIQToRGB();
|
|
719 // }
|
|
720 if (nComponents is 4) {
|
|
721 return convertCMYKToRGB();
|
|
722 }
|
|
723 return convertYToRGB();
|
|
724 }
|
|
725 void decodeMCUAtXAndY(int xmcu, int ymcu, int nComponentsInScan, bool first, int start, int end, int approxBit) {
|
|
726 for (int iComp = 0; iComp < nComponentsInScan; iComp++) {
|
|
727 int scanComponent = iComp;
|
|
728 while (scanHeader.componentParameters[componentIds[scanComponent]] is null) {
|
|
729 scanComponent++;
|
|
730 }
|
|
731 int[] frameComponent = frameComponents[componentIds[scanComponent]];
|
|
732 int hi = frameComponent[HI];
|
|
733 int vi = frameComponent[VI];
|
|
734 if (nComponentsInScan is 1) {
|
|
735 hi = 1;
|
|
736 vi = 1;
|
|
737 }
|
|
738 int compWidth = frameComponent[CW];
|
|
739 for (int ivi = 0; ivi < vi; ivi++) {
|
|
740 for (int ihi = 0; ihi < hi; ihi++) {
|
|
741 if (progressive) {
|
|
742 // Progressive: First scan - create a new data unit.
|
|
743 // Subsequent scans - refine the existing data unit.
|
|
744 int index = (ymcu * vi + ivi) * compWidth + xmcu * hi + ihi;
|
|
745 dataUnit = dataUnits[scanComponent][index];
|
|
746 if (dataUnit is null) {
|
|
747 dataUnit = new int[64];
|
|
748 dataUnits[scanComponent][index] = dataUnit;
|
|
749 }
|
|
750 } else {
|
|
751 // Sequential: Clear and reuse the data unit buffer.
|
|
752 for (int i = 0; i < dataUnit.length; i++) {
|
|
753 dataUnit[i] = 0;
|
|
754 }
|
|
755 }
|
|
756 if (!progressive || scanHeader.isDCProgressiveScan()) {
|
|
757 decodeDCCoefficient(dataUnit, scanComponent, first, approxBit);
|
|
758 }
|
|
759 if (!progressive) {
|
|
760 decodeACCoefficients(dataUnit, scanComponent);
|
|
761 } else {
|
|
762 if (scanHeader.isACProgressiveScan()) {
|
|
763 if (first) {
|
|
764 decodeACFirstCoefficients(dataUnit, scanComponent, start, end, approxBit);
|
|
765 } else {
|
|
766 decodeACRefineCoefficients(dataUnit, scanComponent, start, end, approxBit);
|
|
767 }
|
|
768 }
|
|
769 if (loader.hasListeners()) {
|
|
770 // Dequantization, IDCT, up-sampling and color conversion
|
|
771 // are done on a copy of the coefficient data in order to
|
|
772 // display the image incrementally.
|
|
773 int[] temp = dataUnit;
|
|
774 dataUnit = new int[64];
|
|
775 System.arraycopy(temp, 0, dataUnit, 0, 64);
|
|
776 }
|
|
777 }
|
|
778 if (!progressive || (progressive && loader.hasListeners())) {
|
|
779 dequantize(dataUnit, scanComponent);
|
|
780 inverseDCT(dataUnit);
|
|
781 storeData(dataUnit, scanComponent, xmcu, ymcu, hi, ihi, vi, ivi);
|
|
782 }
|
|
783 }
|
|
784 }
|
|
785 }
|
|
786 }
|
|
787 void decodeScan() {
|
|
788 if (progressive && !scanHeader.verifyProgressiveScan()) {
|
|
789 SWT.error(SWT.ERROR_INVALID_IMAGE);
|
|
790 }
|
|
791 int nComponentsInScan = scanHeader.getNumberOfImageComponents();
|
|
792 int mcuRowsInScan = interleavedMcuRows;
|
|
793 int mcusPerRow = interleavedMcuCols;
|
|
794 if (nComponentsInScan is 1) {
|
|
795 // Non-interleaved.
|
|
796 int scanComponent = 0;
|
|
797 while (scanHeader.componentParameters[componentIds[scanComponent]] is null) {
|
|
798 scanComponent++;
|
|
799 }
|
|
800 int[] frameComponent = frameComponents[componentIds[scanComponent]];
|
|
801 int hi = frameComponent[HI];
|
|
802 int vi = frameComponent[VI];
|
|
803 int mcuWidth = DCTSIZE * maxH / hi;
|
|
804 int mcuHeight = DCTSIZE * maxV / vi;
|
|
805 mcusPerRow = (imageWidth + mcuWidth - 1) / mcuWidth;
|
|
806 mcuRowsInScan = (imageHeight + mcuHeight - 1) / mcuHeight;
|
|
807 }
|
|
808 bool first = scanHeader.isFirstScan();
|
|
809 int start = scanHeader.getStartOfSpectralSelection();
|
|
810 int end = scanHeader.getEndOfSpectralSelection();
|
|
811 int approxBit = scanHeader.getApproxBitPositionLow();
|
|
812 restartsToGo = restartInterval;
|
|
813 nextRestartNumber = 0;
|
|
814 for (int ymcu = 0; ymcu < mcuRowsInScan; ymcu++) {
|
|
815 for (int xmcu = 0; xmcu < mcusPerRow; xmcu++) {
|
|
816 if (restartInterval !is 0) {
|
|
817 if (restartsToGo is 0) processRestartInterval();
|
|
818 restartsToGo--;
|
|
819 }
|
|
820 decodeMCUAtXAndY(xmcu, ymcu, nComponentsInScan, first, start, end, approxBit);
|
|
821 }
|
|
822 }
|
|
823 }
|
|
824 int decodeUsingTable(JPEGHuffmanTable huffmanTable) {
|
|
825 int i = 0;
|
|
826 int[] maxCodes = huffmanTable.getDhMaxCodes();
|
|
827 int[] minCodes = huffmanTable.getDhMinCodes();
|
|
828 int[] valPtrs = huffmanTable.getDhValPtrs();
|
|
829 int[] huffVals = huffmanTable.getDhValues();
|
|
830 int code = nextBit();
|
|
831 while (code > maxCodes[i]) {
|
|
832 code = code * 2 + nextBit();
|
|
833 i++;
|
|
834 }
|
|
835 int j = valPtrs[i] + code - minCodes[i];
|
|
836 return huffVals[j];
|
|
837 }
|
|
838 void emit(int huffCode, int nBits) {
|
|
839 if (nBits is 0) {
|
|
840 SWT.error(SWT.ERROR_INVALID_IMAGE);
|
|
841 }
|
|
842 int[] power2m1 = [
|
|
843 1, 3, 7, 15, 31, 63, 127, 255, 511, 1023, 2047, 4095, 8191,
|
|
844 16383, 32767, 65535, 131125
|
|
845 ];
|
|
846 int code = (huffCode & power2m1[nBits - 1]) << (24 - nBits - currentBitCount);
|
|
847 byte[] codeBuffer = new byte[4];
|
|
848 codeBuffer[0] = cast(byte)(code & 0xFF);
|
|
849 codeBuffer[1] = cast(byte)((code >> 8) & 0xFF);
|
|
850 codeBuffer[2] = cast(byte)((code >> 16) & 0xFF);
|
|
851 codeBuffer[3] = cast(byte)((code >> 24) & 0xFF);
|
|
852 int abs = nBits - (8 - currentBitCount);
|
|
853 if (abs < 0) abs = -abs;
|
|
854 if ((abs >> 3) > 0) {
|
|
855 currentByte += codeBuffer[2];
|
|
856 emitByte(cast(byte)currentByte);
|
|
857 emitByte(codeBuffer[1]);
|
|
858 currentByte = codeBuffer[0];
|
|
859 currentBitCount += nBits - 16;
|
|
860 } else {
|
|
861 currentBitCount += nBits;
|
|
862 if (currentBitCount >= 8) {
|
|
863 currentByte += codeBuffer[2];
|
|
864 emitByte(cast(byte)currentByte);
|
|
865 currentByte = codeBuffer[1];
|
|
866 currentBitCount -= 8;
|
|
867 } else {
|
|
868 currentByte += codeBuffer[2];
|
|
869 }
|
|
870 }
|
|
871 }
|
|
872 void emitByte(byte byteValue) {
|
|
873 if (bufferCurrentPosition >= 512) {
|
|
874 resetOutputBuffer();
|
|
875 }
|
|
876 dataBuffer[bufferCurrentPosition] = byteValue;
|
|
877 bufferCurrentPosition++;
|
|
878 if (byteValue is -1) {
|
|
879 emitByte(cast(byte)0);
|
|
880 }
|
|
881 }
|
|
882 void encodeACCoefficients(int[] dataUnit, int iComp) {
|
|
883 int[] sParams = scanHeader.componentParameters[iComp];
|
|
884 JPEGHuffmanTable acTable = acHuffmanTables[sParams[AC]];
|
|
885 int[] ehCodes = acTable.ehCodes;
|
|
886 byte[] ehSizes = acTable.ehCodeLengths;
|
|
887 int r = 0;
|
|
888 int k = 1;
|
|
889 while (k < 64) {
|
|
890 k++;
|
|
891 int acValue = dataUnit[ZigZag8x8[k - 1]];
|
|
892 if (acValue is 0) {
|
|
893 if (k is 64) {
|
|
894 emit(ehCodes[0], ehSizes[0] & 0xFF);
|
|
895 } else {
|
|
896 r++;
|
|
897 }
|
|
898 } else {
|
|
899 while (r > 15) {
|
|
900 emit(ehCodes[0xF0], ehSizes[0xF0] & 0xFF);
|
|
901 r -= 16;
|
|
902 }
|
|
903 if (acValue < 0) {
|
|
904 int absACValue = acValue;
|
|
905 if (absACValue < 0) absACValue = -absACValue;
|
|
906 int nBits = NBitsTable[absACValue];
|
|
907 int rs = r * 16 + nBits;
|
|
908 emit(ehCodes[rs], ehSizes[rs] & 0xFF);
|
|
909 emit(0xFFFFFF - absACValue, nBits);
|
|
910 } else {
|
|
911 int nBits = NBitsTable[acValue];
|
|
912 int rs = r * 16 + nBits;
|
|
913 emit(ehCodes[rs], ehSizes[rs] & 0xFF);
|
|
914 emit(acValue, nBits);
|
|
915 }
|
|
916 r = 0;
|
|
917 }
|
|
918 }
|
|
919 }
|
|
920 void encodeDCCoefficients(int[] dataUnit, int iComp) {
|
|
921 int[] sParams = scanHeader.componentParameters[iComp];
|
|
922 JPEGHuffmanTable dcTable = dcHuffmanTables[sParams[DC]];
|
|
923 int lastDC = precedingDCs[iComp];
|
|
924 int dcValue = dataUnit[0];
|
|
925 int diff = dcValue - lastDC;
|
|
926 precedingDCs[iComp] = dcValue;
|
|
927 if (diff < 0) {
|
|
928 int absDiff = 0 - diff;
|
|
929 int nBits = NBitsTable[absDiff];
|
|
930 emit(dcTable.ehCodes[nBits], dcTable.ehCodeLengths[nBits]);
|
|
931 emit(0xFFFFFF - absDiff, nBits);
|
|
932 } else {
|
|
933 int nBits = NBitsTable[diff];
|
|
934 emit(dcTable.ehCodes[nBits], dcTable.ehCodeLengths[nBits]);
|
|
935 if (nBits !is 0) {
|
|
936 emit(diff, nBits);
|
|
937 }
|
|
938 }
|
|
939 }
|
|
940 void encodeMCUAtXAndY(int xmcu, int ymcu) {
|
|
941 int nComponentsInScan = scanHeader.getNumberOfImageComponents();
|
|
942 dataUnit = new int[64];
|
|
943 for (int iComp = 0; iComp < nComponentsInScan; iComp++) {
|
|
944 int[] frameComponent = frameComponents[componentIds[iComp]];
|
|
945 int hi = frameComponent[HI];
|
|
946 int vi = frameComponent[VI];
|
|
947 for (int ivi = 0; ivi < vi; ivi++) {
|
|
948 for (int ihi = 0; ihi < hi; ihi++) {
|
|
949 extractData(dataUnit, iComp, xmcu, ymcu, ihi, ivi);
|
|
950 forwardDCT(dataUnit);
|
|
951 quantizeData(dataUnit, iComp);
|
|
952 encodeDCCoefficients(dataUnit, iComp);
|
|
953 encodeACCoefficients(dataUnit, iComp);
|
|
954 }
|
|
955 }
|
|
956 }
|
|
957 }
|
|
958 void encodeScan() {
|
|
959 for (int ymcu = 0; ymcu < interleavedMcuRows; ymcu++) {
|
|
960 for (int xmcu = 0; xmcu < interleavedMcuCols; xmcu++) {
|
|
961 encodeMCUAtXAndY(xmcu, ymcu);
|
|
962 }
|
|
963 }
|
|
964 if (currentBitCount !is 0) {
|
|
965 emitByte(cast(byte)currentByte);
|
|
966 }
|
|
967 resetOutputBuffer();
|
|
968 }
|
|
969 void expandImageComponents() {
|
|
970 for (int iComp = 0; iComp < nComponents; iComp++) {
|
|
971 int[] frameComponent = frameComponents[componentIds[iComp]];
|
|
972 int hi = frameComponent[HI];
|
|
973 int vi = frameComponent[VI];
|
|
974 int upH = maxH / hi;
|
|
975 int upV = maxV / vi;
|
|
976 if ((upH * upV) > 1) {
|
|
977 byte[] component = imageComponents[iComp];
|
|
978 int compWidth = frameComponent[CW];
|
|
979 int compHeight = frameComponent[CH];
|
|
980 int upCompWidth = compWidth * upH;
|
|
981 int upCompHeight = compHeight * upV;
|
|
982 ImageData src = new ImageData(compWidth, compHeight, 8, new PaletteData(RGB16), 4, component);
|
|
983 ImageData dest = src.scaledTo(upCompWidth, upCompHeight);
|
|
984 imageComponents[iComp] = dest.data;
|
|
985 }
|
|
986 }
|
|
987 }
|
|
988 int extendBy(int diff, int t) {
|
|
989 if (diff < ExtendTest[t]) {
|
|
990 return diff + ExtendOffset[t];
|
|
991 } else {
|
|
992 return diff;
|
|
993 }
|
|
994 }
|
|
995 void extractData(int[] dataUnit, int iComp, int xmcu, int ymcu, int ihi, int ivi) {
|
|
996 byte[] compImage = imageComponents[iComp];
|
|
997 int[] frameComponent = frameComponents[componentIds[iComp]];
|
|
998 int hi = frameComponent[HI];
|
|
999 int vi = frameComponent[VI];
|
|
1000 int compWidth = frameComponent[CW];
|
|
1001 int srcIndex = ((ymcu * vi + ivi) * compWidth * DCTSIZE) + ((xmcu * hi + ihi) * DCTSIZE);
|
|
1002 int destIndex = 0;
|
|
1003 for (int i = 0; i < DCTSIZE; i++) {
|
|
1004 for (int col = 0; col < DCTSIZE; col++) {
|
|
1005 dataUnit[destIndex] = (compImage[srcIndex + col] & 0xFF) - 128;
|
|
1006 destIndex++;
|
|
1007 }
|
|
1008 srcIndex += compWidth;
|
|
1009 }
|
|
1010 }
|
|
1011 void forwardDCT(int[] dataUnit) {
|
|
1012 for (int row = 0; row < 8; row++) {
|
|
1013 int rIndex = row * DCTSIZE;
|
|
1014 int tmp0 = dataUnit[rIndex] + dataUnit[rIndex + 7];
|
|
1015 int tmp7 = dataUnit[rIndex] - dataUnit[rIndex + 7];
|
|
1016 int tmp1 = dataUnit[rIndex + 1] + dataUnit[rIndex + 6];
|
|
1017 int tmp6 = dataUnit[rIndex + 1] - dataUnit[rIndex + 6];
|
|
1018 int tmp2 = dataUnit[rIndex + 2] + dataUnit[rIndex + 5];
|
|
1019 int tmp5 = dataUnit[rIndex + 2] - dataUnit[rIndex + 5];
|
|
1020 int tmp3 = dataUnit[rIndex + 3] + dataUnit[rIndex + 4];
|
|
1021 int tmp4 = dataUnit[rIndex + 3] - dataUnit[rIndex + 4];
|
|
1022
|
|
1023 /**
|
|
1024 * Even part per LL&M figure 1 --- note that published figure
|
|
1025 * is faulty; rotator 'sqrt(2)*c1' should be 'sqrt(2)*c6'.
|
|
1026 */
|
|
1027 int tmp10 = tmp0 + tmp3;
|
|
1028 int tmp13 = tmp0 - tmp3;
|
|
1029 int tmp11 = tmp1 + tmp2;
|
|
1030 int tmp12 = tmp1 - tmp2;
|
|
1031
|
|
1032 dataUnit[rIndex] = (tmp10 + tmp11) * 4;
|
|
1033 dataUnit[rIndex + 4] = (tmp10 - tmp11) * 4;
|
|
1034
|
|
1035 int z1 = (tmp12 + tmp13) * FIX_0_541196100;
|
|
1036 int n = z1 + (tmp13 * FIX_0_765366865) + 1024;
|
|
1037 dataUnit[rIndex + 2] = n >> 11;
|
|
1038 if ((n < 0) && ((n & 0x07FF) !is 0)) dataUnit[rIndex + 2]--;
|
|
1039 n = z1 + (tmp12 * (0 - FIX_1_847759065)) + 1024;
|
|
1040 dataUnit[rIndex + 6] = n >> 11;
|
|
1041 if ((n < 0) && ((n & 0x07FF) !is 0)) dataUnit[rIndex + 6]--;
|
|
1042
|
|
1043 /**
|
|
1044 * Odd part per figure 8 --- note paper omits factor of sqrt(2).
|
|
1045 * cK represents cos(K*pi/16).
|
|
1046 * i0..i3 in the paper are tmp4..tmp7 here.
|
|
1047 */
|
|
1048 z1 = tmp4 + tmp7;
|
|
1049 int z2 = tmp5 + tmp6;
|
|
1050 int z3 = tmp4 + tmp6;
|
|
1051 int z4 = tmp5 + tmp7;
|
|
1052 int z5 = (z3 + z4) * FIX_1_175875602; // sqrt(2) * c3
|
|
1053
|
|
1054 tmp4 *= FIX_0_298631336; // sqrt(2) * (-c1+c3+c5-c7)
|
|
1055 tmp5 *= FIX_2_053119869; // sqrt(2) * ( c1+c3-c5+c7)
|
|
1056 tmp6 *= FIX_3_072711026; // sqrt(2) * ( c1+c3+c5-c7)
|
|
1057 tmp7 *= FIX_1_501321110; // sqrt(2) * ( c1+c3-c5-c7)
|
|
1058 z1 *= 0 - FIX_0_899976223; // sqrt(2) * (c7-c3)
|
|
1059 z2 *= 0 - FIX_2_562915447; // sqrt(2) * (-c1-c3)
|
|
1060 z3 *= 0 - FIX_1_961570560; // sqrt(2) * (-c3-c5)
|
|
1061 z4 *= 0 - FIX_0_390180644; // sqrt(2) * (c5-c3)
|
|
1062
|
|
1063 z3 += z5;
|
|
1064 z4 += z5;
|
|
1065
|
|
1066 n = tmp4 + z1 + z3 + 1024;
|
|
1067 dataUnit[rIndex + 7] = n >> 11;
|
|
1068 if ((n < 0) && ((n & 0x07FF) !is 0)) dataUnit[rIndex + 7]--;
|
|
1069 n = tmp5 + z2 + z4 + 1024;
|
|
1070 dataUnit[rIndex + 5] = n >> 11;
|
|
1071 if ((n < 0) && ((n & 0x07FF) !is 0)) dataUnit[rIndex + 5]--;
|
|
1072 n = tmp6 + z2 + z3 + 1024;
|
|
1073 dataUnit[rIndex + 3] = n >> 11;
|
|
1074 if ((n < 0) && ((n & 0x07FF) !is 0)) dataUnit[rIndex + 3]--;
|
|
1075 n = tmp7 + z1 + z4 + 1024;
|
|
1076 dataUnit[rIndex + 1] = n >> 11;
|
|
1077 if ((n < 0) && ((n & 0x07FF) !is 0)) dataUnit[rIndex + 1]--;
|
|
1078 }
|
|
1079
|
|
1080 /**
|
|
1081 * Pass 2: process columns.
|
|
1082 * Note that we must descale the results by a factor of 8 is 2**3,
|
|
1083 * and also undo the PASS1_BITS scaling.
|
|
1084 */
|
|
1085 for (int col = 0; col < 8; col++) {
|
|
1086 int c0 = col;
|
|
1087 int c1 = col + 8;
|
|
1088 int c2 = col + 16;
|
|
1089 int c3 = col + 24;
|
|
1090 int c4 = col + 32;
|
|
1091 int c5 = col + 40;
|
|
1092 int c6 = col + 48;
|
|
1093 int c7 = col + 56;
|
|
1094 int tmp0 = dataUnit[c0] + dataUnit[c7];
|
|
1095 int tmp7 = dataUnit[c0] - dataUnit[c7];
|
|
1096 int tmp1 = dataUnit[c1] + dataUnit[c6];
|
|
1097 int tmp6 = dataUnit[c1] - dataUnit[c6];
|
|
1098 int tmp2 = dataUnit[c2] + dataUnit[c5];
|
|
1099 int tmp5 = dataUnit[c2] - dataUnit[c5];
|
|
1100 int tmp3 = dataUnit[c3] + dataUnit[c4];
|
|
1101 int tmp4 = dataUnit[c3] - dataUnit[c4];
|
|
1102
|
|
1103 /**
|
|
1104 * Even part per LL&M figure 1 --- note that published figure
|
|
1105 * is faulty; rotator 'sqrt(2)*c1' should be 'sqrt(2)*c6'.
|
|
1106 */
|
|
1107 int tmp10 = tmp0 + tmp3;
|
|
1108 int tmp13 = tmp0 - tmp3;
|
|
1109 int tmp11 = tmp1 + tmp2;
|
|
1110 int tmp12 = tmp1 - tmp2;
|
|
1111
|
|
1112 int n = tmp10 + tmp11 + 16;
|
|
1113 dataUnit[c0] = n >> 5;
|
|
1114 if ((n < 0) && ((n & 0x1F) !is 0)) dataUnit[c0]--;
|
|
1115 n = tmp10 - tmp11 + 16;
|
|
1116 dataUnit[c4] = n >> 5;
|
|
1117 if ((n < 0) && ((n & 0x1F) !is 0)) dataUnit[c4]--;
|
|
1118
|
|
1119 int z1 = (tmp12 + tmp13) * FIX_0_541196100;
|
|
1120 n = z1 + (tmp13 * FIX_0_765366865) + 131072;
|
|
1121 dataUnit[c2] = n >> 18;
|
|
1122 if ((n < 0) && ((n & 0x3FFFF) !is 0)) dataUnit[c2]--;
|
|
1123 n = z1 + (tmp12 * (0 - FIX_1_847759065)) + 131072;
|
|
1124 dataUnit[c6] = n >> 18;
|
|
1125 if ((n < 0) && ((n & 0x3FFFF) !is 0)) dataUnit[c6]--;
|
|
1126
|
|
1127 /**
|
|
1128 * Odd part per figure 8 --- note paper omits factor of sqrt(2).
|
|
1129 * cK represents cos(K*pi/16).
|
|
1130 * i0..i3 in the paper are tmp4..tmp7 here.
|
|
1131 */
|
|
1132 z1 = tmp4 + tmp7;
|
|
1133 int z2 = tmp5 + tmp6;
|
|
1134 int z3 = tmp4 + tmp6;
|
|
1135 int z4 = tmp5 + tmp7;
|
|
1136 int z5 = (z3 + z4) * FIX_1_175875602; // sqrt(2) * c3
|
|
1137
|
|
1138 tmp4 *= FIX_0_298631336; // sqrt(2) * (-c1+c3+c5-c7)
|
|
1139 tmp5 *= FIX_2_053119869; // sqrt(2) * ( c1+c3-c5+c7)
|
|
1140 tmp6 *= FIX_3_072711026; // sqrt(2) * ( c1+c3+c5-c7)
|
|
1141 tmp7 *= FIX_1_501321110; // sqrt(2) * ( c1+c3-c5-c7)
|
|
1142 z1 *= 0 - FIX_0_899976223; // sqrt(2) * (c7-c3)
|
|
1143 z2 *= 0 - FIX_2_562915447; // sqrt(2) * (-c1-c3)
|
|
1144 z3 *= 0 - FIX_1_961570560; // sqrt(2) * (-c3-c5)
|
|
1145 z4 *= 0 - FIX_0_390180644; // sqrt(2) * (c5-c3)
|
|
1146
|
|
1147 z3 += z5;
|
|
1148 z4 += z5;
|
|
1149
|
|
1150 n = tmp4 + z1 + z3 + 131072;
|
|
1151 dataUnit[c7] = n >> 18;
|
|
1152 if ((n < 0) && ((n & 0x3FFFF) !is 0)) dataUnit[c7]--;
|
|
1153 n = tmp5 + z2 + z4 + 131072;
|
|
1154 dataUnit[c5] = n >> 18;
|
|
1155 if ((n < 0) && ((n & 0x3FFFF) !is 0)) dataUnit[c5]--;
|
|
1156 n = tmp6 + z2 + z3 + 131072;
|
|
1157 dataUnit[c3] = n >> 18;
|
|
1158 if ((n < 0) && ((n & 0x3FFFF) !is 0)) dataUnit[c3]--;
|
|
1159 n = tmp7 + z1 + z4 + 131072;
|
|
1160 dataUnit[c1] = n >> 18;
|
|
1161 if ((n < 0) && ((n & 0x3FFFF) !is 0)) dataUnit[c1]--;
|
|
1162 }
|
|
1163 }
|
|
1164 void getAPP0() {
|
|
1165 JPEGAppn appn = new JPEGAppn(inputStream);
|
|
1166 if (!appn.verify()) {
|
|
1167 SWT.error(SWT.ERROR_INVALID_IMAGE);
|
|
1168 }
|
|
1169 }
|
|
1170 void getCOM() {
|
|
1171 new JPEGComment(inputStream);
|
|
1172 }
|
|
1173 void getDAC() {
|
|
1174 new JPEGArithmeticConditioningTable(inputStream);
|
|
1175 }
|
|
1176 void getDHT() {
|
|
1177 JPEGHuffmanTable dht = new JPEGHuffmanTable(inputStream);
|
|
1178 if (!dht.verify()) {
|
|
1179 SWT.error(SWT.ERROR_INVALID_IMAGE);
|
|
1180 }
|
|
1181 if (acHuffmanTables is null) {
|
|
1182 acHuffmanTables = new JPEGHuffmanTable[4];
|
|
1183 }
|
|
1184 if (dcHuffmanTables is null) {
|
|
1185 dcHuffmanTables = new JPEGHuffmanTable[4];
|
|
1186 }
|
|
1187 JPEGHuffmanTable[] dhtTables = dht.getAllTables();
|
|
1188 for (int i = 0; i < dhtTables.length; i++) {
|
|
1189 JPEGHuffmanTable dhtTable = dhtTables[i];
|
|
1190 if (dhtTable.getTableClass() is 0) {
|
|
1191 dcHuffmanTables[dhtTable.getTableIdentifier()] = dhtTable;
|
|
1192 } else {
|
|
1193 acHuffmanTables[dhtTable.getTableIdentifier()] = dhtTable;
|
|
1194 }
|
|
1195 }
|
|
1196 }
|
|
1197 void getDNL() {
|
|
1198 new JPEGRestartInterval(inputStream);
|
|
1199 }
|
|
1200 void getDQT() {
|
|
1201 JPEGQuantizationTable dqt = new JPEGQuantizationTable(inputStream);
|
|
1202 int[][] currentTables = quantizationTables;
|
|
1203 if (currentTables is null) {
|
|
1204 currentTables = new int[][](4);
|
|
1205 }
|
|
1206 int[] dqtTablesKeys = dqt.getQuantizationTablesKeys();
|
|
1207 int[][] dqtTablesValues = dqt.getQuantizationTablesValues();
|
|
1208 for (int i = 0; i < dqtTablesKeys.length; i++) {
|
|
1209 int index = dqtTablesKeys[i];
|
|
1210 currentTables[index] = dqtTablesValues[i];
|
|
1211 }
|
|
1212 quantizationTables = currentTables;
|
|
1213 }
|
|
1214 void getDRI() {
|
|
1215 JPEGRestartInterval dri = new JPEGRestartInterval(inputStream);
|
|
1216 if (!dri.verify()) {
|
|
1217 SWT.error(SWT.ERROR_INVALID_IMAGE);
|
|
1218 }
|
|
1219 restartInterval = dri.getRestartInterval();
|
|
1220 }
|
|
1221 void inverseDCT(int[] dataUnit) {
|
|
1222 for (int row = 0; row < 8; row++) {
|
|
1223 int rIndex = row * DCTSIZE;
|
|
1224 /**
|
|
1225 * Due to quantization, we will usually find that many of the input
|
|
1226 * coefficients are zero, especially the AC terms. We can exploit this
|
|
1227 * by short-circuiting the IDCT calculation for any row in which all
|
|
1228 * the AC terms are zero. In that case each output is equal to the
|
|
1229 * DC coefficient (with scale factor as needed).
|
|
1230 * With typical images and quantization tables, half or more of the
|
|
1231 * row DCT calculations can be simplified this way.
|
|
1232 */
|
|
1233 if (isZeroInRow(dataUnit, rIndex)) {
|
|
1234 int dcVal = dataUnit[rIndex] << 2;
|
|
1235 for (int i = rIndex + 7; i >= rIndex; i--) {
|
|
1236 dataUnit[i] = dcVal;
|
|
1237 }
|
|
1238 } else {
|
|
1239 /**
|
|
1240 * Even part: reverse the even part of the forward DCT.
|
|
1241 * The rotator is sqrt(2)*c(-6).
|
|
1242 */
|
|
1243 int z2 = dataUnit[rIndex + 2];
|
|
1244 int z3 = dataUnit[rIndex + 6];
|
|
1245 int z1 = (z2 + z3) * FIX_0_541196100;
|
|
1246 int tmp2 = z1 + (z3 * (0 - FIX_1_847759065));
|
|
1247 int tmp3 = z1 + (z2 * FIX_0_765366865);
|
|
1248 int tmp0 = (dataUnit[rIndex] + dataUnit[rIndex + 4]) << 13;
|
|
1249 int tmp1 = (dataUnit[rIndex] - dataUnit[rIndex + 4]) << 13;
|
|
1250 int tmp10 = tmp0 + tmp3;
|
|
1251 int tmp13 = tmp0 - tmp3;
|
|
1252 int tmp11 = tmp1 + tmp2;
|
|
1253 int tmp12 = tmp1 - tmp2;
|
|
1254 /**
|
|
1255 * Odd part per figure 8; the matrix is unitary and hence its
|
|
1256 * transpose is its inverse. i0..i3 are y7,y5,y3,y1 respectively.
|
|
1257 */
|
|
1258 tmp0 = dataUnit[rIndex + 7];
|
|
1259 tmp1 = dataUnit[rIndex + 5];
|
|
1260 tmp2 = dataUnit[rIndex + 3];
|
|
1261 tmp3 = dataUnit[rIndex + 1];
|
|
1262 z1 = tmp0 + tmp3;
|
|
1263 z2 = tmp1 + tmp2;
|
|
1264 z3 = tmp0 + tmp2;
|
|
1265 int z4 = tmp1 + tmp3;
|
|
1266 int z5 = (z3 + z4) * FIX_1_175875602; /* sqrt(2) * c3 */
|
|
1267
|
|
1268 tmp0 *= FIX_0_298631336; /* sqrt(2) * (-c1+c3+c5-c7) */
|
|
1269 tmp1 *= FIX_2_053119869; /* sqrt(2) * ( c1+c3-c5+c7) */
|
|
1270 tmp2 *= FIX_3_072711026; /* sqrt(2) * ( c1+c3+c5-c7) */
|
|
1271 tmp3 *= FIX_1_501321110; /* sqrt(2) * ( c1+c3-c5-c7) */
|
|
1272 z1 *= 0 - FIX_0_899976223; /* sqrt(2) * (c7-c3) */
|
|
1273 z2 *= 0 - FIX_2_562915447; /* sqrt(2) * (-c1-c3) */
|
|
1274 z3 *= 0 - FIX_1_961570560; /* sqrt(2) * (-c3-c5) */
|
|
1275 z4 *= 0 - FIX_0_390180644; /* sqrt(2) * (c5-c3) */
|
|
1276
|
|
1277 z3 += z5;
|
|
1278 z4 += z5;
|
|
1279 tmp0 += z1 + z3;
|
|
1280 tmp1 += z2 + z4;
|
|
1281 tmp2 += z2 + z3;
|
|
1282 tmp3 += z1 + z4;
|
|
1283
|
|
1284 dataUnit[rIndex] = (tmp10 + tmp3 + 1024) >> 11;
|
|
1285 dataUnit[rIndex + 7] = (tmp10 - tmp3 + 1024) >> 11;
|
|
1286 dataUnit[rIndex + 1] = (tmp11 + tmp2 + 1024) >> 11;
|
|
1287 dataUnit[rIndex + 6] = (tmp11 - tmp2 + 1024) >> 11;
|
|
1288 dataUnit[rIndex + 2] = (tmp12 + tmp1 + 1024) >> 11;
|
|
1289 dataUnit[rIndex + 5] = (tmp12 - tmp1 + 1024) >> 11;
|
|
1290 dataUnit[rIndex + 3] = (tmp13 + tmp0 + 1024) >> 11;
|
|
1291 dataUnit[rIndex + 4] = (tmp13 - tmp0 + 1024) >> 11;
|
|
1292 }
|
|
1293 }
|
|
1294 /**
|
|
1295 * Pass 2: process columns.
|
|
1296 * Note that we must descale the results by a factor of 8 is 2**3,
|
|
1297 * and also undo the PASS1_BITS scaling.
|
|
1298 */
|
|
1299 for (int col = 0; col < 8; col++) {
|
|
1300 int c0 = col;
|
|
1301 int c1 = col + 8;
|
|
1302 int c2 = col + 16;
|
|
1303 int c3 = col + 24;
|
|
1304 int c4 = col + 32;
|
|
1305 int c5 = col + 40;
|
|
1306 int c6 = col + 48;
|
|
1307 int c7 = col + 56;
|
|
1308 if (isZeroInColumn(dataUnit, col)) {
|
|
1309 int dcVal = (dataUnit[c0] + 16) >> 5;
|
|
1310 dataUnit[c0] = dcVal;
|
|
1311 dataUnit[c1] = dcVal;
|
|
1312 dataUnit[c2] = dcVal;
|
|
1313 dataUnit[c3] = dcVal;
|
|
1314 dataUnit[c4] = dcVal;
|
|
1315 dataUnit[c5] = dcVal;
|
|
1316 dataUnit[c6] = dcVal;
|
|
1317 dataUnit[c7] = dcVal;
|
|
1318 } else {
|
|
1319 /**
|
|
1320 * Even part: reverse the even part of the forward DCT.
|
|
1321 * The rotator is sqrt(2)*c(-6).
|
|
1322 */
|
|
1323 int z0 = dataUnit[c0];
|
|
1324 int z2 = dataUnit[c2];
|
|
1325 int z3 = dataUnit[c6];
|
|
1326 int z4 = dataUnit[c4];
|
|
1327 int z1 = (z2 + z3) * FIX_0_541196100;
|
|
1328 int tmp2 = z1 + (z3 * (0 - FIX_1_847759065));
|
|
1329 int tmp3 = z1 + (z2 * FIX_0_765366865);
|
|
1330 int tmp0 = (z0 + z4) << 13;
|
|
1331 int tmp1 = (z0 - z4) << 13;
|
|
1332 int tmp10 = tmp0 + tmp3;
|
|
1333 int tmp13 = tmp0 - tmp3;
|
|
1334 int tmp11 = tmp1 + tmp2;
|
|
1335 int tmp12 = tmp1 - tmp2;
|
|
1336 /**
|
|
1337 * Odd part per figure 8; the matrix is unitary and hence its
|
|
1338 * transpose is its inverse. i0..i3 are y7,y5,y3,y1 respectively.
|
|
1339 */
|
|
1340 tmp0 = dataUnit[c7];
|
|
1341 tmp1 = dataUnit[c5];
|
|
1342 tmp2 = dataUnit[c3];
|
|
1343 tmp3 = dataUnit[c1];
|
|
1344 z1 = tmp0 + tmp3;
|
|
1345 z2 = tmp1 + tmp2;
|
|
1346 z3 = tmp0 + tmp2;
|
|
1347 z4 = tmp1 + tmp3;
|
|
1348 z0 = (z3 + z4) * FIX_1_175875602; /* sqrt(2) * c3 */
|
|
1349
|
|
1350 tmp0 *= FIX_0_298631336; /* sqrt(2) * (-c1+c3+c5-c7) */
|
|
1351 tmp1 *= FIX_2_053119869; /* sqrt(2) * ( c1+c3-c5+c7) */
|
|
1352 tmp2 *= FIX_3_072711026; /* sqrt(2) * ( c1+c3+c5-c7) */
|
|
1353 tmp3 *= FIX_1_501321110; /* sqrt(2) * ( c1+c3-c5-c7) */
|
|
1354 z1 *= 0 - FIX_0_899976223; /* sqrt(2) * (c7-c3) */
|
|
1355 z2 *= 0 - FIX_2_562915447; /* sqrt(2) * (-c1-c3) */
|
|
1356 z3 *= 0 - FIX_1_961570560; /* sqrt(2) * (-c3-c5) */
|
|
1357 z4 *= 0 - FIX_0_390180644; /* sqrt(2) * (c5-c3) */
|
|
1358
|
|
1359 z3 += z0;
|
|
1360 z4 += z0;
|
|
1361
|
|
1362 tmp0 += z1 + z3;
|
|
1363 tmp1 += z2 + z4;
|
|
1364 tmp2 += z2 + z3;
|
|
1365 tmp3 += z1 + z4;
|
|
1366
|
|
1367 /* Final output stage: inputs are tmp10..tmp13, tmp0..tmp3 */
|
|
1368 dataUnit[c0] = (tmp10 + tmp3 + 131072) >> 18;
|
|
1369 dataUnit[c7] = (tmp10 - tmp3 + 131072) >> 18;
|
|
1370 dataUnit[c1] = (tmp11 + tmp2 + 131072) >> 18;
|
|
1371 dataUnit[c6] = (tmp11 - tmp2 + 131072) >> 18;
|
|
1372 dataUnit[c2] = (tmp12 + tmp1 + 131072) >> 18;
|
|
1373 dataUnit[c5] = (tmp12 - tmp1 + 131072) >> 18;
|
|
1374 dataUnit[c3] = (tmp13 + tmp0 + 131072) >> 18;
|
|
1375 dataUnit[c4] = (tmp13 - tmp0 + 131072) >> 18;
|
|
1376 }
|
|
1377 }
|
|
1378 }
|
|
1379 override bool isFileFormat(LEDataInputStream stream) {
|
|
1380 try {
|
|
1381 JPEGStartOfImage soi = new JPEGStartOfImage(stream);
|
|
1382 stream.unread(soi.reference);
|
|
1383 return soi.verify(); // we no longer check for appN
|
|
1384 } catch (Exception e) {
|
|
1385 return false;
|
|
1386 }
|
|
1387 }
|
|
1388 bool isZeroInColumn(int[] dataUnit, int col) {
|
|
1389 return dataUnit[col + 8] is 0 && dataUnit[col + 16] is 0
|
|
1390 && dataUnit[col + 24] is 0 && dataUnit[col + 32] is 0
|
|
1391 && dataUnit[col + 40] is 0 && dataUnit[col + 48] is 0
|
|
1392 && dataUnit[col + 56] is 0;
|
|
1393 }
|
|
1394 bool isZeroInRow(int[] dataUnit, int rIndex) {
|
|
1395 return dataUnit[rIndex + 1] is 0 && dataUnit[rIndex + 2] is 0
|
|
1396 && dataUnit[rIndex + 3] is 0 && dataUnit[rIndex + 4] is 0
|
|
1397 && dataUnit[rIndex + 5] is 0 && dataUnit[rIndex + 6] is 0
|
|
1398 && dataUnit[rIndex + 7] is 0;
|
|
1399 }
|
|
1400 override ImageData[] loadFromByteStream() {
|
|
1401 //TEMPORARY CODE
|
|
1402 //PORTING_FIXME
|
|
1403 if (/+System.getProperty("org.eclipse.swt.internal.image.JPEGFileFormat_3.2") is null+/ true ) {
|
|
1404 return JPEGDecoder.loadFromByteStream(inputStream, loader);
|
|
1405 }
|
|
1406 JPEGStartOfImage soi = new JPEGStartOfImage(inputStream);
|
|
1407 if (!soi.verify()) SWT.error(SWT.ERROR_INVALID_IMAGE);
|
|
1408 restartInterval = 0;
|
|
1409
|
|
1410 /* Process the tables preceding the frame header. */
|
|
1411 processTables();
|
|
1412
|
|
1413 /* Start of Frame. */
|
|
1414 frameHeader = new JPEGFrameHeader(inputStream);
|
|
1415 if (!frameHeader.verify()) SWT.error(SWT.ERROR_INVALID_IMAGE);
|
|
1416 imageWidth = frameHeader.getSamplesPerLine();
|
|
1417 imageHeight = frameHeader.getNumberOfLines();
|
|
1418 maxH = frameHeader.getMaxHFactor();
|
|
1419 maxV = frameHeader.getMaxVFactor();
|
|
1420 int mcuWidth = maxH * DCTSIZE;
|
|
1421 int mcuHeight = maxV * DCTSIZE;
|
|
1422 interleavedMcuCols = (imageWidth + mcuWidth - 1) / mcuWidth;
|
|
1423 interleavedMcuRows = (imageHeight + mcuHeight - 1) / mcuHeight;
|
|
1424 progressive = frameHeader.isProgressive();
|
|
1425 samplePrecision = frameHeader.getSamplePrecision();
|
|
1426 nComponents = frameHeader.getNumberOfImageComponents();
|
|
1427 frameComponents = frameHeader.componentParameters;
|
|
1428 componentIds = frameHeader.componentIdentifiers;
|
|
1429 imageComponents = new byte[][](nComponents);
|
|
1430 if (progressive) {
|
|
1431 // Progressive jpeg: need to keep all of the data units.
|
|
1432 dataUnits = new int[][][](nComponents);
|
|
1433 } else {
|
|
1434 // Sequential jpeg: only need one data unit.
|
|
1435 dataUnit = new int[8 * 8];
|
|
1436 }
|
|
1437 for (int i = 0; i < nComponents; i++) {
|
|
1438 int[] frameComponent = frameComponents[componentIds[i]];
|
|
1439 int bufferSize = frameComponent[CW] * frameComponent[CH];
|
|
1440 imageComponents[i] = new byte[bufferSize];
|
|
1441 if (progressive) {
|
|
1442 dataUnits[i] = new int[][](bufferSize);
|
|
1443 }
|
|
1444 }
|
|
1445
|
|
1446 /* Process the tables preceding the scan header. */
|
|
1447 processTables();
|
|
1448
|
|
1449 /* Start of Scan. */
|
|
1450 scanHeader = new JPEGScanHeader(inputStream);
|
|
1451 if (!scanHeader.verify()) SWT.error(SWT.ERROR_INVALID_IMAGE);
|
|
1452
|
|
1453 /* Process scan(s) and further tables until EOI. */
|
|
1454 int progressiveScanCount = 0;
|
|
1455 bool done = false;
|
|
1456 while(!done) {
|
|
1457 resetInputBuffer();
|
|
1458 precedingDCs = new int[4];
|
|
1459 decodeScan();
|
|
1460 if (progressive && loader.hasListeners()) {
|
|
1461 ImageData imageData = createImageData();
|
|
1462 loader.notifyListeners(new ImageLoaderEvent(loader, imageData, progressiveScanCount, false));
|
|
1463 progressiveScanCount++;
|
|
1464 }
|
|
1465
|
|
1466 /* Unread any buffered data before looking for tables again. */
|
|
1467 int delta = 512 - bufferCurrentPosition - 1;
|
|
1468 if (delta > 0) {
|
|
1469 byte[] unreadBuffer = new byte[delta];
|
|
1470 System.arraycopy(dataBuffer, bufferCurrentPosition + 1, unreadBuffer, 0, delta);
|
|
1471 try {
|
|
1472 inputStream.unread(unreadBuffer);
|
|
1473 } catch (IOException e) {
|
|
1474 SWT.error(SWT.ERROR_IO, e);
|
|
1475 }
|
|
1476 }
|
|
1477
|
|
1478 /* Process the tables preceding the next scan header. */
|
|
1479 JPEGSegment jpegSegment = processTables();
|
|
1480 if (jpegSegment is null || jpegSegment.getSegmentMarker() is EOI) {
|
|
1481 done = true;
|
|
1482 } else {
|
|
1483 scanHeader = new JPEGScanHeader(inputStream);
|
|
1484 if (!scanHeader.verify()) SWT.error(SWT.ERROR_INVALID_IMAGE);
|
|
1485 }
|
|
1486 }
|
|
1487
|
|
1488 if (progressive) {
|
|
1489 for (int ymcu = 0; ymcu < interleavedMcuRows; ymcu++) {
|
|
1490 for (int xmcu = 0; xmcu < interleavedMcuCols; xmcu++) {
|
|
1491 for (int iComp = 0; iComp < nComponents; iComp++) {
|
|
1492 int[] frameComponent = frameComponents[componentIds[iComp]];
|
|
1493 int hi = frameComponent[HI];
|
|
1494 int vi = frameComponent[VI];
|
|
1495 int compWidth = frameComponent[CW];
|
|
1496 for (int ivi = 0; ivi < vi; ivi++) {
|
|
1497 for (int ihi = 0; ihi < hi; ihi++) {
|
|
1498 int index = (ymcu * vi + ivi) * compWidth + xmcu * hi + ihi;
|
|
1499 dataUnit = dataUnits[iComp][index];
|
|
1500 dequantize(dataUnit, iComp);
|
|
1501 inverseDCT(dataUnit);
|
|
1502 storeData(dataUnit, iComp, xmcu, ymcu, hi, ihi, vi, ivi);
|
|
1503 }
|
|
1504 }
|
|
1505 }
|
|
1506 }
|
|
1507 }
|
|
1508 dataUnits = null; // release memory
|
|
1509 }
|
|
1510 ImageData imageData = createImageData();
|
|
1511 if (progressive && loader.hasListeners()) {
|
|
1512 loader.notifyListeners(new ImageLoaderEvent(loader, imageData, progressiveScanCount, true));
|
|
1513 }
|
|
1514 return [imageData];
|
|
1515 }
|
|
1516 ImageData createImageData() {
|
|
1517 return ImageData.internal_new(
|
|
1518 imageWidth,
|
|
1519 imageHeight,
|
|
1520 nComponents * samplePrecision,
|
|
1521 setUpPalette(),
|
|
1522 nComponents is 1 ? 4 : 1,
|
|
1523 decodeImageComponents(),
|
|
1524 0,
|
|
1525 null,
|
|
1526 null,
|
|
1527 -1,
|
|
1528 -1,
|
|
1529 SWT.IMAGE_JPEG,
|
|
1530 0,
|
|
1531 0,
|
|
1532 0,
|
|
1533 0);
|
|
1534 }
|
|
1535 int nextBit() {
|
|
1536 if (currentBitCount !is 0) {
|
|
1537 currentBitCount--;
|
|
1538 currentByte *= 2;
|
|
1539 if (currentByte > 255) {
|
|
1540 currentByte -= 256;
|
|
1541 return 1;
|
|
1542 } else {
|
|
1543 return 0;
|
|
1544 }
|
|
1545 }
|
|
1546 bufferCurrentPosition++;
|
|
1547 if (bufferCurrentPosition >= 512) {
|
|
1548 resetInputBuffer();
|
|
1549 bufferCurrentPosition = 0;
|
|
1550 }
|
|
1551 currentByte = dataBuffer[bufferCurrentPosition] & 0xFF;
|
|
1552 currentBitCount = 8;
|
|
1553 byte nextByte;
|
|
1554 if (bufferCurrentPosition is 511) {
|
|
1555 resetInputBuffer();
|
|
1556 currentBitCount = 8;
|
|
1557 nextByte = dataBuffer[0];
|
|
1558 } else {
|
|
1559 nextByte = dataBuffer[bufferCurrentPosition + 1];
|
|
1560 }
|
|
1561 if (currentByte is 0xFF) {
|
|
1562 if (nextByte is 0) {
|
|
1563 bufferCurrentPosition ++;
|
|
1564 currentBitCount--;
|
|
1565 currentByte *= 2;
|
|
1566 if (currentByte > 255) {
|
|
1567 currentByte -= 256;
|
|
1568 return 1;
|
|
1569 } else {
|
|
1570 return 0;
|
|
1571 }
|
|
1572 } else {
|
|
1573 if ((nextByte & 0xFF) + 0xFF00 is DNL) {
|
|
1574 getDNL();
|
|
1575 return 0;
|
|
1576 } else {
|
|
1577 SWT.error(SWT.ERROR_INVALID_IMAGE);
|
|
1578 return 0;
|
|
1579 }
|
|
1580 }
|
|
1581 } else {
|
|
1582 currentBitCount--;
|
|
1583 currentByte *= 2;
|
|
1584 if (currentByte > 255) {
|
|
1585 currentByte -= 256;
|
|
1586 return 1;
|
|
1587 } else {
|
|
1588 return 0;
|
|
1589 }
|
|
1590 }
|
|
1591 }
|
|
1592 void processRestartInterval() {
|
|
1593 do {
|
|
1594 bufferCurrentPosition++;
|
|
1595 if (bufferCurrentPosition > 511) {
|
|
1596 resetInputBuffer();
|
|
1597 bufferCurrentPosition = 0;
|
|
1598 }
|
|
1599 currentByte = dataBuffer[bufferCurrentPosition] & 0xFF;
|
|
1600 } while (currentByte !is 0xFF);
|
|
1601 while (currentByte is 0xFF) {
|
|
1602 bufferCurrentPosition++;
|
|
1603 if (bufferCurrentPosition > 511) {
|
|
1604 resetInputBuffer();
|
|
1605 bufferCurrentPosition = 0;
|
|
1606 }
|
|
1607 currentByte = dataBuffer[bufferCurrentPosition] & 0xFF;
|
|
1608 }
|
|
1609 if (currentByte !is ((RST0 + nextRestartNumber) & 0xFF)) {
|
|
1610 SWT.error(SWT.ERROR_INVALID_IMAGE);
|
|
1611 }
|
|
1612 bufferCurrentPosition++;
|
|
1613 if (bufferCurrentPosition > 511) {
|
|
1614 resetInputBuffer();
|
|
1615 bufferCurrentPosition = 0;
|
|
1616 }
|
|
1617 currentByte = dataBuffer[bufferCurrentPosition] & 0xFF;
|
|
1618 currentBitCount = 8;
|
|
1619 restartsToGo = restartInterval;
|
|
1620 nextRestartNumber = (nextRestartNumber + 1) & 0x7;
|
|
1621 precedingDCs = new int[4];
|
|
1622 eobrun = 0;
|
|
1623 }
|
|
1624 /* Process all markers until a frame header, scan header, or EOI is found. */
|
|
1625 JPEGSegment processTables() {
|
|
1626 while (true) {
|
|
1627 JPEGSegment jpegSegment = seekUnspecifiedMarker(inputStream);
|
|
1628 if (jpegSegment is null) return null;
|
|
1629 JPEGFrameHeader sof = new JPEGFrameHeader(jpegSegment.reference);
|
|
1630 if (sof.verify()) {
|
|
1631 return jpegSegment;
|
|
1632 }
|
|
1633 int marker = jpegSegment.getSegmentMarker();
|
|
1634 switch (marker) {
|
|
1635 case SOI: // there should only be one SOI per file
|
|
1636 SWT.error(SWT.ERROR_INVALID_IMAGE);
|
|
1637 case EOI:
|
|
1638 case SOS:
|
|
1639 return jpegSegment;
|
|
1640 case DQT:
|
|
1641 getDQT();
|
|
1642 break;
|
|
1643 case DHT:
|
|
1644 getDHT();
|
|
1645 break;
|
|
1646 case DAC:
|
|
1647 getDAC();
|
|
1648 break;
|
|
1649 case DRI:
|
|
1650 getDRI();
|
|
1651 break;
|
|
1652 case APP0:
|
|
1653 getAPP0();
|
|
1654 break;
|
|
1655 case COM:
|
|
1656 getCOM();
|
|
1657 break;
|
|
1658 default:
|
|
1659 skipSegmentFrom(inputStream);
|
|
1660
|
|
1661 }
|
|
1662 }
|
|
1663 }
|
|
1664 void quantizeData(int[] dataUnit, int iComp) {
|
|
1665 int[] qTable = quantizationTables[frameComponents[componentIds[iComp]][TQI]];
|
|
1666 for (int i = 0; i < dataUnit.length; i++) {
|
|
1667 int zzIndex = ZigZag8x8[i];
|
|
1668 int data = dataUnit[zzIndex];
|
|
1669 int absData = data < 0 ? 0 - data : data;
|
|
1670 int qValue = qTable[i];
|
|
1671 int q2 = qValue >> 1;
|
|
1672 absData += q2;
|
|
1673 if (absData < qValue) {
|
|
1674 dataUnit[zzIndex] = 0;
|
|
1675 } else {
|
|
1676 absData /= qValue;
|
|
1677 if (data >= 0) {
|
|
1678 dataUnit[zzIndex] = absData;
|
|
1679 } else {
|
|
1680 dataUnit[zzIndex] = 0 - absData;
|
|
1681 }
|
|
1682 }
|
|
1683 }
|
|
1684 }
|
|
1685 int receive(int nBits) {
|
|
1686 int v = 0;
|
|
1687 for (int i = 0; i < nBits; i++) {
|
|
1688 v = v * 2 + nextBit();
|
|
1689 }
|
|
1690 return v;
|
|
1691 }
|
|
1692 void resetInputBuffer() {
|
|
1693 if (dataBuffer is null) {
|
|
1694 dataBuffer = new byte[512];
|
|
1695 }
|
|
1696 try {
|
|
1697 inputStream.read(dataBuffer);
|
|
1698 } catch (IOException e) {
|
|
1699 SWT.error(SWT.ERROR_IO, e);
|
|
1700 }
|
|
1701 currentBitCount = 0;
|
|
1702 bufferCurrentPosition = -1;
|
|
1703 }
|
|
1704 void resetOutputBuffer() {
|
|
1705 if (dataBuffer is null) {
|
|
1706 dataBuffer = new byte[512];
|
|
1707 } else {
|
|
1708 try {
|
|
1709 outputStream.write(dataBuffer, 0, bufferCurrentPosition);
|
|
1710 } catch (IOException e) {
|
|
1711 SWT.error(SWT.ERROR_IO, e);
|
|
1712 }
|
|
1713 }
|
|
1714 bufferCurrentPosition = 0;
|
|
1715 }
|
|
1716 static JPEGSegment seekUnspecifiedMarker(LEDataInputStream byteStream) {
|
|
1717 byte[] byteArray = new byte[2];
|
|
1718 try {
|
|
1719 while (true) {
|
|
1720 if (byteStream.read(byteArray, 0, 1) !is 1) return null;
|
|
1721 if (byteArray[0] is cast(byte) 0xFF) {
|
|
1722 if (byteStream.read(byteArray, 1, 1) !is 1) return null;
|
|
1723 if (byteArray[1] !is cast(byte) 0xFF && byteArray[1] !is 0) {
|
|
1724 byteStream.unread(byteArray);
|
|
1725 return new JPEGSegment(byteArray);
|
|
1726 }
|
|
1727 }
|
|
1728 }
|
|
1729 } catch (IOException e) {
|
|
1730 SWT.error(SWT.ERROR_IO, e);
|
|
1731 }
|
|
1732 return null;
|
|
1733 }
|
|
1734 PaletteData setUpPalette() {
|
|
1735 if (nComponents is 1) {
|
|
1736 RGB[] entries = new RGB[256];
|
|
1737 for (int i = 0; i < 256; i++) {
|
|
1738 entries[i] = new RGB(i, i, i);
|
|
1739 }
|
|
1740 return new PaletteData(entries);
|
|
1741 }
|
|
1742 return new PaletteData(0xFF, 0xFF00, 0xFF0000);
|
|
1743 }
|
|
1744 static void skipSegmentFrom(LEDataInputStream byteStream) {
|
|
1745 try {
|
|
1746 byte[] byteArray = new byte[4];
|
|
1747 JPEGSegment jpegSegment = new JPEGSegment(byteArray);
|
|
1748
|
|
1749 if (byteStream.read(byteArray) !is byteArray.length) {
|
|
1750 SWT.error(SWT.ERROR_INVALID_IMAGE);
|
|
1751 }
|
|
1752 if (!(byteArray[0] is -1 && byteArray[1] !is 0 && byteArray[1] !is -1)) {
|
|
1753 SWT.error(SWT.ERROR_INVALID_IMAGE);
|
|
1754 }
|
|
1755 int delta = jpegSegment.getSegmentLength() - 2;
|
|
1756 byteStream.skip(delta);
|
|
1757 } catch (Exception e) {
|
|
1758 SWT.error(SWT.ERROR_IO, e);
|
|
1759 }
|
|
1760 }
|
|
1761 void storeData(int[] dataUnit, int iComp, int xmcu, int ymcu, int hi, int ihi, int vi, int ivi) {
|
|
1762 byte[] compImage = imageComponents[iComp];
|
|
1763 int[] frameComponent = frameComponents[componentIds[iComp]];
|
|
1764 int compWidth = frameComponent[CW];
|
|
1765 int destIndex = ((ymcu * vi + ivi) * compWidth * DCTSIZE) + ((xmcu * hi + ihi) * DCTSIZE);
|
|
1766 int srcIndex = 0;
|
|
1767 for (int i = 0; i < DCTSIZE; i++) {
|
|
1768 for (int col = 0; col < DCTSIZE; col++) {
|
|
1769 int x = dataUnit[srcIndex] + 128;
|
|
1770 if (x < 0) {
|
|
1771 x = 0;
|
|
1772 } else {
|
|
1773 if (x > 255) x = 255;
|
|
1774 }
|
|
1775 compImage[destIndex + col] = cast(byte)x;
|
|
1776 srcIndex++;
|
|
1777 }
|
|
1778 destIndex += compWidth;
|
|
1779 }
|
|
1780 }
|
|
1781 override void unloadIntoByteStream(ImageLoader loader) {
|
|
1782 ImageData image = loader.data[0];
|
|
1783 if (!(new JPEGStartOfImage()).writeToStream(outputStream)) {
|
|
1784 SWT.error(SWT.ERROR_IO);
|
|
1785 }
|
|
1786 JPEGAppn appn = new JPEGAppn([cast(byte)0xFF, cast(byte)0xE0, 0, 0x10, 0x4A, 0x46, 0x49, 0x46, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0]);
|
|
1787 if (!appn.writeToStream(outputStream)) {
|
|
1788 SWT.error(SWT.ERROR_IO);
|
|
1789 }
|
|
1790 quantizationTables = new int[][](4);
|
|
1791 JPEGQuantizationTable chromDQT = JPEGQuantizationTable.defaultChrominanceTable();
|
|
1792 chromDQT.scaleBy(encoderQFactor);
|
|
1793 int[] jpegDQTKeys = chromDQT.getQuantizationTablesKeys();
|
|
1794 int[][] jpegDQTValues = chromDQT.getQuantizationTablesValues();
|
|
1795 for (int i = 0; i < jpegDQTKeys.length; i++) {
|
|
1796 quantizationTables[jpegDQTKeys[i]] = jpegDQTValues[i];
|
|
1797 }
|
|
1798 JPEGQuantizationTable lumDQT = JPEGQuantizationTable.defaultLuminanceTable();
|
|
1799 lumDQT.scaleBy(encoderQFactor);
|
|
1800 jpegDQTKeys = lumDQT.getQuantizationTablesKeys();
|
|
1801 jpegDQTValues = lumDQT.getQuantizationTablesValues();
|
|
1802 for (int i = 0; i < jpegDQTKeys.length; i++) {
|
|
1803 quantizationTables[jpegDQTKeys[i]] = jpegDQTValues[i];
|
|
1804 }
|
|
1805 if (!lumDQT.writeToStream(outputStream)) {
|
|
1806 SWT.error(SWT.ERROR_IO);
|
|
1807 }
|
|
1808 if (!chromDQT.writeToStream(outputStream)) {
|
|
1809 SWT.error(SWT.ERROR_IO);
|
|
1810 }
|
|
1811 int frameLength, scanLength, precision;
|
|
1812 int[][] frameParams, scanParams;
|
|
1813 if (image.depth is 1) {
|
|
1814 frameLength = 11;
|
|
1815 frameParams = new int[][](1);
|
|
1816 frameParams[0] = [1, 1, 1, 0, 0];
|
|
1817 scanParams = new int[][](1);
|
|
1818 scanParams[0] = [0, 0];
|
|
1819 scanLength = 8;
|
|
1820 nComponents = 1;
|
|
1821 precision = 1;
|
|
1822 } else {
|
|
1823 frameLength = 17;
|
|
1824 frameParams = new int[][](3);
|
|
1825 frameParams[0] = [0, 2, 2, 0, 0];
|
|
1826 frameParams[1] = [1, 1, 1, 0, 0];
|
|
1827 frameParams[2] = [1, 1, 1, 0, 0];
|
|
1828 scanParams = new int[][](3);
|
|
1829 scanParams[0] = [0, 0];
|
|
1830 scanParams[1] = [1, 1];
|
|
1831 scanParams[2] = [1, 1];
|
|
1832 scanLength = 12;
|
|
1833 nComponents = 3;
|
|
1834 precision = 8;
|
|
1835 }
|
|
1836 imageWidth = image.width;
|
|
1837 imageHeight = image.height;
|
|
1838 frameHeader = new JPEGFrameHeader(new byte[19]);
|
|
1839 frameHeader.setSegmentMarker(SOF0);
|
|
1840 frameHeader.setSegmentLength(frameLength);
|
|
1841 frameHeader.setSamplePrecision(precision);
|
|
1842 frameHeader.setSamplesPerLine(imageWidth);
|
|
1843 frameHeader.setNumberOfLines(imageHeight);
|
|
1844 frameHeader.setNumberOfImageComponents(nComponents);
|
|
1845 frameHeader.componentParameters = frameParams;
|
|
1846 frameHeader.componentIdentifiers = [0, 1, 2];
|
|
1847 frameHeader.initializeContents();
|
|
1848 if (!frameHeader.writeToStream(outputStream)) {
|
|
1849 SWT.error(SWT.ERROR_IO);
|
|
1850 }
|
|
1851 frameComponents = frameParams;
|
|
1852 componentIds = frameHeader.componentIdentifiers;
|
|
1853 maxH = frameHeader.getMaxHFactor();
|
|
1854 maxV = frameHeader.getMaxVFactor();
|
|
1855 int mcuWidth = maxH * DCTSIZE;
|
|
1856 int mcuHeight = maxV * DCTSIZE;
|
|
1857 interleavedMcuCols = (imageWidth + mcuWidth - 1) / mcuWidth;
|
|
1858 interleavedMcuRows = (imageHeight + mcuHeight - 1) / mcuHeight;
|
|
1859 acHuffmanTables = new JPEGHuffmanTable[4];
|
|
1860 dcHuffmanTables = new JPEGHuffmanTable[4];
|
|
1861 JPEGHuffmanTable[] dhtTables = [
|
|
1862 JPEGHuffmanTable.getDefaultDCLuminanceTable(),
|
|
1863 JPEGHuffmanTable.getDefaultDCChrominanceTable(),
|
|
1864 JPEGHuffmanTable.getDefaultACLuminanceTable(),
|
|
1865 JPEGHuffmanTable.getDefaultACChrominanceTable()
|
|
1866 ];
|
|
1867 for (int i = 0; i < dhtTables.length; i++) {
|
|
1868 JPEGHuffmanTable dhtTable = dhtTables[i];
|
|
1869 if (!dhtTable.writeToStream(outputStream)) {
|
|
1870 SWT.error(SWT.ERROR_IO);
|
|
1871 }
|
|
1872 JPEGHuffmanTable[] allTables = dhtTable.getAllTables();
|
|
1873 for (int j = 0; j < allTables.length; j++) {
|
|
1874 JPEGHuffmanTable huffmanTable = allTables[j];
|
|
1875 if (huffmanTable.getTableClass() is 0) {
|
|
1876 dcHuffmanTables[huffmanTable.getTableIdentifier()] = huffmanTable;
|
|
1877 } else {
|
|
1878 acHuffmanTables[huffmanTable.getTableIdentifier()] = huffmanTable;
|
|
1879 }
|
|
1880 }
|
|
1881 }
|
|
1882 precedingDCs = new int[4];
|
|
1883 scanHeader = new JPEGScanHeader(new byte[14]);
|
|
1884 scanHeader.setSegmentMarker(SOS);
|
|
1885 scanHeader.setSegmentLength(scanLength);
|
|
1886 scanHeader.setNumberOfImageComponents(nComponents);
|
|
1887 scanHeader.setStartOfSpectralSelection(0);
|
|
1888 scanHeader.setEndOfSpectralSelection(63);
|
|
1889 scanHeader.componentParameters = scanParams;
|
|
1890 scanHeader.initializeContents();
|
|
1891 if (!scanHeader.writeToStream(outputStream)) {
|
|
1892 SWT.error(SWT.ERROR_IO);
|
|
1893 }
|
|
1894 convertImageToYCbCr(image);
|
|
1895 resetOutputBuffer();
|
|
1896 currentByte = 0;
|
|
1897 currentBitCount = 0;
|
|
1898 encodeScan();
|
|
1899 if (!(new JPEGEndOfImage()).writeToStream(outputStream)) {
|
|
1900 SWT.error(SWT.ERROR_IO);
|
|
1901 }
|
|
1902 }
|
|
1903 }
|