Java Code Examples for javax.imageio.stream.ImageOutputStream#write()

The following examples show how to use javax.imageio.stream.ImageOutputStream#write() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JFIFMarkerSegment.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
void write(ImageOutputStream ios,
           JPEGImageWriter writer) throws IOException {
    int progInterval = data.length / 20;  // approx. every 5%
    if (progInterval == 0) {
        progInterval = 1;
    }
    for (int offset = 0;
         offset < data.length;) {
        int len = Math.min(progInterval, data.length-offset);
        ios.write(data, offset, len);
        offset += progInterval;
        float percentDone = ((float) offset * 100) / data.length;
        if (percentDone > 100.0F) {
            percentDone = 100.0F;
        }
        writer.thumbnailProgress (percentDone);
    }
}
 
Example 2
Source File: JFIFMarkerSegment.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
void write(ImageOutputStream ios,
           JPEGImageWriter writer) throws IOException {
    int progInterval = data.length / 20;  // approx. every 5%
    if (progInterval == 0) {
        progInterval = 1;
    }
    for (int offset = 0;
         offset < data.length;) {
        int len = Math.min(progInterval, data.length-offset);
        ios.write(data, offset, len);
        offset += progInterval;
        float percentDone = ((float) offset * 100) / data.length;
        if (percentDone > 100.0F) {
            percentDone = 100.0F;
        }
        writer.thumbnailProgress (percentDone);
    }
}
 
Example 3
Source File: JFIFMarkerSegment.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
void write(ImageOutputStream ios,
           JPEGImageWriter writer) throws IOException {
    super.write(ios, writer); // width and height
    // Write the palette (must be 768 bytes)
    byte [] palette = new byte[768];
    IndexColorModel icm = (IndexColorModel) thumbnail.getColorModel();
    byte [] reds = new byte [256];
    byte [] greens = new byte [256];
    byte [] blues = new byte [256];
    icm.getReds(reds);
    icm.getGreens(greens);
    icm.getBlues(blues);
    for (int i = 0; i < 256; i++) {
        palette[i*3] = reds[i];
        palette[i*3+1] = greens[i];
        palette[i*3+2] = blues[i];
    }
    ios.write(palette);
    writePixels(ios, writer);
}
 
Example 4
Source File: JFIFMarkerSegment.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
void write(ImageOutputStream ios,
           JPEGImageWriter writer) throws IOException {
    super.write(ios, writer); // width and height
    // Write the palette (must be 768 bytes)
    byte [] palette = new byte[768];
    IndexColorModel icm = (IndexColorModel) thumbnail.getColorModel();
    byte [] reds = new byte [256];
    byte [] greens = new byte [256];
    byte [] blues = new byte [256];
    icm.getReds(reds);
    icm.getGreens(greens);
    icm.getBlues(blues);
    for (int i = 0; i < 256; i++) {
        palette[i*3] = reds[i];
        palette[i*3+1] = greens[i];
        palette[i*3+2] = blues[i];
    }
    ios.write(palette);
    writePixels(ios, writer);
}
 
Example 5
Source File: JFIFMarkerSegment.java    From jdk1.8-source-analysis with Apache License 2.0 5 votes vote down vote up
/**
 * Writes the data for this segment to the stream in
 * valid JPEG format.  The length written takes the thumbnail
 * width and height into account.  If necessary, the thumbnail
 * is clipped to 255 x 255 and a warning is sent to the writer
 * argument.  Progress updates are sent to the writer argument.
 */
