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

The following examples show how to use java.awt.image.WritableRaster#getPixels() . 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: SVGFigure.java    From bonita-studio with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Converts an AWT based buffered image into an SWT <code>Image</code>. This will always return an <code>Image</code> that
 * has 24 bit depth regardless of the type of AWT buffered image that is passed into the method.
 * 
 * @param awtImage the {@link java.awt.image.BufferedImage} to be converted to an <code>Image</code>
 * @return an <code>Image</code> that represents the same image data as the AWT <code>BufferedImage</code> type.
 */
protected static org.eclipse.swt.graphics.Image toSWT(final Device device, final BufferedImage awtImage) {
    // We can force bitdepth to be 24 bit because BufferedImage getRGB
    // allows us to always retrieve 24 bit data regardless of source color depth.
    final PaletteData palette = new PaletteData(0xFF0000, 0xFF00, 0xFF);
    final ImageData swtImageData = new ImageData(awtImage.getWidth(), awtImage.getHeight(), 24, palette);
    // Ensure scansize is aligned on 32 bit.
    final int scansize = (awtImage.getWidth() * 3 + 3) * 4 / 4;
    final WritableRaster alphaRaster = awtImage.getAlphaRaster();
    final byte[] alphaBytes = new byte[awtImage.getWidth()];
    for (int y = 0; y < awtImage.getHeight(); y++) {
        final int[] buff = awtImage.getRGB(0, y, awtImage.getWidth(), 1, null, 0, scansize);
        swtImageData.setPixels(0, y, awtImage.getWidth(), buff, 0);
        if (alphaRaster != null) {
            final int[] alpha = alphaRaster.getPixels(0, y, awtImage.getWidth(), 1, (int[]) null);
            for (int i = 0; i < awtImage.getWidth(); i++) {
                alphaBytes[i] = (byte) alpha[i];
            }
            swtImageData.setAlphas(0, y, awtImage.getWidth(), alphaBytes, 0);
        }
    }
    return new org.eclipse.swt.graphics.Image(device, swtImageData);
}
 
Example 2
Source File: ImageConvert.java    From MyBox with Apache License 2.0 5 votes vote down vote up
public static void ycck2cmyk(WritableRaster rast, boolean invertedColors) {
    int w = rast.getWidth(), h = rast.getHeight();
    double c, m, y, k;
    double Y, Cb, Cr, K;
    int[] pixels = null;
    for (int row = 0; row < h; row++) {
        pixels = rast.getPixels(0, row, w, 1, pixels);
        for (int i = 0; i < pixels.length; i += 4) {
            Y = pixels[i];
            Cb = pixels[i + 1];
            Cr = pixels[i + 2];
            K = pixels[i + 3];

            c = Math.min(255, Math.max(0, 255 - (Y + 1.402 * Cr - 179.456)));
            m = Math.min(255, Math.max(0, 255 - (Y - 0.34414 * Cb - 0.71414 * Cr + 135.45984)));
            y = Math.min(255, Math.max(0, 255 - (Y + 1.7718d * Cb - 226.816)));
            k = K;

            if (invertedColors) {
                pixels[i] = (byte) (255 - c);
                pixels[i + 1] = (byte) (255 - m);
                pixels[i + 2] = (byte) (255 - y);
                pixels[i + 3] = (byte) (255 - k);
            } else {
                pixels[i] = (byte) c;
                pixels[i + 1] = (byte) m;
                pixels[i + 2] = (byte) y;
                pixels[i + 3] = (byte) k;
            }
        }
        rast.setPixels(0, row, w, 1, pixels);
    }
}
 
Example 3
Source File: ImageConvert.java    From MyBox with Apache License 2.0 5 votes vote down vote up
public static void invertPixelValue(WritableRaster raster) {
    int height = raster.getHeight();
    int width = raster.getWidth();
    int stride = width * 4;
    int[] pixelRow = new int[stride];
    for (int h = 0; h < height; h++) {
        raster.getPixels(0, h, width, 1, pixelRow);
        for (int x = 0; x < stride; x++) {
            pixelRow[x] = 255 - pixelRow[x];
        }
        raster.setPixels(0, h, width, 1, pixelRow);
    }
}
 
Example 4
Source File: RVizVisualization.java    From coordination_oru with GNU General Public License v3.0 5 votes vote down vote up
private BufferedImage toBlackAndWhite(BufferedImage image, int threshold) {
    BufferedImage result = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_BYTE_GRAY);
    result.getGraphics().drawImage(image, 0, 0, null);
    WritableRaster raster = result.getRaster();
    int[] pixels = new int[image.getWidth()];
    for (int y = 0; y < image.getHeight(); y++) {
        raster.getPixels(0, y, image.getWidth(), 1, pixels);
        for (int i = 0; i < pixels.length; i++) {
            if (pixels[i] < threshold) pixels[i] = 0;
            else pixels[i] = 255;
        }
        raster.setPixels(0, y, image.getWidth(), 1, pixels);
    }
    return result;
}
 
Example 5
Source File: OffScreenImageSource.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
private void sendPixels() {
    ColorModel cm = image.getColorModel();
    WritableRaster raster = image.getRaster();
    int numDataElements = raster.getNumDataElements();
    int dataType = raster.getDataBuffer().getDataType();
    int[] scanline = new int[width*numDataElements];
    boolean needToCvt = true;

    if (cm instanceof IndexColorModel) {
        byte[] pixels = new byte[width];
        theConsumer.setColorModel(cm);

        if (raster instanceof ByteComponentRaster) {
            needToCvt = false;
            for (int y=0; y < height; y++) {
                raster.getDataElements(0, y, width, 1, pixels);
                theConsumer.setPixels(0, y, width, 1, cm, pixels, 0,
                                      width);
            }
        }
        else if (raster instanceof BytePackedRaster) {
            needToCvt = false;
            // Binary image.  Need to unpack it
            for (int y=0; y < height; y++) {
                raster.getPixels(0, y, width, 1, scanline);
                for (int x=0; x < width; x++) {
                    pixels[x] = (byte) scanline[x];
                }
                theConsumer.setPixels(0, y, width, 1, cm, pixels, 0,
                                      width);
            }
        }
        else if (dataType == DataBuffer.TYPE_SHORT ||
                 dataType == DataBuffer.TYPE_INT)
        {
            // Probably a short or int "GRAY" image
            needToCvt = false;
            for (int y=0; y < height; y++) {
                raster.getPixels(0, y, width, 1, scanline);
                theConsumer.setPixels(0, y, width, 1, cm, scanline, 0,
                                      width);
            }
        }
    }
    else if (cm instanceof DirectColorModel) {
        theConsumer.setColorModel(cm);
        needToCvt = false;
        switch (dataType) {
        case DataBuffer.TYPE_INT:
            for (int y=0; y < height; y++) {
                raster.getDataElements(0, y, width, 1, scanline);
                theConsumer.setPixels(0, y, width, 1, cm, scanline, 0,
                                      width);
            }
            break;
        case DataBuffer.TYPE_BYTE:
            byte[] bscanline = new byte[width];
            for (int y=0; y < height; y++) {
                raster.getDataElements(0, y, width, 1, bscanline);
                for (int x=0; x < width; x++) {
                    scanline[x] = bscanline[x]&0xff;
                }
                theConsumer.setPixels(0, y, width, 1, cm, scanline, 0,
                                      width);
            }
            break;
        case DataBuffer.TYPE_USHORT:
            short[] sscanline = new short[width];
            for (int y=0; y < height; y++) {
                raster.getDataElements(0, y, width, 1, sscanline);
                for (int x=0; x < width; x++) {
                    scanline[x] = sscanline[x]&0xffff;
                }
                theConsumer.setPixels(0, y, width, 1, cm, scanline, 0,
                                      width);
            }
            break;
        default:
            needToCvt = true;
        }
    }

    if (needToCvt) {
        // REMIND: Need to add other types of CMs here
        ColorModel newcm = ColorModel.getRGBdefault();
        theConsumer.setColorModel(newcm);

        for (int y=0; y < height; y++) {
            for (int x=0; x < width; x++) {
                scanline[x] = image.getRGB(x, y);
            }
            theConsumer.setPixels(0, y, width, 1, newcm, scanline, 0,
                                  width);
        }
    }
}
 
