Java Code Examples for java.awt.image.Raster#createBandedRaster()

The following examples show how to use java.awt.image.Raster#createBandedRaster() . 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: PCXReader.java    From icafe with Eclipse Public License 1.0 6 votes vote down vote up
private BufferedImage readTrueColorPcx(InputStream is) throws Exception {
  	byte brgb[] = IOUtils.readFully(is, 4096);
	byte pixels[] = new byte[bytesPerLine*NPlanes*height];

	/**
	 * A BufferedInputStream could have been constructed from the InputStream,
	 * but for maximum decoding speed, one time reading of the image data 
	 * into memory is ideal though this is memory consuming.
	 */
   	LOGGER.info("true color pcx image!");
		
	readScanLines(brgb, brgb.length, pixels);
	is.close();
	
 DataBuffer db = new DataBufferByte(pixels, pixels.length);
 int trans = Transparency.OPAQUE;
 int[] nBits = {8, 8, 8};						
 WritableRaster raster = Raster.createBandedRaster(db, width, height, bytesPerLine*3,
            new int[]{0, 0, 0}, new int[] {0, bytesPerLine, bytesPerLine*2}, null);
 ColorModel cm = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB), nBits, false, false,
            trans, DataBuffer.TYPE_BYTE);
 
 return new BufferedImage(cm, raster, false, null);
}
 
Example 2
Source File: TestImage2.java    From libreveris with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static RenderedImage toRenderedImage (String[] rows)
{
    // Create the DataBuffer to hold the pixel samples
    final int width = rows[0].length();
    final int height = rows.length;
    final int size = width * height;
    byte[] pixels = new byte[size];
    int index = 0;
    for (String row : rows) {
        for (int x = 0; x < width; x++) {
            pixels[index++] = (byte) decodeGray(row.charAt(x));
        }
    }
    DataBuffer dataBuffer = new DataBufferByte(pixels, size);

    // Create Raster
    WritableRaster writableRaster = Raster.createBandedRaster
        (dataBuffer, width, height,
         width,                      // scanlineStride
         new int[] {0},             // bankIndices,
         new int[] {0},             // bandOffsets,
         null);                     // location

    // Create the image
    BufferedImage bufferedImage = new BufferedImage
            (width, height, BufferedImage.TYPE_BYTE_GRAY);
    bufferedImage.setData(writableRaster);

    return bufferedImage;
}
 
Example 3
Source File: PDSeparation.java    From gcs with Mozilla Public License 2.0 4 votes vote down vote up
@Override
public BufferedImage toRGBImage(WritableRaster raster) throws IOException
{
    if (alternateColorSpace instanceof PDLab)
    {
        // PDFBOX-3622 - regular converter fails for Lab colorspaces
        return toRGBImage2(raster);
    }
    
    // use the tint transform to convert the sample into
    // the alternate color space (this is usually 1:many)
    WritableRaster altRaster = Raster.createBandedRaster(DataBuffer.TYPE_BYTE,
            raster.getWidth(), raster.getHeight(),
            alternateColorSpace.getNumberOfComponents(),
            new Point(0, 0));

    int numAltComponents = alternateColorSpace.getNumberOfComponents();
    int width = raster.getWidth();
    int height = raster.getHeight();
    float[] samples = new float[1];

    Map<Integer, int[]> calculatedValues = new HashMap<Integer, int[]>();
    Integer hash;
    for (int y = 0; y < height; y++)
    {
        for (int x = 0; x < width; x++)
        {
            raster.getPixel(x, y, samples);
            hash = Float.floatToIntBits(samples[0]);
            int[] alt = calculatedValues.get(hash);
            if (alt == null)
            {
                alt = new int[numAltComponents];
                tintTransform(samples, alt);
                calculatedValues.put(hash, alt);
            }                
            altRaster.setPixel(x, y, alt);
        }
    }

    // convert the alternate color space to RGB
    return alternateColorSpace.toRGBImage(altRaster);
}
 