void write(ImageOutputStream ios,
           BufferedImage thumb,
           JPEGImageWriter writer) throws IOException {
    int thumbWidth = 0;
    int thumbHeight = 0;
    int thumbLength = 0;
    int [] thumbData = null;
    if (thumb != null) {
        // Clip if necessary and get the data in thumbData
        thumbWidth = thumb.getWidth();
        thumbHeight = thumb.getHeight();
        if ((thumbWidth > MAX_THUMB_WIDTH)
            || (thumbHeight > MAX_THUMB_HEIGHT)) {
            writer.warningOccurred(JPEGImageWriter.WARNING_THUMB_CLIPPED);
        }
        thumbWidth = Math.min(thumbWidth, MAX_THUMB_WIDTH);
        thumbHeight = Math.min(thumbHeight, MAX_THUMB_HEIGHT);
        thumbData = thumb.getRaster().getPixels(0, 0,
                                                thumbWidth, thumbHeight,
                                                (int []) null);
        thumbLength = thumbData.length;
    }
    length = DATA_SIZE + LENGTH_SIZE + thumbLength;
    writeTag(ios);
    byte [] id = {0x4A, 0x46, 0x49, 0x46, 0x00};
    ios.write(id);
    ios.write(majorVersion);
    ios.write(minorVersion);
    ios.write(resUnits);
    write2bytes(ios, Xdensity);
    write2bytes(ios, Ydensity);
    ios.write(thumbWidth);
    ios.write(thumbHeight);
    if (thumbData != null) {
        writer.thumbnailStarted(0);
        writeThumbnailData(ios, thumbData, writer);
        writer.thumbnailComplete();
    }
}
 
Example 6
Source File: JFIFMarkerSegment.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
void writeThumbnailData(ImageOutputStream ios,
                        int [] thumbData,
                        JPEGImageWriter writer) throws IOException {
    int progInterval = thumbData.length / 20;  // approx. every 5%
    if (progInterval == 0) {
        progInterval = 1;
    }
    for (int i = 0; i < thumbData.length; i++) {
        ios.write(thumbData[i]);
        if ((i > progInterval) && (i % progInterval == 0)) {
            writer.thumbnailProgress
                (((float) i * 100) / ((float) thumbData.length));
        }
    }
}
 
Example 7
Source File: JFIFMarkerSegment.java    From jdk1.8-source-analysis with Apache License 2.0 5 votes vote down vote up
void write(ImageOutputStream ios,
           JPEGImageWriter writer) throws IOException {
    length = LENGTH_SIZE + DATA_SIZE + thumb.getLength();
    writeTag(ios);
    byte [] id = {0x4A, 0x46, 0x58, 0x58, 0x00};
    ios.write(id);
    ios.write(code);
    thumb.write(ios, writer);
}
 
Example 8
Source File: MarkerSegment.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Writes the data for this segment to the stream in
 * valid JPEG format.
 */
void write(ImageOutputStream ios) throws IOException {
    length = 2 + ((data != null) ? data.length : 0);
    writeTag(ios);
    if (data != null) {
        ios.write(data);
    }
}
 
Example 9
Source File: MarkerSegment.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Writes the data for this segment to the stream in
 * valid JPEG format.
 */
void write(ImageOutputStream ios) throws IOException {
    length = 2 + ((data != null) ? data.length : 0);
    writeTag(ios);
    if (data != null) {
        ios.write(data);
    }
}
 
Example 10
Source File: JFIFMarkerSegment.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Writes the data for this segment to the stream in
 * valid JPEG format.  The length written takes the thumbnail
 * width and height into account.  If necessary, the thumbnail
 * is clipped to 255 x 255 and a warning is sent to the writer
 * argument.  Progress updates are sent to the writer argument.
 */
void write(ImageOutputStream ios,
           BufferedImage thumb,
           JPEGImageWriter writer) throws IOException {
    int thumbWidth = 0;
    int thumbHeight = 0;
    int thumbLength = 0;
    int [] thumbData = null;
    if (thumb != null) {
        // Clip if necessary and get the data in thumbData
        thumbWidth = thumb.getWidth();
        thumbHeight = thumb.getHeight();
        if ((thumbWidth > MAX_THUMB_WIDTH)
            || (thumbHeight > MAX_THUMB_HEIGHT)) {
            writer.warningOccurred(JPEGImageWriter.WARNING_THUMB_CLIPPED);
        }
        thumbWidth = Math.min(thumbWidth, MAX_THUMB_WIDTH);
        thumbHeight = Math.min(thumbHeight, MAX_THUMB_HEIGHT);
        thumbData = thumb.getRaster().getPixels(0, 0,
                                                thumbWidth, thumbHeight,
                                                (int []) null);
        thumbLength = thumbData.length;
    }
    length = DATA_SIZE + LENGTH_SIZE + thumbLength;
    writeTag(ios);
    byte [] id = {0x4A, 0x46, 0x49, 0x46, 0x00};
    ios.write(id);
    ios.write(majorVersion);
    ios.write(minorVersion);
    ios.write(resUnits);
    write2bytes(ios, Xdensity);
    write2bytes(ios, Ydensity);
    ios.write(thumbWidth);
    ios.write(thumbHeight);
    if (thumbData != null) {
        writer.thumbnailStarted(0);
        writeThumbnailData(ios, thumbData, writer);
        writer.thumbnailComplete();
    }
}
 
