Java Code Examples for java.awt.image.WritableRaster#getPixel()

The following examples show how to use java.awt.image.WritableRaster#getPixel() . 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: ImageDecorator.java    From bither-desktop-java with Apache License 2.0 6 votes vote down vote up
/**
 * <p>Applies a single alpha-blended color over all pixels</p>
 *
 * @param image    The source image
 * @param newColor The color to use as the replacement to non-transparent pixels
 * @return The new image with color applied
 */
public static BufferedImage applyColor(BufferedImage image, Color newColor) {

    int width = image.getWidth();
    int height = image.getHeight();

    WritableRaster raster = image.getRaster();

    int newColorRed = newColor.getRed();
    int newColorGreen = newColor.getGreen();
    int newColorBlue = newColor.getBlue();

    for (int x = 0; x < width; x++) {
        for (int y = 0; y < height; y++) {
            int[] pixels = raster.getPixel(x, y, (int[]) null);

            pixels[0] = newColorRed;
            pixels[1] = newColorGreen;
            pixels[2] = newColorBlue;

            raster.setPixel(x, y, pixels);
        }
    }

    return image;
}
 
Example 2
Source File: ImageDecorator.java    From bither-desktop-java with Apache License 2.0 6 votes vote down vote up
/**
 * <p>Applies a single alpha-blended color over all pixels</p>
 *
 * @param image    The source image
 * @param newColor The color to use as the replacement to non-transparent pixels
 * @return The new image with color applied
 */
public static BufferedImage applyColor(BufferedImage image, Color newColor) {

    int width = image.getWidth();
    int height = image.getHeight();

    WritableRaster raster = image.getRaster();

    int newColorRed = newColor.getRed();
    int newColorGreen = newColor.getGreen();
    int newColorBlue = newColor.getBlue();

    for (int x = 0; x < width; x++) {
        for (int y = 0; y < height; y++) {
            int[] pixels = raster.getPixel(x, y, (int[]) null);

            pixels[0] = newColorRed;
            pixels[1] = newColorGreen;
            pixels[2] = newColorBlue;

            raster.setPixel(x, y, pixels);
        }
    }

    return image;
}
 
Example 3
Source File: DeviceSocketClient.java    From AndroidRobot with Apache License 2.0 6 votes vote down vote up
private static ImageData getImageData2(BufferedImage bufferedImage){
    DirectColorModel colorModel = (DirectColorModel) bufferedImage.getColorModel();
    //System.out.println("robot:" +colorModel.getRedMask() + " "+colorModel.getGreenMask() + " "+colorModel.getBlueMask());   
    PaletteData palette = new PaletteData(colorModel.getRedMask(), colorModel.getGreenMask(), colorModel
            .getBlueMask());
    ImageData data = new ImageData(bufferedImage.getWidth(), bufferedImage.getHeight(), colorModel
            .getPixelSize(), palette);
    WritableRaster raster = bufferedImage.getRaster();
    int[] pixelArray = new int[3];
    for (int y = 0; y < data.height; y++) {
        for (int x = 0; x < data.width; x++) {
            raster.getPixel(x, y, pixelArray);
            int pixel = palette.getPixel(new RGB(pixelArray[0], pixelArray[1], pixelArray[2]));
            data.setPixel(x, y, pixel);
        }
    }
    return data;
}
 
Example 4
Source File: ImageProcessor.java    From gdx-texture-packer-gui with Apache License 2.0 6 votes vote down vote up
/** Hunts for the start or end of a sequence of split pixels. Begins searching at (startX, startY) then follows along the x or y
 * axis (depending on value of xAxis) for the first non-transparent pixel if startPoint is true, or the first transparent pixel
 * if startPoint is false. Returns 0 if none found, as 0 is considered an invalid split point being in the outer border which
 * will be stripped. */
static private int getSplitPoint (WritableRaster raster, String name, int startX, int startY, boolean startPoint, boolean xAxis) {
	int[] rgba = new int[4];

	int next = xAxis ? startX : startY;
	int end = xAxis ? raster.getWidth() : raster.getHeight();
	int breakA = startPoint ? 255 : 0;

	int x = startX;
	int y = startY;
	while (next != end) {
		if (xAxis)
			x = next;
		else
			y = next;

		raster.getPixel(x, y, rgba);
		if (rgba[3] == breakA) return next;

		if (!startPoint && (rgba[0] != 0 || rgba[1] != 0 || rgba[2] != 0 || rgba[3] != 255)) splitError(x, y, rgba, name);

		next++;
	}

	return 0;
}
 
