for (int i = 0; i < data.length; i += stride) { buf.put((byte) cm.getRed(data[i])); // R buf.put((byte) cm.getGreen(data[i])); // G buf.put((byte) cm.getBlue(data[i])); // B buf.put((byte) cm.getAlpha(data[i])); // A
private void drawImage( SwingUniversalImage image, int centerX, int centerY, double angle, int imageSize ) { if ( isDrawingPixelatedImages() && image.isBitmap() ) { BufferedImage img = image.getAsBitmapForSize( imageSize, imageSize, angle ); ColorModel cm = img.getColorModel(); Raster raster = img.getRaster(); int offx = centerX + xOffset - img.getWidth() / 2; int offy = centerY + yOffset - img.getHeight() / 2; for ( int x = 0; x < img.getWidth( observer ); x++ ) { for ( int y = 0; y < img.getHeight( observer ); y++ ) { Object pix = raster.getDataElements( x, y, null ); gc.setColor( new Color( cm.getRed( pix ), cm.getGreen( pix ), cm.getBlue( pix ), cm.getAlpha( pix ) ) ); gc.setStroke( new BasicStroke( 1.0f ) ); gc.drawLine( offx + x, offy + y, offx + x + 1, offy + y + 1 ); } } } else { image.drawToGraphics( gc, centerX, centerY, imageSize, imageSize, angle ); } }
private void drawImage( SwingUniversalImage image, int locationX, int locationY, int imageSize ) { if ( isDrawingPixelatedImages() && image.isBitmap() ) { BufferedImage img = image.getAsBitmapForSize( imageSize, imageSize ); ColorModel cm = img.getColorModel(); Raster raster = img.getRaster(); for ( int x = 0; x < img.getWidth( observer ); x++ ) { for ( int y = 0; y < img.getHeight( observer ); y++ ) { Object pix = raster.getDataElements( x, y, null ); gc.setColor( new Color( cm.getRed( pix ), cm.getGreen( pix ), cm.getBlue( pix ), cm.getAlpha( pix ) ) ); gc.setStroke( new BasicStroke( 1.0f ) ); gc.drawLine( locationX + xOffset + x, locationY + yOffset + y, locationX + xOffset + x + 1, locationY + yOffset + y + 1 ); } } } else { image.drawToGraphics( gc, locationX, locationY, imageSize, imageSize ); } }
/** * Gets a specific pixel color from the specified buffered image * * @param image * @param i * @param j * @param color */ protected Color getPixelColor(BufferedImage image, int i, int j) { ColorModel cm = image.getColorModel(); Raster raster = image.getRaster(); Object pixel = raster.getDataElements(i, j, null); Color actual; if (cm.hasAlpha()) { actual = new Color( cm.getRed(pixel), cm.getGreen(pixel), cm.getBlue(pixel), cm.getAlpha(pixel)); } else { actual = new Color(cm.getRed(pixel), cm.getGreen(pixel), cm.getBlue(pixel), 255); } return actual; }
pixelOutput[1] = rasterCM.getGreen(pixelInput); pixelOutput[2] = rasterCM.getBlue(pixelInput); pixelOutput[3] = rasterCM.getAlpha(pixelInput);
/** * Gets a specific pixel color from the specified buffered image * * @param image * @param i * @param j * @param color * @return */ protected Color getPixelColor(BufferedImage image, int i, int j) { ColorModel cm = image.getColorModel(); Raster raster = image.getRaster(); Object pixel = raster.getDataElements(i, j, null); Color actual; if (cm.hasAlpha()) { actual = new Color( cm.getRed(pixel), cm.getGreen(pixel), cm.getBlue(pixel), cm.getAlpha(pixel)); } else { actual = new Color(cm.getRed(pixel), cm.getGreen(pixel), cm.getBlue(pixel), 255); } return actual; } }
/** * Gets a specific pixel color from the specified buffered image * * @param image * @param i * @param j * @param color * @return */ private static Color getPixelColor(BufferedImage image, int i, int j) { ColorModel cm = image.getColorModel(); Raster raster = image.getRaster(); Object pixel = raster.getDataElements(i, j, null); Color actual; if (cm.hasAlpha()) { actual = new Color( cm.getRed(pixel), cm.getGreen(pixel), cm.getBlue(pixel), cm.getAlpha(pixel)); } else { actual = new Color(cm.getRed(pixel), cm.getGreen(pixel), cm.getBlue(pixel), 255); } return actual; }
public void setPixels(int x, int y, int w, int h, ColorModel model, byte[] pixels, int off, int scansize) { for ( int m = 0; m < w; m++ ) { for ( int n = 0; n < h; n++ ) { int mappedY = y; if (myFlipVerticalFlag) { mappedY = m_height-(y+n)-1; } int new_location = ( mappedY * m_width + (x+m) ) * 3; int orig_location = n*scansize + m + off; // Stu asks - is this approach to pixel extraction correct? // We must remember that bytes are *signed* 8 bit values! m_bytes[new_location+0] = (byte)model.getBlue(pixels[orig_location]); m_bytes[new_location+1] = (byte)model.getGreen(pixels[orig_location]); m_bytes[new_location+2] = (byte)model.getRed(pixels[orig_location]); } } }
private synchronized void setPixelsPrivate(int x, int y, int w, int h, ColorModel model, int[] pixels, int off, int scansize) { for ( int m = 0; m < w; m++ ) { for ( int n = 0; n < h; n++ ) { int mappedY = y; if (myFlipVerticalFlag) { mappedY = m_height-(y+n)-1; } int new_location = ( mappedY * m_width + (x+m) ) * 3; int orig_location = n*scansize + m + off; // Stu asks - is this approach to pixel extraction correct? // We must remember that bytes are *signed* 8 bit values! m_bytes[new_location+0] = (byte)model.getBlue(pixels[orig_location]); m_bytes[new_location+1] = (byte)model.getGreen(pixels[orig_location]); m_bytes[new_location+2] = (byte)model.getRed(pixels[orig_location]); } } }
public void setPixels(int x, int y, int w, int h, ColorModel model, byte[] pixels, int off, int scansize) { for ( int m = 0; m < w; m++ ) { for ( int n = 0; n < h; n++ ) { int mappedY = y; if (myFlipVerticalFlag) { mappedY = m_height-(y+n)-1; } int new_location = ( mappedY * m_width + (x+m) ) * 3; int orig_location = n*scansize + m + off; // Stu asks - is this approach to pixel extraction correct? // We must remember that bytes are *signed* 8 bit values! m_bytes[new_location+0] = (byte)model.getBlue(pixels[orig_location]); m_bytes[new_location+1] = (byte)model.getGreen(pixels[orig_location]); m_bytes[new_location+2] = (byte)model.getRed(pixels[orig_location]); } } }
private synchronized void setPixelsPrivate(int x, int y, int w, int h, ColorModel model, int[] pixels, int off, int scansize) { for ( int m = 0; m < w; m++ ) { for ( int n = 0; n < h; n++ ) { int mappedY = y; if (myFlipVerticalFlag) { mappedY = m_height-(y+n)-1; } int new_location = ( mappedY * m_width + (x+m) ) * 3; int orig_location = n*scansize + m + off; // Stu asks - is this approach to pixel extraction correct? // We must remember that bytes are *signed* 8 bit values! m_bytes[new_location+0] = (byte)model.getBlue(pixels[orig_location]); m_bytes[new_location+1] = (byte)model.getGreen(pixels[orig_location]); m_bytes[new_location+2] = (byte)model.getRed(pixels[orig_location]); } } }
/** * @param pixels * @param cm * @param byteBuffer * @param rdi */ private static void copyIntValues( int[] pixels, ColorModel cm, ByteBuffer byteBuffer, RasterDataInfo rdi ) { byteBuffer.clear(); byte[] res = new byte[rdi.bands()]; for ( int i = 0; i < pixels.length; ++i ) { int pixel = pixels[i]; res[0] = (byte) cm.getRed( pixel ); res[1] = (byte) cm.getGreen( pixel ); res[2] = (byte) cm.getBlue( pixel ); if ( res.length == 4 ) { res[3] = (byte) cm.getAlpha( i ); } byteBuffer.put( res ); // System.out.println( "pixel(" + pixels.length + "): " + i + "| color: " + ( res[0] & 0xff ) + "," // + ( res[1] & 0xff ) + "," + ( res[2] & 0xff ) ); // System.out.println( "pos(" + byteBuffer.position() + "): " + i + "| color: " + ( res[0] & 0xff ) + "," // + ( res[1] & 0xff ) + "," + ( res[2] & 0xff ) ); } }
/** * @param pixels * @param cm * @param byteBuffer * @param rdi */ private static void copyShortValues( short[] pixels, ColorModel cm, ByteBuffer byteBuffer, RasterDataInfo rdi ) { byte[] res = new byte[rdi.bands()]; for ( int i = 0; i < pixels.length; ++i ) { short pixel = pixels[i]; res[0] = (byte) cm.getRed( pixel ); res[1] = (byte) cm.getGreen( pixel ); res[2] = (byte) cm.getBlue( pixel ); if ( res.length == 4 ) { res[3] = (byte) cm.getAlpha( i ); } byteBuffer.put( res ); } }
enum ImageDataType { TYPE_NULL, TYPE_BYTE_ARRAY, TYPE_INT_ARRAY, TYPE_BYTE_BUFFER, TYPE_INT_BUFFER }
int width = bufferedImage.getWidth(), height = bufferedImage.getHeight(); Object dataElements = null; Raster raster = bufferedImage.getRaster(); ColorModel colorModel = bufferedImage.getColorModel(); int red, blue, green, alpha; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { dataElements = raster.getDataElements(x, y, dataElements); // extract colors red = colorModel.getRed(dataElements); blue = colorModel.getBlue(dataElements); green = colorModel.getGreen(dataElements); alpha = colorModel.getAlpha(dataElements); } }
index = i * width + j; m_printstream.print(toHex(model.getRed(pixels[index]))); m_printstream.print(toHex(model.getGreen(pixels[index]))); m_printstream.print(toHex(model.getBlue(pixels[index])));
public void computeRect(Raster[] sources, WritableRaster dest, Rectangle destRect) { ColorModel srcCM = getSourceImage(0).getColorModel(); ColorModel destCM = getColorModel(); Object srcData = null; Object destData = null; for (int i = destRect.x + destRect.width; --i >= destRect.x;) { for (int j = destRect.y + destRect.height; --j >= destRect.y;) { srcData = sources[0].getDataElements(i, j, srcData); int rgb = srcCM.getRGB(srcData); if (srcCM.getRed(srcData) == transColor.getRed() && srcCM.getGreen(srcData) == transColor.getGreen() && srcCM.getBlue(srcData) == transColor.getBlue()) { destData = destCM.getDataElements(transColor.getRGB(), destData); } else { destData = destCM.getDataElements(rgb, destData); } dest.setDataElements(i, j, destData); } } }
index = i * width + j; m_printstream.print(toHex(model.getRed(pixels[index]))); m_printstream.print(toHex(model.getGreen(pixels[index]))); m_printstream.print(toHex(model.getBlue(pixels[index])));
pixelOutput[1] = rasterCM.getGreen(pixelInput); pixelOutput[2] = rasterCM.getBlue(pixelInput); pixelOutput[3] = rasterCM.getAlpha(pixelInput);
/** * Gets a specific pixel color from the specified buffered image * * @param image * @param i * @param j * @param color */ protected Color getPixelColor(BufferedImage image, int i, int j) { ColorModel cm = image.getColorModel(); Raster raster = image.getRaster(); Object pixel = raster.getDataElements(i, j, null); Color actual; if (cm.hasAlpha()) { actual = new Color( cm.getRed(pixel), cm.getGreen(pixel), cm.getBlue(pixel), cm.getAlpha(pixel)); } else { actual = new Color(cm.getRed(pixel), cm.getGreen(pixel), cm.getBlue(pixel), 255); } return actual; } }