Search in sources :

Example 11 with PixelInterleavedSampleModel

use of java.awt.image.PixelInterleavedSampleModel in project scisoft-core by DawnScience.

the class AWTImageUtils method makeBufferedImage.

/**
 * Get image from a dataset
 * @param data
 * @param bits number of bits (<=16 for non-RGB datasets)
 * @return buffered image
 */
public static BufferedImage makeBufferedImage(final Dataset data, final int bits) {
    final int[] shape = data.getShape();
    if (shape.length > 2) {
        throw new IllegalArgumentException("Rank of data must be less than or equal to two");
    }
    final int height = shape[0];
    // allow 1D datasets to be saved
    final int width = shape.length == 1 ? 1 : shape[1];
    final int size = data.getSize();
    BufferedImage image = null;
    if (data instanceof RGBByteDataset) {
        RGBByteDataset rgbds = (RGBByteDataset) data;
        byte[] rgbdata = rgbds.getData();
        DataBufferByte dataBuffer = new DataBufferByte(rgbdata, rgbdata.length);
        WritableRaster raster = Raster.createInterleavedRaster(dataBuffer, width, height, 3 * width, 3, new int[] { 0, 1, 2 }, null);
        // DirectColorModel colourModel = new DirectColorModel(24, 0xff0000, 0x00ff00, 0x0000ff);
        ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
        ComponentColorModel colorModel = new ComponentColorModel(cs, new int[] { 8, 8, 8 }, false, false, Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
        image = new BufferedImage(colorModel, raster, false, null);
    } else if (data instanceof RGBDataset) {
        RGBDataset rgbds = (RGBDataset) data;
        image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        short maxv = rgbds.max().shortValue();
        final IndexIterator iter = rgbds.getIterator(true);
        final int[] pos = iter.getPos();
        final short[] rgbdata = rgbds.getData();
        if (maxv < 256) {
            // 888
            while (iter.hasNext()) {
                final int n = iter.index;
                final int rgb = ((rgbdata[n] & 0xff) << 16) | ((rgbdata[n + 1] & 0xff) << 8) | (rgbdata[n + 2] & 0xff);
                image.setRGB(pos[1], pos[0], rgb);
            }
        } else {
            int shift = 0;
            while (maxv >= 256) {
                shift++;
                maxv >>= 2;
            }
            while (iter.hasNext()) {
                final int n = iter.index;
                final int rgb = (((rgbdata[n] >> shift) & 0xff) << 16) | (((rgbdata[n + 1] >> shift) & 0xff) << 8) | ((rgbdata[n + 2] >> shift) & 0xff);
                image.setRGB(pos[1], pos[0], rgb);
            }
        }
    } else {
        DataBuffer buffer = null;
        SampleModel sampleModel = null;
        // reconcile data with output format
        // populate data buffer using sample model
        IntegerDataset tmp = DatasetUtils.cast(IntegerDataset.class, data);
        if (bits <= 8) {
            buffer = new DataBufferByte(size);
            sampleModel = new PixelInterleavedSampleModel(DataBuffer.TYPE_BYTE, width, height, 1, width, new int[] { 0 });
            image = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
            sampleModel.setPixels(0, 0, width, height, tmp.getData(), buffer);
        } else if (bits <= 16) {
            buffer = new DataBufferUShort(size);
            sampleModel = new PixelInterleavedSampleModel(DataBuffer.TYPE_USHORT, width, height, 1, width, new int[] { 0 });
            image = new BufferedImage(width, height, BufferedImage.TYPE_USHORT_GRAY);
            sampleModel.setPixels(0, 0, width, height, tmp.getData(), buffer);
        } else {
            throw new IllegalArgumentException("Number of bits must be less than or equal to 16");
        }
        WritableRaster wRas = Raster.createWritableRaster(sampleModel, buffer, null);
        image.setData(wRas);
    }
    return image;
}
Also used : PixelInterleavedSampleModel(java.awt.image.PixelInterleavedSampleModel) ColorSpace(java.awt.color.ColorSpace) ComponentColorModel(java.awt.image.ComponentColorModel) RGBDataset(org.eclipse.january.dataset.RGBDataset) IndexIterator(org.eclipse.january.dataset.IndexIterator) DataBufferByte(java.awt.image.DataBufferByte) BufferedImage(java.awt.image.BufferedImage) IntegerDataset(org.eclipse.january.dataset.IntegerDataset) RGBByteDataset(org.eclipse.january.dataset.RGBByteDataset) ComponentSampleModel(java.awt.image.ComponentSampleModel) SampleModel(java.awt.image.SampleModel) SinglePixelPackedSampleModel(java.awt.image.SinglePixelPackedSampleModel) PixelInterleavedSampleModel(java.awt.image.PixelInterleavedSampleModel) WritableRaster(java.awt.image.WritableRaster) DataBufferUShort(java.awt.image.DataBufferUShort) DataBuffer(java.awt.image.DataBuffer)

Example 12 with PixelInterleavedSampleModel

use of java.awt.image.PixelInterleavedSampleModel in project structure-project by wudskq.

the class BMPImageReader method readHeader.

/**
 * Process the image header.
 *
 * @exception IllegalStateException if source stream is not set.
 *
 * @exception IOException if image stream is corrupted.
 *
 * @exception IllegalArgumentException if the image stream does not contain
 *             a BMP image, or if a sample model instance to describe the
 *             image can not be created.
 */
protected void readHeader() throws IOException, IllegalArgumentException {
    if (gotHeader)
        return;
    if (iis == null) {
        throw new IllegalStateException("Input source not set!");
    }
    int profileData = 0, profileSize = 0;
    this.metadata = new BMPMetadata();
    iis.mark();
    // read and check the magic marker
    byte[] marker = new byte[2];
    iis.read(marker);
    if (marker[0] != 0x42 || marker[1] != 0x4d)
        throw new IllegalArgumentException(I18N.getString("BMPImageReader1"));
    // Read file size
    bitmapFileSize = iis.readUnsignedInt();
    // skip the two reserved fields
    iis.skipBytes(4);
    // Offset to the bitmap from the beginning
    bitmapOffset = iis.readUnsignedInt();
    // End File Header
    // Start BitmapCoreHeader
    long size = iis.readUnsignedInt();
    if (size == 12) {
        width = iis.readShort();
        height = iis.readShort();
    } else {
        width = iis.readInt();
        height = iis.readInt();
    }
    metadata.width = width;
    metadata.height = height;
    int planes = iis.readUnsignedShort();
    bitsPerPixel = iis.readUnsignedShort();
    // metadata.colorPlane = planes;
    metadata.bitsPerPixel = (short) bitsPerPixel;
    // As BMP always has 3 rgb bands, except for Version 5,
    // which is bgra
    numBands = 3;
    if (size == 12) {
        // Windows 2.x and OS/2 1.x
        metadata.bmpVersion = VERSION_2;
        // Classify the image type
        if (bitsPerPixel == 1) {
            imageType = VERSION_2_1_BIT;
        } else if (bitsPerPixel == 4) {
            imageType = VERSION_2_4_BIT;
        } else if (bitsPerPixel == 8) {
            imageType = VERSION_2_8_BIT;
        } else if (bitsPerPixel == 24) {
            imageType = VERSION_2_24_BIT;
        }
        // Read in the palette
        int numberOfEntries = (int) ((bitmapOffset - 14 - size) / 3);
        int sizeOfPalette = numberOfEntries * 3;
        palette = new byte[sizeOfPalette];
        iis.readFully(palette, 0, sizeOfPalette);
        metadata.palette = palette;
        metadata.paletteSize = numberOfEntries;
    } else {
        compression = iis.readUnsignedInt();
        imageSize = iis.readUnsignedInt();
        long xPelsPerMeter = iis.readInt();
        long yPelsPerMeter = iis.readInt();
        long colorsUsed = iis.readUnsignedInt();
        long colorsImportant = iis.readUnsignedInt();
        metadata.compression = (int) compression;
        metadata.xPixelsPerMeter = (int) xPelsPerMeter;
        metadata.yPixelsPerMeter = (int) yPelsPerMeter;
        metadata.colorsUsed = (int) colorsUsed;
        metadata.colorsImportant = (int) colorsImportant;
        if (size == 40) {
            // Windows 3.x and Windows NT
            switch((int) compression) {
                case BI_JPEG:
                case BI_PNG:
                    metadata.bmpVersion = VERSION_3;
                    imageType = VERSION_3_XP_EMBEDDED;
                    break;
                // No compression
                case BI_RGB:
                // 8-bit RLE compression
                case BI_RLE8:
                case // 4-bit RLE compression
                BI_RLE4:
                    // Read in the palette
                    if (bitmapOffset < (size + 14)) {
                        throw new IIOException(I18N.getString("BMPImageReader7"));
                    }
                    int numberOfEntries = (int) ((bitmapOffset - 14 - size) / 4);
                    int sizeOfPalette = numberOfEntries * 4;
                    palette = new byte[sizeOfPalette];
                    iis.readFully(palette, 0, sizeOfPalette);
                    metadata.palette = palette;
                    metadata.paletteSize = numberOfEntries;
                    if (bitsPerPixel == 1) {
                        imageType = VERSION_3_1_BIT;
                    } else if (bitsPerPixel == 4) {
                        imageType = VERSION_3_4_BIT;
                    } else if (bitsPerPixel == 8) {
                        imageType = VERSION_3_8_BIT;
                    } else if (bitsPerPixel == 24) {
                        imageType = VERSION_3_24_BIT;
                    } else if (bitsPerPixel == 16) {
                        imageType = VERSION_3_NT_16_BIT;
                        redMask = 0x7C00;
                        greenMask = 0x3E0;
                        // 0x1F;
                        blueMask = (1 << 5) - 1;
                        metadata.redMask = redMask;
                        metadata.greenMask = greenMask;
                        metadata.blueMask = blueMask;
                    } else if (bitsPerPixel == 32) {
                        imageType = VERSION_3_NT_32_BIT;
                        redMask = 0x00FF0000;
                        greenMask = 0x0000FF00;
                        blueMask = 0x000000FF;
                        metadata.redMask = redMask;
                        metadata.greenMask = greenMask;
                        metadata.blueMask = blueMask;
                    }
                    metadata.bmpVersion = VERSION_3;
                    break;
                case BI_BITFIELDS:
                    if (bitsPerPixel == 16) {
                        imageType = VERSION_3_NT_16_BIT;
                    } else if (bitsPerPixel == 32) {
                        imageType = VERSION_3_NT_32_BIT;
                    }
                    // BitsField encoding
                    redMask = (int) iis.readUnsignedInt();
                    greenMask = (int) iis.readUnsignedInt();
                    blueMask = (int) iis.readUnsignedInt();
                    metadata.redMask = redMask;
                    metadata.greenMask = greenMask;
                    metadata.blueMask = blueMask;
                    if (colorsUsed != 0) {
                        // there is a palette
                        sizeOfPalette = (int) colorsUsed * 4;
                        palette = new byte[sizeOfPalette];
                        iis.readFully(palette, 0, sizeOfPalette);
                        metadata.palette = palette;
                        metadata.paletteSize = (int) colorsUsed;
                    }
                    metadata.bmpVersion = VERSION_3_NT;
                    break;
                default:
                    throw new IIOException(I18N.getString("BMPImageReader2"));
            }
        } else if (size == 108 || size == 124) {
            // Windows 4.x BMP
            if (size == 108)
                metadata.bmpVersion = VERSION_4;
            else if (size == 124)
                metadata.bmpVersion = VERSION_5;
            // rgb masks, valid only if comp is BI_BITFIELDS
            redMask = (int) iis.readUnsignedInt();
            greenMask = (int) iis.readUnsignedInt();
            blueMask = (int) iis.readUnsignedInt();
            // Only supported for 32bpp BI_RGB argb
            alphaMask = (int) iis.readUnsignedInt();
            long csType = iis.readUnsignedInt();
            int redX = iis.readInt();
            int redY = iis.readInt();
            int redZ = iis.readInt();
            int greenX = iis.readInt();
            int greenY = iis.readInt();
            int greenZ = iis.readInt();
            int blueX = iis.readInt();
            int blueY = iis.readInt();
            int blueZ = iis.readInt();
            long gammaRed = iis.readUnsignedInt();
            long gammaGreen = iis.readUnsignedInt();
            long gammaBlue = iis.readUnsignedInt();
            if (size == 124) {
                metadata.intent = iis.readInt();
                profileData = iis.readInt();
                profileSize = iis.readInt();
                iis.skipBytes(4);
            }
            metadata.colorSpace = (int) csType;
            if (csType == LCS_CALIBRATED_RGB) {
                // All the new fields are valid only for this case
                metadata.redX = redX;
                metadata.redY = redY;
                metadata.redZ = redZ;
                metadata.greenX = greenX;
                metadata.greenY = greenY;
                metadata.greenZ = greenZ;
                metadata.blueX = blueX;
                metadata.blueY = blueY;
                metadata.blueZ = blueZ;
                metadata.gammaRed = (int) gammaRed;
                metadata.gammaGreen = (int) gammaGreen;
                metadata.gammaBlue = (int) gammaBlue;
            }
            // Read in the palette
            int numberOfEntries = (int) ((bitmapOffset - 14 - size) / 4);
            int sizeOfPalette = numberOfEntries * 4;
            palette = new byte[sizeOfPalette];
            iis.readFully(palette, 0, sizeOfPalette);
            metadata.palette = palette;
            metadata.paletteSize = numberOfEntries;
            switch((int) compression) {
                case BI_JPEG:
                case BI_PNG:
                    if (size == 108) {
                        imageType = VERSION_4_XP_EMBEDDED;
                    } else if (size == 124) {
                        imageType = VERSION_5_XP_EMBEDDED;
                    }
                    break;
                default:
                    if (bitsPerPixel == 1) {
                        imageType = VERSION_4_1_BIT;
                    } else if (bitsPerPixel == 4) {
                        imageType = VERSION_4_4_BIT;
                    } else if (bitsPerPixel == 8) {
                        imageType = VERSION_4_8_BIT;
                    } else if (bitsPerPixel == 16) {
                        imageType = VERSION_4_16_BIT;
                        if ((int) compression == BI_RGB) {
                            redMask = 0x7C00;
                            greenMask = 0x3E0;
                            blueMask = 0x1F;
                        }
                    } else if (bitsPerPixel == 24) {
                        imageType = VERSION_4_24_BIT;
                    } else if (bitsPerPixel == 32) {
                        imageType = VERSION_4_32_BIT;
                        if ((int) compression == BI_RGB) {
                            redMask = 0x00FF0000;
                            greenMask = 0x0000FF00;
                            blueMask = 0x000000FF;
                        }
                    }
                    metadata.redMask = redMask;
                    metadata.greenMask = greenMask;
                    metadata.blueMask = blueMask;
                    metadata.alphaMask = alphaMask;
            }
        } else {
            throw new IIOException(I18N.getString("BMPImageReader3"));
        }
    }
    if (height > 0) {
        // bottom up image
        isBottomUp = true;
    } else {
        // top down image
        isBottomUp = false;
        height = Math.abs(height);
    }
    // Reset Image Layout so there's only one tile.
    // Define the color space
    ColorSpace colorSpace = ColorSpace.getInstance(ColorSpace.CS_sRGB);
    if (metadata.colorSpace == PROFILE_LINKED || metadata.colorSpace == PROFILE_EMBEDDED) {
        iis.mark();
        iis.skipBytes(profileData - size);
        byte[] profile = new byte[profileSize];
        iis.readFully(profile, 0, profileSize);
        iis.reset();
        try {
            if (metadata.colorSpace == PROFILE_LINKED && isLinkedProfileAllowed() && !isUncOrDevicePath(profile)) {
                String path = new String(profile, "windows-1252");
                colorSpace = new ICC_ColorSpace(ICC_Profile.getInstance(path));
            } else {
                colorSpace = new ICC_ColorSpace(ICC_Profile.getInstance(profile));
            }
        } catch (Exception e) {
            colorSpace = ColorSpace.getInstance(ColorSpace.CS_sRGB);
        }
    }
    if (bitsPerPixel == 0 || compression == BI_JPEG || compression == BI_PNG) {
        // the colorModel and sampleModel will be initialzed
        // by the  reader of embedded image
        colorModel = null;
        sampleModel = null;
    } else if (bitsPerPixel == 1 || bitsPerPixel == 4 || bitsPerPixel == 8) {
        // When number of bitsPerPixel is <= 8, we use IndexColorModel.
        numBands = 1;
        if (bitsPerPixel == 8) {
            int[] bandOffsets = new int[numBands];
            for (int i = 0; i < numBands; i++) {
                bandOffsets[i] = numBands - 1 - i;
            }
            sampleModel = new PixelInterleavedSampleModel(DataBuffer.TYPE_BYTE, width, height, numBands, numBands * width, bandOffsets);
        } else {
            // 1 and 4 bit pixels can be stored in a packed format.
            sampleModel = new MultiPixelPackedSampleModel(DataBuffer.TYPE_BYTE, width, height, bitsPerPixel);
        }
        // Create IndexColorModel from the palette.
        byte[] r, g, b;
        if (imageType == VERSION_2_1_BIT || imageType == VERSION_2_4_BIT || imageType == VERSION_2_8_BIT) {
            size = palette.length / 3;
            if (size > 256) {
                size = 256;
            }
            int off;
            r = new byte[(int) size];
            g = new byte[(int) size];
            b = new byte[(int) size];
            for (int i = 0; i < (int) size; i++) {
                off = 3 * i;
                b[i] = palette[off];
                g[i] = palette[off + 1];
                r[i] = palette[off + 2];
            }
        } else {
            size = palette.length / 4;
            if (size > 256) {
                size = 256;
            }
            int off;
            r = new byte[(int) size];
            g = new byte[(int) size];
            b = new byte[(int) size];
            for (int i = 0; i < size; i++) {
                off = 4 * i;
                b[i] = palette[off];
                g[i] = palette[off + 1];
                r[i] = palette[off + 2];
            }
        }
        if (ImageUtil.isIndicesForGrayscale(r, g, b))
            colorModel = ImageUtil.createColorModel(null, sampleModel);
        else
            colorModel = new IndexColorModel(bitsPerPixel, (int) size, r, g, b);
    } else if (bitsPerPixel == 16) {
        numBands = 3;
        sampleModel = new SinglePixelPackedSampleModel(DataBuffer.TYPE_USHORT, width, height, new int[] { redMask, greenMask, blueMask });
        colorModel = new DirectColorModel(colorSpace, 16, redMask, greenMask, blueMask, 0, false, DataBuffer.TYPE_USHORT);
    } else if (bitsPerPixel == 32) {
        numBands = alphaMask == 0 ? 3 : 4;
        // The number of bands in the SampleModel is determined by
        // the length of the mask array passed in.
        int[] bitMasks = numBands == 3 ? new int[] { redMask, greenMask, blueMask } : new int[] { redMask, greenMask, blueMask, alphaMask };
        sampleModel = new SinglePixelPackedSampleModel(DataBuffer.TYPE_INT, width, height, bitMasks);
        colorModel = new DirectColorModel(colorSpace, 32, redMask, greenMask, blueMask, alphaMask, false, DataBuffer.TYPE_INT);
    } else {
        numBands = 3;
        // Create SampleModel
        int[] bandOffsets = new int[numBands];
        for (int i = 0; i < numBands; i++) {
            bandOffsets[i] = numBands - 1 - i;
        }
        sampleModel = new PixelInterleavedSampleModel(DataBuffer.TYPE_BYTE, width, height, numBands, numBands * width, bandOffsets);
        colorModel = ImageUtil.createColorModel(colorSpace, sampleModel);
    }
    originalSampleModel = sampleModel;
    originalColorModel = colorModel;
    // Reset to the start of bitmap; then jump to the
    // start of image data
    iis.reset();
    iis.skipBytes(bitmapOffset);
    gotHeader = true;
}
Also used : PixelInterleavedSampleModel(java.awt.image.PixelInterleavedSampleModel) ColorSpace(java.awt.color.ColorSpace) ICC_ColorSpace(java.awt.color.ICC_ColorSpace) SinglePixelPackedSampleModel(java.awt.image.SinglePixelPackedSampleModel) MultiPixelPackedSampleModel(java.awt.image.MultiPixelPackedSampleModel) IIOException(javax.imageio.IIOException) Point(java.awt.Point) IIOException(javax.imageio.IIOException) ICC_ColorSpace(java.awt.color.ICC_ColorSpace) DirectColorModel(java.awt.image.DirectColorModel) IndexColorModel(java.awt.image.IndexColorModel)

Example 13 with PixelInterleavedSampleModel

use of java.awt.image.PixelInterleavedSampleModel in project structure-project by wudskq.

the class GIFImageReader method createIndexed.

// We don't check all parameters as ImageTypeSpecifier.createIndexed do
// since this method is private and we pass consistent data here
private ImageTypeSpecifier createIndexed(byte[] r, byte[] g, byte[] b, int bits) {
    ColorModel colorModel;
    if (imageMetadata.transparentColorFlag) {
        // Some files erroneously have a transparent color index
        // of 255 even though there are fewer than 256 colors.
        int idx = Math.min(imageMetadata.transparentColorIndex, r.length - 1);
        colorModel = new IndexColorModel(bits, r.length, r, g, b, idx);
    } else {
        colorModel = new IndexColorModel(bits, r.length, r, g, b);
    }
    SampleModel sampleModel;
    if (bits == 8) {
        int[] bandOffsets = { 0 };
        sampleModel = new PixelInterleavedSampleModel(DataBuffer.TYPE_BYTE, 1, 1, 1, 1, bandOffsets);
    } else {
        sampleModel = new MultiPixelPackedSampleModel(DataBuffer.TYPE_BYTE, 1, 1, bits);
    }
    return new ImageTypeSpecifier(colorModel, sampleModel);
}
Also used : PixelInterleavedSampleModel(java.awt.image.PixelInterleavedSampleModel) PixelInterleavedSampleModel(java.awt.image.PixelInterleavedSampleModel) SampleModel(java.awt.image.SampleModel) MultiPixelPackedSampleModel(java.awt.image.MultiPixelPackedSampleModel) ColorModel(java.awt.image.ColorModel) IndexColorModel(java.awt.image.IndexColorModel) MultiPixelPackedSampleModel(java.awt.image.MultiPixelPackedSampleModel) Point(java.awt.Point) ImageTypeSpecifier(javax.imageio.ImageTypeSpecifier) IndexColorModel(java.awt.image.IndexColorModel)

Example 14 with PixelInterleavedSampleModel

use of java.awt.image.PixelInterleavedSampleModel in project dcm4che by dcm4che.

the class NativeImageReader method createImageType.

protected static final ImageTypeSpecifier createImageType(ImageParameters params, ColorModel colorModel) throws IOException {
    int nType = params.getDataType();
    int nWidth = params.getWidth();
    int nHeight = params.getHeight();
    int nBands = params.getSamplesPerPixel();
    int nBitDepth = params.getBitsPerSample();
    int nScanlineStride = params.getBytesPerLine() / ((nBitDepth + 7) / 8);
    if (nType < 0 || (nType > ImageParameters.TYPE_BIT)) {
        throw new UnsupportedOperationException("Unsupported data type" + " " + nType);
    }
    int[] bandOffsets = new int[nBands];
    for (int i = 0; i < nBands; i++) {
        bandOffsets[i] = i;
    }
    SampleModel sampleModel = new PixelInterleavedSampleModel(nType, nWidth, nHeight, nBands, nScanlineStride, bandOffsets);
    return new ImageTypeSpecifier(colorModel, sampleModel);
}
Also used : PixelInterleavedSampleModel(java.awt.image.PixelInterleavedSampleModel) SampleModel(java.awt.image.SampleModel) PixelInterleavedSampleModel(java.awt.image.PixelInterleavedSampleModel) ImageTypeSpecifier(javax.imageio.ImageTypeSpecifier)

Example 15 with PixelInterleavedSampleModel

use of java.awt.image.PixelInterleavedSampleModel in project quick-media by liuyueyi.

the class TIFFTranscoderImageIOWriteAdapter method writeImage.

/**
 * @throws TranscoderException
 * @see org.apache.batik.transcoder.image.TIFFTranscoder.WriteAdapter#writeImage(TIFFTranscoder, java.awt.image.BufferedImage, org.apache.batik.transcoder.TranscoderOutput)
 */
public void writeImage(TIFFTranscoder transcoder, BufferedImage img, TranscoderOutput output) throws TranscoderException {
    TranscodingHints hints = transcoder.getTranscodingHints();
    ImageWriter writer = ImageWriterRegistry.getInstance().getWriterFor("image/tiff");
    ImageWriterParams params = new ImageWriterParams();
    float PixSzMM = transcoder.getUserAgent().getPixelUnitToMillimeter();
    int PixSzInch = (int) (25.4 / PixSzMM + 0.5);
    params.setResolution(PixSzInch);
    if (hints.containsKey(TIFFTranscoder.KEY_COMPRESSION_METHOD)) {
        String method = (String) hints.get(TIFFTranscoder.KEY_COMPRESSION_METHOD);
        // Values set here as defined in TIFFImageWriteParam of JAI Image I/O Tools
        if ("packbits".equals(method)) {
            params.setCompressionMethod("PackBits");
        } else if ("deflate".equals(method)) {
            params.setCompressionMethod("Deflate");
        } else if ("lzw".equals(method)) {
            params.setCompressionMethod("LZW");
        } else if ("jpeg".equals(method)) {
            params.setCompressionMethod("JPEG");
        } else {
        // nop
        }
    }
    try {
        OutputStream ostream = output.getOutputStream();
        int w = img.getWidth();
        int h = img.getHeight();
        SinglePixelPackedSampleModel sppsm;
        sppsm = (SinglePixelPackedSampleModel) img.getSampleModel();
        int bands = sppsm.getNumBands();
        int[] off = new int[bands];
        for (int i = 0; i < bands; i++) off[i] = i;
        SampleModel sm = new PixelInterleavedSampleModel(DataBuffer.TYPE_BYTE, w, h, bands, w * bands, off);
        RenderedImage rimg = new FormatRed(GraphicsUtil.wrap(img), sm);
        writer.writeImage(rimg, ostream, params);
        ostream.flush();
    } catch (IOException ex) {
        throw new TranscoderException(ex);
    }
}
Also used : PixelInterleavedSampleModel(java.awt.image.PixelInterleavedSampleModel) OutputStream(java.io.OutputStream) ImageWriter(org.apache.batik.ext.awt.image.spi.ImageWriter) SinglePixelPackedSampleModel(java.awt.image.SinglePixelPackedSampleModel) IOException(java.io.IOException) PixelInterleavedSampleModel(java.awt.image.PixelInterleavedSampleModel) SampleModel(java.awt.image.SampleModel) SinglePixelPackedSampleModel(java.awt.image.SinglePixelPackedSampleModel) TranscodingHints(org.apache.batik.transcoder.TranscodingHints) ImageWriterParams(org.apache.batik.ext.awt.image.spi.ImageWriterParams) FormatRed(org.apache.batik.ext.awt.image.rendered.FormatRed) TranscoderException(org.apache.batik.transcoder.TranscoderException) RenderedImage(java.awt.image.RenderedImage)

Aggregations

PixelInterleavedSampleModel (java.awt.image.PixelInterleavedSampleModel)32 SampleModel (java.awt.image.SampleModel)21 Point (java.awt.Point)16 WritableRaster (java.awt.image.WritableRaster)14 BufferedImage (java.awt.image.BufferedImage)12 DataBufferByte (java.awt.image.DataBufferByte)11 ColorModel (java.awt.image.ColorModel)9 DataBuffer (java.awt.image.DataBuffer)9 IndexColorModel (java.awt.image.IndexColorModel)9 MultiPixelPackedSampleModel (java.awt.image.MultiPixelPackedSampleModel)9 SinglePixelPackedSampleModel (java.awt.image.SinglePixelPackedSampleModel)9 BandedSampleModel (java.awt.image.BandedSampleModel)8 ComponentColorModel (java.awt.image.ComponentColorModel)8 ComponentSampleModel (java.awt.image.ComponentSampleModel)6 ColorSpace (java.awt.color.ColorSpace)5 DataBufferUShort (java.awt.image.DataBufferUShort)5 DirectColorModel (java.awt.image.DirectColorModel)5 DataBufferInt (java.awt.image.DataBufferInt)4 ImageTypeSpecifier (javax.imageio.ImageTypeSpecifier)4 DataBufferDouble (java.awt.image.DataBufferDouble)3