Example 11
Source File: JFIFMarkerSegment.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Write out the given profile to the stream, embedded in
 * the necessary number of APP2 segments, per the ICC spec.
 * This is the only mechanism for writing an ICC profile
 * to a stream.
 */
static void writeICC(ICC_Profile profile, ImageOutputStream ios)
    throws IOException {
    int LENGTH_LENGTH = 2;
    final String ID = "ICC_PROFILE";
    int ID_LENGTH = ID.length()+1; // spec says it's null-terminated
    int COUNTS_LENGTH = 2;
    int MAX_ICC_CHUNK_SIZE =
        65535 - LENGTH_LENGTH - ID_LENGTH - COUNTS_LENGTH;

    byte [] data = profile.getData();
    int numChunks = data.length / MAX_ICC_CHUNK_SIZE;
    if ((data.length % MAX_ICC_CHUNK_SIZE) != 0) {
        numChunks++;
    }
    int chunkNum = 1;
    int offset = 0;
    for (int i = 0; i < numChunks; i++) {
        int dataLength = Math.min(data.length-offset, MAX_ICC_CHUNK_SIZE);
        int segLength = dataLength+COUNTS_LENGTH+ID_LENGTH+LENGTH_LENGTH;
        ios.write(0xff);
        ios.write(JPEG.APP2);
        MarkerSegment.write2bytes(ios, segLength);
        byte [] id = ID.getBytes("US-ASCII");
        ios.write(id);
        ios.write(0); // Null-terminate the string
        ios.write(chunkNum++);
        ios.write(numChunks);
        ios.write(data, offset, dataLength);
        offset += dataLength;
    }
}
 
Example 12
Source File: JFIFMarkerSegment.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Writes the data for this segment to the stream in
 * valid JPEG format.  The length written takes the thumbnail
 * width and height into account.  If necessary, the thumbnail
 * is clipped to 255 x 255 and a warning is sent to the writer
 * argument.  Progress updates are sent to the writer argument.
 */
void write(ImageOutputStream ios,
           BufferedImage thumb,
           JPEGImageWriter writer) throws IOException {
    int thumbWidth = 0;
    int thumbHeight = 0;
    int thumbLength = 0;
    int [] thumbData = null;
    if (thumb != null) {
        // Clip if necessary and get the data in thumbData
        thumbWidth = thumb.getWidth();
        thumbHeight = thumb.getHeight();
        if ((thumbWidth > MAX_THUMB_WIDTH)
            || (thumbHeight > MAX_THUMB_HEIGHT)) {
            writer.warningOccurred(JPEGImageWriter.WARNING_THUMB_CLIPPED);
        }
        thumbWidth = Math.min(thumbWidth, MAX_THUMB_WIDTH);
        thumbHeight = Math.min(thumbHeight, MAX_THUMB_HEIGHT);
        thumbData = thumb.getRaster().getPixels(0, 0,
                                                thumbWidth, thumbHeight,
                                                (int []) null);
        thumbLength = thumbData.length;
    }
    length = DATA_SIZE + LENGTH_SIZE + thumbLength;
    writeTag(ios);
    byte [] id = {0x4A, 0x46, 0x49, 0x46, 0x00};
    ios.write(id);
    ios.write(majorVersion);
    ios.write(minorVersion);
    ios.write(resUnits);
    write2bytes(ios, Xdensity);
    write2bytes(ios, Ydensity);
    ios.write(thumbWidth);
    ios.write(thumbHeight);
    if (thumbData != null) {
        writer.thumbnailStarted(0);
        writeThumbnailData(ios, thumbData, writer);
        writer.thumbnailComplete();
    }
}
 
