From: Ray Johnston <ray.johnston@artifex.com> Date: Fri, 1 Nov 2019 18:55:23 +0000 (-0700) Subject: Fix bugs 701787, 701806, 701810. Problems with cdj970 device. X-Git-Tag: ghostpdl-9.51rc1~367 Origin: https://git.ghostscript.com/?p=ghostpdl.git;a=commitdiff_plain;h=4f73e8b4d578e69a17f452fa60d2130c5faaefd6 Last-Update: 2020-08-19 Reviewed-by: Sylvain Beucler <beuc@debian.org> Fix bugs 701787, 701806, 701810. Problems with cdj970 device. As Robin mentioned in bug 701787, the device was changing resolution AFTER the device had been opened and all of the buffers configured. Move the "one time" initial setup into the open function, leaving the code to write the header in the print_page function. Presumably that only needs to be written once even if there are multiple pages. Also add a check for valid resolutions since it appears that the intent was to have the "Quality" parameter set up 300 or 600 dpi. Other deskjet devices have this type of check. Add a gs_closedevice if the Quality is changed since this will change the resolution and thus the page buffer geometry. Lastly, fix cdj970_put_params so that errors are not ignored for all but the last (which happened to be BlackCorrect). These changes prevent the bugs cited, but remain untested except for some parameter testing to make sure bad values don't cause memory violations. It does seem that some parameter values that are out of range (like Quality) are ignored, but that may be intentional. --- Index: ghostscript-9.26a~dfsg/contrib/gdevdj9.c =================================================================== --- ghostscript-9.26a~dfsg.orig/contrib/gdevdj9.c +++ ghostscript-9.26a~dfsg/contrib/gdevdj9.c @@ -575,26 +575,55 @@ static int cdj_set_bpp(gx_device *, int, static int hp_colour_open(gx_device * pdev) { - int retCode; + int retCode = 0; + + /* Change the margins if necessary. */ + static const float dj_a4[4] = { + DESKJET_MARGINS_A4 + }; + + static const float dj_letter[4] = { + DESKJET_MARGINS_LETTER + }; + const float *m = (float *)0; cdj970->PageCtr = 0; + /* quality setup */ + if (cdj970->quality == DRAFT) { + gx_device_set_resolution((gx_device *) pdev, 300.0, 300.0); + cdj970->xscal = 0; + cdj970->yscal = 0; + cdj970->intensities = 2; + } else if (cdj970->quality == NORMAL) { + gx_device_set_resolution((gx_device *) pdev, 600.0, 600.0); + cdj970->xscal = 1; + cdj970->yscal = 1; + /* intensities = 4 from initialization */ + } else { /* quality == PRESENTATION */ + gx_device_set_resolution((gx_device *) pdev, 600.0, 600.0); + cdj970->xscal = 0; + cdj970->yscal = 0; + /* intensities = 4 from initialization */ + } + + m = (gdev_pcl_paper_size((gx_device *) pdev) == + PAPER_SIZE_A4 ? dj_a4 : dj_letter); + + gx_device_set_margins((gx_device *) pdev, m, true); + /* Set up colour params if put_params has not already done so */ if (pdev->color_info.num_components == 0) { - int code = cdj_set_bpp(pdev, pdev->color_info.depth, + retCode = cdj_set_bpp(pdev, pdev->color_info.depth, pdev->color_info.num_components); - if (code < 0) - return code; + if (retCode < 0) + return retCode; } retCode = gdev_prn_open(pdev); - if (retCode < 0) - return (retCode); - else { + if (retCode >= 0) { retCode = gdev_prn_open_printer(pdev, true); - if (retCode < 0) - return (retCode); } return 0; @@ -648,26 +677,25 @@ cdj970_put_params(gx_device * pdev, gs_p int bpp = 0; int code = 0; - code = cdj_put_param_int(plist, "BitsPerPixel", &bpp, 1, 32, code); - code = cdj_put_param_int(plist, "Quality", &quality, 0, 2, code); - code = cdj_put_param_int(plist, "Papertype", &papertype, 0, 4, code); - code = cdj_put_param_int(plist, "Duplex", &duplex, 0, 2, code); - code = - cdj_put_param_float(plist, "MasterGamma", &mastergamma, 0.1, 9.0, - code); - code = - cdj_put_param_float(plist, "GammaValC", &gammavalc, 0.0, 9.0, code); - code = - cdj_put_param_float(plist, "GammaValM", &gammavalm, 0.0, 9.0, code); - code = - cdj_put_param_float(plist, "GammaValY", &gammavaly, 0.0, 9.0, code); - code = - cdj_put_param_float(plist, "GammaValK", &gammavalk, 0.0, 9.0, code); - code = - cdj_put_param_float(plist, "BlackCorrect", &blackcorrect, 0.0, 9.0, - code); - - if (code < 0) + if ((code = cdj_put_param_int(plist, "BitsPerPixel", &bpp, 1, 32, code)) < 0) + return code; + if ((code = cdj_put_param_int(plist, "Quality", &quality, 0, 2, code)) < 0) + return code; + if ((code = cdj_put_param_int(plist, "Papertype", &papertype, 0, 4, code)) < 0) + return code; + if ((code = cdj_put_param_int(plist, "Duplex", &duplex, 0, 2, code)) < 0) + return code; + if ((code = cdj_put_param_float(plist, "MasterGamma", &mastergamma, 0.1, 9.0, code)) < 0) + return code; + if ((code = cdj_put_param_float(plist, "GammaValC", &gammavalc, 0.0, 9.0, code)) < 0) + return code; + if ((code = cdj_put_param_float(plist, "GammaValM", &gammavalm, 0.0, 9.0, code)) < 0) + return code; + if ((code = cdj_put_param_float(plist, "GammaValY", &gammavaly, 0.0, 9.0, code)) < 0) + return code; + if ((code = cdj_put_param_float(plist, "GammaValK", &gammavalk, 0.0, 9.0, code)) < 0) + return code; + if ((code = cdj_put_param_float(plist, "BlackCorrect", &blackcorrect, 0.0, 9.0, code)) < 0) return code; code = cdj_put_param_bpp(pdev, plist, bpp, bpp, 0); @@ -675,7 +703,11 @@ cdj970_put_params(gx_device * pdev, gs_p if (code < 0) return code; - cdj970->quality = quality; + if (cdj970->quality != quality) { + if (pdev->is_open) + gs_closedevice(pdev); /* quality can change resolution, force re-open */ + cdj970->quality = quality; + } cdj970->papertype = papertype; cdj970->duplex = duplex; cdj970->mastergamma = mastergamma; @@ -685,7 +717,7 @@ cdj970_put_params(gx_device * pdev, gs_p cdj970->gammavalk = gammavalk; cdj970->blackcorrect = blackcorrect; - return 0; + return code; } /**********************************************************************************/ @@ -784,47 +816,6 @@ cdj970_terminate_page(gx_device_printer fputs("\033*rC\f\033&l-2H", prn_stream); /* End Graphics, Reset */ } -/* cdj970_one_time_initialisation: -----------------------------------------------------------------------------------*/ -static void -cdj970_one_time_initialisation(gx_device_printer * pdev) -{ - /* Change the margins if necessary. */ - static const float dj_a4[4] = { - DESKJET_MARGINS_A4 - }; - - static const float dj_letter[4] = { - DESKJET_MARGINS_LETTER - }; - const float *m = (float *)0; - - /* quality setup */ - if (cdj970->quality == DRAFT) { - gx_device_set_resolution((gx_device *) pdev, 300.0, 300.0); - cdj970->xscal = 0; - cdj970->yscal = 0; - cdj970->intensities = 2; - } else if (cdj970->quality == NORMAL) { - gx_device_set_resolution((gx_device *) pdev, 600.0, 600.0); - cdj970->xscal = 1; - cdj970->yscal = 1; - /* intensities = 4 from initialization */ - } else { /* quality == PRESENTATION */ - gx_device_set_resolution((gx_device *) pdev, 600.0, 600.0); - cdj970->xscal = 0; - cdj970->yscal = 0; - /* intensities = 4 from initialization */ - } - - m = (gdev_pcl_paper_size((gx_device *) pdev) == - PAPER_SIZE_A4 ? dj_a4 : dj_letter); - - gx_device_set_margins((gx_device *) pdev, m, true); - - cdj970_write_header((gx_device *) pdev, pdev->file); -} - /* cdj970_print_page: Here comes the hp970 output routine ----------------------------------------------------------------------------------*/ static int @@ -837,7 +828,7 @@ cdj970_print_page(gx_device_printer * pd Gamma gamma; if (cdj970->PageCtr == 0 && cdj970->ptype == DJ970C) { - cdj970_one_time_initialisation(pdev); + cdj970_write_header((gx_device *)pdev, prn_stream); } /* make a local writable copy of the Gamma tables */ @@ -2280,6 +2271,10 @@ cdj_set_bpp(gx_device * pdev, int bpp, i ci->dither_colors = (bpp >= 8 ? 5 : bpp > 1 ? 2 : 0); } + if (ci->depth != ((bpp > 1) && (bpp < 8) ? 8 : bpp)) { + if (pdev->is_open) + gs_closedevice(pdev); /* depth changed, make sure we re-open */ + } ci->depth = ((bpp > 1) && (bpp < 8) ? 8 : bpp); return (0); @@ -2598,16 +2593,16 @@ cdj_put_param_bpp(gx_device * pdev, gs_param_list * plist, int new_bpp, int real_bpp, int ccomps) { - if (new_bpp == 0 && ccomps == 0) - return gdev_prn_put_params(pdev, plist); - else { - gx_device_color_info save_info; - int save_bpp; - int code; + int code = 0; + int save_bpp; + gx_device_color_info save_info; - save_info = pdev->color_info; - save_bpp = save_info.depth; + save_info = pdev->color_info; + save_bpp = save_info.depth; + if (new_bpp == 0 && ccomps == 0) { + code = gdev_prn_put_params(pdev, plist); + } else { if (save_bpp == 8 && save_ccomps == 3 && !cprn_device->cmyk) save_bpp = 3; @@ -2631,12 +2626,21 @@ cdj_put_param_bpp(gx_device * pdev, if ((cdj970->color_info.depth != save_bpp || (ccomps != 0 && ccomps != save_ccomps)) && pdev->is_open) - return (gs_closedevice(pdev)); + gs_closedevice(pdev); + } - return (0); + /* check for valid resolutions */ + if (pdev->HWResolution[0] != pdev->HWResolution[1] || + (pdev->HWResolution[0] != 300.0 && pdev->HWResolution[0] != 600.0) ) { + param_signal_error(plist, "HWResolution", gs_error_rangecheck); + emprintf1(pdev->memory, "\ncdj970: Invalid resolution: '%f'. Only 300 or 600 supported.\n\n", + pdev->HWResolution[0]); + cdj_set_bpp(pdev, save_bpp, save_ccomps); + return gs_error_rangecheck; + } + return code; #undef save_ccomps - } } /* cdj970_write_header: