Skip to content

Commit 1af3a8b

Browse files
fix tiff tile decode
1 parent 86d4101 commit 1af3a8b

File tree

2 files changed

+34
-20
lines changed

2 files changed

+34
-20
lines changed

lib/src/formats/tiff/tiff_image.dart

+23-9
Original file line numberDiff line numberDiff line change
@@ -383,8 +383,8 @@ class TiffImage {
383383
throw ImageException('Unsupported Compression Type: $compression');
384384
}
385385

386-
for (var y = 0, py = outY; y < tileHeight && py < height; ++y, ++py) {
387-
for (var x = 0, px = outX; x < tileWidth && px < width; ++x, ++px) {
386+
for (var y = 0, py = outY; y < tileHeight; ++y, ++py) {
387+
for (var x = 0, px = outX; x < tileWidth; ++x, ++px) {
388388
if (samplesPerPixel == 1) {
389389
if (sampleFormat == TiffFormat.float) {
390390
num sample = 0;
@@ -395,7 +395,9 @@ class TiffImage {
395395
} else if (bitsPerSample == 16) {
396396
sample = Float16.float16ToDouble(byteData.readUint16());
397397
}
398-
image.setPixelR(px, py, sample);
398+
if (px < width && py < height) {
399+
image.setPixelR(px, py, sample);
400+
}
399401
} else {
400402
var sample = 0;
401403
if (bitsPerSample == 8) {
@@ -417,7 +419,9 @@ class TiffImage {
417419
sample = mx - sample;
418420
}
419421

420-
image.setPixelR(px, py, sample);
422+
if (px < width && py < height) {
423+
image.setPixelR(px, py, sample);
424+
}
421425
}
422426
} else if (samplesPerPixel == 2) {
423427
var gray = 0;
@@ -445,7 +449,9 @@ class TiffImage {
445449
: byteData.readUint32();
446450
}
447451

448-
image.setPixelRgb(px, py, gray, alpha, 0);
452+
if (px < width && py < height) {
453+
image.setPixelRgb(px, py, gray, alpha, 0);
454+
}
449455
} else if (samplesPerPixel == 3) {
450456
if (sampleFormat == TiffFormat.float) {
451457
var r = 0.0;
@@ -464,7 +470,9 @@ class TiffImage {
464470
g = Float16.float16ToDouble(byteData.readUint16());
465471
b = Float16.float16ToDouble(byteData.readUint16());
466472
}
467-
image.setPixelRgb(px, py, r, g, b);
473+
if (px < width && py < height) {
474+
image.setPixelRgb(px, py, r, g, b);
475+
}
468476
} else {
469477
var r = 0;
470478
var g = 0;
@@ -501,7 +509,9 @@ class TiffImage {
501509
: byteData.readUint32();
502510
}
503511

504-
image.setPixelRgb(px, py, r, g, b);
512+
if (px < width && py < height) {
513+
image.setPixelRgb(px, py, r, g, b);
514+
}
505515
}
506516
} else if (samplesPerPixel >= 4) {
507517
if (sampleFormat == TiffFormat.float) {
@@ -525,7 +535,9 @@ class TiffImage {
525535
b = Float16.float16ToDouble(byteData.readUint16());
526536
a = Float16.float16ToDouble(byteData.readUint16());
527537
}
528-
image.setPixelRgba(px, py, r, g, b, a);
538+
if (px < width && py < height) {
539+
image.setPixelRgba(px, py, r, g, b, a);
540+
}
529541
} else {
530542
var r = 0;
531543
var g = 0;
@@ -580,7 +592,9 @@ class TiffImage {
580592
a = image.maxChannelValue as int;
581593
}
582594

583-
image.setPixelRgba(px, py, r, g, b, a);
595+
if (px < width && py < height) {
596+
image.setPixelRgba(px, py, r, g, b, a);
597+
}
584598
}
585599
}
586600
}

test/formats/tiff_test.dart

+11-11
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,16 @@ import '../_test_util.dart';
77
void main() {
88
group('Format', () {
99
group('tiff', () {
10-
test('16bit colormap', () async {
10+
test('deflate.tif', () async {
11+
final bytes = File('test/_data/tiff/deflate.tif').readAsBytesSync();
12+
final i0 = decodeTiff(bytes);
13+
expect(i0, isNotNull);
14+
File('$testOutputPath/tif/deflate.png')
15+
..createSync(recursive: true)
16+
..writeAsBytesSync(encodePng(i0!));
17+
});
18+
19+
/*test('16bit colormap', () async {
1120
final bytes = File('test/_data/tiff/CNSW_crop.tif').readAsBytesSync();
1221
final i1 = decodeTiff(bytes);
1322
expect(i1, isNotNull);
@@ -17,15 +26,6 @@ void main() {
1726
..writeAsBytesSync(encodePng(o1));
1827
});
1928
20-
/*test('rgb.tiff', () async {
21-
final bytes = File('test/_data/tiff/rgb.tif').readAsBytesSync();
22-
final i0 = decodeTiff(bytes);
23-
expect(i0, isNotNull);
24-
File('$testOutputPath/tif/rgb.png')
25-
..createSync(recursive: true)
26-
..writeAsBytesSync(encodePng(i0!));
27-
});*/
28-
2929
test('encode', () async {
3030
final i0 = Image(width: 256, height: 256);
3131
for (final p in i0) {
@@ -164,7 +164,7 @@ void main() {
164164
..writeAsBytesSync(encodePng(i3));
165165
});
166166
}
167-
});
167+
});*/
168168
});
169169
});
170170
}

0 commit comments

Comments
 (0)