bdelacretaz    2003/06/25 02:01:18

  Added:       src/java/org/apache/fop/rtf/rtflib/interfaces
                        ITableColumnsInfo.java
               src/java/org/apache/fop/rtf/rtflib/tools
                        DomToSaxAdapter.java ElementVisitor.java
                        ImageConstants.java ImageUtil.java
               src/java/org/apache/fop/rtf/rtflib/tools/jpeg DCT.java
                        Encoder.java Huffman.java JPEGException.java
                        JpegInfo.java
  Log:
  additional jfor packages donated to the FOP project
  
  Revision  Changes    Path
  1.1                  
xml-fop/src/java/org/apache/fop/rtf/rtflib/interfaces/ITableColumnsInfo.java
  
  Index: ITableColumnsInfo.java
  ===================================================================
  package org.apache.fop.rtf.rtflib.interfaces;
  
  /*-----------------------------------------------------------------------------
   * jfor - Open-Source XSL-FO to RTF converter - see www.jfor.org
   *
   * ====================================================================
   * jfor Apache-Style Software License.
   * Copyright (c) 2002 by the jfor project. All rights reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   * notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   * notice, this list of conditions and the following disclaimer in
   * the documentation and/or other materials provided with the
   * distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   * if any, must include the following acknowledgment:
   * "This product includes software developed
   * by the jfor project (http://www.jfor.org)."
   * Alternately, this acknowledgment may appear in the software itself,
   * if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The name "jfor" must not be used to endorse
   * or promote products derived from this software without prior written
   * permission.  For written permission, please contact [EMAIL PROTECTED]
   *
   * 5. Products derived from this software may not be called "jfor",
   * nor may "jfor" appear in their name, without prior written
   * permission of [EMAIL PROTECTED]
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE JFOR PROJECT OR ITS CONTRIBUTORS BE
   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
   * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
   * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
   * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
   * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   * ====================================================================
   * Contributor(s):
  -----------------------------------------------------------------------------*/
  
  /**  Used to get information about tables, for example when handling nested tables
   *     
   *  @author Bertrand Delacretaz [EMAIL PROTECTED]
   */
  
  //------------------------------------------------------------------------------
  // $Id: ITableColumnsInfo.java,v 1.1 2003/06/25 09:01:17 bdelacretaz Exp $
  // $Log: ITableColumnsInfo.java,v $
  // Revision 1.1  2003/06/25 09:01:17  bdelacretaz
  // additional jfor packages donated to the FOP project
  //
  // Revision 1.1  2002/08/12 09:40:02  bdelacretaz
  // V0.7.1dev-e, contributions from Boris Poudérous for number-columns-spanned
  // and vertical merging of tables cells.
  //
  //------------------------------------------------------------------------------
  
  public interface ITableColumnsInfo
  {
      float INVALID_COLUM_WIDTH = 200f;
      
      /** reset the column iteration index, meant to be called when creating a new row 
*/
      void selectFirstColumn();
  
      /** increment the column iteration index */
      void selectNextColumn();
  
      /** get current column width according to column iteration index
       *  @return INVALID_COLUMN_WIDTH if we cannot find the value
       */
      float getColumnWidth();
  
       /** return current column iteration index */
       int getColumnIndex();
  
       /** return number of columns */
       int getNumberOfColumns();
  }
  
  
  1.1                  
xml-fop/src/java/org/apache/fop/rtf/rtflib/tools/DomToSaxAdapter.java
  
  Index: DomToSaxAdapter.java
  ===================================================================
  package org.apache.fop.rtf.rtflib.tools;
  
  import java.io.IOException;
  import org.w3c.dom.*;
  import org.xml.sax.*;
  import org.xml.sax.helpers.AttributesImpl;
  
  /*-----------------------------------------------------------------------------
   * jfor - Open-Source XSL-FO to RTF converter - see www.jfor.org
   *
   * ====================================================================
   * jfor Apache-Style Software License.
   * Copyright (c) 2002 by the jfor project. All rights reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   * notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   * notice, this list of conditions and the following disclaimer in
   * the documentation and/or other materials provided with the
   * distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   * if any, must include the following acknowledgment:
   * "This product includes software developed
   * by the jfor project (http://www.jfor.org)."
   * Alternately, this acknowledgment may appear in the software itself,
   * if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The name "jfor" must not be used to endorse
   * or promote products derived from this software without prior written
   * permission.  For written permission, please contact [EMAIL PROTECTED]
   *
   * 5. Products derived from this software may not be called "jfor",
   * nor may "jfor" appear in their name, without prior written
   * permission of [EMAIL PROTECTED]
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE JFOR PROJECT OR ITS CONTRIBUTORS BE
   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
   * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
   * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
   * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
   * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   * ====================================================================
   * Contributor(s):
   *  Thanks to John Cowan ([EMAIL PROTECTED]) for his DomParser class on which parts 
   *  of this are based. <br>
  -----------------------------------------------------------------------------*/
  
  /**  Walks a DOM document and fires (some) SAX events to simulate parsing
   *  of the original document. <br>
   *  Written to use jfor with Cocoon 1.8. <br>
   *  Only SAX events that are used by the jfor Converter class are implemented. <br>
   *  Namespace URIs and local names are not implemented. <br>
   *  @author Bertrand Delacretaz [EMAIL PROTECTED]
   */
  
  //------------------------------------------------------------------------------
  // $Id: DomToSaxAdapter.java,v 1.1 2003/06/25 09:01:17 bdelacretaz Exp $
  // $Log: DomToSaxAdapter.java,v $
  // Revision 1.1  2003/06/25 09:01:17  bdelacretaz
  // additional jfor packages donated to the FOP project
  //
  // Revision 1.4  2002/07/12 08:08:31  bdelacretaz
  // License changed to jfor Apache-style license
  //
  // Revision 1.3  2001/08/31 07:51:01  bdelacretaz
  // MPL license text added + javadoc class comments corrected
  //
  // Revision 1.2  2001/08/30 10:21:50  bdelacretaz
  // Converter modified to be usable as a pure SAX ContentHandler for Cocoon 2 
integration
  // (thanks to Gianugo Rabellino, [EMAIL PROTECTED])
  //
  // Revision 1.1  2001/08/29 13:27:51  bdelacretaz
  // V0.4.1 - base package name changed to org.apache.fop.rtf.rtflib.jfor
  //
  // Revision 1.1.1.1  2001/08/02 12:53:48  bdelacretaz
  // initial SourceForge checkin of V0.1 code
  //
  //------------------------------------------------------------------------------
  
  public class DomToSaxAdapter
  extends ElementVisitor
  {
      private ContentHandler m_handler;
      private final Document m_doc;
      
      /** create an adapter for the given DOM Document */
      public DomToSaxAdapter(Document doc)
      {
          m_doc = doc;
      }
      
      /** walk the document and send SAX events to the given ContentHandler */
      public void simulateParsing(ContentHandler ch) throws IOException,SAXException
      {
          m_handler = ch;
          this.visit(m_doc.getDocumentElement());
          m_handler.endDocument();
      }
      
      /** called at the start of the visit of an Element */
      protected void startElement(Element e) throws IOException,SAXException
      {
          
m_handler.startElement(null,null,e.getTagName(),saxAttributes(e.getAttributes()));
      }
  
      /** called at the end of the visit of an Element */
      protected void endElement(Element e) throws IOException,SAXException
      {
          m_handler.endElement(null,null,e.getTagName());
      }
  
      /** called to visit a Text node */
      protected void visitText(Text t) throws IOException,SAXException
      {
          final String data = t.getNodeValue();
          final int datalen = data.length();
          final char [] array = new char[datalen];
          data.getChars(0, datalen, array, 0);
          m_handler.characters(array, 0, datalen);
      }
  
      /** called to visit a CDATASection node */
      protected void visitCDATA(CDATASection cds) throws IOException
      {
          // not needed by jfor
      }
  
      /** called to visit a Comment node */
      protected void visitComment(Comment c) throws IOException
      {
          // not needed by jfor
      }
  
      /** called to visit a Node that is not of the other types */
      protected void visitNode(Node n) throws IOException
      {
          // not needed by jfor
      }
      
      /** convert DOM attributes to SAX */
      private static Attributes saxAttributes(NamedNodeMap domAttr)
      {
          final String ATTR_TYPE = "CDATA";
          
          final AttributesImpl result = new AttributesImpl();
          for(int i=0; i < domAttr.getLength(); i++) {
              final Attr a = (Attr)domAttr.item(i);
              // namespace information is not provided, not needed by jfor
              result.addAttribute(null,null,a.getName(),ATTR_TYPE,a.getValue());
          }
          return result;
      }
  }
  
  
  1.1                  
