13bad53fa04699ce22a7d2ba6763d7a357ef30540DRC/*
2dcf9f15d6e8acad63388b268452e9de1b1595d7cDRC * Copyright (C)2011-2015 D. R. Commander.  All Rights Reserved.
33bad53fa04699ce22a7d2ba6763d7a357ef30540DRC *
43bad53fa04699ce22a7d2ba6763d7a357ef30540DRC * Redistribution and use in source and binary forms, with or without
53bad53fa04699ce22a7d2ba6763d7a357ef30540DRC * modification, are permitted provided that the following conditions are met:
63bad53fa04699ce22a7d2ba6763d7a357ef30540DRC *
73bad53fa04699ce22a7d2ba6763d7a357ef30540DRC * - Redistributions of source code must retain the above copyright notice,
83bad53fa04699ce22a7d2ba6763d7a357ef30540DRC *   this list of conditions and the following disclaimer.
93bad53fa04699ce22a7d2ba6763d7a357ef30540DRC * - Redistributions in binary form must reproduce the above copyright notice,
103bad53fa04699ce22a7d2ba6763d7a357ef30540DRC *   this list of conditions and the following disclaimer in the documentation
113bad53fa04699ce22a7d2ba6763d7a357ef30540DRC *   and/or other materials provided with the distribution.
123bad53fa04699ce22a7d2ba6763d7a357ef30540DRC * - Neither the name of the libjpeg-turbo Project nor the names of its
133bad53fa04699ce22a7d2ba6763d7a357ef30540DRC *   contributors may be used to endorse or promote products derived from this
143bad53fa04699ce22a7d2ba6763d7a357ef30540DRC *   software without specific prior written permission.
153bad53fa04699ce22a7d2ba6763d7a357ef30540DRC *
163bad53fa04699ce22a7d2ba6763d7a357ef30540DRC * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS",
173bad53fa04699ce22a7d2ba6763d7a357ef30540DRC * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
183bad53fa04699ce22a7d2ba6763d7a357ef30540DRC * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
193bad53fa04699ce22a7d2ba6763d7a357ef30540DRC * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
203bad53fa04699ce22a7d2ba6763d7a357ef30540DRC * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
213bad53fa04699ce22a7d2ba6763d7a357ef30540DRC * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
223bad53fa04699ce22a7d2ba6763d7a357ef30540DRC * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
233bad53fa04699ce22a7d2ba6763d7a357ef30540DRC * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
243bad53fa04699ce22a7d2ba6763d7a357ef30540DRC * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
253bad53fa04699ce22a7d2ba6763d7a357ef30540DRC * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
263bad53fa04699ce22a7d2ba6763d7a357ef30540DRC * POSSIBILITY OF SUCH DAMAGE.
273bad53fa04699ce22a7d2ba6763d7a357ef30540DRC */
283bad53fa04699ce22a7d2ba6763d7a357ef30540DRC
293bad53fa04699ce22a7d2ba6763d7a357ef30540DRC/*
303bad53fa04699ce22a7d2ba6763d7a357ef30540DRC * This program tests the various code paths in the TurboJPEG JNI Wrapper
313bad53fa04699ce22a7d2ba6763d7a357ef30540DRC */
323bad53fa04699ce22a7d2ba6763d7a357ef30540DRC
333bad53fa04699ce22a7d2ba6763d7a357ef30540DRCimport java.io.*;
343bad53fa04699ce22a7d2ba6763d7a357ef30540DRCimport java.util.*;
3584a1bcca6fd0f21091a84f04b820b29012c60857DRCimport java.awt.image.*;
3684a1bcca6fd0f21091a84f04b820b29012c60857DRCimport javax.imageio.*;
37c08e8c15bc73e7931ac5b87b992b17bbbda7f332DRCimport java.nio.*;
383bad53fa04699ce22a7d2ba6763d7a357ef30540DRCimport org.libjpegturbo.turbojpeg.*;
393bad53fa04699ce22a7d2ba6763d7a357ef30540DRC
403bad53fa04699ce22a7d2ba6763d7a357ef30540DRCpublic class TJUnitTest {
413bad53fa04699ce22a7d2ba6763d7a357ef30540DRC
42f7f3ea404cc8618305efc059c34d881566206ed9DRC  private static final String classname =
43f7f3ea404cc8618305efc059c34d881566206ed9DRC    new TJUnitTest().getClass().getName();
44f7f3ea404cc8618305efc059c34d881566206ed9DRC
45f7f3ea404cc8618305efc059c34d881566206ed9DRC  private static void usage() {
46f7f3ea404cc8618305efc059c34d881566206ed9DRC    System.out.println("\nUSAGE: java " + classname + " [options]\n");
47f7f3ea404cc8618305efc059c34d881566206ed9DRC    System.out.println("Options:\n");
48f7f3ea404cc8618305efc059c34d881566206ed9DRC    System.out.println("-yuv = test YUV encoding/decoding support\n");
49fef9852da3a80bfaf84862462609f97d77ad6db7DRC    System.out.println("-noyuvpad = do not pad each line of each Y, U, and V plane to the nearest\n");
50fef9852da3a80bfaf84862462609f97d77ad6db7DRC    System.out.println("            4-byte boundary\n");
51f7f3ea404cc8618305efc059c34d881566206ed9DRC    System.out.println("-bi = test BufferedImage support\n");
52f7f3ea404cc8618305efc059c34d881566206ed9DRC    System.exit(1);
53f7f3ea404cc8618305efc059c34d881566206ed9DRC  }
54f7f3ea404cc8618305efc059c34d881566206ed9DRC
5567bee8683db22050e346e0d002c5969da854d582DRC  private static final String[] subNameLong = {
56a5830628b9cc92f9305ecb7d127ebc7207b12582DRC    "4:4:4", "4:2:2", "4:2:0", "GRAY", "4:4:0", "4:1:1"
57f7f3ea404cc8618305efc059c34d881566206ed9DRC  };
5867bee8683db22050e346e0d002c5969da854d582DRC  private static final String[] subName = {
59a5830628b9cc92f9305ecb7d127ebc7207b12582DRC    "444", "422", "420", "GRAY", "440", "411"
60f7f3ea404cc8618305efc059c34d881566206ed9DRC  };
61f7f3ea404cc8618305efc059c34d881566206ed9DRC
6267bee8683db22050e346e0d002c5969da854d582DRC  private static final String[] pixFormatStr = {
6367ce3b2352fe1f7511edbfed74ec6960e41e97dcDRC    "RGB", "BGR", "RGBX", "BGRX", "XBGR", "XRGB", "Grayscale",
6438cb1ec2a77b7209a260f3cced1e6c6365616c8eDRC    "RGBA", "BGRA", "ABGR", "ARGB", "CMYK"
65f7f3ea404cc8618305efc059c34d881566206ed9DRC  };
66c08e8c15bc73e7931ac5b87b992b17bbbda7f332DRC
6767bee8683db22050e346e0d002c5969da854d582DRC  private static final int[] alphaOffset = {
6838cb1ec2a77b7209a260f3cced1e6c6365616c8eDRC    -1, -1, -1, -1, -1, -1, -1, 3, 3, 0, 0, -1
69f7f3ea404cc8618305efc059c34d881566206ed9DRC  };
70f7f3ea404cc8618305efc059c34d881566206ed9DRC
7167bee8683db22050e346e0d002c5969da854d582DRC  private static final int[] _3byteFormats = {
72f7f3ea404cc8618305efc059c34d881566206ed9DRC    TJ.PF_RGB, TJ.PF_BGR
73f7f3ea404cc8618305efc059c34d881566206ed9DRC  };
7467bee8683db22050e346e0d002c5969da854d582DRC  private static final int[] _3byteFormatsBI = {
75c08e8c15bc73e7931ac5b87b992b17bbbda7f332DRC    BufferedImage.TYPE_3BYTE_BGR
76f7f3ea404cc8618305efc059c34d881566206ed9DRC  };
7767bee8683db22050e346e0d002c5969da854d582DRC  private static final int[] _4byteFormats = {
7838cb1ec2a77b7209a260f3cced1e6c6365616c8eDRC    TJ.PF_RGBX, TJ.PF_BGRX, TJ.PF_XBGR, TJ.PF_XRGB, TJ.PF_CMYK
79f7f3ea404cc8618305efc059c34d881566206ed9DRC  };
8067bee8683db22050e346e0d002c5969da854d582DRC  private static final int[] _4byteFormatsBI = {
81c08e8c15bc73e7931ac5b87b992b17bbbda7f332DRC    BufferedImage.TYPE_INT_BGR, BufferedImage.TYPE_INT_RGB,
82c08e8c15bc73e7931ac5b87b992b17bbbda7f332DRC    BufferedImage.TYPE_4BYTE_ABGR, BufferedImage.TYPE_4BYTE_ABGR_PRE,
83c08e8c15bc73e7931ac5b87b992b17bbbda7f332DRC    BufferedImage.TYPE_INT_ARGB, BufferedImage.TYPE_INT_ARGB_PRE
84f7f3ea404cc8618305efc059c34d881566206ed9DRC  };
8567bee8683db22050e346e0d002c5969da854d582DRC  private static final int[] onlyGray = {
86f7f3ea404cc8618305efc059c34d881566206ed9DRC    TJ.PF_GRAY
87f7f3ea404cc8618305efc059c34d881566206ed9DRC  };
8867bee8683db22050e346e0d002c5969da854d582DRC  private static final int[] onlyGrayBI = {
89c08e8c15bc73e7931ac5b87b992b17bbbda7f332DRC    BufferedImage.TYPE_BYTE_GRAY
90c08e8c15bc73e7931ac5b87b992b17bbbda7f332DRC  };
9167bee8683db22050e346e0d002c5969da854d582DRC  private static final int[] onlyRGB = {
92f7f3ea404cc8618305efc059c34d881566206ed9DRC    TJ.PF_RGB
93f7f3ea404cc8618305efc059c34d881566206ed9DRC  };
94f7f3ea404cc8618305efc059c34d881566206ed9DRC
95fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC  private static boolean doYUV = false;
96fef9852da3a80bfaf84862462609f97d77ad6db7DRC  private static int pad = 4;
97f7f3ea404cc8618305efc059c34d881566206ed9DRC  private static boolean bi = false;
98f7f3ea404cc8618305efc059c34d881566206ed9DRC
99f7f3ea404cc8618305efc059c34d881566206ed9DRC  private static int exitStatus = 0;
100f7f3ea404cc8618305efc059c34d881566206ed9DRC
101c08e8c15bc73e7931ac5b87b992b17bbbda7f332DRC  private static int biTypePF(int biType) {
102c08e8c15bc73e7931ac5b87b992b17bbbda7f332DRC    ByteOrder byteOrder = ByteOrder.nativeOrder();
103c08e8c15bc73e7931ac5b87b992b17bbbda7f332DRC    switch(biType) {
104c08e8c15bc73e7931ac5b87b992b17bbbda7f332DRC      case BufferedImage.TYPE_3BYTE_BGR:
105c08e8c15bc73e7931ac5b87b992b17bbbda7f332DRC        return TJ.PF_BGR;
106c08e8c15bc73e7931ac5b87b992b17bbbda7f332DRC      case BufferedImage.TYPE_4BYTE_ABGR:
107c08e8c15bc73e7931ac5b87b992b17bbbda7f332DRC      case BufferedImage.TYPE_4BYTE_ABGR_PRE:
108dcf9f15d6e8acad63388b268452e9de1b1595d7cDRC        return TJ.PF_ABGR;
109c08e8c15bc73e7931ac5b87b992b17bbbda7f332DRC      case BufferedImage.TYPE_BYTE_GRAY:
110c08e8c15bc73e7931ac5b87b992b17bbbda7f332DRC        return TJ.PF_GRAY;
111c08e8c15bc73e7931ac5b87b992b17bbbda7f332DRC      case BufferedImage.TYPE_INT_BGR:
11267bee8683db22050e346e0d002c5969da854d582DRC        if (byteOrder == ByteOrder.BIG_ENDIAN)
113c08e8c15bc73e7931ac5b87b992b17bbbda7f332DRC          return TJ.PF_XBGR;
114c08e8c15bc73e7931ac5b87b992b17bbbda7f332DRC        else
115c08e8c15bc73e7931ac5b87b992b17bbbda7f332DRC          return TJ.PF_RGBX;
116c08e8c15bc73e7931ac5b87b992b17bbbda7f332DRC      case BufferedImage.TYPE_INT_RGB:
11767bee8683db22050e346e0d002c5969da854d582DRC        if (byteOrder == ByteOrder.BIG_ENDIAN)
118c08e8c15bc73e7931ac5b87b992b17bbbda7f332DRC          return TJ.PF_XRGB;
119c08e8c15bc73e7931ac5b87b992b17bbbda7f332DRC        else
120c08e8c15bc73e7931ac5b87b992b17bbbda7f332DRC          return TJ.PF_BGRX;
12167ce3b2352fe1f7511edbfed74ec6960e41e97dcDRC      case BufferedImage.TYPE_INT_ARGB:
12267ce3b2352fe1f7511edbfed74ec6960e41e97dcDRC      case BufferedImage.TYPE_INT_ARGB_PRE:
12367bee8683db22050e346e0d002c5969da854d582DRC        if (byteOrder == ByteOrder.BIG_ENDIAN)
12467ce3b2352fe1f7511edbfed74ec6960e41e97dcDRC          return TJ.PF_ARGB;
12567ce3b2352fe1f7511edbfed74ec6960e41e97dcDRC        else
12667ce3b2352fe1f7511edbfed74ec6960e41e97dcDRC          return TJ.PF_BGRA;
127c08e8c15bc73e7931ac5b87b992b17bbbda7f332DRC    }
128c08e8c15bc73e7931ac5b87b992b17bbbda7f332DRC    return 0;
129c08e8c15bc73e7931ac5b87b992b17bbbda7f332DRC  }
130c08e8c15bc73e7931ac5b87b992b17bbbda7f332DRC
131c08e8c15bc73e7931ac5b87b992b17bbbda7f332DRC  private static String biTypeStr(int biType) {
132c08e8c15bc73e7931ac5b87b992b17bbbda7f332DRC    switch(biType) {
133c08e8c15bc73e7931ac5b87b992b17bbbda7f332DRC      case BufferedImage.TYPE_3BYTE_BGR:
134c08e8c15bc73e7931ac5b87b992b17bbbda7f332DRC        return "3BYTE_BGR";
135c08e8c15bc73e7931ac5b87b992b17bbbda7f332DRC      case BufferedImage.TYPE_4BYTE_ABGR:
136c08e8c15bc73e7931ac5b87b992b17bbbda7f332DRC        return "4BYTE_ABGR";
137c08e8c15bc73e7931ac5b87b992b17bbbda7f332DRC      case BufferedImage.TYPE_4BYTE_ABGR_PRE:
138c08e8c15bc73e7931ac5b87b992b17bbbda7f332DRC        return "4BYTE_ABGR_PRE";
139c08e8c15bc73e7931ac5b87b992b17bbbda7f332DRC      case BufferedImage.TYPE_BYTE_GRAY:
140c08e8c15bc73e7931ac5b87b992b17bbbda7f332DRC        return "BYTE_GRAY";
141c08e8c15bc73e7931ac5b87b992b17bbbda7f332DRC      case BufferedImage.TYPE_INT_BGR:
142c08e8c15bc73e7931ac5b87b992b17bbbda7f332DRC        return "INT_BGR";
143c08e8c15bc73e7931ac5b87b992b17bbbda7f332DRC      case BufferedImage.TYPE_INT_RGB:
144c08e8c15bc73e7931ac5b87b992b17bbbda7f332DRC        return "INT_RGB";
145c08e8c15bc73e7931ac5b87b992b17bbbda7f332DRC      case BufferedImage.TYPE_INT_ARGB:
146c08e8c15bc73e7931ac5b87b992b17bbbda7f332DRC        return "INT_ARGB";
147c08e8c15bc73e7931ac5b87b992b17bbbda7f332DRC      case BufferedImage.TYPE_INT_ARGB_PRE:
148c08e8c15bc73e7931ac5b87b992b17bbbda7f332DRC        return "INT_ARGB_PRE";
149c08e8c15bc73e7931ac5b87b992b17bbbda7f332DRC    }
150c08e8c15bc73e7931ac5b87b992b17bbbda7f332DRC    return "Unknown";
151c08e8c15bc73e7931ac5b87b992b17bbbda7f332DRC  }
152c08e8c15bc73e7931ac5b87b992b17bbbda7f332DRC
153f7f3ea404cc8618305efc059c34d881566206ed9DRC  private static void initBuf(byte[] buf, int w, int pitch, int h, int pf,
15467bee8683db22050e346e0d002c5969da854d582DRC                              int flags) throws Exception {
1552c74e5124d25112809bdc26cbc36aa764e8870c3DRC    int roffset = TJ.getRedOffset(pf);
1562c74e5124d25112809bdc26cbc36aa764e8870c3DRC    int goffset = TJ.getGreenOffset(pf);
1572c74e5124d25112809bdc26cbc36aa764e8870c3DRC    int boffset = TJ.getBlueOffset(pf);
158c08e8c15bc73e7931ac5b87b992b17bbbda7f332DRC    int aoffset = alphaOffset[pf];
159f7f3ea404cc8618305efc059c34d881566206ed9DRC    int ps = TJ.getPixelSize(pf);
160f962fbb44ad4d7b8200679ad0ed9263e10252638DRC    int index, row, col, halfway = 16;
161f7f3ea404cc8618305efc059c34d881566206ed9DRC
16267bee8683db22050e346e0d002c5969da854d582DRC    if (pf == TJ.PF_GRAY) {
16338cb1ec2a77b7209a260f3cced1e6c6365616c8eDRC      Arrays.fill(buf, (byte)0);
16467bee8683db22050e346e0d002c5969da854d582DRC      for (row = 0; row < h; row++) {
16567bee8683db22050e346e0d002c5969da854d582DRC        for (col = 0; col < w; col++) {
16667bee8683db22050e346e0d002c5969da854d582DRC          if ((flags & TJ.FLAG_BOTTOMUP) != 0)
167f962fbb44ad4d7b8200679ad0ed9263e10252638DRC            index = pitch * (h - row - 1) + col;
16867bee8683db22050e346e0d002c5969da854d582DRC          else
16967bee8683db22050e346e0d002c5969da854d582DRC            index = pitch * row + col;
17067bee8683db22050e346e0d002c5969da854d582DRC          if (((row / 8) + (col / 8)) % 2 == 0)
171f962fbb44ad4d7b8200679ad0ed9263e10252638DRC            buf[index] = (row < halfway) ? (byte)255 : 0;
17267bee8683db22050e346e0d002c5969da854d582DRC          else
17367bee8683db22050e346e0d002c5969da854d582DRC            buf[index] = (row < halfway) ? 76 : (byte)226;
174f7f3ea404cc8618305efc059c34d881566206ed9DRC        }
175f7f3ea404cc8618305efc059c34d881566206ed9DRC      }
176f7f3ea404cc8618305efc059c34d881566206ed9DRC      return;
177f7f3ea404cc8618305efc059c34d881566206ed9DRC    }
17838cb1ec2a77b7209a260f3cced1e6c6365616c8eDRC    if (pf == TJ.PF_CMYK) {
17938cb1ec2a77b7209a260f3cced1e6c6365616c8eDRC      Arrays.fill(buf, (byte)255);
18038cb1ec2a77b7209a260f3cced1e6c6365616c8eDRC      for (row = 0; row < h; row++) {
18138cb1ec2a77b7209a260f3cced1e6c6365616c8eDRC        for (col = 0; col < w; col++) {
18238cb1ec2a77b7209a260f3cced1e6c6365616c8eDRC          if ((flags & TJ.FLAG_BOTTOMUP) != 0)
18338cb1ec2a77b7209a260f3cced1e6c6365616c8eDRC            index = (h - row - 1) * w + col;
18438cb1ec2a77b7209a260f3cced1e6c6365616c8eDRC          else
18538cb1ec2a77b7209a260f3cced1e6c6365616c8eDRC            index = row * w + col;
18638cb1ec2a77b7209a260f3cced1e6c6365616c8eDRC          if (((row / 8) + (col / 8)) % 2 == 0) {
18738cb1ec2a77b7209a260f3cced1e6c6365616c8eDRC            if (row >= halfway) buf[index * ps + 3] = 0;
18838cb1ec2a77b7209a260f3cced1e6c6365616c8eDRC          } else {
18938cb1ec2a77b7209a260f3cced1e6c6365616c8eDRC            buf[index * ps + 2] = 0;
19038cb1ec2a77b7209a260f3cced1e6c6365616c8eDRC            if (row < halfway)
19138cb1ec2a77b7209a260f3cced1e6c6365616c8eDRC              buf[index * ps + 1] = 0;
19238cb1ec2a77b7209a260f3cced1e6c6365616c8eDRC          }
19338cb1ec2a77b7209a260f3cced1e6c6365616c8eDRC        }
19438cb1ec2a77b7209a260f3cced1e6c6365616c8eDRC      }
19538cb1ec2a77b7209a260f3cced1e6c6365616c8eDRC      return;
19638cb1ec2a77b7209a260f3cced1e6c6365616c8eDRC    }
19738cb1ec2a77b7209a260f3cced1e6c6365616c8eDRC
19838cb1ec2a77b7209a260f3cced1e6c6365616c8eDRC    Arrays.fill(buf, (byte)0);
19967bee8683db22050e346e0d002c5969da854d582DRC    for (row = 0; row < h; row++) {
20067bee8683db22050e346e0d002c5969da854d582DRC      for (col = 0; col < w; col++) {
20167bee8683db22050e346e0d002c5969da854d582DRC        if ((flags & TJ.FLAG_BOTTOMUP) != 0)
202f962fbb44ad4d7b8200679ad0ed9263e10252638DRC          index = pitch * (h - row - 1) + col * ps;
20367bee8683db22050e346e0d002c5969da854d582DRC        else
20467bee8683db22050e346e0d002c5969da854d582DRC          index = pitch * row + col * ps;
20567bee8683db22050e346e0d002c5969da854d582DRC        if (((row / 8) + (col / 8)) % 2 == 0) {
20667bee8683db22050e346e0d002c5969da854d582DRC          if (row < halfway) {
207f962fbb44ad4d7b8200679ad0ed9263e10252638DRC            buf[index + roffset] = (byte)255;
208f962fbb44ad4d7b8200679ad0ed9263e10252638DRC            buf[index + goffset] = (byte)255;
209f962fbb44ad4d7b8200679ad0ed9263e10252638DRC            buf[index + boffset] = (byte)255;
210f962fbb44ad4d7b8200679ad0ed9263e10252638DRC          }
21167bee8683db22050e346e0d002c5969da854d582DRC        } else {
212f962fbb44ad4d7b8200679ad0ed9263e10252638DRC          buf[index + roffset] = (byte)255;
21367bee8683db22050e346e0d002c5969da854d582DRC          if (row >= halfway)
21467bee8683db22050e346e0d002c5969da854d582DRC            buf[index + goffset] = (byte)255;
215f7f3ea404cc8618305efc059c34d881566206ed9DRC        }
21667bee8683db22050e346e0d002c5969da854d582DRC        if (aoffset >= 0)
21767bee8683db22050e346e0d002c5969da854d582DRC          buf[index + aoffset] = (byte)255;
218f7f3ea404cc8618305efc059c34d881566206ed9DRC      }
219f7f3ea404cc8618305efc059c34d881566206ed9DRC    }
220f7f3ea404cc8618305efc059c34d881566206ed9DRC  }
221f7f3ea404cc8618305efc059c34d881566206ed9DRC
222f7f3ea404cc8618305efc059c34d881566206ed9DRC  private static void initIntBuf(int[] buf, int w, int pitch, int h, int pf,
22367bee8683db22050e346e0d002c5969da854d582DRC                                 int flags) throws Exception {
2242c74e5124d25112809bdc26cbc36aa764e8870c3DRC    int rshift = TJ.getRedOffset(pf) * 8;
2252c74e5124d25112809bdc26cbc36aa764e8870c3DRC    int gshift = TJ.getGreenOffset(pf) * 8;
2262c74e5124d25112809bdc26cbc36aa764e8870c3DRC    int bshift = TJ.getBlueOffset(pf) * 8;
227c08e8c15bc73e7931ac5b87b992b17bbbda7f332DRC    int ashift = alphaOffset[pf] * 8;
228f962fbb44ad4d7b8200679ad0ed9263e10252638DRC    int index, row, col, halfway = 16;
229f7f3ea404cc8618305efc059c34d881566206ed9DRC
230f7f3ea404cc8618305efc059c34d881566206ed9DRC    Arrays.fill(buf, 0);
23167bee8683db22050e346e0d002c5969da854d582DRC    for (row = 0; row < h; row++) {
23267bee8683db22050e346e0d002c5969da854d582DRC      for (col = 0; col < w; col++) {
23367bee8683db22050e346e0d002c5969da854d582DRC        if ((flags & TJ.FLAG_BOTTOMUP) != 0)
234f962fbb44ad4d7b8200679ad0ed9263e10252638DRC          index = pitch * (h - row - 1) + col;
23567bee8683db22050e346e0d002c5969da854d582DRC        else
23667bee8683db22050e346e0d002c5969da854d582DRC          index = pitch * row + col;
23767bee8683db22050e346e0d002c5969da854d582DRC        if (((row / 8) + (col / 8)) % 2 == 0) {
23867bee8683db22050e346e0d002c5969da854d582DRC          if (row < halfway) {
239f962fbb44ad4d7b8200679ad0ed9263e10252638DRC            buf[index] |= (255 << rshift);
240f962fbb44ad4d7b8200679ad0ed9263e10252638DRC            buf[index] |= (255 << gshift);
241f962fbb44ad4d7b8200679ad0ed9263e10252638DRC            buf[index] |= (255 << bshift);
242f962fbb44ad4d7b8200679ad0ed9263e10252638DRC          }
24367bee8683db22050e346e0d002c5969da854d582DRC        } else {
244f962fbb44ad4d7b8200679ad0ed9263e10252638DRC          buf[index] |= (255 << rshift);
24567bee8683db22050e346e0d002c5969da854d582DRC          if (row >= halfway)
24667bee8683db22050e346e0d002c5969da854d582DRC            buf[index] |= (255 << gshift);
247f7f3ea404cc8618305efc059c34d881566206ed9DRC        }
24867bee8683db22050e346e0d002c5969da854d582DRC        if (ashift >= 0)
24967bee8683db22050e346e0d002c5969da854d582DRC          buf[index] |= (255 << ashift);
250f7f3ea404cc8618305efc059c34d881566206ed9DRC      }
251f7f3ea404cc8618305efc059c34d881566206ed9DRC    }
252f7f3ea404cc8618305efc059c34d881566206ed9DRC  }
253f7f3ea404cc8618305efc059c34d881566206ed9DRC
254f7f3ea404cc8618305efc059c34d881566206ed9DRC  private static void initImg(BufferedImage img, int pf, int flags)
25567bee8683db22050e346e0d002c5969da854d582DRC                              throws Exception {
256f7f3ea404cc8618305efc059c34d881566206ed9DRC    WritableRaster wr = img.getRaster();
257c08e8c15bc73e7931ac5b87b992b17bbbda7f332DRC    int imgType = img.getType();
25867bee8683db22050e346e0d002c5969da854d582DRC    if (imgType == BufferedImage.TYPE_INT_RGB ||
25967bee8683db22050e346e0d002c5969da854d582DRC        imgType == BufferedImage.TYPE_INT_BGR ||
26067bee8683db22050e346e0d002c5969da854d582DRC        imgType == BufferedImage.TYPE_INT_ARGB ||
26167bee8683db22050e346e0d002c5969da854d582DRC        imgType == BufferedImage.TYPE_INT_ARGB_PRE) {
262f7f3ea404cc8618305efc059c34d881566206ed9DRC      SinglePixelPackedSampleModel sm =
263f7f3ea404cc8618305efc059c34d881566206ed9DRC        (SinglePixelPackedSampleModel)img.getSampleModel();
264f7f3ea404cc8618305efc059c34d881566206ed9DRC      int pitch = sm.getScanlineStride();
265f7f3ea404cc8618305efc059c34d881566206ed9DRC      DataBufferInt db = (DataBufferInt)wr.getDataBuffer();
266f7f3ea404cc8618305efc059c34d881566206ed9DRC      int[] buf = db.getData();
267f7f3ea404cc8618305efc059c34d881566206ed9DRC      initIntBuf(buf, img.getWidth(), pitch, img.getHeight(), pf, flags);
26867bee8683db22050e346e0d002c5969da854d582DRC    } else {
269f7f3ea404cc8618305efc059c34d881566206ed9DRC      ComponentSampleModel sm = (ComponentSampleModel)img.getSampleModel();
270f7f3ea404cc8618305efc059c34d881566206ed9DRC      int pitch = sm.getScanlineStride();
271f7f3ea404cc8618305efc059c34d881566206ed9DRC      DataBufferByte db = (DataBufferByte)wr.getDataBuffer();
272f7f3ea404cc8618305efc059c34d881566206ed9DRC      byte[] buf = db.getData();
273f7f3ea404cc8618305efc059c34d881566206ed9DRC      initBuf(buf, img.getWidth(), pitch, img.getHeight(), pf, flags);
274f7f3ea404cc8618305efc059c34d881566206ed9DRC    }
275f7f3ea404cc8618305efc059c34d881566206ed9DRC  }
276f7f3ea404cc8618305efc059c34d881566206ed9DRC
277f962fbb44ad4d7b8200679ad0ed9263e10252638DRC  private static void checkVal(int row, int col, int v, String vname, int cv)
27867bee8683db22050e346e0d002c5969da854d582DRC                               throws Exception {
279f7f3ea404cc8618305efc059c34d881566206ed9DRC    v = (v < 0) ? v + 256 : v;
28067bee8683db22050e346e0d002c5969da854d582DRC    if (v < cv - 1 || v > cv + 1) {
281b7c41932ed1ee9859a1aeea75e6e379e46a46158DRC      throw new Exception("Comp. " + vname + " at " + row + "," + col +
282b7c41932ed1ee9859a1aeea75e6e379e46a46158DRC                          " should be " + cv + ", not " + v);
283f7f3ea404cc8618305efc059c34d881566206ed9DRC    }
284f7f3ea404cc8618305efc059c34d881566206ed9DRC  }
285f7f3ea404cc8618305efc059c34d881566206ed9DRC
286f962fbb44ad4d7b8200679ad0ed9263e10252638DRC  private static void checkVal0(int row, int col, int v, String vname)
28767bee8683db22050e346e0d002c5969da854d582DRC                                throws Exception {
288f7f3ea404cc8618305efc059c34d881566206ed9DRC    v = (v < 0) ? v + 256 : v;
28967bee8683db22050e346e0d002c5969da854d582DRC    if (v > 1) {
290b7c41932ed1ee9859a1aeea75e6e379e46a46158DRC      throw new Exception("Comp. " + vname + " at " + row + "," + col +
291b7c41932ed1ee9859a1aeea75e6e379e46a46158DRC                          " should be 0, not " + v);
292f7f3ea404cc8618305efc059c34d881566206ed9DRC    }
293f7f3ea404cc8618305efc059c34d881566206ed9DRC  }
294f7f3ea404cc8618305efc059c34d881566206ed9DRC
295f962fbb44ad4d7b8200679ad0ed9263e10252638DRC  private static void checkVal255(int row, int col, int v, String vname)
29667bee8683db22050e346e0d002c5969da854d582DRC                                  throws Exception {
297f7f3ea404cc8618305efc059c34d881566206ed9DRC    v = (v < 0) ? v + 256 : v;
29867bee8683db22050e346e0d002c5969da854d582DRC    if (v < 254) {
299b7c41932ed1ee9859a1aeea75e6e379e46a46158DRC      throw new Exception("Comp. " + vname + " at " + row + "," + col +
300b7c41932ed1ee9859a1aeea75e6e379e46a46158DRC                          " should be 255, not " + v);
301f7f3ea404cc8618305efc059c34d881566206ed9DRC    }
302f7f3ea404cc8618305efc059c34d881566206ed9DRC  }
303f7f3ea404cc8618305efc059c34d881566206ed9DRC
304f7f3ea404cc8618305efc059c34d881566206ed9DRC  private static int checkBuf(byte[] buf, int w, int pitch, int h, int pf,
30567bee8683db22050e346e0d002c5969da854d582DRC                              int subsamp, TJScalingFactor sf, int flags)
30667bee8683db22050e346e0d002c5969da854d582DRC                              throws Exception {
3072c74e5124d25112809bdc26cbc36aa764e8870c3DRC    int roffset = TJ.getRedOffset(pf);
3082c74e5124d25112809bdc26cbc36aa764e8870c3DRC    int goffset = TJ.getGreenOffset(pf);
3092c74e5124d25112809bdc26cbc36aa764e8870c3DRC    int boffset = TJ.getBlueOffset(pf);
310c08e8c15bc73e7931ac5b87b992b17bbbda7f332DRC    int aoffset = alphaOffset[pf];
311f7f3ea404cc8618305efc059c34d881566206ed9DRC    int ps = TJ.getPixelSize(pf);
312f962fbb44ad4d7b8200679ad0ed9263e10252638DRC    int index, row, col, retval = 1;
313b2f9415a6365b8eb412a5b6159be3cc0f875325fDRC    int halfway = 16 * sf.getNum() / sf.getDenom();
314b2f9415a6365b8eb412a5b6159be3cc0f875325fDRC    int blockSize = 8 * sf.getNum() / sf.getDenom();
315f7f3ea404cc8618305efc059c34d881566206ed9DRC
316f7f3ea404cc8618305efc059c34d881566206ed9DRC    try {
31738cb1ec2a77b7209a260f3cced1e6c6365616c8eDRC
31838cb1ec2a77b7209a260f3cced1e6c6365616c8eDRC      if (pf == TJ.PF_CMYK) {
31938cb1ec2a77b7209a260f3cced1e6c6365616c8eDRC        for (row = 0; row < h; row++) {
32038cb1ec2a77b7209a260f3cced1e6c6365616c8eDRC          for (col = 0; col < w; col++) {
32138cb1ec2a77b7209a260f3cced1e6c6365616c8eDRC            if ((flags & TJ.FLAG_BOTTOMUP) != 0)
32238cb1ec2a77b7209a260f3cced1e6c6365616c8eDRC              index = (h - row - 1) * w + col;
32338cb1ec2a77b7209a260f3cced1e6c6365616c8eDRC            else
32438cb1ec2a77b7209a260f3cced1e6c6365616c8eDRC              index = row * w + col;
32538cb1ec2a77b7209a260f3cced1e6c6365616c8eDRC            byte c = buf[index * ps];
32638cb1ec2a77b7209a260f3cced1e6c6365616c8eDRC            byte m = buf[index * ps + 1];
3271a45b81fa2edf682ae72251a18fd84eacef9dbbeDRC            byte y = buf[index * ps + 2];
32838cb1ec2a77b7209a260f3cced1e6c6365616c8eDRC            byte k = buf[index * ps + 3];
32938cb1ec2a77b7209a260f3cced1e6c6365616c8eDRC            checkVal255(row, col, c, "C");
33038cb1ec2a77b7209a260f3cced1e6c6365616c8eDRC            if (((row / blockSize) + (col / blockSize)) % 2 == 0) {
33138cb1ec2a77b7209a260f3cced1e6c6365616c8eDRC              checkVal255(row, col, m, "M");
33238cb1ec2a77b7209a260f3cced1e6c6365616c8eDRC              checkVal255(row, col, y, "Y");
33338cb1ec2a77b7209a260f3cced1e6c6365616c8eDRC              if (row < halfway)
33438cb1ec2a77b7209a260f3cced1e6c6365616c8eDRC                checkVal255(row, col, k, "K");
33538cb1ec2a77b7209a260f3cced1e6c6365616c8eDRC              else
33638cb1ec2a77b7209a260f3cced1e6c6365616c8eDRC                checkVal0(row, col, k, "K");
33738cb1ec2a77b7209a260f3cced1e6c6365616c8eDRC            } else {
33838cb1ec2a77b7209a260f3cced1e6c6365616c8eDRC              checkVal0(row, col, y, "Y");
33938cb1ec2a77b7209a260f3cced1e6c6365616c8eDRC              checkVal255(row, col, k, "K");
34038cb1ec2a77b7209a260f3cced1e6c6365616c8eDRC              if (row < halfway)
34138cb1ec2a77b7209a260f3cced1e6c6365616c8eDRC                checkVal0(row, col, m, "M");
34238cb1ec2a77b7209a260f3cced1e6c6365616c8eDRC              else
34338cb1ec2a77b7209a260f3cced1e6c6365616c8eDRC                checkVal255(row, col, m, "M");
34438cb1ec2a77b7209a260f3cced1e6c6365616c8eDRC            }
34538cb1ec2a77b7209a260f3cced1e6c6365616c8eDRC          }
34638cb1ec2a77b7209a260f3cced1e6c6365616c8eDRC        }
34738cb1ec2a77b7209a260f3cced1e6c6365616c8eDRC        return 1;
34838cb1ec2a77b7209a260f3cced1e6c6365616c8eDRC      }
34938cb1ec2a77b7209a260f3cced1e6c6365616c8eDRC
35067bee8683db22050e346e0d002c5969da854d582DRC      for (row = 0; row < halfway; row++) {
35167bee8683db22050e346e0d002c5969da854d582DRC        for (col = 0; col < w; col++) {
35267bee8683db22050e346e0d002c5969da854d582DRC          if ((flags & TJ.FLAG_BOTTOMUP) != 0)
353f962fbb44ad4d7b8200679ad0ed9263e10252638DRC            index = pitch * (h - row - 1) + col * ps;
35467bee8683db22050e346e0d002c5969da854d582DRC          else
35567bee8683db22050e346e0d002c5969da854d582DRC            index = pitch * row + col * ps;
356f962fbb44ad4d7b8200679ad0ed9263e10252638DRC          byte r = buf[index + roffset];
357f962fbb44ad4d7b8200679ad0ed9263e10252638DRC          byte g = buf[index + goffset];
358f962fbb44ad4d7b8200679ad0ed9263e10252638DRC          byte b = buf[index + boffset];
359c08e8c15bc73e7931ac5b87b992b17bbbda7f332DRC          byte a = aoffset >= 0 ? buf[index + aoffset] : (byte)255;
36067bee8683db22050e346e0d002c5969da854d582DRC          if (((row / blockSize) + (col / blockSize)) % 2 == 0) {
36167bee8683db22050e346e0d002c5969da854d582DRC            if (row < halfway) {
362f962fbb44ad4d7b8200679ad0ed9263e10252638DRC              checkVal255(row, col, r, "R");
363f962fbb44ad4d7b8200679ad0ed9263e10252638DRC              checkVal255(row, col, g, "G");
364f962fbb44ad4d7b8200679ad0ed9263e10252638DRC              checkVal255(row, col, b, "B");
36567bee8683db22050e346e0d002c5969da854d582DRC            } else {
366f962fbb44ad4d7b8200679ad0ed9263e10252638DRC              checkVal0(row, col, r, "R");
367f962fbb44ad4d7b8200679ad0ed9263e10252638DRC              checkVal0(row, col, g, "G");
368f962fbb44ad4d7b8200679ad0ed9263e10252638DRC              checkVal0(row, col, b, "B");
369f7f3ea404cc8618305efc059c34d881566206ed9DRC            }
37067bee8683db22050e346e0d002c5969da854d582DRC          } else {
37167bee8683db22050e346e0d002c5969da854d582DRC            if (subsamp == TJ.SAMP_GRAY) {
37267bee8683db22050e346e0d002c5969da854d582DRC              if (row < halfway) {
373f962fbb44ad4d7b8200679ad0ed9263e10252638DRC                checkVal(row, col, r, "R", 76);
374f962fbb44ad4d7b8200679ad0ed9263e10252638DRC                checkVal(row, col, g, "G", 76);
375f962fbb44ad4d7b8200679ad0ed9263e10252638DRC                checkVal(row, col, b, "B", 76);
37667bee8683db22050e346e0d002c5969da854d582DRC              } else {
377f962fbb44ad4d7b8200679ad0ed9263e10252638DRC                checkVal(row, col, r, "R", 226);
378f962fbb44ad4d7b8200679ad0ed9263e10252638DRC                checkVal(row, col, g, "G", 226);
379f962fbb44ad4d7b8200679ad0ed9263e10252638DRC                checkVal(row, col, b, "B", 226);
380f962fbb44ad4d7b8200679ad0ed9263e10252638DRC              }
38167bee8683db22050e346e0d002c5969da854d582DRC            } else {
382f962fbb44ad4d7b8200679ad0ed9263e10252638DRC              checkVal255(row, col, r, "R");
38367bee8683db22050e346e0d002c5969da854d582DRC              if (row < halfway) {
384f962fbb44ad4d7b8200679ad0ed9263e10252638DRC                checkVal0(row, col, g, "G");
38567bee8683db22050e346e0d002c5969da854d582DRC              } else {
386f962fbb44ad4d7b8200679ad0ed9263e10252638DRC                checkVal255(row, col, g, "G");
387f962fbb44ad4d7b8200679ad0ed9263e10252638DRC              }
38867bee8683db22050e346e0d002c5969da854d582DRC              checkVal0(row, col, b, "B");
389f7f3ea404cc8618305efc059c34d881566206ed9DRC            }
390f7f3ea404cc8618305efc059c34d881566206ed9DRC          }
391c08e8c15bc73e7931ac5b87b992b17bbbda7f332DRC          checkVal255(row, col, a, "A");
392f7f3ea404cc8618305efc059c34d881566206ed9DRC        }
393f7f3ea404cc8618305efc059c34d881566206ed9DRC      }
39467bee8683db22050e346e0d002c5969da854d582DRC    } catch(Exception e) {
395b7c41932ed1ee9859a1aeea75e6e379e46a46158DRC      System.out.println("\n" + e.getMessage());
396f7f3ea404cc8618305efc059c34d881566206ed9DRC      retval = 0;
397f7f3ea404cc8618305efc059c34d881566206ed9DRC    }
398f7f3ea404cc8618305efc059c34d881566206ed9DRC
39967bee8683db22050e346e0d002c5969da854d582DRC    if (retval == 0) {
40067bee8683db22050e346e0d002c5969da854d582DRC      for (row = 0; row < h; row++) {
40167bee8683db22050e346e0d002c5969da854d582DRC        for (col = 0; col < w; col++) {
40238cb1ec2a77b7209a260f3cced1e6c6365616c8eDRC          if (pf == TJ.PF_CMYK) {
40338cb1ec2a77b7209a260f3cced1e6c6365616c8eDRC            int c = buf[pitch * row + col * ps];
40438cb1ec2a77b7209a260f3cced1e6c6365616c8eDRC            int m = buf[pitch * row + col * ps + 1];
40538cb1ec2a77b7209a260f3cced1e6c6365616c8eDRC            int y = buf[pitch * row + col * ps + 2];
40638cb1ec2a77b7209a260f3cced1e6c6365616c8eDRC            int k = buf[pitch * row + col * ps + 3];
40738cb1ec2a77b7209a260f3cced1e6c6365616c8eDRC            if (c < 0) c += 256;
40838cb1ec2a77b7209a260f3cced1e6c6365616c8eDRC            if (m < 0) m += 256;
40938cb1ec2a77b7209a260f3cced1e6c6365616c8eDRC            if (y < 0) y += 256;
41038cb1ec2a77b7209a260f3cced1e6c6365616c8eDRC            if (k < 0) k += 256;
41138cb1ec2a77b7209a260f3cced1e6c6365616c8eDRC            System.out.format("%3d/%3d/%3d/%3d ", c, m, y, k);
41238cb1ec2a77b7209a260f3cced1e6c6365616c8eDRC          } else {
41338cb1ec2a77b7209a260f3cced1e6c6365616c8eDRC            int r = buf[pitch * row + col * ps + roffset];
41438cb1ec2a77b7209a260f3cced1e6c6365616c8eDRC            int g = buf[pitch * row + col * ps + goffset];
41538cb1ec2a77b7209a260f3cced1e6c6365616c8eDRC            int b = buf[pitch * row + col * ps + boffset];
41638cb1ec2a77b7209a260f3cced1e6c6365616c8eDRC            if (r < 0) r += 256;
41738cb1ec2a77b7209a260f3cced1e6c6365616c8eDRC            if (g < 0) g += 256;
41838cb1ec2a77b7209a260f3cced1e6c6365616c8eDRC            if (b < 0) b += 256;
41938cb1ec2a77b7209a260f3cced1e6c6365616c8eDRC            System.out.format("%3d/%3d/%3d ", r, g, b);
42038cb1ec2a77b7209a260f3cced1e6c6365616c8eDRC          }
421f7f3ea404cc8618305efc059c34d881566206ed9DRC        }
422f7f3ea404cc8618305efc059c34d881566206ed9DRC        System.out.print("\n");
423f7f3ea404cc8618305efc059c34d881566206ed9DRC      }
424f7f3ea404cc8618305efc059c34d881566206ed9DRC    }
425f7f3ea404cc8618305efc059c34d881566206ed9DRC    return retval;
426f7f3ea404cc8618305efc059c34d881566206ed9DRC  }
427f7f3ea404cc8618305efc059c34d881566206ed9DRC
428f7f3ea404cc8618305efc059c34d881566206ed9DRC  private static int checkIntBuf(int[] buf, int w, int pitch, int h, int pf,
42967bee8683db22050e346e0d002c5969da854d582DRC                                 int subsamp, TJScalingFactor sf, int flags)
43067bee8683db22050e346e0d002c5969da854d582DRC                                 throws Exception {
4312c74e5124d25112809bdc26cbc36aa764e8870c3DRC    int rshift = TJ.getRedOffset(pf) * 8;
4322c74e5124d25112809bdc26cbc36aa764e8870c3DRC    int gshift = TJ.getGreenOffset(pf) * 8;
4332c74e5124d25112809bdc26cbc36aa764e8870c3DRC    int bshift = TJ.getBlueOffset(pf) * 8;
434c08e8c15bc73e7931ac5b87b992b17bbbda7f332DRC    int ashift = alphaOffset[pf] * 8;
435f962fbb44ad4d7b8200679ad0ed9263e10252638DRC    int index, row, col, retval = 1;
436b2f9415a6365b8eb412a5b6159be3cc0f875325fDRC    int halfway = 16 * sf.getNum() / sf.getDenom();
437b2f9415a6365b8eb412a5b6159be3cc0f875325fDRC    int blockSize = 8 * sf.getNum() / sf.getDenom();
438f7f3ea404cc8618305efc059c34d881566206ed9DRC
439f7f3ea404cc8618305efc059c34d881566206ed9DRC    try {
44067bee8683db22050e346e0d002c5969da854d582DRC      for (row = 0; row < halfway; row++) {
44167bee8683db22050e346e0d002c5969da854d582DRC        for (col = 0; col < w; col++) {
44267bee8683db22050e346e0d002c5969da854d582DRC          if ((flags & TJ.FLAG_BOTTOMUP) != 0)
443f962fbb44ad4d7b8200679ad0ed9263e10252638DRC            index = pitch * (h - row - 1) + col;
44467bee8683db22050e346e0d002c5969da854d582DRC          else
44567bee8683db22050e346e0d002c5969da854d582DRC            index = pitch * row + col;
446f962fbb44ad4d7b8200679ad0ed9263e10252638DRC          int r = (buf[index] >> rshift) & 0xFF;
447f962fbb44ad4d7b8200679ad0ed9263e10252638DRC          int g = (buf[index] >> gshift) & 0xFF;
448f962fbb44ad4d7b8200679ad0ed9263e10252638DRC          int b = (buf[index] >> bshift) & 0xFF;
449c08e8c15bc73e7931ac5b87b992b17bbbda7f332DRC          int a = ashift >= 0 ? (buf[index] >> ashift) & 0xFF : 255;
45067bee8683db22050e346e0d002c5969da854d582DRC          if (((row / blockSize) + (col / blockSize)) % 2 == 0) {
45167bee8683db22050e346e0d002c5969da854d582DRC            if (row < halfway) {
452f962fbb44ad4d7b8200679ad0ed9263e10252638DRC              checkVal255(row, col, r, "R");
453f962fbb44ad4d7b8200679ad0ed9263e10252638DRC              checkVal255(row, col, g, "G");
454f962fbb44ad4d7b8200679ad0ed9263e10252638DRC              checkVal255(row, col, b, "B");
45567bee8683db22050e346e0d002c5969da854d582DRC            } else {
456f962fbb44ad4d7b8200679ad0ed9263e10252638DRC              checkVal0(row, col, r, "R");
457f962fbb44ad4d7b8200679ad0ed9263e10252638DRC              checkVal0(row, col, g, "G");
458f962fbb44ad4d7b8200679ad0ed9263e10252638DRC              checkVal0(row, col, b, "B");
459f7f3ea404cc8618305efc059c34d881566206ed9DRC            }
46067bee8683db22050e346e0d002c5969da854d582DRC          } else {
46167bee8683db22050e346e0d002c5969da854d582DRC            if (subsamp == TJ.SAMP_GRAY) {
46267bee8683db22050e346e0d002c5969da854d582DRC              if (row < halfway) {
463f962fbb44ad4d7b8200679ad0ed9263e10252638DRC                checkVal(row, col, r, "R", 76);
464f962fbb44ad4d7b8200679ad0ed9263e10252638DRC                checkVal(row, col, g, "G", 76);
465f962fbb44ad4d7b8200679ad0ed9263e10252638DRC                checkVal(row, col, b, "B", 76);
46667bee8683db22050e346e0d002c5969da854d582DRC              } else {
467f962fbb44ad4d7b8200679ad0ed9263e10252638DRC                checkVal(row, col, r, "R", 226);
468f962fbb44ad4d7b8200679ad0ed9263e10252638DRC                checkVal(row, col, g, "G", 226);
469f962fbb44ad4d7b8200679ad0ed9263e10252638DRC                checkVal(row, col, b, "B", 226);
470f962fbb44ad4d7b8200679ad0ed9263e10252638DRC              }
47167bee8683db22050e346e0d002c5969da854d582DRC            } else {
472f962fbb44ad4d7b8200679ad0ed9263e10252638DRC              checkVal255(row, col, r, "R");
47367bee8683db22050e346e0d002c5969da854d582DRC              if (row < halfway) {
474f962fbb44ad4d7b8200679ad0ed9263e10252638DRC                checkVal0(row, col, g, "G");
47567bee8683db22050e346e0d002c5969da854d582DRC              } else {
476f962fbb44ad4d7b8200679ad0ed9263e10252638DRC                checkVal255(row, col, g, "G");
477f962fbb44ad4d7b8200679ad0ed9263e10252638DRC              }
478f962fbb44ad4d7b8200679ad0ed9263e10252638DRC              checkVal0(row, col, b, "B");
479f7f3ea404cc8618305efc059c34d881566206ed9DRC            }
480f7f3ea404cc8618305efc059c34d881566206ed9DRC          }
481c08e8c15bc73e7931ac5b87b992b17bbbda7f332DRC          checkVal255(row, col, a, "A");
482f7f3ea404cc8618305efc059c34d881566206ed9DRC        }
483f7f3ea404cc8618305efc059c34d881566206ed9DRC      }
48467bee8683db22050e346e0d002c5969da854d582DRC    } catch(Exception e) {
485b7c41932ed1ee9859a1aeea75e6e379e46a46158DRC      System.out.println("\n" + e.getMessage());
486f7f3ea404cc8618305efc059c34d881566206ed9DRC      retval = 0;
487f7f3ea404cc8618305efc059c34d881566206ed9DRC    }
488f7f3ea404cc8618305efc059c34d881566206ed9DRC
48967bee8683db22050e346e0d002c5969da854d582DRC    if (retval == 0) {
49067bee8683db22050e346e0d002c5969da854d582DRC      for (row = 0; row < h; row++) {
49167bee8683db22050e346e0d002c5969da854d582DRC        for (col = 0; col < w; col++) {
492f962fbb44ad4d7b8200679ad0ed9263e10252638DRC          int r = (buf[pitch * row + col] >> rshift) & 0xFF;
493f962fbb44ad4d7b8200679ad0ed9263e10252638DRC          int g = (buf[pitch * row + col] >> gshift) & 0xFF;
494f962fbb44ad4d7b8200679ad0ed9263e10252638DRC          int b = (buf[pitch * row + col] >> bshift) & 0xFF;
49567bee8683db22050e346e0d002c5969da854d582DRC          if (r < 0) r += 256;
49667bee8683db22050e346e0d002c5969da854d582DRC          if (g < 0) g += 256;
49767bee8683db22050e346e0d002c5969da854d582DRC          if (b < 0) b += 256;
498f7f3ea404cc8618305efc059c34d881566206ed9DRC          System.out.format("%3d/%3d/%3d ", r, g, b);
499f7f3ea404cc8618305efc059c34d881566206ed9DRC        }
500f7f3ea404cc8618305efc059c34d881566206ed9DRC        System.out.print("\n");
501f7f3ea404cc8618305efc059c34d881566206ed9DRC      }
502f7f3ea404cc8618305efc059c34d881566206ed9DRC    }
503f7f3ea404cc8618305efc059c34d881566206ed9DRC    return retval;
504f7f3ea404cc8618305efc059c34d881566206ed9DRC  }
505f7f3ea404cc8618305efc059c34d881566206ed9DRC
50667bee8683db22050e346e0d002c5969da854d582DRC  private static int checkImg(BufferedImage img, int pf, int subsamp,
50767bee8683db22050e346e0d002c5969da854d582DRC                              TJScalingFactor sf, int flags) throws Exception {
508f7f3ea404cc8618305efc059c34d881566206ed9DRC    WritableRaster wr = img.getRaster();
509c08e8c15bc73e7931ac5b87b992b17bbbda7f332DRC    int imgType = img.getType();
51067bee8683db22050e346e0d002c5969da854d582DRC    if (imgType == BufferedImage.TYPE_INT_RGB ||
51167bee8683db22050e346e0d002c5969da854d582DRC        imgType == BufferedImage.TYPE_INT_BGR ||
51267bee8683db22050e346e0d002c5969da854d582DRC        imgType == BufferedImage.TYPE_INT_ARGB ||
51367bee8683db22050e346e0d002c5969da854d582DRC        imgType == BufferedImage.TYPE_INT_ARGB_PRE) {
51467bee8683db22050e346e0d002c5969da854d582DRC      SinglePixelPackedSampleModel sm =
515f7f3ea404cc8618305efc059c34d881566206ed9DRC        (SinglePixelPackedSampleModel)img.getSampleModel();
516f7f3ea404cc8618305efc059c34d881566206ed9DRC      int pitch = sm.getScanlineStride();
517f7f3ea404cc8618305efc059c34d881566206ed9DRC      DataBufferInt db = (DataBufferInt)wr.getDataBuffer();
518f7f3ea404cc8618305efc059c34d881566206ed9DRC      int[] buf = db.getData();
519f7f3ea404cc8618305efc059c34d881566206ed9DRC      return checkIntBuf(buf, img.getWidth(), pitch, img.getHeight(), pf,
52067bee8683db22050e346e0d002c5969da854d582DRC                         subsamp, sf, flags);
52167bee8683db22050e346e0d002c5969da854d582DRC    } else {
522f7f3ea404cc8618305efc059c34d881566206ed9DRC      ComponentSampleModel sm = (ComponentSampleModel)img.getSampleModel();
523f7f3ea404cc8618305efc059c34d881566206ed9DRC      int pitch = sm.getScanlineStride();
524f7f3ea404cc8618305efc059c34d881566206ed9DRC      DataBufferByte db = (DataBufferByte)wr.getDataBuffer();
525f7f3ea404cc8618305efc059c34d881566206ed9DRC      byte[] buf = db.getData();
526f7f3ea404cc8618305efc059c34d881566206ed9DRC      return checkBuf(buf, img.getWidth(), pitch, img.getHeight(), pf, subsamp,
52767bee8683db22050e346e0d002c5969da854d582DRC                      sf, flags);
528f7f3ea404cc8618305efc059c34d881566206ed9DRC    }
529f7f3ea404cc8618305efc059c34d881566206ed9DRC  }
530f7f3ea404cc8618305efc059c34d881566206ed9DRC
531f7f3ea404cc8618305efc059c34d881566206ed9DRC  private static int PAD(int v, int p) {
532f7f3ea404cc8618305efc059c34d881566206ed9DRC    return ((v + (p) - 1) & (~((p) - 1)));
533f7f3ea404cc8618305efc059c34d881566206ed9DRC  }
534f7f3ea404cc8618305efc059c34d881566206ed9DRC
535f7f3ea404cc8618305efc059c34d881566206ed9DRC  private static int checkBufYUV(byte[] buf, int size, int w, int h,
536fef9852da3a80bfaf84862462609f97d77ad6db7DRC                                 int subsamp, TJScalingFactor sf)
537fef9852da3a80bfaf84862462609f97d77ad6db7DRC                                 throws Exception {
538215aa8b78bccea6ef301c5fb256722b4099e7b90DRC    int row, col;
53967bee8683db22050e346e0d002c5969da854d582DRC    int hsf = TJ.getMCUWidth(subsamp) / 8, vsf = TJ.getMCUHeight(subsamp) / 8;
540f7f3ea404cc8618305efc059c34d881566206ed9DRC    int pw = PAD(w, hsf), ph = PAD(h, vsf);
541f7f3ea404cc8618305efc059c34d881566206ed9DRC    int cw = pw / hsf, ch = ph / vsf;
542fef9852da3a80bfaf84862462609f97d77ad6db7DRC    int ypitch = PAD(pw, pad), uvpitch = PAD(cw, pad);
543f7f3ea404cc8618305efc059c34d881566206ed9DRC    int retval = 1;
54467bee8683db22050e346e0d002c5969da854d582DRC    int correctsize = ypitch * ph +
54567bee8683db22050e346e0d002c5969da854d582DRC                      (subsamp == TJ.SAMP_GRAY ? 0 : uvpitch * ch * 2);
546fef9852da3a80bfaf84862462609f97d77ad6db7DRC    int halfway = 16 * sf.getNum() / sf.getDenom();
547fef9852da3a80bfaf84862462609f97d77ad6db7DRC    int blockSize = 8 * sf.getNum() / sf.getDenom();
548f7f3ea404cc8618305efc059c34d881566206ed9DRC
549f7f3ea404cc8618305efc059c34d881566206ed9DRC    try {
55067bee8683db22050e346e0d002c5969da854d582DRC      if (size != correctsize)
551b7c41932ed1ee9859a1aeea75e6e379e46a46158DRC        throw new Exception("Incorrect size " + size + ".  Should be " +
55267bee8683db22050e346e0d002c5969da854d582DRC                            correctsize);
553f7f3ea404cc8618305efc059c34d881566206ed9DRC
55467bee8683db22050e346e0d002c5969da854d582DRC      for (row = 0; row < ph; row++) {
55567bee8683db22050e346e0d002c5969da854d582DRC        for (col = 0; col < pw; col++) {
556215aa8b78bccea6ef301c5fb256722b4099e7b90DRC          byte y = buf[ypitch * row + col];
557fef9852da3a80bfaf84862462609f97d77ad6db7DRC          if (((row / blockSize) + (col / blockSize)) % 2 == 0) {
55867bee8683db22050e346e0d002c5969da854d582DRC            if (row < halfway)
55967bee8683db22050e346e0d002c5969da854d582DRC              checkVal255(row, col, y, "Y");
56067bee8683db22050e346e0d002c5969da854d582DRC            else
56167bee8683db22050e346e0d002c5969da854d582DRC              checkVal0(row, col, y, "Y");
56267bee8683db22050e346e0d002c5969da854d582DRC          } else {
56367bee8683db22050e346e0d002c5969da854d582DRC            if (row < halfway)
56467bee8683db22050e346e0d002c5969da854d582DRC              checkVal(row, col, y, "Y", 76);
56567bee8683db22050e346e0d002c5969da854d582DRC            else
56667bee8683db22050e346e0d002c5969da854d582DRC              checkVal(row, col, y, "Y", 226);
567215aa8b78bccea6ef301c5fb256722b4099e7b90DRC          }
568f7f3ea404cc8618305efc059c34d881566206ed9DRC        }
569f7f3ea404cc8618305efc059c34d881566206ed9DRC      }
57067bee8683db22050e346e0d002c5969da854d582DRC      if (subsamp != TJ.SAMP_GRAY) {
571fef9852da3a80bfaf84862462609f97d77ad6db7DRC        halfway = 16 / vsf * sf.getNum() / sf.getDenom();
57267bee8683db22050e346e0d002c5969da854d582DRC        for (row = 0; row < ch; row++) {
57367bee8683db22050e346e0d002c5969da854d582DRC          for (col = 0; col < cw; col++) {
574215aa8b78bccea6ef301c5fb256722b4099e7b90DRC            byte u = buf[ypitch * ph + (uvpitch * row + col)],
57567bee8683db22050e346e0d002c5969da854d582DRC                 v = buf[ypitch * ph + uvpitch * ch + (uvpitch * row + col)];
576fef9852da3a80bfaf84862462609f97d77ad6db7DRC            if (((row * vsf / blockSize) + (col * hsf / blockSize)) % 2 == 0) {
577215aa8b78bccea6ef301c5fb256722b4099e7b90DRC              checkVal(row, col, u, "U", 128);
578215aa8b78bccea6ef301c5fb256722b4099e7b90DRC              checkVal(row, col, v, "V", 128);
57967bee8683db22050e346e0d002c5969da854d582DRC            } else {
58067bee8683db22050e346e0d002c5969da854d582DRC              if (row < halfway) {
581215aa8b78bccea6ef301c5fb256722b4099e7b90DRC                checkVal(row, col, u, "U", 85);
582215aa8b78bccea6ef301c5fb256722b4099e7b90DRC                checkVal255(row, col, v, "V");
58367bee8683db22050e346e0d002c5969da854d582DRC              } else {
584215aa8b78bccea6ef301c5fb256722b4099e7b90DRC                checkVal0(row, col, u, "U");
585215aa8b78bccea6ef301c5fb256722b4099e7b90DRC                checkVal(row, col, v, "V", 149);
586215aa8b78bccea6ef301c5fb256722b4099e7b90DRC              }
587f7f3ea404cc8618305efc059c34d881566206ed9DRC            }
588f7f3ea404cc8618305efc059c34d881566206ed9DRC          }
589f7f3ea404cc8618305efc059c34d881566206ed9DRC        }
590f7f3ea404cc8618305efc059c34d881566206ed9DRC      }
59167bee8683db22050e346e0d002c5969da854d582DRC    } catch(Exception e) {
592b7c41932ed1ee9859a1aeea75e6e379e46a46158DRC      System.out.println("\n" + e.getMessage());
593f7f3ea404cc8618305efc059c34d881566206ed9DRC      retval = 0;
594f7f3ea404cc8618305efc059c34d881566206ed9DRC    }
595f7f3ea404cc8618305efc059c34d881566206ed9DRC
59667bee8683db22050e346e0d002c5969da854d582DRC    if (retval == 0) {
59767bee8683db22050e346e0d002c5969da854d582DRC      for (row = 0; row < ph; row++) {
59867bee8683db22050e346e0d002c5969da854d582DRC        for (col = 0; col < pw; col++) {
599215aa8b78bccea6ef301c5fb256722b4099e7b90DRC          int y = buf[ypitch * row + col];
60067bee8683db22050e346e0d002c5969da854d582DRC          if (y < 0) y += 256;
601f7f3ea404cc8618305efc059c34d881566206ed9DRC          System.out.format("%3d ", y);
602f7f3ea404cc8618305efc059c34d881566206ed9DRC        }
603f7f3ea404cc8618305efc059c34d881566206ed9DRC        System.out.print("\n");
604f7f3ea404cc8618305efc059c34d881566206ed9DRC      }
605f7f3ea404cc8618305efc059c34d881566206ed9DRC      System.out.print("\n");
60667bee8683db22050e346e0d002c5969da854d582DRC      for (row = 0; row < ch; row++) {
60767bee8683db22050e346e0d002c5969da854d582DRC        for (col = 0; col < cw; col++) {
608215aa8b78bccea6ef301c5fb256722b4099e7b90DRC          int u = buf[ypitch * ph + (uvpitch * row + col)];
60967bee8683db22050e346e0d002c5969da854d582DRC          if (u < 0) u += 256;
610f7f3ea404cc8618305efc059c34d881566206ed9DRC          System.out.format("%3d ", u);
611f7f3ea404cc8618305efc059c34d881566206ed9DRC        }
612f7f3ea404cc8618305efc059c34d881566206ed9DRC        System.out.print("\n");
613f7f3ea404cc8618305efc059c34d881566206ed9DRC      }
614f7f3ea404cc8618305efc059c34d881566206ed9DRC      System.out.print("\n");
61567bee8683db22050e346e0d002c5969da854d582DRC      for (row = 0; row < ch; row++) {
61667bee8683db22050e346e0d002c5969da854d582DRC        for (col = 0; col < cw; col++) {
617215aa8b78bccea6ef301c5fb256722b4099e7b90DRC          int v = buf[ypitch * ph + uvpitch * ch + (uvpitch * row + col)];
61867bee8683db22050e346e0d002c5969da854d582DRC          if (v < 0) v += 256;
619f7f3ea404cc8618305efc059c34d881566206ed9DRC          System.out.format("%3d ", v);
620f7f3ea404cc8618305efc059c34d881566206ed9DRC        }
621f7f3ea404cc8618305efc059c34d881566206ed9DRC        System.out.print("\n");
622f7f3ea404cc8618305efc059c34d881566206ed9DRC      }
623f7f3ea404cc8618305efc059c34d881566206ed9DRC    }
624f7f3ea404cc8618305efc059c34d881566206ed9DRC
625f7f3ea404cc8618305efc059c34d881566206ed9DRC    return retval;
626f7f3ea404cc8618305efc059c34d881566206ed9DRC  }
627f7f3ea404cc8618305efc059c34d881566206ed9DRC
628f7f3ea404cc8618305efc059c34d881566206ed9DRC  private static void writeJPEG(byte[] jpegBuf, int jpegBufSize,
62967bee8683db22050e346e0d002c5969da854d582DRC                                String filename) throws Exception {
630f7f3ea404cc8618305efc059c34d881566206ed9DRC    File file = new File(filename);
631f7f3ea404cc8618305efc059c34d881566206ed9DRC    FileOutputStream fos = new FileOutputStream(file);
632f7f3ea404cc8618305efc059c34d881566206ed9DRC    fos.write(jpegBuf, 0, jpegBufSize);
633f7f3ea404cc8618305efc059c34d881566206ed9DRC    fos.close();
634f7f3ea404cc8618305efc059c34d881566206ed9DRC  }
635f7f3ea404cc8618305efc059c34d881566206ed9DRC
636f962fbb44ad4d7b8200679ad0ed9263e10252638DRC  private static int compTest(TJCompressor tjc, byte[] dstBuf, int w,
63767bee8683db22050e346e0d002c5969da854d582DRC                              int h, int pf, String baseName, int subsamp,
63867bee8683db22050e346e0d002c5969da854d582DRC                              int jpegQual, int flags) throws Exception {
639fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC    String tempStr;
640f962fbb44ad4d7b8200679ad0ed9263e10252638DRC    byte[] srcBuf = null;
641f7f3ea404cc8618305efc059c34d881566206ed9DRC    BufferedImage img = null;
642fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC    String pfStr, pfStrLong;
643fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC    String buStr = (flags & TJ.FLAG_BOTTOMUP) != 0 ? "BU" : "TD";
6441e67274bd7f77f6ace653db7b356ab3f24453c88DRC    String buStrLong = (flags & TJ.FLAG_BOTTOMUP) != 0 ?
6451e67274bd7f77f6ace653db7b356ab3f24453c88DRC                       "Bottom-Up" : "Top-Down ";
646c08e8c15bc73e7931ac5b87b992b17bbbda7f332DRC    int size = 0, ps, imgType = pf;
647f7f3ea404cc8618305efc059c34d881566206ed9DRC
648fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC    if (bi) {
649fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC      pf = biTypePF(imgType);
650fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC      pfStr = biTypeStr(imgType);
651fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC      pfStrLong = pfStr + " (" + pixFormatStr[pf] + ")";
65267bee8683db22050e346e0d002c5969da854d582DRC    } else {
653fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC      pfStr = pixFormatStr[pf];
654fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC      pfStrLong = pfStr;
655fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC    }
656fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC    ps =  TJ.getPixelSize(pf);
6571e67274bd7f77f6ace653db7b356ab3f24453c88DRC
658fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC    if (bi) {
659fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC      img = new BufferedImage(w, h, imgType);
660fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC      initImg(img, pf, flags);
661fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC      tempStr = baseName + "_enc_" + pfStr + "_" + buStr + "_" +
662fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC                subName[subsamp] + "_Q" + jpegQual + ".png";
663fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC      File file = new File(tempStr);
664fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC      ImageIO.write(img, "png", file);
665fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC      tjc.setSourceImage(img, 0, 0, 0, 0);
666fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC    } else {
667fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC      srcBuf = new byte[w * h * ps + 1];
668fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC      initBuf(srcBuf, w, w * ps, h, pf, flags);
669fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC      tjc.setSourceImage(srcBuf, 0, 0, w, 0, h, pf);
670f7f3ea404cc8618305efc059c34d881566206ed9DRC    }
671f962fbb44ad4d7b8200679ad0ed9263e10252638DRC    Arrays.fill(dstBuf, (byte)0);
672f7f3ea404cc8618305efc059c34d881566206ed9DRC
673f7f3ea404cc8618305efc059c34d881566206ed9DRC    tjc.setSubsamp(subsamp);
674f962fbb44ad4d7b8200679ad0ed9263e10252638DRC    tjc.setJPEGQuality(jpegQual);
675fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC    if (doYUV) {
6761a45b81fa2edf682ae72251a18fd84eacef9dbbeDRC      System.out.format("%s %s -> YUV %s ... ", pfStrLong, buStrLong,
677fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC                        subNameLong[subsamp]);
678fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC      YUVImage yuvImage = tjc.encodeYUV(pad, flags);
679fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC      if (checkBufYUV(yuvImage.getBuf(), yuvImage.getSize(), w, h, subsamp,
680fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC          new TJScalingFactor(1, 1)) == 1)
681fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC        System.out.print("Passed.\n");
682f7f3ea404cc8618305efc059c34d881566206ed9DRC      else {
683fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC        System.out.print("FAILED!\n");
68467bee8683db22050e346e0d002c5969da854d582DRC        exitStatus = -1;
685f7f3ea404cc8618305efc059c34d881566206ed9DRC      }
686fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC
687fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC      System.out.format("YUV %s %s -> JPEG Q%d ... ", subNameLong[subsamp],
688fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC                        buStrLong, jpegQual);
689fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC      tjc.setSourceImage(yuvImage);
690fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC    } else {
691fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC      System.out.format("%s %s -> %s Q%d ... ", pfStrLong, buStrLong,
692fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC                        subNameLong[subsamp], jpegQual);
693fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC    }
694fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC    tjc.compress(dstBuf, flags);
695fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC    size = tjc.getCompressedSize();
696fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC
697fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC    tempStr = baseName + "_enc_" + pfStr + "_" + buStr + "_" +
698fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC              subName[subsamp] + "_Q" + jpegQual + ".jpg";
699fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC    writeJPEG(dstBuf, size, tempStr);
700fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC    System.out.println("Done.\n  Result in " + tempStr);
701f7f3ea404cc8618305efc059c34d881566206ed9DRC
702f7f3ea404cc8618305efc059c34d881566206ed9DRC    return size;
703f7f3ea404cc8618305efc059c34d881566206ed9DRC  }
704f7f3ea404cc8618305efc059c34d881566206ed9DRC
705f962fbb44ad4d7b8200679ad0ed9263e10252638DRC  private static void decompTest(TJDecompressor tjd, byte[] jpegBuf,
70667bee8683db22050e346e0d002c5969da854d582DRC                                 int jpegSize, int w, int h, int pf,
70767bee8683db22050e346e0d002c5969da854d582DRC                                 String baseName, int subsamp, int flags,
70867bee8683db22050e346e0d002c5969da854d582DRC                                 TJScalingFactor sf) throws Exception {
709fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC    String pfStr, pfStrLong, tempStr;
710fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC    String buStrLong = (flags & TJ.FLAG_BOTTOMUP) != 0 ?
711fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC                       "Bottom-Up" : "Top-Down ";
712b2f9415a6365b8eb412a5b6159be3cc0f875325fDRC    int scaledWidth = sf.getScaled(w);
713b2f9415a6365b8eb412a5b6159be3cc0f875325fDRC    int scaledHeight = sf.getScaled(h);
714c08e8c15bc73e7931ac5b87b992b17bbbda7f332DRC    int temp1, temp2, imgType = pf;
715f7f3ea404cc8618305efc059c34d881566206ed9DRC    BufferedImage img = null;
716f962fbb44ad4d7b8200679ad0ed9263e10252638DRC    byte[] dstBuf = null;
717f7f3ea404cc8618305efc059c34d881566206ed9DRC
718c08e8c15bc73e7931ac5b87b992b17bbbda7f332DRC    if (bi) {
719c08e8c15bc73e7931ac5b87b992b17bbbda7f332DRC      pf = biTypePF(imgType);
720c08e8c15bc73e7931ac5b87b992b17bbbda7f332DRC      pfStr = biTypeStr(imgType);
721fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC      pfStrLong = pfStr + " (" + pixFormatStr[pf] + ")";
722fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC    } else {
72367bee8683db22050e346e0d002c5969da854d582DRC      pfStr = pixFormatStr[pf];
724fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC      pfStrLong = pfStr;
725f7f3ea404cc8618305efc059c34d881566206ed9DRC    }
726f7f3ea404cc8618305efc059c34d881566206ed9DRC
727fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC    tjd.setSourceImage(jpegBuf, jpegSize);
72867bee8683db22050e346e0d002c5969da854d582DRC    if (tjd.getWidth() != w || tjd.getHeight() != h ||
72967bee8683db22050e346e0d002c5969da854d582DRC        tjd.getSubsamp() != subsamp)
730f7f3ea404cc8618305efc059c34d881566206ed9DRC      throw new Exception("Incorrect JPEG header");
731f7f3ea404cc8618305efc059c34d881566206ed9DRC
732f7f3ea404cc8618305efc059c34d881566206ed9DRC    temp1 = scaledWidth;
733f7f3ea404cc8618305efc059c34d881566206ed9DRC    temp2 = scaledHeight;
734f7f3ea404cc8618305efc059c34d881566206ed9DRC    temp1 = tjd.getScaledWidth(temp1, temp2);
735f7f3ea404cc8618305efc059c34d881566206ed9DRC    temp2 = tjd.getScaledHeight(temp1, temp2);
73667bee8683db22050e346e0d002c5969da854d582DRC    if (temp1 != scaledWidth || temp2 != scaledHeight)
737f7f3ea404cc8618305efc059c34d881566206ed9DRC      throw new Exception("Scaled size mismatch");
738f7f3ea404cc8618305efc059c34d881566206ed9DRC
739fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC    if (doYUV) {
740fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC      System.out.format("JPEG -> YUV %s ", subNameLong[subsamp]);
741fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC      if(!sf.isOne())
742fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC        System.out.format("%d/%d ... ", sf.getNum(), sf.getDenom());
743fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC      else System.out.print("... ");
744fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC      YUVImage yuvImage = tjd.decompressToYUV(scaledWidth, pad, scaledHeight,
745fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC                                              flags);
746fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC      if (checkBufYUV(yuvImage.getBuf(), yuvImage.getSize(), scaledWidth,
747fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC                      scaledHeight, subsamp, sf) == 1)
748fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC        System.out.print("Passed.\n");
749fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC      else {
750fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC        System.out.print("FAILED!\n");  exitStatus = -1;
751fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC      }
752fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC
753fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC      System.out.format("YUV %s -> %s %s ... ", subNameLong[subsamp],
754fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC                        pfStrLong, buStrLong);
755fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC      tjd.setSourceImage(yuvImage);
756fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC    } else {
757fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC      System.out.format("JPEG -> %s %s ", pfStrLong, buStrLong);
758fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC      if(!sf.isOne())
759fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC        System.out.format("%d/%d ... ", sf.getNum(), sf.getDenom());
760fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC      else System.out.print("... ");
761f7f3ea404cc8618305efc059c34d881566206ed9DRC    }
762fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC    if (bi)
763fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC      img = tjd.decompress(scaledWidth, scaledHeight, imgType, flags);
764fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC    else
765fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC      dstBuf = tjd.decompress(scaledWidth, 0, scaledHeight, pf, flags);
766f7f3ea404cc8618305efc059c34d881566206ed9DRC
76767bee8683db22050e346e0d002c5969da854d582DRC    if (bi) {
768fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC      tempStr = baseName + "_dec_" + pfStr + "_" +
76967bee8683db22050e346e0d002c5969da854d582DRC                (((flags & TJ.FLAG_BOTTOMUP) != 0) ? "BU" : "TD") + "_" +
77067bee8683db22050e346e0d002c5969da854d582DRC                subName[subsamp] + "_" +
77167bee8683db22050e346e0d002c5969da854d582DRC                (double)sf.getNum() / (double)sf.getDenom() + "x" + ".png";
772fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC      File file = new File(tempStr);
773f7f3ea404cc8618305efc059c34d881566206ed9DRC      ImageIO.write(img, "png", file);
774f7f3ea404cc8618305efc059c34d881566206ed9DRC    }
775f7f3ea404cc8618305efc059c34d881566206ed9DRC
776fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC    if ((bi && checkImg(img, pf, subsamp, sf, flags) == 1) ||
777fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC        (!bi && checkBuf(dstBuf, scaledWidth,
778fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC                         scaledWidth * TJ.getPixelSize(pf), scaledHeight, pf,
779fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC                         subsamp, sf, flags) == 1))
780fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC      System.out.print("Passed.\n");
781fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC    else {
782fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC      System.out.print("FAILED!\n");
783fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC      exitStatus = -1;
784f7f3ea404cc8618305efc059c34d881566206ed9DRC    }
785f7f3ea404cc8618305efc059c34d881566206ed9DRC  }
786f7f3ea404cc8618305efc059c34d881566206ed9DRC
787f962fbb44ad4d7b8200679ad0ed9263e10252638DRC  private static void decompTest(TJDecompressor tjd, byte[] jpegBuf,
78867bee8683db22050e346e0d002c5969da854d582DRC                                 int jpegSize, int w, int h, int pf,
78967bee8683db22050e346e0d002c5969da854d582DRC                                 String baseName, int subsamp,
79067bee8683db22050e346e0d002c5969da854d582DRC                                 int flags) throws Exception {
791f7f3ea404cc8618305efc059c34d881566206ed9DRC    int i;
792418fe286c2fe90dcd3338f1f1d2f221c0e0e2bf3DRC    TJScalingFactor[] sf = TJ.getScalingFactors();
793418fe286c2fe90dcd3338f1f1d2f221c0e0e2bf3DRC    for (i = 0; i < sf.length; i++) {
794418fe286c2fe90dcd3338f1f1d2f221c0e0e2bf3DRC      int num = sf[i].getNum();
795418fe286c2fe90dcd3338f1f1d2f221c0e0e2bf3DRC      int denom = sf[i].getDenom();
796418fe286c2fe90dcd3338f1f1d2f221c0e0e2bf3DRC      if (subsamp == TJ.SAMP_444 || subsamp == TJ.SAMP_GRAY ||
797a5830628b9cc92f9305ecb7d127ebc7207b12582DRC          (subsamp == TJ.SAMP_411 && num == 1 &&
798a5830628b9cc92f9305ecb7d127ebc7207b12582DRC           (denom == 2 || denom == 1)) ||
799a5830628b9cc92f9305ecb7d127ebc7207b12582DRC          (subsamp != TJ.SAMP_411 && num == 1 &&
800a5830628b9cc92f9305ecb7d127ebc7207b12582DRC           (denom == 4 || denom == 2 || denom == 1)))
801f962fbb44ad4d7b8200679ad0ed9263e10252638DRC        decompTest(tjd, jpegBuf, jpegSize, w, h, pf, baseName, subsamp,
80267bee8683db22050e346e0d002c5969da854d582DRC                   flags, sf[i]);
803418fe286c2fe90dcd3338f1f1d2f221c0e0e2bf3DRC    }
804f7f3ea404cc8618305efc059c34d881566206ed9DRC  }
805f7f3ea404cc8618305efc059c34d881566206ed9DRC
806f7f3ea404cc8618305efc059c34d881566206ed9DRC  private static void doTest(int w, int h, int[] formats, int subsamp,
80767bee8683db22050e346e0d002c5969da854d582DRC                             String baseName) throws Exception {
808f7f3ea404cc8618305efc059c34d881566206ed9DRC    TJCompressor tjc = null;
809f7f3ea404cc8618305efc059c34d881566206ed9DRC    TJDecompressor tjd = null;
8104f8c29572e297e58cf1534bf596ff49a51a59575DRC    int size;
811f962fbb44ad4d7b8200679ad0ed9263e10252638DRC    byte[] dstBuf;
812f7f3ea404cc8618305efc059c34d881566206ed9DRC
813fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC    dstBuf = new byte[TJ.bufSize(w, h, subsamp)];
814f7f3ea404cc8618305efc059c34d881566206ed9DRC
815f7f3ea404cc8618305efc059c34d881566206ed9DRC    try {
816f7f3ea404cc8618305efc059c34d881566206ed9DRC      tjc = new TJCompressor();
81767bee8683db22050e346e0d002c5969da854d582DRC      tjd = new TJDecompressor();
818f7f3ea404cc8618305efc059c34d881566206ed9DRC
81967bee8683db22050e346e0d002c5969da854d582DRC      for (int pf : formats) {
82038cb1ec2a77b7209a260f3cced1e6c6365616c8eDRC        if (pf < 0) continue;
82167bee8683db22050e346e0d002c5969da854d582DRC        for (int i = 0; i < 2; i++) {
822f7f3ea404cc8618305efc059c34d881566206ed9DRC          int flags = 0;
82367bee8683db22050e346e0d002c5969da854d582DRC          if (subsamp == TJ.SAMP_422 || subsamp == TJ.SAMP_420 ||
824a5830628b9cc92f9305ecb7d127ebc7207b12582DRC              subsamp == TJ.SAMP_440 || subsamp == TJ.SAMP_411)
825cac105133e75a52fa5d57a2abccf242bb7b820d0DRC            flags |= TJ.FLAG_FASTUPSAMPLE;
826fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC          if (i == 1)
827fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC            flags |= TJ.FLAG_BOTTOMUP;
828f962fbb44ad4d7b8200679ad0ed9263e10252638DRC          size = compTest(tjc, dstBuf, w, h, pf, baseName, subsamp, 100,
82967bee8683db22050e346e0d002c5969da854d582DRC                          flags);
830f962fbb44ad4d7b8200679ad0ed9263e10252638DRC          decompTest(tjd, dstBuf, size, w, h, pf, baseName, subsamp, flags);
831fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC          if (pf >= TJ.PF_RGBX && pf <= TJ.PF_XRGB && !bi) {
832fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC            System.out.print("\n");
83367ce3b2352fe1f7511edbfed74ec6960e41e97dcDRC            decompTest(tjd, dstBuf, size, w, h, pf + (TJ.PF_RGBA - TJ.PF_RGBX),
83467bee8683db22050e346e0d002c5969da854d582DRC                       baseName, subsamp, flags);
835fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC          }
836b7c41932ed1ee9859a1aeea75e6e379e46a46158DRC          System.out.print("\n");
837f7f3ea404cc8618305efc059c34d881566206ed9DRC        }
838f7f3ea404cc8618305efc059c34d881566206ed9DRC      }
839b7c41932ed1ee9859a1aeea75e6e379e46a46158DRC      System.out.print("--------------------\n\n");
84067bee8683db22050e346e0d002c5969da854d582DRC    } catch(Exception e) {
84167bee8683db22050e346e0d002c5969da854d582DRC      if (tjc != null) tjc.close();
84267bee8683db22050e346e0d002c5969da854d582DRC      if (tjd != null) tjd.close();
843f7f3ea404cc8618305efc059c34d881566206ed9DRC      throw e;
844f7f3ea404cc8618305efc059c34d881566206ed9DRC    }
84567bee8683db22050e346e0d002c5969da854d582DRC    if (tjc != null) tjc.close();
84667bee8683db22050e346e0d002c5969da854d582DRC    if (tjd != null) tjd.close();
847f7f3ea404cc8618305efc059c34d881566206ed9DRC  }
848f7f3ea404cc8618305efc059c34d881566206ed9DRC
849724c56b46af1ad81212d044689960f433046508bDRC  private static void bufSizeTest() throws Exception {
850724c56b46af1ad81212d044689960f433046508bDRC    int w, h, i, subsamp;
851fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC    byte[] srcBuf, dstBuf = null;
852fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC    YUVImage dstImage = null;
853f7f3ea404cc8618305efc059c34d881566206ed9DRC    TJCompressor tjc = null;
854724c56b46af1ad81212d044689960f433046508bDRC    Random r = new Random();
855f7f3ea404cc8618305efc059c34d881566206ed9DRC
856f7f3ea404cc8618305efc059c34d881566206ed9DRC    try {
857f7f3ea404cc8618305efc059c34d881566206ed9DRC      tjc = new TJCompressor();
858f7f3ea404cc8618305efc059c34d881566206ed9DRC      System.out.println("Buffer size regression test");
85967bee8683db22050e346e0d002c5969da854d582DRC      for (subsamp = 0; subsamp < TJ.NUMSAMP; subsamp++) {
86067bee8683db22050e346e0d002c5969da854d582DRC        for (w = 1; w < 48; w++) {
861724c56b46af1ad81212d044689960f433046508bDRC          int maxh = (w == 1) ? 2048 : 48;
86267bee8683db22050e346e0d002c5969da854d582DRC          for (h = 1; h < maxh; h++) {
86367bee8683db22050e346e0d002c5969da854d582DRC            if (h % 100 == 0)
864724c56b46af1ad81212d044689960f433046508bDRC              System.out.format("%04d x %04d\b\b\b\b\b\b\b\b\b\b\b", w, h);
865724c56b46af1ad81212d044689960f433046508bDRC            srcBuf = new byte[w * h * 4];
866fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC            if (doYUV)
867fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC              dstImage = new YUVImage(w, pad, h, subsamp);
86838c9970b95f8f83769b5476a9d7e6062714c19f6DRC            else
86938c9970b95f8f83769b5476a9d7e6062714c19f6DRC              dstBuf = new byte[TJ.bufSize(w, h, subsamp)];
87067bee8683db22050e346e0d002c5969da854d582DRC            for (i = 0; i < w * h * 4; i++) {
871724c56b46af1ad81212d044689960f433046508bDRC              srcBuf[i] = (byte)(r.nextInt(2) * 255);
872724c56b46af1ad81212d044689960f433046508bDRC            }
873b14813947ee5abacb40fc20455c8d7af1adfd3a7DRC            tjc.setSourceImage(srcBuf, 0, 0, w, 0, h, TJ.PF_BGRX);
874724c56b46af1ad81212d044689960f433046508bDRC            tjc.setSubsamp(subsamp);
875724c56b46af1ad81212d044689960f433046508bDRC            tjc.setJPEGQuality(100);
876fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC            if (doYUV)
877fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC              tjc.encodeYUV(dstImage, 0);
87838c9970b95f8f83769b5476a9d7e6062714c19f6DRC            else
87938c9970b95f8f83769b5476a9d7e6062714c19f6DRC              tjc.compress(dstBuf, 0);
880724c56b46af1ad81212d044689960f433046508bDRC
881724c56b46af1ad81212d044689960f433046508bDRC            srcBuf = new byte[h * w * 4];
882fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC            if (doYUV)
883fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC              dstImage = new YUVImage(h, pad, w, subsamp);
88438c9970b95f8f83769b5476a9d7e6062714c19f6DRC            else
88538c9970b95f8f83769b5476a9d7e6062714c19f6DRC              dstBuf = new byte[TJ.bufSize(h, w, subsamp)];
88667bee8683db22050e346e0d002c5969da854d582DRC            for (i = 0; i < h * w * 4; i++) {
887724c56b46af1ad81212d044689960f433046508bDRC              srcBuf[i] = (byte)(r.nextInt(2) * 255);
888724c56b46af1ad81212d044689960f433046508bDRC            }
889b14813947ee5abacb40fc20455c8d7af1adfd3a7DRC            tjc.setSourceImage(srcBuf, 0, 0, h, 0, w, TJ.PF_BGRX);
890fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC            if (doYUV)
891fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC              tjc.encodeYUV(dstImage, 0);
89238c9970b95f8f83769b5476a9d7e6062714c19f6DRC            else
89338c9970b95f8f83769b5476a9d7e6062714c19f6DRC              tjc.compress(dstBuf, 0);
894f7f3ea404cc8618305efc059c34d881566206ed9DRC          }
895f7f3ea404cc8618305efc059c34d881566206ed9DRC        }
896f7f3ea404cc8618305efc059c34d881566206ed9DRC      }
897f7f3ea404cc8618305efc059c34d881566206ed9DRC      System.out.println("Done.      ");
89867bee8683db22050e346e0d002c5969da854d582DRC    } catch(Exception e) {
89967bee8683db22050e346e0d002c5969da854d582DRC      if (tjc != null) tjc.close();
900f7f3ea404cc8618305efc059c34d881566206ed9DRC      throw e;
901f7f3ea404cc8618305efc059c34d881566206ed9DRC    }
90267bee8683db22050e346e0d002c5969da854d582DRC    if (tjc != null) tjc.close();
903f7f3ea404cc8618305efc059c34d881566206ed9DRC  }
904f7f3ea404cc8618305efc059c34d881566206ed9DRC
90567bee8683db22050e346e0d002c5969da854d582DRC  public static void main(String[] argv) {
906f7f3ea404cc8618305efc059c34d881566206ed9DRC    try {
907b6ed7d347a3a25dc6fa42c33a9f96c159b36bb6dDRC      String testName = "javatest";
90867bee8683db22050e346e0d002c5969da854d582DRC      for (int i = 0; i < argv.length; i++) {
90967bee8683db22050e346e0d002c5969da854d582DRC        if (argv[i].equalsIgnoreCase("-yuv"))
910fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC          doYUV = true;
911fef9852da3a80bfaf84862462609f97d77ad6db7DRC        if (argv[i].equalsIgnoreCase("-noyuvpad"))
912fef9852da3a80bfaf84862462609f97d77ad6db7DRC          pad = 1;
91367bee8683db22050e346e0d002c5969da854d582DRC        if (argv[i].substring(0, 1).equalsIgnoreCase("-h") ||
91467bee8683db22050e346e0d002c5969da854d582DRC            argv[i].equalsIgnoreCase("-?"))
915f7f3ea404cc8618305efc059c34d881566206ed9DRC          usage();
91667bee8683db22050e346e0d002c5969da854d582DRC        if (argv[i].equalsIgnoreCase("-bi")) {
917b6ed7d347a3a25dc6fa42c33a9f96c159b36bb6dDRC          bi = true;
918b6ed7d347a3a25dc6fa42c33a9f96c159b36bb6dDRC          testName = "javabitest";
919b6ed7d347a3a25dc6fa42c33a9f96c159b36bb6dDRC        }
920f7f3ea404cc8618305efc059c34d881566206ed9DRC      }
921fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC      if (doYUV)
92238cb1ec2a77b7209a260f3cced1e6c6365616c8eDRC        _4byteFormats[4] = -1;
92367bee8683db22050e346e0d002c5969da854d582DRC      doTest(35, 39, bi ? _3byteFormatsBI : _3byteFormats, TJ.SAMP_444,
92467bee8683db22050e346e0d002c5969da854d582DRC             testName);
92567bee8683db22050e346e0d002c5969da854d582DRC      doTest(39, 41, bi ? _4byteFormatsBI : _4byteFormats, TJ.SAMP_444,
92667bee8683db22050e346e0d002c5969da854d582DRC             testName);
927cac105133e75a52fa5d57a2abccf242bb7b820d0DRC      doTest(41, 35, bi ? _3byteFormatsBI : _3byteFormats, TJ.SAMP_422,
92867bee8683db22050e346e0d002c5969da854d582DRC             testName);
929cac105133e75a52fa5d57a2abccf242bb7b820d0DRC      doTest(35, 39, bi ? _4byteFormatsBI : _4byteFormats, TJ.SAMP_422,
93067bee8683db22050e346e0d002c5969da854d582DRC             testName);
931cac105133e75a52fa5d57a2abccf242bb7b820d0DRC      doTest(39, 41, bi ? _3byteFormatsBI : _3byteFormats, TJ.SAMP_420,
93267bee8683db22050e346e0d002c5969da854d582DRC             testName);
933cac105133e75a52fa5d57a2abccf242bb7b820d0DRC      doTest(41, 35, bi ? _4byteFormatsBI : _4byteFormats, TJ.SAMP_420,
93467bee8683db22050e346e0d002c5969da854d582DRC             testName);
935cac105133e75a52fa5d57a2abccf242bb7b820d0DRC      doTest(35, 39, bi ? _3byteFormatsBI : _3byteFormats, TJ.SAMP_440,
93667bee8683db22050e346e0d002c5969da854d582DRC             testName);
937cac105133e75a52fa5d57a2abccf242bb7b820d0DRC      doTest(39, 41, bi ? _4byteFormatsBI : _4byteFormats, TJ.SAMP_440,
93867bee8683db22050e346e0d002c5969da854d582DRC             testName);
939a5830628b9cc92f9305ecb7d127ebc7207b12582DRC      doTest(41, 35, bi ? _3byteFormatsBI : _3byteFormats, TJ.SAMP_411,
94067bee8683db22050e346e0d002c5969da854d582DRC             testName);
941a5830628b9cc92f9305ecb7d127ebc7207b12582DRC      doTest(35, 39, bi ? _4byteFormatsBI : _4byteFormats, TJ.SAMP_411,
942a5830628b9cc92f9305ecb7d127ebc7207b12582DRC             testName);
943a5830628b9cc92f9305ecb7d127ebc7207b12582DRC      doTest(39, 41, bi ? onlyGrayBI : onlyGray, TJ.SAMP_GRAY, testName);
944a5830628b9cc92f9305ecb7d127ebc7207b12582DRC      doTest(41, 35, bi ? _3byteFormatsBI : _3byteFormats, TJ.SAMP_GRAY,
945a5830628b9cc92f9305ecb7d127ebc7207b12582DRC             testName);
94638cb1ec2a77b7209a260f3cced1e6c6365616c8eDRC      _4byteFormats[4] = -1;
947a5830628b9cc92f9305ecb7d127ebc7207b12582DRC      doTest(35, 39, bi ? _4byteFormatsBI : _4byteFormats, TJ.SAMP_GRAY,
94867bee8683db22050e346e0d002c5969da854d582DRC             testName);
94938c9970b95f8f83769b5476a9d7e6062714c19f6DRC      if (!bi)
95067bee8683db22050e346e0d002c5969da854d582DRC        bufSizeTest();
951fc26b6577a2c422899e5cb9f483ee9d3ed37e185DRC      if (doYUV && !bi) {
9524f7b7b339c2cda3683a1bfe30e2551d7ef2db803DRC        System.out.print("\n--------------------\n\n");
953b6ed7d347a3a25dc6fa42c33a9f96c159b36bb6dDRC        doTest(48, 48, onlyRGB, TJ.SAMP_444, "javatest_yuv0");
954b6ed7d347a3a25dc6fa42c33a9f96c159b36bb6dDRC        doTest(48, 48, onlyRGB, TJ.SAMP_422, "javatest_yuv0");
955b6ed7d347a3a25dc6fa42c33a9f96c159b36bb6dDRC        doTest(48, 48, onlyRGB, TJ.SAMP_420, "javatest_yuv0");
956b6ed7d347a3a25dc6fa42c33a9f96c159b36bb6dDRC        doTest(48, 48, onlyRGB, TJ.SAMP_440, "javatest_yuv0");
957a5830628b9cc92f9305ecb7d127ebc7207b12582DRC        doTest(48, 48, onlyRGB, TJ.SAMP_411, "javatest_yuv0");
958b6ed7d347a3a25dc6fa42c33a9f96c159b36bb6dDRC        doTest(48, 48, onlyRGB, TJ.SAMP_GRAY, "javatest_yuv0");
959b6ed7d347a3a25dc6fa42c33a9f96c159b36bb6dDRC        doTest(48, 48, onlyGray, TJ.SAMP_GRAY, "javatest_yuv0");
960f7f3ea404cc8618305efc059c34d881566206ed9DRC      }
96167bee8683db22050e346e0d002c5969da854d582DRC    } catch(Exception e) {
9622e2358eae408d1a04e74c4544e9997c507387b4cDRC      e.printStackTrace();
963f7f3ea404cc8618305efc059c34d881566206ed9DRC      exitStatus = -1;
964f7f3ea404cc8618305efc059c34d881566206ed9DRC    }
965f7f3ea404cc8618305efc059c34d881566206ed9DRC    System.exit(exitStatus);
966f7f3ea404cc8618305efc059c34d881566206ed9DRC  }
9673bad53fa04699ce22a7d2ba6763d7a357ef30540DRC}
968