Skip to content

Commit

Permalink
2 channel images map to 4 channe
Browse files Browse the repository at this point in the history
  • Loading branch information
brendan-duncan committed Jan 15, 2024
1 parent e1892ee commit 9f39c25
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 20 deletions.
41 changes: 27 additions & 14 deletions lib/src/image/pixel_uint8.dart
Original file line number Diff line number Diff line change
Expand Up @@ -158,42 +158,54 @@ class PixelUint8 extends Iterable<num> implements Pixel {

@override
num get g => palette == null
? numChannels > 1
? data[_index + 1]
: 0
? numChannels == 2
? data[_index]
: numChannels > 1
? data[_index + 1]
: 0
: palette!.getGreen(data[_index]);

@override
set g(num g) {
if (image.numChannels > 1) {
if (numChannels == 2) {
data[_index] = g.clamp(0, 255).toInt();
} else if (image.numChannels > 1) {
data[_index + 1] = g.clamp(0, 255).toInt();
}
}

@override
num get b => palette == null
? numChannels > 2
? data[_index + 2]
: 0
? numChannels == 2
? data[_index]
: numChannels > 2
? data[_index + 2]
: 0
: palette!.getBlue(data[_index]);

@override
set b(num b) {
if (image.numChannels > 2) {
if (numChannels == 2) {
data[_index] = b.clamp(0, 255).toInt();
} else if (image.numChannels > 2) {
data[_index + 2] = b.clamp(0, 255).toInt();
}
}

@override
num get a => palette == null
? numChannels > 3
? data[_index + 3]
: 255
? numChannels == 2
? data[_index + 1]
: numChannels > 3
? data[_index + 3]
: 255
: palette!.getAlpha(data[_index]);

@override
set a(num a) {
if (image.numChannels > 3) {
if (numChannels == 2) {
data[_index + 1] = a.clamp(0, 255).toInt();
} else if (image.numChannels > 3) {
data[_index + 3] = a.clamp(0, 255).toInt();
}
}
Expand All @@ -219,9 +231,10 @@ class PixelUint8 extends Iterable<num> implements Pixel {
set aNormalized(num v) => a = v * maxChannelValue;

@override
num get luminance => getLuminance(this);
num get luminance => numChannels == 2 ? r : getLuminance(this);
@override
num get luminanceNormalized => getLuminanceNormalized(this);
num get luminanceNormalized =>
numChannels == 2 ? rNormalized : getLuminanceNormalized(this);

@override
num getChannel(Channel channel) => channel == Channel.luminance
Expand Down
27 changes: 21 additions & 6 deletions lib/src/util/color_util.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,27 @@ Color _convertColor(Color c, Color c2, num a) {
c2[ci] = convertFormatValue(c[ci], fromFormat, format);
}
} else {
for (var ci = 0; ci < cl; ++ci) {
c2[ci] = convertFormatValue(c[ci], fromFormat, format);
}
final v = cl == 1 ? c2[0] : 0;
for (var ci = cl; ci < numChannels; ++ci) {
c2[ci] = ci == 3 ? a : v;
if (cl == 2) {
final l = convertFormatValue(c[0], fromFormat, format);
if (numChannels == 3) {
c2[0] = l;
c2[1] = l;
c2[2] = l;
} else {
final a = convertFormatValue(c[1], fromFormat, format);
c2[0] = l;
c2[1] = l;
c2[2] = l;
c2[3] = a;
}
} else {
for (var ci = 0; ci < cl; ++ci) {
c2[ci] = convertFormatValue(c[ci], fromFormat, format);
}
final v = cl == 1 ? c2[0] : 0;
for (var ci = cl; ci < numChannels; ++ci) {
c2[ci] = ci == 3 ? a : v;
}
}
}
return c2;
Expand Down
Binary file added test/_data/png/png_LA.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions test/formats/png_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ void main() {
const buck24Hash = 817446904;
Image? buck24Image;

test('luminanceAlpha', () async {
final png = (await decodePngFile('test/_data/png/png_LA.png'))!;
expect(png.numChannels, equals(2));
final rgba = png.convert(numChannels: 4);
await encodePngFile('$testOutputPath/png/png_LA_rgba.png', rgba);
});

test('hungry_180', () async {
final png = (await decodePngFile('test/_data/png/hungry_180.png'))!;
flip(png, direction: FlipDirection.horizontal);
Expand Down

0 comments on commit 9f39c25

Please sign in to comment.