Skip to content

Commit

Permalink
improve convertColor to 1-channel; fix bmp encoder for 1bpp images
Browse files Browse the repository at this point in the history
  • Loading branch information
brendan-duncan committed Jan 15, 2023
1 parent f06fdb4 commit 8d2ee6c
Show file tree
Hide file tree
Showing 9 changed files with 21 additions and 14 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ out/
pubspec.lock
.packages
.DS_Store
_out/
5 changes: 0 additions & 5 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,3 @@ linter:
- unnecessary_string_interpolations
- use_string_buffers
- void_checks

analyzer:
strong-mode:
implicit-casts: false
implicit-dynamic: false
6 changes: 3 additions & 3 deletions lib/src/formats/bmp_encoder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import 'encoder.dart';

/// Encode a BMP image.
class BmpEncoder extends Encoder {
int _roundToMultiple(int x) {
/*int _roundToMultiple(int x) {
final y = x & 0x3;
if (y == 0) {
return x;
}
return x + 4 - y;
}
}*/

@override
Uint8List encode(Image image, {bool singleFrame = false}) {
Expand Down Expand Up @@ -101,7 +101,7 @@ class BmpEncoder extends Encoder {
final headerSize = headerInfoSize + 14;
final paletteSize = (image.palette?.numColors ?? 0) * 4;
final origImageOffset = headerSize + paletteSize;
final imageOffset = _roundToMultiple(origImageOffset);
final imageOffset = origImageOffset; //_roundToMultiple(origImageOffset);
final gapSize = imageOffset - origImageOffset;
final fileSize = imageFileSize + headerSize + paletteSize + gapSize;

Expand Down
5 changes: 4 additions & 1 deletion lib/src/util/color_util.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ Color _convertColor(Color c, Color c2, num a) {
final format = c2.format;
final fromFormat = c.palette?.format ?? c.format;
final cl = c.length;
if (numChannels <= cl) {
if (numChannels == 1) {
final g = c.length > 2 ? c.luminance : c[0];
c2[0] = convertFormatValue(g, fromFormat, format);
} else if (numChannels <= cl) {
for (var ci = 0; ci < numChannels; ++ci) {
c2[ci] = convertFormatValue(c[ci], fromFormat, format);
}
Expand Down
Binary file added test/_data/bmp/alpha_bmp.bmp
Binary file not shown.
Binary file added test/_data/png/alpha_bmp.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion test/_test_util.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import 'package:image/image.dart';
import 'package:test/test.dart';

final testOutputPath = '${Directory.systemTemp.createTempSync().path}/out';
//const testOutputPath = './out';
//const testOutputPath = './_out';

int hashImage(Image image) {
var hash = 0;
Expand Down
8 changes: 8 additions & 0 deletions test/image/image_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -169,5 +169,13 @@ void main() {
}
}
});
test('alpha_bmp_1bpp', () async {
final img = await decodePngFile('test/_data/png/alpha_bmp.png');
final bg = Image(width: img!.width, height: img.height)
..clear(ColorRgb8(255, 255, 255));
compositeImage(bg, img);
final bpp1 = bg.convert(format: Format.uint1, numChannels: 1);
await encodeBmpFile('$testOutputPath/bmp/alpha_bmp_cvt.bmp', bpp1);
});
});
}
8 changes: 4 additions & 4 deletions test/transform/copy_resize_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ void main() {

test('copyResize palette', () async {
final img = await decodePngFile('test/_data/png/test.png');
final i0 = copyResize(img!, width: 64,
interpolation: Interpolation.cubic);
await encodePngFile('$testOutputPath/transform/copyResize_palette.png',
i0);
final i0 =
copyResize(img!, width: 64, interpolation: Interpolation.cubic);
await encodePngFile(
'$testOutputPath/transform/copyResize_palette.png', i0);
});
});
}

0 comments on commit 8d2ee6c

Please sign in to comment.