Skip to content

Commit

Permalink
don't allocate an array for CmykToRgb
Browse files Browse the repository at this point in the history
  • Loading branch information
brendan-duncan committed Jan 8, 2025
1 parent 6a4234d commit 3f8ed8d
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
4 changes: 3 additions & 1 deletion lib/src/formats/psd/psd_image.dart
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,8 @@ class PsdImage implements DecodeInfo {
final channel2 = channels[2];
final channel_1 = channels[-1];

final rgb = [0, 0, 0];

var si = -ns;
for (final p in output) {
si += ns;
Expand Down Expand Up @@ -594,7 +596,7 @@ class PsdImage implements DecodeInfo {
final y = _ch(channel2!.data, si, ns);
final k = _ch(channels[numChannels == 4 ? -1 : 3]!.data, si, ns);
final alpha = numChannels >= 5 ? _ch(channel_1!.data, si, ns) : 255;
final rgb = cmykToRgb(255 - c, 255 - m, 255 - y, 255 - k);
cmykToRgb(255 - c, 255 - m, 255 - y, 255 - k, rgb);
p.r = rgb[0];
p.g = rgb[1];
p.b = rgb[2];
Expand Down
10 changes: 6 additions & 4 deletions lib/src/formats/tiff/tiff_image.dart
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,8 @@ class TiffImage {
throw ImageException('Unsupported Compression Type: $compression');
}

final rgb = [0, 0, 0];

for (var y = 0, py = outY; y < tileHeight; ++y, ++py) {
for (var x = 0, px = outX; x < tileWidth; ++x, ++px) {
if (byteData.isEOS) {
Expand Down Expand Up @@ -588,10 +590,10 @@ class TiffImage {
}

if (photometricType == TiffPhotometricType.cmyk) {
final rgba = cmykToRgb(r, g, b, a);
r = rgba[0];
g = rgba[1];
b = rgba[2];
cmykToRgb(r, g, b, a, rgb);
r = rgb[0];
g = rgb[1];
b = rgb[2];
a = image.maxChannelValue as int;
}

Expand Down
10 changes: 4 additions & 6 deletions lib/src/util/color_util.dart
Original file line number Diff line number Diff line change
Expand Up @@ -321,16 +321,14 @@ List<int> xyzToRgb(num x, num y, num z) {

/// Convert a CMYK color to RGB, where c, m, y, k values are in the range
/// \[0, 255\]. Returns a list \[r, g, b\] with values in the range \[0, 255\].
List<int> cmykToRgb(num c, num m, num y, num k) {
void cmykToRgb(num c, num m, num y, num k, List<int> rgb) {
c /= 255.0;
m /= 255.0;
y /= 255.0;
k /= 255.0;
return [
(255.0 * (1.0 - c) * (1.0 - k)).round(),
(255.0 * (1.0 - m) * (1.0 - k)).round(),
(255.0 * (1.0 - y) * (1.0 - k)).round()
];
rgb[0] = (255.0 * (1.0 - c) * (1.0 - k)).round();
rgb[1] = (255.0 * (1.0 - m) * (1.0 - k)).round();
rgb[2] = (255.0 * (1.0 - y) * (1.0 - k)).round();
}

/// Convert a CIE-L*ab color to RGB.
Expand Down

0 comments on commit 3f8ed8d

Please sign in to comment.