Example 6
Source File: ImageUtils.java    From openbd-core with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Compose src onto dst using the alpha of sel to interpolate between the two.
 * I can't think of a way to do this using AlphaComposite.
    * @param src the source raster
    * @param dst the destination raster
    * @param sel the mask raster
 */
public static void composeThroughMask(Raster src, WritableRaster dst, Raster sel) {
	int x = src.getMinX();
	int y = src.getMinY();
	int w = src.getWidth();
	int h = src.getHeight();

	int srcRGB[] = null;
	int selRGB[] = null;
	int dstRGB[] = null;

	for ( int i = 0; i < h; i++ ) {
		srcRGB = src.getPixels(x, y, w, 1, srcRGB);
		selRGB = sel.getPixels(x, y, w, 1, selRGB);
		dstRGB = dst.getPixels(x, y, w, 1, dstRGB);

		int k = x;
		for ( int j = 0; j < w; j++ ) {
			int sr = srcRGB[k];
			int dir = dstRGB[k];
			int sg = srcRGB[k+1];
			int dig = dstRGB[k+1];
			int sb = srcRGB[k+2];
			int dib = dstRGB[k+2];
			int sa = srcRGB[k+3];
			int dia = dstRGB[k+3];

			float a = selRGB[k+3]/255f;
			float ac = 1-a;

			dstRGB[k] = (int)(a*sr + ac*dir); 
			dstRGB[k+1] = (int)(a*sg + ac*dig); 
			dstRGB[k+2] = (int)(a*sb + ac*dib); 
			dstRGB[k+3] = (int)(a*sa + ac*dia);
			k += 4;
		}

		dst.setPixels(x, y, w, 1, dstRGB);
		y++;
	}
}
 
Example 7
Source File: OffScreenImageSource.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
private void sendPixels() {
    ColorModel cm = image.getColorModel();
    WritableRaster raster = image.getRaster();
    int numDataElements = raster.getNumDataElements();
    int dataType = raster.getDataBuffer().getDataType();
    int[] scanline = new int[width*numDataElements];
    boolean needToCvt = true;

    if (cm instanceof IndexColorModel) {
        byte[] pixels = new byte[width];
        theConsumer.setColorModel(cm);

        if (raster instanceof ByteComponentRaster) {
            needToCvt = false;
            for (int y=0; y < height; y++) {
                raster.getDataElements(0, y, width, 1, pixels);
                theConsumer.setPixels(0, y, width, 1, cm, pixels, 0,
                                      width);
            }
        }
        else if (raster instanceof BytePackedRaster) {
            needToCvt = false;
            // Binary image.  Need to unpack it
            for (int y=0; y < height; y++) {
                raster.getPixels(0, y, width, 1, scanline);
                for (int x=0; x < width; x++) {
                    pixels[x] = (byte) scanline[x];
                }
                theConsumer.setPixels(0, y, width, 1, cm, pixels, 0,
                                      width);
            }
        }
        else if (dataType == DataBuffer.TYPE_SHORT ||
                 dataType == DataBuffer.TYPE_INT)
        {
            // Probably a short or int "GRAY" image
            needToCvt = false;
            for (int y=0; y < height; y++) {
                raster.getPixels(0, y, width, 1, scanline);
                theConsumer.setPixels(0, y, width, 1, cm, scanline, 0,
                                      width);
            }
        }
    }
    else if (cm instanceof DirectColorModel) {
        theConsumer.setColorModel(cm);
        needToCvt = false;
        switch (dataType) {
        case DataBuffer.TYPE_INT:
            for (int y=0; y < height; y++) {
                raster.getDataElements(0, y, width, 1, scanline);
                theConsumer.setPixels(0, y, width, 1, cm, scanline, 0,
                                      width);
            }
            break;
        case DataBuffer.TYPE_BYTE:
            byte[] bscanline = new byte[width];
            for (int y=0; y < height; y++) {
                raster.getDataElements(0, y, width, 1, bscanline);
                for (int x=0; x < width; x++) {
                    scanline[x] = bscanline[x]&0xff;
                }
                theConsumer.setPixels(0, y, width, 1, cm, scanline, 0,
                                      width);
            }
            break;
        case DataBuffer.TYPE_USHORT:
            short[] sscanline = new short[width];
            for (int y=0; y < height; y++) {
                raster.getDataElements(0, y, width, 1, sscanline);
                for (int x=0; x < width; x++) {
                    scanline[x] = sscanline[x]&0xffff;
                }
                theConsumer.setPixels(0, y, width, 1, cm, scanline, 0,
                                      width);
            }
            break;
        default:
            needToCvt = true;
        }
    }

    if (needToCvt) {
        // REMIND: Need to add other types of CMs here
        ColorModel newcm = ColorModel.getRGBdefault();
        theConsumer.setColorModel(newcm);

        for (int y=0; y < height; y++) {
            for (int x=0; x < width; x++) {
                scanline[x] = image.getRGB(x, y);
            }
            theConsumer.setPixels(0, y, width, 1, newcm, scanline, 0,
                                  width);
        }
    }
}
 
