Merge branch 'master' of https://source.jalview.org/git/jalviewjs.git
[jalviewjs.git] / src / javajs / img / JpgEncoder.java
index 684af37..5c128d6 100644 (file)
-// Version 1.0a\r
-// Copyright (C) 1998, James R. Weeks and BioElectroMech.\r
-// Visit BioElectroMech at www.obrador.com.  Email James@obrador.com.\r
-\r
-// See license.txt for details about the allowed used of this software.\r
-// This software is based in part on the work of the Independent JPEG Group.\r
-// See IJGreadme.txt for details about the Independent JPEG Group's license.\r
-\r
-// This encoder is inspired by the Java Jpeg encoder by Florian Raemy,\r
-// studwww.eurecom.fr/~raemy.\r
-// It borrows a great deal of code and structure from the Independent\r
-// Jpeg Group's Jpeg 6a library, Copyright Thomas G. Lane.\r
-// See license.txt for details \r
-\r
-/*\r
- * JpegEncoder and its associated classes are Copyright (c) 1998, James R. Weeks and BioElectroMech\r
- * see(Jmol/src/com/obrador/license.txt)\r
- * \r
- * javjs.img.JpegEncoder.java was adapted by Bob Hanson\r
- * \r
- * for Jmol in the following ways:\r
- * \r
- * 1) minor coding efficiencies were made in some for() loops.\r
- * 2) methods not used by Jmol were commented out\r
- * 3) method and variable signatures were modified to provide \r
- *    more appropriate method privacy.\r
- * 4) additions for Java2Script compatibility \r
- * \r
- * Original files are maintained in the Jmol.src.com.obrador package, but\r
- * these original files are not distributed with Jmol.\r
- *   \r
-*/\r
-\r
-package javajs.img;\r
-\r
-import java.io.IOException;\r
-import java.util.Map;\r
-\r
-import javajs.img.ImageEncoder;\r
-import javajs.util.AU;\r
-import javajs.util.OC;\r
-\r
-/**\r
- * JpegEncoder - The JPEG main program which performs a jpeg compression of an\r
- * image.\r
- * \r
- *  A system to allow the full Jmol state -- regardless of length -- \r
- *  to be encoded in a set of APP1 (FFE1) tags.\r
- *  But we have to be careful about line ends for backward compatibility. \r
- *  This solution is not 100% effective, because some data lines may in principle be \r
- *  Very large and may not contain new lines for more than 65500 characters, \r
- *  But that would be very unusual. Perhaps a huge data set loaded from a \r
- *  string. Introduced in Jmol 12.1.36. Bob Hanson\r
- *  \r
- * See org.com.obrador.license.txt\r
- * \r
- */\r
-\r
-public class JpgEncoder extends ImageEncoder {\r
-\r
-  // this string will GENERALLY appear at the end of lines and be escaped \r
-  private static final int CONTINUE_MAX = 65500; // some room to spare here. \r
-  private static final int CONTINUE_MAX_BUFFER = CONTINUE_MAX + 10; // never break up last 10 bytes\r
-\r
-  private JpegObj jpegObj;\r
-  private Huffman huf;\r
-  private DCT dct;\r
-  protected int defaultQuality = 100;\r
-  private String applicationTag;\r
-\r
-  public JpgEncoder() {\r
-\r
-  }\r
-\r
-  @Override\r
-  protected void setParams(Map<String, Object> params) {\r
-    if (quality <= 0)\r
-      quality = (params.containsKey("qualityJPG") ? ((Integer) params.get("qualityJPG")).intValue() : defaultQuality);\r
-    jpegObj = new JpegObj();\r
-    jpegObj.comment = (String) params.get("comment");\r
-    applicationTag = (String) params.get("jpgAppTag");\r
-  }\r
-\r
-  @Override\r
-  protected void generate() throws IOException {\r
-    jpegObj.imageWidth = width;\r
-    jpegObj.imageHeight = height;\r
-    dct = new DCT(quality);\r
-    huf = new Huffman(width, height);\r
-    if (jpegObj == null)\r
-      return;\r
-    jpegObj.getYCCArray(pixels);\r
-    String longState = writeHeaders(jpegObj, dct);\r
-    writeCompressedData(jpegObj, dct, huf);\r
-    writeMarker(eoi);\r
-    if (longState != null) {\r
-      byte[] b = longState.getBytes();\r
-      out.write(b, 0, b.length);\r
-    }\r
-  }\r
-\r
-  private void writeCompressedData(JpegObj jpegObj, DCT dct, Huffman huf) {\r
-    int i, j, r, c, a, b;\r
-    int comp, xpos, ypos, xblockoffset, yblockoffset;\r
-    float inputArray[][];\r
-    float dctArray1[][] = new float[8][8];\r
-    double dctArray2[][] = new double[8][8];\r
-    int dctArray3[] = new int[8 * 8];\r
-\r
-    /*\r
-     * This method controls the compression of the image.\r
-     * Starting at the upper left of the image, it compresses 8x8 blocks\r
-     * of data until the entire image has been compressed.\r
-     */\r
-\r
-    int lastDCvalue[] = new int[jpegObj.numberOfComponents];\r
-    //int zeroArray[] = new int[64]; // initialized to hold all zeros\r
-    //int Width = 0, Height = 0;\r
-    //int nothing = 0, not;\r
-    int minBlockWidth, minBlockHeight;\r
-    // This initial setting of MinBlockWidth and MinBlockHeight is done to\r
-    // ensure they start with values larger than will actually be the case.\r
-    minBlockWidth = ((huf.imageWidth % 8 != 0) ? (int) (Math\r
-        .floor(huf.imageWidth / 8.0) + 1) * 8 : huf.imageWidth);\r
-    minBlockHeight = ((huf.imageHeight % 8 != 0) ? (int) (Math\r
-        .floor(huf.imageHeight / 8.0) + 1) * 8 : huf.imageHeight);\r
-    for (comp = 0; comp < jpegObj.numberOfComponents; comp++) {\r
-      minBlockWidth = Math.min(minBlockWidth, jpegObj.blockWidth[comp]);\r
-      minBlockHeight = Math.min(minBlockHeight, jpegObj.blockHeight[comp]);\r
-    }\r
-    xpos = 0;\r
-    for (r = 0; r < minBlockHeight; r++) {\r
-      for (c = 0; c < minBlockWidth; c++) {\r
-        xpos = c * 8;\r
-        ypos = r * 8;\r
-        for (comp = 0; comp < jpegObj.numberOfComponents; comp++) {\r
-          //Width = JpegObj.BlockWidth[comp];\r
-          //Height = JpegObj.BlockHeight[comp];\r
-          inputArray = jpegObj.components[comp];\r
-          int vsampF = jpegObj.vsampFactor[comp];\r
-          int hsampF = jpegObj.hsampFactor[comp];\r
-          int qNumber = jpegObj.qtableNumber[comp];\r
-          int dcNumber = jpegObj.dctableNumber[comp];\r
-          int acNumber = jpegObj.actableNumber[comp];\r
-\r
-          for (i = 0; i < vsampF; i++) {\r
-            for (j = 0; j < hsampF; j++) {\r
-              xblockoffset = j * 8;\r
-              yblockoffset = i * 8;\r
-              for (a = 0; a < 8; a++) {\r
-                for (b = 0; b < 8; b++) {\r
-\r
-                  // I believe this is where the dirty line at the bottom of\r
-                  // the image is coming from.\r
-                  // I need to do a check here to make sure I'm not reading past\r
-                  // image data.\r
-                  // This seems to not be a big issue right now. (04/04/98)\r
-\r
-                  dctArray1[a][b] = inputArray[ypos + yblockoffset + a][xpos\r
-                      + xblockoffset + b];\r
-                }\r
-              }\r
-              // The following code commented out because on some images this technique\r
-              // results in poor right and bottom borders.\r
-              // if ((!JpegObj.lastColumnIsDummy[comp] || c < Width - 1) &&\r
-              //       (!JpegObj.lastRowIsDummy[comp] || r < Height - 1)) {\r
-              dctArray2 = DCT.forwardDCT(dctArray1);\r
-              dctArray3 = DCT.quantizeBlock(dctArray2, dct.divisors[qNumber]);\r
-              // }\r
-              // else {\r
-              //   zeroArray[0] = dctArray3[0];\r
-              //   zeroArray[0] = lastDCvalue[comp];\r
-              //   dctArray3 = zeroArray;\r
-              // }\r
-              huf.HuffmanBlockEncoder(out, dctArray3, lastDCvalue[comp],\r
-                  dcNumber, acNumber);\r
-              lastDCvalue[comp] = dctArray3[0];\r
-            }\r
-          }\r
-        }\r
-      }\r
-    }\r
-    huf.flushBuffer(out);\r
-  }\r
-\r
-  private static byte[] eoi = { (byte) 0xFF, (byte) 0xD9 };\r
-\r
-  private static byte[] jfif = new byte[] {\r
-  /* JFIF[0] =*/(byte) 0xff,\r
-  /* JFIF[1] =*/(byte) 0xe0,\r
-  /* JFIF[2] =*/0,\r
-  /* JFIF[3] =*/16,\r
-  /* JFIF[4] =*/(byte) 0x4a, //'J'\r
-      /* JFIF[5] =*/(byte) 0x46, //'F'\r
-      /* JFIF[6] =*/(byte) 0x49, //'I'\r
-      /* JFIF[7] =*/(byte) 0x46, //'F'\r
-      /* JFIF[8] =*/0,\r
-      /* JFIF[9] =*/1,\r
-      /* JFIF[10] =*/0,\r
-      /* JFIF[11] =*/0,\r
-      /* JFIF[12] =*/0,\r
-      /* JFIF[13] =*/1,\r
-      /* JFIF[14] =*/0,\r
-      /* JFIF[15] =*/1,\r
-      /* JFIF[16] =*/0,\r
-      /* JFIF[17] =*/0 };\r
-\r
-  private static byte[] soi = { (byte) 0xFF, (byte) 0xD8 };\r
-\r
-  private String writeHeaders(JpegObj jpegObj, DCT dct) {\r
-    int i, j, index, offset;\r
-    int tempArray[];\r
-\r
-    // the SOI marker\r
-    writeMarker(soi);\r
-\r
-    // The order of the following headers is quite inconsequential.\r
-    // the JFIF header\r
-    writeArray(jfif);\r
-\r
-    // Comment Header\r
-    String comment = null;\r
-    if (jpegObj.comment.length() > 0)\r
-      writeString(jpegObj.comment, (byte) 0xE1); // App data 1\r
-    writeString(\r
-        "JPEG Encoder Copyright 1998, James R. Weeks and BioElectroMech.\n\n",\r
-        (byte) 0xFE);\r
-\r
-    // The DQT header\r
-    // 0 is the luminance index and 1 is the chrominance index\r
-    byte dqt[] = new byte[134];\r
-    dqt[0] = (byte) 0xFF;\r
-    dqt[1] = (byte) 0xDB;\r
-    dqt[2] = 0;\r
-    dqt[3] = (byte) 132;\r
-    offset = 4;\r
-    for (i = 0; i < 2; i++) {\r
-      dqt[offset++] = (byte) ((0 << 4) + i);\r
-      tempArray = dct.quantum[i];\r
-      for (j = 0; j < 64; j++) {\r
-        dqt[offset++] = (byte) tempArray[Huffman.jpegNaturalOrder[j]];\r
-      }\r
-    }\r
-    writeArray(dqt);\r
-\r
-    // Start of Frame Header\r
-    byte sof[] = new byte[19];\r
-    sof[0] = (byte) 0xFF;\r
-    sof[1] = (byte) 0xC0;\r
-    sof[2] = 0;\r
-    sof[3] = 17;\r
-    sof[4] = (byte) jpegObj.precision;\r
-    sof[5] = (byte) ((jpegObj.imageHeight >> 8) & 0xFF);\r
-    sof[6] = (byte) ((jpegObj.imageHeight) & 0xFF);\r
-    sof[7] = (byte) ((jpegObj.imageWidth >> 8) & 0xFF);\r
-    sof[8] = (byte) ((jpegObj.imageWidth) & 0xFF);\r
-    sof[9] = (byte) jpegObj.numberOfComponents;\r
-    index = 10;\r
-    for (i = 0; i < sof[9]; i++) {\r
-      sof[index++] = (byte) jpegObj.compID[i];\r
-      sof[index++] = (byte) ((jpegObj.hsampFactor[i] << 4) + jpegObj.vsampFactor[i]);\r
-      sof[index++] = (byte) jpegObj.qtableNumber[i];\r
-    }\r
-    writeArray(sof);\r
-\r
-    WriteDHTHeader(Huffman.bitsDCluminance, Huffman.valDCluminance);\r
-    WriteDHTHeader(Huffman.bitsACluminance, Huffman.valACluminance);\r
-    WriteDHTHeader(Huffman.bitsDCchrominance, Huffman.valDCchrominance);\r
-    WriteDHTHeader(Huffman.bitsACchrominance, Huffman.valACchrominance);\r
-\r
-    // Start of Scan Header\r
-    byte sos[] = new byte[14];\r
-    sos[0] = (byte) 0xFF;\r
-    sos[1] = (byte) 0xDA;\r
-    sos[2] = 0;\r
-    sos[3] = 12;\r
-    sos[4] = (byte) jpegObj.numberOfComponents;\r
-    index = 5;\r
-    for (i = 0; i < sos[4]; i++) {\r
-      sos[index++] = (byte) jpegObj.compID[i];\r
-      sos[index++] = (byte) ((jpegObj.dctableNumber[i] << 4) + jpegObj.actableNumber[i]);\r
-    }\r
-    sos[index++] = (byte) jpegObj.ss;\r
-    sos[index++] = (byte) jpegObj.se;\r
-    sos[index++] = (byte) ((jpegObj.ah << 4) + jpegObj.al);\r
-    writeArray(sos);\r
-    return comment;\r
-  }\r
-\r
-  private void writeString(String s, byte id) {\r
-    int len = s.length();\r
-    int i0 = 0;\r
-    String suffix = applicationTag;\r
-    while (i0 < len) {\r
-      int nBytes = len - i0;\r
-      if (nBytes > CONTINUE_MAX_BUFFER) {\r
-        nBytes = CONTINUE_MAX;\r
-        // but break only at line breaks\r
-        int pt = s.lastIndexOf('\n', i0 + nBytes);\r
-        if (pt > i0 + 1)\r
-          nBytes = pt - i0;\r
-      }\r
-      if (i0 + nBytes == len)\r
-        suffix = "";\r
-      writeTag(nBytes + suffix.length(), id);\r
-      writeArray(s.substring(i0, i0 + nBytes).getBytes());\r
-      if (suffix.length() > 0)\r
-        writeArray(suffix.getBytes());\r
-      i0 += nBytes;\r
-    }\r
-  }\r
-\r
-  private void writeTag(int length, byte id) {\r
-    length += 2;\r
-    byte com[] = new byte[4];\r
-    com[0] = (byte) 0xFF;\r
-    com[1] = id;\r
-    com[2] = (byte) ((length >> 8) & 0xFF);\r
-    com[3] = (byte) (length & 0xFF);\r
-    writeArray(com);\r
-  }\r
-\r
-  void WriteDHTHeader(int[] bits, int[] val) {\r
-    // hansonr@stolaf.edu: simplified code.\r
-    byte[] dht;\r
-    int bytes = 0;\r
-    for (int j = 1; j < 17; j++)\r
-      bytes += bits[j];\r
-    dht = new byte[21 + bytes];\r
-    dht[0] = (byte) 0xFF;\r
-    dht[1] = (byte) 0xC4;\r
-    int index = 4;\r
-    for (int j = 0; j < 17; j++)\r
-      dht[index++] = (byte) bits[j];\r
-    for (int j = 0; j < bytes; j++)\r
-      dht[index++] = (byte) val[j];\r
-    dht[2] = (byte) (((index - 2) >> 8) & 0xFF);\r
-    dht[3] = (byte) ((index - 2) & 0xFF);\r
-    writeArray(dht);\r
-  }\r
-\r
-  void writeMarker(byte[] data) {\r
-    out.write(data, 0, 2);\r
-  }\r
-\r
-  void writeArray(byte[] data) {\r
-    out.write(data, 0, data.length);\r
-  }\r
-\r
-}\r
-\r
-// This class incorporates quality scaling as implemented in the JPEG-6a\r
-// library.\r
-\r
-/*\r
- * DCT - A Java implementation of the Discreet Cosine Transform\r
- */\r
-\r
-class DCT {\r
-\r
-  /**\r
-   * DCT Block Size - default 8\r
-   */\r
-  private final static int N = 8;\r
-  private final static int NN = N * N;\r
-\r
-  /**\r
-   * Image Quality (0-100) - default 80 (good image / good compression)\r
-   */\r
-  //public int QUALITY = 80;\r
-\r
-  int[][] quantum = AU.newInt2(2);\r
-  double[][] divisors = AU.newDouble2(2);\r
-\r
-  /**\r
-   * Quantitization Matrix for luminace.\r
-   */\r
-  private int quantum_luminance[] = new int[NN];\r
-  private double DivisorsLuminance[] = new double[NN];\r
-\r
-  /**\r
-   * Quantitization Matrix for chrominance.\r
-   */\r
-  private int quantum_chrominance[] = new int[NN];\r
-  private double DivisorsChrominance[] = new double[NN];\r
-\r
-  /**\r
-   * Constructs a new DCT object. Initializes the cosine transform matrix these\r
-   * are used when computing the DCT and it's inverse. This also initializes the\r
-   * run length counters and the ZigZag sequence. Note that the image quality\r
-   * can be worse than 25 however the image will be extemely pixelated, usually\r
-   * to a block size of N.\r
-   * \r
-   * @param quality\r
-   *        The quality of the image (0 worst - 100 best)\r
-   * \r
-   */\r
-  DCT(int quality) {\r
-    initMatrix(quality);\r
-  }\r
-\r
-  /*\r
-   * This method sets up the quantization matrix for luminance and\r
-   * chrominance using the Quality parameter.\r
-   */\r
-  private void initMatrix(int quality) {\r
-    // converting quality setting to that specified in the jpeg_quality_scaling\r
-    // method in the IJG Jpeg-6a C libraries\r
-\r
-    quality = (quality < 1 ? 1 : quality > 100 ? 100 : quality);\r
-    quality = (quality < 50 ? 5000 / quality : 200 - quality * 2);\r
-\r
-    // Creating the luminance matrix\r
-\r
-    quantum_luminance[0] = 16;\r
-    quantum_luminance[1] = 11;\r
-    quantum_luminance[2] = 10;\r
-    quantum_luminance[3] = 16;\r
-    quantum_luminance[4] = 24;\r
-    quantum_luminance[5] = 40;\r
-    quantum_luminance[6] = 51;\r
-    quantum_luminance[7] = 61;\r
-    quantum_luminance[8] = 12;\r
-    quantum_luminance[9] = 12;\r
-    quantum_luminance[10] = 14;\r
-    quantum_luminance[11] = 19;\r
-    quantum_luminance[12] = 26;\r
-    quantum_luminance[13] = 58;\r
-    quantum_luminance[14] = 60;\r
-    quantum_luminance[15] = 55;\r
-    quantum_luminance[16] = 14;\r
-    quantum_luminance[17] = 13;\r
-    quantum_luminance[18] = 16;\r
-    quantum_luminance[19] = 24;\r
-    quantum_luminance[20] = 40;\r
-    quantum_luminance[21] = 57;\r
-    quantum_luminance[22] = 69;\r
-    quantum_luminance[23] = 56;\r
-    quantum_luminance[24] = 14;\r
-    quantum_luminance[25] = 17;\r
-    quantum_luminance[26] = 22;\r
-    quantum_luminance[27] = 29;\r
-    quantum_luminance[28] = 51;\r
-    quantum_luminance[29] = 87;\r
-    quantum_luminance[30] = 80;\r
-    quantum_luminance[31] = 62;\r
-    quantum_luminance[32] = 18;\r
-    quantum_luminance[33] = 22;\r
-    quantum_luminance[34] = 37;\r
-    quantum_luminance[35] = 56;\r
-    quantum_luminance[36] = 68;\r
-    quantum_luminance[37] = 109;\r
-    quantum_luminance[38] = 103;\r
-    quantum_luminance[39] = 77;\r
-    quantum_luminance[40] = 24;\r
-    quantum_luminance[41] = 35;\r
-    quantum_luminance[42] = 55;\r
-    quantum_luminance[43] = 64;\r
-    quantum_luminance[44] = 81;\r
-    quantum_luminance[45] = 104;\r
-    quantum_luminance[46] = 113;\r
-    quantum_luminance[47] = 92;\r
-    quantum_luminance[48] = 49;\r
-    quantum_luminance[49] = 64;\r
-    quantum_luminance[50] = 78;\r
-    quantum_luminance[51] = 87;\r
-    quantum_luminance[52] = 103;\r
-    quantum_luminance[53] = 121;\r
-    quantum_luminance[54] = 120;\r
-    quantum_luminance[55] = 101;\r
-    quantum_luminance[56] = 72;\r
-    quantum_luminance[57] = 92;\r
-    quantum_luminance[58] = 95;\r
-    quantum_luminance[59] = 98;\r
-    quantum_luminance[60] = 112;\r
-    quantum_luminance[61] = 100;\r
-    quantum_luminance[62] = 103;\r
-    quantum_luminance[63] = 99;\r
-\r
-    AANscale(DivisorsLuminance, quantum_luminance, quality);\r
-\r
-    // Creating the chrominance matrix\r
-\r
-    for (int i = 4; i < 64; i++)\r
-      quantum_chrominance[i] = 99;\r
-\r
-    quantum_chrominance[0] = 17;\r
-    quantum_chrominance[1] = 18;\r
-    quantum_chrominance[2] = 24;\r
-    quantum_chrominance[3] = 47;\r
-\r
-    quantum_chrominance[8] = 18;\r
-    quantum_chrominance[9] = 21;\r
-    quantum_chrominance[10] = 26;\r
-    quantum_chrominance[11] = 66;\r
-\r
-    quantum_chrominance[16] = 24;\r
-    quantum_chrominance[17] = 26;\r
-    quantum_chrominance[18] = 56;\r
-\r
-    quantum_chrominance[24] = 47;\r
-    quantum_chrominance[25] = 66;\r
-\r
-    AANscale(DivisorsChrominance, quantum_chrominance, quality);\r
-\r
-    // quantum and Divisors are objects used to hold the appropriate matices\r
-\r
-    quantum[0] = quantum_luminance;\r
-    quantum[1] = quantum_chrominance;\r
-\r
-    divisors[0] = DivisorsLuminance;\r
-    divisors[1] = DivisorsChrominance;\r
-\r
-  }\r
-\r
-  private final static double[] AANscaleFactor = { 1.0, 1.387039845,\r
-      1.306562965, 1.175875602, 1.0, 0.785694958, 0.541196100, 0.275899379 };\r
-\r
-  static private void AANscale(double[] divisors, int[] values, int quality) {\r
-\r
-    for (int j = 0; j < 64; j++) {\r
-      int temp = (values[j] * quality + 50) / 100;\r
-      values[j] = (temp < 1 ? 1 : temp > 255 ? 255 : temp);\r
-    }\r
-\r
-    for (int i = 0, index = 0; i < 8; i++)\r
-      for (int j = 0; j < 8; j++, index++)\r
-        // The divisors for the LL&M method (the slow integer method used in\r
-        // jpeg 6a library).  This method is currently (04/04/98) incompletely\r
-        // implemented.\r
-        // DivisorsLuminance[index] = ((double) quantum_luminance[index]) << 3;\r
-        // The divisors for the AAN method (the float method used in jpeg 6a library.\r
-        divisors[index] = (0.125 / (values[index] * AANscaleFactor[i] * AANscaleFactor[j]));\r
-  }\r
-\r
-  /*\r
-   * This method preforms forward DCT on a block of image data using\r
-   * the literal method specified for a 2-D Discrete Cosine Transform.\r
-   * It is included as a curiosity and can give you an idea of the\r
-   * difference in the compression result (the resulting image quality)\r
-   * by comparing its output to the output of the AAN method below.\r
-   * It is ridiculously inefficient.\r
-   */\r
-\r
-  // For now the final output is unusable.  The associated quantization step\r
-  // needs some tweaking.  If you get this part working, please let me know.\r
-  /*\r
-    public double[][] forwardDCTExtreme(float input[][])\r
-    {\r
-      double output[][] = new double[N][N];\r
-      int v, u, x, y;\r
-      for (v = 0; v < 8; v++) {\r
-        for (u = 0; u < 8; u++) {\r
-          for (x = 0; x < 8; x++) {\r
-            for (y = 0; y < 8; y++) {\r
-              output[v][u] += input[x][y] * \r
-                  Math.cos(((double)(2*x + 1)*(double)u*Math.PI)/16)*\r
-                  Math.cos(((double)(2*y + 1)*(double)v*Math.PI)/16);\r
-            }\r
-          }\r
-          output[v][u] *= (0.25)*((u == 0) ? (1.0/Math.sqrt(2)) : (double) 1.0)*((v == 0) ? (1.0/Math.sqrt(2)) : (double) 1.0);\r
-        }\r
-      }\r
-      return output;\r
-    }\r
-    \r
-  */\r
-  /*\r
-   * This method preforms a DCT on a block of image data using the AAN\r
-   * method as implemented in the IJG Jpeg-6a library.\r
-   */\r
-  static double[][] forwardDCT(float input[][]) {\r
-    double output[][] = new double[N][N];\r
-    double tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7;\r
-    double tmp10, tmp11, tmp12, tmp13;\r
-    double z1, z2, z3, z4, z5, z11, z13;\r
-    // Subtracts 128 from the input values\r
-    for (int i = 0; i < 8; i++)\r
-      for (int j = 0; j < 8; j++)\r
-        output[i][j] = (input[i][j] - 128.0);\r
-    // input[i][j] -= 128;\r
-\r
-    for (int i = 0; i < 8; i++) {\r
-      tmp0 = output[i][0] + output[i][7];\r
-      tmp7 = output[i][0] - output[i][7];\r
-      tmp1 = output[i][1] + output[i][6];\r
-      tmp6 = output[i][1] - output[i][6];\r
-      tmp2 = output[i][2] + output[i][5];\r
-      tmp5 = output[i][2] - output[i][5];\r
-      tmp3 = output[i][3] + output[i][4];\r
-      tmp4 = output[i][3] - output[i][4];\r
-\r
-      tmp10 = tmp0 + tmp3;\r
-      tmp13 = tmp0 - tmp3;\r
-      tmp11 = tmp1 + tmp2;\r
-      tmp12 = tmp1 - tmp2;\r
-\r
-      output[i][0] = tmp10 + tmp11;\r
-      output[i][4] = tmp10 - tmp11;\r
-\r
-      z1 = (tmp12 + tmp13) * 0.707106781;\r
-      output[i][2] = tmp13 + z1;\r
-      output[i][6] = tmp13 - z1;\r
-\r
-      tmp10 = tmp4 + tmp5;\r
-      tmp11 = tmp5 + tmp6;\r
-      tmp12 = tmp6 + tmp7;\r
-\r
-      z5 = (tmp10 - tmp12) * 0.382683433;\r
-      z2 = 0.541196100 * tmp10 + z5;\r
-      z4 = 1.306562965 * tmp12 + z5;\r
-      z3 = tmp11 * 0.707106781;\r
-\r
-      z11 = tmp7 + z3;\r
-      z13 = tmp7 - z3;\r
-\r
-      output[i][5] = z13 + z2;\r
-      output[i][3] = z13 - z2;\r
-      output[i][1] = z11 + z4;\r
-      output[i][7] = z11 - z4;\r
-    }\r
-\r
-    for (int i = 0; i < 8; i++) {\r
-      tmp0 = output[0][i] + output[7][i];\r
-      tmp7 = output[0][i] - output[7][i];\r
-      tmp1 = output[1][i] + output[6][i];\r
-      tmp6 = output[1][i] - output[6][i];\r
-      tmp2 = output[2][i] + output[5][i];\r
-      tmp5 = output[2][i] - output[5][i];\r
-      tmp3 = output[3][i] + output[4][i];\r
-      tmp4 = output[3][i] - output[4][i];\r
-\r
-      tmp10 = tmp0 + tmp3;\r
-      tmp13 = tmp0 - tmp3;\r
-      tmp11 = tmp1 + tmp2;\r
-      tmp12 = tmp1 - tmp2;\r
-\r
-      output[0][i] = tmp10 + tmp11;\r
-      output[4][i] = tmp10 - tmp11;\r
-\r
-      z1 = (tmp12 + tmp13) * 0.707106781;\r
-      output[2][i] = tmp13 + z1;\r
-      output[6][i] = tmp13 - z1;\r
-\r
-      tmp10 = tmp4 + tmp5;\r
-      tmp11 = tmp5 + tmp6;\r
-      tmp12 = tmp6 + tmp7;\r
-\r
-      z5 = (tmp10 - tmp12) * 0.382683433;\r
-      z2 = 0.541196100 * tmp10 + z5;\r
-      z4 = 1.306562965 * tmp12 + z5;\r
-      z3 = tmp11 * 0.707106781;\r
-\r
-      z11 = tmp7 + z3;\r
-      z13 = tmp7 - z3;\r
-\r
-      output[5][i] = z13 + z2;\r
-      output[3][i] = z13 - z2;\r
-      output[1][i] = z11 + z4;\r
-      output[7][i] = z11 - z4;\r
-    }\r
-\r
-    return output;\r
-  }\r
-\r
-  /*\r
-   * This method quantitizes data and rounds it to the nearest integer.\r
-   */\r
-  static int[] quantizeBlock(double inputData[][], double[] divisorsCode) {\r
-    int outputData[] = new int[NN];\r
-    for (int i = 0, index = 0; i < 8; i++)\r
-      for (int j = 0; j < 8; j++, index++)\r
-        // The second line results in significantly better compression.\r
-        outputData[index] = (int) (Math.round(inputData[i][j]\r
-            * divisorsCode[index]));\r
-    //                        outputData[index] = (int)(((inputData[i][j] * (((double[]) (Divisors[code]))[index])) + 16384.5) -16384);\r
-    return outputData;\r
-  }\r
-\r
-  /*\r
-   * This is the method for quantizing a block DCT'ed with forwardDCTExtreme\r
-   * This method quantitizes data and rounds it to the nearest integer.\r
-   */\r
-\r
-  /*\r
-\r
-    public double[][] forwardDCTExtreme(float input[][])\r
-    {\r
-      double output[][] = new double[N][N];\r
-      int v, u, x, y;\r
-      for (v = 0; v < 8; v++) {\r
-        for (u = 0; u < 8; u++) {\r
-          for (x = 0; x < 8; x++) {\r
-            for (y = 0; y < 8; y++) {\r
-              output[v][u] += input[x][y] * \r
-                  Math.cos(((double)(2*x + 1)*(double)u*Math.PI)/16)*\r
-                  Math.cos(((double)(2*y + 1)*(double)v*Math.PI)/16);\r
-            }\r
-          }\r
-          output[v][u] *= (0.25)*((u == 0) ? (1.0/Math.sqrt(2)) : (double) 1.0)*((v == 0) ? (1.0/Math.sqrt(2)) : (double) 1.0);\r
-        }\r
-      }\r
-      return output;\r
-    }\r
-\r
-   */\r
-  /*\r
-    public int[] quantizeBlockExtreme(double inputData[][], int code)\r
-    {\r
-      int outputData[] = new int[NN];\r
-      int i, j;\r
-      int index;\r
-      index = 0;\r
-      for (i = 0; i < 8; i++) {\r
-        for (j = 0; j < 8; j++) {\r
-          outputData[index] = (int)(Math.round(inputData[i][j] / (((int[]) (quantum[code]))[index])));\r
-          index++;\r
-        }\r
-      }\r
-      \r
-      return outputData;\r
-    }\r
-  */\r
-}\r
-\r
-// This class was modified by James R. Weeks on 3/27/98.\r
-// It now incorporates Huffman table derivation as in the C jpeg library\r
-// from the IJG, Jpeg-6a.\r
-\r
-class Huffman {\r
-  private int bufferPutBits, bufferPutBuffer;\r
-  int imageHeight;\r
-  int imageWidth;\r
-  private int dc_matrix0[][];\r
-  private int ac_matrix0[][];\r
-  private int dc_matrix1[][];\r
-  private int ac_matrix1[][];\r
-  private int[][][] dc_matrix;\r
-  private int[][][] ac_matrix;\r
-  //private int code;\r
-  int numOfDCTables;\r
-  int numOfACTables;\r
-  final static int[] bitsDCluminance = { 0x00, 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0,\r
-      0, 0, 0, 0, 0 };\r
-  final static int[] valDCluminance = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };\r
-  final static int[] bitsDCchrominance = { 0x01, 0, 3, 1, 1, 1, 1, 1, 1, 1, 1,\r
-      1, 0, 0, 0, 0, 0 };\r
-  final static int[] valDCchrominance = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };\r
-  final static int[] bitsACluminance = { 0x10, 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4,\r
-      4, 0, 0, 1, 0x7d };\r
-  final static int[] valACluminance = { 0x01, 0x02, 0x03, 0x00, 0x04, 0x11,\r
-      0x05, 0x12, 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07, 0x22, 0x71,\r
-      0x14, 0x32, 0x81, 0x91, 0xa1, 0x08, 0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52,\r
-      0xd1, 0xf0, 0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16, 0x17, 0x18,\r
-      0x19, 0x1a, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x34, 0x35, 0x36, 0x37,\r
-      0x38, 0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x53,\r
-      0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67,\r
-      0x68, 0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x83,\r
-      0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96,\r
-      0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9,\r
-      0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3,\r
-      0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6,\r
-      0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8,\r
-      0xe9, 0xea, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa };\r
-  final static int[] bitsACchrominance = { 0x11, 0, 2, 1, 2, 4, 4, 3, 4, 7, 5,\r
-      4, 4, 0, 1, 2, 0x77 };\r
-  final static int[] valACchrominance = { 0x00, 0x01, 0x02, 0x03, 0x11, 0x04,\r
-      0x05, 0x21, 0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71, 0x13, 0x22,\r
-      0x32, 0x81, 0x08, 0x14, 0x42, 0x91, 0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33,\r
-      0x52, 0xf0, 0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34, 0xe1, 0x25,\r
-      0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x35, 0x36,\r
-      0x37, 0x38, 0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a,\r
-      0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x63, 0x64, 0x65, 0x66,\r
-      0x67, 0x68, 0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a,\r
-      0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x92, 0x93, 0x94,\r
-      0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,\r
-      0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba,\r
-      0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4,\r
-      0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7,\r
-      0xe8, 0xe9, 0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa };\r
-\r
-  /*\r
-   * jpegNaturalOrder[i] is the natural-order position of the i'th element\r
-   * of zigzag order.\r
-   */\r
-  final static int[] jpegNaturalOrder = { 0, 1, 8, 16, 9, 2, 3, 10, 17, 24, 32,\r
-      25, 18, 11, 4, 5, 12, 19, 26, 33, 40, 48, 41, 34, 27, 20, 13, 6, 7, 14,\r
-      21, 28, 35, 42, 49, 56, 57, 50, 43, 36, 29, 22, 15, 23, 30, 37, 44, 51,\r
-      58, 59, 52, 45, 38, 31, 39, 46, 53, 60, 61, 54, 47, 55, 62, 63, };\r
-\r
-  Huffman(int width, int height) {\r
-    initHuf();\r
-    imageWidth = width;\r
-    imageHeight = height;\r
-\r
-  }\r
-\r
-  /**\r
-   * HuffmanBlockEncoder run length encodes and Huffman encodes the quantized\r
-   * data.\r
-   * \r
-   * @param out\r
-   * @param zigzag\r
-   * @param prec\r
-   * @param dcCode\r
-   * @param acCode\r
-   **/\r
-\r
-  void HuffmanBlockEncoder(OC out, int zigzag[], int prec,\r
-                           int dcCode, int acCode) {\r
-    int temp, temp2, nbits, k, r, i;\r
-\r
-    numOfDCTables = 2;\r
-    numOfACTables = 2;\r
-\r
-    int[][] matrixDC = dc_matrix[dcCode];\r
-    int[][] matrixAC = ac_matrix[acCode];\r
-\r
-    // The DC portion\r
-\r
-    temp = temp2 = zigzag[0] - prec;\r
-    if (temp < 0) {\r
-      temp = -temp;\r
-      temp2--;\r
-    }\r
-    nbits = 0;\r
-    while (temp != 0) {\r
-      nbits++;\r
-      temp >>= 1;\r
-    }\r
-    //        if (nbits > 11) nbits = 11;\r
-    bufferIt(out, matrixDC[nbits][0], matrixDC[nbits][1]);\r
-    // The arguments in bufferIt are code and size.\r
-    if (nbits != 0) {\r
-      bufferIt(out, temp2, nbits);\r
-    }\r
-\r
-    // The AC portion\r
-\r
-    r = 0;\r
-\r
-    for (k = 1; k < 64; k++) {\r
-      if ((temp = zigzag[jpegNaturalOrder[k]]) == 0) {\r
-        r++;\r
-      } else {\r
-        while (r > 15) {\r
-          bufferIt(out, matrixAC[0xF0][0], matrixAC[0xF0][1]);\r
-          r -= 16;\r
-        }\r
-        temp2 = temp;\r
-        if (temp < 0) {\r
-          temp = -temp;\r
-          temp2--;\r
-        }\r
-        nbits = 1;\r
-        while ((temp >>= 1) != 0) {\r
-          nbits++;\r
-        }\r
-        i = (r << 4) + nbits;\r
-        bufferIt(out, matrixAC[i][0], matrixAC[i][1]);\r
-        bufferIt(out, temp2, nbits);\r
-\r
-        r = 0;\r
-      }\r
-    }\r
-\r
-    if (r > 0) {\r
-      bufferIt(out, matrixAC[0][0], matrixAC[0][1]);\r
-    }\r
-\r
-  }\r
-\r
-  // Uses an integer long (32 bits) buffer to store the Huffman encoded bits\r
-  // and sends them to out by the byte.\r
-\r
-  void bufferIt(OC out, int code, int size) {\r
-    int putBuffer = code;\r
-    int putBits = bufferPutBits;\r
-\r
-    putBuffer &= (1 << size) - 1;\r
-    putBits += size;\r
-    putBuffer <<= 24 - putBits;\r
-    putBuffer |= bufferPutBuffer;\r
-\r
-    while (putBits >= 8) {\r
-      int c = ((putBuffer >> 16) & 0xFF);\r
-      out.writeByteAsInt(c);\r
-      if (c == 0xFF) {\r
-        out.writeByteAsInt(0);\r
-      }\r
-      putBuffer <<= 8;\r
-      putBits -= 8;\r
-    }\r
-    bufferPutBuffer = putBuffer;\r
-    bufferPutBits = putBits;\r
-\r
-  }\r
-\r
-  void flushBuffer(OC out) {\r
-    int putBuffer = bufferPutBuffer;\r
-    int putBits = bufferPutBits;\r
-    while (putBits >= 8) {\r
-      int c = ((putBuffer >> 16) & 0xFF);\r
-      out.writeByteAsInt(c);\r
-      if (c == 0xFF) {\r
-        out.writeByteAsInt(0);\r
-      }\r
-      putBuffer <<= 8;\r
-      putBits -= 8;\r
-    }\r
-    if (putBits > 0) {\r
-      int c = ((putBuffer >> 16) & 0xFF);\r
-      out.writeByteAsInt(c);\r
-    }\r
-  }\r
-\r
-  /*\r
-   * Initialisation of the Huffman codes for Luminance and Chrominance.\r
-   * This code results in the same tables created in the IJG Jpeg-6a\r
-   * library.\r
-   */\r
-\r
-  private void initHuf() {\r
-    dc_matrix0 = new int[12][2];\r
-    dc_matrix1 = new int[12][2];\r
-    ac_matrix0 = new int[255][2];\r
-    ac_matrix1 = new int[255][2];\r
-    dc_matrix = AU.newInt3(2, -1);\r
-    ac_matrix = AU.newInt3(2, -1);\r
-    int p, l, i, lastp, si, code;\r
-    int[] huffsize = new int[257];\r
-    int[] huffcode = new int[257];\r
-\r
-    /*\r
-     * init of the DC values for the chrominance\r
-     * [][0] is the code   [][1] is the number of bit\r
-     */\r
-\r
-    p = 0;\r
-    for (l = 1; l <= 16; l++) {\r
-      //      for (i = 1; i <= bitsDCchrominance[l]; i++)\r
-      for (i = bitsDCchrominance[l]; --i >= 0;) {\r
-        huffsize[p++] = l; //that's an "el", not a "one"\r
-      }\r
-    }\r
-    huffsize[p] = 0;\r
-    lastp = p;\r
-\r
-    code = 0;\r
-    si = huffsize[0];\r
-    p = 0;\r
-    while (huffsize[p] != 0) {\r
-      while (huffsize[p] == si) {\r
-        huffcode[p++] = code;\r
-        code++;\r
-      }\r
-      code <<= 1;\r
-      si++;\r
-    }\r
-\r
-    for (p = 0; p < lastp; p++) {\r
-      dc_matrix1[valDCchrominance[p]][0] = huffcode[p];\r
-      dc_matrix1[valDCchrominance[p]][1] = huffsize[p];\r
-    }\r
-\r
-    /*\r
-     * Init of the AC huffman code for the chrominance\r
-     * matrix [][][0] is the code & matrix[][][1] is the number of bit needed\r
-     */\r
-\r
-    p = 0;\r
-    for (l = 1; l <= 16; l++) {\r
-      for (i = bitsACchrominance[l]; --i >= 0;)\r
-      //      for (i = 1; i <= bitsACchrominance[l]; i++)\r
-      {\r
-        huffsize[p++] = l;\r
-      }\r
-    }\r
-    huffsize[p] = 0;\r
-    lastp = p;\r
-\r
-    code = 0;\r
-    si = huffsize[0];\r
-    p = 0;\r
-    while (huffsize[p] != 0) {\r
-      while (huffsize[p] == si) {\r
-        huffcode[p++] = code;\r
-        code++;\r
-      }\r
-      code <<= 1;\r
-      si++;\r
-    }\r
-\r
-    for (p = 0; p < lastp; p++) {\r
-      ac_matrix1[valACchrominance[p]][0] = huffcode[p];\r
-      ac_matrix1[valACchrominance[p]][1] = huffsize[p];\r
-    }\r
-\r
-    /*\r
-     * init of the DC values for the luminance\r
-     * [][0] is the code   [][1] is the number of bit\r
-     */\r
-    p = 0;\r
-    for (l = 1; l <= 16; l++) {\r
-      //      for (i = 1; i <= bitsDCluminance[l]; i++)\r
-      for (i = bitsDCluminance[l]; --i >= 0;) {\r
-        huffsize[p++] = l;\r
-      }\r
-    }\r
-    huffsize[p] = 0;\r
-    lastp = p;\r
-\r
-    code = 0;\r
-    si = huffsize[0];\r
-    p = 0;\r
-    while (huffsize[p] != 0) {\r
-      while (huffsize[p] == si) {\r
-        huffcode[p++] = code;\r
-        code++;\r
-      }\r
-      code <<= 1;\r
-      si++;\r
-    }\r
-\r
-    for (p = 0; p < lastp; p++) {\r
-      dc_matrix0[valDCluminance[p]][0] = huffcode[p];\r
-      dc_matrix0[valDCluminance[p]][1] = huffsize[p];\r
-    }\r
-\r
-    /*\r
-     * Init of the AC huffman code for luminance\r
-     * matrix [][][0] is the code & matrix[][][1] is the number of bit\r
-     */\r
-\r
-    p = 0;\r
-    for (l = 1; l <= 16; l++) {\r
-      //      for (i = 1; i <= bitsACluminance[l]; i++)\r
-      for (i = bitsACluminance[l]; --i >= 0;) {\r
-        huffsize[p++] = l;\r
-      }\r
-    }\r
-    huffsize[p] = 0;\r
-    lastp = p;\r
-\r
-    code = 0;\r
-    si = huffsize[0];\r
-    p = 0;\r
-    while (huffsize[p] != 0) {\r
-      while (huffsize[p] == si) {\r
-        huffcode[p++] = code;\r
-        code++;\r
-      }\r
-      code <<= 1;\r
-      si++;\r
-    }\r
-    for (int q = 0; q < lastp; q++) {\r
-      ac_matrix0[valACluminance[q]][0] = huffcode[q];\r
-      ac_matrix0[valACluminance[q]][1] = huffsize[q];\r
-    }\r
-\r
-    dc_matrix[0] = dc_matrix0;\r
-    dc_matrix[1] = dc_matrix1;\r
-    ac_matrix[0] = ac_matrix0;\r
-    ac_matrix[1] = ac_matrix1;\r
-  }\r
-\r
-}\r
-\r
-/*\r
- * JpegInfo - Given an image, sets default information about it and divides\r
- * it into its constituant components, downsizing those that need to be.\r
- */\r
-\r
-class JpegObj {\r
-  String comment;\r
-  int imageHeight;\r
-  int imageWidth;\r
-  int blockWidth[];\r
-  int blockHeight[];\r
-\r
-  int precision = 8;\r
-  int numberOfComponents = 3;\r
-  float[][][] components;\r
-  int[] compID = { 1, 2, 3 };\r
-  int[] hsampFactor = { 1, 1, 1 };\r
-  int[] vsampFactor = { 1, 1, 1 };\r
-  int[] qtableNumber = { 0, 1, 1 };\r
-  int[] dctableNumber = { 0, 1, 1 };\r
-  int[] actableNumber = { 0, 1, 1 };\r
-  private boolean[] lastColumnIsDummy = { false, false, false };\r
-  private boolean[] lastRowIsDummy = { false, false, false };\r
-  int ss = 0;\r
-  int se = 63;\r
-  int ah = 0;\r
-  int al = 0;\r
-  private int compWidth[];\r
-  private int compHeight[];\r
-  private int maxHsampFactor;\r
-  private int maxVsampFactor;\r
-\r
-  public JpegObj() {\r
-    components = AU.newFloat3(numberOfComponents, -1);\r
-    compWidth = new int[numberOfComponents];\r
-    compHeight = new int[numberOfComponents];\r
-    blockWidth = new int[numberOfComponents];\r
-    blockHeight = new int[numberOfComponents];\r
-  }\r
-\r
-  /*\r
-   * This method creates and fills three arrays, Y, Cb, and Cr using the\r
-   * input image.\r
-   */\r
-\r
-  void getYCCArray(int[] pixels) {\r
-    // In order to minimize the chance that grabPixels will throw an exception\r
-    // it may be necessary to grab some pixels every few scanlines and process\r
-    // those before going for more.  The time expense may be prohibitive.\r
-    // However, for a situation where memory overhead is a concern, this may be\r
-    // the only choice.\r
-    maxHsampFactor = 1;\r
-    maxVsampFactor = 1;\r
-    for (int y = 0; y < numberOfComponents; y++) {\r
-      maxHsampFactor = Math.max(maxHsampFactor, hsampFactor[y]);\r
-      maxVsampFactor = Math.max(maxVsampFactor, vsampFactor[y]);\r
-    }\r
-    for (int y = 0; y < numberOfComponents; y++) {\r
-      compWidth[y] = (((imageWidth % 8 != 0) ? ((int) Math\r
-          .ceil(imageWidth / 8.0)) * 8 : imageWidth) / maxHsampFactor)\r
-          * hsampFactor[y];\r
-      if (compWidth[y] != ((imageWidth / maxHsampFactor) * hsampFactor[y])) {\r
-        lastColumnIsDummy[y] = true;\r
-      }\r
-      // results in a multiple of 8 for compWidth\r
-      // this will make the rest of the program fail for the unlikely\r
-      // event that someone tries to compress an 16 x 16 pixel image\r
-      // which would of course be worse than pointless\r
-      blockWidth[y] = (int) Math.ceil(compWidth[y] / 8.0);\r
-      compHeight[y] = (((imageHeight % 8 != 0) ? ((int) Math\r
-          .ceil(imageHeight / 8.0)) * 8 : imageHeight) / maxVsampFactor)\r
-          * vsampFactor[y];\r
-      if (compHeight[y] != ((imageHeight / maxVsampFactor) * vsampFactor[y])) {\r
-        lastRowIsDummy[y] = true;\r
-      }\r
-      blockHeight[y] = (int) Math.ceil(compHeight[y] / 8.0);\r
-    }\r
-    float Y[][] = new float[compHeight[0]][compWidth[0]];\r
-    float Cr1[][] = new float[compHeight[0]][compWidth[0]];\r
-    float Cb1[][] = new float[compHeight[0]][compWidth[0]];\r
-    //float Cb2[][] = new float[compHeight[1]][compWidth[1]];\r
-    //float Cr2[][] = new float[compHeight[2]][compWidth[2]];\r
-    for (int pt = 0, y = 0; y < imageHeight; ++y) {\r
-      for (int x = 0; x < imageWidth; ++x, pt++) {\r
-        int p = pixels[pt];\r
-        int r = ((p >> 16) & 0xff);\r
-        int g = ((p >> 8) & 0xff);\r
-        int b = (p & 0xff);\r
-        // The following three lines are a more correct color conversion but\r
-        // the current conversion technique is sufficient and results in a higher\r
-        // compression rate.\r
-        // Y[y][x] = 16 + (float)(0.8588*(0.299 * (float)r + 0.587 * (float)g + 0.114 * (float)b ));\r
-        // Cb1[y][x] = 128 + (float)(0.8784*(-0.16874 * (float)r - 0.33126 * (float)g + 0.5 * (float)b));\r
-        // Cr1[y][x] = 128 + (float)(0.8784*(0.5 * (float)r - 0.41869 * (float)g - 0.08131 * (float)b));\r
-        Y[y][x] = (float) ((0.299 * r + 0.587 * g + 0.114 * b));\r
-        Cb1[y][x] = 128 + (float) ((-0.16874 * r - 0.33126 * g + 0.5 * b));\r
-        Cr1[y][x] = 128 + (float) ((0.5 * r - 0.41869 * g - 0.08131 * b));\r
-      }\r
-    }\r
-\r
-    // Need a way to set the H and V sample factors before allowing downsampling.\r
-    // For now (04/04/98) downsampling must be hard coded.\r
-    // Until a better downsampler is implemented, this will not be done.\r
-    // Downsampling is currently supported.  The downsampling method here\r
-    // is a simple box filter.\r
-\r
-    components[0] = Y;\r
-    //        Cb2 = DownSample(Cb1, 1);\r
-    components[1] = Cb1;\r
-    //        Cr2 = DownSample(Cr1, 2);\r
-    components[2] = Cr1;\r
-  }\r
-  /*  \r
-    float[][] DownSample(float[][] C, int comp)\r
-    {\r
-      int inrow, incol;\r
-      int outrow, outcol;\r
-      float output[][];\r
-      int bias;\r
-      inrow = 0;\r
-      incol = 0;\r
-      int cHeight = compHeight[comp];\r
-      int cWidth = compWidth[comp];\r
-      output = new float[cHeight][cWidth];\r
-      \r
-      for (outrow = 0; outrow < cHeight; outrow++) {\r
-        bias = 1;\r
-        for (outcol = 0; outcol < cWidth; outcol++) {\r
-          output[outrow][outcol] = (C[inrow][incol++] + C[inrow++][incol--] \r
-                   + C[inrow][incol++] + C[inrow--][incol++] + bias)/(float)4.0;\r
-          bias ^= 3;\r
-        }\r
-        inrow += 2;\r
-        incol = 0;\r
-      }\r
-      return output;\r
-    }\r
-  */\r
-\r
-}\r
+// Version 1.0a
+// Copyright (C) 1998, James R. Weeks and BioElectroMech.
+// Visit BioElectroMech at www.obrador.com.  Email James@obrador.com.
+
+// See license.txt for details about the allowed used of this software.
+// This software is based in part on the work of the Independent JPEG Group.
+// See IJGreadme.txt for details about the Independent JPEG Group's license.
+
+// This encoder is inspired by the Java Jpeg encoder by Florian Raemy,
+// studwww.eurecom.fr/~raemy.
+// It borrows a great deal of code and structure from the Independent
+// Jpeg Group's Jpeg 6a library, Copyright Thomas G. Lane.
+// See license.txt for details 
+
+/*
+ * JpegEncoder and its associated classes are Copyright (c) 1998, James R. Weeks and BioElectroMech
+ * see(Jmol/src/com/obrador/license.txt)
+ * 
+ * javjs.img.JpegEncoder.java was adapted by Bob Hanson
+ * 
+ * for Jmol in the following ways:
+ * 
+ * 1) minor coding efficiencies were made in some for() loops.
+ * 2) methods not used by Jmol were commented out
+ * 3) method and variable signatures were modified to provide 
+ *    more appropriate method privacy.
+ * 4) additions for Java2Script compatibility 
+ * 
+ * Original files are maintained in the Jmol.src.com.obrador package, but
+ * these original files are not distributed with Jmol.
+ *   
+*/
+
+package javajs.img;
+
+import java.io.IOException;
+import java.util.Map;
+
+import javajs.img.ImageEncoder;
+import javajs.util.AU;
+import javajs.util.OC;
+
+/**
+ * JpegEncoder - The JPEG main program which performs a jpeg compression of an
+ * image.
+ * 
+ *  A system to allow the full Jmol state -- regardless of length -- 
+ *  to be encoded in a set of APP1 (FFE1) tags.
+ *  But we have to be careful about line ends for backward compatibility. 
+ *  This solution is not 100% effective, because some data lines may in principle be 
+ *  Very large and may not contain new lines for more than 65500 characters, 
+ *  But that would be very unusual. Perhaps a huge data set loaded from a 
+ *  string. Introduced in Jmol 12.1.36. Bob Hanson
+ *  
+ * See org.com.obrador.license.txt
+ * 
+ */
+
+public class JpgEncoder extends ImageEncoder {
+
+  // this string will GENERALLY appear at the end of lines and be escaped 
+  private static final int CONTINUE_MAX = 65500; // some room to spare here. 
+  private static final int CONTINUE_MAX_BUFFER = CONTINUE_MAX + 10; // never break up last 10 bytes
+
+  private JpegObj jpegObj;
+  private Huffman huf;
+  private DCT dct;
+  protected int defaultQuality = 100;
+  private String applicationTag;
+
+  public JpgEncoder() {
+
+  }
+
+  @Override
+  protected void setParams(Map<String, Object> params) {
+    if (quality <= 0)
+      quality = (params.containsKey("qualityJPG") ? ((Integer) params.get("qualityJPG")).intValue() : defaultQuality);
+    jpegObj = new JpegObj();
+    jpegObj.comment = (String) params.get("comment");
+    applicationTag = (String) params.get("jpgAppTag");
+  }
+
+  @Override
+  protected void generate() throws IOException {
+    jpegObj.imageWidth = width;
+    jpegObj.imageHeight = height;
+    dct = new DCT(quality);
+    huf = new Huffman(width, height);
+    if (jpegObj == null)
+      return;
+    jpegObj.getYCCArray(pixels);
+    String longState = writeHeaders(jpegObj, dct);
+    writeCompressedData(jpegObj, dct, huf);
+    writeMarker(eoi);
+    if (longState != null) {
+      byte[] b = longState.getBytes();
+      out.write(b, 0, b.length);
+    }
+  }
+
+  private void writeCompressedData(JpegObj jpegObj, DCT dct, Huffman huf) {
+    int i, j, r, c, a, b;
+    int comp, xpos, ypos, xblockoffset, yblockoffset;
+    float inputArray[][];
+    float dctArray1[][] = new float[8][8];
+    double dctArray2[][] = new double[8][8];
+    int dctArray3[] = new int[8 * 8];
+
+    /*
+     * This method controls the compression of the image.
+     * Starting at the upper left of the image, it compresses 8x8 blocks
+     * of data until the entire image has been compressed.
+     */
+
+    int lastDCvalue[] = new int[jpegObj.numberOfComponents];
+    //int zeroArray[] = new int[64]; // initialized to hold all zeros
+    //int Width = 0, Height = 0;
+    //int nothing = 0, not;
+    int minBlockWidth, minBlockHeight;
+    // This initial setting of MinBlockWidth and MinBlockHeight is done to
+    // ensure they start with values larger than will actually be the case.
+    minBlockWidth = ((huf.imageWidth % 8 != 0) ? (int) (Math
+        .floor(huf.imageWidth / 8.0) + 1) * 8 : huf.imageWidth);
+    minBlockHeight = ((huf.imageHeight % 8 != 0) ? (int) (Math
+        .floor(huf.imageHeight / 8.0) + 1) * 8 : huf.imageHeight);
+    for (comp = 0; comp < jpegObj.numberOfComponents; comp++) {
+      minBlockWidth = Math.min(minBlockWidth, jpegObj.blockWidth[comp]);
+      minBlockHeight = Math.min(minBlockHeight, jpegObj.blockHeight[comp]);
+    }
+    xpos = 0;
+    for (r = 0; r < minBlockHeight; r++) {
+      for (c = 0; c < minBlockWidth; c++) {
+        xpos = c * 8;
+        ypos = r * 8;
+        for (comp = 0; comp < jpegObj.numberOfComponents; comp++) {
+          //Width = JpegObj.BlockWidth[comp];
+          //Height = JpegObj.BlockHeight[comp];
+          inputArray = jpegObj.components[comp];
+          int vsampF = jpegObj.vsampFactor[comp];
+          int hsampF = jpegObj.hsampFactor[comp];
+          int qNumber = jpegObj.qtableNumber[comp];
+          int dcNumber = jpegObj.dctableNumber[comp];
+          int acNumber = jpegObj.actableNumber[comp];
+
+          for (i = 0; i < vsampF; i++) {
+            for (j = 0; j < hsampF; j++) {
+              xblockoffset = j * 8;
+              yblockoffset = i * 8;
+              for (a = 0; a < 8; a++) {
+                for (b = 0; b < 8; b++) {
+
+                  // I believe this is where the dirty line at the bottom of
+                  // the image is coming from.
+                  // I need to do a check here to make sure I'm not reading past
+                  // image data.
+                  // This seems to not be a big issue right now. (04/04/98)
+
+                  dctArray1[a][b] = inputArray[ypos + yblockoffset + a][xpos
+                      + xblockoffset + b];
+                }
+              }
+              // The following code commented out because on some images this technique
+              // results in poor right and bottom borders.
+              // if ((!JpegObj.lastColumnIsDummy[comp] || c < Width - 1) &&
+              //       (!JpegObj.lastRowIsDummy[comp] || r < Height - 1)) {
+              dctArray2 = DCT.forwardDCT(dctArray1);
+              dctArray3 = DCT.quantizeBlock(dctArray2, dct.divisors[qNumber]);
+              // }
+              // else {
+              //   zeroArray[0] = dctArray3[0];
+              //   zeroArray[0] = lastDCvalue[comp];
+              //   dctArray3 = zeroArray;
+              // }
+              huf.HuffmanBlockEncoder(out, dctArray3, lastDCvalue[comp],
+                  dcNumber, acNumber);
+              lastDCvalue[comp] = dctArray3[0];
+            }
+          }
+        }
+      }
+    }
+    huf.flushBuffer(out);
+  }
+
+  private static byte[] eoi = { (byte) 0xFF, (byte) 0xD9 };
+
+  private static byte[] jfif = new byte[] {
+  /* JFIF[0] =*/(byte) 0xff,
+  /* JFIF[1] =*/(byte) 0xe0,
+  /* JFIF[2] =*/0,
+  /* JFIF[3] =*/16,
+  /* JFIF[4] =*/(byte) 0x4a, //'J'
+      /* JFIF[5] =*/(byte) 0x46, //'F'
+      /* JFIF[6] =*/(byte) 0x49, //'I'
+      /* JFIF[7] =*/(byte) 0x46, //'F'
+      /* JFIF[8] =*/0,
+      /* JFIF[9] =*/1,
+      /* JFIF[10] =*/0,
+      /* JFIF[11] =*/0,
+      /* JFIF[12] =*/0,
+      /* JFIF[13] =*/1,
+      /* JFIF[14] =*/0,
+      /* JFIF[15] =*/1,
+      /* JFIF[16] =*/0,
+      /* JFIF[17] =*/0 };
+
+  private static byte[] soi = { (byte) 0xFF, (byte) 0xD8 };
+
+  private String writeHeaders(JpegObj jpegObj, DCT dct) {
+    int i, j, index, offset;
+    int tempArray[];
+
+    // the SOI marker
+    writeMarker(soi);
+
+    // The order of the following headers is quite inconsequential.
+    // the JFIF header
+    writeArray(jfif);
+
+    // Comment Header
+    String comment = null;
+    if (jpegObj.comment.length() > 0)
+      writeString(jpegObj.comment, (byte) 0xE1); // App data 1
+    writeString(
+        "JPEG Encoder Copyright 1998, James R. Weeks and BioElectroMech.\n\n",
+        (byte) 0xFE);
+
+    // The DQT header
+    // 0 is the luminance index and 1 is the chrominance index
+    byte dqt[] = new byte[134];
+    dqt[0] = (byte) 0xFF;
+    dqt[1] = (byte) 0xDB;
+    dqt[2] = 0;
+    dqt[3] = (byte) 132;
+    offset = 4;
+    for (i = 0; i < 2; i++) {
+      dqt[offset++] = (byte) ((0 << 4) + i);
+      tempArray = dct.quantum[i];
+      for (j = 0; j < 64; j++) {
+        dqt[offset++] = (byte) tempArray[Huffman.jpegNaturalOrder[j]];
+      }
+    }
+    writeArray(dqt);
+
+    // Start of Frame Header
+    byte sof[] = new byte[19];
+    sof[0] = (byte) 0xFF;
+    sof[1] = (byte) 0xC0;
+    sof[2] = 0;
+    sof[3] = 17;
+    sof[4] = (byte) jpegObj.precision;
+    sof[5] = (byte) ((jpegObj.imageHeight >> 8) & 0xFF);
+    sof[6] = (byte) ((jpegObj.imageHeight) & 0xFF);
+    sof[7] = (byte) ((jpegObj.imageWidth >> 8) & 0xFF);
+    sof[8] = (byte) ((jpegObj.imageWidth) & 0xFF);
+    sof[9] = (byte) jpegObj.numberOfComponents;
+    index = 10;
+    for (i = 0; i < sof[9]; i++) {
+      sof[index++] = (byte) jpegObj.compID[i];
+      sof[index++] = (byte) ((jpegObj.hsampFactor[i] << 4) + jpegObj.vsampFactor[i]);
+      sof[index++] = (byte) jpegObj.qtableNumber[i];
+    }
+    writeArray(sof);
+
+    WriteDHTHeader(Huffman.bitsDCluminance, Huffman.valDCluminance);
+    WriteDHTHeader(Huffman.bitsACluminance, Huffman.valACluminance);
+    WriteDHTHeader(Huffman.bitsDCchrominance, Huffman.valDCchrominance);
+    WriteDHTHeader(Huffman.bitsACchrominance, Huffman.valACchrominance);
+
+    // Start of Scan Header
+    byte sos[] = new byte[14];
+    sos[0] = (byte) 0xFF;
+    sos[1] = (byte) 0xDA;
+    sos[2] = 0;
+    sos[3] = 12;
+    sos[4] = (byte) jpegObj.numberOfComponents;
+    index = 5;
+    for (i = 0; i < sos[4]; i++) {
+      sos[index++] = (byte) jpegObj.compID[i];
+      sos[index++] = (byte) ((jpegObj.dctableNumber[i] << 4) + jpegObj.actableNumber[i]);
+    }
+    sos[index++] = (byte) jpegObj.ss;
+    sos[index++] = (byte) jpegObj.se;
+    sos[index++] = (byte) ((jpegObj.ah << 4) + jpegObj.al);
+    writeArray(sos);
+    return comment;
+  }
+
+  private void writeString(String s, byte id) {
+    int len = s.length();
+    int i0 = 0;
+    String suffix = applicationTag;
+    while (i0 < len) {
+      int nBytes = len - i0;
+      if (nBytes > CONTINUE_MAX_BUFFER) {
+        nBytes = CONTINUE_MAX;
+        // but break only at line breaks
+        int pt = s.lastIndexOf('\n', i0 + nBytes);
+        if (pt > i0 + 1)
+          nBytes = pt - i0;
+      }
+      if (i0 + nBytes == len)
+        suffix = "";
+      writeTag(nBytes + suffix.length(), id);
+      writeArray(s.substring(i0, i0 + nBytes).getBytes());
+      if (suffix.length() > 0)
+        writeArray(suffix.getBytes());
+      i0 += nBytes;
+    }
+  }
+
+  private void writeTag(int length, byte id) {
+    length += 2;
+    byte com[] = new byte[4];
+    com[0] = (byte) 0xFF;
+    com[1] = id;
+    com[2] = (byte) ((length >> 8) & 0xFF);
+    com[3] = (byte) (length & 0xFF);
+    writeArray(com);
+  }
+
+  void WriteDHTHeader(int[] bits, int[] val) {
+    // hansonr@stolaf.edu: simplified code.
+    byte[] dht;
+    int bytes = 0;
+    for (int j = 1; j < 17; j++)
+      bytes += bits[j];
+    dht = new byte[21 + bytes];
+    dht[0] = (byte) 0xFF;
+    dht[1] = (byte) 0xC4;
+    int index = 4;
+    for (int j = 0; j < 17; j++)
+      dht[index++] = (byte) bits[j];
+    for (int j = 0; j < bytes; j++)
+      dht[index++] = (byte) val[j];
+    dht[2] = (byte) (((index - 2) >> 8) & 0xFF);
+    dht[3] = (byte) ((index - 2) & 0xFF);
+    writeArray(dht);
+  }
+
+  void writeMarker(byte[] data) {
+    out.write(data, 0, 2);
+  }
+
+  void writeArray(byte[] data) {
+    out.write(data, 0, data.length);
+  }
+
+}
+
+// This class incorporates quality scaling as implemented in the JPEG-6a
+// library.
+
+/*
+ * DCT - A Java implementation of the Discreet Cosine Transform
+ */
+
+class DCT {
+
+  /**
+   * DCT Block Size - default 8
+   */
+  private final static int N = 8;
+  private final static int NN = N * N;
+
+  /**
+   * Image Quality (0-100) - default 80 (good image / good compression)
+   */
+  //public int QUALITY = 80;
+
+  int[][] quantum = AU.newInt2(2);
+  double[][] divisors = AU.newDouble2(2);
+
+  /**
+   * Quantitization Matrix for luminace.
+   */
+  private int quantum_luminance[] = new int[NN];
+  private double DivisorsLuminance[] = new double[NN];
+
+  /**
+   * Quantitization Matrix for chrominance.
+   */
+  private int quantum_chrominance[] = new int[NN];
+  private double DivisorsChrominance[] = new double[NN];
+
+  /**
+   * Constructs a new DCT object. Initializes the cosine transform matrix these
+   * are used when computing the DCT and it's inverse. This also initializes the
+   * run length counters and the ZigZag sequence. Note that the image quality
+   * can be worse than 25 however the image will be extemely pixelated, usually
+   * to a block size of N.
+   * 
+   * @param quality
+   *        The quality of the image (0 worst - 100 best)
+   * 
+   */
+  DCT(int quality) {
+    initMatrix(quality);
+  }
+
+  /*
+   * This method sets up the quantization matrix for luminance and
+   * chrominance using the Quality parameter.
+   */
+  private void initMatrix(int quality) {
+    // converting quality setting to that specified in the jpeg_quality_scaling
+    // method in the IJG Jpeg-6a C libraries
+
+    quality = (quality < 1 ? 1 : quality > 100 ? 100 : quality);
+    quality = (quality < 50 ? 5000 / quality : 200 - quality * 2);
+
+    // Creating the luminance matrix
+
+    quantum_luminance[0] = 16;
+    quantum_luminance[1] = 11;
+    quantum_luminance[2] = 10;
+    quantum_luminance[3] = 16;
+    quantum_luminance[4] = 24;
+    quantum_luminance[5] = 40;
+    quantum_luminance[6] = 51;
+    quantum_luminance[7] = 61;
+    quantum_luminance[8] = 12;
+    quantum_luminance[9] = 12;
+    quantum_luminance[10] = 14;
+    quantum_luminance[11] = 19;
+    quantum_luminance[12] = 26;
+    quantum_luminance[13] = 58;
+    quantum_luminance[14] = 60;
+    quantum_luminance[15] = 55;
+    quantum_luminance[16] = 14;
+    quantum_luminance[17] = 13;
+    quantum_luminance[18] = 16;
+    quantum_luminance[19] = 24;
+    quantum_luminance[20] = 40;
+    quantum_luminance[21] = 57;
+    quantum_luminance[22] = 69;
+    quantum_luminance[23] = 56;
+    quantum_luminance[24] = 14;
+    quantum_luminance[25] = 17;
+    quantum_luminance[26] = 22;
+    quantum_luminance[27] = 29;
+    quantum_luminance[28] = 51;
+    quantum_luminance[29] = 87;
+    quantum_luminance[30] = 80;
+    quantum_luminance[31] = 62;
+    quantum_luminance[32] = 18;
+    quantum_luminance[33] = 22;
+    quantum_luminance[34] = 37;
+    quantum_luminance[35] = 56;
+    quantum_luminance[36] = 68;
+    quantum_luminance[37] = 109;
+    quantum_luminance[38] = 103;
+    quantum_luminance[39] = 77;
+    quantum_luminance[40] = 24;
+    quantum_luminance[41] = 35;
+    quantum_luminance[42] = 55;
+    quantum_luminance[43] = 64;
+    quantum_luminance[44] = 81;
+    quantum_luminance[45] = 104;
+    quantum_luminance[46] = 113;
+    quantum_luminance[47] = 92;
+    quantum_luminance[48] = 49;
+    quantum_luminance[49] = 64;
+    quantum_luminance[50] = 78;
+    quantum_luminance[51] = 87;
+    quantum_luminance[52] = 103;
+    quantum_luminance[53] = 121;
+    quantum_luminance[54] = 120;
+    quantum_luminance[55] = 101;
+    quantum_luminance[56] = 72;
+    quantum_luminance[57] = 92;
+    quantum_luminance[58] = 95;
+    quantum_luminance[59] = 98;
+    quantum_luminance[60] = 112;
+    quantum_luminance[61] = 100;
+    quantum_luminance[62] = 103;
+    quantum_luminance[63] = 99;
+
+    AANscale(DivisorsLuminance, quantum_luminance, quality);
+
+    // Creating the chrominance matrix
+
+    for (int i = 4; i < 64; i++)
+      quantum_chrominance[i] = 99;
+
+    quantum_chrominance[0] = 17;
+    quantum_chrominance[1] = 18;
+    quantum_chrominance[2] = 24;
+    quantum_chrominance[3] = 47;
+
+    quantum_chrominance[8] = 18;
+    quantum_chrominance[9] = 21;
+    quantum_chrominance[10] = 26;
+    quantum_chrominance[11] = 66;
+
+    quantum_chrominance[16] = 24;
+    quantum_chrominance[17] = 26;
+    quantum_chrominance[18] = 56;
+
+    quantum_chrominance[24] = 47;
+    quantum_chrominance[25] = 66;
+
+    AANscale(DivisorsChrominance, quantum_chrominance, quality);
+
+    // quantum and Divisors are objects used to hold the appropriate matices
+
+    quantum[0] = quantum_luminance;
+    quantum[1] = quantum_chrominance;
+
+    divisors[0] = DivisorsLuminance;
+    divisors[1] = DivisorsChrominance;
+
+  }
+
+  private final static double[] AANscaleFactor = { 1.0, 1.387039845,
+      1.306562965, 1.175875602, 1.0, 0.785694958, 0.541196100, 0.275899379 };
+
+  static private void AANscale(double[] divisors, int[] values, int quality) {
+
+    for (int j = 0; j < 64; j++) {
+      int temp = (values[j] * quality + 50) / 100;
+      values[j] = (temp < 1 ? 1 : temp > 255 ? 255 : temp);
+    }
+
+    for (int i = 0, index = 0; i < 8; i++)
+      for (int j = 0; j < 8; j++, index++)
+        // The divisors for the LL&M method (the slow integer method used in
+        // jpeg 6a library).  This method is currently (04/04/98) incompletely
+        // implemented.
+        // DivisorsLuminance[index] = ((double) quantum_luminance[index]) << 3;
+        // The divisors for the AAN method (the float method used in jpeg 6a library.
+        divisors[index] = (0.125 / (values[index] * AANscaleFactor[i] * AANscaleFactor[j]));
+  }
+
+  /*
+   * This method preforms forward DCT on a block of image data using
+   * the literal method specified for a 2-D Discrete Cosine Transform.
+   * It is included as a curiosity and can give you an idea of the
+   * difference in the compression result (the resulting image quality)
+   * by comparing its output to the output of the AAN method below.
+   * It is ridiculously inefficient.
+   */
+
+  // For now the final output is unusable.  The associated quantization step
+  // needs some tweaking.  If you get this part working, please let me know.
+  /*
+    public double[][] forwardDCTExtreme(float input[][])
+    {
+      double output[][] = new double[N][N];
+      int v, u, x, y;
+      for (v = 0; v < 8; v++) {
+        for (u = 0; u < 8; u++) {
+          for (x = 0; x < 8; x++) {
+            for (y = 0; y < 8; y++) {
+              output[v][u] += input[x][y] * 
+                  Math.cos(((double)(2*x + 1)*(double)u*Math.PI)/16)*
+                  Math.cos(((double)(2*y + 1)*(double)v*Math.PI)/16);
+            }
+          }
+          output[v][u] *= (0.25)*((u == 0) ? (1.0/Math.sqrt(2)) : (double) 1.0)*((v == 0) ? (1.0/Math.sqrt(2)) : (double) 1.0);
+        }
+      }
+      return output;
+    }
+    
+  */
+  /*
+   * This method preforms a DCT on a block of image data using the AAN
+   * method as implemented in the IJG Jpeg-6a library.
+   */
+  static double[][] forwardDCT(float input[][]) {
+    double output[][] = new double[N][N];
+    double tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7;
+    double tmp10, tmp11, tmp12, tmp13;
+    double z1, z2, z3, z4, z5, z11, z13;
+    // Subtracts 128 from the input values
+    for (int i = 0; i < 8; i++)
+      for (int j = 0; j < 8; j++)
+        output[i][j] = (input[i][j] - 128.0);
+    // input[i][j] -= 128;
+
+    for (int i = 0; i < 8; i++) {
+      tmp0 = output[i][0] + output[i][7];
+      tmp7 = output[i][0] - output[i][7];
+      tmp1 = output[i][1] + output[i][6];
+      tmp6 = output[i][1] - output[i][6];
+      tmp2 = output[i][2] + output[i][5];
+      tmp5 = output[i][2] - output[i][5];
+      tmp3 = output[i][3] + output[i][4];
+      tmp4 = output[i][3] - output[i][4];
+
+      tmp10 = tmp0 + tmp3;
+      tmp13 = tmp0 - tmp3;
+      tmp11 = tmp1 + tmp2;
+      tmp12 = tmp1 - tmp2;
+
+      output[i][0] = tmp10 + tmp11;
+      output[i][4] = tmp10 - tmp11;
+
+      z1 = (tmp12 + tmp13) * 0.707106781;
+      output[i][2] = tmp13 + z1;
+      output[i][6] = tmp13 - z1;
+
+      tmp10 = tmp4 + tmp5;
+      tmp11 = tmp5 + tmp6;
+      tmp12 = tmp6 + tmp7;
+
+      z5 = (tmp10 - tmp12) * 0.382683433;
+      z2 = 0.541196100 * tmp10 + z5;
+      z4 = 1.306562965 * tmp12 + z5;
+      z3 = tmp11 * 0.707106781;
+
+      z11 = tmp7 + z3;
+      z13 = tmp7 - z3;
+
+      output[i][5] = z13 + z2;
+      output[i][3] = z13 - z2;
+      output[i][1] = z11 + z4;
+      output[i][7] = z11 - z4;
+    }
+
+    for (int i = 0; i < 8; i++) {
+      tmp0 = output[0][i] + output[7][i];
+      tmp7 = output[0][i] - output[7][i];
+      tmp1 = output[1][i] + output[6][i];
+      tmp6 = output[1][i] - output[6][i];
+      tmp2 = output[2][i] + output[5][i];
+      tmp5 = output[2][i] - output[5][i];
+      tmp3 = output[3][i] + output[4][i];
+      tmp4 = output[3][i] - output[4][i];
+
+      tmp10 = tmp0 + tmp3;
+      tmp13 = tmp0 - tmp3;
+      tmp11 = tmp1 + tmp2;
+      tmp12 = tmp1 - tmp2;
+
+      output[0][i] = tmp10 + tmp11;
+      output[4][i] = tmp10 - tmp11;
+
+      z1 = (tmp12 + tmp13) * 0.707106781;
+      output[2][i] = tmp13 + z1;
+      output[6][i] = tmp13 - z1;
+
+      tmp10 = tmp4 + tmp5;
+      tmp11 = tmp5 + tmp6;
+      tmp12 = tmp6 + tmp7;
+
+      z5 = (tmp10 - tmp12) * 0.382683433;
+      z2 = 0.541196100 * tmp10 + z5;
+      z4 = 1.306562965 * tmp12 + z5;
+      z3 = tmp11 * 0.707106781;
+
+      z11 = tmp7 + z3;
+      z13 = tmp7 - z3;
+
+      output[5][i] = z13 + z2;
+      output[3][i] = z13 - z2;
+      output[1][i] = z11 + z4;
+      output[7][i] = z11 - z4;
+    }
+
+    return output;
+  }
+
+  /*
+   * This method quantitizes data and rounds it to the nearest integer.
+   */
+  static int[] quantizeBlock(double inputData[][], double[] divisorsCode) {
+    int outputData[] = new int[NN];
+    for (int i = 0, index = 0; i < 8; i++)
+      for (int j = 0; j < 8; j++, index++)
+        // The second line results in significantly better compression.
+        outputData[index] = (int) (Math.round(inputData[i][j]
+            * divisorsCode[index]));
+    //                        outputData[index] = (int)(((inputData[i][j] * (((double[]) (Divisors[code]))[index])) + 16384.5) -16384);
+    return outputData;
+  }
+
+  /*
+   * This is the method for quantizing a block DCT'ed with forwardDCTExtreme
+   * This method quantitizes data and rounds it to the nearest integer.
+   */
+
+  /*
+
+    public double[][] forwardDCTExtreme(float input[][])
+    {
+      double output[][] = new double[N][N];
+      int v, u, x, y;
+      for (v = 0; v < 8; v++) {
+        for (u = 0; u < 8; u++) {
+          for (x = 0; x < 8; x++) {
+            for (y = 0; y < 8; y++) {
+              output[v][u] += input[x][y] * 
+                  Math.cos(((double)(2*x + 1)*(double)u*Math.PI)/16)*
+                  Math.cos(((double)(2*y + 1)*(double)v*Math.PI)/16);
+            }
+          }
+          output[v][u] *= (0.25)*((u == 0) ? (1.0/Math.sqrt(2)) : (double) 1.0)*((v == 0) ? (1.0/Math.sqrt(2)) : (double) 1.0);
+        }
+      }
+      return output;
+    }
+
+   */
+  /*
+    public int[] quantizeBlockExtreme(double inputData[][], int code)
+    {
+      int outputData[] = new int[NN];
+      int i, j;
+      int index;
+      index = 0;
+      for (i = 0; i < 8; i++) {
+        for (j = 0; j < 8; j++) {
+          outputData[index] = (int)(Math.round(inputData[i][j] / (((int[]) (quantum[code]))[index])));
+          index++;
+        }
+      }
+      
+      return outputData;
+    }
+  */
+}
+
+// This class was modified by James R. Weeks on 3/27/98.
+// It now incorporates Huffman table derivation as in the C jpeg library
+// from the IJG, Jpeg-6a.
+
+class Huffman {
+  private int bufferPutBits, bufferPutBuffer;
+  int imageHeight;
+  int imageWidth;
+  private int dc_matrix0[][];
+  private int ac_matrix0[][];
+  private int dc_matrix1[][];
+  private int ac_matrix1[][];
+  private int[][][] dc_matrix;
+  private int[][][] ac_matrix;
+  //private int code;
+  int numOfDCTables;
+  int numOfACTables;
+  final static int[] bitsDCluminance = { 0x00, 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0,
+      0, 0, 0, 0, 0 };
+  final static int[] valDCluminance = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
+  final static int[] bitsDCchrominance = { 0x01, 0, 3, 1, 1, 1, 1, 1, 1, 1, 1,
+      1, 0, 0, 0, 0, 0 };
+  final static int[] valDCchrominance = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
+  final static int[] bitsACluminance = { 0x10, 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4,
+      4, 0, 0, 1, 0x7d };
+  final static int[] valACluminance = { 0x01, 0x02, 0x03, 0x00, 0x04, 0x11,
+      0x05, 0x12, 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07, 0x22, 0x71,
+      0x14, 0x32, 0x81, 0x91, 0xa1, 0x08, 0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52,
+      0xd1, 0xf0, 0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16, 0x17, 0x18,
+      0x19, 0x1a, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x34, 0x35, 0x36, 0x37,
+      0x38, 0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x53,
+      0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67,
+      0x68, 0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x83,
+      0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96,
+      0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9,
+      0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3,
+      0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6,
+      0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8,
+      0xe9, 0xea, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa };
+  final static int[] bitsACchrominance = { 0x11, 0, 2, 1, 2, 4, 4, 3, 4, 7, 5,
+      4, 4, 0, 1, 2, 0x77 };
+  final static int[] valACchrominance = { 0x00, 0x01, 0x02, 0x03, 0x11, 0x04,
+      0x05, 0x21, 0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71, 0x13, 0x22,
+      0x32, 0x81, 0x08, 0x14, 0x42, 0x91, 0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33,
+      0x52, 0xf0, 0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34, 0xe1, 0x25,
+      0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x35, 0x36,
+      0x37, 0x38, 0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a,
+      0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x63, 0x64, 0x65, 0x66,
+      0x67, 0x68, 0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a,
+      0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x92, 0x93, 0x94,
+      0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
+      0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba,
+      0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4,
+      0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7,
+      0xe8, 0xe9, 0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa };
+
+  /*
+   * jpegNaturalOrder[i] is the natural-order position of the i'th element
+   * of zigzag order.
+   */
+  final static int[] jpegNaturalOrder = { 0, 1, 8, 16, 9, 2, 3, 10, 17, 24, 32,
+      25, 18, 11, 4, 5, 12, 19, 26, 33, 40, 48, 41, 34, 27, 20, 13, 6, 7, 14,
+      21, 28, 35, 42, 49, 56, 57, 50, 43, 36, 29, 22, 15, 23, 30, 37, 44, 51,
+      58, 59, 52, 45, 38, 31, 39, 46, 53, 60, 61, 54, 47, 55, 62, 63, };
+
+  Huffman(int width, int height) {
+    initHuf();
+    imageWidth = width;
+    imageHeight = height;
+
+  }
+
+  /**
+   * HuffmanBlockEncoder run length encodes and Huffman encodes the quantized
+   * data.
+   * 
+   * @param out
+   * @param zigzag
+   * @param prec
+   * @param dcCode
+   * @param acCode
+   **/
+
+  void HuffmanBlockEncoder(OC out, int zigzag[], int prec,
+                           int dcCode, int acCode) {
+    int temp, temp2, nbits, k, r, i;
+
+    numOfDCTables = 2;
+    numOfACTables = 2;
+
+    int[][] matrixDC = dc_matrix[dcCode];
+    int[][] matrixAC = ac_matrix[acCode];
+
+    // The DC portion
+
+    temp = temp2 = zigzag[0] - prec;
+    if (temp < 0) {
+      temp = -temp;
+      temp2--;
+    }
+    nbits = 0;
+    while (temp != 0) {
+      nbits++;
+      temp >>= 1;
+    }
+    //        if (nbits > 11) nbits = 11;
+    bufferIt(out, matrixDC[nbits][0], matrixDC[nbits][1]);
+    // The arguments in bufferIt are code and size.
+    if (nbits != 0) {
+      bufferIt(out, temp2, nbits);
+    }
+
+    // The AC portion
+
+    r = 0;
+
+    for (k = 1; k < 64; k++) {
+      if ((temp = zigzag[jpegNaturalOrder[k]]) == 0) {
+        r++;
+      } else {
+        while (r > 15) {
+          bufferIt(out, matrixAC[0xF0][0], matrixAC[0xF0][1]);
+          r -= 16;
+        }
+        temp2 = temp;
+        if (temp < 0) {
+          temp = -temp;
+          temp2--;
+        }
+        nbits = 1;
+        while ((temp >>= 1) != 0) {
+          nbits++;
+        }
+        i = (r << 4) + nbits;
+        bufferIt(out, matrixAC[i][0], matrixAC[i][1]);
+        bufferIt(out, temp2, nbits);
+
+        r = 0;
+      }
+    }
+
+    if (r > 0) {
+      bufferIt(out, matrixAC[0][0], matrixAC[0][1]);
+    }
+
+  }
+
+  // Uses an integer long (32 bits) buffer to store the Huffman encoded bits
+  // and sends them to out by the byte.
+
+  void bufferIt(OC out, int code, int size) {
+    int putBuffer = code;
+    int putBits = bufferPutBits;
+
+    putBuffer &= (1 << size) - 1;
+    putBits += size;
+    putBuffer <<= 24 - putBits;
+    putBuffer |= bufferPutBuffer;
+
+    while (putBits >= 8) {
+      int c = ((putBuffer >> 16) & 0xFF);
+      out.writeByteAsInt(c);
+      if (c == 0xFF) {
+        out.writeByteAsInt(0);
+      }
+      putBuffer <<= 8;
+      putBits -= 8;
+    }
+    bufferPutBuffer = putBuffer;
+    bufferPutBits = putBits;
+
+  }
+
+  void flushBuffer(OC out) {
+    int putBuffer = bufferPutBuffer;
+    int putBits = bufferPutBits;
+    while (putBits >= 8) {
+      int c = ((putBuffer >> 16) & 0xFF);
+      out.writeByteAsInt(c);
+      if (c == 0xFF) {
+        out.writeByteAsInt(0);
+      }
+      putBuffer <<= 8;
+      putBits -= 8;
+    }
+    if (putBits > 0) {
+      int c = ((putBuffer >> 16) & 0xFF);
+      out.writeByteAsInt(c);
+    }
+  }
+
+  /*
+   * Initialisation of the Huffman codes for Luminance and Chrominance.
+   * This code results in the same tables created in the IJG Jpeg-6a
+   * library.
+   */
+
+  private void initHuf() {
+    dc_matrix0 = new int[12][2];
+    dc_matrix1 = new int[12][2];
+    ac_matrix0 = new int[255][2];
+    ac_matrix1 = new int[255][2];
+    dc_matrix = AU.newInt3(2, -1);
+    ac_matrix = AU.newInt3(2, -1);
+    int p, l, i, lastp, si, code;
+    int[] huffsize = new int[257];
+    int[] huffcode = new int[257];
+
+    /*
+     * init of the DC values for the chrominance
+     * [][0] is the code   [][1] is the number of bit
+     */
+
+    p = 0;
+    for (l = 1; l <= 16; l++) {
+      //      for (i = 1; i <= bitsDCchrominance[l]; i++)
+      for (i = bitsDCchrominance[l]; --i >= 0;) {
+        huffsize[p++] = l; //that's an "el", not a "one"
+      }
+    }
+    huffsize[p] = 0;
+    lastp = p;
+
+    code = 0;
+    si = huffsize[0];
+    p = 0;
+    while (huffsize[p] != 0) {
+      while (huffsize[p] == si) {
+        huffcode[p++] = code;
+        code++;
+      }
+      code <<= 1;
+      si++;
+    }
+
+    for (p = 0; p < lastp; p++) {
+      dc_matrix1[valDCchrominance[p]][0] = huffcode[p];
+      dc_matrix1[valDCchrominance[p]][1] = huffsize[p];
+    }
+
+    /*
+     * Init of the AC huffman code for the chrominance
+     * matrix [][][0] is the code & matrix[][][1] is the number of bit needed
+     */
+
+    p = 0;
+    for (l = 1; l <= 16; l++) {
+      for (i = bitsACchrominance[l]; --i >= 0;)
+      //      for (i = 1; i <= bitsACchrominance[l]; i++)
+      {
+        huffsize[p++] = l;
+      }
+    }
+    huffsize[p] = 0;
+    lastp = p;
+
+    code = 0;
+    si = huffsize[0];
+    p = 0;
+    while (huffsize[p] != 0) {
+      while (huffsize[p] == si) {
+        huffcode[p++] = code;
+        code++;
+      }
+      code <<= 1;
+      si++;
+    }
+
+    for (p = 0; p < lastp; p++) {
+      ac_matrix1[valACchrominance[p]][0] = huffcode[p];
+      ac_matrix1[valACchrominance[p]][1] = huffsize[p];
+    }
+
+    /*
+     * init of the DC values for the luminance
+     * [][0] is the code   [][1] is the number of bit
+     */
+    p = 0;
+    for (l = 1; l <= 16; l++) {
+      //      for (i = 1; i <= bitsDCluminance[l]; i++)
+      for (i = bitsDCluminance[l]; --i >= 0;) {
+        huffsize[p++] = l;
+      }
+    }
+    huffsize[p] = 0;
+    lastp = p;
+
+    code = 0;
+    si = huffsize[0];
+    p = 0;
+    while (huffsize[p] != 0) {
+      while (huffsize[p] == si) {
+        huffcode[p++] = code;
+        code++;
+      }
+      code <<= 1;
+      si++;
+    }
+
+    for (p = 0; p < lastp; p++) {
+      dc_matrix0[valDCluminance[p]][0] = huffcode[p];
+      dc_matrix0[valDCluminance[p]][1] = huffsize[p];
+    }
+
+    /*
+     * Init of the AC huffman code for luminance
+     * matrix [][][0] is the code & matrix[][][1] is the number of bit
+     */
+
+    p = 0;
+    for (l = 1; l <= 16; l++) {
+      //      for (i = 1; i <= bitsACluminance[l]; i++)
+      for (i = bitsACluminance[l]; --i >= 0;) {
+        huffsize[p++] = l;
+      }
+    }
+    huffsize[p] = 0;
+    lastp = p;
+
+    code = 0;
+    si = huffsize[0];
+    p = 0;
+    while (huffsize[p] != 0) {
+      while (huffsize[p] == si) {
+        huffcode[p++] = code;
+        code++;
+      }
+      code <<= 1;
+      si++;
+    }
+    for (int q = 0; q < lastp; q++) {
+      ac_matrix0[valACluminance[q]][0] = huffcode[q];
+      ac_matrix0[valACluminance[q]][1] = huffsize[q];
+    }
+
+    dc_matrix[0] = dc_matrix0;
+    dc_matrix[1] = dc_matrix1;
+    ac_matrix[0] = ac_matrix0;
+    ac_matrix[1] = ac_matrix1;
+  }
+
+}
+
+/*
+ * JpegInfo - Given an image, sets default information about it and divides
+ * it into its constituant components, downsizing those that need to be.
+ */
+
+class JpegObj {
+  String comment;
+  int imageHeight;
+  int imageWidth;
+  int blockWidth[];
+  int blockHeight[];
+
+  int precision = 8;
+  int numberOfComponents = 3;
+  float[][][] components;
+  int[] compID = { 1, 2, 3 };
+  int[] hsampFactor = { 1, 1, 1 };
+  int[] vsampFactor = { 1, 1, 1 };
+  int[] qtableNumber = { 0, 1, 1 };
+  int[] dctableNumber = { 0, 1, 1 };
+  int[] actableNumber = { 0, 1, 1 };
+  private boolean[] lastColumnIsDummy = { false, false, false };
+  private boolean[] lastRowIsDummy = { false, false, false };
+  int ss = 0;
+  int se = 63;
+  int ah = 0;
+  int al = 0;
+  private int compWidth[];
+  private int compHeight[];
+  private int maxHsampFactor;
+  private int maxVsampFactor;
+
+  public JpegObj() {
+    components = AU.newFloat3(numberOfComponents, -1);
+    compWidth = new int[numberOfComponents];
+    compHeight = new int[numberOfComponents];
+    blockWidth = new int[numberOfComponents];
+    blockHeight = new int[numberOfComponents];
+  }
+
+  /*
+   * This method creates and fills three arrays, Y, Cb, and Cr using the
+   * input image.
+   */
+
+  void getYCCArray(int[] pixels) {
+    // In order to minimize the chance that grabPixels will throw an exception
+    // it may be necessary to grab some pixels every few scanlines and process
+    // those before going for more.  The time expense may be prohibitive.
+    // However, for a situation where memory overhead is a concern, this may be
+    // the only choice.
+    maxHsampFactor = 1;
+    maxVsampFactor = 1;
+    for (int y = 0; y < numberOfComponents; y++) {
+      maxHsampFactor = Math.max(maxHsampFactor, hsampFactor[y]);
+      maxVsampFactor = Math.max(maxVsampFactor, vsampFactor[y]);
+    }
+    for (int y = 0; y < numberOfComponents; y++) {
+      compWidth[y] = (((imageWidth % 8 != 0) ? ((int) Math
+          .ceil(imageWidth / 8.0)) * 8 : imageWidth) / maxHsampFactor)
+          * hsampFactor[y];
+      if (compWidth[y] != ((imageWidth / maxHsampFactor) * hsampFactor[y])) {
+        lastColumnIsDummy[y] = true;
+      }
+      // results in a multiple of 8 for compWidth
+      // this will make the rest of the program fail for the unlikely
+      // event that someone tries to compress an 16 x 16 pixel image
+      // which would of course be worse than pointless
+      blockWidth[y] = (int) Math.ceil(compWidth[y] / 8.0);
+      compHeight[y] = (((imageHeight % 8 != 0) ? ((int) Math
+          .ceil(imageHeight / 8.0)) * 8 : imageHeight) / maxVsampFactor)
+          * vsampFactor[y];
+      if (compHeight[y] != ((imageHeight / maxVsampFactor) * vsampFactor[y])) {
+        lastRowIsDummy[y] = true;
+      }
+      blockHeight[y] = (int) Math.ceil(compHeight[y] / 8.0);
+    }
+    float Y[][] = new float[compHeight[0]][compWidth[0]];
+    float Cr1[][] = new float[compHeight[0]][compWidth[0]];
+    float Cb1[][] = new float[compHeight[0]][compWidth[0]];
+    //float Cb2[][] = new float[compHeight[1]][compWidth[1]];
+    //float Cr2[][] = new float[compHeight[2]][compWidth[2]];
+    for (int pt = 0, y = 0; y < imageHeight; ++y) {
+      for (int x = 0; x < imageWidth; ++x, pt++) {
+        int p = pixels[pt];
+        int r = ((p >> 16) & 0xff);
+        int g = ((p >> 8) & 0xff);
+        int b = (p & 0xff);
+        // The following three lines are a more correct color conversion but
+        // the current conversion technique is sufficient and results in a higher
+        // compression rate.
+        // Y[y][x] = 16 + (float)(0.8588*(0.299 * (float)r + 0.587 * (float)g + 0.114 * (float)b ));
+        // Cb1[y][x] = 128 + (float)(0.8784*(-0.16874 * (float)r - 0.33126 * (float)g + 0.5 * (float)b));
+        // Cr1[y][x] = 128 + (float)(0.8784*(0.5 * (float)r - 0.41869 * (float)g - 0.08131 * (float)b));
+        Y[y][x] = (float) ((0.299 * r + 0.587 * g + 0.114 * b));
+        Cb1[y][x] = 128 + (float) ((-0.16874 * r - 0.33126 * g + 0.5 * b));
+        Cr1[y][x] = 128 + (float) ((0.5 * r - 0.41869 * g - 0.08131 * b));
+      }
+    }
+
+    // Need a way to set the H and V sample factors before allowing downsampling.
+    // For now (04/04/98) downsampling must be hard coded.
+    // Until a better downsampler is implemented, this will not be done.
+    // Downsampling is currently supported.  The downsampling method here
+    // is a simple box filter.
+
+    components[0] = Y;
+    //        Cb2 = DownSample(Cb1, 1);
+    components[1] = Cb1;
+    //        Cr2 = DownSample(Cr1, 2);
+    components[2] = Cr1;
+  }
+  /*  
+    float[][] DownSample(float[][] C, int comp)
+    {
+      int inrow, incol;
+      int outrow, outcol;
+      float output[][];
+      int bias;
+      inrow = 0;
+      incol = 0;
+      int cHeight = compHeight[comp];
+      int cWidth = compWidth[comp];
+      output = new float[cHeight][cWidth];
+      
+      for (outrow = 0; outrow < cHeight; outrow++) {
+        bias = 1;
+        for (outcol = 0; outcol < cWidth; outcol++) {
+          output[outrow][outcol] = (C[inrow][incol++] + C[inrow++][incol--] 
+                   + C[inrow][incol++] + C[inrow--][incol++] + bias)/(float)4.0;
+          bias ^= 3;
+        }
+        inrow += 2;
+        incol = 0;
+      }
+      return output;
+    }
+  */
+
+}