xml-fop/src/java/org/apache/fop/rtf/rtflib/tools/ElementVisitor.java
  
  Index: ElementVisitor.java
  ===================================================================
  package org.apache.fop.rtf.rtflib.tools;
  
  import java.io.IOException;
  import org.w3c.dom.*;
  import org.xml.sax.SAXException;
  
  /*-----------------------------------------------------------------------------
   * jfor - Open-Source XSL-FO to RTF converter - see www.jfor.org
   *
   * ====================================================================
   * jfor Apache-Style Software License.
   * Copyright (c) 2002 by the jfor project. All rights reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   * notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   * notice, this list of conditions and the following disclaimer in
   * the documentation and/or other materials provided with the
   * distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   * if any, must include the following acknowledgment:
   * "This product includes software developed
   * by the jfor project (http://www.jfor.org)."
   * Alternately, this acknowledgment may appear in the software itself,
   * if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The name "jfor" must not be used to endorse
   * or promote products derived from this software without prior written
   * permission.  For written permission, please contact [EMAIL PROTECTED]
   *
   * 5. Products derived from this software may not be called "jfor",
   * nor may "jfor" appear in their name, without prior written
   * permission of [EMAIL PROTECTED]
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE JFOR PROJECT OR ITS CONTRIBUTORS BE
   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
   * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
   * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
   * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
   * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   * ====================================================================
   * Contributor(s):
  -----------------------------------------------------------------------------*/
  
  /**  Recursive visit of a DOM Element.
   *  @author Bertrand Delacretaz [EMAIL PROTECTED]
   */
  
  //------------------------------------------------------------------------------
  // $Id: ElementVisitor.java,v 1.1 2003/06/25 09:01:17 bdelacretaz Exp $
  // $Log: ElementVisitor.java,v $
  // Revision 1.1  2003/06/25 09:01:17  bdelacretaz
  // additional jfor packages donated to the FOP project
  //
  // Revision 1.3  2002/07/12 08:08:31  bdelacretaz
  // License changed to jfor Apache-style license
  //
  // Revision 1.2  2001/08/31 07:51:01  bdelacretaz
  // MPL license text added + javadoc class comments corrected
  //
  // Revision 1.1  2001/08/29 13:27:51  bdelacretaz
  // V0.4.1 - base package name changed to org.apache.fop.rtf.rtflib.jfor
  //
  // Revision 1.1.1.1  2001/08/02 12:53:48  bdelacretaz
  // initial SourceForge checkin of V0.1 code
  //
  //------------------------------------------------------------------------------
  
  public abstract class ElementVisitor
  {
      /** recursively visit element e */
      public final void visit(Element e)
          throws IOException,SAXException
      {
          if(e!=null) {
              startElement(e);
  
              final NodeList list = e.getChildNodes();
              for(int i=0; i < list.getLength(); i++) {
                  Node n = list.item(i);
                  if(n instanceof Element) {
                      visit((Element)n);
                  } else if(n instanceof CDATASection) {
                      visitCDATA((CDATASection)n);
                  } else if(n instanceof Text) {
                      visitText((Text)n);
                  } else if(n instanceof Comment) {
                      visitComment((Comment)n);
                  } else {
                      visitNode(n);
                  }
              }
  
              endElement(e);
          }
      }
  
      /** called at the start of the visit of an Element */
      protected abstract void startElement(Element e) throws IOException,SAXException;
  
      /** called at the end of the visit of an Element */
      protected abstract void endElement(Element e) throws IOException,SAXException;
  
      /** called to visit a Text node */
      protected abstract void visitText(Text t) throws IOException,SAXException;
  
      /** called to visit a CDATASection node */
      protected abstract void visitCDATA(CDATASection cds) throws 
IOException,SAXException;
  
      /** called to visit a Comment node */
      protected abstract void visitComment(Comment c) throws IOException,SAXException;
  
      /** called to visit a Node that is not of the other types */
      protected abstract void visitNode(Node n) throws IOException,SAXException;
  }
  
  
  1.1                  
