deweese     2002/09/04 12:20:21

  Modified:    resources/org/apache/batik/apps/rasterizer/resources
                        Messages.properties
               sources/org/apache/batik/apps/rasterizer Main.java
                        SVGConverter.java
               sources/org/apache/batik/ext/awt/image/rendered
                        IndexImage.java
               sources/org/apache/batik/transcoder/image PNGTranscoder.java
  Log:
  1) PNGTranscoder can now produce 1,2,4 & 8 bit indexed PNGs.
     I should mention that my median cut adaptive pallete alg is
     pretty pathetic for 1 & 2 bit images, and mostly pathetic for
     4 bit.
     For the low bit depths there should be a better alg used.
  
  Submitted by: Huang Haitao
  Reviewed by: Thomas DeWeese
  
  Revision  Changes    Path
  1.9       +11 -8     
xml-batik/resources/org/apache/batik/apps/rasterizer/resources/Messages.properties
  
  Index: Messages.properties
  ===================================================================
  RCS file: 
/home/cvs/xml-batik/resources/org/apache/batik/apps/rasterizer/resources/Messages.properties,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- Messages.properties       22 Jun 2002 11:52:55 -0000      1.8
  +++ Messages.properties       4 Sep 2002 19:20:21 -0000       1.9
  @@ -91,9 +91,10 @@
    -q <quality> \n \
   \tQuality for the output image. This is only relevant for the \n \
   \timage/jpeg mime type. \n \
  - -indexed \n \
  -\tReduces the image to 256 colors, resulting in an Indexed image.\n \
  -\tThis is only used for PNG conversion.\n \
  + -indexed [2|4|8]\n \
  +\tReduces the image to given number of bits per pixel using an\n \
  +\tadaptive pallete, resulting in an Indexed image.  This is\n \
  +\tcurrently only supported for PNG conversion.\n \
    -dpi <resolution> \n \
   \tResolution for the ouptut image. \n \
    -validate  \n \
  @@ -167,28 +168,30 @@
   
   Main.cl.option.cssUser.description = \
   -cssUser <userStylesheetURI> User CSS stylesheet to apply when converting SVG 
files. \n \
  -Example: -cssUser myStylesheet.css
  +Example: -cssUser myStylesheet.css \n \
   Default: none
   
   Main.cl.option.quality.description = \
   -q <quality> Quality for the generated output image. This is only used for JPEG 
conversion. \n \
   The value should be in the [0,1] range. \n \
  -Example: -q 0.5
  +Example: -q 0.5 \n \
   Default: 0.99
   
   Main.cl.option.indexed.description = \
  --indexed indicates that the image should be reduced to 256 colors, resulting in an 
Indexed image. This is only used for PNG conversion.
  +-indexed [2|4|8] indicates that the image should be reduced to the given number of 
bits per pixel using an  adaptive pallete, resulting in an Indexed image.  This is 
currently only supported for PNG conversion.\n \
  +Example: -indexed 8 \n \
  +Default: none
   
   Main.cl.option.dpi.description = \
   -dpi <resolution> Resolution for the output image. This is used to compute the \n \
   "pixel to millimeter" ratio used when processing SVG documents. \n \
  -Example: -dpi 300
  +Example: -dpi 300 \n \
   Default: 96
   
   Main.cl.option.lang.description = \
   -lang <language> Language to use when processing SVG documents. This is important 
