| 1 |
// Copyright 2000-2007 FreeHEP |
| 2 |
package org.freehep.graphicsio.pdf; |
| 3 |
|
| 4 |
import org.freehep.graphicsio.ImageConstants; |
| 5 |
import org.freehep.graphicsio.ImageGraphics2D; |
| 6 |
|
| 7 |
import java.awt.image.RenderedImage; |
| 8 |
import java.awt.Color; |
| 9 |
import java.io.IOException; |
| 10 |
|
| 11 |
/** |
| 12 |
* PDF writes images as ZLIB or JPEG. This class converts an image to a |
| 13 |
* byte[]. If neither {@link ImageConstants#ZLIB} nor |
| 14 |
* {@link ImageConstants#JPEG} is passed the smallest size is stored |
| 15 |
* for {@link #getBytes()}. The method {@link #getFormat()} returns the |
| 16 |
* used format. |
| 17 |
* |
| 18 |
* @author Steffen Greiffenberg |
| 19 |
* @version $Revision$ |
| 20 |
*/ |
| 21 |
class ImageBytes { |
| 22 |
|
| 23 |
/** |
| 24 |
* Used format during creation of bytes |
| 25 |
*/ |
| 26 |
private String format; |
| 27 |
|
| 28 |
/** |
| 29 |
* Bytes in given format |
| 30 |
*/ |
| 31 |
private byte[] bytes; |
| 32 |
|
| 33 |
/** |
| 34 |
* Encodes the passed image |
| 35 |
* |
| 36 |
* @param image image to convert |
| 37 |
* @param bkg background color |
| 38 |
* @param format format could be {@link ImageConstants#ZLIB} or {@link ImageConstants#JPEG} |
| 39 |
* @param colorModel e.g. {@link org.freehep.graphicsio.ImageConstants#COLOR_MODEL_RGB} |
| 40 |
* @throws IOException thrown by {@link org.freehep.graphicsio.ImageGraphics2D#toByteArray(java.awt.image.RenderedImage, String, String, java.util.Properties)} |
| 41 |
*/ |
| 42 |
public ImageBytes(RenderedImage image, Color bkg, String format, String colorModel) throws IOException { |
| 43 |
|
| 44 |
// ZLIB encoding, transparent images allways require ZLIB |
| 45 |
if (ImageConstants.ZLIB.equals(format) || (image.getColorModel().hasAlpha() && (bkg == null))) { |
| 46 |
bytes = toZLIB(image, bkg, colorModel); |
| 47 |
this.format = ImageConstants.ZLIB; |
| 48 |
} |
| 49 |
|
| 50 |
// JPG encoding |
| 51 |
else if (ImageConstants.JPEG.equals(format)) { |
| 52 |
bytes = toJPG(image); |
| 53 |
this.format = ImageConstants.JPG; |
| 54 |
} else { |
| 55 |
// calculate both byte arrays |
| 56 |
byte[] jpgBytes = toJPG(image); |
| 57 |
byte[] zlibBytes = toZLIB(image, bkg, colorModel); |
| 58 |
|
| 59 |
// compare sizes to determine smalles format |
| 60 |
if (jpgBytes.length < 0.5 * zlibBytes.length) { |
| 61 |
bytes = jpgBytes; |
| 62 |
this.format = ImageConstants.JPG; |
| 63 |
} else { |
| 64 |
bytes = zlibBytes; |
| 65 |
this.format = ImageConstants.ZLIB; |
| 66 |
} |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Creates the ZLIB Bytes for PDF images |
| 72 |
* |
| 73 |
* @param image image to convert |
| 74 |
* @param bkg background color |
| 75 |
* @param colorModel e.g. {@link org.freehep.graphicsio.ImageConstants#COLOR_MODEL_RGB} |
| 76 |
* @return bytes |
| 77 |
* @throws IOException thrown by {@link org.freehep.graphicsio.ImageGraphics2D#toByteArray(java.awt.image.RenderedImage, String, String, java.util.Properties)} |
| 78 |
*/ |
| 79 |
private byte[] toZLIB(RenderedImage image, Color bkg, String colorModel) throws IOException { |
| 80 |
return ImageGraphics2D.toByteArray( |
| 81 |
image, |
| 82 |
ImageConstants.RAW, |
| 83 |
ImageConstants.ENCODING_FLATE_ASCII85, |
| 84 |
ImageGraphics2D.getRAWProperties(bkg, colorModel)); |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Creates the JPG bytes for PDF images |
| 89 |
* |
| 90 |
* @param image image to convert |
| 91 |
* @return bytes |
| 92 |
* @throws IOException thrown by {@link org.freehep.graphicsio.ImageGraphics2D#toByteArray(java.awt.image.RenderedImage, String, String, java.util.Properties)} |
| 93 |
*/ |
| 94 |
private byte[] toJPG(RenderedImage image) throws IOException { |
| 95 |
return ImageGraphics2D.toByteArray( |
| 96 |
image, |
| 97 |
ImageConstants.JPG, |
| 98 |
ImageConstants.ENCODING_ASCII85, |
| 99 |
null); |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* @return bytes |
| 104 |
*/ |
| 105 |
public byte[] getBytes() { |
| 106 |
return bytes; |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* @return used encoding format |
| 111 |
*/ |
| 112 |
public String getFormat() { |
| 113 |
return format; |
| 114 |
} |
| 115 |
} |
| 116 |
/** |
| 117 |
* $Log$ |
| 118 |
*/ |