You are viewing a plain text version of this content. The canonical link for it is here.
Posted to fop-dev@xmlgraphics.apache.org by Eric SCHAEFFER <es...@posterconseil.com> on 2000/01/12 17:35:26 UTC
Patch - Jimi images
Here is my patch for images using Jimi.
Can you tell me if it works for you, and what you think about it before I
commit. It seems to work under WinNT, but I've got strange results for
Gif/Jpeg under Linux (maybe the JVM blackdown port...) : the priducer return
an IMAGEERROR status.
There's maybe some changes to make for the 'bit per pixel' value (I just
assume it's 8, no check).
I've also changed the image consumer class to wait for the good producer
status.
Eric.
PS: diff of existing files + the new JimiImage class
========================================================
Diff content
========================================================
Index: ./src/org/apache/fop/image/FopImage.java
===================================================================
RCS file: /home/cvspublic/xml-fop/src/org/apache/fop/image/FopImage.java,v
retrieving revision 1.5
diff -r1.5 FopImage.java
64,65c64,65
< public int getpixelwidth();
< public int getpixelheight();
---
> public int getPixelWidth();
> public int getPixelHeight();
67a68
> public void setDimensions(int w, int h);
70,73c71,76
< public String gethref();
< public int[] getimagemap();
< public boolean getcolor();
< public int getbitperpixel();
---
> public String getHRef();
> // be carreful : image is compressed with zlib
> public byte[] getImageMap();
> public int getImageSize();
> public boolean isColored();
> public int getBitsPerPixel();
Index: ./src/org/apache/fop/image/FopImageFactory.java
===================================================================
RCS file:
/home/cvspublic/xml-fop/src/org/apache/fop/image/FopImageFactory.java,v
retrieving revision 1.5
diff -r1.5 FopImageFactory.java
63d62
< import java.io.FileInputStream;
64a64
> import java.net.*;
67,92d66
<
< public static FopImage Make(String ref,int x,int y, int width, int
height) {
<
<
< int colorpos=28; file://offset positioning for w and height in bmp files
< int [] headermap = new int[54];
< try{
< FileInputStream file=new FileInputStream(ref);
< boolean eof=false;
< int count=0;
< while ((!eof) && (count<54) ) {
< int input =file.read();
< if (input==-1)
< eof=true;
< else
< headermap[count++]=input;
< }
< file.close();
< } catch (IOException e) {System.err.println("Image not found");}
< int bpp=headermap[28];
< if (bpp==8) {
< return (new BmpBwImage(ref,x,y,width,height));
< } else if (bpp==24) {
< return (new BmpColImage(ref,x,y,width,height));
< }
< System.err.println("Unsupported bmp format");
94c68,84
< return null;
---
> // Image file formats supported
> public final static int IMAGE_GIF = 1;
> public final static int IMAGE_JPEG = 2;
> public final static int IMAGE_PNG = 3;
> public final static int IMAGE_TGA = 4;
> public final static int IMAGE_DIB = 5;
> public final static int IMAGE_DDB = 6;
> public final static int IMAGE_BMP = 7;
> public final static int IMAGE_PICT = 8;
> public final static int IMAGE_PSD = 9;
> public final static int IMAGE_CMU_RASTER = 10;
> public final static int IMAGE_TIFF = 11;
> public final static int IMAGE_XBM = 12;
> public final static int IMAGE_XPM = 13;
> public final static int IMAGE_ICO = 14;
> public final static int IMAGE_CUR = 15;
> public final static int IMAGE_PCX = 16;
96,97c86,276
< }
< }
---
> public static FopImage Make(String ref,int x,int y, int width, int
height) {
> URL absoluteURL = null;
> try {
> absoluteURL = new URL(ref);
> }
> catch (MalformedURLException e) {
> // maybe relative
> /* URL context_url = null;
> try {
> context_url = new URL(); // how to get the context URL ?
> try {
> absoluteURL = new URL(context_url, ref);
> }
> catch (MalformedURLException e_abs) {
> // not found
> System.err.println("Invalid Image URL : " + e_abs.getMessage() +
"(base URL " + context_url.toString() + ")");
> return null;
> }
> }
> catch (MalformedURLException e_context) {
> // pb context url
> System.err.println("Invalid Image URL - error on relative URL : " +
e_context.getMessage());
> return null;
> }
> */
> System.err.println("Invalid Image URL : " + e.getMessage());
> return null;
> }
>
> int imageType = -1;
>
> // try to get image type by 'Content-Type'
> try {
> URLConnection url_conn = absoluteURL.openConnection();
> String contentType = url_conn.getContentType();
> if ( contentType != null ) {
> if ( ( contentType.equalsIgnoreCase("image/gif") ) ||
> ( contentType.equalsIgnoreCase("image/x-gif") ) ) {
> imageType = IMAGE_GIF;
> } else if ( ( contentType.equalsIgnoreCase("image/jpeg") ) ||
> ( contentType.equalsIgnoreCase("image/x-jpeg") ) ) {
> imageType = IMAGE_JPEG;
> } else if ( ( contentType.equalsIgnoreCase("image/png") ) ||
> ( contentType.equalsIgnoreCase("image/x-png") ) ) {
> imageType = IMAGE_PNG;
> } else if ( ( contentType.equalsIgnoreCase("image/tga") ) ||
> ( contentType.equalsIgnoreCase("image/x-tga") ) ) {
> imageType = IMAGE_TGA;
> } else if ( contentType.equalsIgnoreCase("image/dib") ) {
> imageType = IMAGE_DIB;
> } else if ( ( contentType.equalsIgnoreCase("image/ddb") ) ||
> ( contentType.equalsIgnoreCase("image/x-ddb") ) ) {
> imageType = IMAGE_DDB;
> } else if ( ( contentType.equalsIgnoreCase("image/bmp") ) ||
> ( contentType.equalsIgnoreCase("image/x-bmp") ) ||
> ( contentType.equalsIgnoreCase("image/x-ms-bmp") ) ) {
> imageType = IMAGE_BMP;
> } else if ( ( contentType.equalsIgnoreCase("image/pict") ) ||
> ( contentType.equalsIgnoreCase("image/x-pict") ) ) {
> imageType = IMAGE_PICT;
> } else if ( ( contentType.equalsIgnoreCase("image/psd") ) ||
> ( contentType.equalsIgnoreCase("image/x-psd") ) ) {
> imageType = IMAGE_PSD;
> } else if ( ( contentType.equalsIgnoreCase("image/cmu-raster") ) ||
> ( contentType.equalsIgnoreCase("image/x-cmu-raster") ) ) {
> imageType = IMAGE_CMU_RASTER;
> } else if ( ( contentType.equalsIgnoreCase("image/tiff") ) ||
> ( contentType.equalsIgnoreCase("image/x-tiff") ) ) {
> imageType = IMAGE_TIFF;
> } else if ( ( contentType.equalsIgnoreCase("image/xbm") ) ||
> ( contentType.equalsIgnoreCase("image/x-xbm") ) ||
> ( contentType.equalsIgnoreCase("image/x-xbitmap") ) ) {
> imageType = IMAGE_XBM;
> } else if ( ( contentType.equalsIgnoreCase("image/xpm") ) ||
> ( contentType.equalsIgnoreCase("image/x-xpm") ) ||
> ( contentType.equalsIgnoreCase("image/x-xbitmap") ) ) {
> imageType = IMAGE_XPM;
> } else if ( ( contentType.equalsIgnoreCase("image/ico") ) ||
> ( contentType.equalsIgnoreCase("image/x-ico") ) ) {
> imageType = IMAGE_ICO;
> } else if ( ( contentType.equalsIgnoreCase("image/cur") ) ||
> ( contentType.equalsIgnoreCase("image/x-cur") ) ) {
> imageType = IMAGE_CUR;
> } else if ( ( contentType.equalsIgnoreCase("image/pcx") ) ||
> ( contentType.equalsIgnoreCase("image/x-pcx") ) ) {
> imageType = IMAGE_PCX;
> }
> }
> }
> catch (IOException e) {
> System.err.println("Error while recovering Image Type : " +
e.getMessage());
> return null;
> }
>
> // try get image type by extension if the first method failed
> if ( imageType < 0 ) {
> String stringURL = absoluteURL.toString();
> int extensionStart = stringURL.lastIndexOf(".");
> String extensionURL = "";
> if ( ( extensionStart > 0 ) && ( extensionStart < stringURL.length() -
1 ) ) {
> extensionURL = stringURL.substring(extensionStart + 1,
stringURL.length());
> }
>
> if ( extensionURL.equalsIgnoreCase("gif") ) {
> imageType = IMAGE_GIF;
> } else if ( ( extensionURL.equalsIgnoreCase("jpeg") ) ||
> ( extensionURL.equalsIgnoreCase("jpg") ) ||
> ( extensionURL.equalsIgnoreCase("jpe") ) ) {
> imageType = IMAGE_JPEG;
> } else if ( extensionURL.equalsIgnoreCase("png") ) {
> imageType = IMAGE_PNG;
> } else if ( extensionURL.equalsIgnoreCase("tga") ) {
> imageType = IMAGE_TGA;
> } else if ( extensionURL.equalsIgnoreCase("dib") ) {
> imageType = IMAGE_DIB;
> } else if ( extensionURL.equalsIgnoreCase("ddb") ) {
> imageType = IMAGE_DDB;
> } else if ( extensionURL.equalsIgnoreCase("bmp") ) {
> imageType = IMAGE_BMP;
> } else if ( extensionURL.equalsIgnoreCase("pict") ) {
> // ?
> imageType = IMAGE_PICT;
> } else if ( extensionURL.equalsIgnoreCase("psd") ) {
> imageType = IMAGE_PSD;
> } else if ( extensionURL.equalsIgnoreCase("ras") ) {
> // ?
> imageType = IMAGE_CMU_RASTER;
> } else if ( ( extensionURL.equalsIgnoreCase("tiff") ) ||
> ( extensionURL.equalsIgnoreCase("tif") ) ) {
> imageType = IMAGE_TIFF;
> } else if ( extensionURL.equalsIgnoreCase("xbm") ) {
> imageType = IMAGE_XBM;
> } else if ( extensionURL.equalsIgnoreCase("xpm") ) {
> imageType = IMAGE_XPM;
> } else if ( extensionURL.equalsIgnoreCase("ico") ) {
> imageType = IMAGE_ICO;
> } else if ( extensionURL.equalsIgnoreCase("cur") ) {
> imageType = IMAGE_CUR;
> } else if ( extensionURL.equalsIgnoreCase("pcx") ) {
> imageType = IMAGE_PCX;
> }
> }
>
> // if image type not defined
> if ( imageType < 0 ) {
> System.err.println("Image format not supported (" +
absoluteURL.toString() + ")");
> return null;
> }
>
> // Load the right class
> FopImage image = null;
> if ( imageType == IMAGE_GIF ||
> imageType == IMAGE_JPEG ) {
> try {
> image = new GifJpegImage(absoluteURL, x, y, width, height);
> } catch (InstantiationException e) {
> System.err.println(e.getMessage());
> return null;
> }
> return image;
> } else if ( imageType == IMAGE_PNG ||
> imageType == IMAGE_TGA ||
> imageType == IMAGE_DIB ||
> imageType == IMAGE_DDB ||
> imageType == IMAGE_BMP ||
> imageType == IMAGE_PICT ||
> imageType == IMAGE_PSD ||
> imageType == IMAGE_CMU_RASTER ||
> imageType == IMAGE_TIFF ||
> imageType == IMAGE_XBM ||
> imageType == IMAGE_XPM ||
> imageType == IMAGE_ICO ||
> imageType == IMAGE_CUR ||
> imageType == IMAGE_PCX ) {
> try {
> image = new JimiImage(absoluteURL, x, y, width, height);
> } catch (InstantiationException e) {
> System.err.println(e.getMessage());
> return null;
> }
> return image;
> }
> /* else if ( ... */
>
> // no Image Producer
> System.err.println("Image format not supported (" +
absoluteURL.toString() + ")");
> return null;
>
> }
> }
>
Index: ./src/org/apache/fop/image/GifJpegImage.java
===================================================================
RCS file:
/home/cvspublic/xml-fop/src/org/apache/fop/image/GifJpegImage.java,v
retrieving revision 1.6
diff -r1.6 GifJpegImage.java
64a65,68
> import java.lang.Math;
>
> import java.util.zip.Deflater;
>
67,77c71,81
< int X;
< int Y;
< int width;
< int height;
< int pixelwidth;
< int pixelheight;
< String ref;
< boolean color = true;
< int bitperpixel = 8;
< int[] imagemap;
< int[] tempmap;
---
> int X = 0;
> int Y = 0;
> int width = 0;
> int height = 0;
> int pixelWidth = 0;
> int pixelHeight = 0;
> URL HRef = null;
> boolean colored = true;
> int bitsPerPixel = 8;
> byte[] compressedMap;
> int compressedSize = 0;
82c86,87
< public GifJpegImage(String href, int x, int y, int w, int h) {
---
> public GifJpegImage(URL href, int x, int y, int w, int h)
> throws InstantiationException {
84c89
< this.ref = href;
---
> this.HRef = href;
87,88c92,95
< this.pixelheight = -1;
< this.pixelwidth = -1;
---
> this.width = w;
> this.height = h;
> this.pixelHeight = -1;
> this.pixelWidth = -1;
89a97,98
> int[] tempMap;
>
91,129c100,162
< URL url = new URL(href);
< ImageProducer ip = (ImageProducer)url.getContent();
< FopImageConsumer consumer = new FopImageConsumer(this);
< ip.startProduction(consumer);
< synchronized (imageWait) {
< imageWait.wait();
< }
< /* this only works on windows, not on linux
< while ((this.pixelheight = consumer.getHeight())==-1) {}
< while ((this.pixelwidth = consumer.getWidth())==-1) {}
< */
< this.tempmap = new int[this.pixelwidth * this.pixelheight];
<
< PixelGrabber pg = new
< PixelGrabber(ip, 0, 0,
< this.pixelwidth, this.pixelheight,
< this.tempmap, 0, w);
< try {
< pg.grabPixels();
< } catch (InterruptedException e) {
< System.err.println("Image grabbing interrupted");
< }
< } catch (ClassCastException e) {
< System.err.println("Image format not supported: " + href);
< } catch (Exception e) {
< System.err.println("Error loading image " + href +
< " : " + e);
< }
<
< if (w==0) {
< this.width=this.pixelwidth*1000;
< } else {
< this.width=w;
< }
<
< if (h==0) {
< this.height=this.pixelheight*1000;
< } else {
< this.height=h;
---
> ImageProducer ip = (ImageProducer)HRef.getContent();
> FopImageConsumer consumer = new FopImageConsumer(this);
> ip.startProduction(consumer);
>
> while (! consumer.isImageReady() ) {}
> this.pixelHeight = consumer.getHeight();
> this.pixelWidth = consumer.getWidth();
>
> tempMap = new int[this.pixelWidth * this.pixelHeight];
>
> PixelGrabber pg = new PixelGrabber( ip,
> 0, 0,
> this.pixelWidth, this.pixelHeight,
> tempMap,
> 0, this.pixelWidth
> );
> try {
> pg.grabPixels();
> } catch (InterruptedException e) {
> // System.err.println("Image grabbing interrupted");
> throw new InstantiationException("Image grabbing interrupted");
> }
>
> } catch (Exception e) {
> // System.err.println("Error loading image " + HRef.toString() + " : " +
e.getClass() + " - " + e.getMessage());
> throw new InstantiationException("Error loading image " +
HRef.toString() + " : " + e.getClass() + " - " + e.getMessage());
> }
>
> // try to Deflat the image
> Deflater compressor = new Deflater();
> compressor.setLevel(Deflater.DEFAULT_COMPRESSION);
> compressor.setStrategy(Deflater.DEFAULT_STRATEGY);
> byte[] byteTempMap = new byte[this.pixelWidth * this.pixelHeight * 3];
> for (int i = 0; i < this.pixelHeight; i++) {
> for (int j = 0; j < this.pixelWidth; j++) {
> int p = tempMap[i * this.pixelWidth + j];
> int r = (p >> 16) & 0xFF;
> int g = (p >> 8) & 0xFF;
> int b = (p ) & 0xFF;
> byteTempMap[3 * (i * this.pixelWidth + j)] = (byte) (r & 0xFF);
> byteTempMap[3 * (i * this.pixelWidth + j) + 1] = (byte) (g & 0xFF);
> byteTempMap[3 * (i * this.pixelWidth + j) + 2] = (byte) (b & 0xFF);
> }
> }
> compressor.setInput(byteTempMap);
> compressor.finish();
> compressedMap = new byte[this.pixelWidth * this.pixelHeight * 3];
> compressedSize = compressor.deflate(compressedMap);
> compressor.end();
>
> tempMap = null;
> byteTempMap = null;
>
> double ratio = ((double) pixelWidth) / ((double) pixelHeight);
>
> // found formatting size
> if ( ( width == 0 ) && ( height == 0 ) ) {
> height = (int) (1000.0 * ((double) pixelHeight) / 2.0);
> width = (int) (1000.0 * ((double) pixelWidth) / 2.0);
> } else if ( width == 0 ) {
> width = (int) (ratio * ((double) height));
> } else if ( height == 0 ) {
> height = (int) (((double) width) / ratio);
130a164
>
132,149c166
<
< public static class FopImageConsumer implements ImageConsumer {
< int width = -1;
< int height = -1;
< GifJpegImage graphic;
<
< public FopImageConsumer(GifJpegImage graphic) {
< this.graphic = graphic;
< }
<
< public void imageComplete(int status) {
< synchronized(graphic.imageWait) {
< graphic.imageWait.notifyAll();
< }
< }
<
< public void setColorModel(ColorModel model) {}
<
---
>
151,172c168,169
< this.width = width;
< this.height = height;
< }
<
< public void setHints(int hintflags) {}
<
< public void setPixels(int x, int y, int w, int h,
< ColorModel model, byte[] pixels,int off,
< int scansize) {}
<
< public void setPixels(int x, int y, int w, int h,
< ColorModel model, int[] pixels, int off,
< int scansize) {}
<
< public void setProperties(Hashtable props) {}
<
< public int getWidth() {
< return this.width;
< }
<
< public int getHeight() {
< return this.height;
---
> this.width = width;
> this.height = height;
174,177c171,240
< }
<
< public String gethref() {
< return this.ref;
---
>
> public static class FopImageConsumer implements ImageConsumer {
> int width = -1;
> int height = -1;
> GifJpegImage graphic;
> Integer imageStatus = new Integer(-1);
>
> public FopImageConsumer(GifJpegImage graphic) {
> this.graphic = graphic;
> }
>
> public void imageComplete(int status) {
> synchronized (this.imageStatus) {
> this.imageStatus = new Integer(status);
> }
> }
>
> public synchronized boolean isImageReady()
> throws Exception {
> synchronized (this.imageStatus) {
> if ( this.imageStatus.intValue() != -1 ) {
> String statusStr = null;
> if ( this.imageStatus.intValue() == ImageConsumer.IMAGEABORTED ) {
> statusStr = new String("Image Aborted");
> } else if ( this.imageStatus.intValue() == ImageConsumer.IMAGEERROR )
{
> statusStr = new String("Image Error");
> }
> if ( statusStr != null ) {
> throw new Exception("Error in image consumer (" + statusStr + ")");
> }
>
> if ( this.imageStatus.intValue() == ImageConsumer.STATICIMAGEDONE ) {
> return true;
> }
> }
> return false;
> }
> }
>
> public void setColorModel(ColorModel model) {}
>
> public void setDimensions(int width, int height) {
> this.width = width;
> this.height = height;
> }
>
> public void setHints(int hintflags) {}
>
> public void setPixels(int x, int y, int w, int h,
> ColorModel model, byte[] pixels,int off,
> int scansize) {}
>
> public void setPixels(int x, int y, int w, int h,
> ColorModel model, int[] pixels, int off,
> int scansize) {}
>
> public void setProperties(Hashtable props) {}
>
> public int getWidth() {
> return this.width;
> }
>
> public int getHeight() {
> return this.height;
> }
> }
>
> public String getHRef() {
> // need to be changed (URL everywhere...)
> return this.HRef.toString();
188,189c251,252
< public int getpixelwidth() {
< return this.pixelwidth;
---
> public int getPixelWidth() {
> return this.pixelWidth;
192,193c255,256
< public int getpixelheight() {
< return this.pixelheight;
---
> public int getPixelHeight() {
> return this.pixelHeight;
204,214c267,268
< public int[] getimagemap() {
< this.imagemap=new int[this.pixelheight * this.pixelwidth * 3];
< int count = 0;
< int i;
< for(i = 0; i < (this.pixelheight * this.pixelwidth); i++) {
< int red = ((this.tempmap[i]>>16) & 0xff);
< int green = ((this.tempmap[i]>> 8) & 0xff);
< int blue = ((this.tempmap[i] ) & 0xff);
< this.imagemap[count++]=red;
< this.imagemap[count++]=green;
< this.imagemap[count++]=blue;
---
> public int getImageSize() {
> return this.compressedSize;
216,219c270,275
< return imagemap;
< }
<
< public boolean getcolor() {
---
>
> public byte[] getImageMap() {
> return this.compressedMap;
> }
>
> public boolean isColored() {
223,224c279,280
< public int getbitperpixel() {
< return this.bitperpixel;
---
> public int getBitsPerPixel() {
> return this.bitsPerPixel;
Index: ./src/org/apache/fop/image/ImageArea.java
===================================================================
RCS file: /home/cvspublic/xml-fop/src/org/apache/fop/image/ImageArea.java,v
retrieving revision 1.5
diff -r1.5 ImageArea.java
56a57
> import org.apache.fop.fo.properties.TextAlign;
69,88c70,89
< super(fontState,width,height);
< this.currentHeight = height;
< this.contentRectangleWidth = width;
< this.image = img;
<
< switch (align) {
< case 1:
< xOffset = startIndent;
< break;
< case 2:
< if (endIndent == 0)
< endIndent = AllocationWidth;
< xOffset = (endIndent - width);
< break;
< case 3:
< case 4:
< if (endIndent == 0)
< endIndent = AllocationWidth;
< xOffset = startIndent + ((endIndent - startIndent) - width)/2;
< break;
---
>
> super(fontState,width,height);
> this.currentHeight = height;
> this.contentRectangleWidth = width;
> this.image = img;
>
> switch ( align ) {
> case TextAlign.START:
> xOffset = startIndent;
> break;
> case TextAlign.END:
> xOffset = AllocationWidth - endIndent - width;
> break;
> case TextAlign.JUSTIFIED:
> xOffset = startIndent;
> break;
> case TextAlign.CENTERED:
> xOffset = startIndent + (int) (((double) (AllocationWidth -
startIndent - endIndent - width)) / 2.0 );
> break;
> }
90d90
< }
Index: ./src/org/apache/fop/image/Makefile
===================================================================
RCS file: /home/cvspublic/xml-fop/src/org/apache/fop/image/Makefile,v
retrieving revision 1.2
diff -r1.2 Makefile
8,10c8
< SOURCES=BmpBwImage.java \
< BmpColImage.java \
< ImageArea.java \
---
> SOURCES=ImageArea.java \
13c11,12
< GifJpegImage.java
---
> GifJpegImage.java \
> JimiImage.java
Index: ./src/org/apache/fop/render/pdf/PDFRenderer.java
===================================================================
RCS file:
/home/cvspublic/xml-fop/src/org/apache/fop/render/pdf/PDFRenderer.java,v
retrieving revision 1.9
diff -r1.9 PDFRenderer.java
283c283,284
< this.currentYPosition -= h*1000;
---
> // this.currentYPosition -= h*1000;
> this.currentYPosition -= h;
286a288,314
> int pw = img.getPixelWidth();
> int ph = img.getPixelHeight();
> byte[] pixels = img.getImageMap();
> int size = img.getImageSize();
>
> float rx = (x + img.getX())/1000f;
> float ry = (y + img.getY())/1000f;
> float rh = img.getHeight()/1000f;
> float rw = img.getWidth()/1000f;
>
> currentStream.add("ET\nq\n" + rw + " 0 0 " + rh + " " + rx + " " +
(ry-rh)
> + " cm\nBI\n/W " + pw + "\n/H " + ph +
> "\n/BPC 8\n/CS /RGB\n/F [/ASCIIHexDecode /FlateDecode]\nID\n");
>
> // currentStream.addImageArray(pixels, pw, ph);
> for (int i = 0; i < size; i++) {
> int val = (int) (pixels[i] & 0xFF);
> if ( val < 16 ) {
> this.currentStream.add(Integer.toString(0));
> }
> this.currentStream.add(Integer.toHexString(val));
> }
> this.currentStream.add(">\n");
>
> currentStream.add("EI\nQ\nBT\n");
>
> /*
287a316,321
>
> currentStream.add("ET\nq\n" + rw + " 0 0 " + rh + " " + rx + " " + (ry -
h)
> + " cm\n/Im" + xObjectNum + " Do\nQ\nBT\n");
> */
> //// TEST ////
> /* int xObjectNum = this.pdfDoc.addImage(img);
294c328
< }
---
> */ }
The command completed successfully.
========================================================
JimiImage.java
========================================================
package org.apache.fop.image;
// Java
import java.util.Hashtable;
import java.net.URL;
import java.io.IOException;
import java.io.PrintWriter;
import java.awt.image.*;
import java.awt.*;
// png compression
import java.util.zip.Deflater;
// Jimi
import com.sun.jimi.core.*;
public class JimiImage implements FopImage {
int X = 0;
int Y = 0;
int width = 0;
int height = 0;
int pixelWidth = 0;
int pixelHeight = 0;
URL HRef = null;
boolean colored = true;
int bitsPerPixel = 8;
byte[] compressedMap;
int compressedSize = 0;
public JimiImage(URL href, int x, int y, int w, int h)
throws InstantiationException {
this.HRef = href;
this.X = x;
this.Y = y;
this.width = w;
this.height = h;
this.pixelHeight = -1;
this.pixelWidth = -1;
int[] tempMap;
try {
ImageProducer ip = Jimi.getImageProducer(href, Jimi.SYNCHRONOUS |
Jimi.IN_MEMORY);
FopImageConsumer consumer = new FopImageConsumer(this);
ip.startProduction(consumer);
while (! consumer.isImageReady() ) {}
this.pixelHeight = consumer.getHeight();
this.pixelWidth = consumer.getWidth();
tempMap = new int[this.pixelWidth * this.pixelHeight];
PixelGrabber pg = new PixelGrabber( ip,
0, 0,
this.pixelWidth, this.pixelHeight,
tempMap,
0, this.pixelWidth
);
try {
pg.grabPixels();
} catch (InterruptedException e) {
// System.err.println("Image grabbing interrupted");
throw new InstantiationException("Image grabbing interrupted");
}
} catch (Exception e) {
// System.err.println("Error loading image " + HRef.toString() + " : " +
e.getClass() + " - " + e.getMessage());
throw new InstantiationException("Error loading image " + HRef.toString()
+ " : " + e.getClass() + " - " + e.getMessage());
}
// try to Deflat the image
Deflater compressor = new Deflater();
compressor.setLevel(Deflater.DEFAULT_COMPRESSION);
compressor.setStrategy(Deflater.DEFAULT_STRATEGY);
byte[] byteTempMap = new byte[this.pixelWidth * this.pixelHeight * 3];
for (int i = 0; i < this.pixelHeight; i++) {
for (int j = 0; j < this.pixelWidth; j++) {
int p = tempMap[i * this.pixelWidth + j];
int r = (p >> 16) & 0xFF;
int g = (p >> 8) & 0xFF;
int b = (p ) & 0xFF;
byteTempMap[3 * (i * this.pixelWidth + j)] = (byte) (r & 0xFF);
byteTempMap[3 * (i * this.pixelWidth + j) + 1] = (byte) (g & 0xFF);
byteTempMap[3 * (i * this.pixelWidth + j) + 2] = (byte) (b & 0xFF);
}
}
compressor.setInput(byteTempMap);
compressor.finish();
compressedMap = new byte[this.pixelWidth * this.pixelHeight * 3];
compressedSize = compressor.deflate(compressedMap);
compressor.end();
tempMap = null;
byteTempMap = null;
double ratio = ((double) pixelWidth) / ((double) pixelHeight);
// found formatting size
if ( ( width == 0 ) && ( height == 0 ) ) {
height = (int) (1000.0d * ((double) pixelHeight) / 2.0d);
width = (int) (1000.0d * ((double) pixelWidth) / 2.0d);
} else if ( width == 0 ) {
width = (int) (ratio * ((double) height));
} else if ( height == 0 ) {
height = (int) (((double) width) / ratio);
}
}
public void setDimensions(int width, int height) {
this.width = width;
this.height = height;
}
public static class FopImageConsumer implements ImageConsumer {
int width = -1;
int height = -1;
JimiImage graphic;
Integer imageStatus = new Integer(-1);
public FopImageConsumer(JimiImage graphic) {
this.graphic = graphic;
}
public void imageComplete(int status) {
synchronized (this.imageStatus) {
this.imageStatus = new Integer(status);
}
}
public synchronized boolean isImageReady()
throws Exception {
synchronized (this.imageStatus) {
if ( this.imageStatus.intValue() != -1 ) {
String statusStr = null;
if ( this.imageStatus.intValue() == ImageConsumer.IMAGEABORTED ) {
statusStr = new String("Image Aborted");
} else if ( this.imageStatus.intValue() == ImageConsumer.IMAGEERROR ) {
statusStr = new String("Image Error");
}
if ( statusStr != null ) {
throw new Exception("Error in image consumer (" + statusStr + ")");
}
if ( this.imageStatus.intValue() == ImageConsumer.STATICIMAGEDONE ) {
return true;
}
}
return false;
}
}
public void setColorModel(ColorModel model) {}
public void setDimensions(int width, int height) {
this.width = width;
this.height = height;
}
public void setHints(int hintflags) {}
public void setPixels(int x, int y, int w, int h,
ColorModel model, byte[] pixels,int off,
int scansize) {}
public void setPixels(int x, int y, int w, int h,
ColorModel model, int[] pixels, int off,
int scansize) {}
public void setProperties(Hashtable props) {}
public int getWidth() {
return this.width;
}
public int getHeight() {
return this.height;
}
}
public String getHRef() {
// need to be changed (URL everywhere...)
return this.HRef.toString();
}
public int getWidth() {
return this.width;
}
public int getHeight() {
return this.height;
}
public int getPixelWidth() {
return this.pixelWidth;
}
public int getPixelHeight() {
return this.pixelHeight;
}
public int getX() {
return this.X;
}
public int getY() {
return this.Y;
}
public int getImageSize() {
return this.compressedSize;
}
public byte[] getImageMap() {
return this.compressedMap;
}
public boolean isColored() {
return true;
}
public int getBitsPerPixel() {
return this.bitsPerPixel;
}
}
_______________________________________
Eric SCHAEFFER
eschaeffer@posterconseil.com
POSTER CONSEIL
118 rue de Tocqueville
75017 PARIS
FRANCE
Tel. : 33-140541058
Fax : 33-140541059
_______________________________________