Example 8
Source File: OffScreenImageSource.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
private void sendPixels() {
    ColorModel cm = image.getColorModel();
    WritableRaster raster = image.getRaster();
    int numDataElements = raster.getNumDataElements();
    int dataType = raster.getDataBuffer().getDataType();
    int[] scanline = new int[width*numDataElements];
    boolean needToCvt = true;

    if (cm instanceof IndexColorModel) {
        byte[] pixels = new byte[width];
        theConsumer.setColorModel(cm);

        if (raster instanceof ByteComponentRaster) {
            needToCvt = false;
            for (int y=0; y < height; y++) {
                raster.getDataElements(0, y, width, 1, pixels);
                theConsumer.setPixels(0, y, width, 1, cm, pixels, 0,
                                      width);
            }
        }
        else if (raster instanceof BytePackedRaster) {
            needToCvt = false;
            // Binary image.  Need to unpack it
            for (int y=0; y < height; y++) {
                raster.getPixels(0, y, width, 1, scanline);
                for (int x=0; x < width; x++) {
                    pixels[x] = (byte) scanline[x];
                }
                theConsumer.setPixels(0, y, width, 1, cm, pixels, 0,
                                      width);
            }
        }
        else if (dataType == DataBuffer.TYPE_SHORT ||
                 dataType == DataBuffer.TYPE_INT)
        {
            // Probably a short or int "GRAY" image
            needToCvt = false;
            for (int y=0; y < height; y++) {
                raster.getPixels(0, y, width, 1, scanline);
                theConsumer.setPixels(0, y, width, 1, cm, scanline, 0,
                                      width);
            }
        }
    }
    else if (cm instanceof DirectColorModel) {
        theConsumer.setColorModel(cm);
        needToCvt = false;
        switch (dataType) {
        case DataBuffer.TYPE_INT:
            for (int y=0; y < height; y++) {
                raster.getDataElements(0, y, width, 1, scanline);
                theConsumer.setPixels(0, y, width, 1, cm, scanline, 0,
                                      width);
            }
            break;
        case DataBuffer.TYPE_BYTE:
            byte[] bscanline = new byte[width];
            for (int y=0; y < height; y++) {
                raster.getDataElements(0, y, width, 1, bscanline);
                for (int x=0; x < width; x++) {
                    scanline[x] = bscanline[x]&0xff;
                }
                theConsumer.setPixels(0, y, width, 1, cm, scanline, 0,
                                      width);
            }
            break;
        case DataBuffer.TYPE_USHORT:
            short[] sscanline = new short[width];
            for (int y=0; y < height; y++) {
                raster.getDataElements(0, y, width, 1, sscanline);
                for (int x=0; x < width; x++) {
                    scanline[x] = sscanline[x]&0xffff;
                }
                theConsumer.setPixels(0, y, width, 1, cm, scanline, 0,
                                      width);
            }
            break;
        default:
            needToCvt = true;
        }
    }

    if (needToCvt) {
        // REMIND: Need to add other types of CMs here
        ColorModel newcm = ColorModel.getRGBdefault();
        theConsumer.setColorModel(newcm);

        for (int y=0; y < height; y++) {
            for (int x=0; x < width; x++) {
                scanline[x] = image.getRGB(x, y);
            }
            theConsumer.setPixels(0, y, width, 1, newcm, scanline, 0,
                                  width);
        }
    }
}
 
Example 9
Source File: OffScreenImageSource.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
private void sendPixels() {
    ColorModel cm = image.getColorModel();
    WritableRaster raster = image.getRaster();
    int numDataElements = raster.getNumDataElements();
    int dataType = raster.getDataBuffer().getDataType();
    int[] scanline = new int[width*numDataElements];
    boolean needToCvt = true;

    if (cm instanceof IndexColorModel) {
        byte[] pixels = new byte[width];
        theConsumer.setColorModel(cm);

        if (raster instanceof ByteComponentRaster) {
            needToCvt = false;
            for (int y=0; y < height; y++) {
                raster.getDataElements(0, y, width, 1, pixels);
                theConsumer.setPixels(0, y, width, 1, cm, pixels, 0,
                                      width);
            }
        }
        else if (raster instanceof BytePackedRaster) {
            needToCvt = false;
            // Binary image.  Need to unpack it
            for (int y=0; y < height; y++) {
                raster.getPixels(0, y, width, 1, scanline);
                for (int x=0; x < width; x++) {
                    pixels[x] = (byte) scanline[x];
                }
                theConsumer.setPixels(0, y, width, 1, cm, pixels, 0,
                                      width);
            }
        }
        else if (dataType == DataBuffer.TYPE_SHORT ||
                 dataType == DataBuffer.TYPE_INT)
        {
            // Probably a short or int "GRAY" image
            needToCvt = false;
            for (int y=0; y < height; y++) {
                raster.getPixels(0, y, width, 1, scanline);
                theConsumer.setPixels(0, y, width, 1, cm, scanline, 0,
                                      width);
            }
        }
    }
    else if (cm instanceof DirectColorModel) {
        theConsumer.setColorModel(cm);
        needToCvt = false;
        switch (dataType) {
        case DataBuffer.TYPE_INT:
            for (int y=0; y < height; y++) {
                raster.getDataElements(0, y, width, 1, scanline);
                theConsumer.setPixels(0, y, width, 1, cm, scanline, 0,
                                      width);
            }
            break;
        case DataBuffer.TYPE_BYTE:
            byte[] bscanline = new byte[width];
            for (int y=0; y < height; y++) {
                raster.getDataElements(0, y, width, 1, bscanline);
                for (int x=0; x < width; x++) {
                    scanline[x] = bscanline[x]&0xff;
                }
                theConsumer.setPixels(0, y, width, 1, cm, scanline, 0,
                                      width);
            }
            break;
        case DataBuffer.TYPE_USHORT:
            short[] sscanline = new short[width];
            for (int y=0; y < height; y++) {
                raster.getDataElements(0, y, width, 1, sscanline);
                for (int x=0; x < width; x++) {
                    scanline[x] = sscanline[x]&0xffff;
                }
                theConsumer.setPixels(0, y, width, 1, cm, scanline, 0,
                                      width);
            }
            break;
        default:
            needToCvt = true;
        }
    }

    if (needToCvt) {
        // REMIND: Need to add other types of CMs here
        ColorModel newcm = ColorModel.getRGBdefault();
        theConsumer.setColorModel(newcm);

        for (int y=0; y < height; y++) {
            for (int x=0; x < width; x++) {
                scanline[x] = image.getRGB(x, y);
            }
            theConsumer.setPixels(0, y, width, 1, newcm, scanline, 0,
                                  width);
        }
    }
}
 
