static int
write_direct8(png_structp png_ptr, png_infop info_ptr, i_img *im);
+static int
+write_direct16(png_structp png_ptr, png_infop info_ptr, i_img *im);
+
static int
write_paletted(png_structp png_ptr, png_infop info_ptr, i_img *im, int bits);
fprintf(stderr, "Internal error, channels = %d\n", channels);
abort();
}
- bits = 8;
+ bits = im->bits > 8 ? 16 : 8;
mm_log((1, "i_writepng: direct output\n"));
}
return 0;
}
}
- else {
+ else if (bits == 16) {
+ if (!write_direct16(png_ptr, info_ptr, im)) {
+ png_destroy_write_struct(&png_ptr, &info_ptr);
+ return 0;
+ }
+ }
+ else {
if (!write_direct8(png_ptr, info_ptr, im)) {
png_destroy_write_struct(&png_ptr, &info_ptr);
return 0;
return 1;
}
+static int
+write_direct16(png_structp png_ptr, png_infop info_ptr, i_img *im) {
+ unsigned *data, *volatile vdata = NULL;
+ unsigned char *tran_data, * volatile vtran_data = NULL;
+ i_img_dim samples_per_row = im->xsize * im->channels;
+
+ i_img_dim y;
+
+ if (setjmp(png_jmpbuf(png_ptr))) {
+ if (vdata)
+ myfree(vdata);
+ if (vtran_data)
+ myfree(vtran_data);
+
+ return 0;
+ }
+
+ png_write_info(png_ptr, info_ptr);
+
+ vdata = data = mymalloc(samples_per_row * sizeof(unsigned));
+ vtran_data = tran_data = mymalloc(samples_per_row * 2);
+ for (y = 0; y < im->ysize; y++) {
+ i_img_dim i;
+ unsigned char *p = tran_data;
+ i_gsamp_bits(im, 0, im->xsize, y, data, NULL, im->channels, 16);
+ for (i = 0; i < samples_per_row; ++i) {
+ p[0] = data[i] >> 8;
+ p[1] = data[i] & 0xff;
+ p += 2;
+ }
+ png_write_row(png_ptr, (png_bytep)tran_data);
+ }
+ myfree(tran_data);
+ myfree(data);
+
+ return 1;
+}
+
static int
write_paletted(png_structp png_ptr, png_infop info_ptr, i_img *im, int bits) {
unsigned char *data, *volatile vdata = NULL;
use strict;
use Imager qw(:all);
use Test::More;
-use Imager::Test qw(test_image_raw test_image is_image);
+use Imager::Test qw(test_image_raw test_image is_image is_imaged test_image_16 test_image_double);
my $debug_writes = 1;
init_log("testout/t102png.log",1);
-plan tests => 192;
+plan tests => 204;
# this loads Imager::File::PNG too
ok($Imager::formats{"png"}, "must have png format");
is($in->tags(name => "png_bits"), 1, "1 bit representation");
}
+SKIP:
+{
+ my $im = test_image_16();
+ ok($im->write(file => "testout/rgb16.png", type => "png"),
+ "write 16-bit/sample image")
+ or diag("Could not write rgb16.png: ".$im->errstr);
+ my $in = Imager->new(file => "testout/rgb16.png")
+ or diag("Could not read rgb16.png: ".Imager->errstr);
+ ok($in, "read rgb16.png back in")
+ or skip("Could not load image to check", 4);
+ is_imaged($in, $im, 0, "check image matches");
+ is($in->bits, 16, "check we got a 16-bit image");
+ is($in->type, "direct", "check it's direct");
+ is($in->tags(name => "png_bits"), 16, "check png_bits");
+}
+
+SKIP:
+{
+ my $im = test_image_double();
+ my $cmp = $im->to_rgb16;
+ ok($im->write(file => "testout/rgbdbl.png", type => "png"),
+ "write double/sample image - should write as 16-bit/sample")
+ or diag("Could not write rgbdbl.png: ".$im->errstr);
+ my $in = Imager->new(file => "testout/rgbdbl.png")
+ or diag("Could not read rgbdbl.png: ".Imager->errstr);
+ ok($in, "read pngdbl.png back in")
+ or skip("Could not load image to check", 4);
+ is_imaged($in, $cmp, 0, "check image matches");
+ is($in->bits, 16, "check we got a 16-bit image");
+ is($in->type, "direct", "check it's direct");
+ is($in->tags(name => "png_bits"), 16, "check png_bits");
+}
+
sub limited_write {
my ($limit) = @_;