Example 4
Source File: PDDeviceN.java    From gcs with Mozilla Public License 2.0 4 votes vote down vote up
private BufferedImage toRGBWithAttributes(WritableRaster raster) throws IOException
{
    int width = raster.getWidth();
    int height = raster.getHeight();

    BufferedImage rgbImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    WritableRaster rgbRaster = rgbImage.getRaster();

    // white background
    Graphics2D g = rgbImage.createGraphics();
    g.setBackground(Color.WHITE);
    g.clearRect(0, 0, width, height);
    g.dispose();

    // look up each colorant
    for (int c = 0; c < numColorants; c++)
    {
        PDColorSpace componentColorSpace;
        if (colorantToComponent[c] >= 0)
        {
            // process color
            componentColorSpace = processColorSpace;
        }
        else if (spotColorSpaces[c] == null)
        {
            // TODO this happens in the Altona Visual test, is there a better workaround?
            // missing spot color, fallback to using tintTransform
            return toRGBWithTintTransform(raster);
        }
        else
        {
            // spot color
            componentColorSpace = spotColorSpaces[c];
        }

        // copy single-component to its own raster in the component color space
        WritableRaster componentRaster = Raster.createBandedRaster(DataBuffer.TYPE_BYTE,
            width, height, componentColorSpace.getNumberOfComponents(), new Point(0, 0));

        int[] samples = new int[numColorants];
        int[] componentSamples = new int[componentColorSpace.getNumberOfComponents()];
        boolean isProcessColorant = colorantToComponent[c] >= 0;
        int componentIndex = colorantToComponent[c];
        for (int y = 0; y < height; y++)
        {
            for (int x = 0; x < width; x++)
            {
                raster.getPixel(x, y, samples);
                if (isProcessColorant)
                {
                    // process color
                    componentSamples[componentIndex] = samples[c];
                }
                else
                {
                    // spot color
                    componentSamples[0] = samples[c];
                }
                componentRaster.setPixel(x, y, componentSamples);
            }
        }

        // convert single-component raster to RGB
        BufferedImage rgbComponentImage = componentColorSpace.toRGBImage(componentRaster);
        WritableRaster rgbComponentRaster = rgbComponentImage.getRaster();

        // combine the RGB component with the RGB composite raster
        int[] rgbChannel = new int[3];
        int[] rgbComposite = new int[3];
        for (int y = 0; y < height; y++)
        {
            for (int x = 0; x < width; x++)
            {
                rgbComponentRaster.getPixel(x, y, rgbChannel);
                rgbRaster.getPixel(x, y, rgbComposite);

                // multiply (blend mode)
                rgbChannel[0] = rgbChannel[0] * rgbComposite[0] >> 8;
                rgbChannel[1] = rgbChannel[1] * rgbComposite[1] >> 8;
                rgbChannel[2] = rgbChannel[2] * rgbComposite[2] >> 8;

                rgbRaster.setPixel(x, y, rgbChannel);
            }
        }
    }

    return rgbImage;
}
 
Example 5
Source File: PDIndexed.java    From gcs with Mozilla Public License 2.0 4 votes vote down vote up
private void initRgbColorTable() throws IOException
{
    int numBaseComponents = baseColorSpace.getNumberOfComponents();

    // convert the color table into a 1-row BufferedImage in the base color space,
    // using a writable raster for high performance
    WritableRaster baseRaster;
    try
    {
        baseRaster = Raster.createBandedRaster(DataBuffer.TYPE_BYTE,
                actualMaxIndex + 1, 1, numBaseComponents, new Point(0, 0));
    }
    catch (IllegalArgumentException ex)
    {
        // PDFBOX-4503: when stream is empty or null
        throw new IOException(ex);
    }

    int[] base = new int[numBaseComponents];
    for (int i = 0, n = actualMaxIndex; i <= n; i++)
    {
        for (int c = 0; c < numBaseComponents; c++)
        {
            base[c] = (int)(colorTable[i][c] * 255f);
        }
        baseRaster.setPixel(i, 0, base);
    }

    // convert the base image to RGB
    BufferedImage rgbImage = baseColorSpace.toRGBImage(baseRaster);
    WritableRaster rgbRaster = rgbImage.getRaster();

    // build an RGB lookup table from the raster
    rgbColorTable = new int[actualMaxIndex + 1][3];
    int[] nil = null;

    for (int i = 0, n = actualMaxIndex; i <= n; i++)
    {
        rgbColorTable[i] = rgbRaster.getPixel(i, 0, nil);
    }
}
 