Example 10
Source File: OffScreenImageSource.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
private void sendPixels() {
    ColorModel cm = image.getColorModel();
    WritableRaster raster = image.getRaster();
    int numDataElements = raster.getNumDataElements();
    int dataType = raster.getDataBuffer().getDataType();
    int[] scanline = new int[width*numDataElements];
    boolean needToCvt = true;

    if (cm instanceof IndexColorModel) {
        byte[] pixels = new byte[width];
        theConsumer.setColorModel(cm);

        if (raster instanceof ByteComponentRaster) {
            needToCvt = false;
            for (int y=0; y < height; y++) {
                raster.getDataElements(0, y, width, 1, pixels);
                theConsumer.setPixels(0, y, width, 1, cm, pixels, 0,
                                      width);
            }
        }
        else if (raster instanceof BytePackedRaster) {
            needToCvt = false;
            // Binary image.  Need to unpack it
            for (int y=0; y < height; y++) {
                raster.getPixels(0, y, width, 1, scanline);
                for (int x=0; x < width; x++) {
                    pixels[x] = (byte) scanline[x];
                }
                theConsumer.setPixels(0, y, width, 1, cm, pixels, 0,
                                      width);
            }
        }
        else if (dataType == DataBuffer.TYPE_SHORT ||
                 dataType == DataBuffer.TYPE_INT)
        {
            // Probably a short or int "GRAY" image
            needToCvt = false;
            for (int y=0; y < height; y++) {
                raster.getPixels(0, y, width, 1, scanline);
                theConsumer.setPixels(0, y, width, 1, cm, scanline, 0,
                                      width);
            }
        }
    }
    else if (cm instanceof DirectColorModel) {
        theConsumer.setColorModel(cm);
        needToCvt = false;
        switch (dataType) {
        case DataBuffer.TYPE_INT:
            for (int y=0; y < height; y++) {
                raster.getDataElements(0, y, width, 1, scanline);
                theConsumer.setPixels(0, y, width, 1, cm, scanline, 0,
                                      width);
            }
            break;
        case DataBuffer.TYPE_BYTE:
            byte[] bscanline = new byte[width];
            for (int y=0; y < height; y++) {
                raster.getDataElements(0, y, width, 1, bscanline);
                for (int x=0; x < width; x++) {
                    scanline[x] = bscanline[x]&0xff;
                }
                theConsumer.setPixels(0, y, width, 1, cm, scanline, 0,
                                      width);
            }
            break;
        case DataBuffer.TYPE_USHORT:
            short[] sscanline = new short[width];
            for (int y=0; y < height; y++) {
                raster.getDataElements(0, y, width, 1, sscanline);
                for (int x=0; x < width; x++) {
                    scanline[x] = sscanline[x]&0xffff;
                }
                theConsumer.setPixels(0, y, width, 1, cm, scanline, 0,
                                      width);
            }
            break;
        default:
            needToCvt = true;
        }
    }

    if (needToCvt) {
        // REMIND: Need to add other types of CMs here
        ColorModel newcm = ColorModel.getRGBdefault();
        theConsumer.setColorModel(newcm);

        for (int y=0; y < height; y++) {
            for (int x=0; x < width; x++) {
                scanline[x] = image.getRGB(x, y);
            }
            theConsumer.setPixels(0, y, width, 1, newcm, scanline, 0,
                                  width);
        }
    }
}
 
Example 11
Source File: OffScreenImageSource.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
private void sendPixels() {
    ColorModel cm = image.getColorModel();
    WritableRaster raster = image.getRaster();
    int numDataElements = raster.getNumDataElements();
    int dataType = raster.getDataBuffer().getDataType();
    int[] scanline = new int[width*numDataElements];
    boolean needToCvt = true;

    if (cm instanceof IndexColorModel) {
        byte[] pixels = new byte[width];
        theConsumer.setColorModel(cm);

        if (raster instanceof ByteComponentRaster) {
            needToCvt = false;
            for (int y=0; y < height; y++) {
                raster.getDataElements(0, y, width, 1, pixels);
                theConsumer.setPixels(0, y, width, 1, cm, pixels, 0,
                                      width);
            }
        }
        else if (raster instanceof BytePackedRaster) {
            needToCvt = false;
            // Binary image.  Need to unpack it
            for (int y=0; y < height; y++) {
                raster.getPixels(0, y, width, 1, scanline);
                for (int x=0; x < width; x++) {
                    pixels[x] = (byte) scanline[x];
                }
                theConsumer.setPixels(0, y, width, 1, cm, pixels, 0,
                                      width);
            }
        }
        else if (dataType == DataBuffer.TYPE_SHORT ||
                 dataType == DataBuffer.TYPE_INT)
        {
            // Probably a short or int "GRAY" image
            needToCvt = false;
            for (int y=0; y < height; y++) {
                raster.getPixels(0, y, width, 1, scanline);
                theConsumer.setPixels(0, y, width, 1, cm, scanline, 0,
                                      width);
            }
        }
    }
    else if (cm instanceof DirectColorModel) {
        theConsumer.setColorModel(cm);
        needToCvt = false;
        switch (dataType) {
        case DataBuffer.TYPE_INT:
            for (int y=0; y < height; y++) {
                raster.getDataElements(0, y, width, 1, scanline);
                theConsumer.setPixels(0, y, width, 1, cm, scanline, 0,
                                      width);
            }
            break;
        case DataBuffer.TYPE_BYTE:
            byte[] bscanline = new byte[width];
            for (int y=0; y < height; y++) {
                raster.getDataElements(0, y, width, 1, bscanline);
                for (int x=0; x < width; x++) {
                    scanline[x] = bscanline[x]&0xff;
                }
                theConsumer.setPixels(0, y, width, 1, cm, scanline, 0,
                                      width);
            }
            break;
        case DataBuffer.TYPE_USHORT:
            short[] sscanline = new short[width];
            for (int y=0; y < height; y++) {
                raster.getDataElements(0, y, width, 1, sscanline);
                for (int x=0; x < width; x++) {
                    scanline[x] = sscanline[x]&0xffff;
                }
                theConsumer.setPixels(0, y, width, 1, cm, scanline, 0,
                                      width);
            }
            break;
        default:
            needToCvt = true;
        }
    }

    if (needToCvt) {
        // REMIND: Need to add other types of CMs here
        ColorModel newcm = ColorModel.getRGBdefault();
        theConsumer.setColorModel(newcm);

        for (int y=0; y < height; y++) {
            for (int x=0; x < width; x++) {
                scanline[x] = image.getRGB(x, y);
            }
            theConsumer.setPixels(0, y, width, 1, newcm, scanline, 0,
                                  width);
        }
    }
}
 