Example 5
Source File: UnrestrictedSizeUploadServlet.java    From easybuggy with Apache License 2.0 6 votes vote down vote up
private boolean reverseColor(String fileName) throws IOException {
    boolean isConverted = false;
    try {
        BufferedImage image = ImageIO.read(new File(fileName));
        WritableRaster raster = image.getRaster();
        int[] pixelBuffer = new int[raster.getNumDataElements()];
        for (int y = 0; y < raster.getHeight(); y++) {
            for (int x = 0; x < raster.getWidth(); x++) {
                raster.getPixel(x, y, pixelBuffer);
                pixelBuffer[0] = ~pixelBuffer[0];
                pixelBuffer[1] = ~pixelBuffer[1];
                pixelBuffer[2] = ~pixelBuffer[2];
                raster.setPixel(x, y, pixelBuffer);
            }
        }
        // Output the image
        ImageIO.write(image, "png", new File(fileName));
        isConverted = true;
    } catch (Exception e) {
        // Log and ignore the exception
        log.warn("Exception occurs: ", e);
    }
    return isConverted;
}
 
Example 6
Source File: MedianGrayFilterTest.java    From audiveris with GNU Affero General Public License v3.0 6 votes vote down vote up
private void dump (BufferedImage img,
                   String title)
{
    final WritableRaster raster = img.getRaster();
    final int width = img.getWidth();
    final int height = img.getHeight();
    final int[] pixel = new int[1];

    System.out.println(title);

    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            raster.getPixel(x, y, pixel);
            System.out.printf("%4d", pixel[0]);
        }

        System.out.println();
    }
}
 
Example 7
Source File: PDIndexed.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
@Override
public BufferedImage toRGBImage(WritableRaster raster) throws IOException
{
    // use lookup table
    int width = raster.getWidth();
    int height = raster.getHeight();

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

    int[] src = new int[1];
    for (int y = 0; y < height; y++)
    {
        for (int x = 0; x < width; x++)
        {
            raster.getPixel(x, y, src);

            // lookup
            int index = Math.min(src[0], actualMaxIndex);
            rgbRaster.setPixel(x, y, rgbColorTable[index]);
        }
    }

    return rgbImage;
}
 
Example 8
Source File: SampledImageReader.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
private static BufferedImage applyColorKeyMask(BufferedImage image, BufferedImage mask)
        throws IOException
{
    int width = image.getWidth();
    int height = image.getHeight();

    // compose to ARGB
    BufferedImage masked = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

    WritableRaster src = image.getRaster();
    WritableRaster dest = masked.getRaster();
    WritableRaster alpha = mask.getRaster();

    float[] rgb = new float[3];
    float[] rgba = new float[4];
    float[] alphaPixel = null;
    for (int y = 0; y < height; y++)
    {
        for (int x = 0; x < width; x++)
        {
            src.getPixel(x, y, rgb);

            rgba[0] = rgb[0];
            rgba[1] = rgb[1];
            rgba[2] = rgb[2];
            alphaPixel = alpha.getPixel(x, y, alphaPixel);
            rgba[3] = 255 - alphaPixel[0];

            dest.setPixel(x, y, rgba);
        }
    }

    return masked;
}
 
Example 9
Source File: SampledImageReader.java    From sambox with Apache License 2.0 5 votes vote down vote up
private static BufferedImage applyColorKeyMask(BufferedImage image, BufferedImage mask)
{
    int width = image.getWidth();
    int height = image.getHeight();

    // compose to ARGB
    BufferedImage masked = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

    WritableRaster src = image.getRaster();
    WritableRaster dest = masked.getRaster();
    WritableRaster alpha = mask.getRaster();

    float[] rgb = new float[3];
    float[] rgba = new float[4];
    float[] alphaPixel = null;
    for (int y = 0; y < height; y++)
    {
        for (int x = 0; x < width; x++)
        {
            src.getPixel(x, y, rgb);

            rgba[0] = rgb[0];
            rgba[1] = rgb[1];
            rgba[2] = rgb[2];
            alphaPixel = alpha.getPixel(x, y, alphaPixel);
            rgba[3] = 255 - alphaPixel[0];

            dest.setPixel(x, y, rgba);
        }
    }

    return masked;
}
 
