blob: bc535e79da11cf2bdaa8806b94e100f6d0f0b423 [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2003, 2008 IBM Corporation and Others
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Junji MAEDA - initial API and implementation
*******************************************************************************/
package org.eclipse.actf.visualization.engines.lowvision.io;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import org.eclipse.actf.visualization.engines.lowvision.image.BinaryImage;
import org.eclipse.actf.visualization.engines.lowvision.image.ImageUtil;
import org.eclipse.actf.visualization.engines.lowvision.image.Int2D;
import org.eclipse.actf.visualization.engines.lowvision.internal.io.BMPWriter;
import org.eclipse.actf.visualization.engines.lowvision.internal.io.JPEGWriter;
import org.eclipse.actf.visualization.engines.lowvision.internal.io.PBMWriter;
public class ImageWriter {
public static void writeBufferedImage(BufferedImage _bi, String _fileName)
throws LowVisionIOException {
short type = IoUtil.getFileType(_fileName);
if (type != IoUtil.TYPE_UNKNOWN)
writeBufferedImage(_bi, _fileName, type);
else
throw new LowVisionIOException("Unknown image format.");
}
public static void writeBufferedImage(BufferedImage _bi, String _fileName,
short _type) throws LowVisionIOException {
if (_type == IoUtil.TYPE_BMP)
BMPWriter.writeBufferedImage(_bi, _fileName);
else if (_type == IoUtil.TYPE_JPEG)
JPEGWriter.writeBufferedImage(_bi, _fileName);
else if (_type == IoUtil.TYPE_GIF) {
File outFile = new File(_fileName);
boolean result = true;
try {
result = ImageIO.write(_bi, "GIF", outFile);
} catch (IOException ioe) {
ioe.printStackTrace();
throw new LowVisionIOException(
"An error occurs while writing a GIF image.");
}
if (!result) {
throw new LowVisionIOException(
"No appropriate writer is found while writing a GIF image.");
}
// throw new LowVisionIOException("This file format cannot be
// written out.");
} else if (_type == IoUtil.TYPE_PNG) {
File outFile = new File(_fileName);
boolean result = true;
try {
result = ImageIO.write(_bi, "PNG", outFile);
} catch (IOException ioe) {
ioe.printStackTrace();
throw new LowVisionIOException(
"An error occurs while writing a PNG image.");
}
if (!result) {
throw new LowVisionIOException(
"No appropriate writer is found while writing a PNG image.");
}
// throw new LowVisionIOException("This file format cannot be
// written out.");
} else
throw new LowVisionIOException("Unknown image format.");
}
public static void writeInt2D(Int2D _i2d, String _fileName)
throws LowVisionIOException {
writeBufferedImage(ImageUtil.int2DToBufferedImage(_i2d), _fileName);
}
public static void writeInt2D(Int2D _i2d, String _fileName, short _type)
throws LowVisionIOException {
writeBufferedImage(ImageUtil.int2DToBufferedImage(_i2d), _fileName,
_type);
}
public static void writeBinaryImage(BinaryImage _bi, String _fileName)
throws LowVisionIOException {
short type = IoUtil.getFileType(_fileName);
if (type != IoUtil.TYPE_UNKNOWN)
writeBinaryImage(_bi, _fileName, type);
else
throw new LowVisionIOException("Unknown image format.");
}
public static void writeBinaryImage(BinaryImage _bi, String _fileName,
short _type) throws LowVisionIOException {
if (_type == IoUtil.TYPE_PBM)
PBMWriter.writeBinaryImage(_bi, _fileName);
else
throw new LowVisionIOException("Unknown image format.");
}
}