Example 12
Source File: OffScreenImageSource.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
private void sendPixels() {
    ColorModel cm = image.getColorModel();
    WritableRaster raster = image.getRaster();
    int numDataElements = raster.getNumDataElements();
    int dataType = raster.getDataBuffer().getDataType();
    int[] scanline = new int[width*numDataElements];
    boolean needToCvt = true;

    if (cm instanceof IndexColorModel) {
        byte[] pixels = new byte[width];
        theConsumer.setColorModel(cm);

        if (raster instanceof ByteComponentRaster) {
            needToCvt = false;
            for (int y=0; y < height; y++) {
                raster.getDataElements(0, y, width, 1, pixels);
                theConsumer.setPixels(0, y, width, 1, cm, pixels, 0,
                                      width);
            }
        }
        else if (raster instanceof BytePackedRaster) {
            needToCvt = false;
            // Binary image.  Need to unpack it
            for (int y=0; y < height; y++) {
                raster.getPixels(0, y, width, 1, scanline);
                for (int x=0; x < width; x++) {
                    pixels[x] = (byte) scanline[x];
                }
                theConsumer.setPixels(0, y, width, 1, cm, pixels, 0,
                                      width);
            }
        }
        else if (dataType == DataBuffer.TYPE_SHORT ||
                 dataType == DataBuffer.TYPE_INT)
        {
            // Probably a short or int "GRAY" image
            needToCvt = false;
            for (int y=0; y < height; y++) {
                raster.getPixels(0, y, width, 1, scanline);
                theConsumer.setPixels(0, y, width, 1, cm, scanline, 0,
                                      width);
            }
        }
    }
    else if (cm instanceof DirectColorModel) {
        theConsumer.setColorModel(cm);
        needToCvt = false;
        switch (dataType) {
        case DataBuffer.TYPE_INT:
            for (int y=0; y < height; y++) {
                raster.getDataElements(0, y, width, 1, scanline);
                theConsumer.setPixels(0, y, width, 1, cm, scanline, 0,
                                      width);
            }
            break;
        case DataBuffer.TYPE_BYTE:
            byte[] bscanline = new byte[width];
            for (int y=0; y < height; y++) {
                raster.getDataElements(0, y, width, 1, bscanline);
                for (int x=0; x < width; x++) {
                    scanline[x] = bscanline[x]&0xff;
                }
                theConsumer.setPixels(0, y, width, 1, cm, scanline, 0,
                                      width);
            }
            break;
        case DataBuffer.TYPE_USHORT:
            short[] sscanline = new short[width];
            for (int y=0; y < height; y++) {
                raster.getDataElements(0, y, width, 1, sscanline);
                for (int x=0; x < width; x++) {
                    scanline[x] = sscanline[x]&0xffff;
                }
                theConsumer.setPixels(0, y, width, 1, cm, scanline, 0,
                                      width);
            }
            break;
        default:
            needToCvt = true;
        }
    }

    if (needToCvt) {
        // REMIND: Need to add other types of CMs here
        ColorModel newcm = ColorModel.getRGBdefault();
        theConsumer.setColorModel(newcm);

        for (int y=0; y < height; y++) {
            for (int x=0; x < width; x++) {
                scanline[x] = image.getRGB(x, y);
            }
            theConsumer.setPixels(0, y, width, 1, newcm, scanline, 0,
                                  width);
        }
    }
}
 
Example 13
Source File: OffScreenImageSource.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
private void sendPixels() {
    ColorModel cm = image.getColorModel();
    WritableRaster raster = image.getRaster();
    int numDataElements = raster.getNumDataElements();
    int dataType = raster.getDataBuffer().getDataType();
    int[] scanline = new int[width*numDataElements];
    boolean needToCvt = true;

    if (cm instanceof IndexColorModel) {
        byte[] pixels = new byte[width];
        theConsumer.setColorModel(cm);

        if (raster instanceof ByteComponentRaster) {
            needToCvt = false;
            for (int y=0; y < height; y++) {
                raster.getDataElements(0, y, width, 1, pixels);
                theConsumer.setPixels(0, y, width, 1, cm, pixels, 0,
                                      width);
            }
        }
        else if (raster instanceof BytePackedRaster) {
            needToCvt = false;
            // Binary image.  Need to unpack it
            for (int y=0; y < height; y++) {
                raster.getPixels(0, y, width, 1, scanline);
                for (int x=0; x < width; x++) {
                    pixels[x] = (byte) scanline[x];
                }
                theConsumer.setPixels(0, y, width, 1, cm, pixels, 0,
                                      width);
            }
        }
        else if (dataType == DataBuffer.TYPE_SHORT ||
                 dataType == DataBuffer.TYPE_INT)
        {
            // Probably a short or int "GRAY" image
            needToCvt = false;
            for (int y=0; y < height; y++) {
                raster.getPixels(0, y, width, 1, scanline);
                theConsumer.setPixels(0, y, width, 1, cm, scanline, 0,
                                      width);
            }
        }
    }
    else if (cm instanceof DirectColorModel) {
        theConsumer.setColorModel(cm);
        needToCvt = false;
        switch (dataType) {
        case DataBuffer.TYPE_INT:
            for (int y=0; y < height; y++) {
                raster.getDataElements(0, y, width, 1, scanline);
                theConsumer.setPixels(0, y, width, 1, cm, scanline, 0,
                                      width);
            }
            break;
        case DataBuffer.TYPE_BYTE:
            byte[] bscanline = new byte[width];
            for (int y=0; y < height; y++) {
                raster.getDataElements(0, y, width, 1, bscanline);
                for (int x=0; x < width; x++) {
                    scanline[x] = bscanline[x]&0xff;
                }
                theConsumer.setPixels(0, y, width, 1, cm, scanline, 0,
                                      width);
            }
            break;
        case DataBuffer.TYPE_USHORT:
            short[] sscanline = new short[width];
            for (int y=0; y < height; y++) {
                raster.getDataElements(0, y, width, 1, sscanline);
                for (int x=0; x < width; x++) {
                    scanline[x] = sscanline[x]&0xffff;
                }
                theConsumer.setPixels(0, y, width, 1, cm, scanline, 0,
                                      width);
            }
            break;
        default:
            needToCvt = true;
        }
    }

    if (needToCvt) {
        // REMIND: Need to add other types of CMs here
        ColorModel newcm = ColorModel.getRGBdefault();
        theConsumer.setColorModel(newcm);

        for (int y=0; y < height; y++) {
            for (int x=0; x < width; x++) {
                scanline[x] = image.getRGB(x, y);
            }
            theConsumer.setPixels(0, y, width, 1, newcm, scanline, 0,
                                  width);
        }
    }
}
 
