From 2cc4789cf67fe8aab66f3e2ef787d571d0b3b4e5 Mon Sep 17 00:00:00 2001 From: Michael R Sweet Date: Sun, 18 Jul 2021 09:50:22 -0400 Subject: [PATCH] Fix image writing issues for pdfioFileCreateImageObjFromData (Issue #15) --- pdfio-content.c | 6 ++++-- testpdfio.c | 23 ++++++++++++++++------- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/pdfio-content.c b/pdfio-content.c index e7c5903..22095b5 100644 --- a/pdfio-content.c +++ b/pdfio-content.c @@ -1694,7 +1694,8 @@ pdfioFileCreateImageObjFromData( pdfioDictSetNumber(dict, "Width", width); pdfioDictSetNumber(dict, "Height", height); pdfioDictSetNumber(dict, "BitsPerComponent", 8); - pdfioDictSetBoolean(dict, "ImageMask", true); + pdfioDictSetName(dict, "ColorSpace", "DeviceGray"); + pdfioDictSetName(dict, "Filter", "FlateDecode"); if ((decode = pdfioDictCreate(pdf)) == NULL) { @@ -1703,7 +1704,7 @@ pdfioFileCreateImageObjFromData( } pdfioDictSetNumber(decode, "BitsPerComponent", 8); - pdfioDictSetNumber(decode, "Colors", num_colors - 1); + pdfioDictSetNumber(decode, "Colors", 1); pdfioDictSetNumber(decode, "Columns", width); pdfioDictSetNumber(decode, "Predictor", _PDFIO_PREDICTOR_PNG_AUTO); pdfioDictSetDict(dict, "DecodeParms", decode); @@ -1746,6 +1747,7 @@ pdfioFileCreateImageObjFromData( pdfioDictSetNumber(dict, "Width", width); pdfioDictSetNumber(dict, "Height", height); pdfioDictSetNumber(dict, "BitsPerComponent", 8); + pdfioDictSetName(dict, "Filter", "FlateDecode"); if (color_data) pdfioDictSetArray(dict, "ColorSpace", color_data); diff --git a/testpdfio.c b/testpdfio.c index ba799b0..8f8b4e8 100644 --- a/testpdfio.c +++ b/testpdfio.c @@ -772,7 +772,10 @@ write_alpha_test( if (i > 2) { // Add alpha channel - *bufptr++ = (unsigned char)((x + y) / 2); + if (x < 112 || x >= 144 || y < 112 || y >= 144) + *bufptr++ = (unsigned char)((x - 128) * (y - 128)); + else + *bufptr++ = 0; } } } @@ -792,7 +795,10 @@ write_alpha_test( if (i > 2) { // Add alpha channel - *bufptr++ = (unsigned char)((x + y) / 2); + if (x < 112 || x >= 144 || y < 112 || y >= 144) + *bufptr++ = (unsigned char)((x - 128) * (y - 128)); + else + *bufptr++ = 0; } } } @@ -817,7 +823,10 @@ write_alpha_test( if (i > 2) { // Add alpha channel - *bufptr++ = (unsigned char)((x + y) / 2); + if (x < 112 || x >= 144 || y < 112 || y >= 144) + *bufptr++ = (unsigned char)((x - 128) * (y - 128)); + else + *bufptr++ = 0; } } } @@ -828,7 +837,7 @@ write_alpha_test( printf("pdfioFileCreateImageObjFromData(num_colors=%u, alpha=%s): ", (unsigned)num_colors, i > 2 ? "true" : "false"); if ((images[i] = pdfioFileCreateImageObjFromData(pdf, buffer, 256, 256, num_colors, NULL, i > 2, false)) != NULL) { - puts("PASS"); + printf("PASS (%u)\n", (unsigned)pdfioObjGetNumber(images[i])); } else { @@ -877,9 +886,9 @@ write_alpha_test( "DeviceGray", "DeviceRGB", "DeviceCMYK", - "DeviceGray + Alpha", - "DeviceRGB + Alpha", - "DeviceCMYK + Alpha" + "DevGray + Alpha", + "DevRGB + Alpha", + "DevCMYK + Alpha" }; snprintf(iname, sizeof(iname), "IM%d", i + 1);