Example 6
Source File: PDSeparation.java    From sambox with Apache License 2.0 4 votes vote down vote up
@Override
public BufferedImage toRGBImage(WritableRaster raster) throws IOException
{
    if (alternateColorSpace instanceof PDLab)
    {
        // PDFBOX-3622 - regular converter fails for Lab colorspaces
        return toRGBImage2(raster);
    }

    // use the tint transform to convert the sample into
    // the alternate color space (this is usually 1:many)
    WritableRaster altRaster = Raster.createBandedRaster(DataBuffer.TYPE_BYTE,
            raster.getWidth(), raster.getHeight(), alternateColorSpace.getNumberOfComponents(),
            new Point(0, 0));

    int numAltComponents = alternateColorSpace.getNumberOfComponents();
    int width = raster.getWidth();
    int height = raster.getHeight();
    float[] samples = new float[1];

    Map<Integer, int[]> calculatedValues = new HashMap<Integer, int[]>();
    Integer hash;
    for (int y = 0; y < height; y++)
    {
        for (int x = 0; x < width; x++)
        {
            raster.getPixel(x, y, samples);
            hash = Float.floatToIntBits(samples[0]);
            int[] alt = calculatedValues.get(hash);
            if (alt == null)
            {
                alt = new int[numAltComponents];
                tintTransform(samples, alt);
                calculatedValues.put(hash, alt);
            }
            altRaster.setPixel(x, y, alt);
        }
    }

    // convert the alternate color space to RGB
    return alternateColorSpace.toRGBImage(altRaster);
}
 
Example 7
Source File: PDDeviceN.java    From sambox with Apache License 2.0 4 votes vote down vote up
private BufferedImage toRGBWithAttributes(WritableRaster raster) throws IOException
{
    int width = raster.getWidth();
    int height = raster.getHeight();

    BufferedImage rgbImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    WritableRaster rgbRaster = rgbImage.getRaster();

    // white background
    Graphics2D g = rgbImage.createGraphics();
    g.setBackground(Color.WHITE);
    g.clearRect(0, 0, width, height);
    g.dispose();

    // look up each colorant
    for (int c = 0; c < numColorants; c++)
    {
        PDColorSpace componentColorSpace;
        if (colorantToComponent[c] >= 0)
        {
            // process color
            componentColorSpace = processColorSpace;
        }
        else if (spotColorSpaces[c] == null)
        {
            // TODO this happens in the Altona Visual test, is there a better workaround?
            // missing spot color, fallback to using tintTransform
            return toRGBWithTintTransform(raster);
        }
        else
        {
            // spot color
            componentColorSpace = spotColorSpaces[c];
        }

        // copy single-component to its own raster in the component color space
        WritableRaster componentRaster = Raster.createBandedRaster(DataBuffer.TYPE_BYTE, width,
                height, componentColorSpace.getNumberOfComponents(), new Point(0, 0));

        int[] samples = new int[numColorants];
        int[] componentSamples = new int[componentColorSpace.getNumberOfComponents()];
        boolean isProcessColorant = colorantToComponent[c] >= 0;
        int componentIndex = colorantToComponent[c];
        for (int y = 0; y < height; y++)
        {
            for (int x = 0; x < width; x++)
            {
                raster.getPixel(x, y, samples);
                if (isProcessColorant)
                {
                    // process color
                    componentSamples[componentIndex] = samples[c];
                }
                else
                {
                    // spot color
                    componentSamples[0] = samples[c];
                }
                componentRaster.setPixel(x, y, componentSamples);
            }
        }

        // convert single-component raster to RGB
        BufferedImage rgbComponentImage = componentColorSpace.toRGBImage(componentRaster);
        WritableRaster rgbComponentRaster = rgbComponentImage.getRaster();

        // combine the RGB component with the RGB composite raster
        int[] rgbChannel = new int[3];
        int[] rgbComposite = new int[3];
        for (int y = 0; y < height; y++)
        {
            for (int x = 0; x < width; x++)
            {
                rgbComponentRaster.getPixel(x, y, rgbChannel);
                rgbRaster.getPixel(x, y, rgbComposite);

                // multiply (blend mode)
                rgbChannel[0] = rgbChannel[0] * rgbComposite[0] >> 8;
                rgbChannel[1] = rgbChannel[1] * rgbComposite[1] >> 8;
                rgbChannel[2] = rgbChannel[2] * rgbComposite[2] >> 8;

                rgbRaster.setPixel(x, y, rgbChannel);
            }
        }
    }

    return rgbImage;
}
 
