use of javax.imageio.stream.MemoryCacheImageOutputStream in project imageio-ext by geosolutions-it.
the class TIFFBaseJPEGCompressor method encode.
public final int encode(byte[] b, int off, int width, int height, int[] bitsPerSample, int scanlineStride) throws IOException {
if (this.JPEGWriter == null) {
throw new IIOException("JPEG writer has not been initialized!");
}
if (!((bitsPerSample.length == 3 && bitsPerSample[0] == 8 && bitsPerSample[1] == 8 && bitsPerSample[2] == 8) || (bitsPerSample.length == 1 && bitsPerSample[0] == 8))) {
throw new IIOException("Can only JPEG compress 8- and 24-bit images!");
}
// Set the stream.
ImageOutputStream ios;
// usingCodecLib && !writeAbbreviatedStream
long initialStreamPosition;
if (usingCodecLib && !writeAbbreviatedStream) {
ios = stream;
initialStreamPosition = stream.getStreamPosition();
} else {
// is using a stream on the native side which cannot be reset.
if (baos == null) {
baos = new IIOByteArrayOutputStream();
} else {
baos.reset();
}
ios = new MemoryCacheImageOutputStream(baos);
initialStreamPosition = 0L;
}
JPEGWriter.setOutput(ios);
// Create a DataBuffer.
DataBufferByte dbb;
if (off == 0 || usingCodecLib) {
dbb = new DataBufferByte(b, b.length);
} else {
//
// Workaround for bug in core Java Image I/O JPEG
// ImageWriter which cannot handle non-zero offsets.
//
int bytesPerSegment = scanlineStride * height;
byte[] btmp = new byte[bytesPerSegment];
System.arraycopy(b, off, btmp, 0, bytesPerSegment);
dbb = new DataBufferByte(btmp, bytesPerSegment);
off = 0;
}
// Set up the ColorSpace.
int[] offsets;
ColorSpace cs;
if (bitsPerSample.length == 3) {
offsets = new int[] { off, off + 1, off + 2 };
cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
} else {
offsets = new int[] { off };
cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
}
// Create the ColorModel.
ColorModel cm = new ComponentColorModel(cs, false, false, Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
// Create the SampleModel.
SampleModel sm = new PixelInterleavedSampleModel(DataBuffer.TYPE_BYTE, width, height, bitsPerSample.length, scanlineStride, offsets);
// Create the WritableRaster.
WritableRaster wras = Raster.createWritableRaster(sm, dbb, new Point(0, 0));
// Create the BufferedImage.
BufferedImage bi = new BufferedImage(cm, wras, false, null);
// Get the pruned JPEG image metadata (may be null).
IIOMetadata imageMetadata = getImageMetadata(writeAbbreviatedStream);
// Compress the image into the output stream.
int compDataLength;
if (usingCodecLib && !writeAbbreviatedStream) {
// Write complete JPEG stream
JPEGWriter.write(null, new IIOImage(bi, null, imageMetadata), JPEGParam);
compDataLength = (int) (stream.getStreamPosition() - initialStreamPosition);
} else {
if (writeAbbreviatedStream) {
// Write abbreviated JPEG stream
// First write the tables-only data.
JPEGWriter.prepareWriteSequence(JPEGStreamMetadata);
ios.flush();
// Rewind to the beginning of the byte array.
baos.reset();
// Write the abbreviated image data.
IIOImage image = new IIOImage(bi, null, imageMetadata);
JPEGWriter.writeToSequence(image, JPEGParam);
JPEGWriter.endWriteSequence();
} else {
// Write complete JPEG stream
JPEGWriter.write(null, new IIOImage(bi, null, imageMetadata), JPEGParam);
}
compDataLength = baos.size();
baos.writeTo(stream);
baos.reset();
}
return compDataLength;
}
use of javax.imageio.stream.MemoryCacheImageOutputStream in project OpenOLAT by OpenOLAT.
the class ImageHelperImpl method writeTo.
/**
* Can change this to choose a better compression level as the default
* @param image
* @param scaledImage
* @return
*/
private static boolean writeTo(BufferedImage image, OutputStream scaledImage, Size scaledSize, String outputFormat) {
try {
if (!StringHelper.containsNonWhitespace(outputFormat)) {
outputFormat = OUTPUT_FORMAT;
}
Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName(outputFormat);
if (writers.hasNext()) {
ImageWriter writer = writers.next();
ImageWriteParam iwp = getOptimizedImageWriteParam(writer, scaledSize);
IIOImage iiOImage = new IIOImage(image, null, null);
ImageOutputStream iOut = new MemoryCacheImageOutputStream(scaledImage);
writer.setOutput(iOut);
writer.write(null, iiOImage, iwp);
writer.dispose();
iOut.flush();
iOut.close();
return true;
} else {
return ImageIO.write(image, outputFormat, scaledImage);
}
} catch (IOException e) {
return false;
}
}
use of javax.imageio.stream.MemoryCacheImageOutputStream in project alliance by codice.
the class Jpeg2000ServiceImpl method encodeToByteArray.
private byte[] encodeToByteArray(BufferedImage bufferedImage, J2KImageWriter writer, J2KImageWriteParam writeParams) throws IOException {
try (ByteArrayOutputStream os = new ByteArrayOutputStream()) {
try (ImageOutputStream ios = new MemoryCacheImageOutputStream(os)) {
writer.setOutput(ios);
writer.write(null, new IIOImage(bufferedImage, null, null), writeParams);
writer.dispose();
}
return os.toByteArray();
}
}
use of javax.imageio.stream.MemoryCacheImageOutputStream in project pdfbox by apache.
the class CCITTFactory method createFromImage.
/**
* Creates a new CCITT group 4 (T6) compressed image XObject from a b/w BufferedImage. This
* compression technique usually results in smaller images than those produced by {@link LosslessFactory#createFromImage(PDDocument, BufferedImage)
* }.
*
* @param document the document to create the image as part of.
* @param image the image.
* @return a new image XObject.
* @throws IOException if there is an error creating the image.
* @throws IllegalArgumentException if the BufferedImage is not a b/w image.
*/
public static PDImageXObject createFromImage(PDDocument document, BufferedImage image) throws IOException {
if (image.getType() != BufferedImage.TYPE_BYTE_BINARY && image.getColorModel().getPixelSize() != 1) {
throw new IllegalArgumentException("Only 1-bit b/w images supported");
}
int height = image.getHeight();
int width = image.getWidth();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try (MemoryCacheImageOutputStream mcios = new MemoryCacheImageOutputStream(bos)) {
for (int y = 0; y < height; ++y) {
for (int x = 0; x < width; ++x) {
// flip bit to avoid having to set /BlackIs1
mcios.writeBits(~(image.getRGB(x, y) & 1), 1);
}
if (mcios.getBitOffset() != 0) {
mcios.writeBits(0, 8 - mcios.getBitOffset());
}
}
mcios.flush();
}
return prepareImageXObject(document, bos.toByteArray(), width, height, PDDeviceGray.INSTANCE);
}
use of javax.imageio.stream.MemoryCacheImageOutputStream in project wcomponents by BorderTech.
the class ThumbnailUtil method createScaledJPEG.
/**
* This method creates an array of bytes representing a JPEG image that is a "scaled" version of the given
* {@link Image}.
*
* @param image The image to be turned into a scaled JPEG.
* @param scaledSize The size to which the given <em>image</em> is to be scaled.
* @return A byte[] representing the JPEG image containing the scaled {@link Image}.
* @throws IOException on any sort of error.
*/
private static byte[] createScaledJPEG(final Image image, final Dimension scaledSize) throws IOException {
// Scale the image.
Image scaledImage = image.getScaledInstance(scaledSize.width, scaledSize.height, Image.SCALE_SMOOTH);
// Create a BufferedImage copy of the scaledImage.
BufferedImage bufferedImage = new BufferedImage(scaledImage.getWidth(null), scaledImage.getHeight(null), BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = bufferedImage.createGraphics();
graphics.drawImage(scaledImage, 0, 0, null);
scaledImage.flush();
graphics.dispose();
// Convert the scaled image to a JPEG byte array.
ByteArrayOutputStream baos = new ByteArrayOutputStream();
MemoryCacheImageOutputStream mciis = new MemoryCacheImageOutputStream(baos);
ImageIO.write(bufferedImage, IMAGE_JPEG_FORMAT, mciis);
mciis.flush();
bufferedImage.flush();
byte[] jpeg = baos.toByteArray();
mciis.close();
return jpeg;
}
Aggregations