You are viewing a plain text version of this content. The canonical link for it is here.
Posted to fop-commits@xmlgraphics.apache.org by je...@apache.org on 2004/10/11 23:43:13 UTC

cvs commit: xml-fop/src/java/org/apache/fop/image BatikImage.java TIFFImage.java PNGImage.java

jeremias    2004/10/11 14:43:13

  Modified:    src/java/org/apache/fop/image TIFFImage.java PNGImage.java
  Added:       src/java/org/apache/fop/image BatikImage.java
  Log:
  Made a common base class for both PNG and TIFF classes because they are almost identical.
  Some style updates.
  
  Revision  Changes    Path
  1.2       +20 -159   xml-fop/src/java/org/apache/fop/image/TIFFImage.java
  
  Index: TIFFImage.java
  ===================================================================
  RCS file: /home/cvs/xml-fop/src/java/org/apache/fop/image/TIFFImage.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- TIFFImage.java	11 Oct 2004 21:06:14 -0000	1.1
  +++ TIFFImage.java	11 Oct 2004 21:43:13 -0000	1.2
  @@ -1,48 +1,35 @@
   /*
  - *  Copyright 2004 The Apache Software Foundation 
  + * Copyright 2004 The Apache Software Foundation
    *
  - *  Licensed under the Apache License, Version 2.0 (the "License");
  - *  you may not use this file except in compliance with the License.
  - *  You may obtain a copy of the License at
  + * Licensed under the Apache License, Version 2.0 (the "License");
  + * you may not use this file except in compliance with the License.
  + * You may obtain a copy of the License at
    *
    *      http://www.apache.org/licenses/LICENSE-2.0
    *
  - *  Unless required by applicable law or agreed to in writing, software
  - *  distributed under the License is distributed on an "AS IS" BASIS,
  - *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  - *  See the License for the specific language governing permissions and
  - *  limitations under the License.
  - *
  + * Unless required by applicable law or agreed to in writing, software
  + * distributed under the License is distributed on an "AS IS" BASIS,
  + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  + * See the License for the specific language governing permissions and
  + * limitations under the License.
    */
   
  +/* $Id$ */
  +
   package org.apache.fop.image;
   
  -import java.awt.Color;
  -import java.awt.Transparency;
  -import java.awt.image.ColorModel;
  -import java.awt.image.IndexColorModel;
  -import java.awt.image.WritableRaster;
  -import java.awt.image.BufferedImage;
   import java.io.IOException;
   
   import org.apache.batik.ext.awt.image.codec.SeekableStream;
  -import org.apache.batik.ext.awt.image.codec.MemoryCacheSeekableStream;
  -import org.apache.batik.ext.awt.image.codec.FileCacheSeekableStream;
  -import org.apache.batik.ext.awt.image.rendered.Any2sRGBRed;
   import org.apache.batik.ext.awt.image.rendered.CachableRed;
   
   /**
  - * FopImage object using TIFF
  - * @author Eric SCHAEFFER
  - * @see AbstractFopImage
  - * @see FopImage
  + * @author Jeremias Maerki
    */
  -public class TIFFImage extends AbstractFopImage {
  -
  -    private byte[] softMask = null;
  +public class TIFFImage extends BatikImage {
   
       /**
  -     * Constructs a new TIFFImage instance.
  +     * Constructs a new BatikImage instance.
        * @param imgReader basic metadata for the image
        */
       public TIFFImage(FopImage.ImageInfo imgReader) {
  @@ -50,137 +37,11 @@
       }
   
       /**
  -     * @see org.apache.fop.image.AbstractFopImage#loadDimensions()
  -     */
  -    protected boolean loadDimensions() {
  -        if (this.bitmaps == null) {
  -            loadImage();
  -        }
  -
  -        return this.bitmaps != null;
  -    }
  -
  -    /**
  -     * @see org.apache.fop.image.AbstractFopImage#loadBitmap()
  -     */
  -    protected boolean loadBitmap() {
  -        if (this.bitmaps == null) {
  -            loadImage();
  -        }
  -
  -        return this.bitmaps != null;
  -    }
  -
  -    /**
  -     * @see org.apache.fop.image.FopImage#hasSoftMask()
  -     */
  -    public boolean hasSoftMask() {
  -        if (this.bitmaps == null) {
  -            loadImage();
  -        }
  -
  -        return (this.softMask != null);
  -    }
  -
  -    /**
  -     * @see org.apache.fop.image.FopImage#getSoftMask()
  -     */
  -    public byte[] getSoftMask() {
  -        if (this.bitmaps == null) {
  -            loadImage();
  -        }
  -
  -        return this.softMask;
  -    }
  -
  -    /**
  -     * Loads the image from the InputStream.
  +     * @see org.apache.fop.image.BatikImage#decodeImage(org.apache.batik.ext.awt.image.codec.SeekableStream)
        */
  -    protected void loadImage() {
  -        try {
  -            SeekableStream seekableInput;
  -            try { seekableInput = new FileCacheSeekableStream(inputStream);
  -            } catch (IOException ioe) {
  -                seekableInput = new MemoryCacheSeekableStream(inputStream);
  -            }
  -
  -            CachableRed cr;
  -            cr = new org.apache.batik.ext.awt.image.codec.tiff.TIFFImage
  -                (seekableInput, null, 0);
  -            ColorModel cm = cr.getColorModel();
  -
  -            this.height = cr.getHeight();
  -            this.width  = cr.getWidth();
  -
  -            cr = new Any2sRGBRed(cr);
  -
  -            this.isTransparent = false;
  -            this.softMask = null;
  -            int transparencyType = cm.getTransparency();
  -            if ((transparencyType == Transparency.BITMASK) 
  -                    && (cm instanceof IndexColorModel)) {
  -                // Use 'transparent color'.
  -                IndexColorModel icm = (IndexColorModel)cm;
  -                int numColor = icm.getMapSize();
  -                byte [] alpha = new byte[numColor];
  -                icm.getAlphas(alpha);
  -                for (int i = 0; i < numColor; i++) {
  -                    if ((alpha[i] & 0xFF) == 0) {
  -                        this.isTransparent = true;
  -                        int red = (icm.getRed  (i)) & 0xFF;
  -                        int grn = (icm.getGreen(i)) & 0xFF;
  -                        int blu = (icm.getBlue (i)) & 0xFF;
  -                        this.transparentColor = new Color(red, grn, blu);
  -                        break;
  -                    }
  -                }
  -            }
  -
  -            // Get our current ColorModel
  -            cm = cr.getColorModel();
  -
  -            // It has an alpha channel so generate a soft mask.
  -            if ((!this.isTransparent) && cm.hasAlpha()) {
  -                this.softMask = new byte[this.width * this.height];
  -            }
  -
  -            this.bitsPerPixel = 8;
  -            this.bitmapsSize = this.width * this.height * 3;
  -            this.bitmaps = new byte[this.bitmapsSize];
  -            this.colorSpace = cm.getColorSpace();
  -
  -            WritableRaster wr = (WritableRaster)cr.getData();
  -            BufferedImage bi = new BufferedImage
  -                (cm, wr.createWritableTranslatedChild(0, 0), 
  -                 cm.isAlphaPremultiplied(), null);
  -            int [] tmpMap = new int[this.width];
  -            int idx = 0;
  -            int sfIdx = 0;
  -            for (int y = 0; y < this.height; y++) {
  -                tmpMap = bi.getRGB(0, y, this.width, 1, tmpMap, 0, this.width);
  -                if (softMask != null) {
  -                    for (int x = 0; x < this.width; x++) {
  -                        int pix = tmpMap[x];
  -                        this.softMask[sfIdx++] = (byte)(pix >>> 24);
  -                        this.bitmaps[idx++]    = (byte)((pix >>> 16) & 0xFF);
  -                        this.bitmaps[idx++]    = (byte)((pix >>> 8)  & 0xFF);
  -                        this.bitmaps[idx++]    = (byte)((pix)        & 0xFF);
  -                    }
  -                } else {
  -                    for (int x = 0; x < this.width; x++) {
  -                        int pix = tmpMap[x];
  -                        this.bitmaps[idx++] = (byte)((pix >> 16) & 0xFF);
  -                        this.bitmaps[idx++] = (byte)((pix >> 8)  & 0xFF);
  -                        this.bitmaps[idx++] = (byte)((pix)       & 0xFF);
  -                    }
  -                }
  -            }
  -        } catch (Exception ex) {
  -            /*throw new FopImageException("Error while loading image "
  -                                         + "" + " : "
  -                                         + ex.getClass() + " - "
  -                                         + ex.getMessage());
  -            */
  -        }
  +    protected CachableRed decodeImage(SeekableStream stream) throws IOException {
  +        return new org.apache.batik.ext.awt.image.codec.tiff.TIFFImage
  +                (stream, null, 0);
       }
  -};
  +    
  +}
  
  
  
  1.2       +24 -141   xml-fop/src/java/org/apache/fop/image/PNGImage.java
  
  Index: PNGImage.java
  ===================================================================
  RCS file: /home/cvs/xml-fop/src/java/org/apache/fop/image/PNGImage.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- PNGImage.java	11 Oct 2004 21:06:14 -0000	1.1
  +++ PNGImage.java	11 Oct 2004 21:43:13 -0000	1.2
  @@ -1,36 +1,28 @@
   /*
  - *  Copyright 2004 The Apache Software Foundation 
  + * Copyright 2004 The Apache Software Foundation 
    *
  - *  Licensed under the Apache License, Version 2.0 (the "License");
  - *  you may not use this file except in compliance with the License.
  - *  You may obtain a copy of the License at
  + * Licensed under the Apache License, Version 2.0 (the "License");
  + * you may not use this file except in compliance with the License.
  + * You may obtain a copy of the License at
    *
    *      http://www.apache.org/licenses/LICENSE-2.0
    *
  - *  Unless required by applicable law or agreed to in writing, software
  - *  distributed under the License is distributed on an "AS IS" BASIS,
  - *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  - *  See the License for the specific language governing permissions and
  - *  limitations under the License.
  - *
  + * Unless required by applicable law or agreed to in writing, software
  + * distributed under the License is distributed on an "AS IS" BASIS,
  + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  + * See the License for the specific language governing permissions and
  + * limitations under the License.
    */
   
  +/* $Id$ */
  +
   package org.apache.fop.image;
   
  -import java.awt.Color;
  -import java.awt.Transparency;
  -import java.awt.image.ColorModel;
  -import java.awt.image.IndexColorModel;
  -import java.awt.image.WritableRaster;
  -import java.awt.image.BufferedImage;
   import java.io.IOException;
   
   import org.apache.batik.ext.awt.image.codec.PNGRed;
   import org.apache.batik.ext.awt.image.codec.PNGDecodeParam;
   import org.apache.batik.ext.awt.image.codec.SeekableStream;
  -import org.apache.batik.ext.awt.image.codec.MemoryCacheSeekableStream;
  -import org.apache.batik.ext.awt.image.codec.FileCacheSeekableStream;
  -import org.apache.batik.ext.awt.image.rendered.Any2sRGBRed;
   import org.apache.batik.ext.awt.image.rendered.CachableRed;
   
   /**
  @@ -39,133 +31,24 @@
    * @see AbstractFopImage
    * @see FopImage
    */
  -public class PNGImage extends AbstractFopImage {
  -
  -    private byte[] softMask = null;
  +public class PNGImage extends BatikImage {
   
  +    /**
  +     * Constructs a new PNGImage instance.
  +     * @param imgReader basic metadata for the image
  +     */
       public PNGImage(FopImage.ImageInfo imgReader) {
           super(imgReader);
       }
   
  -    protected boolean loadDimensions() {
  -        if (this.bitmaps == null) {
  -            loadImage();
  -        }
  -
  -        return this.bitmaps != null;
  -    }
  -
       /**
  -     * @see org.apache.fop.image.AbstractFopImage#loadBitmap()
  +     * @see org.apache.fop.image.BatikImage#decodeImage(org.apache.batik.ext.awt.image.codec.SeekableStream)
        */
  -    protected boolean loadBitmap() {
  -        if (this.bitmaps == null) {
  -            loadImage();
  -        }
  -
  -        return this.bitmaps != null;
  -    }
  -
  -    public boolean hasSoftMask() {
  -        if (this.bitmaps == null) {
  -            loadImage();
  -        }
  -
  -        return (this.softMask != null);
  -    }
  -
  -    public byte[] getSoftMask() {
  -        if (this.bitmaps == null) {
  -            loadImage();
  -        }
  -
  -        return this.softMask;
  -    }
  -
  -    protected void loadImage() {
  -        try {
  -            SeekableStream seekableInput;
  -            try { seekableInput = new FileCacheSeekableStream(inputStream);
  -            } catch (IOException ioe) {
  -                seekableInput = new MemoryCacheSeekableStream(inputStream);
  -            }
  -
  -            PNGDecodeParam param = new PNGDecodeParam();
  -            param.setPerformGammaCorrection(true);
  -            param.setDisplayExponent(2.2f); // sRGB gamma
  -            CachableRed cr = new PNGRed(seekableInput, param);
  -            ColorModel cm = cr.getColorModel();
  -
  -            this.height = cr.getHeight();
  -            this.width  = cr.getWidth();
  -
  -            cr = new Any2sRGBRed(cr);
  -
  -            this.isTransparent = false;
  -            this.softMask = null;
  -            int transparencyType = cm.getTransparency();
  -            if ((transparencyType == Transparency.BITMASK) 
  -                    && (cm instanceof IndexColorModel)) {
  -                // Currently only supports 'transparent color'(?)
  -                IndexColorModel icm = (IndexColorModel)cm;
  -                int numColor = icm.getMapSize();
  -                byte [] alpha = new byte[numColor];
  -                icm.getAlphas(alpha);
  -                for (int i = 0; i < numColor; i++) {
  -                    if ((alpha[i] & 0xFF) == 0) {
  -                        this.isTransparent = true;
  -                        int red = (icm.getRed  (i)) & 0xFF;
  -                        int grn = (icm.getGreen(i)) & 0xFF;
  -                        int blu = (icm.getBlue (i)) & 0xFF;
  -                        this.transparentColor = new Color(red, grn, blu);
  -                        break;
  -                    }
  -                }
  -            }
  -
  -            cm = cr.getColorModel();
  -            if ((!this.isTransparent) && cm.hasAlpha()) {
  -                this.softMask = new byte[this.width * this.height];
  -            }
  -
  -            this.bitsPerPixel = 8;
  -            this.bitmapsSize = this.width * this.height * 3;
  -            this.bitmaps = new byte[this.bitmapsSize];
  -            this.colorSpace = cm.getColorSpace();
  -
  -            WritableRaster wr = (WritableRaster)cr.getData();
  -            BufferedImage bi = new BufferedImage
  -                (cm, wr.createWritableTranslatedChild(0, 0), 
  -                 cm.isAlphaPremultiplied(), null);
  -            int [] tmpMap = new int[this.width];
  -            int idx = 0;
  -            int sfIdx = 0;
  -            for (int y = 0; y < this.height; y++) {
  -                tmpMap = bi.getRGB(0, y, this.width, 1, tmpMap, 0, this.width);
  -                if (softMask != null) {
  -                    for (int x = 0; x < this.width; x++) {
  -                        int pix = tmpMap[x];
  -                        this.softMask[sfIdx++] = (byte)(pix >>> 24);
  -                        this.bitmaps[idx++]    = (byte)((pix >>> 16) & 0xFF);
  -                        this.bitmaps[idx++]    = (byte)((pix >>> 8)  & 0xFF);
  -                        this.bitmaps[idx++]    = (byte)((pix)        & 0xFF);
  -                    }
  -                } else {
  -                    for (int x = 0; x < this.width; x++) {
  -                        int pix = tmpMap[x];
  -                        this.bitmaps[idx++] = (byte)((pix >> 16) & 0xFF);
  -                        this.bitmaps[idx++] = (byte)((pix >> 8)  & 0xFF);
  -                        this.bitmaps[idx++] = (byte)((pix)       & 0xFF);
  -                    }
  -                }
  -            }
  -        } catch (Exception ex) {
  -            ex.printStackTrace();
  -            /*throw new FopImageException("Error while loading image "
  -                                         + "" + " : "
  -                                         + ex.getClass() + " - "
  -                                         + ex.getMessage());
  -            */
  -        }
  +    protected CachableRed decodeImage(SeekableStream stream) throws IOException {
  +        PNGDecodeParam param = new PNGDecodeParam();
  +        param.setPerformGammaCorrection(true);
  +        param.setDisplayExponent(2.2f); // sRGB gamma
  +        return new PNGRed(stream, param);
       }
  -};
  +    
  +}
  
  
  
  1.1                  xml-fop/src/java/org/apache/fop/image/BatikImage.java
  
  Index: BatikImage.java
  ===================================================================
  /*
   * Copyright 2004 The Apache Software Foundation 
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  /* $Id: BatikImage.java,v 1.1 2004/10/11 21:43:13 jeremias Exp $ */
  
  package org.apache.fop.image;
  
  import java.awt.Color;
  import java.awt.Transparency;
  import java.awt.image.ColorModel;
  import java.awt.image.IndexColorModel;
  import java.awt.image.WritableRaster;
  import java.awt.image.BufferedImage;
  import java.io.IOException;
  
  import org.apache.batik.ext.awt.image.codec.SeekableStream;
  import org.apache.batik.ext.awt.image.codec.MemoryCacheSeekableStream;
  import org.apache.batik.ext.awt.image.codec.FileCacheSeekableStream;
  import org.apache.batik.ext.awt.image.rendered.Any2sRGBRed;
  import org.apache.batik.ext.awt.image.rendered.CachableRed;
  
  /**
   * FopImage object using TIFF
   * @author Eric SCHAEFFER
   * @see AbstractFopImage
   * @see FopImage
   */
  public abstract class BatikImage extends AbstractFopImage {
  
      private byte[] softMask = null;
  
      /**
       * Constructs a new BatikImage instance.
       * @param imgReader basic metadata for the image
       */
      public BatikImage(FopImage.ImageInfo imgReader) {
          super(imgReader);
      }
  
      /**
       * @see org.apache.fop.image.AbstractFopImage#loadDimensions()
       */
      protected boolean loadDimensions() {
          if (this.bitmaps == null) {
              loadImage();
          }
  
          return this.bitmaps != null;
      }
  
      /**
       * @see org.apache.fop.image.AbstractFopImage#loadBitmap()
       */
      protected boolean loadBitmap() {
          if (this.bitmaps == null) {
              loadImage();
          }
  
          return this.bitmaps != null;
      }
  
      /**
       * @see org.apache.fop.image.FopImage#hasSoftMask()
       */
      public boolean hasSoftMask() {
          if (this.bitmaps == null) {
              loadImage();
          }
  
          return (this.softMask != null);
      }
  
      /**
       * @see org.apache.fop.image.FopImage#getSoftMask()
       */
      public byte[] getSoftMask() {
          if (this.bitmaps == null) {
              loadImage();
          }
  
          return this.softMask;
      }
  
      /**
       * Decodes the image from the stream.
       * @param stream the stream to read the image from
       * @return the decoded image
       * @throws IOException in case an I/O problem occurs
       */
      protected abstract CachableRed decodeImage(SeekableStream stream) throws IOException;
      
      /**
       * Loads the image from the InputStream.
       */
      protected void loadImage() {
          try {
              SeekableStream seekableInput;
              try { seekableInput = new FileCacheSeekableStream(inputStream);
              } catch (IOException ioe) {
                  seekableInput = new MemoryCacheSeekableStream(inputStream);
              }
  
              CachableRed cr = decodeImage(seekableInput);
              ColorModel cm = cr.getColorModel();
  
              this.height = cr.getHeight();
              this.width  = cr.getWidth();
  
              cr = new Any2sRGBRed(cr);
  
              this.isTransparent = false;
              this.softMask = null;
              int transparencyType = cm.getTransparency();
              if ((transparencyType == Transparency.BITMASK) 
                      && (cm instanceof IndexColorModel)) {
                  // Use 'transparent color'.
                  IndexColorModel icm = (IndexColorModel)cm;
                  int numColor = icm.getMapSize();
                  byte [] alpha = new byte[numColor];
                  icm.getAlphas(alpha);
                  for (int i = 0; i < numColor; i++) {
                      if ((alpha[i] & 0xFF) == 0) {
                          this.isTransparent = true;
                          int red = (icm.getRed  (i)) & 0xFF;
                          int grn = (icm.getGreen(i)) & 0xFF;
                          int blu = (icm.getBlue (i)) & 0xFF;
                          this.transparentColor = new Color(red, grn, blu);
                          break;
                      }
                  }
              }
  
              // Get our current ColorModel
              cm = cr.getColorModel();
  
              // It has an alpha channel so generate a soft mask.
              if ((!this.isTransparent) && cm.hasAlpha()) {
                  this.softMask = new byte[this.width * this.height];
              }
  
              this.bitsPerPixel = 8;
              this.bitmapsSize = this.width * this.height * 3;
              this.bitmaps = new byte[this.bitmapsSize];
              this.colorSpace = cm.getColorSpace();
  
              WritableRaster wr = (WritableRaster)cr.getData();
              BufferedImage bi = new BufferedImage
                  (cm, wr.createWritableTranslatedChild(0, 0), 
                   cm.isAlphaPremultiplied(), null);
              int [] tmpMap = new int[this.width];
              int idx = 0;
              int sfIdx = 0;
              for (int y = 0; y < this.height; y++) {
                  tmpMap = bi.getRGB(0, y, this.width, 1, tmpMap, 0, this.width);
                  if (softMask != null) {
                      for (int x = 0; x < this.width; x++) {
                          int pix = tmpMap[x];
                          this.softMask[sfIdx++] = (byte)(pix >>> 24);
                          this.bitmaps[idx++]    = (byte)((pix >>> 16) & 0xFF);
                          this.bitmaps[idx++]    = (byte)((pix >>> 8)  & 0xFF);
                          this.bitmaps[idx++]    = (byte)((pix)        & 0xFF);
                      }
                  } else {
                      for (int x = 0; x < this.width; x++) {
                          int pix = tmpMap[x];
                          this.bitmaps[idx++] = (byte)((pix >> 16) & 0xFF);
                          this.bitmaps[idx++] = (byte)((pix >> 8)  & 0xFF);
                          this.bitmaps[idx++] = (byte)((pix)       & 0xFF);
                      }
                  }
              }
          } catch (Exception ex) {
              log.error("Error loading an image" , ex);
              /*throw new FopImageException("Error while loading image "
                                           + "" + " : "
                                           + ex.getClass() + " - "
                                           + ex.getMessage());
              */
          }
      }
  };
  
  
  

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