Example 13
Source File: MarkerSegment.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
static void write2bytes(ImageOutputStream ios,
                        int value) throws IOException {
    ios.write((value >> 8) & 0xff);
    ios.write(value & 0xff);

}
 
Example 14
Source File: COMMarkerSegment.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Writes the data for this segment to the stream in
 * valid JPEG format, directly from the data array.
 */
void write(ImageOutputStream ios) throws IOException {
    length = 2 + data.length;
    writeTag(ios);
    ios.write(data);
}
 
Example 15
Source File: COMMarkerSegment.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
/**
 * Writes the data for this segment to the stream in
 * valid JPEG format, directly from the data array.
 */
void write(ImageOutputStream ios) throws IOException {
    length = 2 + data.length;
    writeTag(ios);
    ios.write(data);
}
 
Example 16
Source File: MarkerSegment.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
static void write2bytes(ImageOutputStream ios,
                        int value) throws IOException {
    ios.write((value >> 8) & 0xff);
    ios.write(value & 0xff);

}
 
Example 17
Source File: TIFFIFD.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
private static void writeTIFFFieldToStream(TIFFField field,
                                           ImageOutputStream stream)
    throws IOException {
    int count = field.getCount();
    Object data = field.getData();

    switch (field.getType()) {
    case TIFFTag.TIFF_ASCII:
        for (int i = 0; i < count; i++) {
            String s = ((String[])data)[i];
            int length = s.length();
            for (int j = 0; j < length; j++) {
                stream.writeByte(s.charAt(j) & 0xff);
            }
            stream.writeByte(0);
        }
        break;
    case TIFFTag.TIFF_UNDEFINED:
    case TIFFTag.TIFF_BYTE:
    case TIFFTag.TIFF_SBYTE:
        stream.write((byte[])data);
        break;
    case TIFFTag.TIFF_SHORT:
        stream.writeChars((char[])data, 0, ((char[])data).length);
        break;
    case TIFFTag.TIFF_SSHORT:
        stream.writeShorts((short[])data, 0, ((short[])data).length);
        break;
    case TIFFTag.TIFF_SLONG:
        stream.writeInts((int[])data, 0, ((int[])data).length);
        break;
    case TIFFTag.TIFF_LONG:
        for (int i = 0; i < count; i++) {
            stream.writeInt((int)(((long[])data)[i]));
        }
        break;
    case TIFFTag.TIFF_IFD_POINTER:
        stream.writeInt(0); // will need to be backpatched
        break;
    case TIFFTag.TIFF_FLOAT:
        stream.writeFloats((float[])data, 0, ((float[])data).length);
        break;
    case TIFFTag.TIFF_DOUBLE:
        stream.writeDoubles((double[])data, 0, ((double[])data).length);
        break;
    case TIFFTag.TIFF_SRATIONAL:
        for (int i = 0; i < count; i++) {
            stream.writeInt(((int[][])data)[i][0]);
            stream.writeInt(((int[][])data)[i][1]);
        }
        break;
    case TIFFTag.TIFF_RATIONAL:
        for (int i = 0; i < count; i++) {
            long num = ((long[][])data)[i][0];
            long den = ((long[][])data)[i][1];
            stream.writeInt((int)num);
            stream.writeInt((int)den);
        }
        break;
    default:
        // error
    }
}
 
Example 18
Source File: MarkerSegment.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Writes the marker, tag, and length.  Note that length
 * should be verified by the caller as a correct JPEG
 * length, i.e it includes itself.
 */
void writeTag(ImageOutputStream ios) throws IOException {
    ios.write(0xff);
    ios.write(tag);
    write2bytes(ios, length);
}
 
Example 19
Source File: MarkerSegment.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
static void write2bytes(ImageOutputStream ios,
                        int value) throws IOException {
    ios.write((value >> 8) & 0xff);
    ios.write(value & 0xff);

}
 
Example 20
Source File: AbstractVideoCodec.java    From jpexs-decompiler with GNU General Public License v3.0 4 votes vote down vote up
protected void writeInt24(ImageOutputStream out, int v) throws IOException {
    byteBuf[0] = (byte) (v >>> 16);
    byteBuf[1] = (byte) (v >>> 8);
    byteBuf[2] = (byte) (v >>> 0);
    out.write(byteBuf, 0, 3);
}