From dbc90f93740f2ef844594c0f9356ef35cf7e4d8c Mon Sep 17 00:00:00 2001 From: Thomas Bernard Date: Wed, 18 Mar 2020 01:37:54 +0100 Subject: [PATCH 1/2] tiff2pdf: avoid divide by 0 fixes #133 http://bugzilla.maptools.org/show_bug.cgi?id=2796 --- tools/tiff2pdf.c | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/tools/tiff2pdf.c b/tools/tiff2pdf.c index bca54d53..9c57e0a0 100644 --- a/tools/tiff2pdf.c +++ b/tools/tiff2pdf.c @@ -4993,9 +4993,11 @@ tsize_t t2p_write_pdf_xobject_cs(T2P* t2p, TIFF* output){ X_W = t2p->tiff_whitechromaticities[0]; Y_W = t2p->tiff_whitechromaticities[1]; Z_W = 1.0F - (X_W + Y_W); - X_W /= Y_W; - Z_W /= Y_W; - Y_W = 1.0F; + if (Y_W != 0.0F) { + X_W /= Y_W; + Z_W /= Y_W; + Y_W = 1.0F; + } buflen=snprintf(buffer, sizeof(buffer), "[%.4f %.4f %.4f] \n", X_W, Y_W, Z_W); check_snprintf_ret(t2p, buflen, buffer); written += t2pWriteFile(output, (tdata_t) buffer, buflen); @@ -5124,9 +5126,11 @@ tsize_t t2p_write_pdf_xobject_calcs(T2P* t2p, TIFF* output){ X_W = t2p->tiff_whitechromaticities[0]; Y_W = t2p->tiff_whitechromaticities[1]; Z_W = 1.0F - (X_W + Y_W); - X_W /= Y_W; - Z_W /= Y_W; - Y_W = 1.0F; + if (Y_W != 0.0F) { + X_W /= Y_W; + Z_W /= Y_W; + Y_W = 1.0F; + } } if(t2p->pdf_colorspace & T2P_CS_CALRGB){ written += t2pWriteFile(output, (tdata_t) "/CalRGB ", 8); @@ -5151,9 +5155,11 @@ tsize_t t2p_write_pdf_xobject_calcs(T2P* t2p, TIFF* output){ X_W = (X_R * R) + (X_G * G) + (X_B * B); Y_W = (Y_R * R) + (Y_G * G) + (Y_B * B); Z_W = (Z_R * R) + (Z_G * G) + (Z_B * B); - X_W /= Y_W; - Z_W /= Y_W; - Y_W = 1.0; + if (Y_W != 0.0F) { + X_W /= Y_W; + Z_W /= Y_W; + Y_W = 1.0; + } } written += t2pWriteFile(output, (tdata_t) "<< \n", 4); if(t2p->pdf_colorspace & T2P_CS_CALGRAY){ From bd49c5810f0098b4e29ca6d10578810f99d394cd Mon Sep 17 00:00:00 2001 From: Thomas Bernard Date: Tue, 24 Mar 2020 11:34:36 +0100 Subject: [PATCH 2/2] tiff2pdf: normalizePoint() macro to normalize the white point --- tools/tiff2pdf.c | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/tools/tiff2pdf.c b/tools/tiff2pdf.c index 9c57e0a0..94b3d01e 100644 --- a/tools/tiff2pdf.c +++ b/tools/tiff2pdf.c @@ -4933,6 +4933,16 @@ tsize_t t2p_write_pdf_xobject_stream_dict(ttile_t tile, return(written); } + +/* used to normalize the White Point */ +#define normalizePoint(x,y,z) do { \ + if (y != 0.0F) { \ + x /= y; \ + z /= y; \ + y = 1.0F; \ + } \ +} while(0) + /* * This function writes a PDF Image XObject Colorspace name to output. */ @@ -4993,11 +5003,7 @@ tsize_t t2p_write_pdf_xobject_cs(T2P* t2p, TIFF* output){ X_W = t2p->tiff_whitechromaticities[0]; Y_W = t2p->tiff_whitechromaticities[1]; Z_W = 1.0F - (X_W + Y_W); - if (Y_W != 0.0F) { - X_W /= Y_W; - Z_W /= Y_W; - Y_W = 1.0F; - } + normalizePoint(X_W, Y_W, Z_W); buflen=snprintf(buffer, sizeof(buffer), "[%.4f %.4f %.4f] \n", X_W, Y_W, Z_W); check_snprintf_ret(t2p, buflen, buffer); written += t2pWriteFile(output, (tdata_t) buffer, buflen); @@ -5126,11 +5132,7 @@ tsize_t t2p_write_pdf_xobject_calcs(T2P* t2p, TIFF* output){ X_W = t2p->tiff_whitechromaticities[0]; Y_W = t2p->tiff_whitechromaticities[1]; Z_W = 1.0F - (X_W + Y_W); - if (Y_W != 0.0F) { - X_W /= Y_W; - Z_W /= Y_W; - Y_W = 1.0F; - } + normalizePoint(X_W, Y_W, Z_W); } if(t2p->pdf_colorspace & T2P_CS_CALRGB){ written += t2pWriteFile(output, (tdata_t) "/CalRGB ", 8); @@ -5155,11 +5157,7 @@ tsize_t t2p_write_pdf_xobject_calcs(T2P* t2p, TIFF* output){ X_W = (X_R * R) + (X_G * G) + (X_B * B); Y_W = (Y_R * R) + (Y_G * G) + (Y_B * B); Z_W = (Z_R * R) + (Z_G * G) + (Z_B * B); - if (Y_W != 0.0F) { - X_W /= Y_W; - Z_W /= Y_W; - Y_W = 1.0; - } + normalizePoint(X_W, Y_W, Z_W); } written += t2pWriteFile(output, (tdata_t) "<< \n", 4); if(t2p->pdf_colorspace & T2P_CS_CALGRAY){