ensure alpha channels are initialized for mediancut make_colors
authorTony Cook <tony@develop-help.com>
Fri, 27 Mar 2020 06:32:30 +0000 (17:32 +1100)
committerTony Cook <tony@develop-help.com>
Fri, 27 Mar 2020 06:32:30 +0000 (17:32 +1100)
This could result in a 4 channel paletted image with some random
alpha channel.

CPAN #132237

quant.c
t/150-type/040-palette.t

diff --git a/quant.c b/quant.c
index c798080c5b70be2a254e11c341242cc7d6a472fb..419b81b76b52388f9e2b9f66cf13ab521393b2fb 100644 (file)
--- a/quant.c
+++ b/quant.c
@@ -633,6 +633,7 @@ makemap_mediancut(i_quantize *quant, i_img **imgs, int count) {
       for (ch = 0; ch < 3; ++ch) {
         quant->mc_colors[i].channel[ch] = colors[i].rgb[ch];
       }
+      quant->mc_colors[i].rgba.a = 255;
     }
     quant->mc_count = out;
   }
@@ -720,6 +721,7 @@ makemap_mediancut(i_quantize *quant, i_img **imgs, int count) {
       for (ch = 0; ch < 3; ++ch) {
         quant->mc_colors[part_num].channel[ch] = sums[ch] / workpart->pixels;
       }
+      quant->mc_colors[part_num].rgba.a = 255;
     }
     quant->mc_count = color_count;
   }
index 00d8935b15db777560697c61c3eff9a074c931ce..2d50dbbba78b10de21233ee5b0ec09d98c23aab1 100644 (file)
@@ -649,6 +649,16 @@ my $psamp_outside_error = "Image position outside of image";
      "check error message");
 }
 
+{
+  # https://rt.cpan.org/Ticket/Display.html?id=132237
+  # a 4 channel opaque image with a default to_paletted would
+  # return palette values with non-max alpha values
+  my $im = test_image()->convert(preset => 'addalpha')->to_paletted;
+  my @col = $im->getcolors;
+  is($im->getchannels, 4, "still 4 channels");
+  is($col[0]->alpha, 255, "should have a 255 alpha");
+}
+
 Imager->close_log;
 
 done_testing();