/* * Copyright 1999-2005 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$ */packageorg.apache.fop.image;// AWTimportjava.awt.image.ColorModel;importjava.awt.image.IndexColorModel;importjava.awt.image.BufferedImage;importjava.awt.Color;// JAIimportjavax.media.jai.JAI;importjavax.media.jai.RenderedOp;importorg.apache.commons.io.IOUtils;// Sun codecimportcom.sun.media.jai.codec.FileCacheSeekableStream;/** * FopImage object using JAI. * @author Eric SCHAEFFER * @see AbstractFopImage * @see FopImage */publicclassJAIImageextendsAbstractFopImage{/** * Create a new JAI image. * * @param imgInfo the image info for this JAI image */publicJAIImage(FopImage.ImageInfoimgInfo){super(imgInfo);}/** * @see org.apache.fop.image.AbstractFopImage#loadDimensions() */protectedbooleanloadDimensions(){if(this.bitmaps==null){loadImage();}returnthis.bitmaps!=null;}/** * @see org.apache.fop.image.AbstractFopImage#loadBitmap() */protectedbooleanloadBitmap(){if(this.bitmaps==null){loadImage();}returnthis.bitmaps!=null;}/** * Loads the image from the inputstream */protectedvoidloadImage(){com.sun.media.jai.codec.FileCacheSeekableStreamseekableInput=null;RenderedOpimageOp=null;try{seekableInput=newFileCacheSeekableStream(inputStream);imageOp=JAI.create("stream",seekableInput);this.height=imageOp.getHeight();this.width=imageOp.getWidth();ColorModelcm=imageOp.getColorModel();//this.bitsPerPixel = 8;this.bitsPerPixel=cm.getPixelSize();//this.colorSpace = ColorSpace.getInstance(ColorSpace.CS_LINEAR_RGB);this.colorSpace=cm.getColorSpace();BufferedImageimageData=imageOp.getAsBufferedImage();int[]tmpMap=imageData.getRGB(0,0,this.width,this.height,null,0,this.width);if(cm.hasAlpha()){// java.awt.Transparency. BITMASK or OPAQUE or TRANSLUCENTinttransparencyType=cm.getTransparency();if(transparencyType==java.awt.Transparency.OPAQUE){this.isTransparent=false;}elseif(transparencyType==java.awt.Transparency.BITMASK){if(cminstanceofIndexColorModel){this.isTransparent=false;byte[]alphas=newbyte[((IndexColorModel)cm).getMapSize()];byte[]reds=newbyte[((IndexColorModel)cm).getMapSize()];byte[]greens=newbyte[((IndexColorModel)cm).getMapSize()];byte[]blues=newbyte[((IndexColorModel)cm).getMapSize()];((IndexColorModel)cm).getAlphas(alphas);((IndexColorModel)cm).getReds(reds);((IndexColorModel)cm).getGreens(greens);((IndexColorModel)cm).getBlues(blues);for(inti=0;i<((IndexColorModel)cm).getMapSize();i++){if((alphas[i]&0xFF)==0){this.isTransparent=true;this.transparentColor=newColor((int)(reds[i]&0xFF),(int)(greens[i]&0xFF),(int)(blues[i]&0xFF));break;}}}else{// TRANSLUCENT/* * this.isTransparent = false; * for (int i = 0; i < this.width * this.height; i++) { * if (cm.getAlpha(tmpMap[i]) == 0) { * this.isTransparent = true; * this.transparentColor = new PDFColor(cm.getRed(tmpMap[i]), * cm.getGreen(tmpMap[i]), cm.getBlue(tmpMap[i])); * break; * } * } * // or use special API... */this.isTransparent=false;}}else{this.isTransparent=false;}}else{this.isTransparent=false;}// Should take care of the ColorSpace and bitsPerPixelthis.bitmaps=newbyte[this.width*this.height*3];for(inti=0;i<this.height;i++){for(intj=0;j<this.width;j++){intp=tmpMap[i*this.width+j];intr=(p>>16)&0xFF;intg=(p>>8)&0xFF;intb=(p)&0xFF;this.bitmaps[3*(i*this.width+j)]=(byte)(r&0xFF);this.bitmaps[3*(i*this.width+j)+1]=(byte)(g&0xFF);this.bitmaps[3*(i*this.width+j)+2]=(byte)(b&0xFF);}}}catch(Exceptionex){log.error("Error while loading image (JAI): "+ex.getMessage(),ex);}finally{IOUtils.closeQuietly(inputStream);inputStream=null;if(imageOp!=null){imageOp.dispose();}if(seekableInput!=null){IOUtils.closeQuietly(seekableInput);}}}/** @see org.apache.fop.image.AbstractFopImage#loadOriginalData() */protectedbooleanloadOriginalData(){returnloadDefaultOriginalData();}}