Example 14
Source File: OffScreenImageSource.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
private void sendPixels() {
    ColorModel cm = image.getColorModel();
    WritableRaster raster = image.getRaster();
    int numDataElements = raster.getNumDataElements();
    int dataType = raster.getDataBuffer().getDataType();
    int[] scanline = new int[width*numDataElements];
    boolean needToCvt = true;

    if (cm instanceof IndexColorModel) {
        byte[] pixels = new byte[width];
        theConsumer.setColorModel(cm);

        if (raster instanceof ByteComponentRaster) {
            needToCvt = false;
            for (int y=0; y < height; y++) {
                raster.getDataElements(0, y, width, 1, pixels);
                theConsumer.setPixels(0, y, width, 1, cm, pixels, 0,
                                      width);
            }
        }
        else if (raster instanceof BytePackedRaster) {
            needToCvt = false;
            // Binary image.  Need to unpack it
            for (int y=0; y < height; y++) {
                raster.getPixels(0, y, width, 1, scanline);
                for (int x=0; x < width; x++) {
                    pixels[x] = (byte) scanline[x];
                }
                theConsumer.setPixels(0, y, width, 1, cm, pixels, 0,
                                      width);
            }
        }
        else if (dataType == DataBuffer.TYPE_SHORT ||
                 dataType == DataBuffer.TYPE_INT)
        {
            // Probably a short or int "GRAY" image
            needToCvt = false;
            for (int y=0; y < height; y++) {
                raster.getPixels(0, y, width, 1, scanline);
                theConsumer.setPixels(0, y, width, 1, cm, scanline, 0,
                                      width);
            }
        }
    }
    else if (cm instanceof DirectColorModel) {
        theConsumer.setColorModel(cm);
        needToCvt = false;
        switch (dataType) {
        case DataBuffer.TYPE_INT:
            for (int y=0; y < height; y++) {
                raster.getDataElements(0, y, width, 1, scanline);
                theConsumer.setPixels(0, y, width, 1, cm, scanline, 0,
                                      width);
            }
            break;
        case DataBuffer.TYPE_BYTE:
            byte[] bscanline = new byte[width];
            for (int y=0; y < height; y++) {
                raster.getDataElements(0, y, width, 1, bscanline);
                for (int x=0; x < width; x++) {
                    scanline[x] = bscanline[x]&0xff;
                }
                theConsumer.setPixels(0, y, width, 1, cm, scanline, 0,
                                      width);
            }
            break;
        case DataBuffer.TYPE_USHORT:
            short[] sscanline = new short[width];
            for (int y=0; y < height; y++) {
                raster.getDataElements(0, y, width, 1, sscanline);
                for (int x=0; x < width; x++) {
                    scanline[x] = sscanline[x]&0xffff;
                }
                theConsumer.setPixels(0, y, width, 1, cm, scanline, 0,
                                      width);
            }
            break;
        default:
            needToCvt = true;
        }
    }

    if (needToCvt) {
        // REMIND: Need to add other types of CMs here
        ColorModel newcm = ColorModel.getRGBdefault();
        theConsumer.setColorModel(newcm);

        for (int y=0; y < height; y++) {
            for (int x=0; x < width; x++) {
                scanline[x] = image.getRGB(x, y);
            }
            theConsumer.setPixels(0, y, width, 1, newcm, scanline, 0,
                                  width);
        }
    }
}
 
Example 15
Source File: OffScreenImageSource.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
private void sendPixels() {
    ColorModel cm = image.getColorModel();
    WritableRaster raster = image.getRaster();
    int numDataElements = raster.getNumDataElements();
    int dataType = raster.getDataBuffer().getDataType();
    int[] scanline = new int[width*numDataElements];
    boolean needToCvt = true;

    if (cm instanceof IndexColorModel) {
        byte[] pixels = new byte[width];
        theConsumer.setColorModel(cm);

        if (raster instanceof ByteComponentRaster) {
            needToCvt = false;
            for (int y=0; y < height; y++) {
                raster.getDataElements(0, y, width, 1, pixels);
                theConsumer.setPixels(0, y, width, 1, cm, pixels, 0,
                                      width);
            }
        }
        else if (raster instanceof BytePackedRaster) {
            needToCvt = false;
            // Binary image.  Need to unpack it
            for (int y=0; y < height; y++) {
                raster.getPixels(0, y, width, 1, scanline);
                for (int x=0; x < width; x++) {
                    pixels[x] = (byte) scanline[x];
                }
                theConsumer.setPixels(0, y, width, 1, cm, pixels, 0,
                                      width);
            }
        }
        else if (dataType == DataBuffer.TYPE_SHORT ||
                 dataType == DataBuffer.TYPE_INT)
        {
            // Probably a short or int "GRAY" image
            needToCvt = false;
            for (int y=0; y < height; y++) {
                raster.getPixels(0, y, width, 1, scanline);
                theConsumer.setPixels(0, y, width, 1, cm, scanline, 0,
                                      width);
            }
        }
    }
    else if (cm instanceof DirectColorModel) {
        theConsumer.setColorModel(cm);
        needToCvt = false;
        switch (dataType) {
        case DataBuffer.TYPE_INT:
            for (int y=0; y < height; y++) {
                raster.getDataElements(0, y, width, 1, scanline);
                theConsumer.setPixels(0, y, width, 1, cm, scanline, 0,
                                      width);
            }
            break;
        case DataBuffer.TYPE_BYTE:
            byte[] bscanline = new byte[width];
            for (int y=0; y < height; y++) {
                raster.getDataElements(0, y, width, 1, bscanline);
                for (int x=0; x < width; x++) {
                    scanline[x] = bscanline[x]&0xff;
                }
                theConsumer.setPixels(0, y, width, 1, cm, scanline, 0,
                                      width);
            }
            break;
        case DataBuffer.TYPE_USHORT:
            short[] sscanline = new short[width];
            for (int y=0; y < height; y++) {
                raster.getDataElements(0, y, width, 1, sscanline);
                for (int x=0; x < width; x++) {
                    scanline[x] = sscanline[x]&0xffff;
                }
                theConsumer.setPixels(0, y, width, 1, cm, scanline, 0,
                                      width);
            }
            break;
        default:
            needToCvt = true;
        }
    }

    if (needToCvt) {
        // REMIND: Need to add other types of CMs here
        ColorModel newcm = ColorModel.getRGBdefault();
        theConsumer.setColorModel(newcm);

        for (int y=0; y < height; y++) {
            for (int x=0; x < width; x++) {
                scanline[x] = image.getRGB(x, y);
            }
            theConsumer.setPixels(0, y, width, 1, newcm, scanline, 0,
                                  width);
        }
    }
}
 
