brendan-duncan / image

Dart Image Library for opening, manipulating, and saving various different image file formats.
MIT License
1.14k stars 255 forks source link

img.data[index] = 0xFF000000 | (b << 16) | (g << 8) | r; #621

Closed ronak-cipl closed 3 months ago

ronak-cipl commented 4 months ago

img.data[index] = 0xFF000000 | (b << 16) | (g << 8) | r;

error on this line in [image](image: ^4.1.7):

The method '[]' can't be unconditionally invoked because the receiver can be 'null'. (Documentation) Try making the call conditional (using '?.') or adding a null check to the target ('!').

brendan-duncan commented 3 months ago

data is nullable.

For what you're trying to do here, use

final data = Uint32List(img.buffer);
data[index] = 0xFF000000 | (b << 16) | (g << 8) | r;

Or you can access the bytes directly,

final data = img.toUint8List();
data[0] = r;
data[1] = g;
data[2] = b;
data[3] = 0xFF;
ronak-cipl commented 3 months ago

one more issue in latest image lib

var pixel = image.getPixel(j, i); buffer[pixelIndex++] = (imglib.getRed(pixel) - mean) / std; buffer[pixelIndex++] = (imglib.getGreen(pixel) - mean) / std; buffer[pixelIndex++] = (imglib.getBlue(pixel) - mean) / std;

getRed, getGreen, getBlue those functions are not define.

var pixel = image.getPixel(j, i); buffer[pixelIndex++] = ((pixel & 0xFF) - mean) / std; buffer[pixelIndex++] = (((pixel >> 8) & 0xFF) - mean) / std; buffer[pixelIndex++] = (((pixel >> 16) & 0xFF) - mean) / std;

this operand (& and >>) is also not support

please help for solutions.

brendan-duncan commented 3 months ago

Pixel has r, g, b, and a getters.

var pixel = image.getPixel(j, i);
buffer[pixelIndex++] = (pixel.r - mean) / std;
buffer[pixelIndex++] = (pixel.g - mean) / std;
buffer[pixelIndex++] = (pixel.b - mean) / std;

Using getPixel for each pixel is slower, so you can use an iterator to improve performance a little bit:

for (final pixel in image) {
  buffer[pixelIndex++] = (pixel.r - mean) / std;
  buffer[pixelIndex++] = (pixel.g - mean) / std;
  buffer[pixelIndex++] = (pixel.b - mean) / std;
}

The 4.* version of the library uses abstract image data / pixels instead of the previous "encode everything as a uint32." The current version supports many different image formats. But if you know your image is rgba32, you can emulate the past behavior using final uint32Buffer = Uint32List(image.buffer);

ronak-cipl commented 3 months ago

not work,

I am using face recognition with tensorflowlite model

working file in image: ^3.3.0 version but not work in latest version image: ^4.1.7