use of javax.imageio.stream.MemoryCacheImageOutputStream in project jdk8u_jdk by JetBrains.
the class WriteBitsTest method main.
public static void main(String[] argv) throws RuntimeException {
ByteArrayOutputStream ostream = new ByteArrayOutputStream();
MemoryCacheImageOutputStream mcios = new MemoryCacheImageOutputStream(ostream);
try {
// verify correct writeBits() functionality
long streampos = 0;
int bitoffset = 0;
mcios.setBitOffset(bitoffset);
verify(mcios, streampos, bitoffset);
bitoffset = 3;
mcios.setBitOffset(bitoffset);
verify(mcios, streampos, bitoffset);
for (int incr = 3; incr <= 15; incr += 12) {
for (int i = 0; i < 64; i += incr) {
mcios.writeBits(10, incr);
bitoffset += incr;
if (bitoffset > 7) {
int stroffset = bitoffset / 8;
bitoffset = bitoffset % 8;
streampos += stroffset;
}
verify(mcios, streampos, bitoffset);
}
}
// verify correct read(byte[], int, int) functionality
byte[] bytearr = new byte[2];
mcios.seek(2);
mcios.setBitOffset(3);
int numread = mcios.read(bytearr, 0, 2);
if (numread != 2) {
throw new RuntimeException("Error in mcios.read([BII)I");
}
verify(mcios, 4, 0);
// verify correct read() functionality
mcios.setBitOffset(3);
mcios.read();
verify(mcios, 5, 0);
} catch (IOException e) {
throw new RuntimeException("Unexpected IOException: " + e);
}
}
use of javax.imageio.stream.MemoryCacheImageOutputStream in project jdk8u_jdk by JetBrains.
the class FlushBefore method main.
public static void main(String[] args) throws IOException {
OutputStream ostream = new ByteArrayOutputStream();
FileCacheImageOutputStream fcios = new FileCacheImageOutputStream(ostream, null);
test(fcios);
MemoryCacheImageOutputStream mcios = new MemoryCacheImageOutputStream(ostream);
test(mcios);
}
use of javax.imageio.stream.MemoryCacheImageOutputStream in project jdk8u_jdk by JetBrains.
the class MemoryCacheImageOutputStreamTest method main.
public static void main(String[] args) throws IOException {
try {
MemoryCacheImageOutputStream stream = new MemoryCacheImageOutputStream(new ByteArrayOutputStream());
// or write anything, for that matter
stream.write(0);
stream.flush();
} catch (Exception e) {
throw new RuntimeException("Error flushing stream: " + e);
}
ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageOutputStream ios = new MemoryCacheImageOutputStream(os);
byte[] b = new byte[30 * 256];
byte byteVal = (byte) 0;
for (int i = 0; i < b.length; i++) {
b[i] = byteVal++;
}
// Write 261,120 bytes
for (int i = 0; i < 34; i++) {
ios.write(b);
}
// Scatter 256 values at positions 1000, 2000, ...
// Using both write(int) and write(byte[])
byte[] buf = new byte[1];
for (int i = 0; i < 256; i += 2) {
ios.seek(1000 * i);
ios.write(i);
ios.seek(1000 * (i + 1));
buf[0] = (byte) (i + 1);
ios.write(buf);
}
// Re-read scattered values
for (int i = 0; i < 256; i++) {
ios.seek(1000 * i);
int val = ios.read();
if (val != i) {
System.out.println("Got bad value (1) at pos = " + (1000 * i));
}
}
// Discard two buffers and re-read scattered values
ios.flushBefore(2 * 8192);
for (int i = 0; i < 256; i++) {
long pos = 1000 * i;
if (pos >= 2 * 8192) {
ios.seek(pos);
int val = ios.read();
if (val != i) {
System.out.println("Got bad value (2) at pos = " + (1000 * i));
}
}
}
ios.close();
byte[] data = os.toByteArray();
for (int i = 0; i < data.length; i++) {
byte val = data[i];
if ((i < 256000) && (i % 1000) == 0) {
if (val != (byte) (i / 1000)) {
System.out.println("Got bad value (3) at pos = " + i);
}
} else {
byte gval = (byte) ((i % (30 * 256)) % 256);
if (val != gval) {
System.out.println("Got bad value (4) at pos = " + i + "(got " + (val & 0xff) + " wanted " + (gval & 0xff) + ")");
}
}
}
}
use of javax.imageio.stream.MemoryCacheImageOutputStream in project imageio-ext by geosolutions-it.
the class JPEGWriterCompareTest method writeJPEG.
/**
* Writes outs the image contained into this {@link ImageWorker} as a JPEG using the provided
* destination , compression and compression rate.
* <p>
* The destination object can be anything providing that we have an {@link ImageOutputStreamSpi}
* that recognizes it.
*
* @param destination
* where to write the internal {@link #image} as a JPEG.
* @param compression
* algorithm.
* @param compressionRate
* percentage of compression.
* @param nativeAcc
* should we use native acceleration.
* @return this {@link ImageWorker}.
* @throws IOException
* In case an error occurs during the search for an {@link ImageOutputStream} or
* during the eoncding process.
*/
public static final void writeJPEG(final RenderedImage image, final Object destination, final String compression, final float compressionRate, final boolean nativeAcc) throws IOException {
ImageWriterSpi spi = nativeAcc ? clibSPI : turboSPI;
ImageWriter writer = spi.createWriterInstance();
// Compression is available on both lib
final ImageWriteParam iwp = writer.getDefaultWriteParam();
final ImageOutputStream outStream = nativeAcc ? new MemoryCacheImageOutputStream((OutputStream) destination) : new ImageOutputStreamAdapter((OutputStream) destination);
iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
iwp.setCompressionType("JPEG");
// We can control quality here.
iwp.setCompressionQuality(compressionRate);
if (nativeAcc) {
// Lossy compression.
iwp.setCompressionType(compression);
}
try {
if (iwp instanceof JPEGImageWriteParam) {
final JPEGImageWriteParam param = (JPEGImageWriteParam) iwp;
param.setOptimizeHuffmanTables(true);
param.setProgressiveMode(JPEGImageWriteParam.MODE_DEFAULT);
}
writer.setOutput(outStream);
writer.write(null, new IIOImage(image, null, null), iwp);
} finally {
if (writer != null) {
try {
writer.dispose();
} catch (Throwable e) {
System.out.println(e.getLocalizedMessage());
}
}
if (outStream != null) {
try {
((ImageOutputStream) outStream).close();
} catch (Throwable e) {
System.out.println(e.getLocalizedMessage());
}
}
}
}
use of javax.imageio.stream.MemoryCacheImageOutputStream in project imageio-ext by geosolutions-it.
the class TIFFJPEGCompressor method setMetadata.
/**
* Sets the value of the <code>metadata</code> field.
*
* <p>The implementation in this class also adds the TIFF fields
* JPEGTables, YCbCrSubSampling, YCbCrPositioning, and
* ReferenceBlackWhite superseding any prior settings of those
* fields.</p>
*
* @param metadata the <code>IIOMetadata</code> object for the
* image being written.
*
* @see #getMetadata()
*/
public void setMetadata(IIOMetadata metadata) {
super.setMetadata(metadata);
if (metadata instanceof TIFFImageMetadata) {
TIFFImageMetadata tim = (TIFFImageMetadata) metadata;
TIFFIFD rootIFD = tim.getRootIFD();
BaselineTIFFTagSet base = BaselineTIFFTagSet.getInstance();
TIFFField f = tim.getTIFFField(BaselineTIFFTagSet.TAG_SAMPLES_PER_PIXEL);
int numBands = f.getAsInt(0);
if (numBands == 1) {
// Remove YCbCr fields not relevant for grayscale.
rootIFD.removeTIFFField(BaselineTIFFTagSet.TAG_Y_CB_CR_SUBSAMPLING);
rootIFD.removeTIFFField(BaselineTIFFTagSet.TAG_Y_CB_CR_POSITIONING);
rootIFD.removeTIFFField(BaselineTIFFTagSet.TAG_REFERENCE_BLACK_WHITE);
} else {
// numBands == 3
// Replace YCbCr fields.
// YCbCrSubSampling
TIFFField YCbCrSubSamplingField = new TIFFField(base.getTag(BaselineTIFFTagSet.TAG_Y_CB_CR_SUBSAMPLING), TIFFTag.TIFF_SHORT, 2, new char[] { CHROMA_SUBSAMPLING, CHROMA_SUBSAMPLING });
rootIFD.addTIFFField(YCbCrSubSamplingField);
// YCbCrPositioning
TIFFField YCbCrPositioningField = new TIFFField(base.getTag(BaselineTIFFTagSet.TAG_Y_CB_CR_POSITIONING), TIFFTag.TIFF_SHORT, 1, new char[] { BaselineTIFFTagSet.Y_CB_CR_POSITIONING_CENTERED });
rootIFD.addTIFFField(YCbCrPositioningField);
// ReferenceBlackWhite
TIFFField referenceBlackWhiteField = new TIFFField(base.getTag(BaselineTIFFTagSet.TAG_REFERENCE_BLACK_WHITE), TIFFTag.TIFF_RATIONAL, 6, new long[][] { // no headroon/footroom
{ 0, 1 }, { 255, 1 }, { 128, 1 }, { 255, 1 }, { 128, 1 }, { 255, 1 } });
rootIFD.addTIFFField(referenceBlackWhiteField);
}
// JPEGTables field is written if and only if one is
// already present in the metadata. If one is present
// and has either zero length or does not represent a
// valid tables-only stream, then a JPEGTables field
// will be written initialized to the standard tables-
// only stream written by the JPEG writer.
// Retrieve the JPEGTables field.
TIFFField JPEGTablesField = tim.getTIFFField(BaselineTIFFTagSet.TAG_JPEG_TABLES);
// Initialize JPEG writer to one supporting abbreviated streams.
if (JPEGTablesField != null) {
// Intialize the JPEG writer to one that supports stream
// metadata, i.e., abbreviated streams, and may or may not
// support image metadata.
initJPEGWriter(true, false);
}
// streams was available.
if (JPEGTablesField != null && JPEGWriter != null) {
if (DEBUG)
System.out.println("Has JPEGTables ...");
// Set the abbreviated stream flag.
this.writeAbbreviatedStream = true;
// Branch based on field value count.
if (JPEGTablesField.getCount() > 0) {
if (DEBUG)
System.out.println("JPEGTables > 0");
// Derive the stream metadata from the field.
// Get the field values.
byte[] tables = JPEGTablesField.getAsBytes();
// Create an input stream for the tables.
ByteArrayInputStream bais = new ByteArrayInputStream(tables);
MemoryCacheImageInputStream iis = new MemoryCacheImageInputStream(bais);
// Read the tables stream using the JPEG reader.
ImageReader jpegReader = getJPEGTablesReader();
jpegReader.setInput(iis);
// Initialize the stream metadata object.
try {
JPEGStreamMetadata = jpegReader.getStreamMetadata();
} catch (Exception e) {
// Fall back to default tables.
JPEGStreamMetadata = null;
} finally {
jpegReader.reset();
}
if (DEBUG)
System.out.println(JPEGStreamMetadata);
}
if (JPEGStreamMetadata == null) {
if (DEBUG)
System.out.println("JPEGTables == 0");
// Derive the field from default stream metadata.
// Get default stream metadata.
JPEGStreamMetadata = JPEGWriter.getDefaultStreamMetadata(JPEGParam);
// Create an output stream for the tables.
ByteArrayOutputStream tableByteStream = new ByteArrayOutputStream();
MemoryCacheImageOutputStream tableStream = new MemoryCacheImageOutputStream(tableByteStream);
// Write a tables-only stream.
JPEGWriter.setOutput(tableStream);
try {
JPEGWriter.prepareWriteSequence(JPEGStreamMetadata);
tableStream.flush();
JPEGWriter.endWriteSequence();
// Get the tables-only stream content.
byte[] tables = tableByteStream.toByteArray();
if (DEBUG)
System.out.println("tables.length = " + tables.length);
// Add the JPEGTables field.
JPEGTablesField = new TIFFField(base.getTag(BaselineTIFFTagSet.TAG_JPEG_TABLES), TIFFTag.TIFF_UNDEFINED, tables.length, tables);
rootIFD.addTIFFField(JPEGTablesField);
} catch (Exception e) {
// Do not write JPEGTables field.
rootIFD.removeTIFFField(BaselineTIFFTagSet.TAG_JPEG_TABLES);
this.writeAbbreviatedStream = false;
}
}
} else {
// Do not write JPEGTables field.
// Remove any field present.
rootIFD.removeTIFFField(BaselineTIFFTagSet.TAG_JPEG_TABLES);
// Initialize the writer preferring codecLib.
initJPEGWriter(false, false);
}
}
}
Aggregations