Example 16
Source File: OffScreenImageSource.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
private void sendPixels() {
    ColorModel cm = image.getColorModel();
    WritableRaster raster = image.getRaster();
    int numDataElements = raster.getNumDataElements();
    int dataType = raster.getDataBuffer().getDataType();
    int[] scanline = new int[width*numDataElements];
    boolean needToCvt = true;

    if (cm instanceof IndexColorModel) {
        byte[] pixels = new byte[width];
        theConsumer.setColorModel(cm);

        if (raster instanceof ByteComponentRaster) {
            needToCvt = false;
            for (int y=0; y < height; y++) {
                raster.getDataElements(0, y, width, 1, pixels);
                theConsumer.setPixels(0, y, width, 1, cm, pixels, 0,
                                      width);
            }
        }
        else if (raster instanceof BytePackedRaster) {
            needToCvt = false;
            // Binary image.  Need to unpack it
            for (int y=0; y < height; y++) {
                raster.getPixels(0, y, width, 1, scanline);
                for (int x=0; x < width; x++) {
                    pixels[x] = (byte) scanline[x];
                }
                theConsumer.setPixels(0, y, width, 1, cm, pixels, 0,
                                      width);
            }
        }
        else if (dataType == DataBuffer.TYPE_SHORT ||
                 dataType == DataBuffer.TYPE_INT)
        {
            // Probably a short or int "GRAY" image
            needToCvt = false;
            for (int y=0; y < height; y++) {
                raster.getPixels(0, y, width, 1, scanline);
                theConsumer.setPixels(0, y, width, 1, cm, scanline, 0,
                                      width);
            }
        }
    }
    else if (cm instanceof DirectColorModel) {
        theConsumer.setColorModel(cm);
        needToCvt = false;
        switch (dataType) {
        case DataBuffer.TYPE_INT:
            for (int y=0; y < height; y++) {
                raster.getDataElements(0, y, width, 1, scanline);
                theConsumer.setPixels(0, y, width, 1, cm, scanline, 0,
                                      width);
            }
            break;
        case DataBuffer.TYPE_BYTE:
            byte[] bscanline = new byte[width];
            for (int y=0; y < height; y++) {
                raster.getDataElements(0, y, width, 1, bscanline);
                for (int x=0; x < width; x++) {
                    scanline[x] = bscanline[x]&0xff;
                }
                theConsumer.setPixels(0, y, width, 1, cm, scanline, 0,
                                      width);
            }
            break;
        case DataBuffer.TYPE_USHORT:
            short[] sscanline = new short[width];
            for (int y=0; y < height; y++) {
                raster.getDataElements(0, y, width, 1, sscanline);
                for (int x=0; x < width; x++) {
                    scanline[x] = sscanline[x]&0xffff;
                }
                theConsumer.setPixels(0, y, width, 1, cm, scanline, 0,
                                      width);
            }
            break;
        default:
            needToCvt = true;
        }
    }

    if (needToCvt) {
        // REMIND: Need to add other types of CMs here
        ColorModel newcm = ColorModel.getRGBdefault();
        theConsumer.setColorModel(newcm);

        for (int y=0; y < height; y++) {
            for (int x=0; x < width; x++) {
                scanline[x] = image.getRGB(x, y);
            }
            theConsumer.setPixels(0, y, width, 1, newcm, scanline, 0,
                                  width);
        }
    }
}
 
Example 17
Source File: OffScreenImageSource.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
private void sendPixels() {
    ColorModel cm = image.getColorModel();
    WritableRaster raster = image.getRaster();
    int numDataElements = raster.getNumDataElements();
    int dataType = raster.getDataBuffer().getDataType();
    int[] scanline = new int[width*numDataElements];
    boolean needToCvt = true;

    if (cm instanceof IndexColorModel) {
        byte[] pixels = new byte[width];
        theConsumer.setColorModel(cm);

        if (raster instanceof ByteComponentRaster) {
            needToCvt = false;
            for (int y=0; y < height; y++) {
                raster.getDataElements(0, y, width, 1, pixels);
                theConsumer.setPixels(0, y, width, 1, cm, pixels, 0,
                                      width);
            }
        }
        else if (raster instanceof BytePackedRaster) {
            needToCvt = false;
            // Binary image.  Need to unpack it
            for (int y=0; y < height; y++) {
                raster.getPixels(0, y, width, 1, scanline);
                for (int x=0; x < width; x++) {
                    pixels[x] = (byte) scanline[x];
                }
                theConsumer.setPixels(0, y, width, 1, cm, pixels, 0,
                                      width);
            }
        }
        else if (dataType == DataBuffer.TYPE_SHORT ||
                 dataType == DataBuffer.TYPE_INT)
        {
            // Probably a short or int "GRAY" image
            needToCvt = false;
            for (int y=0; y < height; y++) {
                raster.getPixels(0, y, width, 1, scanline);
                theConsumer.setPixels(0, y, width, 1, cm, scanline, 0,
                                      width);
            }
        }
    }
    else if (cm instanceof DirectColorModel) {
        theConsumer.setColorModel(cm);
        needToCvt = false;
        switch (dataType) {
        case DataBuffer.TYPE_INT:
            for (int y=0; y < height; y++) {
                raster.getDataElements(0, y, width, 1, scanline);
                theConsumer.setPixels(0, y, width, 1, cm, scanline, 0,
                                      width);
            }
            break;
        case DataBuffer.TYPE_BYTE:
            byte[] bscanline = new byte[width];
            for (int y=0; y < height; y++) {
                raster.getDataElements(0, y, width, 1, bscanline);
                for (int x=0; x < width; x++) {
                    scanline[x] = bscanline[x]&0xff;
                }
                theConsumer.setPixels(0, y, width, 1, cm, scanline, 0,
                                      width);
            }
            break;
        case DataBuffer.TYPE_USHORT:
            short[] sscanline = new short[width];
            for (int y=0; y < height; y++) {
                raster.getDataElements(0, y, width, 1, sscanline);
                for (int x=0; x < width; x++) {
                    scanline[x] = sscanline[x]&0xffff;
                }
                theConsumer.setPixels(0, y, width, 1, cm, scanline, 0,
                                      width);
            }
            break;
        default:
            needToCvt = true;
        }
    }

    if (needToCvt) {
        // REMIND: Need to add other types of CMs here
        ColorModel newcm = ColorModel.getRGBdefault();
        theConsumer.setColorModel(newcm);

        for (int y=0; y < height; y++) {
            for (int x=0; x < width; x++) {
                scanline[x] = image.getRGB(x, y);
            }
            theConsumer.setPixels(0, y, width, 1, newcm, scanline, 0,
                                  width);
        }
    }
}
 