Example 10
Source File: PDCIEBasedColorSpace.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
@Override
public BufferedImage toRGBImage(WritableRaster raster) throws IOException
{
    // This method calls toRGB to convert images one pixel at a time. For matrix-based
    // CIE color spaces this is fast enough. However, it should not be used with any
    // color space which uses an ICC Profile as it will be far too slow.

    int width = raster.getWidth();
    int height = raster.getHeight();

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

    // always three components: ABC
    float[] abc = new float[3];
    for (int y = 0; y < height; y++)
    {
        for (int x = 0; x < width; x++)
        {
            raster.getPixel(x, y, abc);

            // 0..255 -> 0..1
            abc[0] /= 255;
            abc[1] /= 255;
            abc[2] /= 255;

            float[] rgb = toRGB(abc);

            // 0..1 -> 0..255
            rgb[0] *= 255;
            rgb[1] *= 255;
            rgb[2] *= 255;

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

    return rgbImage;
}
 
Example 11
Source File: OmsHillshade.java    From hortonmachine with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Evaluate the hillshade.
 * 
 * @param pitWR
 *            the raster of elevation.
 * @param hillshadeWR
 *            the WR where store the result.
 * @param gradientWR
 *            the raster of the gradient value of the dem.
 * @param dx
 *            the resolution of the dem. .
 */
private void calchillshade( WritableRaster pitWR, WritableRaster hillshadeWR, WritableRaster gradientWR, double dx ) {

    pAzimuth = Math.toRadians(pAzimuth);
    pElev = Math.toRadians(pElev);

    double[] sunVector = calcSunVector();
    double[] normalSunVector = calcNormalSunVector(sunVector);
    double[] inverseSunVector = calcInverseSunVector(sunVector);
    int rows = pitWR.getHeight();
    int cols = pitWR.getWidth();
    WritableRaster sOmbraWR = calculateFactor(rows, cols, sunVector, inverseSunVector, normalSunVector, pitWR, dx);
    pm.beginTask(msg.message("hillshade.calculating"), rows * cols);
    for( int j = 1; j < rows - 1; j++ ) {
        for( int i = 1; i < cols - 1; i++ ) {

            double[] ng = gradientWR.getPixel(i, j, new double[3]);
            double cosinc = scalarProduct(sunVector, ng);
            if (cosinc < 0) {
                sOmbraWR.setSample(i, j, 0, 0);
            }
            hillshadeWR.setSample(i, j, 0, (int) (212.5 * (cosinc * sOmbraWR.getSample(i, j, 0) + pMinDiffuse)));
            pm.worked(1);
        }
    }
    pm.done();
}
 
Example 12
Source File: PDSeparation.java    From sambox with Apache License 2.0 5 votes vote down vote up
private BufferedImage toRGBImage2(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();
    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);
            int[] rgb = calculatedValues.get(hash = Float.floatToIntBits(samples[0]));
            if (rgb == null)
            {
                samples[0] /= 255;
                float[] altColor = tintTransform.eval(samples);
                float[] fltab = alternateColorSpace.toRGB(altColor);
                rgb = new int[3];
                rgb[0] = (int) (fltab[0] * 255);
                rgb[1] = (int) (fltab[1] * 255);
                rgb[2] = (int) (fltab[2] * 255);
                calculatedValues.put(hash, rgb);
            }
            rgbRaster.setPixel(x, y, rgb);
        }
    }
    return rgbImage;
}
 
Example 13
Source File: LCDStretch.java    From ev3dev-lang-java with MIT License 5 votes vote down vote up
private void bitBlt(BufferedImage src, int sx, int sy, BufferedImage dst, int dx, int dy, int w, int h, int rop) {
    WritableRaster srcR = src.getRaster();
    WritableRaster dstR = dst.getRaster();

    byte msk_dst = (byte) (0xFF & (rop >> 24));
    byte xor_dst = (byte) (0xFF & (rop >> 16));
    byte msk_src = (byte) (0xFF & (rop >> 8));
    byte xor_src = (byte) (0xFF & (rop));
    boolean dstskip = msk_dst == 0 && xor_dst == 0;

    int[] dstpix = new int[4];
    int[] srcpix = new int[4];
    for (int vx = 0; vx < w; vx++) {
        for (int vy = 0; vy < h; vy++) {
            int srcx = sx + vx;
            int srcy = sy + vy;
            int dstx = dx + vx;
            int dsty = dy + vy;
            srcR.getPixel(srcx, srcy, srcpix);

            if (dstskip) {
                // only rgb, no a
                for (int s = 0; s < 3; s++) {
                    dstpix[s] = ((srcpix[s] & msk_src) ^ xor_src);
                }
            } else {
                dstR.getPixel(dstx, dsty, dstpix);
                // only rgb, no a
                for (int s = 0; s < 3; s++) {
                    dstpix[s] = ((dstpix[s] & msk_dst) ^ xor_dst) ^ ((srcpix[s] & msk_src) ^ xor_src);
                }
            }
            dstR.setPixel(dstx, dsty, dstpix);
        }
    }
}
 
