Here is the error I observe, I might do something wrong:
<code>RangeError (index): Index out of range: index should be less than 3: 3
dart:typed_data Uint8List.[]
package:image/src/image/palette_uint8.dart 66:35 PaletteUint8.get
package:image/src/image/pixel_uint1.dart 166:18 PixelUint1._getChannel
</code>
<code>RangeError (index): Index out of range: index should be less than 3: 3
dart:typed_data Uint8List.[]
package:image/src/image/palette_uint8.dart 66:35 PaletteUint8.get
package:image/src/image/pixel_uint1.dart 166:18 PixelUint1._getChannel
</code>
RangeError (index): Index out of range: index should be less than 3: 3
dart:typed_data Uint8List.[]
package:image/src/image/palette_uint8.dart 66:35 PaletteUint8.get
package:image/src/image/pixel_uint1.dart 166:18 PixelUint1._getChannel
The input is a simple png of 5×5 yellow pixels.
Here is the code:
<code>Iterable<img.Image> getUniqTiles(img.Image image, int tileSize) {
// Map to store unique tiles
final Map<String, img.Image> uniqueTiles = {};
// Split image into tiles
for (int y = 0; y < image.height; y += tileSize) {
for (int x = 0; x < image.width; x += tileSize) {
if (y + tileSize > image.height || x + tileSize > image.width) {
dev.log("skip too small tile");
continue;
}
final img.Image tile = img.copyCrop(
image,
x: x,
y: y,
width: tileSize,
height: tileSize,
antialias: false,
);
// Convert tile to a unique key
final tileKey = _tileToKey(tile);
uniqueTiles[tileKey] = tile;
}
}
return uniqueTiles.values;
}
String _tileToKey(img.Image tile) {
if (!tile.isValid) {
throw StateError("tile invalid");
}
if (tile.isEmpty) {
throw StateError("tile empty");
}
final buffer = StringBuffer();
for (int y = 0; y < tile.height; y++) {
for (int x = 0; x < tile.width; x++) {
final pixel = tile.getPixel(x, y);
if (!pixel.isValid) {
throw StateError("invalid pixel");
}
num red = pixel.r;
num green = pixel.g;
num blue = pixel.b;
num alpha = pixel.a;
buffer.write('$red,$green,$blue,$alpha;');
}
}
return buffer.toString();
}
void main() {
test('Counter value should be incremented', () async {
img.Image? image = await img.decodePngFile('test/testData/5x5yellow.png');
if (image == null) {
throw StateError("bad input");
}
var tiles = getUniqTiles(image, 5);
expect(tiles.length, 1);
});
}
</code>
<code>Iterable<img.Image> getUniqTiles(img.Image image, int tileSize) {
// Map to store unique tiles
final Map<String, img.Image> uniqueTiles = {};
// Split image into tiles
for (int y = 0; y < image.height; y += tileSize) {
for (int x = 0; x < image.width; x += tileSize) {
if (y + tileSize > image.height || x + tileSize > image.width) {
dev.log("skip too small tile");
continue;
}
final img.Image tile = img.copyCrop(
image,
x: x,
y: y,
width: tileSize,
height: tileSize,
antialias: false,
);
// Convert tile to a unique key
final tileKey = _tileToKey(tile);
uniqueTiles[tileKey] = tile;
}
}
return uniqueTiles.values;
}
String _tileToKey(img.Image tile) {
if (!tile.isValid) {
throw StateError("tile invalid");
}
if (tile.isEmpty) {
throw StateError("tile empty");
}
final buffer = StringBuffer();
for (int y = 0; y < tile.height; y++) {
for (int x = 0; x < tile.width; x++) {
final pixel = tile.getPixel(x, y);
if (!pixel.isValid) {
throw StateError("invalid pixel");
}
num red = pixel.r;
num green = pixel.g;
num blue = pixel.b;
num alpha = pixel.a;
buffer.write('$red,$green,$blue,$alpha;');
}
}
return buffer.toString();
}
void main() {
test('Counter value should be incremented', () async {
img.Image? image = await img.decodePngFile('test/testData/5x5yellow.png');
if (image == null) {
throw StateError("bad input");
}
var tiles = getUniqTiles(image, 5);
expect(tiles.length, 1);
});
}
</code>
Iterable<img.Image> getUniqTiles(img.Image image, int tileSize) {
// Map to store unique tiles
final Map<String, img.Image> uniqueTiles = {};
// Split image into tiles
for (int y = 0; y < image.height; y += tileSize) {
for (int x = 0; x < image.width; x += tileSize) {
if (y + tileSize > image.height || x + tileSize > image.width) {
dev.log("skip too small tile");
continue;
}
final img.Image tile = img.copyCrop(
image,
x: x,
y: y,
width: tileSize,
height: tileSize,
antialias: false,
);
// Convert tile to a unique key
final tileKey = _tileToKey(tile);
uniqueTiles[tileKey] = tile;
}
}
return uniqueTiles.values;
}
String _tileToKey(img.Image tile) {
if (!tile.isValid) {
throw StateError("tile invalid");
}
if (tile.isEmpty) {
throw StateError("tile empty");
}
final buffer = StringBuffer();
for (int y = 0; y < tile.height; y++) {
for (int x = 0; x < tile.width; x++) {
final pixel = tile.getPixel(x, y);
if (!pixel.isValid) {
throw StateError("invalid pixel");
}
num red = pixel.r;
num green = pixel.g;
num blue = pixel.b;
num alpha = pixel.a;
buffer.write('$red,$green,$blue,$alpha;');
}
}
return buffer.toString();
}
void main() {
test('Counter value should be incremented', () async {
img.Image? image = await img.decodePngFile('test/testData/5x5yellow.png');
if (image == null) {
throw StateError("bad input");
}
var tiles = getUniqTiles(image, 5);
expect(tiles.length, 1);
});
}
The error occurs on num red = pixel.r;
(when I try to access the pixel value) in _tileToKey
my usage ofimg.copyCrop
might be the issue.