Example 18
Source File: OffScreenImageSource.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
private void sendPixels() {
    ColorModel cm = image.getColorModel();
    WritableRaster raster = image.getRaster();
    int numDataElements = raster.getNumDataElements();
    int dataType = raster.getDataBuffer().getDataType();
    int[] scanline = new int[width*numDataElements];
    boolean needToCvt = true;

    if (cm instanceof IndexColorModel) {
        byte[] pixels = new byte[width];
        theConsumer.setColorModel(cm);

        if (raster instanceof ByteComponentRaster) {
            needToCvt = false;
            for (int y=0; y < height; y++) {
                raster.getDataElements(0, y, width, 1, pixels);
                theConsumer.setPixels(0, y, width, 1, cm, pixels, 0,
                                      width);
            }
        }
        else if (raster instanceof BytePackedRaster) {
            needToCvt = false;
            // Binary image.  Need to unpack it
            for (int y=0; y < height; y++) {
                raster.getPixels(0, y, width, 1, scanline);
                for (int x=0; x < width; x++) {
                    pixels[x] = (byte) scanline[x];
                }
                theConsumer.setPixels(0, y, width, 1, cm, pixels, 0,
                                      width);
            }
        }
        else if (dataType == DataBuffer.TYPE_SHORT ||
                 dataType == DataBuffer.TYPE_INT)
        {
            // Probably a short or int "GRAY" image
            needToCvt = false;
            for (int y=0; y < height; y++) {
                raster.getPixels(0, y, width, 1, scanline);
                theConsumer.setPixels(0, y, width, 1, cm, scanline, 0,
                                      width);
            }
        }
    }
    else if (cm instanceof DirectColorModel) {
        theConsumer.setColorModel(cm);
        needToCvt = false;
        switch (dataType) {
        case DataBuffer.TYPE_INT:
            for (int y=0; y < height; y++) {
                raster.getDataElements(0, y, width, 1, scanline);
                theConsumer.setPixels(0, y, width, 1, cm, scanline, 0,
                                      width);
            }
            break;
        case DataBuffer.TYPE_BYTE:
            byte[] bscanline = new byte[width];
            for (int y=0; y < height; y++) {
                raster.getDataElements(0, y, width, 1, bscanline);
                for (int x=0; x < width; x++) {
                    scanline[x] = bscanline[x]&0xff;
                }
                theConsumer.setPixels(0, y, width, 1, cm, scanline, 0,
                                      width);
            }
            break;
        case DataBuffer.TYPE_USHORT:
            short[] sscanline = new short[width];
            for (int y=0; y < height; y++) {
                raster.getDataElements(0, y, width, 1, sscanline);
                for (int x=0; x < width; x++) {
                    scanline[x] = sscanline[x]&0xffff;
                }
                theConsumer.setPixels(0, y, width, 1, cm, scanline, 0,
                                      width);
            }
            break;
        default:
            needToCvt = true;
        }
    }

    if (needToCvt) {
        // REMIND: Need to add other types of CMs here
        ColorModel newcm = ColorModel.getRGBdefault();
        theConsumer.setColorModel(newcm);

        for (int y=0; y < height; y++) {
            for (int x=0; x < width; x++) {
                scanline[x] = image.getRGB(x, y);
            }
            theConsumer.setPixels(0, y, width, 1, newcm, scanline, 0,
                                  width);
        }
    }
}
 
Example 19
Source File: OffScreenImageSource.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
private void sendPixels() {
    ColorModel cm = image.getColorModel();
    WritableRaster raster = image.getRaster();
    int numDataElements = raster.getNumDataElements();
    int dataType = raster.getDataBuffer().getDataType();
    int[] scanline = new int[width*numDataElements];
    boolean needToCvt = true;

    if (cm instanceof IndexColorModel) {
        byte[] pixels = new byte[width];
        theConsumer.setColorModel(cm);

        if (raster instanceof ByteComponentRaster) {
            needToCvt = false;
            for (int y=0; y < height; y++) {
                raster.getDataElements(0, y, width, 1, pixels);
                theConsumer.setPixels(0, y, width, 1, cm, pixels, 0,
                                      width);
            }
        }
        else if (raster instanceof BytePackedRaster) {
            needToCvt = false;
            // Binary image.  Need to unpack it
            for (int y=0; y < height; y++) {
                raster.getPixels(0, y, width, 1, scanline);
                for (int x=0; x < width; x++) {
                    pixels[x] = (byte) scanline[x];
                }
                theConsumer.setPixels(0, y, width, 1, cm, pixels, 0,
                                      width);
            }
        }
        else if (dataType == DataBuffer.TYPE_SHORT ||
                 dataType == DataBuffer.TYPE_INT)
        {
            // Probably a short or int "GRAY" image
            needToCvt = false;
            for (int y=0; y < height; y++) {
                raster.getPixels(0, y, width, 1, scanline);
                theConsumer.setPixels(0, y, width, 1, cm, scanline, 0,
                                      width);
            }
        }
    }
    else if (cm instanceof DirectColorModel) {
        theConsumer.setColorModel(cm);
        needToCvt = false;
        switch (dataType) {
        case DataBuffer.TYPE_INT:
            for (int y=0; y < height; y++) {
                raster.getDataElements(0, y, width, 1, scanline);
                theConsumer.setPixels(0, y, width, 1, cm, scanline, 0,
                                      width);
            }
            break;
        case DataBuffer.TYPE_BYTE:
            byte[] bscanline = new byte[width];
            for (int y=0; y < height; y++) {
                raster.getDataElements(0, y, width, 1, bscanline);
                for (int x=0; x < width; x++) {
                    scanline[x] = bscanline[x]&0xff;
                }
                theConsumer.setPixels(0, y, width, 1, cm, scanline, 0,
                                      width);
            }
            break;
        case DataBuffer.TYPE_USHORT:
            short[] sscanline = new short[width];
            for (int y=0; y < height; y++) {
                raster.getDataElements(0, y, width, 1, sscanline);
                for (int x=0; x < width; x++) {
                    scanline[x] = sscanline[x]&0xffff;
                }
                theConsumer.setPixels(0, y, width, 1, cm, scanline, 0,
                                      width);
            }
            break;
        default:
            needToCvt = true;
        }
    }

    if (needToCvt) {
        // REMIND: Need to add other types of CMs here
        ColorModel newcm = ColorModel.getRGBdefault();
        theConsumer.setColorModel(newcm);

        for (int y=0; y < height; y++) {
            for (int x=0; x < width; x++) {
                scanline[x] = image.getRGB(x, y);
            }
            theConsumer.setPixels(0, y, width, 1, newcm, scanline, 0,
                                  width);
        }
    }
}