You are viewing a plain text version of this content. The canonical link for it is here.
Posted to batik-dev@xmlgraphics.apache.org by vh...@apache.org on 2001/09/19 13:16:37 UTC

cvs commit: xml-batik/sources/org/apache/batik/transcoder/image PNGTranscoder.java

vhardy      01/09/19 04:16:37

  Modified:    sources/org/apache/batik/apps/rasterizer Main.java
               sources/org/apache/batik/apps/svgbrowser
                        JSVGViewerFrame.java
               sources/org/apache/batik/transcoder/image PNGTranscoder.java
  Log:
  Modified PNG encoding approach with regards to opacity:
  
  a. PNGTranscoder no longer forces fully transparent pixels to white.
     Instead, forcing white transparent pixel has become a transcoding
     hint option: PNGTranscoder.KEY_FORCE_TRANSPARENT_WHITE.
  
  b. The svgbrowser retains its previous PNG Save As behavior and
     still forces transparent white on the images it exports.
  
  c. The svgrasterizer now uses the new default PNGTranscoder
     behavior (*not* forcing transparent white). To get a white
     background, a '-bg' option has been added to the rasterizer
     which lets the user specify the desired background color.
     The default is fully opaque white background.
  
  Revision  Changes    Path
  1.12      +86 -22    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.11
  retrieving revision 1.12
  diff -u -r1.11 -r1.12
  --- Main.java	2001/07/05 16:19:27	1.11
  +++ Main.java	2001/09/19 11:16:37	1.12
  @@ -8,6 +8,7 @@
   
   package org.apache.batik.apps.rasterizer;
   
  +import java.awt.Color;
   import java.io.BufferedOutputStream;
   import java.io.File;
   import java.io.FileOutputStream;
  @@ -17,6 +18,7 @@
   import java.util.Iterator;
   import java.util.LinkedList;
   import java.util.List;
  +import java.util.StringTokenizer;
   import java.net.URL;
   import java.net.MalformedURLException;
   import org.apache.batik.transcoder.TranscoderInput;
  @@ -32,7 +34,7 @@
    * A simple class that can generate images from svg documents.
    *
    * @author <a href="mailto:Thierry.Kormann@sophia.inria.fr">Thierry Kormann</a>
  - * @version $Id: Main.java,v 1.11 2001/07/05 16:19:27 vhardy Exp $
  + * @version $Id: Main.java,v 1.12 2001/09/19 11:16:37 vhardy Exp $
    */
   public class Main {
   
  @@ -62,6 +64,7 @@
           out.println("-m <mimetype>    Mime type for output files");
           out.println("-w <width>       Width of the output image");
           out.println("-h <height>      Height of the output image");
  +        out.println("-bg <color>      Background color (a.r.g.b decimal notation) for the output image. White is the default");
       }
   
       public static void main(String [] args) {
  @@ -71,6 +74,7 @@
           int i=0;
           float width = Float.NaN;
           float height = Float.NaN;
  +        Color background = Color.white;
   
           while (i < args.length) {
               if (args[i].equals("-d")) {
  @@ -108,6 +112,22 @@
                       usage(System.err);
                       System.exit(1);
                   }
  +            } else if (args[i].equals("-bg")) {
  +                if (i+1 < args.length) {
  +                    i++;
  +                    String argbVal = args[i++];
  +                    Color c = parseARGB(argbVal);
  +                    if(c == null){
  +                        usage(System.err);
  +                        System.exit(1);
  +                    }
  +                    background = c;
  +                    continue;
  +                } else {
  +                    error("option -w requires an argument");
  +                    usage(System.err);
  +                    System.exit(1);
  +                }
               } else if (args[i].equals("-h")) {
                   if (i+1 < args.length) {
                       i++;
  @@ -168,6 +188,9 @@
                                    new Float(height));
           }
   
  +        t.addTranscodingHint(ImageTranscoder.KEY_BACKGROUND_COLOR,
  +                             background);
  +
           for (Iterator iter = svgFiles.iterator(); iter.hasNext();) {
               String s = (String) iter.next();
               URL url = getSVGURL(s);
  @@ -210,33 +233,74 @@
           System.exit(0);
       }
   
  -        public static URL getSVGURL(String s) {
  -                URL url = null;
  +    public static URL getSVGURL(String s) {
  +        URL url = null;
   
  -                try{
  -                        File f = new File(s);
  -                        if(f.exists()){
  -                                url = f.toURL();
  -                        }
  -                        else{
  -                                url = new URL(s);
  -                        }
  -                }catch(MalformedURLException e){
  -                        error("Bad svg file: " + s);
  -                }
  +        try{
  +            File f = new File(s);
  +            if(f.exists()){
  +                url = f.toURL();
  +            }
  +            else{
  +                url = new URL(s);
  +            }
  +        }catch(MalformedURLException e){
  +            error("Bad svg file: " + s);
  +        }
  +
  +        return url;
  +    }
   
  -                return url;
  +    public static String getDirectory(String s){
  +        File f = new File(s);
  +        if(f.exists()){
  +            return f.getParent();
           }
  +        else{
  +            return null;
  +        }
  +    }
   
  -        public static String getDirectory(String s){
  -                File f = new File(s);
  -                if(f.exists()){
  -                        return f.getParent();
  -                }
  -                else{
  -                        return null;
  +    /**
  +     * Parse the input value, which should be in the following
  +     * format: a.r.g.b where a, r, g and b are integer values,
  +     * in decimal notation, between 0 and 255.
  +     * @return the parsed color if successful. null otherwise.
  +     */
  +    public static Color parseARGB(String argbVal){
  +        Color c = null;
  +        if(argbVal != null){
  +            StringTokenizer st = new StringTokenizer(argbVal, ".");
  +            if(st.countTokens() == 4){
  +                String aStr = st.nextToken();
  +                String rStr = st.nextToken();
  +                String gStr = st.nextToken();
  +                String bStr = st.nextToken();
  +                int a = -1, r = -1, g = -1, b = -1;
  +                try {
  +                    a = Integer.parseInt(aStr);
  +                    r = Integer.parseInt(rStr);
  +                    g = Integer.parseInt(gStr);
  +                    b = Integer.parseInt(bStr);
  +                }catch(NumberFormatException e){
  +                    // If an error occured, the a, r, g, b
  +                    // values will not be in the 0-255 range
  +                    // and the next if test will fail
  +                }
  +
  +                if( a>=0 && a<=255
  +                    &&
  +                    r>=0 && r<=255
  +                    &&
  +                    g>=0 && g<=255
  +                    &&
  +                    b>=0 && b<=255 ){
  +                    c = new Color(r,g,b,a);
                   }
  +            }
           }
  +        return c;
  +    }
   
   }
   
  
  
  
  1.51      +3 -1      xml-batik/sources/org/apache/batik/apps/svgbrowser/JSVGViewerFrame.java
  
  Index: JSVGViewerFrame.java
  ===================================================================
  RCS file: /home/cvs/xml-batik/sources/org/apache/batik/apps/svgbrowser/JSVGViewerFrame.java,v
  retrieving revision 1.50
  retrieving revision 1.51
  diff -u -r1.50 -r1.51
  --- JSVGViewerFrame.java	2001/09/18 10:52:24	1.50
  +++ JSVGViewerFrame.java	2001/09/19 11:16:37	1.51
  @@ -138,7 +138,7 @@
    * This class represents a SVG viewer swing frame.
    *
    * @author <a href="mailto:stephane@hillion.org">Stephane Hillion</a>
  - * @version $Id: JSVGViewerFrame.java,v 1.50 2001/09/18 10:52:24 vhardy Exp $
  + * @version $Id: JSVGViewerFrame.java,v 1.51 2001/09/19 11:16:37 vhardy Exp $
    */
   public class JSVGViewerFrame
       extends    JFrame
  @@ -935,6 +935,8 @@
                       final ImageTranscoder trans = new PNGTranscoder();
                       trans.addTranscodingHint(PNGTranscoder.KEY_XML_PARSER_CLASSNAME,
                                                application.getXMLParserClassName());
  +                    trans.addTranscodingHint(PNGTranscoder.KEY_FORCE_TRANSPARENT_WHITE,
  +                                             new Boolean(true));
                       final BufferedImage img = trans.createImage(w, h);
   
                       // paint the buffer to the image
  
  
  
  1.7       +57 -28    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.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- PNGTranscoder.java	2001/07/04 16:50:05	1.6
  +++ PNGTranscoder.java	2001/09/19 11:16:37	1.7
  @@ -13,6 +13,7 @@
   import java.awt.image.SinglePixelPackedSampleModel;
   import java.io.IOException;
   import java.io.OutputStream;
  +import org.apache.batik.transcoder.keys.BooleanKey;
   import org.apache.batik.transcoder.TranscoderException;
   import org.apache.batik.transcoder.TranscoderOutput;
   import org.apache.batik.transcoder.TranscodingHints;
  @@ -24,14 +25,16 @@
    * This class is an <tt>ImageTranscoder</tt> that produces a PNG image.
    *
    * @author <a href="mailto:Thierry.Kormann@sophia.inria.fr">Thierry Kormann</a>
  - * @version $Id: PNGTranscoder.java,v 1.6 2001/07/04 16:50:05 cjolif Exp $
  + * @version $Id: PNGTranscoder.java,v 1.7 2001/09/19 11:16:37 vhardy Exp $
    */
   public class PNGTranscoder extends ImageTranscoder {
  -
       /**
        * Constructs a new transcoder that produces png images.
        */
  -    public PNGTranscoder() { }
  +    public PNGTranscoder() { 
  +        hints.put(KEY_FORCE_TRANSPARENT_WHITE, 
  +                  new Boolean(false));
  +    }
   
       /**
        * Creates a new ARGB image with the specified dimension.
  @@ -67,32 +70,42 @@
           // the alpha channel will see a white background (and not
           // a black one).
           //
  -        int w = img.getWidth(), h = img.getHeight();
  -        DataBufferInt biDB = (DataBufferInt)img.getRaster().getDataBuffer();
  -        int scanStride = ((SinglePixelPackedSampleModel)img.getSampleModel()).getScanlineStride();
  -        int dbOffset = biDB.getOffset();
  -        int pixels[] = biDB.getBankData()[0];
  -        int p = dbOffset;
  -        int adjust = scanStride - w;
  -        int a=0, r=0, g=0, b=0, pel=0;
  -        for(int i=0; i<h; i++){
  -            for(int j=0; j<w; j++){
  -                pel = pixels[p];
  -                a = (pel >> 24) & 0xff;
  -                r = (pel >> 16) & 0xff;
  -                g = (pel >> 8 ) & 0xff;
  -                b =  pel        & 0xff;
  -                r = (255*(255 -a) + a*r)/255;
  -                g = (255*(255 -a) + a*g)/255;
  -                b = (255*(255 -a) + a*b)/255;
  -                pixels[p++] =
  -                            (a<<24 & 0xff000000) |
  -                            (r<<16 & 0xff0000) |
  -                            (g<<8  & 0xff00) |
  -                            (b     & 0xff);
  -             }
  -            p += adjust;
  +        boolean forceTransparentWhite = true;
  +
  +        if (hints.containsKey(KEY_FORCE_TRANSPARENT_WHITE)) {
  +            forceTransparentWhite = ((Boolean)hints.get(KEY_FORCE_TRANSPARENT_WHITE)).booleanValue();
  +        }
  +
  +        if(forceTransparentWhite){
  +            System.out.println("Forcing transparent white");
  +            int w = img.getWidth(), h = img.getHeight();
  +            DataBufferInt biDB = (DataBufferInt)img.getRaster().getDataBuffer();
  +            int scanStride = ((SinglePixelPackedSampleModel)img.getSampleModel()).getScanlineStride();
  +            int dbOffset = biDB.getOffset();
  +            int pixels[] = biDB.getBankData()[0];
  +            int p = dbOffset;
  +            int adjust = scanStride - w;
  +            int a=0, r=0, g=0, b=0, pel=0;
  +            for(int i=0; i<h; i++){
  +                for(int j=0; j<w; j++){
  +                    pel = pixels[p];
  +                    a = (pel >> 24) & 0xff;
  +                    r = (pel >> 16) & 0xff;
  +                    g = (pel >> 8 ) & 0xff;
  +                    b =  pel        & 0xff;
  +                    r = (255*(255 -a) + a*r)/255;
  +                    g = (255*(255 -a) + a*g)/255;
  +                    b = (255*(255 -a) + a*b)/255;
  +                    pixels[p++] =
  +                        (a<<24 & 0xff000000) |
  +                        (r<<16 & 0xff0000) |
  +                        (g<<8  & 0xff00) |
  +                        (b     & 0xff);
  +                }
  +                p += adjust;
  +            }
           }
  +
           try {
               PNGImageEncoder pngEncoder = new PNGImageEncoder(ostream, params);
               pngEncoder.encode(img);
  @@ -101,4 +114,20 @@
               throw new TranscoderException(ex);
           }
       }
  +
  +    // --------------------------------------------------------------------
  +    // Keys definition
  +    // --------------------------------------------------------------------
  +    /**
  +     * The 'forceTransparentWhite' key.
  +     * It controls whether the encoder should force the image's fully transparent
  +     * pixels to be fully transparent white instead of fully transparent black. 
  +     * This is usefull when the encoded PNG is displayed in a browser which
  +     * does not support PNG transparency and lets the image display
  +     * with a white background instead of a black background. <br />
  +     * However, note that the modified image will display differently
  +     * over a white background in a viewer that supports transparency.
  +     */
  +    public static final TranscodingHints.Key KEY_FORCE_TRANSPARENT_WHITE
  +        = new BooleanKey();
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: batik-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: batik-dev-help@xml.apache.org