Example 14
Source File: PDDeviceN.java    From sambox with Apache License 2.0 4 votes vote down vote up
private BufferedImage toRGBWithTintTransform(WritableRaster raster) throws IOException
{
    // cache color mappings
    Map<String, int[]> map1 = new HashMap<String, int[]>();
    String key = null;

    int width = raster.getWidth();
    int height = raster.getHeight();

    // use the tint transform to convert the sample into
    // the alternate color space (this is usually 1:many)
    BufferedImage rgbImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    WritableRaster rgbRaster = rgbImage.getRaster();
    int[] rgb = new int[3];
    int numSrcComponents = getColorantNames().size();
    float[] src = new float[numSrcComponents];
    for (int y = 0; y < height; y++)
    {
        for (int x = 0; x < width; x++)
        {
            raster.getPixel(x, y, src);
            // use a string representation as key
            key = Float.toString(src[0]);
            for (int s = 1; s < numSrcComponents; s++)
            {
                key += "#" + Float.toString(src[s]);
            }
            int[] pxl = map1.get(key);
            if (pxl != null)
            {
                rgbRaster.setPixel(x, y, pxl);
                continue;
            }

            // scale to 0..1
            for (int s = 0; s < numSrcComponents; s++)
            {
                src[s] = src[s] / 255;
            }

            // convert to alternate color space via tint transform
            float[] result = tintTransform.eval(src);

            // convert from alternate color space to RGB
            float[] rgbFloat = alternateColorSpace.toRGB(result);

            for (int s = 0; s < 3; s++)
            {
                // scale to 0..255
                rgb[s] = (int) (rgbFloat[s] * 255f);
            }

            // must clone because rgb is reused
            map1.put(key, rgb.clone());

            rgbRaster.setPixel(x, y, rgb);
        }
    }
    return rgbImage;
}
 
Example 15
Source File: PDDeviceN.java    From gcs with Mozilla Public License 2.0 4 votes vote down vote up
private BufferedImage toRGBWithTintTransform(WritableRaster raster) throws IOException
{
    // cache color mappings
    Map<String, int[]> map1 = new HashMap<String, int[]>();
    String key = null;

    int width = raster.getWidth();
    int height = raster.getHeight();

    // use the tint transform to convert the sample into
    // the alternate color space (this is usually 1:many)
    BufferedImage rgbImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    WritableRaster rgbRaster = rgbImage.getRaster();
    int[] rgb = new int[3];
    int numSrcComponents = getColorantNames().size();
    float[] src = new float[numSrcComponents];
    for (int y = 0; y < height; y++)
    {
        for (int x = 0; x < width; x++)
        {
            raster.getPixel(x, y, src);
            // use a string representation as key
            key = Float.toString(src[0]);
            for (int s = 1; s < numSrcComponents; s++)
            {
                key += "#" + Float.toString(src[s]);
            }
            int[] pxl = map1.get(key);
            if (pxl != null)
            {
                rgbRaster.setPixel(x, y, pxl);
                continue;
            }
            // scale to 0..1
            for (int s = 0; s < numSrcComponents; s++)
            {
                src[s] = src[s] / 255;
            }
            // convert to alternate color space via tint transform
            float[] result = tintTransform.eval(src);
            
            // convert from alternate color space to RGB
            float[] rgbFloat = alternateColorSpace.toRGB(result);
            
            for (int s = 0; s < 3; s++)
            {
                // scale to 0..255
                rgb[s] = (int) (rgbFloat[s] * 255f);
            }                
            // must clone because rgb is reused
            map1.put(key, rgb.clone());

            rgbRaster.setPixel(x, y, rgb);
        }
    }
    return rgbImage;
}
 
