DRC | 3bad53f | 2011-02-23 02:20:49 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
| 33 | import java.io.*; |
| 34 | import java.util.*; |
DRC | 84a1bcc | 2011-02-23 12:09:56 +0000 | [diff] [blame] | 35 | import java.awt.image.*; |
| 36 | import javax.imageio.*; |
DRC | 3bad53f | 2011-02-23 02:20:49 +0000 | [diff] [blame] | 37 | import org.libjpegturbo.turbojpeg.*; |
| 38 | |
| 39 | public class TJUnitTest { |
| 40 | |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 41 | private static final String classname = |
| 42 | new TJUnitTest().getClass().getName(); |
DRC | 3bad53f | 2011-02-23 02:20:49 +0000 | [diff] [blame] | 43 | |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 44 | private static void usage() { |
| 45 | System.out.println("\nUSAGE: java " + classname + " [options]\n"); |
| 46 | System.out.println("Options:\n"); |
| 47 | System.out.println("-yuv = test YUV encoding/decoding support\n"); |
| 48 | System.out.println("-bi = test BufferedImage support\n"); |
| 49 | System.exit(1); |
| 50 | } |
DRC | 3bad53f | 2011-02-23 02:20:49 +0000 | [diff] [blame] | 51 | |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 52 | private final static String subNameLong[] = { |
DRC | d0a8136 | 2011-03-04 13:04:24 +0000 | [diff] [blame] | 53 | "4:4:4", "4:2:2", "4:2:0", "GRAY", "4:4:0" |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 54 | }; |
| 55 | private final static String subName[] = { |
DRC | d0a8136 | 2011-03-04 13:04:24 +0000 | [diff] [blame] | 56 | "444", "422", "420", "GRAY", "440" |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 57 | }; |
DRC | 3bad53f | 2011-02-23 02:20:49 +0000 | [diff] [blame] | 58 | |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 59 | private final static String pixFormatStr[] = { |
| 60 | "RGB", "BGR", "RGBX", "BGRX", "XBGR", "XRGB", "Grayscale" |
| 61 | }; |
| 62 | private final static int biType[] = { |
| 63 | 0, BufferedImage.TYPE_3BYTE_BGR, BufferedImage.TYPE_INT_BGR, |
| 64 | BufferedImage.TYPE_INT_RGB, 0, 0, BufferedImage.TYPE_BYTE_GRAY |
| 65 | }; |
DRC | 3bad53f | 2011-02-23 02:20:49 +0000 | [diff] [blame] | 66 | |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 67 | private final static int _3byteFormats[] = { |
| 68 | TJ.PF_RGB, TJ.PF_BGR |
| 69 | }; |
| 70 | private final static int _3byteFormatsBI[] = { |
| 71 | TJ.PF_BGR |
| 72 | }; |
| 73 | private final static int _4byteFormats[] = { |
| 74 | TJ.PF_RGBX, TJ.PF_BGRX, TJ.PF_XBGR, TJ.PF_XRGB |
| 75 | }; |
| 76 | private final static int _4byteFormatsBI[] = { |
| 77 | TJ.PF_RGBX, TJ.PF_BGRX |
| 78 | }; |
| 79 | private final static int onlyGray[] = { |
| 80 | TJ.PF_GRAY |
| 81 | }; |
| 82 | private final static int onlyRGB[] = { |
| 83 | TJ.PF_RGB |
| 84 | }; |
DRC | 3bad53f | 2011-02-23 02:20:49 +0000 | [diff] [blame] | 85 | |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 86 | private final static int YUVENCODE = 1; |
| 87 | private final static int YUVDECODE = 2; |
| 88 | private static int yuv = 0; |
| 89 | private static boolean bi = false; |
DRC | 3bad53f | 2011-02-23 02:20:49 +0000 | [diff] [blame] | 90 | |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 91 | private static int exitStatus = 0; |
DRC | 3bad53f | 2011-02-23 02:20:49 +0000 | [diff] [blame] | 92 | |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 93 | private static double getTime() { |
| 94 | return (double)System.nanoTime() / 1.0e9; |
| 95 | } |
DRC | 3bad53f | 2011-02-23 02:20:49 +0000 | [diff] [blame] | 96 | |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 97 | private final static byte pixels[][] = { |
| 98 | {0, (byte)255, 0}, |
| 99 | {(byte)255, 0, (byte)255}, |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 100 | {0, (byte)255, (byte)255}, |
| 101 | {(byte)255, 0, 0}, |
DRC | f962fbb | 2011-05-23 05:49:08 +0000 | [diff] [blame] | 102 | {(byte)255, (byte)255, 0}, |
| 103 | {0, 0, (byte)255}, |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 104 | {(byte)255, (byte)255, (byte)255}, |
| 105 | {0, 0, 0}, |
DRC | f962fbb | 2011-05-23 05:49:08 +0000 | [diff] [blame] | 106 | {0, 0, (byte)255} |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 107 | }; |
DRC | 3bad53f | 2011-02-23 02:20:49 +0000 | [diff] [blame] | 108 | |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 109 | private static void initBuf(byte[] buf, int w, int pitch, int h, int pf, |
| 110 | int flags) throws Exception { |
DRC | 2c74e51 | 2011-03-16 00:02:53 +0000 | [diff] [blame] | 111 | int roffset = TJ.getRedOffset(pf); |
| 112 | int goffset = TJ.getGreenOffset(pf); |
| 113 | int boffset = TJ.getBlueOffset(pf); |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 114 | int ps = TJ.getPixelSize(pf); |
DRC | f962fbb | 2011-05-23 05:49:08 +0000 | [diff] [blame] | 115 | int index, row, col, halfway = 16; |
DRC | 3bad53f | 2011-02-23 02:20:49 +0000 | [diff] [blame] | 116 | |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 117 | Arrays.fill(buf, (byte)0); |
| 118 | if(pf == TJ.PF_GRAY) { |
DRC | f962fbb | 2011-05-23 05:49:08 +0000 | [diff] [blame] | 119 | for(row = 0; row < h; row++) { |
| 120 | for(col = 0; col < w; col++) { |
| 121 | if((flags & TJ.FLAG_BOTTOMUP) != 0) |
| 122 | index = pitch * (h - row - 1) + col; |
| 123 | else index = pitch * row + col; |
| 124 | if(((row / 8) + (col / 8)) % 2 == 0) |
| 125 | buf[index] = (row < halfway) ? (byte)255 : 0; |
| 126 | else buf[index] = (row < halfway) ? 76 : (byte)226; |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 127 | } |
| 128 | } |
| 129 | return; |
| 130 | } |
DRC | f962fbb | 2011-05-23 05:49:08 +0000 | [diff] [blame] | 131 | for(row = 0; row < h; row++) { |
| 132 | for(col = 0; col < w; col++) { |
| 133 | if((flags & TJ.FLAG_BOTTOMUP) != 0) |
| 134 | index = pitch * (h - row - 1) + col * ps; |
| 135 | else index = pitch * row + col * ps; |
| 136 | if(((row / 8) + (col / 8)) % 2 == 0) { |
| 137 | if(row < halfway) { |
| 138 | buf[index + roffset] = (byte)255; |
| 139 | buf[index + goffset] = (byte)255; |
| 140 | buf[index + boffset] = (byte)255; |
| 141 | } |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 142 | } |
DRC | f962fbb | 2011-05-23 05:49:08 +0000 | [diff] [blame] | 143 | else { |
| 144 | buf[index + roffset] = (byte)255; |
| 145 | if(row >= halfway) buf[index + goffset] = (byte)255; |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | } |
| 149 | } |
DRC | 3bad53f | 2011-02-23 02:20:49 +0000 | [diff] [blame] | 150 | |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 151 | private static void initIntBuf(int[] buf, int w, int pitch, int h, int pf, |
| 152 | int flags) throws Exception { |
DRC | 2c74e51 | 2011-03-16 00:02:53 +0000 | [diff] [blame] | 153 | int rshift = TJ.getRedOffset(pf) * 8; |
| 154 | int gshift = TJ.getGreenOffset(pf) * 8; |
| 155 | int bshift = TJ.getBlueOffset(pf) * 8; |
DRC | f962fbb | 2011-05-23 05:49:08 +0000 | [diff] [blame] | 156 | int index, row, col, halfway = 16; |
DRC | 84a1bcc | 2011-02-23 12:09:56 +0000 | [diff] [blame] | 157 | |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 158 | Arrays.fill(buf, 0); |
DRC | f962fbb | 2011-05-23 05:49:08 +0000 | [diff] [blame] | 159 | for(row = 0; row < h; row++) { |
| 160 | for(col = 0; col < w; col++) { |
| 161 | if((flags & TJ.FLAG_BOTTOMUP) != 0) |
| 162 | index = pitch * (h - row - 1) + col; |
| 163 | else index = pitch * row + col; |
| 164 | if(((row / 8) + (col / 8)) % 2 == 0) { |
| 165 | if(row < halfway) { |
| 166 | buf[index] |= (255 << rshift); |
| 167 | buf[index] |= (255 << gshift); |
| 168 | buf[index] |= (255 << bshift); |
| 169 | } |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 170 | } |
DRC | f962fbb | 2011-05-23 05:49:08 +0000 | [diff] [blame] | 171 | else { |
| 172 | buf[index] |= (255 << rshift); |
| 173 | if(row >= halfway) buf[index] |= (255 << gshift); |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 174 | } |
| 175 | } |
| 176 | } |
| 177 | } |
DRC | 84a1bcc | 2011-02-23 12:09:56 +0000 | [diff] [blame] | 178 | |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 179 | private static void initImg(BufferedImage img, int pf, int flags) |
| 180 | throws Exception { |
| 181 | WritableRaster wr = img.getRaster(); |
| 182 | int imgtype = img.getType(); |
| 183 | if(imgtype == BufferedImage.TYPE_INT_RGB |
| 184 | || imgtype == BufferedImage.TYPE_INT_BGR) { |
| 185 | SinglePixelPackedSampleModel sm = |
| 186 | (SinglePixelPackedSampleModel)img.getSampleModel(); |
| 187 | int pitch = sm.getScanlineStride(); |
| 188 | DataBufferInt db = (DataBufferInt)wr.getDataBuffer(); |
| 189 | int[] buf = db.getData(); |
| 190 | initIntBuf(buf, img.getWidth(), pitch, img.getHeight(), pf, flags); |
| 191 | } |
| 192 | else { |
| 193 | ComponentSampleModel sm = (ComponentSampleModel)img.getSampleModel(); |
| 194 | int pitch = sm.getScanlineStride(); |
| 195 | DataBufferByte db = (DataBufferByte)wr.getDataBuffer(); |
| 196 | byte[] buf = db.getData(); |
| 197 | initBuf(buf, img.getWidth(), pitch, img.getHeight(), pf, flags); |
| 198 | } |
| 199 | } |
DRC | 3bad53f | 2011-02-23 02:20:49 +0000 | [diff] [blame] | 200 | |
DRC | f962fbb | 2011-05-23 05:49:08 +0000 | [diff] [blame] | 201 | private static void checkVal(int row, int col, int v, String vname, int cv) |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 202 | throws Exception { |
| 203 | v = (v < 0) ? v + 256 : v; |
| 204 | if(v < cv - 1 || v > cv + 1) { |
DRC | f962fbb | 2011-05-23 05:49:08 +0000 | [diff] [blame] | 205 | throw new Exception("\nComp. " + vname + " at " + row + "," + col |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 206 | + " should be " + cv + ", not " + v + "\n"); |
| 207 | } |
| 208 | } |
DRC | 3bad53f | 2011-02-23 02:20:49 +0000 | [diff] [blame] | 209 | |
DRC | f962fbb | 2011-05-23 05:49:08 +0000 | [diff] [blame] | 210 | private static void checkVal0(int row, int col, int v, String vname) |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 211 | throws Exception { |
| 212 | v = (v < 0) ? v + 256 : v; |
| 213 | if(v > 1) { |
DRC | f962fbb | 2011-05-23 05:49:08 +0000 | [diff] [blame] | 214 | throw new Exception("\nComp. " + vname + " at " + row + "," + col |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 215 | + " should be 0, not " + v + "\n"); |
| 216 | } |
| 217 | } |
DRC | 3bad53f | 2011-02-23 02:20:49 +0000 | [diff] [blame] | 218 | |
DRC | f962fbb | 2011-05-23 05:49:08 +0000 | [diff] [blame] | 219 | private static void checkVal255(int row, int col, int v, String vname) |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 220 | throws Exception { |
| 221 | v = (v < 0) ? v + 256 : v; |
| 222 | if(v < 254) { |
DRC | f962fbb | 2011-05-23 05:49:08 +0000 | [diff] [blame] | 223 | throw new Exception("\nComp. " + vname + " at " + row + "," + col |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 224 | + " should be 255, not " + v + "\n"); |
| 225 | } |
| 226 | } |
DRC | 3bad53f | 2011-02-23 02:20:49 +0000 | [diff] [blame] | 227 | |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 228 | private static int checkBuf(byte[] buf, int w, int pitch, int h, int pf, |
DRC | b2f9415 | 2011-04-02 02:09:03 +0000 | [diff] [blame] | 229 | int subsamp, TJScalingFactor sf, int flags) throws Exception { |
DRC | 2c74e51 | 2011-03-16 00:02:53 +0000 | [diff] [blame] | 230 | int roffset = TJ.getRedOffset(pf); |
| 231 | int goffset = TJ.getGreenOffset(pf); |
| 232 | int boffset = TJ.getBlueOffset(pf); |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 233 | int ps = TJ.getPixelSize(pf); |
DRC | f962fbb | 2011-05-23 05:49:08 +0000 | [diff] [blame] | 234 | int index, row, col, retval = 1; |
DRC | b2f9415 | 2011-04-02 02:09:03 +0000 | [diff] [blame] | 235 | int halfway = 16 * sf.getNum() / sf.getDenom(); |
| 236 | int blockSize = 8 * sf.getNum() / sf.getDenom(); |
DRC | 3bad53f | 2011-02-23 02:20:49 +0000 | [diff] [blame] | 237 | |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 238 | try { |
DRC | f962fbb | 2011-05-23 05:49:08 +0000 | [diff] [blame] | 239 | for(row = 0; row < halfway; row++) { |
| 240 | for(col = 0; col < w; col++) { |
| 241 | if((flags & TJ.FLAG_BOTTOMUP) != 0) |
| 242 | index = pitch * (h - row - 1) + col * ps; |
| 243 | else index = pitch * row + col * ps; |
| 244 | byte r = buf[index + roffset]; |
| 245 | byte g = buf[index + goffset]; |
| 246 | byte b = buf[index + boffset]; |
| 247 | if(((row / blockSize) + (col / blockSize)) % 2 == 0) { |
| 248 | if(row < halfway) { |
| 249 | checkVal255(row, col, r, "R"); |
| 250 | checkVal255(row, col, g, "G"); |
| 251 | checkVal255(row, col, b, "B"); |
| 252 | } |
| 253 | else { |
| 254 | checkVal0(row, col, r, "R"); |
| 255 | checkVal0(row, col, g, "G"); |
| 256 | checkVal0(row, col, b, "B"); |
| 257 | } |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 258 | } |
| 259 | else { |
| 260 | if(subsamp == TJ.SAMP_GRAY) { |
DRC | f962fbb | 2011-05-23 05:49:08 +0000 | [diff] [blame] | 261 | if(row < halfway) { |
| 262 | checkVal(row, col, r, "R", 76); |
| 263 | checkVal(row, col, g, "G", 76); |
| 264 | checkVal(row, col, b, "B", 76); |
| 265 | } |
| 266 | else { |
| 267 | checkVal(row, col, r, "R", 226); |
| 268 | checkVal(row, col, g, "G", 226); |
| 269 | checkVal(row, col, b, "B", 226); |
| 270 | } |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 271 | } |
| 272 | else { |
DRC | f962fbb | 2011-05-23 05:49:08 +0000 | [diff] [blame] | 273 | checkVal255(row, col, r, "R"); |
| 274 | if(row < halfway) { |
| 275 | checkVal0(row, col, g, "G"); |
| 276 | } |
| 277 | else { |
| 278 | checkVal255(row, col, g, "G"); |
| 279 | } |
| 280 | checkVal0(row, col, b, "B"); |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 281 | } |
| 282 | } |
| 283 | } |
| 284 | } |
| 285 | } |
| 286 | catch(Exception e) { |
| 287 | System.out.println(e); |
| 288 | retval = 0; |
| 289 | } |
DRC | 4f1580c | 2011-02-25 06:11:03 +0000 | [diff] [blame] | 290 | |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 291 | if(retval == 0) { |
| 292 | System.out.print("\n"); |
DRC | f962fbb | 2011-05-23 05:49:08 +0000 | [diff] [blame] | 293 | for(row = 0; row < h; row++) { |
| 294 | for(col = 0; col < w; col++) { |
| 295 | int r = buf[pitch * row + col * ps + roffset]; |
| 296 | int g = buf[pitch * row + col * ps + goffset]; |
| 297 | int b = buf[pitch * row + col * ps + boffset]; |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 298 | if(r < 0) r += 256; if(g < 0) g += 256; if(b < 0) b += 256; |
| 299 | System.out.format("%3d/%3d/%3d ", r, g, b); |
| 300 | } |
| 301 | System.out.print("\n"); |
| 302 | } |
| 303 | } |
| 304 | return retval; |
| 305 | } |
DRC | 4f1580c | 2011-02-25 06:11:03 +0000 | [diff] [blame] | 306 | |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 307 | private static int checkIntBuf(int[] buf, int w, int pitch, int h, int pf, |
DRC | b2f9415 | 2011-04-02 02:09:03 +0000 | [diff] [blame] | 308 | int subsamp, TJScalingFactor sf, int flags) throws Exception { |
DRC | 2c74e51 | 2011-03-16 00:02:53 +0000 | [diff] [blame] | 309 | int rshift = TJ.getRedOffset(pf) * 8; |
| 310 | int gshift = TJ.getGreenOffset(pf) * 8; |
| 311 | int bshift = TJ.getBlueOffset(pf) * 8; |
DRC | f962fbb | 2011-05-23 05:49:08 +0000 | [diff] [blame] | 312 | int index, row, col, retval = 1; |
DRC | b2f9415 | 2011-04-02 02:09:03 +0000 | [diff] [blame] | 313 | int halfway = 16 * sf.getNum() / sf.getDenom(); |
| 314 | int blockSize = 8 * sf.getNum() / sf.getDenom(); |
DRC | 4f1580c | 2011-02-25 06:11:03 +0000 | [diff] [blame] | 315 | |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 316 | try { |
DRC | f962fbb | 2011-05-23 05:49:08 +0000 | [diff] [blame] | 317 | for(row = 0; row < halfway; row++) { |
| 318 | for(col = 0; col < w; col++) { |
| 319 | if((flags & TJ.FLAG_BOTTOMUP) != 0) |
| 320 | index = pitch * (h - row - 1) + col; |
| 321 | else index = pitch * row + col; |
| 322 | int r = (buf[index] >> rshift) & 0xFF; |
| 323 | int g = (buf[index] >> gshift) & 0xFF; |
| 324 | int b = (buf[index] >> bshift) & 0xFF; |
| 325 | if(((row / blockSize) + (col / blockSize)) % 2 == 0) { |
| 326 | if(row < halfway) { |
| 327 | checkVal255(row, col, r, "R"); |
| 328 | checkVal255(row, col, g, "G"); |
| 329 | checkVal255(row, col, b, "B"); |
| 330 | } |
| 331 | else { |
| 332 | checkVal0(row, col, r, "R"); |
| 333 | checkVal0(row, col, g, "G"); |
| 334 | checkVal0(row, col, b, "B"); |
| 335 | } |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 336 | } |
| 337 | else { |
| 338 | if(subsamp == TJ.SAMP_GRAY) { |
DRC | f962fbb | 2011-05-23 05:49:08 +0000 | [diff] [blame] | 339 | if(row < halfway) { |
| 340 | checkVal(row, col, r, "R", 76); |
| 341 | checkVal(row, col, g, "G", 76); |
| 342 | checkVal(row, col, b, "B", 76); |
| 343 | } |
| 344 | else { |
| 345 | checkVal(row, col, r, "R", 226); |
| 346 | checkVal(row, col, g, "G", 226); |
| 347 | checkVal(row, col, b, "B", 226); |
| 348 | } |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 349 | } |
| 350 | else { |
DRC | f962fbb | 2011-05-23 05:49:08 +0000 | [diff] [blame] | 351 | checkVal255(row, col, r, "R"); |
| 352 | if(row < halfway) { |
| 353 | checkVal0(row, col, g, "G"); |
| 354 | } |
| 355 | else { |
| 356 | checkVal255(row, col, g, "G"); |
| 357 | } |
| 358 | checkVal0(row, col, b, "B"); |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 359 | } |
| 360 | } |
| 361 | } |
| 362 | } |
| 363 | } |
| 364 | catch(Exception e) { |
| 365 | System.out.println(e); |
| 366 | retval = 0; |
| 367 | } |
DRC | 4f1580c | 2011-02-25 06:11:03 +0000 | [diff] [blame] | 368 | |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 369 | if(retval == 0) { |
| 370 | System.out.print("\n"); |
DRC | f962fbb | 2011-05-23 05:49:08 +0000 | [diff] [blame] | 371 | for(row = 0; row < h; row++) { |
| 372 | for(col = 0; col < w; col++) { |
| 373 | int r = (buf[pitch * row + col] >> rshift) & 0xFF; |
| 374 | int g = (buf[pitch * row + col] >> gshift) & 0xFF; |
| 375 | int b = (buf[pitch * row + col] >> bshift) & 0xFF; |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 376 | if(r < 0) r += 256; if(g < 0) g += 256; if(b < 0) b += 256; |
| 377 | System.out.format("%3d/%3d/%3d ", r, g, b); |
| 378 | } |
| 379 | System.out.print("\n"); |
| 380 | } |
| 381 | } |
| 382 | return retval; |
| 383 | } |
DRC | 3bad53f | 2011-02-23 02:20:49 +0000 | [diff] [blame] | 384 | |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 385 | private static int checkImg(BufferedImage img, int pf, |
DRC | b2f9415 | 2011-04-02 02:09:03 +0000 | [diff] [blame] | 386 | int subsamp, TJScalingFactor sf, int flags) throws Exception { |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 387 | WritableRaster wr = img.getRaster(); |
| 388 | int imgtype = img.getType(); |
| 389 | if(imgtype == BufferedImage.TYPE_INT_RGB |
| 390 | || imgtype == BufferedImage.TYPE_INT_BGR) { |
| 391 | SinglePixelPackedSampleModel sm = |
| 392 | (SinglePixelPackedSampleModel)img.getSampleModel(); |
| 393 | int pitch = sm.getScanlineStride(); |
| 394 | DataBufferInt db = (DataBufferInt)wr.getDataBuffer(); |
| 395 | int[] buf = db.getData(); |
| 396 | return checkIntBuf(buf, img.getWidth(), pitch, img.getHeight(), pf, |
DRC | b2f9415 | 2011-04-02 02:09:03 +0000 | [diff] [blame] | 397 | subsamp, sf, flags); |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 398 | } |
| 399 | else { |
| 400 | ComponentSampleModel sm = (ComponentSampleModel)img.getSampleModel(); |
| 401 | int pitch = sm.getScanlineStride(); |
| 402 | DataBufferByte db = (DataBufferByte)wr.getDataBuffer(); |
| 403 | byte[] buf = db.getData(); |
| 404 | return checkBuf(buf, img.getWidth(), pitch, img.getHeight(), pf, subsamp, |
DRC | b2f9415 | 2011-04-02 02:09:03 +0000 | [diff] [blame] | 405 | sf, flags); |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 406 | } |
| 407 | } |
DRC | 84a1bcc | 2011-02-23 12:09:56 +0000 | [diff] [blame] | 408 | |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 409 | private static int PAD(int v, int p) { |
| 410 | return ((v + (p) - 1) & (~((p) - 1))); |
| 411 | } |
DRC | 3bad53f | 2011-02-23 02:20:49 +0000 | [diff] [blame] | 412 | |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 413 | private static int checkBufYUV(byte[] buf, int size, int w, int h, |
DRC | d0a8136 | 2011-03-04 13:04:24 +0000 | [diff] [blame] | 414 | int subsamp) throws Exception { |
DRC | 215aa8b | 2011-05-27 02:10:42 +0000 | [diff] [blame] | 415 | int row, col; |
DRC | d0a8136 | 2011-03-04 13:04:24 +0000 | [diff] [blame] | 416 | int hsf = TJ.getMCUWidth(subsamp)/8, vsf = TJ.getMCUHeight(subsamp)/8; |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 417 | int pw = PAD(w, hsf), ph = PAD(h, vsf); |
| 418 | int cw = pw / hsf, ch = ph / vsf; |
| 419 | int ypitch = PAD(pw, 4), uvpitch = PAD(cw, 4); |
| 420 | int retval = 1; |
| 421 | int correctsize = ypitch * ph |
| 422 | + (subsamp == TJ.SAMP_GRAY ? 0 : uvpitch * ch * 2); |
DRC | 215aa8b | 2011-05-27 02:10:42 +0000 | [diff] [blame] | 423 | int halfway = 16; |
DRC | 3bad53f | 2011-02-23 02:20:49 +0000 | [diff] [blame] | 424 | |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 425 | try { |
| 426 | if(size != correctsize) |
| 427 | throw new Exception("\nIncorrect size " + size + ". Should be " |
| 428 | + correctsize); |
DRC | 3bad53f | 2011-02-23 02:20:49 +0000 | [diff] [blame] | 429 | |
DRC | 215aa8b | 2011-05-27 02:10:42 +0000 | [diff] [blame] | 430 | for(row = 0; row < ph; row++) { |
| 431 | for(col = 0; col < pw; col++) { |
| 432 | byte y = buf[ypitch * row + col]; |
| 433 | if(((row / 8) + (col / 8)) % 2 == 0) { |
| 434 | if(row < halfway) checkVal255(row, col, y, "Y"); |
| 435 | else checkVal0(row, col, y, "Y"); |
| 436 | } |
| 437 | else { |
| 438 | if(row < halfway) checkVal(row, col, y, "Y", 76); |
| 439 | else checkVal(row, col, y, "Y", 226); |
| 440 | } |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 441 | } |
| 442 | } |
| 443 | if(subsamp != TJ.SAMP_GRAY) { |
DRC | 215aa8b | 2011-05-27 02:10:42 +0000 | [diff] [blame] | 444 | halfway = 16 / vsf; |
| 445 | for(row = 0; row < ch; row++) { |
| 446 | for(col = 0; col < cw; col++) { |
| 447 | byte u = buf[ypitch * ph + (uvpitch * row + col)], |
| 448 | v = buf[ypitch * ph + uvpitch * ch + (uvpitch * row + col)]; |
| 449 | if(((row * vsf / 8) + (col * hsf / 8)) % 2 == 0) { |
| 450 | checkVal(row, col, u, "U", 128); |
| 451 | checkVal(row, col, v, "V", 128); |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 452 | } |
| 453 | else { |
DRC | 215aa8b | 2011-05-27 02:10:42 +0000 | [diff] [blame] | 454 | if(row < halfway) { |
| 455 | checkVal(row, col, u, "U", 85); |
| 456 | checkVal255(row, col, v, "V"); |
| 457 | } |
| 458 | else { |
| 459 | checkVal0(row, col, u, "U"); |
| 460 | checkVal(row, col, v, "V", 149); |
| 461 | } |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 462 | } |
| 463 | } |
| 464 | } |
| 465 | } |
| 466 | } |
| 467 | catch(Exception e) { |
| 468 | System.out.println(e); |
| 469 | retval = 0; |
| 470 | } |
DRC | 3bad53f | 2011-02-23 02:20:49 +0000 | [diff] [blame] | 471 | |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 472 | if(retval == 0) { |
DRC | 215aa8b | 2011-05-27 02:10:42 +0000 | [diff] [blame] | 473 | for(row = 0; row < ph; row++) { |
| 474 | for(col = 0; col < pw; col++) { |
| 475 | int y = buf[ypitch * row + col]; |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 476 | if(y < 0) y += 256; |
| 477 | System.out.format("%3d ", y); |
| 478 | } |
| 479 | System.out.print("\n"); |
| 480 | } |
| 481 | System.out.print("\n"); |
DRC | 215aa8b | 2011-05-27 02:10:42 +0000 | [diff] [blame] | 482 | for(row = 0; row < ch; row++) { |
| 483 | for(col = 0; col < cw; col++) { |
| 484 | int u = buf[ypitch * ph + (uvpitch * row + col)]; |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 485 | if(u < 0) u += 256; |
| 486 | System.out.format("%3d ", u); |
| 487 | } |
| 488 | System.out.print("\n"); |
| 489 | } |
| 490 | System.out.print("\n"); |
DRC | 215aa8b | 2011-05-27 02:10:42 +0000 | [diff] [blame] | 491 | for(row = 0; row < ch; row++) { |
| 492 | for(col = 0; col < cw; col++) { |
| 493 | int v = buf[ypitch * ph + uvpitch * ch + (uvpitch * row + col)]; |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 494 | if(v < 0) v += 256; |
| 495 | System.out.format("%3d ", v); |
| 496 | } |
| 497 | System.out.print("\n"); |
| 498 | } |
| 499 | System.out.print("\n"); |
| 500 | } |
DRC | 3bad53f | 2011-02-23 02:20:49 +0000 | [diff] [blame] | 501 | |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 502 | return retval; |
| 503 | } |
DRC | 3bad53f | 2011-02-23 02:20:49 +0000 | [diff] [blame] | 504 | |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 505 | private static void writeJPEG(byte[] jpegBuf, int jpegBufSize, |
| 506 | String filename) throws Exception { |
| 507 | File file = new File(filename); |
| 508 | FileOutputStream fos = new FileOutputStream(file); |
| 509 | fos.write(jpegBuf, 0, jpegBufSize); |
| 510 | fos.close(); |
| 511 | } |
DRC | 3bad53f | 2011-02-23 02:20:49 +0000 | [diff] [blame] | 512 | |
DRC | f962fbb | 2011-05-23 05:49:08 +0000 | [diff] [blame] | 513 | private static int compTest(TJCompressor tjc, byte[] dstBuf, int w, |
| 514 | int h, int pf, String baseName, int subsamp, int jpegQual, |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 515 | int flags) throws Exception { |
| 516 | String tempstr; |
DRC | f962fbb | 2011-05-23 05:49:08 +0000 | [diff] [blame] | 517 | byte[] srcBuf = null; |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 518 | BufferedImage img = null; |
| 519 | String pfStr; |
| 520 | double t; |
| 521 | int size = 0, ps = TJ.getPixelSize(pf); |
DRC | 3bad53f | 2011-02-23 02:20:49 +0000 | [diff] [blame] | 522 | |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 523 | pfStr = pixFormatStr[pf]; |
DRC | 3bad53f | 2011-02-23 02:20:49 +0000 | [diff] [blame] | 524 | |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 525 | System.out.print(pfStr + " "); |
DRC | 92549de | 2011-03-15 20:52:02 +0000 | [diff] [blame] | 526 | if((flags & TJ.FLAG_BOTTOMUP) != 0) System.out.print("Bottom-Up"); |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 527 | else System.out.print("Top-Down "); |
| 528 | System.out.print(" -> " + subNameLong[subsamp] + " "); |
| 529 | if(yuv == YUVENCODE) System.out.print("YUV ... "); |
DRC | f962fbb | 2011-05-23 05:49:08 +0000 | [diff] [blame] | 530 | else System.out.print("Q" + jpegQual + " ... "); |
DRC | 3bad53f | 2011-02-23 02:20:49 +0000 | [diff] [blame] | 531 | |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 532 | if(bi) { |
| 533 | img = new BufferedImage(w, h, biType[pf]); |
| 534 | initImg(img, pf, flags); |
DRC | f962fbb | 2011-05-23 05:49:08 +0000 | [diff] [blame] | 535 | tempstr = baseName + "_enc_" + pfStr + "_" |
DRC | 92549de | 2011-03-15 20:52:02 +0000 | [diff] [blame] | 536 | + (((flags & TJ.FLAG_BOTTOMUP) != 0) ? "BU" : "TD") + "_" |
DRC | f962fbb | 2011-05-23 05:49:08 +0000 | [diff] [blame] | 537 | + subName[subsamp] + "_Q" + jpegQual + ".png"; |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 538 | File file = new File(tempstr); |
| 539 | ImageIO.write(img, "png", file); |
| 540 | } |
| 541 | else { |
DRC | f962fbb | 2011-05-23 05:49:08 +0000 | [diff] [blame] | 542 | srcBuf = new byte[w * h * ps + 1]; |
| 543 | initBuf(srcBuf, w, w * ps, h, pf, flags); |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 544 | } |
DRC | f962fbb | 2011-05-23 05:49:08 +0000 | [diff] [blame] | 545 | Arrays.fill(dstBuf, (byte)0); |
DRC | 3bad53f | 2011-02-23 02:20:49 +0000 | [diff] [blame] | 546 | |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 547 | t = getTime(); |
| 548 | tjc.setSubsamp(subsamp); |
DRC | f962fbb | 2011-05-23 05:49:08 +0000 | [diff] [blame] | 549 | tjc.setJPEGQuality(jpegQual); |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 550 | if(bi) { |
DRC | f962fbb | 2011-05-23 05:49:08 +0000 | [diff] [blame] | 551 | if(yuv == YUVENCODE) tjc.encodeYUV(img, dstBuf, flags); |
| 552 | else tjc.compress(img, dstBuf, flags); |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 553 | } |
| 554 | else { |
DRC | f962fbb | 2011-05-23 05:49:08 +0000 | [diff] [blame] | 555 | tjc.setSourceImage(srcBuf, w, 0, h, pf); |
| 556 | if(yuv == YUVENCODE) tjc.encodeYUV(dstBuf, flags); |
| 557 | else tjc.compress(dstBuf, flags); |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 558 | } |
| 559 | size = tjc.getCompressedSize(); |
| 560 | t = getTime() - t; |
DRC | 3bad53f | 2011-02-23 02:20:49 +0000 | [diff] [blame] | 561 | |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 562 | if(yuv == YUVENCODE) |
DRC | f962fbb | 2011-05-23 05:49:08 +0000 | [diff] [blame] | 563 | tempstr = baseName + "_enc_" + pfStr + "_" |
DRC | 92549de | 2011-03-15 20:52:02 +0000 | [diff] [blame] | 564 | + (((flags & TJ.FLAG_BOTTOMUP) != 0) ? "BU" : "TD") + "_" |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 565 | + subName[subsamp] + ".yuv"; |
| 566 | else |
DRC | f962fbb | 2011-05-23 05:49:08 +0000 | [diff] [blame] | 567 | tempstr = baseName + "_enc_" + pfStr + "_" |
DRC | 92549de | 2011-03-15 20:52:02 +0000 | [diff] [blame] | 568 | + (((flags & TJ.FLAG_BOTTOMUP) != 0) ? "BU" : "TD") + "_" |
DRC | f962fbb | 2011-05-23 05:49:08 +0000 | [diff] [blame] | 569 | + subName[subsamp] + "_Q" + jpegQual + ".jpg"; |
| 570 | writeJPEG(dstBuf, size, tempstr); |
DRC | 84a1bcc | 2011-02-23 12:09:56 +0000 | [diff] [blame] | 571 | |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 572 | if(yuv == YUVENCODE) { |
DRC | f962fbb | 2011-05-23 05:49:08 +0000 | [diff] [blame] | 573 | if(checkBufYUV(dstBuf, size, w, h, subsamp) == 1) |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 574 | System.out.print("Passed."); |
| 575 | else { |
| 576 | System.out.print("FAILED!"); exitStatus = -1; |
| 577 | } |
| 578 | } |
| 579 | else System.out.print("Done."); |
| 580 | System.out.format(" %.6f ms\n", t * 1000.); |
| 581 | System.out.println(" Result in " + tempstr); |
DRC | 3bad53f | 2011-02-23 02:20:49 +0000 | [diff] [blame] | 582 | |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 583 | return size; |
| 584 | } |
DRC | 3bad53f | 2011-02-23 02:20:49 +0000 | [diff] [blame] | 585 | |
DRC | f962fbb | 2011-05-23 05:49:08 +0000 | [diff] [blame] | 586 | private static void decompTest(TJDecompressor tjd, byte[] jpegBuf, |
| 587 | int jpegSize, int w, int h, int pf, String baseName, int subsamp, |
DRC | b2f9415 | 2011-04-02 02:09:03 +0000 | [diff] [blame] | 588 | int flags, TJScalingFactor sf) throws Exception { |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 589 | String pfStr, tempstr; |
| 590 | double t; |
DRC | b2f9415 | 2011-04-02 02:09:03 +0000 | [diff] [blame] | 591 | int scaledWidth = sf.getScaled(w); |
| 592 | int scaledHeight = sf.getScaled(h); |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 593 | int temp1, temp2; |
| 594 | BufferedImage img = null; |
DRC | f962fbb | 2011-05-23 05:49:08 +0000 | [diff] [blame] | 595 | byte[] dstBuf = null; |
DRC | 3bad53f | 2011-02-23 02:20:49 +0000 | [diff] [blame] | 596 | |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 597 | if(yuv == YUVENCODE) return; |
DRC | 3bad53f | 2011-02-23 02:20:49 +0000 | [diff] [blame] | 598 | |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 599 | pfStr = pixFormatStr[pf]; |
| 600 | System.out.print("JPEG -> "); |
| 601 | if(yuv == YUVDECODE) |
| 602 | System.out.print("YUV " + subName[subsamp] + " ... "); |
| 603 | else { |
| 604 | System.out.print(pfStr + " "); |
DRC | 92549de | 2011-03-15 20:52:02 +0000 | [diff] [blame] | 605 | if((flags & TJ.FLAG_BOTTOMUP) != 0) System.out.print("Bottom-Up "); |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 606 | else System.out.print("Top-Down "); |
DRC | b2f9415 | 2011-04-02 02:09:03 +0000 | [diff] [blame] | 607 | if(!sf.isOne()) |
| 608 | System.out.print(sf.getNum() + "/" + sf.getDenom() + " ... "); |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 609 | else System.out.print("... "); |
| 610 | } |
DRC | 3bad53f | 2011-02-23 02:20:49 +0000 | [diff] [blame] | 611 | |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 612 | t = getTime(); |
DRC | f962fbb | 2011-05-23 05:49:08 +0000 | [diff] [blame] | 613 | tjd.setJPEGImage(jpegBuf, jpegSize); |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 614 | if(tjd.getWidth() != w || tjd.getHeight() != h |
| 615 | || tjd.getSubsamp() != subsamp) |
| 616 | throw new Exception("Incorrect JPEG header"); |
DRC | 3bad53f | 2011-02-23 02:20:49 +0000 | [diff] [blame] | 617 | |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 618 | temp1 = scaledWidth; |
| 619 | temp2 = scaledHeight; |
| 620 | temp1 = tjd.getScaledWidth(temp1, temp2); |
| 621 | temp2 = tjd.getScaledHeight(temp1, temp2); |
| 622 | if(temp1 != scaledWidth || temp2 != scaledHeight) |
| 623 | throw new Exception("Scaled size mismatch"); |
DRC | 3bad53f | 2011-02-23 02:20:49 +0000 | [diff] [blame] | 624 | |
DRC | f962fbb | 2011-05-23 05:49:08 +0000 | [diff] [blame] | 625 | if(yuv == YUVDECODE) dstBuf = tjd.decompressToYUV(flags); |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 626 | else { |
| 627 | if(bi) |
| 628 | img = tjd.decompress(scaledWidth, scaledHeight, biType[pf], flags); |
DRC | f962fbb | 2011-05-23 05:49:08 +0000 | [diff] [blame] | 629 | else dstBuf = tjd.decompress(scaledWidth, 0, scaledHeight, pf, flags); |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 630 | } |
| 631 | t = getTime() - t; |
DRC | 3bad53f | 2011-02-23 02:20:49 +0000 | [diff] [blame] | 632 | |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 633 | if(bi) { |
DRC | f962fbb | 2011-05-23 05:49:08 +0000 | [diff] [blame] | 634 | tempstr = baseName + "_dec_" + pfStr + "_" |
DRC | 92549de | 2011-03-15 20:52:02 +0000 | [diff] [blame] | 635 | + (((flags & TJ.FLAG_BOTTOMUP) != 0) ? "BU" : "TD") + "_" |
DRC | b2f9415 | 2011-04-02 02:09:03 +0000 | [diff] [blame] | 636 | + subName[subsamp] + "_" + (double)sf.getNum() / (double)sf.getDenom() |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 637 | + "x" + ".png"; |
| 638 | File file = new File(tempstr); |
| 639 | ImageIO.write(img, "png", file); |
| 640 | } |
DRC | 84a1bcc | 2011-02-23 12:09:56 +0000 | [diff] [blame] | 641 | |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 642 | if(yuv == YUVDECODE) { |
DRC | f962fbb | 2011-05-23 05:49:08 +0000 | [diff] [blame] | 643 | if(checkBufYUV(dstBuf, dstBuf.length, w, h, subsamp) == 1) |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 644 | System.out.print("Passed."); |
| 645 | else { |
| 646 | System.out.print("FAILED!"); exitStatus = -1; |
| 647 | } |
| 648 | } |
| 649 | else { |
DRC | b2f9415 | 2011-04-02 02:09:03 +0000 | [diff] [blame] | 650 | if((bi && checkImg(img, pf, subsamp, sf, flags) == 1) |
DRC | f962fbb | 2011-05-23 05:49:08 +0000 | [diff] [blame] | 651 | || (!bi && checkBuf(dstBuf, scaledWidth, scaledWidth |
DRC | b2f9415 | 2011-04-02 02:09:03 +0000 | [diff] [blame] | 652 | * TJ.getPixelSize(pf), scaledHeight, pf, subsamp, sf, flags) == 1)) |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 653 | System.out.print("Passed."); |
| 654 | else { |
| 655 | System.out.print("FAILED!"); exitStatus = -1; |
| 656 | } |
| 657 | } |
| 658 | System.out.format(" %.6f ms\n", t * 1000.); |
| 659 | } |
DRC | 3bad53f | 2011-02-23 02:20:49 +0000 | [diff] [blame] | 660 | |
DRC | f962fbb | 2011-05-23 05:49:08 +0000 | [diff] [blame] | 661 | private static void decompTest(TJDecompressor tjd, byte[] jpegBuf, |
| 662 | int jpegSize, int w, int h, int pf, String baseName, int subsamp, |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 663 | int flags) throws Exception { |
| 664 | int i; |
| 665 | if((subsamp == TJ.SAMP_444 || subsamp == TJ.SAMP_GRAY) && yuv == 0) { |
DRC | b2f9415 | 2011-04-02 02:09:03 +0000 | [diff] [blame] | 666 | TJScalingFactor sf[] = TJ.getScalingFactors(); |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 667 | for(i = 0; i < sf.length; i++) |
DRC | f962fbb | 2011-05-23 05:49:08 +0000 | [diff] [blame] | 668 | decompTest(tjd, jpegBuf, jpegSize, w, h, pf, baseName, subsamp, |
DRC | b2f9415 | 2011-04-02 02:09:03 +0000 | [diff] [blame] | 669 | flags, sf[i]); |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 670 | } |
| 671 | else |
DRC | f962fbb | 2011-05-23 05:49:08 +0000 | [diff] [blame] | 672 | decompTest(tjd, jpegBuf, jpegSize, w, h, pf, baseName, subsamp, |
DRC | b2f9415 | 2011-04-02 02:09:03 +0000 | [diff] [blame] | 673 | flags, new TJScalingFactor(1, 1)); |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 674 | System.out.print("\n"); |
| 675 | } |
DRC | 3bad53f | 2011-02-23 02:20:49 +0000 | [diff] [blame] | 676 | |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 677 | private static void doTest(int w, int h, int[] formats, int subsamp, |
DRC | f962fbb | 2011-05-23 05:49:08 +0000 | [diff] [blame] | 678 | String baseName) throws Exception { |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 679 | TJCompressor tjc = null; |
| 680 | TJDecompressor tjd = null; |
DRC | 4f8c295 | 2011-03-31 10:06:17 +0000 | [diff] [blame] | 681 | int size; |
DRC | f962fbb | 2011-05-23 05:49:08 +0000 | [diff] [blame] | 682 | byte[] dstBuf; |
DRC | 3bad53f | 2011-02-23 02:20:49 +0000 | [diff] [blame] | 683 | |
DRC | f962fbb | 2011-05-23 05:49:08 +0000 | [diff] [blame] | 684 | if(yuv == YUVENCODE) dstBuf = new byte[TJ.bufSizeYUV(w, h, subsamp)]; |
DRC | 9b49f0e | 2011-07-12 03:17:23 +0000 | [diff] [blame^] | 685 | else dstBuf = new byte[TJ.bufSize(w, h, subsamp)]; |
DRC | 3bad53f | 2011-02-23 02:20:49 +0000 | [diff] [blame] | 686 | |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 687 | try { |
| 688 | tjc = new TJCompressor(); |
| 689 | tjd = new TJDecompressor(); |
DRC | 3bad53f | 2011-02-23 02:20:49 +0000 | [diff] [blame] | 690 | |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 691 | for(int pf : formats) { |
| 692 | for(int i = 0; i < 2; i++) { |
| 693 | int flags = 0; |
| 694 | if(i == 1) { |
| 695 | if(yuv == YUVDECODE) { |
| 696 | tjc.close(); tjd.close(); return; |
| 697 | } |
DRC | 92549de | 2011-03-15 20:52:02 +0000 | [diff] [blame] | 698 | else flags |= TJ.FLAG_BOTTOMUP; |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 699 | } |
DRC | f962fbb | 2011-05-23 05:49:08 +0000 | [diff] [blame] | 700 | size = compTest(tjc, dstBuf, w, h, pf, baseName, subsamp, 100, |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 701 | flags); |
DRC | f962fbb | 2011-05-23 05:49:08 +0000 | [diff] [blame] | 702 | decompTest(tjd, dstBuf, size, w, h, pf, baseName, subsamp, flags); |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 703 | } |
| 704 | } |
| 705 | } |
| 706 | catch(Exception e) { |
| 707 | if(tjc != null) tjc.close(); |
| 708 | if(tjd != null) tjd.close(); |
| 709 | throw e; |
| 710 | } |
| 711 | if(tjc != null) tjc.close(); |
| 712 | if(tjd != null) tjd.close(); |
| 713 | } |
DRC | 3bad53f | 2011-02-23 02:20:49 +0000 | [diff] [blame] | 714 | |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 715 | private static void doTest1() throws Exception { |
DRC | f962fbb | 2011-05-23 05:49:08 +0000 | [diff] [blame] | 716 | int w, h, i; |
| 717 | byte[] srcBuf, jpegBuf; |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 718 | TJCompressor tjc = null; |
DRC | 3bad53f | 2011-02-23 02:20:49 +0000 | [diff] [blame] | 719 | |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 720 | try { |
| 721 | tjc = new TJCompressor(); |
| 722 | System.out.println("Buffer size regression test"); |
DRC | f962fbb | 2011-05-23 05:49:08 +0000 | [diff] [blame] | 723 | for(w = 1; w < 48; w++) { |
| 724 | int maxh = (w == 1) ? 2048 : 48; |
| 725 | for(h = 1; h < maxh; h++) { |
| 726 | if(h % 100 == 0) |
| 727 | System.out.format("%04d x %04d\b\b\b\b\b\b\b\b\b\b\b", w, h); |
| 728 | srcBuf = new byte[w * h * 4]; |
DRC | 9b49f0e | 2011-07-12 03:17:23 +0000 | [diff] [blame^] | 729 | jpegBuf = new byte[TJ.bufSize(w, h, TJ.SAMP_444)]; |
DRC | f962fbb | 2011-05-23 05:49:08 +0000 | [diff] [blame] | 730 | Arrays.fill(srcBuf, (byte)0); |
| 731 | for(i = 0; i < w * h; i++) { |
| 732 | srcBuf[i * 4] = pixels[i % 9][0]; |
| 733 | srcBuf[i * 4 + 1] = pixels[i % 9][1]; |
| 734 | srcBuf[i * 4 + 2] = pixels[i % 9][2]; |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 735 | } |
DRC | f962fbb | 2011-05-23 05:49:08 +0000 | [diff] [blame] | 736 | tjc.setSourceImage(srcBuf, w, 0, h, TJ.PF_BGRX); |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 737 | tjc.setSubsamp(TJ.SAMP_444); |
| 738 | tjc.setJPEGQuality(100); |
| 739 | tjc.compress(jpegBuf, 0); |
DRC | 3bad53f | 2011-02-23 02:20:49 +0000 | [diff] [blame] | 740 | |
DRC | f962fbb | 2011-05-23 05:49:08 +0000 | [diff] [blame] | 741 | srcBuf = new byte[h * w * 4]; |
DRC | 9b49f0e | 2011-07-12 03:17:23 +0000 | [diff] [blame^] | 742 | jpegBuf = new byte[TJ.bufSize(h, w, TJ.SAMP_444)]; |
DRC | f962fbb | 2011-05-23 05:49:08 +0000 | [diff] [blame] | 743 | for(i = 0; i < h * w; i++) { |
| 744 | if(i % 2 == 0) srcBuf[i * 4] = |
| 745 | srcBuf[i * 4 + 1] = srcBuf[i * 4 + 2] = (byte)0xFF; |
| 746 | else srcBuf[i * 4] = srcBuf[i * 4 + 1] = srcBuf[i * 4 + 2] = 0; |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 747 | } |
DRC | f962fbb | 2011-05-23 05:49:08 +0000 | [diff] [blame] | 748 | tjc.setSourceImage(srcBuf, h, 0, w, TJ.PF_BGRX); |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 749 | tjc.compress(jpegBuf, 0); |
| 750 | } |
| 751 | } |
| 752 | System.out.println("Done. "); |
| 753 | } |
| 754 | catch(Exception e) { |
| 755 | if(tjc != null) tjc.close(); |
| 756 | throw e; |
| 757 | } |
| 758 | if(tjc != null) tjc.close(); |
| 759 | } |
DRC | 3bad53f | 2011-02-23 02:20:49 +0000 | [diff] [blame] | 760 | |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 761 | public static void main(String argv[]) { |
| 762 | try { |
DRC | b6ed7d3 | 2011-03-31 20:58:03 +0000 | [diff] [blame] | 763 | String testName = "javatest"; |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 764 | boolean doyuv = false; |
| 765 | for(int i = 0; i < argv.length; i++) { |
| 766 | if(argv[i].equalsIgnoreCase("-yuv")) doyuv = true; |
| 767 | if(argv[i].substring(0, 1).equalsIgnoreCase("-h") |
| 768 | || argv[i].equalsIgnoreCase("-?")) |
| 769 | usage(); |
DRC | b6ed7d3 | 2011-03-31 20:58:03 +0000 | [diff] [blame] | 770 | if(argv[i].equalsIgnoreCase("-bi")) { |
| 771 | bi = true; |
| 772 | testName = "javabitest"; |
| 773 | } |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 774 | } |
| 775 | if(doyuv) yuv = YUVENCODE; |
DRC | b6ed7d3 | 2011-03-31 20:58:03 +0000 | [diff] [blame] | 776 | doTest(35, 39, bi ? _3byteFormatsBI : _3byteFormats, TJ.SAMP_444, testName); |
| 777 | doTest(39, 41, bi ? _4byteFormatsBI : _4byteFormats, TJ.SAMP_444, testName); |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 778 | if(doyuv) { |
| 779 | doTest(41, 35, bi ? _3byteFormatsBI : _3byteFormats, TJ.SAMP_422, |
DRC | b6ed7d3 | 2011-03-31 20:58:03 +0000 | [diff] [blame] | 780 | testName); |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 781 | doTest(35, 39, bi ? _4byteFormatsBI : _4byteFormats, TJ.SAMP_422, |
DRC | b6ed7d3 | 2011-03-31 20:58:03 +0000 | [diff] [blame] | 782 | testName); |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 783 | doTest(39, 41, bi ? _3byteFormatsBI : _3byteFormats, TJ.SAMP_420, |
DRC | b6ed7d3 | 2011-03-31 20:58:03 +0000 | [diff] [blame] | 784 | testName); |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 785 | doTest(41, 35, bi ? _4byteFormatsBI : _4byteFormats, TJ.SAMP_420, |
DRC | b6ed7d3 | 2011-03-31 20:58:03 +0000 | [diff] [blame] | 786 | testName); |
DRC | d0a8136 | 2011-03-04 13:04:24 +0000 | [diff] [blame] | 787 | doTest(35, 39, bi ? _3byteFormatsBI : _3byteFormats, TJ.SAMP_440, |
DRC | b6ed7d3 | 2011-03-31 20:58:03 +0000 | [diff] [blame] | 788 | testName); |
DRC | d0a8136 | 2011-03-04 13:04:24 +0000 | [diff] [blame] | 789 | doTest(39, 41, bi ? _4byteFormatsBI : _4byteFormats, TJ.SAMP_440, |
DRC | b6ed7d3 | 2011-03-31 20:58:03 +0000 | [diff] [blame] | 790 | testName); |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 791 | } |
DRC | b6ed7d3 | 2011-03-31 20:58:03 +0000 | [diff] [blame] | 792 | doTest(35, 39, onlyGray, TJ.SAMP_GRAY, testName); |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 793 | doTest(39, 41, bi ? _3byteFormatsBI : _3byteFormats, TJ.SAMP_GRAY, |
DRC | b6ed7d3 | 2011-03-31 20:58:03 +0000 | [diff] [blame] | 794 | testName); |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 795 | doTest(41, 35, bi ? _4byteFormatsBI : _4byteFormats, TJ.SAMP_GRAY, |
DRC | b6ed7d3 | 2011-03-31 20:58:03 +0000 | [diff] [blame] | 796 | testName); |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 797 | if(!doyuv && !bi) doTest1(); |
| 798 | if(doyuv && !bi) { |
| 799 | yuv = YUVDECODE; |
DRC | b6ed7d3 | 2011-03-31 20:58:03 +0000 | [diff] [blame] | 800 | doTest(48, 48, onlyRGB, TJ.SAMP_444, "javatest_yuv0"); |
| 801 | doTest(35, 39, onlyRGB, TJ.SAMP_444, "javatest_yuv1"); |
| 802 | doTest(48, 48, onlyRGB, TJ.SAMP_422, "javatest_yuv0"); |
| 803 | doTest(39, 41, onlyRGB, TJ.SAMP_422, "javatest_yuv1"); |
| 804 | doTest(48, 48, onlyRGB, TJ.SAMP_420, "javatest_yuv0"); |
| 805 | doTest(41, 35, onlyRGB, TJ.SAMP_420, "javatest_yuv1"); |
| 806 | doTest(48, 48, onlyRGB, TJ.SAMP_440, "javatest_yuv0"); |
| 807 | doTest(35, 39, onlyRGB, TJ.SAMP_440, "javatest_yuv1"); |
| 808 | doTest(48, 48, onlyRGB, TJ.SAMP_GRAY, "javatest_yuv0"); |
| 809 | doTest(35, 39, onlyRGB, TJ.SAMP_GRAY, "javatest_yuv1"); |
| 810 | doTest(48, 48, onlyGray, TJ.SAMP_GRAY, "javatest_yuv0"); |
| 811 | doTest(39, 41, onlyGray, TJ.SAMP_GRAY, "javatest_yuv1"); |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 812 | } |
| 813 | } |
| 814 | catch(Exception e) { |
DRC | 2e2358e | 2011-03-04 09:54:59 +0000 | [diff] [blame] | 815 | e.printStackTrace(); |
DRC | f7f3ea4 | 2011-03-01 20:03:32 +0000 | [diff] [blame] | 816 | exitStatus = -1; |
| 817 | } |
| 818 | System.exit(exitStatus); |
| 819 | } |
DRC | 3bad53f | 2011-02-23 02:20:49 +0000 | [diff] [blame] | 820 | } |