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