缓冲图像像素操作 [英] Buffered image pixel manipulation

查看:119
本文介绍了缓冲图像像素操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码:

public Image toNegative()
{
    int imageWidth =  originalImage.getWidth();
    int imageHeight = originalImage.getHeight();
    int [] rgb = null; // new int[imageWidth * imageWidth];
    originalImage.getRGB(0, 0, imageWidth, imageHeight, rgb, 0,imageWidth);

    for (int y = 0; y < imageHeight; y++)
    {
         for (int x = 0; x < imageWidth; x++)
         {
             int index = y * imageWidth + x;
             int R = (rgb[index] >> 16) & 0xff;     //bitwise shifting
             int G = (rgb[index] >> 8) & 0xff;
             int B = rgb[index] & 0xff;

             R = 255 - R;
             G = 255 - R;
             B = 255 - R;

             rgb[index] = 0xff000000 | (R << 16) | (G << 8) | B;                                
         }
    }


    return getImageFromArray(rgb, imageWidth, imageHeight);
}

当我在传递之前分配数组时,它会抛出NPE或使用数组或抛出ArrayOutOfBoundsException它得到了RGB。我签入调试器并且图像具有大小并被分配。

It throws NPE or when array is used or ArrayOutOfBoundsException when I allocate array before passing it getRGB. I check in debugger and image has the size and is allocated.

更新:
getRGB

UPDATE: The getRGB

 /**
 * Returns an array of integer pixels in the default RGB color model
 * (TYPE_INT_ARGB) and default sRGB color space,
 * from a portion of the image data.  Color conversion takes
 * place if the default model does not match the image
 * <code>ColorModel</code>.  There are only 8-bits of precision for
 * each color component in the returned data when
 * using this method.  With a specified coordinate (x,&nbsp;y) in the
 * image, the ARGB pixel can be accessed in this way:
 * </p>
 *
 * <pre>
 *    pixel   = rgbArray[offset + (y-startY)*scansize + (x-startX)]; </pre>
 *
 * <p>
 *
 * An <code>ArrayOutOfBoundsException</code> may be thrown
 * if the region is not in bounds.
 * However, explicit bounds checking is not guaranteed.
 *
 * @param startX      the starting X coordinate
 * @param startY      the starting Y coordinate
 * @param w           width of region
 * @param h           height of region
 * @param rgbArray    if not <code>null</code>, the rgb pixels are
 *          written here
 * @param offset      offset into the <code>rgbArray</code>
 * @param scansize    scanline stride for the <code>rgbArray</code>
 * @return            array of RGB pixels.
 * @see #setRGB(int, int, int)
 * @see #setRGB(int, int, int, int, int[], int, int)
 */
public int[] getRGB(int startX, int startY, int w, int h,
                    int[] rgbArray, int offset, int scansize) {
    int yoff  = offset;
    int off;
    Object data;
    int nbands = raster.getNumBands();
    int dataType = raster.getDataBuffer().getDataType();
    switch (dataType) {
    case DataBuffer.TYPE_BYTE:
        data = new byte[nbands];
        break;
    case DataBuffer.TYPE_USHORT:
        data = new short[nbands];
        break;
    case DataBuffer.TYPE_INT:
        data = new int[nbands];
        break;
    case DataBuffer.TYPE_FLOAT:
        data = new float[nbands];
        break;
    case DataBuffer.TYPE_DOUBLE:
        data = new double[nbands];
        break;
    default:
        throw new IllegalArgumentException("Unknown data buffer type: "+
                                           dataType);
    }

    if (rgbArray == null) {
        rgbArray = new int[offset+h*scansize];
    }

    for (int y = startY; y < startY+h; y++, yoff+=scansize) {
        off = yoff;
        for (int x = startX; x < startX+w; x++) {
            rgbArray[off++] = colorModel.getRGB(raster.getDataElements(x,
                                                                    y,
                                                                    data));
        }
    }

    return rgbArray;
}


推荐答案

您的代码将抛出 NullPointerException 因为您永远不会为<$​​ c $ c> rgb 变量分配非空引用。因此,对它的引用(例如 rgb [index] )将生成异常。如果您希望将null数组传入getRGB,则需要确保分配方法返回的结果数组;例如。

Your code will throw a NullPointerException because you are never assigning a non-null reference to the rgb variable. Hence, references to it (e.g. rgb[index]) will generate the exception. If you wish to pass in a null array to getRGB you need to ensure you assign the result array returned by the method; e.g.

int[] rgb = originalImage.getRGB(0, 0, imageWidth, imageHeight, rgb, 0,imageWidth);

如果您要取消注释注释掉的代码,那么您将分配数组作为错误 imageWidth * imageWidth 而不是 imageWidth * imageHeight ,这就是为什么你会看到 ArrayIndexOutOfBoundsException

If you were to uncomment the code commented out there is a bug in that you are allocating the array as imageWidth * imageWidth instead of imageWidth * imageHeight, which is why you're seeing an ArrayIndexOutOfBoundsException.

这篇关于缓冲图像像素操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