cjolif 01/07/04 09:50:07
Modified: sources/org/apache/batik/ext/awt/image/codec
PNGImageEncoder.java
sources/org/apache/batik/svggen
ImageHandlerBase64Encoder.java
ImageHandlerPNGEncoder.java
sources/org/apache/batik/transcoder/image PNGTranscoder.java
Log:
remove close() at the end of the PNGImageEncoder encoding method =>
add close() after using the PNGImageEncoder at several places.
close() has been removed for two reasons:
- consistency with Base64Encoder
- a user encounter a problem with twice closing a stream
Revision Changes Path
1.3 +35 -87
xml-batik/sources/org/apache/batik/ext/awt/image/codec/PNGImageEncoder.java
Index: PNGImageEncoder.java
===================================================================
RCS file:
/home/cvs/xml-batik/sources/org/apache/batik/ext/awt/image/codec/PNGImageEncoder.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- PNGImageEncoder.java 2001/02/02 01:12:59 1.2
+++ PNGImageEncoder.java 2001/07/04 16:49:59 1.3
@@ -60,14 +60,14 @@
class ChunkStream extends OutputStream implements DataOutput {
-
+
private String type;
private ByteArrayOutputStream baos;
private DataOutputStream dos;
public ChunkStream(String type) throws IOException {
this.type = type;
-
+
this.baos = new ByteArrayOutputStream();
this.dos = new DataOutputStream(baos);
}
@@ -91,7 +91,7 @@
public void writeByte(int v) throws IOException {
dos.writeByte(v);
}
-
+
public void writeBytes(String s) throws IOException {
dos.writeBytes(s);
}
@@ -264,7 +264,7 @@
private byte[] greenPalette = null;
private byte[] bluePalette = null;
private byte[] alphaPalette = null;
-
+
private DataOutputStream dataOutput;
public PNGImageEncoder(OutputStream output,
@@ -290,7 +290,7 @@
cs.writeByte((byte)0);
cs.writeByte((byte)0);
cs.writeByte(interlace ? (byte)1 : (byte)0);
-
+
cs.writeToStream(dataOutput);
}
@@ -311,12 +311,12 @@
int minY = ras.getMinY();
int width = ras.getWidth();
int height = ras.getHeight();
-
+
xOffset *= numBands;
xSkip *= numBands;
int samplesPerByte = 8/bitDepth;
-
+
int numSamples = width*numBands;
int[] samples = new int[numSamples];
@@ -356,7 +356,7 @@
switch (bitDepth) {
case 1: case 2: case 4:
// Image can only have a single band
-
+
int mask = samplesPerByte - 1;
for (int s = xOffset; s < numSamples; s += xSkip) {
int val = clamp(samples[s] >> bitShift, maxValue);
@@ -399,10 +399,10 @@
int filterType = param.filterRow(currRow, prevRow,
filteredRows,
bytesPerRow, bpp);
-
+
os.write(filterType);
os.write(filteredRows[filterType], bpp, bytesPerRow);
-
+
// Swap current and previous rows
byte[] swap = currRow;
currRow = prevRow;
@@ -465,7 +465,7 @@
private void writeCHRM() throws IOException {
if (param.isChromaticitySet() || param.isSRGBIntentSet()) {
ChunkStream cs = new ChunkStream("cHRM");
-
+
float[] chroma;
if (!param.isSRGBIntentSet()) {
chroma = param.getChromaticity();
@@ -490,7 +490,7 @@
} else {
gamma = 1.0F/2.2F; // SRGB gamma
}
-
+
cs.writeInt((int)(gamma*100000));
cs.writeToStream(dataOutput);
}
@@ -538,7 +538,7 @@
cs.writeByte(greenPalette[i]);
cs.writeByte(bluePalette[i]);
}
-
+
cs.writeToStream(dataOutput);
}
@@ -567,7 +567,7 @@
cs.writeShort(rgb[2]);
break;
}
-
+
cs.writeToStream(dataOutput);
}
}
@@ -575,7 +575,7 @@
private void writeHIST() throws IOException {
if (param.isPaletteHistogramSet()) {
ChunkStream cs = new ChunkStream("hIST");
-
+
int[] hist = param.getPaletteHistogram();
for (int i = 0; i < hist.length; i++) {
cs.writeShort(hist[i]);
@@ -586,7 +586,7 @@
}
private void writeTRNS() throws IOException {
- if (param.isTransparencySet() &&
+ if (param.isTransparencySet() &&
(colorType != PNG_COLOR_GRAY_ALPHA) &&
(colorType != PNG_COLOR_RGB_ALPHA)) {
ChunkStream cs = new ChunkStream("tRNS");
@@ -656,7 +656,7 @@
Date date = param.getModificationTime();
TimeZone gmt = TimeZone.getTimeZone("GMT");
-
+
GregorianCalendar cal = new GregorianCalendar(gmt);
cal.setTime(date);
@@ -681,7 +681,7 @@
private void writeTEXT() throws IOException {
if (param.isTextSet()) {
String[] text = param.getText();
-
+
for (int i = 0; i < text.length/2; i++) {
byte[] keyword = text[2*i].getBytes();
byte[] value = text[2*i + 1].getBytes();
@@ -700,7 +700,7 @@
private void writeZTXT() throws IOException {
if (param.isCompressedTextSet()) {
String[] text = param.getCompressedText();
-
+
for (int i = 0; i < text.length/2; i++) {
byte[] keyword = text[2*i].getBytes();
byte[] value = text[2*i + 1].getBytes();
@@ -727,7 +727,7 @@
char char3 = type.charAt(3);
byte[] data = param.getPrivateChunkData(i);
-
+
ChunkStream cs = new ChunkStream(type);
cs.write(data);
cs.writeToStream(dataOutput);
@@ -763,7 +763,7 @@
byte alpha = alphaPalette[i];
if (alpha == (byte)0) {
param.setTransparentGray(i);
-
+
++numTransparent;
if (numTransparent > 1) {
return null;
@@ -776,6 +776,12 @@
return param;
}
+ /**
+ * This method encodes a <code>RenderedImage</code> into PNG.
+ * The stream into which the PNG is dumped is not closed at
+ * the end of the operation, this should be done if needed
+ * by the caller of this method.
+ */
public void encode(RenderedImage im) throws IOException {
this.image = im;
this.width = image.getWidth();
@@ -800,7 +806,7 @@
this.bitShift = paramg.getBitShift();
}
}
-
+
// Get bit depth from image if not set in param
if (this.bitDepth == -1) {
// Get bit depth from channel 0 of the image
@@ -812,7 +818,7 @@
throw new RuntimeException();
}
}
-
+
// Round bit depth up to a power of 2
if (bitDepth > 2 && bitDepth < 4) {
bitDepth = 4;
@@ -827,7 +833,7 @@
this.numBands = sampleModel.getNumBands();
this.bpp = numBands*((bitDepth == 16) ? 2 : 1);
-
+
ColorModel colorModel = image.getColorModel();
if (colorModel instanceof IndexColorModel) {
if (bitDepth < 1 || bitDepth > 8) {
@@ -839,12 +845,12 @@
IndexColorModel icm = (IndexColorModel)colorModel;
int size = icm.getMapSize();
-
+
redPalette = new byte[size];
greenPalette = new byte[size];
bluePalette = new byte[size];
alphaPalette = new byte[size];
-
+
icm.getReds(redPalette);
icm.getGreens(greenPalette);
icm.getBlues(bluePalette);
@@ -870,7 +876,7 @@
if (parami.isPaletteSet()) {
int[] palette = parami.getPalette();
size = palette.length/3;
-
+
int index = 0;
for (int i = 0; i < size; i++) {
redPalette[i] = (byte)palette[index++];
@@ -895,7 +901,7 @@
if (param == null) {
param = new PNGEncodeParam.Gray();
}
-
+
if (param.isTransparencySet()) {
skipAlpha = true;
numBands = 1;
@@ -940,7 +946,7 @@
writeICCP();
writeSBIT();
writeSRGB();
-
+
writePLTE();
writeHIST();
@@ -960,63 +966,5 @@
writeIEND();
dataOutput.flush();
- dataOutput.close();
}
-
-// public static void main(String[] args) {
-// try {
-// SeekableStream stream = new FileSeekableStream(args[0]);
-// String[] names = ImageCodec.getDecoderNames(stream);
-
-// ImageDecoder dec =
-// ImageCodec.createImageDecoder(names[0], stream, null);
-// RenderedImage im = dec.decodeAsRenderedImage();
-
-// OutputStream output = new FileOutputStream(args[1]);
-
-// PNGEncodeParam param = null;
-// Object o = im.getProperty("encode_param");
-// if ((o != null) && (o instanceof PNGEncodeParam)) {
-// param = (PNGEncodeParam)o;
-// } else {
-// param = PNGEncodeParam.getDefaultEncodeParam(im);
-// }
-
-// if (param instanceof PNGEncodeParam.RGB) {
-// int[] rgb = { 50, 100, 150 };
-// ((PNGEncodeParam.RGB)param).setBackgroundRGB(rgb);
-// }
-
-// param.setChromaticity(0.32270F, 0.319F,
-// 0.65F, 0.32F,
-// 0.31F, 0.58F,
-// 0.16F, 0.04F);
-
-// param.setGamma(3.5F);
-
-// int[] sbits = { 8, 8, 8 };
-// param.setSignificantBits(sbits);
-
-// param.setSRGBIntent(0);
-
-// String[] text = new String[4];
-// text[0] = "Title";
-// text[1] = "PNG Test Image";
-// text[2] = "Author";
-// text[3] = "Daniel Rice";
-// param.setText(text);
-
-// String[] ztext = new String[2];
-// ztext[0] = "Description";
-// ztext[1] = "A really incredibly long-winded description of extremely
little if any substance whatsoever.";
-// param.setCompressedText(ztext);
-
-// ImageEncoder enc = new PNGImageEncoder(output, param);
-// enc.encode(im);
-// } catch (Exception e) {
-// e.printStackTrace();
-// System.exit(1);
-// }
-// }
-
}
1.12 +1 -2
xml-batik/sources/org/apache/batik/svggen/ImageHandlerBase64Encoder.java
Index: ImageHandlerBase64Encoder.java
===================================================================
RCS file:
/home/cvs/xml-batik/sources/org/apache/batik/svggen/ImageHandlerBase64Encoder.java,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -r1.11 -r1.12
--- ImageHandlerBase64Encoder.java 2001/04/02 13:36:04 1.11
+++ ImageHandlerBase64Encoder.java 2001/07/04 16:50:01 1.12
@@ -28,7 +28,7 @@
* the data protocol.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Vincent Hardy</a>
- * @version $Id: ImageHandlerBase64Encoder.java,v 1.11 2001/04/02 13:36:04 cjolif
Exp $
+ * @version $Id: ImageHandlerBase64Encoder.java,v 1.12 2001/07/04 16:50:01 cjolif
Exp $
* @see org.apache.batik.svggen.SVGGraphics2D
* @see org.apache.batik.svggen.ImageHandler
*/
@@ -146,7 +146,6 @@
ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageEncoder encoder = new PNGImageEncoder(os, null);
encoder.encode(buf);
- os.flush();
os.close();
return os.toByteArray();
} catch(IOException e) {
1.8 +1 -2
xml-batik/sources/org/apache/batik/svggen/ImageHandlerPNGEncoder.java
Index: ImageHandlerPNGEncoder.java
===================================================================
RCS file:
/home/cvs/xml-batik/sources/org/apache/batik/svggen/ImageHandlerPNGEncoder.java,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- ImageHandlerPNGEncoder.java 2001/04/02 13:36:05 1.7
+++ ImageHandlerPNGEncoder.java 2001/07/04 16:50:02 1.8
@@ -27,7 +27,7 @@
* image elements it handles.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Vincent Hardy</a>
- * @version $Id: ImageHandlerPNGEncoder.java,v 1.7 2001/04/02 13:36:05 cjolif Exp $
+ * @version $Id: ImageHandlerPNGEncoder.java,v 1.8 2001/07/04 16:50:02 cjolif Exp $
* @see org.apache.batik.svggen.SVGGraphics2D
* @see org.apache.batik.svggen.ImageHandlerJPEGEncoder
* @see org.apache.batik.svggen.ImageHandlerPNGEncoder
@@ -71,7 +71,6 @@
OutputStream os = new FileOutputStream(imageFile);
ImageEncoder encoder = new PNGImageEncoder(os, null);
encoder.encode(buf);
- os.flush();
os.close();
} catch (IOException e) {
throw new SVGGraphics2DIOException(ERR_WRITE+imageFile.getName());
1.6 +2 -1
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.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- PNGTranscoder.java 2001/05/31 21:10:26 1.5
+++ PNGTranscoder.java 2001/07/04 16:50:05 1.6
@@ -24,7 +24,7 @@
* This class is an <tt>ImageTranscoder</tt> that produces a PNG image.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Thierry Kormann</a>
- * @version $Id: PNGTranscoder.java,v 1.5 2001/05/31 21:10:26 deweese Exp $
+ * @version $Id: PNGTranscoder.java,v 1.6 2001/07/04 16:50:05 cjolif Exp $
*/
public class PNGTranscoder extends ImageTranscoder {
@@ -96,6 +96,7 @@
try {
PNGImageEncoder pngEncoder = new PNGImageEncoder(ostream, params);
pngEncoder.encode(img);
+ ostream.close();
} catch (IOException ex) {
throw new TranscoderException(ex);
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]