Example usage for java.awt.image IndexColorModel getTransferType

List of usage examples for java.awt.image IndexColorModel getTransferType

Introduction

In this page you can find the example usage for java.awt.image IndexColorModel getTransferType.

Prototype

public final int getTransferType() 

Source Link

Document

Returns the transfer type of this ColorModel .

Usage

From source file:org.apache.fop.render.pcl.PCLGenerator.java

/**
 * Paint a bitmap at the current cursor position. The bitmap must be a monochrome
 * (1-bit) bitmap image.//  ww  w  .j  a v a  2s  .  c o  m
 * @param img the bitmap image (must be 1-bit b/w)
 * @param resolution the resolution of the image (must be a PCL resolution)
 * @throws IOException In case of an I/O error
 */
public void paintMonochromeBitmap(RenderedImage img, int resolution) throws IOException {
    if (!isValidPCLResolution(resolution)) {
        throw new IllegalArgumentException("Invalid PCL resolution: " + resolution);
    }
    boolean monochrome = isMonochromeImage(img);
    if (!monochrome) {
        throw new IllegalArgumentException("img must be a monochrome image");
    }

    setRasterGraphicsResolution(resolution);
    writeCommand("*r0f" + img.getHeight() + "t" + img.getWidth() + "s1A");
    Raster raster = img.getData();

    Encoder encoder = new Encoder(img);
    // Transfer graphics data
    int imgw = img.getWidth();
    IndexColorModel cm = (IndexColorModel) img.getColorModel();
    if (cm.getTransferType() == DataBuffer.TYPE_BYTE) {
        DataBufferByte dataBuffer = (DataBufferByte) raster.getDataBuffer();
        MultiPixelPackedSampleModel packedSampleModel = new MultiPixelPackedSampleModel(DataBuffer.TYPE_BYTE,
                img.getWidth(), img.getHeight(), 1);
        if (img.getSampleModel().equals(packedSampleModel) && dataBuffer.getNumBanks() == 1) {
            //Optimized packed encoding
            byte[] buf = dataBuffer.getData();
            int scanlineStride = packedSampleModel.getScanlineStride();
            int idx = 0;
            int c0 = toGray(cm.getRGB(0));
            int c1 = toGray(cm.getRGB(1));
            boolean zeroIsWhite = c0 > c1;
            for (int y = 0, maxy = img.getHeight(); y < maxy; y++) {
                for (int x = 0, maxx = scanlineStride; x < maxx; x++) {
                    if (zeroIsWhite) {
                        encoder.add8Bits(buf[idx]);
                    } else {
                        encoder.add8Bits((byte) ~buf[idx]);
                    }
                    idx++;
                }
                encoder.endLine();
            }
        } else {
            //Optimized non-packed encoding
            for (int y = 0, maxy = img.getHeight(); y < maxy; y++) {
                byte[] line = (byte[]) raster.getDataElements(0, y, imgw, 1, null);
                for (int x = 0, maxx = imgw; x < maxx; x++) {
                    encoder.addBit(line[x] == 0);
                }
                encoder.endLine();
            }
        }
    } else {
        //Safe but slow fallback
        for (int y = 0, maxy = img.getHeight(); y < maxy; y++) {
            for (int x = 0, maxx = imgw; x < maxx; x++) {
                int sample = raster.getSample(x, y, 0);
                encoder.addBit(sample == 0);
            }
            encoder.endLine();
        }
    }

    // End raster graphics
    writeCommand("*rB");
}

From source file:org.geotools.utils.imagemosaic.MosaicIndexBuilder.java

/**
 * This method checks the {@link ColorModel} of the current image with the
 * one of the first image in order to check if they are compatible or not in
 * order to perform a mosaic operation.//from   ww  w  .  ja  va2s  . com
 * 
 * <p>
 * It is worth to point out that we also check if, in case we have two index
 * color model image, we also try to suggest whether or not we should do a
 * color expansion.
 * 
 * @param defaultCM
 * @param defaultPalette
 * @param actualCM
 * @return a boolean asking to skip this feature.
 */
private boolean checkColorModels(ColorModel defaultCM, byte[][] defaultPalette, ColorModel actualCM) {

    // /////////////////////////////////////////////////////////////////////
    //
    //
    // ComponentColorModel
    //
    //
    // /////////////////////////////////////////////////////////////////////
    if (defaultCM instanceof ComponentColorModel && actualCM instanceof ComponentColorModel) {
        final ComponentColorModel defCCM = (ComponentColorModel) defaultCM,
                actualCCM = (ComponentColorModel) actualCM;
        return !(defCCM.getNumColorComponents() == actualCCM.getNumColorComponents()
                && defCCM.hasAlpha() == actualCCM.hasAlpha()
                && defCCM.getColorSpace().equals(actualCCM.getColorSpace())
                && defCCM.getTransparency() == actualCCM.getTransparency()
                && defCCM.getTransferType() == actualCCM.getTransferType());
    }
    // /////////////////////////////////////////////////////////////////////
    //
    //
    // IndexColorModel
    //
    //
    // /////////////////////////////////////////////////////////////////////
    if (defaultCM instanceof IndexColorModel && actualCM instanceof IndexColorModel) {
        final IndexColorModel defICM = (IndexColorModel) defaultCM, actualICM = (IndexColorModel) actualCM;
        if (defICM.getNumColorComponents() != actualICM.getNumColorComponents()
                || defICM.hasAlpha() != actualICM.hasAlpha()
                || !defICM.getColorSpace().equals(actualICM.getColorSpace())
                || defICM.getTransferType() != actualICM.getTransferType())
            return true;
        // ///
        //
        // Suggesting expansion in the simplest case
        //
        // ///
        if (defICM.getMapSize() != actualICM.getMapSize()
                || defICM.getTransparency() != actualICM.getTransparency()
                || defICM.getTransferType() != actualICM.getTransferType()
                || defICM.getTransparentPixel() != actualICM.getTransparentPixel()) {
            mustConvertToRGB = true;
            return false;
        }
        // //
        //
        // Now checking palettes to see if we need to do a color convert
        //
        // //
        // get the palette for this color model
        int numBands = actualICM.getNumColorComponents();
        byte[][] actualPalette = new byte[3][actualICM.getMapSize()];
        actualICM.getReds(actualPalette[0]);
        actualICM.getGreens(actualPalette[0]);
        actualICM.getBlues(actualPalette[0]);
        if (numBands == 4)
            actualICM.getAlphas(defaultPalette[0]);
        // compare them
        for (int i = 0; i < defICM.getMapSize(); i++)
            for (int j = 0; j < numBands; j++)
                if (actualPalette[j][i] != defaultPalette[j][i]) {
                    mustConvertToRGB = true;
                    break;
                }
        return false;

    }
    // //
    //
    // if we get here this means that the two color models where completely
    // different, hence skip this feature.
    //
    // //
    return true;
}