Example 16
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 17
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 18
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 19
Source File: PDDeviceCMYK.java    From sambox with Apache License 2.0 4 votes vote down vote up
@Override
protected BufferedImage toRGBImageAWT(WritableRaster raster, ColorSpace colorSpace)
{
    if (usePureJavaCMYKConversion)
    {
        BufferedImage dest = new BufferedImage(raster.getWidth(), raster.getHeight(),
                BufferedImage.TYPE_INT_RGB);
        ColorSpace destCS = dest.getColorModel().getColorSpace();
        WritableRaster destRaster = dest.getRaster();
        float[] srcValues = new float[4];
        float[] lastValues = new float[] { -1.0f, -1.0f, -1.0f, -1.0f };
        float[] destValues = new float[3];
        int width = raster.getWidth();
        int startX = raster.getMinX();
        int height = raster.getHeight();
        int startY = raster.getMinY();
        for (int x = startX; x < width + startX; x++)
        {
            for (int y = startY; y < height + startY; y++)
            {
                raster.getPixel(x, y, srcValues);
                // check if the last value can be reused
                if (!Arrays.equals(lastValues, srcValues))
                {
                    for (int k = 0; k < 4; k++)
                    {
                        lastValues[k] = srcValues[k];
                        srcValues[k] = srcValues[k] / 255f;
                    }
                    // use CIEXYZ as intermediate format to optimize the color conversion
                    destValues = destCS.fromCIEXYZ(colorSpace.toCIEXYZ(srcValues));
                    for (int k = 0; k < destValues.length; k++)
                    {
                        destValues[k] = destValues[k] * 255f;
                    }
                }
                destRaster.setPixel(x, y, destValues);
            }
        }
        return dest;
    }
    else
    {
        return super.toRGBImageAWT(raster, colorSpace);
    }
}
 
Example 20
Source File: PDImageXObject.java    From sambox with Apache License 2.0 4 votes vote down vote up
private BufferedImage applyMask(BufferedImage image, BufferedImage mask, boolean isSoft,
        float[] matte)
{
    if (mask == null)
    {
        return image;
    }

    int width = image.getWidth();
    int height = image.getHeight();

    // scale mask to fit image, or image to fit mask, whichever is larger
    if (mask.getWidth() < width || mask.getHeight() < height)
    {
        mask = scaleImage(mask, width, height);
    }
    else if (mask.getWidth() > width || mask.getHeight() > height)
    {
        width = mask.getWidth();
        height = mask.getHeight();
        image = scaleImage(image, width, height);
    }
    else if (image.getRaster().getPixel(0, 0, (int[]) null).length < 3)
    {
        // PDFBOX-4470 bitonal image has only one element => copy into RGB
        image = scaleImage(image, width, height);
    }

    // compose to ARGB
    BufferedImage masked = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    WritableRaster src = image.getRaster();
    WritableRaster dest = masked.getRaster();
    WritableRaster alpha = mask.getRaster();

    float[] rgb = new float[4];
    float[] rgba = new float[4];
    float[] alphaPixel = null;
    for (int y = 0; y < height; y++)
    {
        for (int x = 0; x < width; x++)
        {
            src.getPixel(x, y, rgb);

            rgba[0] = rgb[0];
            rgba[1] = rgb[1];
            rgba[2] = rgb[2];

            alphaPixel = alpha.getPixel(x, y, alphaPixel);
            if (isSoft)
            {
                rgba[3] = alphaPixel[0];
                if (matte != null && Float.compare(alphaPixel[0], 0) != 0)
                {
                    rgba[0] = clampColor(
                            ((rgba[0] / 255 - matte[0]) / (alphaPixel[0] / 255) + matte[0])
                                    * 255);
                    rgba[1] = clampColor(
                            ((rgba[1] / 255 - matte[1]) / (alphaPixel[0] / 255) + matte[1])
                                    * 255);
                    rgba[2] = clampColor(
                            ((rgba[2] / 255 - matte[2]) / (alphaPixel[0] / 255) + matte[2])
                                    * 255);
                }
            }
            else
            {
                rgba[3] = 255 - alphaPixel[0];
            }

            dest.setPixel(x, y, rgba);
        }
    }

    return masked;
}