blob: b732930ae197150730132faaa5aa2107769f3a5c [file] [log] [blame]
DRC3bad53f2011-02-23 02:20:49 +00001/*
2 * Copyright (C)2011 D. R. Commander. All Rights Reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * - Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
9 * - Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation
11 * and/or other materials provided with the distribution.
12 * - Neither the name of the libjpeg-turbo Project nor the names of its
13 * contributors may be used to endorse or promote products derived from this
14 * software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS",
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/*
30 * This program tests the various code paths in the TurboJPEG JNI Wrapper
31 */
32
33import java.io.*;
34import java.util.*;
DRC84a1bcc2011-02-23 12:09:56 +000035import java.awt.image.*;
36import javax.imageio.*;
DRCc08e8c12011-09-08 23:54:40 +000037import java.nio.*;
DRC3bad53f2011-02-23 02:20:49 +000038import org.libjpegturbo.turbojpeg.*;
39
40public class TJUnitTest {
41
DRCf7f3ea42011-03-01 20:03:32 +000042 private static final String classname =
43 new TJUnitTest().getClass().getName();
DRC3bad53f2011-02-23 02:20:49 +000044
DRCf7f3ea42011-03-01 20:03:32 +000045 private static void usage() {
46 System.out.println("\nUSAGE: java " + classname + " [options]\n");
47 System.out.println("Options:\n");
48 System.out.println("-yuv = test YUV encoding/decoding support\n");
49 System.out.println("-bi = test BufferedImage support\n");
50 System.exit(1);
51 }
DRC3bad53f2011-02-23 02:20:49 +000052
DRCf7f3ea42011-03-01 20:03:32 +000053 private final static String subNameLong[] = {
DRCd0a81362011-03-04 13:04:24 +000054 "4:4:4", "4:2:2", "4:2:0", "GRAY", "4:4:0"
DRCf7f3ea42011-03-01 20:03:32 +000055 };
56 private final static String subName[] = {
DRCd0a81362011-03-04 13:04:24 +000057 "444", "422", "420", "GRAY", "440"
DRCf7f3ea42011-03-01 20:03:32 +000058 };
DRC3bad53f2011-02-23 02:20:49 +000059
DRCf7f3ea42011-03-01 20:03:32 +000060 private final static String pixFormatStr[] = {
61 "RGB", "BGR", "RGBX", "BGRX", "XBGR", "XRGB", "Grayscale"
62 };
DRCc08e8c12011-09-08 23:54:40 +000063
64 private final static int alphaOffset[] = {
65 -1, -1, 3, 3, 0, 0, -1
DRCf7f3ea42011-03-01 20:03:32 +000066 };
DRC3bad53f2011-02-23 02:20:49 +000067
DRCf7f3ea42011-03-01 20:03:32 +000068 private final static int _3byteFormats[] = {
69 TJ.PF_RGB, TJ.PF_BGR
70 };
71 private final static int _3byteFormatsBI[] = {
DRCc08e8c12011-09-08 23:54:40 +000072 BufferedImage.TYPE_3BYTE_BGR
DRCf7f3ea42011-03-01 20:03:32 +000073 };
74 private final static int _4byteFormats[] = {
75 TJ.PF_RGBX, TJ.PF_BGRX, TJ.PF_XBGR, TJ.PF_XRGB
76 };
77 private final static int _4byteFormatsBI[] = {
DRCc08e8c12011-09-08 23:54:40 +000078 BufferedImage.TYPE_INT_BGR, BufferedImage.TYPE_INT_RGB,
79 BufferedImage.TYPE_4BYTE_ABGR, BufferedImage.TYPE_4BYTE_ABGR_PRE,
80 BufferedImage.TYPE_INT_ARGB, BufferedImage.TYPE_INT_ARGB_PRE
DRCf7f3ea42011-03-01 20:03:32 +000081 };
82 private final static int onlyGray[] = {
83 TJ.PF_GRAY
84 };
DRCc08e8c12011-09-08 23:54:40 +000085 private final static int onlyGrayBI[] = {
86 BufferedImage.TYPE_BYTE_GRAY
87 };
DRCf7f3ea42011-03-01 20:03:32 +000088 private final static int onlyRGB[] = {
89 TJ.PF_RGB
90 };
DRC3bad53f2011-02-23 02:20:49 +000091
DRCf7f3ea42011-03-01 20:03:32 +000092 private final static int YUVENCODE = 1;
93 private final static int YUVDECODE = 2;
94 private static int yuv = 0;
95 private static boolean bi = false;
DRC3bad53f2011-02-23 02:20:49 +000096
DRCf7f3ea42011-03-01 20:03:32 +000097 private static int exitStatus = 0;
DRC3bad53f2011-02-23 02:20:49 +000098
DRCc08e8c12011-09-08 23:54:40 +000099 private static int biTypePF(int biType) {
100 ByteOrder byteOrder = ByteOrder.nativeOrder();
101 switch(biType) {
102 case BufferedImage.TYPE_3BYTE_BGR:
103 return TJ.PF_BGR;
104 case BufferedImage.TYPE_4BYTE_ABGR:
105 case BufferedImage.TYPE_4BYTE_ABGR_PRE:
106 return TJ.PF_XBGR;
107 case BufferedImage.TYPE_BYTE_GRAY:
108 return TJ.PF_GRAY;
109 case BufferedImage.TYPE_INT_BGR:
110 if(byteOrder == ByteOrder.BIG_ENDIAN)
111 return TJ.PF_XBGR;
112 else
113 return TJ.PF_RGBX;
114 case BufferedImage.TYPE_INT_RGB:
115 case BufferedImage.TYPE_INT_ARGB:
116 case BufferedImage.TYPE_INT_ARGB_PRE:
117 if(byteOrder == ByteOrder.BIG_ENDIAN)
118 return TJ.PF_XRGB;
119 else
120 return TJ.PF_BGRX;
121 }
122 return 0;
123 }
124
125 private static String biTypeStr(int biType) {
126 switch(biType) {
127 case BufferedImage.TYPE_3BYTE_BGR:
128 return "3BYTE_BGR";
129 case BufferedImage.TYPE_4BYTE_ABGR:
130 return "4BYTE_ABGR";
131 case BufferedImage.TYPE_4BYTE_ABGR_PRE:
132 return "4BYTE_ABGR_PRE";
133 case BufferedImage.TYPE_BYTE_GRAY:
134 return "BYTE_GRAY";
135 case BufferedImage.TYPE_INT_BGR:
136 return "INT_BGR";
137 case BufferedImage.TYPE_INT_RGB:
138 return "INT_RGB";
139 case BufferedImage.TYPE_INT_ARGB:
140 return "INT_ARGB";
141 case BufferedImage.TYPE_INT_ARGB_PRE:
142 return "INT_ARGB_PRE";
143 }
144 return "Unknown";
145 }
146
147
DRCf7f3ea42011-03-01 20:03:32 +0000148 private static double getTime() {
149 return (double)System.nanoTime() / 1.0e9;
150 }
DRC3bad53f2011-02-23 02:20:49 +0000151
DRCf7f3ea42011-03-01 20:03:32 +0000152 private static void initBuf(byte[] buf, int w, int pitch, int h, int pf,
153 int flags) throws Exception {
DRC2c74e512011-03-16 00:02:53 +0000154 int roffset = TJ.getRedOffset(pf);
155 int goffset = TJ.getGreenOffset(pf);
156 int boffset = TJ.getBlueOffset(pf);
DRCc08e8c12011-09-08 23:54:40 +0000157 int aoffset = alphaOffset[pf];
DRCf7f3ea42011-03-01 20:03:32 +0000158 int ps = TJ.getPixelSize(pf);
DRCf962fbb2011-05-23 05:49:08 +0000159 int index, row, col, halfway = 16;
DRC3bad53f2011-02-23 02:20:49 +0000160
DRCf7f3ea42011-03-01 20:03:32 +0000161 Arrays.fill(buf, (byte)0);
162 if(pf == TJ.PF_GRAY) {
DRCf962fbb2011-05-23 05:49:08 +0000163 for(row = 0; row < h; row++) {
164 for(col = 0; col < w; col++) {
165 if((flags & TJ.FLAG_BOTTOMUP) != 0)
166 index = pitch * (h - row - 1) + col;
167 else index = pitch * row + col;
168 if(((row / 8) + (col / 8)) % 2 == 0)
169 buf[index] = (row < halfway) ? (byte)255 : 0;
170 else buf[index] = (row < halfway) ? 76 : (byte)226;
DRCf7f3ea42011-03-01 20:03:32 +0000171 }
172 }
173 return;
174 }
DRCf962fbb2011-05-23 05:49:08 +0000175 for(row = 0; row < h; row++) {
176 for(col = 0; col < w; col++) {
177 if((flags & TJ.FLAG_BOTTOMUP) != 0)
178 index = pitch * (h - row - 1) + col * ps;
179 else index = pitch * row + col * ps;
180 if(((row / 8) + (col / 8)) % 2 == 0) {
181 if(row < halfway) {
182 buf[index + roffset] = (byte)255;
183 buf[index + goffset] = (byte)255;
184 buf[index + boffset] = (byte)255;
185 }
DRCf7f3ea42011-03-01 20:03:32 +0000186 }
DRCf962fbb2011-05-23 05:49:08 +0000187 else {
188 buf[index + roffset] = (byte)255;
189 if(row >= halfway) buf[index + goffset] = (byte)255;
DRCf7f3ea42011-03-01 20:03:32 +0000190 }
DRCc08e8c12011-09-08 23:54:40 +0000191 if (aoffset >= 0) buf[index + aoffset] = (byte)255;
DRCf7f3ea42011-03-01 20:03:32 +0000192 }
193 }
194 }
DRC3bad53f2011-02-23 02:20:49 +0000195
DRCf7f3ea42011-03-01 20:03:32 +0000196 private static void initIntBuf(int[] buf, int w, int pitch, int h, int pf,
197 int flags) throws Exception {
DRC2c74e512011-03-16 00:02:53 +0000198 int rshift = TJ.getRedOffset(pf) * 8;
199 int gshift = TJ.getGreenOffset(pf) * 8;
200 int bshift = TJ.getBlueOffset(pf) * 8;
DRCc08e8c12011-09-08 23:54:40 +0000201 int ashift = alphaOffset[pf] * 8;
DRCf962fbb2011-05-23 05:49:08 +0000202 int index, row, col, halfway = 16;
DRC84a1bcc2011-02-23 12:09:56 +0000203
DRCf7f3ea42011-03-01 20:03:32 +0000204 Arrays.fill(buf, 0);
DRCf962fbb2011-05-23 05:49:08 +0000205 for(row = 0; row < h; row++) {
206 for(col = 0; col < w; col++) {
207 if((flags & TJ.FLAG_BOTTOMUP) != 0)
208 index = pitch * (h - row - 1) + col;
209 else index = pitch * row + col;
210 if(((row / 8) + (col / 8)) % 2 == 0) {
211 if(row < halfway) {
212 buf[index] |= (255 << rshift);
213 buf[index] |= (255 << gshift);
214 buf[index] |= (255 << bshift);
215 }
DRCf7f3ea42011-03-01 20:03:32 +0000216 }
DRCf962fbb2011-05-23 05:49:08 +0000217 else {
218 buf[index] |= (255 << rshift);
219 if(row >= halfway) buf[index] |= (255 << gshift);
DRCf7f3ea42011-03-01 20:03:32 +0000220 }
DRCc08e8c12011-09-08 23:54:40 +0000221 if (ashift >= 0) buf[index] |= (255 << ashift);
DRCf7f3ea42011-03-01 20:03:32 +0000222 }
223 }
224 }
DRC84a1bcc2011-02-23 12:09:56 +0000225
DRCf7f3ea42011-03-01 20:03:32 +0000226 private static void initImg(BufferedImage img, int pf, int flags)
227 throws Exception {
228 WritableRaster wr = img.getRaster();
DRCc08e8c12011-09-08 23:54:40 +0000229 int imgType = img.getType();
230 if(imgType == BufferedImage.TYPE_INT_RGB
231 || imgType == BufferedImage.TYPE_INT_BGR
232 || imgType == BufferedImage.TYPE_INT_ARGB
233 || imgType == BufferedImage.TYPE_INT_ARGB_PRE) {
DRCf7f3ea42011-03-01 20:03:32 +0000234 SinglePixelPackedSampleModel sm =
235 (SinglePixelPackedSampleModel)img.getSampleModel();
236 int pitch = sm.getScanlineStride();
237 DataBufferInt db = (DataBufferInt)wr.getDataBuffer();
238 int[] buf = db.getData();
239 initIntBuf(buf, img.getWidth(), pitch, img.getHeight(), pf, flags);
240 }
241 else {
242 ComponentSampleModel sm = (ComponentSampleModel)img.getSampleModel();
243 int pitch = sm.getScanlineStride();
244 DataBufferByte db = (DataBufferByte)wr.getDataBuffer();
245 byte[] buf = db.getData();
246 initBuf(buf, img.getWidth(), pitch, img.getHeight(), pf, flags);
247 }
248 }
DRC3bad53f2011-02-23 02:20:49 +0000249
DRCf962fbb2011-05-23 05:49:08 +0000250 private static void checkVal(int row, int col, int v, String vname, int cv)
DRCf7f3ea42011-03-01 20:03:32 +0000251 throws Exception {
252 v = (v < 0) ? v + 256 : v;
253 if(v < cv - 1 || v > cv + 1) {
DRCf962fbb2011-05-23 05:49:08 +0000254 throw new Exception("\nComp. " + vname + " at " + row + "," + col
DRCf7f3ea42011-03-01 20:03:32 +0000255 + " should be " + cv + ", not " + v + "\n");
256 }
257 }
DRC3bad53f2011-02-23 02:20:49 +0000258
DRCf962fbb2011-05-23 05:49:08 +0000259 private static void checkVal0(int row, int col, int v, String vname)
DRCf7f3ea42011-03-01 20:03:32 +0000260 throws Exception {
261 v = (v < 0) ? v + 256 : v;
262 if(v > 1) {
DRCf962fbb2011-05-23 05:49:08 +0000263 throw new Exception("\nComp. " + vname + " at " + row + "," + col
DRCf7f3ea42011-03-01 20:03:32 +0000264 + " should be 0, not " + v + "\n");
265 }
266 }
DRC3bad53f2011-02-23 02:20:49 +0000267
DRCf962fbb2011-05-23 05:49:08 +0000268 private static void checkVal255(int row, int col, int v, String vname)
DRCf7f3ea42011-03-01 20:03:32 +0000269 throws Exception {
270 v = (v < 0) ? v + 256 : v;
271 if(v < 254) {
DRCf962fbb2011-05-23 05:49:08 +0000272 throw new Exception("\nComp. " + vname + " at " + row + "," + col
DRCf7f3ea42011-03-01 20:03:32 +0000273 + " should be 255, not " + v + "\n");
274 }
275 }
DRC3bad53f2011-02-23 02:20:49 +0000276
DRCf7f3ea42011-03-01 20:03:32 +0000277 private static int checkBuf(byte[] buf, int w, int pitch, int h, int pf,
DRCb2f94152011-04-02 02:09:03 +0000278 int subsamp, TJScalingFactor sf, int flags) throws Exception {
DRC2c74e512011-03-16 00:02:53 +0000279 int roffset = TJ.getRedOffset(pf);
280 int goffset = TJ.getGreenOffset(pf);
281 int boffset = TJ.getBlueOffset(pf);
DRCc08e8c12011-09-08 23:54:40 +0000282 int aoffset = alphaOffset[pf];
DRCf7f3ea42011-03-01 20:03:32 +0000283 int ps = TJ.getPixelSize(pf);
DRCf962fbb2011-05-23 05:49:08 +0000284 int index, row, col, retval = 1;
DRCb2f94152011-04-02 02:09:03 +0000285 int halfway = 16 * sf.getNum() / sf.getDenom();
286 int blockSize = 8 * sf.getNum() / sf.getDenom();
DRC3bad53f2011-02-23 02:20:49 +0000287
DRCf7f3ea42011-03-01 20:03:32 +0000288 try {
DRCf962fbb2011-05-23 05:49:08 +0000289 for(row = 0; row < halfway; row++) {
290 for(col = 0; col < w; col++) {
291 if((flags & TJ.FLAG_BOTTOMUP) != 0)
292 index = pitch * (h - row - 1) + col * ps;
293 else index = pitch * row + col * ps;
294 byte r = buf[index + roffset];
295 byte g = buf[index + goffset];
296 byte b = buf[index + boffset];
DRCc08e8c12011-09-08 23:54:40 +0000297 byte a = aoffset >= 0 ? buf[index + aoffset] : (byte)255;
DRCf962fbb2011-05-23 05:49:08 +0000298 if(((row / blockSize) + (col / blockSize)) % 2 == 0) {
299 if(row < halfway) {
300 checkVal255(row, col, r, "R");
301 checkVal255(row, col, g, "G");
302 checkVal255(row, col, b, "B");
303 }
304 else {
305 checkVal0(row, col, r, "R");
306 checkVal0(row, col, g, "G");
307 checkVal0(row, col, b, "B");
308 }
DRCf7f3ea42011-03-01 20:03:32 +0000309 }
310 else {
311 if(subsamp == TJ.SAMP_GRAY) {
DRCf962fbb2011-05-23 05:49:08 +0000312 if(row < halfway) {
313 checkVal(row, col, r, "R", 76);
314 checkVal(row, col, g, "G", 76);
315 checkVal(row, col, b, "B", 76);
316 }
317 else {
318 checkVal(row, col, r, "R", 226);
319 checkVal(row, col, g, "G", 226);
320 checkVal(row, col, b, "B", 226);
321 }
DRCf7f3ea42011-03-01 20:03:32 +0000322 }
323 else {
DRCf962fbb2011-05-23 05:49:08 +0000324 checkVal255(row, col, r, "R");
325 if(row < halfway) {
326 checkVal0(row, col, g, "G");
327 }
328 else {
329 checkVal255(row, col, g, "G");
330 }
331 checkVal0(row, col, b, "B");
DRCf7f3ea42011-03-01 20:03:32 +0000332 }
333 }
DRCc08e8c12011-09-08 23:54:40 +0000334 checkVal255(row, col, a, "A");
DRCf7f3ea42011-03-01 20:03:32 +0000335 }
336 }
337 }
338 catch(Exception e) {
339 System.out.println(e);
340 retval = 0;
341 }
DRC4f1580c2011-02-25 06:11:03 +0000342
DRCf7f3ea42011-03-01 20:03:32 +0000343 if(retval == 0) {
344 System.out.print("\n");
DRCf962fbb2011-05-23 05:49:08 +0000345 for(row = 0; row < h; row++) {
346 for(col = 0; col < w; col++) {
347 int r = buf[pitch * row + col * ps + roffset];
348 int g = buf[pitch * row + col * ps + goffset];
349 int b = buf[pitch * row + col * ps + boffset];
DRCf7f3ea42011-03-01 20:03:32 +0000350 if(r < 0) r += 256; if(g < 0) g += 256; if(b < 0) b += 256;
351 System.out.format("%3d/%3d/%3d ", r, g, b);
352 }
353 System.out.print("\n");
354 }
355 }
356 return retval;
357 }
DRC4f1580c2011-02-25 06:11:03 +0000358
DRCf7f3ea42011-03-01 20:03:32 +0000359 private static int checkIntBuf(int[] buf, int w, int pitch, int h, int pf,
DRCb2f94152011-04-02 02:09:03 +0000360 int subsamp, TJScalingFactor sf, int flags) throws Exception {
DRC2c74e512011-03-16 00:02:53 +0000361 int rshift = TJ.getRedOffset(pf) * 8;
362 int gshift = TJ.getGreenOffset(pf) * 8;
363 int bshift = TJ.getBlueOffset(pf) * 8;
DRCc08e8c12011-09-08 23:54:40 +0000364 int ashift = alphaOffset[pf] * 8;
DRCf962fbb2011-05-23 05:49:08 +0000365 int index, row, col, retval = 1;
DRCb2f94152011-04-02 02:09:03 +0000366 int halfway = 16 * sf.getNum() / sf.getDenom();
367 int blockSize = 8 * sf.getNum() / sf.getDenom();
DRC4f1580c2011-02-25 06:11:03 +0000368
DRCf7f3ea42011-03-01 20:03:32 +0000369 try {
DRCf962fbb2011-05-23 05:49:08 +0000370 for(row = 0; row < halfway; row++) {
371 for(col = 0; col < w; col++) {
372 if((flags & TJ.FLAG_BOTTOMUP) != 0)
373 index = pitch * (h - row - 1) + col;
374 else index = pitch * row + col;
375 int r = (buf[index] >> rshift) & 0xFF;
376 int g = (buf[index] >> gshift) & 0xFF;
377 int b = (buf[index] >> bshift) & 0xFF;
DRCc08e8c12011-09-08 23:54:40 +0000378 int a = ashift >= 0 ? (buf[index] >> ashift) & 0xFF : 255;
DRCf962fbb2011-05-23 05:49:08 +0000379 if(((row / blockSize) + (col / blockSize)) % 2 == 0) {
380 if(row < halfway) {
381 checkVal255(row, col, r, "R");
382 checkVal255(row, col, g, "G");
383 checkVal255(row, col, b, "B");
384 }
385 else {
386 checkVal0(row, col, r, "R");
387 checkVal0(row, col, g, "G");
388 checkVal0(row, col, b, "B");
389 }
DRCf7f3ea42011-03-01 20:03:32 +0000390 }
391 else {
392 if(subsamp == TJ.SAMP_GRAY) {
DRCf962fbb2011-05-23 05:49:08 +0000393 if(row < halfway) {
394 checkVal(row, col, r, "R", 76);
395 checkVal(row, col, g, "G", 76);
396 checkVal(row, col, b, "B", 76);
397 }
398 else {
399 checkVal(row, col, r, "R", 226);
400 checkVal(row, col, g, "G", 226);
401 checkVal(row, col, b, "B", 226);
402 }
DRCf7f3ea42011-03-01 20:03:32 +0000403 }
404 else {
DRCf962fbb2011-05-23 05:49:08 +0000405 checkVal255(row, col, r, "R");
406 if(row < halfway) {
407 checkVal0(row, col, g, "G");
408 }
409 else {
410 checkVal255(row, col, g, "G");
411 }
412 checkVal0(row, col, b, "B");
DRCf7f3ea42011-03-01 20:03:32 +0000413 }
414 }
DRCc08e8c12011-09-08 23:54:40 +0000415 checkVal255(row, col, a, "A");
DRCf7f3ea42011-03-01 20:03:32 +0000416 }
417 }
418 }
419 catch(Exception e) {
420 System.out.println(e);
421 retval = 0;
422 }
DRC4f1580c2011-02-25 06:11:03 +0000423
DRCf7f3ea42011-03-01 20:03:32 +0000424 if(retval == 0) {
425 System.out.print("\n");
DRCf962fbb2011-05-23 05:49:08 +0000426 for(row = 0; row < h; row++) {
427 for(col = 0; col < w; col++) {
428 int r = (buf[pitch * row + col] >> rshift) & 0xFF;
429 int g = (buf[pitch * row + col] >> gshift) & 0xFF;
430 int b = (buf[pitch * row + col] >> bshift) & 0xFF;
DRCf7f3ea42011-03-01 20:03:32 +0000431 if(r < 0) r += 256; if(g < 0) g += 256; if(b < 0) b += 256;
432 System.out.format("%3d/%3d/%3d ", r, g, b);
433 }
434 System.out.print("\n");
435 }
436 }
437 return retval;
438 }
DRC3bad53f2011-02-23 02:20:49 +0000439
DRCf7f3ea42011-03-01 20:03:32 +0000440 private static int checkImg(BufferedImage img, int pf,
DRCb2f94152011-04-02 02:09:03 +0000441 int subsamp, TJScalingFactor sf, int flags) throws Exception {
DRCf7f3ea42011-03-01 20:03:32 +0000442 WritableRaster wr = img.getRaster();
DRCc08e8c12011-09-08 23:54:40 +0000443 int imgType = img.getType();
444 if(imgType == BufferedImage.TYPE_INT_RGB
445 || imgType == BufferedImage.TYPE_INT_BGR
446 || imgType == BufferedImage.TYPE_INT_ARGB
447 || imgType == BufferedImage.TYPE_INT_ARGB_PRE) {
448 SinglePixelPackedSampleModel sm =
DRCf7f3ea42011-03-01 20:03:32 +0000449 (SinglePixelPackedSampleModel)img.getSampleModel();
450 int pitch = sm.getScanlineStride();
451 DataBufferInt db = (DataBufferInt)wr.getDataBuffer();
452 int[] buf = db.getData();
453 return checkIntBuf(buf, img.getWidth(), pitch, img.getHeight(), pf,
DRCb2f94152011-04-02 02:09:03 +0000454 subsamp, sf, flags);
DRCf7f3ea42011-03-01 20:03:32 +0000455 }
456 else {
457 ComponentSampleModel sm = (ComponentSampleModel)img.getSampleModel();
458 int pitch = sm.getScanlineStride();
459 DataBufferByte db = (DataBufferByte)wr.getDataBuffer();
460 byte[] buf = db.getData();
461 return checkBuf(buf, img.getWidth(), pitch, img.getHeight(), pf, subsamp,
DRCb2f94152011-04-02 02:09:03 +0000462 sf, flags);
DRCf7f3ea42011-03-01 20:03:32 +0000463 }
464 }
DRC84a1bcc2011-02-23 12:09:56 +0000465
DRCf7f3ea42011-03-01 20:03:32 +0000466 private static int PAD(int v, int p) {
467 return ((v + (p) - 1) & (~((p) - 1)));
468 }
DRC3bad53f2011-02-23 02:20:49 +0000469
DRCf7f3ea42011-03-01 20:03:32 +0000470 private static int checkBufYUV(byte[] buf, int size, int w, int h,
DRCd0a81362011-03-04 13:04:24 +0000471 int subsamp) throws Exception {
DRC215aa8b2011-05-27 02:10:42 +0000472 int row, col;
DRCd0a81362011-03-04 13:04:24 +0000473 int hsf = TJ.getMCUWidth(subsamp)/8, vsf = TJ.getMCUHeight(subsamp)/8;
DRCf7f3ea42011-03-01 20:03:32 +0000474 int pw = PAD(w, hsf), ph = PAD(h, vsf);
475 int cw = pw / hsf, ch = ph / vsf;
476 int ypitch = PAD(pw, 4), uvpitch = PAD(cw, 4);
477 int retval = 1;
478 int correctsize = ypitch * ph
479 + (subsamp == TJ.SAMP_GRAY ? 0 : uvpitch * ch * 2);
DRC215aa8b2011-05-27 02:10:42 +0000480 int halfway = 16;
DRC3bad53f2011-02-23 02:20:49 +0000481
DRCf7f3ea42011-03-01 20:03:32 +0000482 try {
483 if(size != correctsize)
484 throw new Exception("\nIncorrect size " + size + ". Should be "
485 + correctsize);
DRC3bad53f2011-02-23 02:20:49 +0000486
DRC215aa8b2011-05-27 02:10:42 +0000487 for(row = 0; row < ph; row++) {
488 for(col = 0; col < pw; col++) {
489 byte y = buf[ypitch * row + col];
490 if(((row / 8) + (col / 8)) % 2 == 0) {
491 if(row < halfway) checkVal255(row, col, y, "Y");
492 else checkVal0(row, col, y, "Y");
493 }
494 else {
495 if(row < halfway) checkVal(row, col, y, "Y", 76);
496 else checkVal(row, col, y, "Y", 226);
497 }
DRCf7f3ea42011-03-01 20:03:32 +0000498 }
499 }
500 if(subsamp != TJ.SAMP_GRAY) {
DRC215aa8b2011-05-27 02:10:42 +0000501 halfway = 16 / vsf;
502 for(row = 0; row < ch; row++) {
503 for(col = 0; col < cw; col++) {
504 byte u = buf[ypitch * ph + (uvpitch * row + col)],
505 v = buf[ypitch * ph + uvpitch * ch + (uvpitch * row + col)];
506 if(((row * vsf / 8) + (col * hsf / 8)) % 2 == 0) {
507 checkVal(row, col, u, "U", 128);
508 checkVal(row, col, v, "V", 128);
DRCf7f3ea42011-03-01 20:03:32 +0000509 }
510 else {
DRC215aa8b2011-05-27 02:10:42 +0000511 if(row < halfway) {
512 checkVal(row, col, u, "U", 85);
513 checkVal255(row, col, v, "V");
514 }
515 else {
516 checkVal0(row, col, u, "U");
517 checkVal(row, col, v, "V", 149);
518 }
DRCf7f3ea42011-03-01 20:03:32 +0000519 }
520 }
521 }
522 }
523 }
524 catch(Exception e) {
525 System.out.println(e);
526 retval = 0;
527 }
DRC3bad53f2011-02-23 02:20:49 +0000528
DRCf7f3ea42011-03-01 20:03:32 +0000529 if(retval == 0) {
DRC215aa8b2011-05-27 02:10:42 +0000530 for(row = 0; row < ph; row++) {
531 for(col = 0; col < pw; col++) {
532 int y = buf[ypitch * row + col];
DRCf7f3ea42011-03-01 20:03:32 +0000533 if(y < 0) y += 256;
534 System.out.format("%3d ", y);
535 }
536 System.out.print("\n");
537 }
538 System.out.print("\n");
DRC215aa8b2011-05-27 02:10:42 +0000539 for(row = 0; row < ch; row++) {
540 for(col = 0; col < cw; col++) {
541 int u = buf[ypitch * ph + (uvpitch * row + col)];
DRCf7f3ea42011-03-01 20:03:32 +0000542 if(u < 0) u += 256;
543 System.out.format("%3d ", u);
544 }
545 System.out.print("\n");
546 }
547 System.out.print("\n");
DRC215aa8b2011-05-27 02:10:42 +0000548 for(row = 0; row < ch; row++) {
549 for(col = 0; col < cw; col++) {
550 int v = buf[ypitch * ph + uvpitch * ch + (uvpitch * row + col)];
DRCf7f3ea42011-03-01 20:03:32 +0000551 if(v < 0) v += 256;
552 System.out.format("%3d ", v);
553 }
554 System.out.print("\n");
555 }
556 System.out.print("\n");
557 }
DRC3bad53f2011-02-23 02:20:49 +0000558
DRCf7f3ea42011-03-01 20:03:32 +0000559 return retval;
560 }
DRC3bad53f2011-02-23 02:20:49 +0000561
DRCf7f3ea42011-03-01 20:03:32 +0000562 private static void writeJPEG(byte[] jpegBuf, int jpegBufSize,
563 String filename) throws Exception {
564 File file = new File(filename);
565 FileOutputStream fos = new FileOutputStream(file);
566 fos.write(jpegBuf, 0, jpegBufSize);
567 fos.close();
568 }
DRC3bad53f2011-02-23 02:20:49 +0000569
DRCf962fbb2011-05-23 05:49:08 +0000570 private static int compTest(TJCompressor tjc, byte[] dstBuf, int w,
571 int h, int pf, String baseName, int subsamp, int jpegQual,
DRCf7f3ea42011-03-01 20:03:32 +0000572 int flags) throws Exception {
573 String tempstr;
DRCf962fbb2011-05-23 05:49:08 +0000574 byte[] srcBuf = null;
DRCf7f3ea42011-03-01 20:03:32 +0000575 BufferedImage img = null;
576 String pfStr;
577 double t;
DRCc08e8c12011-09-08 23:54:40 +0000578 int size = 0, ps, imgType = pf;
DRC3bad53f2011-02-23 02:20:49 +0000579
DRCc08e8c12011-09-08 23:54:40 +0000580 if (bi) {
581 pf = biTypePF(imgType);
582 pfStr = biTypeStr(imgType);
583 }
584 else pfStr = pixFormatStr[pf];
585 ps = TJ.getPixelSize(pf);
DRC3bad53f2011-02-23 02:20:49 +0000586
DRCf7f3ea42011-03-01 20:03:32 +0000587 System.out.print(pfStr + " ");
DRCc08e8c12011-09-08 23:54:40 +0000588 if(bi) System.out.print("(" + pixFormatStr[pf] + ") ");
DRC92549de2011-03-15 20:52:02 +0000589 if((flags & TJ.FLAG_BOTTOMUP) != 0) System.out.print("Bottom-Up");
DRCf7f3ea42011-03-01 20:03:32 +0000590 else System.out.print("Top-Down ");
591 System.out.print(" -> " + subNameLong[subsamp] + " ");
592 if(yuv == YUVENCODE) System.out.print("YUV ... ");
DRCf962fbb2011-05-23 05:49:08 +0000593 else System.out.print("Q" + jpegQual + " ... ");
DRC3bad53f2011-02-23 02:20:49 +0000594
DRCf7f3ea42011-03-01 20:03:32 +0000595 if(bi) {
DRCc08e8c12011-09-08 23:54:40 +0000596 img = new BufferedImage(w, h, imgType);
DRCf7f3ea42011-03-01 20:03:32 +0000597 initImg(img, pf, flags);
DRCf962fbb2011-05-23 05:49:08 +0000598 tempstr = baseName + "_enc_" + pfStr + "_"
DRC92549de2011-03-15 20:52:02 +0000599 + (((flags & TJ.FLAG_BOTTOMUP) != 0) ? "BU" : "TD") + "_"
DRCf962fbb2011-05-23 05:49:08 +0000600 + subName[subsamp] + "_Q" + jpegQual + ".png";
DRCf7f3ea42011-03-01 20:03:32 +0000601 File file = new File(tempstr);
602 ImageIO.write(img, "png", file);
603 }
604 else {
DRCf962fbb2011-05-23 05:49:08 +0000605 srcBuf = new byte[w * h * ps + 1];
606 initBuf(srcBuf, w, w * ps, h, pf, flags);
DRCf7f3ea42011-03-01 20:03:32 +0000607 }
DRCf962fbb2011-05-23 05:49:08 +0000608 Arrays.fill(dstBuf, (byte)0);
DRC3bad53f2011-02-23 02:20:49 +0000609
DRCf7f3ea42011-03-01 20:03:32 +0000610 t = getTime();
611 tjc.setSubsamp(subsamp);
DRCf962fbb2011-05-23 05:49:08 +0000612 tjc.setJPEGQuality(jpegQual);
DRCf7f3ea42011-03-01 20:03:32 +0000613 if(bi) {
DRCf962fbb2011-05-23 05:49:08 +0000614 if(yuv == YUVENCODE) tjc.encodeYUV(img, dstBuf, flags);
615 else tjc.compress(img, dstBuf, flags);
DRCf7f3ea42011-03-01 20:03:32 +0000616 }
617 else {
DRCf962fbb2011-05-23 05:49:08 +0000618 tjc.setSourceImage(srcBuf, w, 0, h, pf);
619 if(yuv == YUVENCODE) tjc.encodeYUV(dstBuf, flags);
620 else tjc.compress(dstBuf, flags);
DRCf7f3ea42011-03-01 20:03:32 +0000621 }
622 size = tjc.getCompressedSize();
623 t = getTime() - t;
DRC3bad53f2011-02-23 02:20:49 +0000624
DRCf7f3ea42011-03-01 20:03:32 +0000625 if(yuv == YUVENCODE)
DRCf962fbb2011-05-23 05:49:08 +0000626 tempstr = baseName + "_enc_" + pfStr + "_"
DRC92549de2011-03-15 20:52:02 +0000627 + (((flags & TJ.FLAG_BOTTOMUP) != 0) ? "BU" : "TD") + "_"
DRCf7f3ea42011-03-01 20:03:32 +0000628 + subName[subsamp] + ".yuv";
629 else
DRCf962fbb2011-05-23 05:49:08 +0000630 tempstr = baseName + "_enc_" + pfStr + "_"
DRC92549de2011-03-15 20:52:02 +0000631 + (((flags & TJ.FLAG_BOTTOMUP) != 0) ? "BU" : "TD") + "_"
DRCf962fbb2011-05-23 05:49:08 +0000632 + subName[subsamp] + "_Q" + jpegQual + ".jpg";
633 writeJPEG(dstBuf, size, tempstr);
DRC84a1bcc2011-02-23 12:09:56 +0000634
DRCf7f3ea42011-03-01 20:03:32 +0000635 if(yuv == YUVENCODE) {
DRCf962fbb2011-05-23 05:49:08 +0000636 if(checkBufYUV(dstBuf, size, w, h, subsamp) == 1)
DRCf7f3ea42011-03-01 20:03:32 +0000637 System.out.print("Passed.");
638 else {
639 System.out.print("FAILED!"); exitStatus = -1;
640 }
641 }
642 else System.out.print("Done.");
643 System.out.format(" %.6f ms\n", t * 1000.);
644 System.out.println(" Result in " + tempstr);
DRC3bad53f2011-02-23 02:20:49 +0000645
DRCf7f3ea42011-03-01 20:03:32 +0000646 return size;
647 }
DRC3bad53f2011-02-23 02:20:49 +0000648
DRCf962fbb2011-05-23 05:49:08 +0000649 private static void decompTest(TJDecompressor tjd, byte[] jpegBuf,
650 int jpegSize, int w, int h, int pf, String baseName, int subsamp,
DRCb2f94152011-04-02 02:09:03 +0000651 int flags, TJScalingFactor sf) throws Exception {
DRCf7f3ea42011-03-01 20:03:32 +0000652 String pfStr, tempstr;
653 double t;
DRCb2f94152011-04-02 02:09:03 +0000654 int scaledWidth = sf.getScaled(w);
655 int scaledHeight = sf.getScaled(h);
DRCc08e8c12011-09-08 23:54:40 +0000656 int temp1, temp2, imgType = pf;
DRCf7f3ea42011-03-01 20:03:32 +0000657 BufferedImage img = null;
DRCf962fbb2011-05-23 05:49:08 +0000658 byte[] dstBuf = null;
DRC3bad53f2011-02-23 02:20:49 +0000659
DRCf7f3ea42011-03-01 20:03:32 +0000660 if(yuv == YUVENCODE) return;
DRC3bad53f2011-02-23 02:20:49 +0000661
DRCc08e8c12011-09-08 23:54:40 +0000662 if (bi) {
663 pf = biTypePF(imgType);
664 pfStr = biTypeStr(imgType);
665 }
666 else pfStr = pixFormatStr[pf];
667
DRCf7f3ea42011-03-01 20:03:32 +0000668 System.out.print("JPEG -> ");
669 if(yuv == YUVDECODE)
670 System.out.print("YUV " + subName[subsamp] + " ... ");
671 else {
672 System.out.print(pfStr + " ");
DRCc08e8c12011-09-08 23:54:40 +0000673 if(bi) System.out.print("(" + pixFormatStr[pf] + ") ");
DRC92549de2011-03-15 20:52:02 +0000674 if((flags & TJ.FLAG_BOTTOMUP) != 0) System.out.print("Bottom-Up ");
DRCf7f3ea42011-03-01 20:03:32 +0000675 else System.out.print("Top-Down ");
DRCb2f94152011-04-02 02:09:03 +0000676 if(!sf.isOne())
677 System.out.print(sf.getNum() + "/" + sf.getDenom() + " ... ");
DRCf7f3ea42011-03-01 20:03:32 +0000678 else System.out.print("... ");
679 }
DRC3bad53f2011-02-23 02:20:49 +0000680
DRCf7f3ea42011-03-01 20:03:32 +0000681 t = getTime();
DRCf962fbb2011-05-23 05:49:08 +0000682 tjd.setJPEGImage(jpegBuf, jpegSize);
DRCf7f3ea42011-03-01 20:03:32 +0000683 if(tjd.getWidth() != w || tjd.getHeight() != h
684 || tjd.getSubsamp() != subsamp)
685 throw new Exception("Incorrect JPEG header");
DRC3bad53f2011-02-23 02:20:49 +0000686
DRCf7f3ea42011-03-01 20:03:32 +0000687 temp1 = scaledWidth;
688 temp2 = scaledHeight;
689 temp1 = tjd.getScaledWidth(temp1, temp2);
690 temp2 = tjd.getScaledHeight(temp1, temp2);
691 if(temp1 != scaledWidth || temp2 != scaledHeight)
692 throw new Exception("Scaled size mismatch");
DRC3bad53f2011-02-23 02:20:49 +0000693
DRCf962fbb2011-05-23 05:49:08 +0000694 if(yuv == YUVDECODE) dstBuf = tjd.decompressToYUV(flags);
DRCf7f3ea42011-03-01 20:03:32 +0000695 else {
696 if(bi)
DRCc08e8c12011-09-08 23:54:40 +0000697 img = tjd.decompress(scaledWidth, scaledHeight, imgType, flags);
DRCf962fbb2011-05-23 05:49:08 +0000698 else dstBuf = tjd.decompress(scaledWidth, 0, scaledHeight, pf, flags);
DRCf7f3ea42011-03-01 20:03:32 +0000699 }
700 t = getTime() - t;
DRC3bad53f2011-02-23 02:20:49 +0000701
DRCf7f3ea42011-03-01 20:03:32 +0000702 if(bi) {
DRCf962fbb2011-05-23 05:49:08 +0000703 tempstr = baseName + "_dec_" + pfStr + "_"
DRC92549de2011-03-15 20:52:02 +0000704 + (((flags & TJ.FLAG_BOTTOMUP) != 0) ? "BU" : "TD") + "_"
DRCb2f94152011-04-02 02:09:03 +0000705 + subName[subsamp] + "_" + (double)sf.getNum() / (double)sf.getDenom()
DRCf7f3ea42011-03-01 20:03:32 +0000706 + "x" + ".png";
707 File file = new File(tempstr);
708 ImageIO.write(img, "png", file);
709 }
DRC84a1bcc2011-02-23 12:09:56 +0000710
DRCf7f3ea42011-03-01 20:03:32 +0000711 if(yuv == YUVDECODE) {
DRCf962fbb2011-05-23 05:49:08 +0000712 if(checkBufYUV(dstBuf, dstBuf.length, w, h, subsamp) == 1)
DRCf7f3ea42011-03-01 20:03:32 +0000713 System.out.print("Passed.");
714 else {
715 System.out.print("FAILED!"); exitStatus = -1;
716 }
717 }
718 else {
DRCb2f94152011-04-02 02:09:03 +0000719 if((bi && checkImg(img, pf, subsamp, sf, flags) == 1)
DRCf962fbb2011-05-23 05:49:08 +0000720 || (!bi && checkBuf(dstBuf, scaledWidth, scaledWidth
DRCb2f94152011-04-02 02:09:03 +0000721 * TJ.getPixelSize(pf), scaledHeight, pf, subsamp, sf, flags) == 1))
DRCf7f3ea42011-03-01 20:03:32 +0000722 System.out.print("Passed.");
723 else {
724 System.out.print("FAILED!"); exitStatus = -1;
725 }
726 }
727 System.out.format(" %.6f ms\n", t * 1000.);
728 }
DRC3bad53f2011-02-23 02:20:49 +0000729
DRCf962fbb2011-05-23 05:49:08 +0000730 private static void decompTest(TJDecompressor tjd, byte[] jpegBuf,
731 int jpegSize, int w, int h, int pf, String baseName, int subsamp,
DRCf7f3ea42011-03-01 20:03:32 +0000732 int flags) throws Exception {
733 int i;
734 if((subsamp == TJ.SAMP_444 || subsamp == TJ.SAMP_GRAY) && yuv == 0) {
DRCb2f94152011-04-02 02:09:03 +0000735 TJScalingFactor sf[] = TJ.getScalingFactors();
DRCf7f3ea42011-03-01 20:03:32 +0000736 for(i = 0; i < sf.length; i++)
DRCf962fbb2011-05-23 05:49:08 +0000737 decompTest(tjd, jpegBuf, jpegSize, w, h, pf, baseName, subsamp,
DRCb2f94152011-04-02 02:09:03 +0000738 flags, sf[i]);
DRCf7f3ea42011-03-01 20:03:32 +0000739 }
740 else
DRCf962fbb2011-05-23 05:49:08 +0000741 decompTest(tjd, jpegBuf, jpegSize, w, h, pf, baseName, subsamp,
DRCb2f94152011-04-02 02:09:03 +0000742 flags, new TJScalingFactor(1, 1));
DRCf7f3ea42011-03-01 20:03:32 +0000743 System.out.print("\n");
744 }
DRC3bad53f2011-02-23 02:20:49 +0000745
DRCf7f3ea42011-03-01 20:03:32 +0000746 private static void doTest(int w, int h, int[] formats, int subsamp,
DRCf962fbb2011-05-23 05:49:08 +0000747 String baseName) throws Exception {
DRCf7f3ea42011-03-01 20:03:32 +0000748 TJCompressor tjc = null;
749 TJDecompressor tjd = null;
DRC4f8c2952011-03-31 10:06:17 +0000750 int size;
DRCf962fbb2011-05-23 05:49:08 +0000751 byte[] dstBuf;
DRC3bad53f2011-02-23 02:20:49 +0000752
DRCf962fbb2011-05-23 05:49:08 +0000753 if(yuv == YUVENCODE) dstBuf = new byte[TJ.bufSizeYUV(w, h, subsamp)];
DRC9b49f0e2011-07-12 03:17:23 +0000754 else dstBuf = new byte[TJ.bufSize(w, h, subsamp)];
DRC3bad53f2011-02-23 02:20:49 +0000755
DRCf7f3ea42011-03-01 20:03:32 +0000756 try {
757 tjc = new TJCompressor();
758 tjd = new TJDecompressor();
DRC3bad53f2011-02-23 02:20:49 +0000759
DRCf7f3ea42011-03-01 20:03:32 +0000760 for(int pf : formats) {
761 for(int i = 0; i < 2; i++) {
762 int flags = 0;
763 if(i == 1) {
764 if(yuv == YUVDECODE) {
765 tjc.close(); tjd.close(); return;
766 }
DRC92549de2011-03-15 20:52:02 +0000767 else flags |= TJ.FLAG_BOTTOMUP;
DRCf7f3ea42011-03-01 20:03:32 +0000768 }
DRCf962fbb2011-05-23 05:49:08 +0000769 size = compTest(tjc, dstBuf, w, h, pf, baseName, subsamp, 100,
DRCf7f3ea42011-03-01 20:03:32 +0000770 flags);
DRCf962fbb2011-05-23 05:49:08 +0000771 decompTest(tjd, dstBuf, size, w, h, pf, baseName, subsamp, flags);
DRCf7f3ea42011-03-01 20:03:32 +0000772 }
773 }
774 }
775 catch(Exception e) {
776 if(tjc != null) tjc.close();
777 if(tjd != null) tjd.close();
778 throw e;
779 }
780 if(tjc != null) tjc.close();
781 if(tjd != null) tjd.close();
782 }
DRC3bad53f2011-02-23 02:20:49 +0000783
DRC724c56b2011-07-12 06:22:06 +0000784 private static void bufSizeTest() throws Exception {
785 int w, h, i, subsamp;
DRCf962fbb2011-05-23 05:49:08 +0000786 byte[] srcBuf, jpegBuf;
DRCf7f3ea42011-03-01 20:03:32 +0000787 TJCompressor tjc = null;
DRC724c56b2011-07-12 06:22:06 +0000788 Random r = new Random();
DRC3bad53f2011-02-23 02:20:49 +0000789
DRCf7f3ea42011-03-01 20:03:32 +0000790 try {
791 tjc = new TJCompressor();
792 System.out.println("Buffer size regression test");
DRC724c56b2011-07-12 06:22:06 +0000793 for(subsamp = 0; subsamp < TJ.NUMSAMP; subsamp++) {
794 for(w = 1; w < 48; w++) {
795 int maxh = (w == 1) ? 2048 : 48;
796 for(h = 1; h < maxh; h++) {
797 if(h % 100 == 0)
798 System.out.format("%04d x %04d\b\b\b\b\b\b\b\b\b\b\b", w, h);
799 srcBuf = new byte[w * h * 4];
800 jpegBuf = new byte[TJ.bufSize(w, h, subsamp)];
801 for(i = 0; i < w * h * 4; i++) {
802 srcBuf[i] = (byte)(r.nextInt(2) * 255);
803 }
804 tjc.setSourceImage(srcBuf, w, 0, h, TJ.PF_BGRX);
805 tjc.setSubsamp(subsamp);
806 tjc.setJPEGQuality(100);
807 tjc.compress(jpegBuf, 0);
DRC3bad53f2011-02-23 02:20:49 +0000808
DRC724c56b2011-07-12 06:22:06 +0000809 srcBuf = new byte[h * w * 4];
810 jpegBuf = new byte[TJ.bufSize(h, w, subsamp)];
811 for(i = 0; i < h * w * 4; i++) {
812 srcBuf[i] = (byte)(r.nextInt(2) * 255);
813 }
814 tjc.setSourceImage(srcBuf, h, 0, w, TJ.PF_BGRX);
815 tjc.compress(jpegBuf, 0);
DRCf7f3ea42011-03-01 20:03:32 +0000816 }
DRCf7f3ea42011-03-01 20:03:32 +0000817 }
818 }
819 System.out.println("Done. ");
820 }
821 catch(Exception e) {
822 if(tjc != null) tjc.close();
823 throw e;
824 }
825 if(tjc != null) tjc.close();
826 }
DRC3bad53f2011-02-23 02:20:49 +0000827
DRCf7f3ea42011-03-01 20:03:32 +0000828 public static void main(String argv[]) {
829 try {
DRCb6ed7d32011-03-31 20:58:03 +0000830 String testName = "javatest";
DRCf7f3ea42011-03-01 20:03:32 +0000831 boolean doyuv = false;
832 for(int i = 0; i < argv.length; i++) {
833 if(argv[i].equalsIgnoreCase("-yuv")) doyuv = true;
834 if(argv[i].substring(0, 1).equalsIgnoreCase("-h")
835 || argv[i].equalsIgnoreCase("-?"))
836 usage();
DRCb6ed7d32011-03-31 20:58:03 +0000837 if(argv[i].equalsIgnoreCase("-bi")) {
838 bi = true;
839 testName = "javabitest";
840 }
DRCf7f3ea42011-03-01 20:03:32 +0000841 }
842 if(doyuv) yuv = YUVENCODE;
DRCb6ed7d32011-03-31 20:58:03 +0000843 doTest(35, 39, bi ? _3byteFormatsBI : _3byteFormats, TJ.SAMP_444, testName);
844 doTest(39, 41, bi ? _4byteFormatsBI : _4byteFormats, TJ.SAMP_444, testName);
DRCf7f3ea42011-03-01 20:03:32 +0000845 if(doyuv) {
846 doTest(41, 35, bi ? _3byteFormatsBI : _3byteFormats, TJ.SAMP_422,
DRCb6ed7d32011-03-31 20:58:03 +0000847 testName);
DRCf7f3ea42011-03-01 20:03:32 +0000848 doTest(35, 39, bi ? _4byteFormatsBI : _4byteFormats, TJ.SAMP_422,
DRCb6ed7d32011-03-31 20:58:03 +0000849 testName);
DRCf7f3ea42011-03-01 20:03:32 +0000850 doTest(39, 41, bi ? _3byteFormatsBI : _3byteFormats, TJ.SAMP_420,
DRCb6ed7d32011-03-31 20:58:03 +0000851 testName);
DRCf7f3ea42011-03-01 20:03:32 +0000852 doTest(41, 35, bi ? _4byteFormatsBI : _4byteFormats, TJ.SAMP_420,
DRCb6ed7d32011-03-31 20:58:03 +0000853 testName);
DRCd0a81362011-03-04 13:04:24 +0000854 doTest(35, 39, bi ? _3byteFormatsBI : _3byteFormats, TJ.SAMP_440,
DRCb6ed7d32011-03-31 20:58:03 +0000855 testName);
DRCd0a81362011-03-04 13:04:24 +0000856 doTest(39, 41, bi ? _4byteFormatsBI : _4byteFormats, TJ.SAMP_440,
DRCb6ed7d32011-03-31 20:58:03 +0000857 testName);
DRCf7f3ea42011-03-01 20:03:32 +0000858 }
DRCc08e8c12011-09-08 23:54:40 +0000859 doTest(35, 39, bi ? onlyGrayBI : onlyGray, TJ.SAMP_GRAY, testName);
DRCf7f3ea42011-03-01 20:03:32 +0000860 doTest(39, 41, bi ? _3byteFormatsBI : _3byteFormats, TJ.SAMP_GRAY,
DRCb6ed7d32011-03-31 20:58:03 +0000861 testName);
DRCf7f3ea42011-03-01 20:03:32 +0000862 doTest(41, 35, bi ? _4byteFormatsBI : _4byteFormats, TJ.SAMP_GRAY,
DRCb6ed7d32011-03-31 20:58:03 +0000863 testName);
DRC724c56b2011-07-12 06:22:06 +0000864 if(!doyuv && !bi) bufSizeTest();
DRCf7f3ea42011-03-01 20:03:32 +0000865 if(doyuv && !bi) {
866 yuv = YUVDECODE;
DRCb6ed7d32011-03-31 20:58:03 +0000867 doTest(48, 48, onlyRGB, TJ.SAMP_444, "javatest_yuv0");
868 doTest(35, 39, onlyRGB, TJ.SAMP_444, "javatest_yuv1");
869 doTest(48, 48, onlyRGB, TJ.SAMP_422, "javatest_yuv0");
870 doTest(39, 41, onlyRGB, TJ.SAMP_422, "javatest_yuv1");
871 doTest(48, 48, onlyRGB, TJ.SAMP_420, "javatest_yuv0");
872 doTest(41, 35, onlyRGB, TJ.SAMP_420, "javatest_yuv1");
873 doTest(48, 48, onlyRGB, TJ.SAMP_440, "javatest_yuv0");
874 doTest(35, 39, onlyRGB, TJ.SAMP_440, "javatest_yuv1");
875 doTest(48, 48, onlyRGB, TJ.SAMP_GRAY, "javatest_yuv0");
876 doTest(35, 39, onlyRGB, TJ.SAMP_GRAY, "javatest_yuv1");
877 doTest(48, 48, onlyGray, TJ.SAMP_GRAY, "javatest_yuv0");
878 doTest(39, 41, onlyGray, TJ.SAMP_GRAY, "javatest_yuv1");
DRCf7f3ea42011-03-01 20:03:32 +0000879 }
880 }
881 catch(Exception e) {
DRC2e2358e2011-03-04 09:54:59 +0000882 e.printStackTrace();
DRCf7f3ea42011-03-01 20:03:32 +0000883 exitStatus = -1;
884 }
885 System.exit(exitStatus);
886 }
DRC3bad53f2011-02-23 02:20:49 +0000887}