for \n \
   SVG documents containing multiple languages. \n
  -Example: -lang fr
  +Example: -lang fr \n \
   Default: en
   
   Main.cl.option.validate.description = \
  
  
  
  1.23      +12 -5     xml-batik/sources/org/apache/batik/apps/rasterizer/Main.java
  
  Index: Main.java
  ===================================================================
  RCS file: /home/cvs/xml-batik/sources/org/apache/batik/apps/rasterizer/Main.java,v
  retrieving revision 1.22
  retrieving revision 1.23
  diff -u -r1.22 -r1.23
  --- Main.java 22 Jun 2002 11:52:56 -0000      1.22
  +++ Main.java 4 Sep 2002 19:20:21 -0000       1.23
  @@ -649,11 +649,18 @@
                             });
   
           optionMap.put(CL_OPTION_INDEXED,
  -                      new NoValueOptionHandler(){
  -                              public void handleOption(SVGConverter c){
  -                                  c.setIndexed(true);
  -                             }
  +                      new FloatOptionHandler(){
  +                              public void handleOption(float optionValue, 
  +                                                       SVGConverter c){
  +                                  if ((optionValue != 1) &&
  +                                      (optionValue != 2) &&
  +                                      (optionValue != 4) &&
  +                                      (optionValue != 8)) 
  +                                      throw new IllegalArgumentException();
   
  +                                  c.setIndexed((int)optionValue);
  +                              }
  +                      
                                 public String getOptionDescription(){
                                     return CL_OPTION_INDEXED_DESCRIPTION;
                                 }
  
  
  
  1.16      +10 -11    
xml-batik/sources/org/apache/batik/apps/rasterizer/SVGConverter.java
  
  Index: SVGConverter.java
  ===================================================================
  RCS file: 
/home/cvs/xml-batik/sources/org/apache/batik/apps/rasterizer/SVGConverter.java,v
  retrieving revision 1.15
  retrieving revision 1.16
  diff -u -r1.15 -r1.16
  --- SVGConverter.java 22 Jun 2002 11:52:56 -0000      1.15
  +++ SVGConverter.java 4 Sep 2002 19:20:21 -0000       1.16
  @@ -69,8 +69,8 @@
    *     background before rendering the image</li>
    * <li>quality: relevant only for JPEG destinations, this controls the 
    *     encoding quality.</li>
  - * <li>indexed: relevant only for PNG, controls the writting of 
  - *     indexed files.</li>
  + * <li>indexed: relevant only for PNG, controls the number of bits
  + *              used in writting of a palletized files.</li>
    * <li>mediaType: controls the CSS media, or list of media, for which the 
    *     image should be rendered.</li>
    * <li>alternate: controls the alternate CSS stylesheet to activate, 
  @@ -86,8 +86,7 @@
    *
    * @version $Id$
    * @author Henri Ruini
  - * @author <a href="mailto:[EMAIL PROTECTED]";>Vincent Hardy</a>
  - */
  + * @author <a href="mailto:[EMAIL PROTECTED]";>Vincent Hardy</a> */
   public class SVGConverter {
       // 
       // Error codes reported by the SVGConverter
  @@ -209,7 +208,7 @@
       protected float quality = DEFAULT_QUALITY;
   
       /** Should output Image be indexed . */
  -    protected boolean indexed = false;
  +    protected int indexed = -1;
   
       /** Output AOI area. */
       protected Rectangle2D area = null;
  @@ -351,11 +350,11 @@
        * Tells the PNG encoder to reduce the image to 256 colors, so the
        * PNG file is indexed.
        */
  -    public void setIndexed(boolean indexed) throws IllegalArgumentException {
  -        this.indexed = indexed;
  +    public void setIndexed(int bits) throws IllegalArgumentException {
  +        this.indexed = bits;
       }
   
  -    public boolean getIndexed(){
  +    public int getIndexed(){
           return indexed;
       }
   
  @@ -762,8 +761,8 @@
           } 
   
           // Set image indexed. ------------------------------------------------
  -        if (indexed) {
  -            map.put(PNGTranscoder.KEY_INDEXED, new Boolean(indexed));
  +        if (indexed != -1) {
  +            map.put(PNGTranscoder.KEY_INDEXED, new Integer(indexed));
           } 
   
           // Set image background color -----------------------------------------
  
  
  
  1.3       +46 -40    
xml-batik/sources/org/apache/batik/ext/awt/image/rendered/IndexImage.java
  
  Index: IndexImage.java
  ===================================================================
  RCS file: 
/home/cvs/xml-batik/sources/org/apache/batik/ext/awt/image/rendered/IndexImage.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- IndexImage.java   29 Aug 2002 19:34:57 -0000      1.2
  +++ IndexImage.java   4 Sep 2002 19:20:21 -0000       1.3
  @@ -23,20 +23,18 @@
   import java.util.Vector;
   import java.util.Iterator;
   
  +import org.apache.batik.ext.awt.image.GraphicsUtil;
  +
   /**
  - * This implements an adaptive pallete generator to reduce images
  - * to 256 colors.
  - *
  - * This should probably be generalized to create a pallete with any number of
  - * colors in it (rather than always 256).  This should be easy to do.
  + * This implements an adaptive pallete generator to reduce images to a
  + * specified number of colors.
    *
    * Ideally this would also support a better dither option than just 
    * the JDK's pattern dither.
    *
    * @author <a href="mailto:[EMAIL PROTECTED]";>Thomas DeWeese</a>
    * @author <a href="mailto:[EMAIL PROTECTED]";>Jun Inamori</a>
  - * @version $Id$
  - */
  + * @version $Id$ */
   public class IndexImage{
   
       /**
  @@ -57,7 +55,8 @@
   
       /**
        * Used to define a cube of the colorspace.  The cube can be split
  -     * approximagely in half to generate two cubes.  */
  +     * approximagely in half to generate two cubes.  
  +     */
       private static class Cube {
           int []min={0, 0, 0}, max={255,255,255};
   
  @@ -324,11 +323,14 @@
       /**
        * Converts the input image (must be TYPE_INT_RGB or
        * TYPE_INT_ARGB) to an indexed image.  Generating an adaptive
  -     * pallete.  
  +     * palette with number of colors specified.
  +     * @param bi the image to be processed.
  +     * @param nColors number of colors in the palette
        */
  -    static public BufferedImage getIndexedImage(BufferedImage bi){
  -     int w=bi.getWidth();
  -     int h=bi.getHeight();
  +    static public BufferedImage getIndexedImage
  +        (BufferedImage bi, int nColors) {
  +        int w=bi.getWidth();
  +        int h=bi.getHeight();
   
           // Using 4 bits from RG & B.
           Vector [] colors = new Vector[1<<12]; 
  @@ -368,10 +370,10 @@
   
           int nCubes=1;
           int fCube=0;
  -        Cube  [] cubes = new Cube[256];
  +        Cube [] cubes = new Cube[nColors];
           cubes[0] = new Cube(colors, w*h);
           
  -        while (nCubes < 256) {
  +        while (nCubes < nColors) {
               while (cubes[fCube].isDone()) {
                   fCube++;
                   if (fCube == nCubes) break;
  @@ -420,41 +422,45 @@
           }
           BufferedImage indexed;
   
  +
  +        // The JDK doesn't seem to dither the image correctly if I go
  +        // below 8bits per pixel.  So I dither to an 8bit pallete
  +        // image that only has nCubes colors.  Then I copy the data to
  +        // a lower bit depth image that I return.
           IndexColorModel icm=new IndexColorModel(8,nCubes,r,g,b);
           indexed =new BufferedImage
               (w, h, BufferedImage.TYPE_BYTE_INDEXED, icm);
  +        Graphics2D g2d=indexed.createGraphics();
  +        g2d.setRenderingHint
  +            (RenderingHints.KEY_DITHERING,
  +             RenderingHints.VALUE_DITHER_ENABLE);
  +        g2d.drawImage(bi, 0, 0, null);
  +        g2d.dispose();
  +
   
  -        /*
  -            // The JDK doesn't seem to dither the image correctly if
  -            // I go below 8bits per pixel.  Otherwise this code should
  -            // work.
           int bits;
           for (bits=1; bits <=8; bits++) {
               if ((1<<bits) >= nCubes) break;
           }
  -        System.out.println("Bits: " + bits + " Cubes: " + nCubes);
  -        if (bits > 4) {
  -            bits = 8;
  -            IndexColorModel icm=new IndexColorModel(8,nCubes,r,g,b);
  -            indexed =new BufferedImage
  -                (w, h, BufferedImage.TYPE_BYTE_INDEXED, icm);
  -        } else {
  -            if (bits ==3) bits = 4;
  -            ColorModel cm=new IndexColorModel(bits,nCubes,r,g,b);
  -            SampleModel sm = new MultiPixelPackedSampleModel
  -                (DataBuffer.TYPE_BYTE, w, h, bits);
  -            WritableRaster ras = Raster.createWritableRaster
  -                (sm, new Point(0,0));
  -            indexed = new BufferedImage(cm, ras, 
  -                                        bi.isAlphaPremultiplied(), null);
  -        }
  -        */
  +        // System.out.println("Bits: " + bits + " Cubes: " + nCubes);
   
  -     Graphics2D g2d=indexed.createGraphics();
  -     g2d.setRenderingHint
  -            (RenderingHints.KEY_DITHERING,RenderingHints.VALUE_DITHER_ENABLE);
  -     g2d.drawImage(bi, 0, 0, null);
  -        g2d.dispose();
  +        if (bits > 4)
  +            // 8 bit image we are done...
  +            return indexed;
  +
  +        // Create our low bit depth image...
  +        if (bits ==3) bits = 4;
  +        ColorModel cm=new IndexColorModel(bits,nCubes,r,g,b);
  +        SampleModel sm = new MultiPixelPackedSampleModel
  +            (DataBuffer.TYPE_BYTE, w, h, bits);
  +        WritableRaster ras = Raster.createWritableRaster
  +            (sm, new Point(0,0));
  +
  +        // Copy the data to the low bitdepth image.
  +        bi = indexed;
  +        indexed = new BufferedImage(cm, ras, 
  +                                    bi.isAlphaPremultiplied(), null);
  +        GraphicsUtil.copyData(bi, indexed);
           return indexed;
       }
   }
  
  
  
  1.16      +16 -11    
xml-batik/sources/org/apache/batik/transcoder/image/PNGTranscoder.java
  
  Index: PNGTranscoder.java
  ===================================================================
  RCS file: 
/home/cvs/xml-batik/sources/org/apache/batik/transcoder/image/PNGTranscoder.java,v
  retrieving revision 1.15
  retrieving revision 1.16
  diff -u -r1.15 -r1.16
  --- PNGTranscoder.java        22 Jun 2002 11:52:56 -0000      1.15
  +++ PNGTranscoder.java        4 Sep 2002 19:20:21 -0000       1.16
  @@ -13,6 +13,7 @@
   import java.awt.image.SinglePixelPackedSampleModel;
   import java.io.IOException;
   import java.io.OutputStream;
  +import org.apache.batik.transcoder.keys.IntegerKey;
   import org.apache.batik.transcoder.keys.BooleanKey;
   import org.apache.batik.transcoder.keys.FloatKey;
   import org.apache.batik.transcoder.TranscoderException;
  @@ -104,9 +105,12 @@
               }
           }
   
  +        int n=-1;
           if (hints.containsKey(KEY_INDEXED)) {
  -            if (((Boolean)hints.get(KEY_INDEXED)).booleanValue())
  -                img = IndexImage.getIndexedImage(img);
  +            n=((Integer)hints.get(KEY_INDEXED)).intValue();
  +            if (n==1||n==2||n==4||n==8) 
  +                //PNGEncodeParam.Palette can handle these numbers only.
  +                img = IndexImage.getIndexedImage(img,1<<n);
           }
   
           PNGEncodeParam params = PNGEncodeParam.getDefaultEncodeParam(img);
  @@ -169,7 +173,8 @@
           = new FloatKey();
   
       /**
  -     * The write a 256 color indexed image key.
  +     * The color indexed image key to specify number of colors used in
  +     * palette.
        *
        * <TABLE BORDER="0" CELLSPACING="0" CELLPADDING="1">
        * <TR>
  @@ -177,21 +182,21 @@
        * <TD VALIGN="TOP">KEY_INDEXED</TD></TR>
        * <TR>
        * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Value: </TH>
  -     * <TD VALIGN="TOP">Boolean</TD></TR>
  +     * <TD VALIGN="TOP">Integer</TD></TR>
        * <TR>
        * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Default: </TH>
  -     * <TD VALIGN="TOP">False</TD></TR>
  +     * <TD VALIGN="TOP">none/true color image</TD></TR>
        * <TR>
        * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Required: </TH>
        * <TD VALIGN="TOP">No</TD></TR>
        * <TR>
        * <TH VALIGN="TOP" ALIGN="RIGHT"><P ALIGN="RIGHT">Description: </TH>
  -     * <TD VALIGN="TOP">Turns on the reduction of the image to 256 colors.  
  -     *     The resultant PNG will be an indexed PNG with 256 colors.</TD>
  +     * <TD VALIGN="TOP">Turns on the reduction of the image to index 
  +     *     colors by specifying color bit depth, 1,2,4,8. The resultant 
  +     *     PNG will be an indexed PNG with color bit depth specified.</TD>
        * </TR>
  -     * </TABLE>
  +     * </TABLE> 
        */
       public static final TranscodingHints.Key KEY_INDEXED
  -        = new BooleanKey();
  -
  +        = new IntegerKey();
   }
  
  
  

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

Reply via email to