Example 8
Source File: PDIndexed.java    From sambox with Apache License 2.0 4 votes vote down vote up
private void initRgbColorTable() throws IOException
{
    int numBaseComponents = baseColorSpace.getNumberOfComponents();

    // convert the color table into a 1-row BufferedImage in the base color space,
    // using a writable raster for high performance
    WritableRaster baseRaster;
    try
    {
        baseRaster = Raster.createBandedRaster(DataBuffer.TYPE_BYTE, actualMaxIndex + 1, 1,
                numBaseComponents, new Point(0, 0));
    }
    catch (IllegalArgumentException ex)
    {
        // PDFBOX-4503: when stream is empty or null
        throw new IOException(ex);
    }

    int[] base = new int[numBaseComponents];
    for (int i = 0, n = actualMaxIndex; i <= n; i++)
    {
        for (int c = 0; c < numBaseComponents; c++)
        {
            base[c] = (int) (colorTable[i][c] * 255f);
        }
        baseRaster.setPixel(i, 0, base);
    }

    // convert the base image to RGB
    BufferedImage rgbImage = baseColorSpace.toRGBImage(baseRaster);
    WritableRaster rgbRaster = rgbImage.getRaster();

    // build an RGB lookup table from the raster
    rgbColorTable = new int[actualMaxIndex + 1][3];
    int[] nil = null;

    for (int i = 0, n = actualMaxIndex; i <= n; i++)
    {
        rgbColorTable[i] = rgbRaster.getPixel(i, 0, nil);
    }
}
 
Example 9
Source File: SampledImageReader.java    From sambox with Apache License 2.0 4 votes vote down vote up
/**
 * Returns the content of the given image as an AWT buffered image with an RGB color space. If a color key mask is
 * provided then an ARGB image is returned instead. This method never returns null.
 * 
 * @param pdImage the image to read
 * @param colorKey an optional color key mask
 * @return content of this image as an RGB buffered image
 * @throws IOException if the image cannot be read
 */
public static BufferedImage getRGBImage(PDImage pdImage, COSArray colorKey) throws IOException
{

    if (pdImage.isEmpty())
    {
        throw new IOException("Image stream is empty");
    }

    // get parameters, they must be valid or have been repaired
    final PDColorSpace colorSpace = pdImage.getColorSpace();
    final int numComponents = colorSpace.getNumberOfComponents();
    final int width = pdImage.getWidth();
    final int height = pdImage.getHeight();
    final int bitsPerComponent = pdImage.getBitsPerComponent();
    final float[] decode = getDecodeArray(pdImage);

    if (width <= 0 || height <= 0)
    {
        throw new IOException("image width and height must be positive");
    }
    try
    {
        if (bitsPerComponent == 1 && colorKey == null && numComponents == 1)
        {
            return from1Bit(pdImage);
        }

        //
        // An AWT raster must use 8/16/32 bits per component. Images with < 8bpc
        // will be unpacked into a byte-backed raster. Images with 16bpc will be reduced
        // in depth to 8bpc as they will be drawn to TYPE_INT_RGB images anyway. All code
        // in PDColorSpace#toRGBImage expects an 8-bit range, i.e. 0-255.
        //
        WritableRaster raster = Raster.createBandedRaster(DataBuffer.TYPE_BYTE, width, height,
                numComponents, new Point(0, 0));
        final float[] defaultDecode = pdImage.getColorSpace().getDefaultDecode(8);
        if (bitsPerComponent == 8 && Arrays.equals(decode, defaultDecode) && colorKey == null)
        {
            // convert image, faster path for non-decoded, non-colormasked 8-bit images
            return from8bit(pdImage, raster);
        }
        return fromAny(pdImage, raster, colorKey);
    }
    catch (NegativeArraySizeException ex)
    {
        throw new IOException(ex);
    }
}
 
Example 10
Source File: MultiBandsIndexColorModel.java    From sis with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a {@code WritableRaster} with the specified width and height that has
 * a data layout ({@code SampleModel}) compatible with this {@code ColorModel}.
 *
 * The difference with standard implementation is that this method creates a banded raster on the assumption that
 * the number of bands is greater than 1. By contrast, the standard implementation provides various optimizations
 * for one-banded raster.
 */
@Override
public WritableRaster createCompatibleWritableRaster(final int width, final int height) {
    return Raster.createBandedRaster(transferType, width, height, numBands, null);
}