У меня есть этот код:
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, когда я выделяю массив перед передачей его getRGB. Я проверяю в отладчике и изображение имеет размер и выделяется.
UPDATE:
GetRGB
<code> /**
* 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, y) in the
* image, the ARGB pixel can be accessed in this way:
* </p>
*
* <pre>
* pixel = rgbArray[offset + (y-startY)*scansize + (x-startX)];
*
*
*
* Может быть брошено ArrayOutOfBoundsException
* если регион не в границах.
* Однако явная проверка границ не гарантируется.
*
* @param startX начальная координата X
* @param startY начальная координата Y
* @param w ширина области
* @param h высота области
* @param rgbArray, если не null
, пиксели rgb
* написано здесь
Смещение * @param в rgbArray
* @param scansize scanline stepde для rgbArray
* @return массив RGB пикселей.
* @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 = смещение;
int off;
Данные объекта;
int nbands = raster.getNumBands ();
int dataType = raster.getDataBuffer (). getDataType ();
switch (dataType) {
case DataBuffer.TYPE_BYTE:
данные = новый байт [nbands];
перерыв;
case DataBuffer.TYPE_USHORT:
данные = новые короткие [nbands];
перерыв;
case DataBuffer.TYPE_INT:
data = new int [nbands];
перерыв;
case DataBuffer.TYPE_FLOAT:
data = new float [nbands];
перерыв;
case DataBuffer.TYPE_DOUBLE:
data = new double [nbands];
перерыв;
дефолт:
бросить новое IllegalArgumentException ("Неизвестный тип буфера данных:" +
тип данных);
}
if (rgbArray == null) {
rgbArray = new int [offset + h * scansize];
}
for (int y = startY; y