xml-fop/src/java/org/apache/fop/rtf/rtflib/tools/ImageConstants.java
  
  Index: ImageConstants.java
  ===================================================================
  /*
   * File: ImageConstants.java
   *
   *
   * Date         Author                   Changes
   * Aug 16 01    Andreas Putz             Created
   * Aug 17 01    Andreas Putz             Extended with "gif"
   *
   * (c) 2001 skynamics AG    All rights reserved.
   */
  package org.apache.fop.rtf.rtflib.tools;
  
  import java.util.Hashtable;
  
  /*-----------------------------------------------------------------------------
   * jfor - Open-Source XSL-FO to RTF converter - see www.jfor.org
   *
   * ====================================================================
   * jfor Apache-Style Software License.
   * Copyright (c) 2002 by the jfor project. All rights reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   * notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   * notice, this list of conditions and the following disclaimer in
   * the documentation and/or other materials provided with the
   * distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   * if any, must include the following acknowledgment:
   * "This product includes software developed
   * by the jfor project (http://www.jfor.org)."
   * Alternately, this acknowledgment may appear in the software itself,
   * if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The name "jfor" must not be used to endorse
   * or promote products derived from this software without prior written
   * permission.  For written permission, please contact [EMAIL PROTECTED]
   *
   * 5. Products derived from this software may not be called "jfor",
   * nor may "jfor" appear in their name, without prior written
   * permission of [EMAIL PROTECTED]
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE JFOR PROJECT OR ITS CONTRIBUTORS BE
   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
   * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
   * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
   * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
   * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   * ====================================================================
   * Contributor(s):
  -----------------------------------------------------------------------------*/
  
  /** Here will be defined all supported image formats.
   *  This class belongs to the <fo:external-graphic> tag processing.
   *  @author <a href="mailto:[EMAIL PROTECTED]">Andreas Putz</a>
   */
  
  public class ImageConstants
  {
        //////////////////////////////////////////////////
        // @@ Symbolic constants
        //////////////////////////////////////////////////
  
        /** Defines the case, if image is not supported */
        public static int I_NOT_SUPPORTED = -1;
  
        public static int I_EMF = 0;
        public static int I_PNG = 1;
        public static int I_JPG = 2;
  
        /** Defines the RTF properties */
        public static String [] RTF_TAGS = new String []
                {
                        "emfblip", "pngblip", "jpegblip"
                };
  
        public static int I_TO_CONVERT_BASIS = 50;
        public static int I_GIF = 50;
        public static int I_JPG_C = 51;
  
        /** Defines the types for converting rtf supported image types */
        public static int [] CONVERT_TO = new int []
                {
                        I_JPG, I_JPG
                };
  
        /** EMF file extension */
        public static String EMF_EXT = "emf";
        /** PNG file extension */
        public static String PNG_EXT = "png";
        /** JPG file extension */
        public static String JPG_EXT = "jpg";
        /** JPEG file extension */
        public static String JPEG_EXT = "jpeg";
        /** GIF file extension */
        public static String GIF_EXT = "gif";
  
        /** Defines the file extensions and the RTF property belongs to */
        public static Hashtable SUPPORTED_IMAGE_TYPES = new Hashtable ();
        static
        {
                SUPPORTED_IMAGE_TYPES.put (EMF_EXT, new Integer (I_EMF));
                SUPPORTED_IMAGE_TYPES.put (PNG_EXT, new Integer (I_PNG));
                SUPPORTED_IMAGE_TYPES.put (JPG_EXT, new Integer (I_JPG_C));
                SUPPORTED_IMAGE_TYPES.put (JPEG_EXT,new Integer (I_JPG_C));
                SUPPORTED_IMAGE_TYPES.put (GIF_EXT, new Integer (I_GIF));
        }
  
        //////////////////////////////////////////////////
        // @@ Construction
        //////////////////////////////////////////////////
  
        /**
         * Private constructor.
         */
        private ImageConstants()
        {
        }
  }
  
  
  
  1.1                  xml-fop/src/java/org/apache/fop/rtf/rtflib/tools/ImageUtil.java
  
  Index: ImageUtil.java
  ===================================================================
  package org.apache.fop.rtf.rtflib.tools;
  
  /*-----------------------------------------------------------------------------
   * jfor - Open-Source XSL-FO to RTF converter - see www.jfor.org
   *
   * ====================================================================
   * jfor Apache-Style Software License.
   * Copyright (c) 2002 by the jfor project. All rights reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   * notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   * notice, this list of conditions and the following disclaimer in
   * the documentation and/or other materials provided with the
   * distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   * if any, must include the following acknowledgment:
   * "This product includes software developed
   * by the jfor project (http://www.jfor.org)."
   * Alternately, this acknowledgment may appear in the software itself,
   * if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The name "jfor" must not be used to endorse
   * or promote products derived from this software without prior written
   * permission.  For written permission, please contact [EMAIL PROTECTED]
   *
   * 5. Products derived from this software may not be called "jfor",
   * nor may "jfor" appear in their name, without prior written
   * permission of [EMAIL PROTECTED]
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE JFOR PROJECT OR ITS CONTRIBUTORS BE
   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
   * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
   * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
   * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
   * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   * ====================================================================
   * Contributor(s):
  -----------------------------------------------------------------------------*/
  
  /**  Misc.utilities for images handling
   *  This class belongs to the <fo:external-graphic> tag processing.
   *  @author <a href="mailto:[EMAIL PROTECTED]">Andreas Putz</a>
   */
  public class ImageUtil
  {
  
        //////////////////////////////////////////////////
        // @@ Construction
        //////////////////////////////////////////////////
  
        /**
         * Private constructor.
         */
        private ImageUtil ()
        {
        }
  
  
        //////////////////////////////////////////////////
        // @@ Public static methods
        //////////////////////////////////////////////////
  
        /**
         * Determines the digits from a string.
         *
         * @param value String with digits
         *
         * @return
         *  -1      There is no digit\n
         *  number  The digits as integer
         */
        public static int getInt (String value)
        {
                String retString = new String ();
                StringBuffer s = new StringBuffer (value);
                int len = s.length ();
  
                for (int i = 0; i < len; i++)
                {
                        if (Character.isDigit (s.charAt (i)))
                        {
                                retString += s.charAt (i);
                        }
                }
  
                if (retString.length () == 0)
                {
                        return -1;
                }
                else
                {
                        return Integer.parseInt (retString);
                }
        }
  
        /**
         * Checks the string for percent character at the end of string.
         *
         * @param value String with digits
         *
         * @return
         * true    The string contains a % value
         * false   Other string
         */
        public static boolean isPercent (String value)
        {
                if (value.endsWith ("%"))
                {
                        return true;
  
                }
  
                return false;
        }
  
        /**
         * Compares two hexadecimal values.
         *
         * @param pattern Target
         * @param data Data
         * @param searchAt Position to start compare
         * @param searchForward Direction to compare byte arrays
         *
         * @return
         *  true    If equal\n
         *  false   If different
         */
        public static boolean compareHexValues (byte[] pattern, byte[] data, int 
searchAt,
                                                                                       
 boolean searchForward)
        {
                if (searchAt >= data.length)
                {
                        return false;
  
                }
  
                int pLen = pattern.length;
  
                if (searchForward)
                {
                        if (pLen >= (data.length - searchAt))
                        {
                                return false;
  
                        }
  
                        for (int i = 0; i < pLen; i++)
                        {
                                if (pattern[i] != data[searchAt + i])
                                {
                                        return false;
                                }
                        }
  
                        return true;
                }
                else
                {
                        if (pLen > (searchAt + 1))
                        {
                                return false;
  
                        }
  
                        for (int i = 0; i < pLen; i++)
                        {
                                if (pattern[pLen - i - 1] != data[searchAt - i])
                                {
                                        return false;
                                }
                        }
  
                        return true;
                }
        }
  
        /**
         * Determines a integer value from a hexadecimal byte array.
         *
         * @param data Image
         * @param start Start index to read from
         * @param end End index until to read
         *
         * @return A number
         */
        public static int getIntFromByteArray (byte[] data, int startAt, int length,
                                                                                   
boolean searchForward)
        {
                int bit = 8;
                int bitMoving = length * bit;
                int retVal = 0;
  
                if (startAt >= data.length)
                {
                        return retVal;
  
                }
  
                if (searchForward)
                {
                        if (length >= (data.length - startAt))
                        {
                                return retVal;
  
                        }
  
                        for (int i = 0; i < length; i++)
                        {
                                bitMoving -= bit;
                                int iData = (int) data[startAt + i];
                                if (iData < 0)
                                        iData += 256;
                                retVal += iData << bitMoving;
                        }
                }
                else
                {
                        if (length > (startAt + 1))
                        {
                                return retVal;
  
                        }
  
                        for (int i = 0; i < length; i++)
                        {
                                bitMoving -= bit;
                                int iData = (int) data[startAt - i];
                                if (iData < 0)
                                        iData += 256;
                                retVal += iData << bitMoving;                   }
                }
  
                return retVal;
        }
  }
  
  
  1.1                  xml-fop/src/java/org/apache/fop/rtf/rtflib/tools/jpeg/DCT.java
  
  Index: DCT.java
  ===================================================================
  /**
   * File: DCT.java
   *
   *
   * Date         Author                   Changes
   * Aug 17 01    Andreas Putz             Created
   */
  package org.apache.fop.rtf.rtflib.tools.jpeg;
  
  
  /*-----------------------------------------------------------------------------
   * jfor - Open-Source XSL-FO to RTF converter - see www.jfor.org
   *
   * ====================================================================
   * jfor Apache-Style Software License.
   * Copyright (c) 2002 by the jfor project. All rights reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   * notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   * notice, this list of conditions and the following disclaimer in
   * the documentation and/or other materials provided with the
   * distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   * if any, must include the following acknowledgment:
   * "This product includes software developed
   * by the jfor project (http://www.jfor.org)."
   * Alternately, this acknowledgment may appear in the software itself,
   * if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The name "jfor" must not be used to endorse
   * or promote products derived from this software without prior written
   * permission.  For written permission, please contact [EMAIL PROTECTED]
   *
   * 5. Products derived from this software may not be called "jfor",
   * nor may "jfor" appear in their name, without prior written
   * permission of [EMAIL PROTECTED]
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE JFOR PROJECT OR ITS CONTRIBUTORS BE
   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
   * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
   * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
   * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
   * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   * ====================================================================
   * Contributor(s):
  -----------------------------------------------------------------------------*/
  
  /**
   * DCT - A Java implementation of the Discreet Cosine Transform.
   * This class incorporates quality scaling as implemented in the JPEG-6a
   * library.
   */
  class DCT
  {
        //////////////////////////////////////////////////
        // @@ Members
        //////////////////////////////////////////////////
  
        /** Quantum */
        private Object quantum[] = new Object[2];
        /** Divisors */
        private Object divisors[] = new Object[2];
  
  
        //////////////////////////////////////////////////
        // @@ Definitions
        //////////////////////////////////////////////////
  
        /** DCT Block Size - default 8 */
        private static int dctBlockSize = 8;
  
  
        //////////////////////////////////////////////////
        // @@ Construction
        //////////////////////////////////////////////////
  
        /**
         * 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) 80 ist the 
best result
         *
         */
        DCT (int quality)
        {
                initMatrix (quality);
        }
  
        /**
         * Dispose.
         */
        public void dispose ()
        {
                // nothing to dispose
        }
  
        //////////////////////////////////////////////////
        // @@ Member access
        //////////////////////////////////////////////////
  
        /**
         * Gets a value from quantum.
         * @param index Index
         * @return Interger array
         */
        int[] getQuantumArray (int index)
        {
                return (int[]) quantum [index];
        }
  
  
        //////////////////////////////////////////////////
        // @@ Methods
        //////////////////////////////////////////////////
  
        /**
         * This method quantitizes data and rounds it to the nearest integer.
         *
         * @param inputData
         * @param code
         *
         * @return Integer array
         */
        int[] quantizeBlock (double inputData[][], int code)
        {
                int outputData[] = new int[dctBlockSize * dctBlockSize];
                int i, j;
                int index;
  
                index = 0;
  
                for (i = 0; i < 8; i++)
                {
                        for (j = 0; j < 8; j++)
                        {
  
                                // The second line results in significantly better 
compression.
                                outputData[index] = (int) (Math.round (inputData[i][j]
                                                                                       
                    * (((double[]) (divisors[code]))[index])));
  
                                index++;
                        }
                }
  
                return outputData;
        }
  
  
        //////////////////////////////////////////////////
        // @@ Private methods
        //////////////////////////////////////////////////
  
        /**
         * This method sets up the quantization matrix for luminance and
         * chrominance using the Quality parameter.
         *
         * @param quality Quality of result image
         */
        private void initMatrix (int quality)
        {
                double[] AANscaleFactor =
                {
                        1.0, 1.387039845, 1.306562965, 1.175875602, 1.0, 0.785694958, 
0.541196100, 0.275899379
                };
  
  
                // converting quality setting to that specified in the 
jpeg_quality_scaling
                // method in the IJG Jpeg-6a C libraries
  
  
                if (quality <= 0)
                {
                        quality = 1;
                }
                else if (quality > 100)
                {
                        quality = 100;
                }
  
                if (quality < 50)
                {
                        quality = 5000 / quality;
                }
                else
                {
                        quality = 200 - quality * 2;
                }
  
                 // Creating quantitization matrix for luminance.
                int quantum_luminance[] = new int[]
                {
                         16, 11, 10, 16, 24, 40, 51, 61, 12, 12,
                         14, 19, 26, 58, 60, 55, 14, 13, 16, 24,
                         40, 57, 69, 56, 14, 17, 22, 29, 51, 87,
                         80, 62, 18, 22, 37, 56, 68,109,103, 77,
                         24, 35, 55, 64, 81,104,113, 92, 49, 64,
                         78, 87,103,121,120,101, 72, 92, 95, 98,
                        112,100,103, 99
                };
  
                for (int j = 0; j < 64; j++)
                {
                        int temp = (quantum_luminance[j] * quality + 50) / 100;
  
                        if (temp <= 0)
                        {
                                temp = 1;
                        }
  
                        if (temp > 255)
                        {
                                temp = 255;
                        }
  
                        quantum_luminance[j] = temp;
                }
  
                int index = 0;
  
                double divisorsLuminance[] = new double[dctBlockSize * dctBlockSize];
  
                for (int i = 0; i < 8; i++)
                {
                        for (int j = 0; j < 8; j++)
                        {
  
                                // 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.
                                divisorsLuminance[index] = (double) ((double) 1.0
                                                                                       
                  / ((double) quantum_luminance[index]
                                                                                       
                         * AANscaleFactor[i] * AANscaleFactor[j]
                                                                                       
                         * (double) 8.0));
                                index++;
                        }
                }
  
                // Creating the quantitization matrix for chrominance.
                int quantum_chrominance[] = new int[]
                {
                        17,18,24,47,99,99,99,99,18,21,
                        26,66,99,99,99,99,24,26,56,99,
                        99,99,99,99,47,66,99,99,99,99,
                        99,99,99,99,99,99,99,99,99,99,
                        99,99,99,99,99,99,99,99,99,99,
                        99,99,99,99,99,99,99,99,99,99,
                        99,99,99,99
                };
  
                for (int j = 0; j < 64; j++)
                {
                        int temp = (quantum_chrominance[j] * quality + 50) / 100;
  
                        if (temp <= 0)
                        {
                                temp = 1;
                        }
  
                        if (temp >= 255)
                        {
                                temp = 255;
                        }
  
                        quantum_chrominance[j] = temp;
                }
  
                index = 0;
  
                double divisorsChrominance[] = new double[dctBlockSize * dctBlockSize];
  
                for (int i = 0; i < 8; i++)
                {
                        for (int j = 0; j < 8; j++)
                        {
  
                                // 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.
                                // divisorsChrominance[index] = ((double) 
quantum_chrominance[index]) << 3;
                                // The divisors for the AAN method (the float method 
used in jpeg 6a library.
                                divisorsChrominance[index] = (double) ((double) 1.0
                                                                                       
                    / ((double) quantum_chrominance[index]
                                                                                       
                           * AANscaleFactor[i] * AANscaleFactor[j]
                                                                                       
                           * (double) 8.0));
                                index++;
                        }
                }
  
                // quantum and Divisors are objects used to hold the appropriate 
matices
  
                quantum[0] = quantum_luminance;
                divisors[0] = divisorsLuminance;
                quantum[1] = quantum_chrominance;
                divisors[1] = divisorsChrominance;
  
  
        }
  
        /**
         * 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.
         *
         * @param input Float array
         *
         * @return Double array
         */
        private double[][] forwardDCTExtreme (float input[][])
        {
                double output[][] = new double[dctBlockSize][dctBlockSize];
  
                for (int v = 0; v < 8; v++)
                {
                        for (int u = 0; u < 8; u++)
                        {
                                for (int x = 0; x < 8; x++)
                                {
                                        for (int y = 0; y < 8; y++)
                                        {
                                                output[v][u] +=
                                                        ((double) input[x][y])
                                                        * Math.cos (((double) (2 * x + 
1) * (double) u * Math.PI) / (double) 16)
                                                        * Math.cos (((double) (2 * y + 
1) * (double) v * Math.PI)
                                                                                / 
(double) 16);
                                        }
                                }
  
                                output[v][u] *= (double) (0.25)
                                                                * ((u == 0) ? 
((double) 1.0 / Math.sqrt (2)) : (double) 1.0)
                                                                * ((v == 0) ? 
((double) 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.
         *
         * @param input Float array
         *
         * @return Double array
         */
        double[][] forwardDCT (float input[][])
        {
                double output[][] = new double[dctBlockSize][dctBlockSize];
                double tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7;
                double tmp10, tmp11, tmp12, tmp13;
                double z1, z2, z3, z4, z5, z11, z13;
                int i;
                int j;
  
                // Subtracts 128 from the input values
                for (i = 0; i < 8; i++)
                {
                        for (j = 0; j < 8; j++)
                        {
                                output[i][j] = ((double) input[i][j] - (double) 128.0);
  
                                // input[i][j] -= 128;
  
                        }
                }
  
                for (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) * (double) 0.707106781;
                        output[i][2] = tmp13 + z1;
                        output[i][6] = tmp13 - z1;
  
                        tmp10 = tmp4 + tmp5;
                        tmp11 = tmp5 + tmp6;
                        tmp12 = tmp6 + tmp7;
  
                        z5 = (tmp10 - tmp12) * (double) 0.382683433;
                        z2 = ((double) 0.541196100) * tmp10 + z5;
                        z4 = ((double) 1.306562965) * tmp12 + z5;
                        z3 = tmp11 * ((double) 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 (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) * (double) 0.707106781;
                        output[2][i] = tmp13 + z1;
                        output[6][i] = tmp13 - z1;
  
                        tmp10 = tmp4 + tmp5;
                        tmp11 = tmp5 + tmp6;
                        tmp12 = tmp6 + tmp7;
  
                        z5 = (tmp10 - tmp12) * (double) 0.382683433;
                        z2 = ((double) 0.541196100) * tmp10 + z5;
                        z4 = ((double) 1.306562965) * tmp12 + z5;
                        z3 = tmp11 * ((double) 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 is the method for quantizing a block DCT'ed with forwardDCTExtreme
         * This method quantitizes data and rounds it to the nearest integer.
         *
         * @param inputData
         * @param code
         *
         * @return
         */
        private int[] quantizeBlockExtreme (double inputData[][], int code)
        {
                int outputData[] = new int[dctBlockSize * dctBlockSize];
                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]
                                                                           / (double) 
(((int[]) (quantum[code]))[index])));
                                index++;
                        }
                }
  
                return outputData;
        }
  }
  
  
  
  
  1.1                  
xml-fop/src/java/org/apache/fop/rtf/rtflib/tools/jpeg/Encoder.java
  
  Index: Encoder.java
  ===================================================================
  /**
   * File: Encoder.java
   *
   *
   * Date         Author                   Changes
   * Aug 16 01    Andreas Putz             Created
   * Aug 21 01    Andreas Putz             Bug fixed (bottom and right line)
   */
  package org.apache.fop.rtf.rtflib.tools.jpeg;
  
  
  import java.awt.Image;
  
  import java.awt.Component;
  import java.awt.MediaTracker;
  import java.io.BufferedOutputStream;
  import java.io.OutputStream;
  import java.io.IOException;
  
  import java.io.*;
  import java.net.URL;
  import java.awt.Toolkit;
  
  /*-----------------------------------------------------------------------------
   * jfor - Open-Source XSL-FO to RTF converter - see www.jfor.org
   *
   * ====================================================================
   * jfor Apache-Style Software License.
   * Copyright (c) 2002 by the jfor project. All rights reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   * notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   * notice, this list of conditions and the following disclaimer in
   * the documentation and/or other materials provided with the
   * distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   * if any, must include the following acknowledgment:
   * "This product includes software developed
   * by the jfor project (http://www.jfor.org)."
   * Alternately, this acknowledgment may appear in the software itself,
   * if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The name "jfor" must not be used to endorse
   * or promote products derived from this software without prior written
   * permission.  For written permission, please contact [EMAIL PROTECTED]
   *
   * 5. Products derived from this software may not be called "jfor",
   * nor may "jfor" appear in their name, without prior written
   * permission of [EMAIL PROTECTED]
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE JFOR PROJECT OR ITS CONTRIBUTORS BE
   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
   * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
   * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
   * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
   * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   * ====================================================================
   * Contributor(s):
   * @author Andreas Putz <[EMAIL PROTECTED]>
  -----------------------------------------------------------------------------*/
  
  /**
   * Encoder - The JPEG main program which performs a jpeg compression of
   * an gif or tif image.
   */
  public class Encoder extends Component
  {
  
        //////////////////////////////////////////////////
        // @@ Members
        //////////////////////////////////////////////////
  
        private BufferedOutputStream outStream = null;
        private JpegInfo jpegObj = null;
        private Huffman huf = null;
        private DCT dct = null;
  
        private int quality;
  
  
        //////////////////////////////////////////////////
        // @@ Static definitions
        //////////////////////////////////////////////////
  
        public 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,
        };
  
  
        //////////////////////////////////////////////////
        // @@ Construction
        //////////////////////////////////////////////////
  
        /**
         * Constructor.
         *
         * @param quality Percentage to compress
         * @param out Outputstream
         */
        public Encoder (int quality, OutputStream out)
        {
                outStream = outStream = new BufferedOutputStream (out);
                this.quality = quality;
        }
  
  
        //////////////////////////////////////////////////
        // @@ Public encode methods
        //////////////////////////////////////////////////
  
        /**
         * Encodes a JPEG.
         * @param image AWT Image
         *
         * @exception JPEGException On error
         */
        public void encodeJPEG (Image image) throws JPEGException
        {
                process (image);
        }
  
        /**
         * Encodes a JPEG.
         *
         * @param url URL of a gif or tif image
         *
         * @exception JPEGException On error
         */
        public void encodeJPEG (URL url) throws JPEGException
        {
                Image image = Toolkit.getDefaultToolkit ().getImage (url);
                process (image);
        }
  
        /**
         * Encodes a JPEG.
         *
         * @param data Byte array with data of a gif or tif image
         *
         * @exception JPEGException On error
         */
        public void encodeJPEG (byte[] data) throws JPEGException
        {
                Image image = Toolkit.getDefaultToolkit ().createImage (data);
                process (image);
        }
  
  
        //////////////////////////////////////////////////
        // @@ Private encode methods
        //////////////////////////////////////////////////
  
        /**
         * Encode Image process.
         *
         * @param image AWT Image
         *
         * @exception JPEGException On error
         */
        private void process (Image image) throws JPEGException
        {
                MediaTracker tracker = new MediaTracker (this);
                tracker.addImage (image, 0);
                try
                {
                  tracker.waitForID (0);
                }
                catch (InterruptedException e)
                {
                  // Got to do something?
                }
  
                /*
                 * Quality of the image.
                 * 0 to 100 and from bad image quality, high compression to good
                 * image quality low compression
                 */
                setQuality (quality);
  
                jpegObj = new JpegInfo (image);
  
                huf = new Huffman ();
  
                compress ();
  
                dispose ();
        }
  
  
        //////////////////////////////////////////////////
        // @@ Member access
        //////////////////////////////////////////////////
  
        /**
         * Gets the quality of jpeg image.
         *
         * return A value between 0 and 100
         */
        public int getQuality ()
        {
                return quality;
        }
  
        /**
         * Sets the quality of jpeg image.
         *
         * @param quality From 0 to 100 %
         */
        public void setQuality (int quality)
        {
                this.quality = quality;
                dct = new DCT (quality);
        }
  
  
        //////////////////////////////////////////////////
        // @@ Helpers
        //////////////////////////////////////////////////
  
        /**
         * Dispose.
         */
        private void dispose ()
        {
                if (jpegObj != null)
                {
                        jpegObj.dispose ();
                        jpegObj = null;
                }
                if (huf != null)
                {
                        huf.dispose ();
                        huf = null;
                }
                if (dct != null)
                {
                        dct.dispose ();
                        dct = null;
                }
        }
  
        /**
         * Compress the image and write to output stream.
         *
         * @exception JPEGException On error
         */
        private void compress () throws JPEGException
        {
                WriteHeaders (outStream);
                WriteCompressedData (outStream);
                WriteEOI (outStream);
  
                try
                {
                        outStream.flush ();
                }
                catch (IOException e)
                {
                        throw new JPEGException (e);
                }
        }
  
        /**
         * Writes the End Of Image marker.
         *
         * @param out Output stream to write out
         */
        private void WriteEOI (BufferedOutputStream out)
        {
                byte[] EndOfImage =
                {
                        (byte) 0xFF, (byte) 0xD9
                };
  
                WriteMarker (EndOfImage, out);
        }
  
        /**
         * Writes the header information.
         *
         * @param out Output stream to write out
         */
        private void WriteHeaders (BufferedOutputStream out)
        {
                int i, j, index, offset, length;
                int tempArray[];
  
                // the SOI marker
                byte[] startOfImage =
                {
                        (byte) 0xFF, (byte) 0xD8
                };
  
                WriteMarker (startOfImage, out);
  
                // The order of the following headers is quiet inconsequential.
                // the jFIF header
                byte jFIF[] = new byte[18];
  
                jFIF[0] = (byte) 0xff;
                jFIF[1] = (byte) 0xe0;
                jFIF[2] = (byte) 0x00;
                jFIF[3] = (byte) 0x10;
                jFIF[4] = (byte) 0x4a;
                jFIF[5] = (byte) 0x46;
                jFIF[6] = (byte) 0x49;
                jFIF[7] = (byte) 0x46;
                jFIF[8] = (byte) 0x00;
                jFIF[9] = (byte) 0x01;
                jFIF[10] = (byte) 0x00;
                jFIF[11] = (byte) 0x00;
                jFIF[12] = (byte) 0x00;
                jFIF[13] = (byte) 0x01;
                jFIF[14] = (byte) 0x00;
                jFIF[15] = (byte) 0x01;
                jFIF[16] = (byte) 0x00;
                jFIF[17] = (byte) 0x00;
  
                WriteArray (jFIF, out);
  
                // Comment Header
                String comment = new String ();
  
                comment = jpegObj.getComment ();
                length = comment.length ();
  
                byte cOM[] = new byte[length + 4];
  
                cOM[0] = (byte) 0xFF;
                cOM[1] = (byte) 0xFE;
                cOM[2] = (byte) ((length >> 8) & 0xFF);
                cOM[3] = (byte) (length & 0xFF);
  
                java.lang.System.arraycopy (jpegObj.getComment().getBytes (), 0, cOM, 
4,
                                                                        
jpegObj.getComment().length ());
                WriteArray (cOM, out);
  
                // 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] = (byte) 0x00;
                dQT[3] = (byte) 0x84;
                offset = 4;
  
                for (i = 0; i < 2; i++)
                {
                        dQT[offset++] = (byte) ((0 << 4) + i);
                        tempArray = dct.getQuantumArray (i);
  
                        for (j = 0; j < 64; j++)
                        {
                                dQT[offset++] = (byte) tempArray[jpegNaturalOrder[j]];
                        }
                }
  
                WriteArray (dQT, out);
  
                // Start of Frame Header
                byte startOfFrame[] = new byte[19];
  
                startOfFrame[0] = (byte) 0xFF;
                startOfFrame[1] = (byte) 0xC0;
                startOfFrame[2] = (byte) 0x00;
                startOfFrame[3] = (byte) 17;
                startOfFrame[4] = (byte) jpegObj.PRECISION;
                startOfFrame[5] = (byte) ((jpegObj.getImageHeight () >> 8) & 0xFF);
                startOfFrame[6] = (byte) ((jpegObj.getImageHeight ()) & 0xFF);
                startOfFrame[7] = (byte) ((jpegObj.getImageWidth () >> 8) & 0xFF);
                startOfFrame[8] = (byte) ((jpegObj.getImageWidth ()) & 0xFF);
                startOfFrame[9] = (byte) jpegObj.getNumberOfComponents ();
                index = 10;
  
                for (i = 0; i < startOfFrame[9]; i++)
                {
                        startOfFrame[index++] = (byte) jpegObj.COMP_ID[i];
                        startOfFrame[index++] = (byte) ((jpegObj.H_SAMP_FACTOR[i] << 
4) + jpegObj.V_SAMP_FACTOR[i]);
                        startOfFrame[index++] = (byte) jpegObj.Q_TABLE_NUMBER[i];
                }
  
                WriteArray (startOfFrame, out);
  
                // The dHT Header
                byte dHT1[], dHT2[], dHT3[], dHT4[];
                int bytes, temp, oldindex, intermediateindex;
  
                length = 2;
                index = 4;
                oldindex = 4;
                dHT1 = new byte[17];
                dHT4 = new byte[4];
                dHT4[0] = (byte) 0xFF;
                dHT4[1] = (byte) 0xC4;
  
                for (i = 0; i < 4; i++)
                {
                        bytes = 0;
                        dHT1[index++ - oldindex] = (byte) huf.getBits (i)[0];
  
                        for (j = 1; j < 17; j++)
                        {
                                temp = huf.getBits (i)[j];
                                dHT1[index++ - oldindex] = (byte) temp;
                                bytes += temp;
                        }
  
                        intermediateindex = index;
                        dHT2 = new byte[bytes];
  
                        for (j = 0; j < bytes; j++)
                        {
                                dHT2[index++ - intermediateindex] = (byte) huf.getVal 
(i)[j];
                        }
  
                        dHT3 = new byte[index];
  
                        java.lang.System.arraycopy (dHT4, 0, dHT3, 0, oldindex);
                        java.lang.System.arraycopy (dHT1, 0, dHT3, oldindex, 17);
                        java.lang.System.arraycopy (dHT2, 0, dHT3, oldindex + 17, 
bytes);
  
                        dHT4 = dHT3;
                        oldindex = index;
                }
  
                dHT4[2] = (byte) (((index - 2) >> 8) & 0xFF);
                dHT4[3] = (byte) ((index - 2) & 0xFF);
  
                WriteArray (dHT4, out);
  
  
                // Start of Scan Header
                byte SOS[] = new byte[14];
  
                SOS[0] = (byte) 0xFF;
                SOS[1] = (byte) 0xDA;
                SOS[2] = (byte) 0x00;
                SOS[3] = (byte) 12;
                SOS[4] = (byte) jpegObj.getNumberOfComponents ();
                index = 5;
  
                for (i = 0; i < SOS[4]; i++)
                {
                        SOS[index++] = (byte) jpegObj.COMP_ID[i];
                        SOS[index++] = (byte) ((jpegObj.D_C_TABLE_NUMBER[i] << 4) + 
jpegObj.A_C_TABLE_NUMBER[i]);
                }
  
                SOS[index++] = (byte) jpegObj.ss;
                SOS[index++] = (byte) jpegObj.se;
                SOS[index++] = (byte) ((jpegObj.ah << 4) + jpegObj.al);
  
                WriteArray (SOS, out);
  
        }
  
        /**
         * Writes the compressed data to the outputstream
         *
         * @param outputStream Stream to write out the jpeg data
         */
        private void WriteCompressedData (BufferedOutputStream outputStream)
        {
                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.getNumberOfComponents ()];
                int zeroArray[] = new int[64];          // initialized to hold all 
zeros
  
                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 = ((jpegObj.getImageWidth () % 8 != 0)
                                                 ? (int) (Math.floor ((double) 
jpegObj.getImageWidth () / 8.0) + 1) * 8 : jpegObj.getImageWidth ());
                minBlockHeight = ((jpegObj.getImageHeight () % 8 != 0)
                                                  ? (int) (Math.floor ((double) 
jpegObj.getImageHeight () / 8.0) + 1) * 8 : jpegObj.getImageHeight ());
  
                for (int comp = 0; comp < jpegObj.getNumberOfComponents (); comp++)
                {
                        minBlockWidth = Math.min (minBlockWidth, jpegObj.getBlockWidth 
(comp));
                        minBlockHeight = Math.min (minBlockHeight, 
jpegObj.getBlockHeight (comp));
                }
  
                for (int r = 0; r < minBlockHeight; r++)
                {
                        for (int c = 0; c < minBlockWidth; c++)
                        {
                                int xpos = c * 8;
                                int ypos = r * 8;
  
                                for (int comp = 0; comp < 
jpegObj.getNumberOfComponents (); comp++)
                                {
                        int blockWidth = jpegObj.getBlockWidth (comp);
                        int blockHeight = jpegObj.getBlockHeight (comp);
                                        float[][] inputArray = (float[][]) 
jpegObj.getComponent (comp);
  
                                        for (int i = 0; i < 
jpegObj.V_SAMP_FACTOR[comp]; i++)
                                        {
                                                for (int j = 0; j < 
jpegObj.H_SAMP_FACTOR[comp]; j++)
                                                {
                                                        int xblockoffset = j * 8;
                                                        int yblockoffset = i * 8;
  
                                                        for (int a = 0; a < 8; a++)
                                                        {
                                                                for (int 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.isLastColumnDummy (comp) || c < blockWidth - 1)
                                                                 && 
(!jpegObj.isLastRowDummy (comp) || r < blockHeight - 1))
                                                         {
                                                                                
dctArray2 = dct.forwardDCT (dctArray1);
                                                                                
dctArray3 = dct.quantizeBlock (dctArray2, jpegObj.Q_TABLE_NUMBER[comp]);
  
                                                         }
                                                         else
                                                         {
                                                                 zeroArray[0] = 
dctArray3[0];
                                                                 zeroArray[0] = 
lastDCvalue[comp];
                                                                 dctArray3 = zeroArray;
                                                         }
  
                                                        huf.encodeHuffmanBlock 
(outputStream, dctArray3, lastDCvalue[comp],
                                                                                       
                  jpegObj.D_C_TABLE_NUMBER[comp],
                                                                                       
                  jpegObj.A_C_TABLE_NUMBER[comp]);
  
                                                        lastDCvalue[comp] = 
dctArray3[0];
                                                }
                                        }
                                }
                        }
                }
  
                huf.flushBuffer (outputStream);
        }
  
        /**
         * Writes a marker.
         *
         * @param data Data to write
         * @param out Output stream to write out
         */
        private void WriteMarker (byte[] data, BufferedOutputStream out)
        {
                try
                {
                        out.write (data, 0, 2);
                }
                catch (IOException e)
                {
                        System.err.println ("Error: " + e.getMessage ());
                }
        }
  
        /**
         * Writes a array.
         *
         * @param data Data to write
         * @param out Output stream to write out
         */
        private void WriteArray (byte[] data, BufferedOutputStream out)
        {
                int i, length;
  
                try
                {
                        length = (((int) (data[2] & 0xFF)) << 8) + (int) (data[3] & 
0xFF) + 2;
  
                        out.write (data, 0, length);
                }
                catch (IOException e)
                {
                        System.out.println ("IO Error: " + e.getMessage ());
                }
        }
  
  
        //////////////////////////////////////////////////
        // @@ Main method
        //////////////////////////////////////////////////
  
        /**
         * Main method
         *
         * @param args Command line arguments
         */
        public static void main (String args[])
        {
                String string = new String ();
                int defaultQuality = 80;
  
                // Check to see if the input file name has one of the extensions:
                // .tif, .gif, .jpg
                // If not, print the standard use info.
                if (args.length < 2)
                {
                        StandardUsage ();
                }
  
                if (!args[0].endsWith (".jpg") &&!args[0].endsWith (".tif") 
&&!args[0].endsWith (".gif"))
                {
                        StandardUsage ();
  
                        // First check to see if there is an OutputFile argument.  If 
there isn't
                        // then name the file "InputFile".jpg
                        // Second check to see if the .jpg extension is on the 
OutputFile argument.
                        // If there isn't one, add it.
                        // Need to check for the existence of the output file.  If it 
exists already,
                        // rename the file with a # after the file name, then the .jpg 
extension.
                }
  
                if (args.length < 3)
                {
                        string = args[0].substring (0, args[0].lastIndexOf (".")) + 
".jpg";
                }
                else
                {
                        string = args[2];
  
                        if (string.endsWith (".tif") || string.endsWith (".gif"))
                        {
                                string = string.substring (0, string.lastIndexOf 
("."));
                        }
  
                        if (!string.endsWith (".jpg"))
                        {
                                string = string.concat (".jpg");
                        }
                }
  
                File outFile = new File (string);
  
                for (int i = 0; outFile.exists (); i++)
                {
                        outFile = new File (string.substring (0, string.lastIndexOf 
(".")) + i + ".jpg");
  
                        if (i > 100)
                        {
                                System.exit (0);
                        }
                }
  
                File inFile = new File (args[0]);
  
                try
                {
                        if (inFile.exists ())
                        {
                                int qual = defaultQuality;
                                FileOutputStream dataOut = null;
  
                                try
                                {
                                        dataOut = new FileOutputStream (outFile);
                                }
                                catch (IOException e)
                                {
                                }
  
                                try
                                {
                                        qual = Integer.parseInt (args[1]);
                                }
                                catch (NumberFormatException e)
                                {
                                        StandardUsage ();
                                }
  
                                Image image = Toolkit.getDefaultToolkit ().getImage 
(args[0]);
                                Encoder jpgEncoder = new Encoder (qual, dataOut);
                                jpgEncoder.encodeJPEG (image);
  
                                try
                                {
                                        dataOut.close ();
                                }
                                catch (IOException e)
                                {
                                }
                        }
                        else
                        {
                                System.out.println ("I couldn't find " + args[0] + ". 
Is it in another directory?");
                        }
                }
                catch (JPEGException e)
                {
                        e.printStackTrace ();
                }
                finally
                {
                        System.exit (0);
                }
        }
  
        /**
         * Standard usage method.
         */
        public static void StandardUsage()
        {
                System.out.println("Program usage: java Jpeg \"InputImage\".\"ext\" 
Quality [\"OutputFile\"[.jpg]]");
                System.exit(0);
        }
  }
  
  
  
  
  1.1                  
xml-fop/src/java/org/apache/fop/rtf/rtflib/tools/jpeg/Huffman.java
  
  Index: Huffman.java
  ===================================================================
  /**
   * File: Huffmann.java
   *
   *
   * Date         Author                   Changes
   * Aug 17 01    Andreas Putz             Created
   */
  package org.apache.fop.rtf.rtflib.tools.jpeg;
  
  import java.util.Vector;
  import java.io.BufferedOutputStream;
  import java.io.IOException;
  
  /*-----------------------------------------------------------------------------
   * jfor - Open-Source XSL-FO to RTF converter - see www.jfor.org
   *
   * ====================================================================
   * jfor Apache-Style Software License.
   * Copyright (c) 2002 by the jfor project. All rights reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   * notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   * notice, this list of conditions and the following disclaimer in
   * the documentation and/or other materials provided with the
   * distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   * if any, must include the following acknowledgment:
   * "This product includes software developed
   * by the jfor project (http://www.jfor.org)."
   * Alternately, this acknowledgment may appear in the software itself,
   * if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The name "jfor" must not be used to endorse
   * or promote products derived from this software without prior written
   * permission.  For written permission, please contact [EMAIL PROTECTED]
   *
   * 5. Products derived from this software may not be called "jfor",
   * nor may "jfor" appear in their name, without prior written
   * permission of [EMAIL PROTECTED]
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE JFOR PROJECT OR ITS CONTRIBUTORS BE
   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
   * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
   * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
   * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
   * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   * ====================================================================
   * Contributor(s):
  -----------------------------------------------------------------------------*/
  
  /**
   * 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
  {
        //////////////////////////////////////////////////
        // @@ Members
        //////////////////////////////////////////////////
  
        private int bufferPutBits;
        private int bufferPutBuffer;
  
        private Object DC_matrix[] = null;
        private Object AC_matrix[] = null;
  
        private Vector bits = null;
        private Vector val = null;
  
  
        //////////////////////////////////////////////////
        // @@ Definitions
        //////////////////////////////////////////////////
  
        private int[] bitsDCluminance =
        {
                0x00, 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0
        };
        private int[] valDCluminance =
        {
                0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
        };
        private int[] bitsDCchrominance =
        {
                0x01, 0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0
        };
        private int[] valDCchrominance =
        {
                0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
        };
        private int[] bitsACluminance =
        {
                0x10, 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 0x7d
        };
        private 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
        };
        private int[] bitsACchrominance =
        {
                0x11, 0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 0x77
        };
  
          private 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.
         */
        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,
        };
  
  
        //////////////////////////////////////////////////
        // @@ Construction
        //////////////////////////////////////////////////
  
        /**
         * The Huffman class constructor.
         */
        Huffman ()
        {
                bits = new Vector ();
  
                bits.addElement (bitsDCluminance);
                bits.addElement (bitsACluminance);
                bits.addElement (bitsDCchrominance);
                bits.addElement (bitsACchrominance);
  
                val = new Vector ();
  
                val.addElement (valDCluminance);
                val.addElement (valACluminance);
                val.addElement (valDCchrominance);
                val.addElement (valACchrominance);
                initHuf ();
        }
  
        /**
         * Dispose.
         */
        public void dispose ()
        {
                DC_matrix = null;
                AC_matrix = null;
                bits = null;
                val = null;
        }
  
  
        //////////////////////////////////////////////////
        // @@ Member access
        //////////////////////////////////////////////////
  
        /**
         * Gets the bits.
         * @param index Index
         * @return Integer array
         */
        int[] getBits (int index)
        {
                return (int []) bits.elementAt (index);
        }
  
        /**
         * Get the values.
         * @param index Index
         * @return Integer array
         */
         int [] getVal (int index)
         {
                return (int []) val.elementAt (index);
         }
  
  
        //////////////////////////////////////////////////
        // @@ Methods
        //////////////////////////////////////////////////
  
        /**
         * HuffmanBlockEncoder run length encodes and Huffman encodes the quantized
         * data.
         *
         * @param outStream Stream for output
         * @param zigzag
         * @param prec
         * @param dCcode
         * @param aCcode
         */
        void encodeHuffmanBlock (BufferedOutputStream outStream, int zigzag[], int 
prec,
                                                                        int dCcode, 
int aCcode)
        {
                // The DC portion
  
                int temp = zigzag[0] - prec;
                int temp2 = temp;
  
                if (temp < 0)
                {
                        temp = -temp;
                        temp2--;
                }
  
                int nbits = 0;
  
                while (temp != 0)
                {
                        nbits++;
                        temp >>= 1;
                }
  
                // if (nbits > 11) nbits = 11;
                bufferIt (outStream, ((int[][]) DC_matrix[dCcode])[nbits][0],
                                  ((int[][]) DC_matrix[dCcode])[nbits][1]);
  
                // The arguments in bufferIt are code and size.
                if (nbits != 0)
                {
                        bufferIt (outStream, temp2, nbits);
                }
  
                // The AC portion
  
                int r = 0;
  
                for (int k = 1; k < 64; k++)
                {
                        if ((temp = zigzag[jpegNaturalOrder[k]]) == 0)
                        {
                                r++;
                        }
                        else
                        {
                                while (r > 15)
                                {
                                        bufferIt (outStream, ((int[][]) 
AC_matrix[aCcode])[0xF0][0],
                                                          ((int[][]) 
AC_matrix[aCcode])[0xF0][1]);
  
                                        r -= 16;
                                }
  
                                temp2 = temp;
  
                                if (temp < 0)
                                {
                                        temp = -temp;
                                        temp2--;
                                }
  
                                nbits = 1;
  
                                while ((temp >>= 1) != 0)
                                {
                                        nbits++;
                                }
  
                                int i = (r << 4) + nbits;
  
                                bufferIt (outStream, ((int[][]) 
AC_matrix[aCcode])[i][0],
                                                  ((int[][]) AC_matrix[aCcode])[i][1]);
                                bufferIt (outStream, temp2, nbits);
  
                                r = 0;
                        }
                }
  
                if (r > 0)
                {
                        bufferIt (outStream, ((int[][]) AC_matrix[aCcode])[0][0],
                                          ((int[][]) AC_matrix[aCcode])[0][1]);
                }
  
        }
  
        /**
         * Uses an integer long (32 bits) buffer to store the Huffman encoded bits
         * and sends them to outStream by the byte.
         *
         * @param outStream Stream for output
         * @param code Code
         * @param size Size
         */
        void bufferIt (BufferedOutputStream outStream, 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);
  
                        try
                        {
                                outStream.write (c);
                        }
                        catch (IOException e)
                        {
                                System.out.println ("IO Error: " + e.getMessage ());
                        }
  
                        if (c == 0xFF)
                        {
                                try
                                {
                                        outStream.write (0);
                                }
                                catch (IOException e)
                                {
                                        System.out.println ("IO Error: " + 
e.getMessage ());
                                }
                        }
  
                        PutBuffer <<= 8;
                        PutBits -= 8;
                }
  
                bufferPutBuffer = PutBuffer;
                bufferPutBits = PutBits;
  
        }
  
        /**
         * Flushed the buffer to output stream.
         *
         * @param outStream Stream for output
         */
        void flushBuffer (BufferedOutputStream outStream)
        {
                int PutBuffer = bufferPutBuffer;
                int PutBits = bufferPutBits;
  
                while (PutBits >= 8)
                {
                        int c = ((PutBuffer >> 16) & 0xFF);
  
                        try
                        {
                                outStream.write (c);
                        }
                        catch (IOException e)
                        {
                                System.out.println ("IO Error: " + e.getMessage ());
                        }
  
                        if (c == 0xFF)
                        {
                                try
                                {
                                        outStream.write (0);
                                }
                                catch (IOException e)
                                {
                                        System.out.println ("IO Error: " + 
e.getMessage ());
                                }
                        }
  
                        PutBuffer <<= 8;
                        PutBits -= 8;
                }
  
                if (PutBits > 0)
                {
                        int c = ((PutBuffer >> 16) & 0xFF);
  
                        try
                        {
                                outStream.write (c);
                        }
                        catch (IOException e)
                        {
                                System.out.println ("IO Error: " + e.getMessage ());
                        }
                }
        }
  
        /**
         * Initialisation of the Huffman codes for Luminance and Chrominance.
         * This code results in the same tables created in the IJG Jpeg-6a
         * library.
         */
        void initHuf ()
        {
                int[][] DC_matrix0 = new int[12][2];
                int[][] DC_matrix1 = new int[12][2];
                int[][] AC_matrix0 = new int[255][2];
                int[][] AC_matrix1 = new int[255][2];
                DC_matrix = new Object[2];
                AC_matrix = new Object[2];
  
                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
                 */
  
                int p = 0;
  
                for (int l = 1; l <= 16; l++)
                {
                        for (int i = 1; i <= bitsDCchrominance[l]; i++)
                        {
                                huffsize[p++] = l;
                        }
                }
  
                huffsize[p] = 0;
                int lastp = p;
  
                int code = 0;
                int 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 hufmann code for the chrominance
                 * matrix [][][0] is the code & matrix[][][1] is the number of bit 
needed
                 */
  
                p = 0;
  
                for (int l = 1; l <= 16; l++)
                {
                        for (int 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 (int l = 1; l <= 16; l++)
                {
                        for (int i = 1; i <= bitsDCluminance[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++)
                {
                        DC_matrix0[valDCluminance[p]][0] = huffcode[p];
                        DC_matrix0[valDCluminance[p]][1] = huffsize[p];
                }
  
                /*
                 * Init of the AC hufmann code for luminance
                 * matrix [][][0] is the code & matrix[][][1] is the number of bit
                 */
  
                p = 0;
  
                for (int l = 1; l <= 16; l++)
                {
                        for (int i = 1; i <= bitsACluminance[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 (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;
        }
  }
  
  
  
  1.1                  
xml-fop/src/java/org/apache/fop/rtf/rtflib/tools/jpeg/JPEGException.java
  
  Index: JPEGException.java
  ===================================================================
  /**
   * File: JPEGException.java
   *
   *
   * Date         Author                   Changes
   * Aug 21 01    Andreas Putz             Created
   */
  package org.apache.fop.rtf.rtflib.tools.jpeg;
  
  import java.io.IOException;
  
  /*-----------------------------------------------------------------------------
   * jfor - Open-Source XSL-FO to RTF converter - see www.jfor.org
   *
   * ====================================================================
   * jfor Apache-Style Software License.
   * Copyright (c) 2002 by the jfor project. All rights reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   * notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   * notice, this list of conditions and the following disclaimer in
   * the documentation and/or other materials provided with the
   * distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   * if any, must include the following acknowledgment:
   * "This product includes software developed
   * by the jfor project (http://www.jfor.org)."
   * Alternately, this acknowledgment may appear in the software itself,
   * if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The name "jfor" must not be used to endorse
   * or promote products derived from this software without prior written
   * permission.  For written permission, please contact [EMAIL PROTECTED]
   *
   * 5. Products derived from this software may not be called "jfor",
   * nor may "jfor" appear in their name, without prior written
   * permission of [EMAIL PROTECTED]
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE JFOR PROJECT OR ITS CONTRIBUTORS BE
   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
   * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
   * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
   * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
   * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   * ====================================================================
   * Contributor(s):
  -----------------------------------------------------------------------------*/
  
  /**
   * Exception if error occured in JPEG encoder.
   *  @author Andreas Putz [EMAIL PROTECTED]
   */
  
  //------------------------------------------------------------------------------
  // $Id: JPEGException.java,v 1.1 2003/06/25 09:01:18 bdelacretaz Exp $
  // $Log: JPEGException.java,v $
  // Revision 1.1  2003/06/25 09:01:18  bdelacretaz
  // additional jfor packages donated to the FOP project
  //
  // Revision 1.3  2002/07/12 08:08:31  bdelacretaz
  // License changed to jfor Apache-style license
  //
  // Revision 1.2  2001/08/31 07:51:01  bdelacretaz
  // MPL license text added + javadoc class comments corrected
  //
  // Revision 1.1  2001/08/29 13:27:51  bdelacretaz
  // V0.4.1 - base package name changed to org.apache.fop.rtf.rtflib.jfor
  //
  // Revision 1.4  2001/08/27 20:35:05  putzi
  // Bigfix in external graphic, insert alignment in paragraph, Converter option.
  //
  // Revision 1.2  2001/08/21 15:58:02  bdelacretaz
  // V0.3.5, jpeg and gif support added
  //
  // Revision 1.1  2001/08/21 15:18:01  bdelacretaz
  // V0.3.4 - bookmarks and links added
  //
  // Revision 1.1.1.1  2001/08/02 12:53:45  bdelacretaz
  // initial SourceForge checkin of V0.1 code
  //
  //------------------------------------------------------------------------------
  
  public class JPEGException extends IOException
  {
        /** Exception */
        private Exception e = null;
        /** Message */
        private String message = null;
  
        /**
         * Constructor.
         *
         * @param message Error message
         */
        public JPEGException (String message)
        {
                super (message);
                this.message = message;
        }
  
        /**
         * Constructor.
         *
         * @param message Error message
         */
        public JPEGException (Exception e)
        {
                this (e.getMessage ());
                this.e = e;
        }
  
        /**
         * Gets the exception.
         *
         * @return Exception
         */
        public Exception getException ()
        {
                return e;
        }
  
        /**
         * Modifies the message.
         *
         * @param message Message
         */
         public void setMessage (String message)
         {
                this.message = message;
         }
  }
  
  
  
  
  1.1                  
xml-fop/src/java/org/apache/fop/rtf/rtflib/tools/jpeg/JpegInfo.java
  
  Index: JpegInfo.java
  ===================================================================
  /**
   * File: JpegInfo.java
   *
   *
   * Date         Author                   Changes
   * Aug 17 01    Andreas Putz             Created
   * Aug 21 01    Andreas Putz             Bug fixed (bottom and right line)
   */
  package org.apache.fop.rtf.rtflib.tools.jpeg;
  
  import java.awt.Image;
  import java.awt.image.PixelGrabber;
  import java.awt.AWTException;
  
  /*-----------------------------------------------------------------------------
   * jfor - Open-Source XSL-FO to RTF converter - see www.jfor.org
   *
   * ====================================================================
   * jfor Apache-Style Software License.
   * Copyright (c) 2002 by the jfor project. All rights reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   * notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   * notice, this list of conditions and the following disclaimer in
   * the documentation and/or other materials provided with the
   * distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   * if any, must include the following acknowledgment:
   * "This product includes software developed
   * by the jfor project (http://www.jfor.org)."
   * Alternately, this acknowledgment may appear in the software itself,
   * if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The name "jfor" must not be used to endorse
   * or promote products derived from this software without prior written
   * permission.  For written permission, please contact [EMAIL PROTECTED]
   *
   * 5. Products derived from this software may not be called "jfor",
   * nor may "jfor" appear in their name, without prior written
   * permission of [EMAIL PROTECTED]
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE JFOR PROJECT OR ITS CONTRIBUTORS BE
   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
   * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
   * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
   * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
   * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   * ====================================================================
   * Contributor(s):
  -----------------------------------------------------------------------------*/
  
  /**
   * JpegInfo - Given an image, sets default information about it and divides
   * it into its constituant components, downsizing those that need to be.
   */
  class JpegInfo
  {
  
        //////////////////////////////////////////////////
        // @@ Constants
        //////////////////////////////////////////////////
  
        /**
         * Default value of precision
         */
        static int PRECISION = 8;
  
        static int[] COMP_ID =
        {
                1, 2, 3
        };
        static int[] H_SAMP_FACTOR =
        {
                1, 1, 1
        };
        static int[] V_SAMP_FACTOR =
        {
                1, 1, 1
        };
        static int[] Q_TABLE_NUMBER =
        {
                0, 1, 1
        };
        static int[] D_C_TABLE_NUMBER =
        {
                0, 1, 1
        };
        static int[] A_C_TABLE_NUMBER =
        {
                0, 1, 1
        };
  
        static int ss = 0;
        static int se = 63;
        static int ah = 0;
        static int al = 0;
  
  
        //////////////////////////////////////////////////
        // @@ Members
        //////////////////////////////////////////////////
  
        /**
         * Image comment
         */
        private String comment;
  
        /**
         * Image height
         */
        private int imageHeight;
  
        /**
         * Image widht
         */
        private int imageWidth;
  
        /**
         * Image block width
         */
        private int[] blockWidth;
  
        /**
         * Image block height
         */
        private int[] blockHeight;
  
        /**
         * Image
         */
        private Image imageObj = null;
  
        // Default settings
  
        /**
         * Number of components
         */
        private int numberOfComponents = 3;
  
        /**
         * Components
         */
        private Object[] components = null;
  
        /**
         * Width of components
         */
        private int[] compWidth = null;
  
        /**
         * Height of components
         */
        private int[] compHeight = null;
  
        /**
         * maximum of horizontal sample factor
         */
        private int maxHsampFactor;
  
        /**
         * maximum of vertical sample factor
         */
        private int maxVsampFactor;
  
        /** Array for the dummy column */
        private boolean[] lastColumnIsDummy = {false, false, false};
        /** Array for the dummy row */
        private boolean[] lastRowIsDummy = {false, false, false};
  
  
        //////////////////////////////////////////////////
        // @@ Construction
        //////////////////////////////////////////////////
  
        /**
         * Constructor.
         *
         * @param image AWT image
         */
        JpegInfo (Image image)
        {
                imageObj = image;
                components = new Object[numberOfComponents];
                compWidth = new int[numberOfComponents];
                compHeight = new int[numberOfComponents];
                blockWidth = new int[numberOfComponents];
                blockHeight = new int[numberOfComponents];
                imageWidth = image.getWidth (null);
                imageHeight = image.getHeight (null);
                comment = "JPEG Encoder, skynamics AG, Munich";
  
                getYCCArray ();
        }
  
        /**
         * Dispose.
         */
        public void dispose ()
        {
                comment = null;
                blockWidth = null;
                blockHeight = null;
                imageObj = null;
                components = null;
                compWidth = null;
                compHeight = null;
        }
  
        //////////////////////////////////////////////////
        // @@ Member access
        //////////////////////////////////////////////////
  
        /**
         * Adds a comment to the existent comment.
         *
         * @param comment Comment to add
         */
        void setComment (String comment)
        {
                this.comment.concat (comment);
        }
  
        /**
         * Gets the comment.
         * @return Comment
         */
        String getComment ()
        {
                return comment;
        }
  
        /**
         * Gets the image height.
         * @return Height
         */
        int getImageHeight ()
        {
                return this.imageHeight;
        }
  
        /**
         * Gets the image width.
         * @return Width
         */
        int getImageWidth ()
        {
                return this.imageWidth;
        }
  
        /**
         * Gets the block width.
         * @param index Block index
         * @return Width
         */
        int getBlockWidth (int index)
        {
                return blockWidth[index];
        }
  
        /**
         * Gets the block height.
         * @param index Component index
         * @return Height
         */
        int getBlockHeight (int index)
        {
                return blockHeight[index];
        }
  
        /**
         * Gets the number of components.
         * @return Number
         */
        int getNumberOfComponents ()
        {
                return numberOfComponents;
        }
  
        /**
         * Get component by component index.
         * @param index Component index
         * @return Object of component
         */
        Object getComponent (int index)
        {
                return this.components[index];
        }
  
        /**
         * Checks, whether last columns is as dummy checked.
         *
         * @param i Index
         * @return
         *  true    If checked\n
         *  false   Is no dummy column
         */
        public boolean isLastColumnDummy (int i)
        {
                return lastColumnIsDummy [i];
        }
  
        /**
         * Checks, whether last row is as dummy checked.
         *
         * @param i Index
         * @return
         *  true    If checked\n
         *  false   Is no dummy row
         */
        public boolean isLastRowDummy (int i)
        {
                return lastRowIsDummy [i];
        }
  
  
        //////////////////////////////////////////////////
        // @@ Helpers
        //////////////////////////////////////////////////
  
        /**
         * This method creates and fills three arrays, Y, Cb, and Cr using the
         * input image.
         */
        private void getYCCArray ()
        {
                int values[] = new int[imageWidth * imageHeight];
                int r, g, b, y, x;
  
                // 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.
                PixelGrabber grabber = new PixelGrabber (imageObj.getSource (), 0, 0, 
imageWidth,
                                                                                       
          imageHeight, values, 0, imageWidth);
  
                maxHsampFactor = 1;
                maxVsampFactor = 1;
  
                for (y = 0; y < numberOfComponents; y++)
                {
                        maxHsampFactor = Math.max (maxHsampFactor, H_SAMP_FACTOR[y]);
                        maxVsampFactor = Math.max (maxVsampFactor, V_SAMP_FACTOR[y]);
                }
  
                for (y = 0; y < numberOfComponents; y++)
                {
                        compWidth[y] =
                                (((imageWidth % 8 != 0) ? ((int) Math
                                .ceil ((double) imageWidth
                                           / 8.0)) * 8 : imageWidth) / maxHsampFactor) 
* H_SAMP_FACTOR[y];
  
                        if (compWidth[y] != ((imageWidth / maxHsampFactor) * 
H_SAMP_FACTOR[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 ((double) compWidth[y] / 8.0);
                        compHeight[y] =
                                (((imageHeight % 8 != 0) ? ((int) Math
                                .ceil ((double) imageHeight
                                           / 8.0)) * 8 : imageHeight) / 
maxVsampFactor) * V_SAMP_FACTOR[y];
  
                        if (compHeight[y] != ((imageHeight / maxVsampFactor) * 
V_SAMP_FACTOR[y]))
                        {
                                lastRowIsDummy[y] = true;
                        }
  
  
                        blockHeight[y] = (int) Math.ceil ((double) compHeight[y] / 
8.0);
                }
  
                try
                {
                        grabber.grabPixels ();
                }
                catch (InterruptedException e)
                {
                        System.err.println ("Error: " + e.getMessage ());
                }
  
                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]];
                int index = 0;
  
                for (y = 0; y < imageHeight; ++y)
                {
                        for (x = 0; x < imageWidth; ++x)
                        {
                                r = ((values[index] >> 16) & 0xff);
                                g = ((values[index] >> 8) & 0xff);
                                b = (values[index] & 0xff);
  
                                Y[y][x] = (float) ((0.299 * (float) r + 0.587 * 
(float) g + 0.114 * (float) b));
                                Cb1[y][x] = 128
                                                        + (float) ((-0.16874 * (float) 
r - 0.33126 * (float) g
                                                                                + 0.5 
* (float) b));
                                Cr1[y][x] = 128
                                                        + (float) ((0.5 * (float) r - 
0.41869 * (float) g
                                                                                - 
0.08131 * (float) b));
                                index++;
                        }
                }
  
                components[0] = Y;
                components[1] = Cb1;
                components[2] = Cr1;
        }
  }
  
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to