diff options
author | Ken Sharp <ken.sharp@artifex.com> | 2023-02-28 15:36:56 +0000 |
---|---|---|
committer | Ken Sharp <ken.sharp@artifex.com> | 2023-02-28 15:39:09 +0000 |
commit | 6894a2826210baf24f9ccd024bbca211b17d1f9b (patch) | |
tree | eeb4b964e285b4173f0170a6cee990c737ed6058 /pdf | |
parent | 1a722c886c4d3fbd9931b627a17270aca9701606 (diff) | |
download | ghostpdl-6894a2826210baf24f9ccd024bbca211b17d1f9b.tar.gz |
GhostPDF + pdfwrite - emit an Alternate for some ICCBased spaces.
Bug #705865 "PDF Writer is dropping the /Alternate color space for ICC based color profile"
This commit starts off with the bug referenced above. Prior to this
commit the PDF interpreter never set the /Alternate space for an
ICCBased colour space. For rendering there is no need since Ghostscript
will always use the ICC profile, or will set the /Alternate instead
(if there is an error in the ICC profile) or will use the number of
components (/N) to set a Device space if there is a fault in the ICC
profile and no /Alternate.
However, this means pdfwrite never sees an Alternate space to write out
for an ICCBased space. This should not be a problem since the
/Alternate is optional for an ICCBased space and indeed the PDF 2.0
specification states "PDF readers should not use this colour space".
The file for the bug has a /ICCBased space where the /Alternate is
an Lab space. Obviously any PDF consumer should use the ICCBased space
but it seems that Chrome, Firefox, possibly other browsers cannot handle
ICCBased colour and so drop back to the Alternate. Surprisingly they
can handle Lab and get the expected colour. Obviously if we drop the
/Alternate then these consumers cannot use Lab and have a last ditch
fallback to RGB (based on the number of components, and that *is* in the
spec). But RGB != Lab and so the colours are incorrect.
Ideally we would simply use the existing colour space code and attach
the alternate space to the ICCBased space's 'base_space' member. That
would write everything perfectly well. But we can't do that because
when we are called from Ghostscript the ICC profile cache is in GC'ed
memory. So we have to create the ICCBased colour space in GC'ed memory
too. We have special hackery in the PDF interpreter colour code to do
exactly that.
Colour spaces call back to the PDF interpreter when freed (with more
hackery for ICCBased spaces), but if we create colour spaces in non-GC
(PDF interpreter) memory and attach them to the ICCBased space in GC'ed
memory then they can outlive the PDF interpreter, leading to crashes.
I did start down the road of making all colour spaces in GC-ed memory,
but that rapidly spiralled out of control because names needed to be
allocated in GC'ed memory, and functions and well, all kinds of things.
Without that we got crashes, and it quickly became clear the only real
way to make this work would be to allocate everything in GC'ed memory
which we really don't want to do.
So instead I added a new enumerated type member to the colour space, in
that member, if the current colour space is ICCBased, we store the type
of Alternate that was supplied (if any). We only support DeviceGray,
DeviceRGB, DeviceCMYK, CalGray, CalRGB and Lab. I also added the
relevant parameters to the 'params' union of the colour space.
In the PDF interpreter; add code to spot the afore-mentioned Alternate
spaces if present, and if we haven't been forced to fall back to using
the Alternate (or /N) because the ICC profile is broken. When we spot
one of those spaces, set the colour space ICC_Alternate_space member
appropriately and for the paramaterised spaces gather the parameter
values and store them.
In the pdfwrite device; if we are writing out an ICCBased space, and
it's ICC_Alternate_space member is not gs_ICC_Alternate_none, create
a ColorSpace resource and call the relevant space-specific code to
create a colour space array with a name and dictionary containing the
required parameters. Attach the resulting object to the ICCBased
colour space by inserting it into the array with a /Alternate key.
This also meant I needed to alter the parameters passed internally to
pdf_make_iccbased so that we pass in the original colour space instead
of the alternate space (which is always NULL currently).
There are also a couple of fixes; when finalising a colour space check
that the colour space is a DeviceN space before checking if the device_n
structure in the params union has a non-zero devn_process_space. The new
members in the union meant we could get here and think we needed to
free the devn_process_space, causing a crash.
In the PDF interpreter; there's a little clause in the PDF specification
which mentions a CalCMYK space. Apparently this was never properly
specified and so should be treated as DeviceCMYK. The PDF interpreter
now does so.
Finally another obsrvation; the initial code wrote the /Alternate space
as a named colour space, eg:
19 0 obj
<</N 3
/Alternate /R18 /Length 1972>>stream
....
Where R18 is defined in the Page's ColorSpace Resources as a named
resource:
<</R18
18 0 R/R17
17 0 R/R20
20 0 R/R22
22 0 R>>
endobj
But this does not work with Chrome (I didn't test Firefox). For this to
work with Chrome we have to reference the object directly, which should
not be required IMO. I believe this to be (another) bug in Chrome's PDF
handling.
Diffstat (limited to 'pdf')
-rw-r--r-- | pdf/pdf_colour.c | 343 |
1 files changed, 339 insertions, 4 deletions
diff --git a/pdf/pdf_colour.c b/pdf/pdf_colour.c index 9d1a7b36a..f47313d02 100644 --- a/pdf/pdf_colour.c +++ b/pdf/pdf_colour.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2018-2022 Artifex Software, Inc. +/* Copyright (C) 2018-2023 Artifex Software, Inc. All Rights Reserved. This software is provided AS-IS with no warranty, either express or @@ -153,6 +153,8 @@ static int pdfi_check_for_spots_by_array(pdf_context *ctx, pdf_array *color_arra goto exit; } else if (pdfi_name_is(space, "CalGray")) { goto exit; + } else if (pdfi_name_is(space, "CalCMYK")) { + goto exit; } else if (pdfi_name_is(space, "ICCBased")) { goto exit; } else if (pdfi_name_is(space, "DeviceRGB")) { @@ -1092,13 +1094,286 @@ static int pdfi_create_iccprofile(pdf_context *ctx, pdf_stream *ICC_obj, char *c return code; } +static int pdfi_set_CalGray_params(pdf_context *ctx, gs_color_space *pcs, pdf_dict *ParamsDict) +{ + int code = 0, i; + double f; + /* The default values here are as per the PDF 1.7 specification, there is + * no default for the WhitePoint as it is a required entry. + */ + float WhitePoint[3], BlackPoint[3] = {0.0f, 0.0f, 0.0f}, Gamma = 1.0f; + pdf_array *PDFArray = NULL; + + code = pdfi_dict_get_type(ctx, ParamsDict, "WhitePoint", PDF_ARRAY, (pdf_obj **)&PDFArray); + if (code < 0) { + pdfi_countdown(PDFArray); + goto exit; + } + if (pdfi_array_size(PDFArray) != 3){ + code = gs_note_error(gs_error_rangecheck); + goto exit; + } + + for (i=0; i < 3; i++) { + code = pdfi_array_get_number(ctx, PDFArray, (uint64_t)i, &f); + if (code < 0) + goto exit; + WhitePoint[i] = (float)f; + } + pdfi_countdown(PDFArray); + PDFArray = NULL; + + /* Check the WhitePoint values, the PDF 1.7 reference states that + * Xw ad Zw must be positive and Yw must be 1.0 + */ + if (WhitePoint[0] < 0 || WhitePoint[2] < 0 || WhitePoint[1] != 1.0f) { + code = gs_note_error(gs_error_rangecheck); + goto exit; + } + + if (pdfi_dict_knownget_type(ctx, ParamsDict, "BlackPoint", PDF_ARRAY, (pdf_obj **)&PDFArray) > 0) { + if (pdfi_array_size(PDFArray) != 3){ + code = gs_note_error(gs_error_rangecheck); + goto exit; + } + for (i=0; i < 3; i++) { + code = pdfi_array_get_number(ctx, PDFArray, (uint64_t)i, &f); + if (code < 0) + goto exit; + /* The PDF 1.7 reference states that all three components of the BlackPoint + * (if present) must be positive. + */ + if (f < 0) { + code = gs_note_error(gs_error_rangecheck); + goto exit; + } + BlackPoint[i] = (float)f; + } + pdfi_countdown(PDFArray); + PDFArray = NULL; + } + + if (pdfi_dict_knownget_number(ctx, ParamsDict, "Gamma", &f) > 0) + Gamma = (float)f; + /* The PDF 1.7 reference states that Gamma + * (if present) must be positive. + */ + if (Gamma < 0) { + code = gs_note_error(gs_error_rangecheck); + goto exit; + } + code = 0; + + for (i = 0;i < 3; i++) { + pcs->params.calgray.WhitePoint[i] = WhitePoint[i]; + pcs->params.calgray.BlackPoint[i] = BlackPoint[i]; + } + pcs->params.calgray.Gamma = Gamma; + +exit: + pdfi_countdown(PDFArray); + return code; +} + +static int pdfi_set_CalRGB_params(pdf_context *ctx, gs_color_space *pcs, pdf_dict *ParamsDict) +{ + int code = 0, i; + pdf_array *PDFArray = NULL; + /* The default values here are as per the PDF 1.7 specification, there is + * no default for the WhitePoint as it is a required entry + */ + float WhitePoint[3], BlackPoint[3] = {0.0f, 0.0f, 0.0f}, Gamma[3] = {1.0f, 1.0f, 1.0f}; + float Matrix[9] = {1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f}; + double f; + + code = pdfi_dict_get_type(ctx, ParamsDict, "WhitePoint", PDF_ARRAY, (pdf_obj **)&PDFArray); + if (code < 0) { + pdfi_countdown(PDFArray); + goto exit; + } + if (pdfi_array_size(PDFArray) != 3){ + code = gs_note_error(gs_error_rangecheck); + goto exit; + } + + for (i=0; i < 3; i++) { + code = pdfi_array_get_number(ctx, PDFArray, (uint64_t)i, &f); + if (code < 0) + goto exit; + WhitePoint[i] = (float)f; + } + pdfi_countdown(PDFArray); + PDFArray = NULL; + + /* Check the WhitePoint values, the PDF 1.7 reference states that + * Xw ad Zw must be positive and Yw must be 1.0 + */ + if (WhitePoint[0] < 0 || WhitePoint[2] < 0 || WhitePoint[1] != 1.0f) { + code = gs_note_error(gs_error_rangecheck); + goto exit; + } + + if (pdfi_dict_knownget_type(ctx, ParamsDict, "BlackPoint", PDF_ARRAY, (pdf_obj **)&PDFArray) > 0) { + if (pdfi_array_size(PDFArray) != 3){ + code = gs_note_error(gs_error_rangecheck); + goto exit; + } + for (i=0; i < 3; i++) { + code = pdfi_array_get_number(ctx, PDFArray, (uint64_t)i, &f); + if (code < 0) + goto exit; + /* The PDF 1.7 reference states that all three components of the BlackPoint + * (if present) must be positive. + */ + if (f < 0) { + code = gs_note_error(gs_error_rangecheck); + goto exit; + } + BlackPoint[i] = (float)f; + } + pdfi_countdown(PDFArray); + PDFArray = NULL; + } + + if (pdfi_dict_knownget_type(ctx, ParamsDict, "Gamma", PDF_ARRAY, (pdf_obj **)&PDFArray) > 0) { + if (pdfi_array_size(PDFArray) != 3){ + code = gs_note_error(gs_error_rangecheck); + goto exit; + } + for (i=0; i < 3; i++) { + code = pdfi_array_get_number(ctx, PDFArray, (uint64_t)i, &f); + if (code < 0) + goto exit; + Gamma[i] = (float)f; + } + pdfi_countdown(PDFArray); + PDFArray = NULL; + } + + if (pdfi_dict_knownget_type(ctx, ParamsDict, "Matrix", PDF_ARRAY, (pdf_obj **)&PDFArray) > 0) { + if (pdfi_array_size(PDFArray) != 9){ + code = gs_note_error(gs_error_rangecheck); + goto exit; + } + for (i=0; i < 9; i++) { + code = pdfi_array_get_number(ctx, PDFArray, (uint64_t)i, &f); + if (code < 0) + goto exit; + Matrix[i] = (float)f; + } + pdfi_countdown(PDFArray); + PDFArray = NULL; + } + code = 0; + + for (i = 0;i < 3; i++) { + pcs->params.calrgb.WhitePoint[i] = WhitePoint[i]; + pcs->params.calrgb.BlackPoint[i] = BlackPoint[i]; + pcs->params.calrgb.Gamma[i] = Gamma[i]; + } + for (i = 0;i < 9; i++) + pcs->params.calrgb.Matrix[i] = Matrix[i]; + +exit: + pdfi_countdown(PDFArray); + return code; +} + +static int pdfi_set_Lab_params(pdf_context *ctx, gs_color_space *pcs, pdf_dict *ParamsDict) +{ + int code = 0, i; + pdf_array *PDFArray = NULL; + /* The default values here are as per the PDF 1.7 specification, there is + * no default for the WhitePoint as it is a required entry + */ + float WhitePoint[3], BlackPoint[3] = {0.0f, 0.0f, 0.0f}, Range[4] = {-100.0, 100.0, -100.0, 100.0}; + double f; + + code = pdfi_dict_get_type(ctx, ParamsDict, "WhitePoint", PDF_ARRAY, (pdf_obj **)&PDFArray); + if (code < 0) { + pdfi_countdown(PDFArray); + goto exit; + } + if (pdfi_array_size(PDFArray) != 3){ + code = gs_note_error(gs_error_rangecheck); + goto exit; + } + + for (i=0; i < 3; i++) { + code = pdfi_array_get_number(ctx, PDFArray, (uint64_t)i, &f); + if (code < 0) + goto exit; + WhitePoint[i] = (float)f; + } + pdfi_countdown(PDFArray); + PDFArray = NULL; + + /* Check the WhitePoint values, the PDF 1.7 reference states that + * Xw ad Zw must be positive and Yw must be 1.0 + */ + if (WhitePoint[0] < 0 || WhitePoint[2] < 0 || WhitePoint[1] != 1.0f) { + code = gs_note_error(gs_error_rangecheck); + goto exit; + } + + if (pdfi_dict_knownget_type(ctx, ParamsDict, "BlackPoint", PDF_ARRAY, (pdf_obj **)&PDFArray) > 0) { + if (pdfi_array_size(PDFArray) != 3){ + code = gs_note_error(gs_error_rangecheck); + goto exit; + } + for (i=0; i < 3; i++) { + code = pdfi_array_get_number(ctx, PDFArray, (uint64_t)i, &f); + if (code < 0) + goto exit; + /* The PDF 1.7 reference states that all three components of the BlackPoint + * (if present) must be positive. + */ + if (f < 0) { + code = gs_note_error(gs_error_rangecheck); + goto exit; + } + BlackPoint[i] = (float)f; + } + pdfi_countdown(PDFArray); + PDFArray = NULL; + } + + if (pdfi_dict_knownget_type(ctx, ParamsDict, "Range", PDF_ARRAY, (pdf_obj **)&PDFArray) > 0) { + if (pdfi_array_size(PDFArray) != 4){ + code = gs_note_error(gs_error_rangecheck); + goto exit; + } + + for (i=0; i < 4; i++) { + code = pdfi_array_get_number(ctx, PDFArray, (uint64_t)i, &f); + if (code < 0) + goto exit; + Range[i] = f; + } + pdfi_countdown(PDFArray); + PDFArray = NULL; + } + code = 0; + + for (i = 0;i < 3; i++) { + pcs->params.lab.WhitePoint[i] = WhitePoint[i]; + pcs->params.lab.BlackPoint[i] = BlackPoint[i]; + } + for (i = 0;i < 4; i++) + pcs->params.lab.Range[i] = Range[i]; + +exit: + pdfi_countdown(PDFArray); + return code; +} + static int pdfi_create_iccbased(pdf_context *ctx, pdf_array *color_array, int index, pdf_dict *stream_dict, pdf_dict *page_dict, gs_color_space **ppcs, bool inline_image) { pdf_stream *ICC_obj = NULL; pdf_dict *dict; /* Alias to avoid tons of casting */ pdf_array *a; int64_t Length, N; - pdf_obj *Name = NULL; + pdf_obj *Name = NULL, *Alt = NULL; char *cname = NULL; int code; bool known = true; @@ -1252,7 +1527,66 @@ static int pdfi_create_iccbased(pdf_context *ctx, pdf_array *color_array, int in code = gs_note_error(gs_error_undefined); break; } + } else { + if (pcs->ICC_Alternate_space == gs_ICC_Alternate_None) { + code = pdfi_dict_knownget(ctx, dict, "Alternate", (pdf_obj **)&Alt); + if (code >= 0) { + switch(pdfi_type_of(Alt)) { + case PDF_NAME: + /* Simple named spaces must be Gray, RGB or CMYK, we ignore /Indexed */ + if (pdfi_name_is((const pdf_name *)Alt, "DeviceGray")) + pcs->ICC_Alternate_space = gs_ICC_Alternate_DeviceGray; + else if (pdfi_name_is((const pdf_name *)Alt, "DeviceRGB")) + pcs->ICC_Alternate_space = gs_ICC_Alternate_DeviceRGB; + else if (pdfi_name_is((const pdf_name *)Alt, "DeviceCMYK")) + pcs->ICC_Alternate_space = gs_ICC_Alternate_DeviceCMYK; + break; + case PDF_ARRAY: + { + pdf_obj *AltName = NULL, *ParamsDict = NULL; + + code = pdfi_array_get_type(ctx, (pdf_array *)Alt, 0, PDF_NAME, &AltName); + if (code >= 0) { + code = pdfi_array_get_type(ctx, (pdf_array *)Alt, 1, PDF_DICT, &ParamsDict); + if (code >= 0) { + if (pdfi_name_is((const pdf_name *)AltName, "CalGray")) { + code = pdfi_set_CalGray_params(ctx, pcs, (pdf_dict *)ParamsDict); + if (code >= 0) + pcs->ICC_Alternate_space = gs_ICC_Alternate_CalGray; + } else { + if (pdfi_name_is((const pdf_name *)AltName, "CalRGB")) { + code = pdfi_set_CalRGB_params(ctx, pcs, (pdf_dict *)ParamsDict); + if (code >= 0) + pcs->ICC_Alternate_space = gs_ICC_Alternate_CalRGB; + } else { + if (pdfi_name_is((const pdf_name *)AltName, "CalCMYK")) { + pcs->ICC_Alternate_space = gs_ICC_Alternate_DeviceCMYK; + } else { + if (pdfi_name_is((const pdf_name *)AltName, "Lab")) { + code = pdfi_set_Lab_params(ctx, pcs, (pdf_dict *)ParamsDict); + if (code >= 0) + pcs->ICC_Alternate_space = gs_ICC_Alternate_Lab; + } + } + } + } + } + } + code = 0; + pdfi_countdown(ParamsDict); + pdfi_countdown(AltName); + } + break; + default: + /* Probably an error, but just ignore it */ + break; + } + pdfi_countdown(Alt); + Alt = NULL; + } + } } + if (ppcs!= NULL) { *ppcs = pcs; if (pcs != NULL) @@ -1270,6 +1604,7 @@ static int pdfi_create_iccbased(pdf_context *ctx, pdf_array *color_array, int in done: if (cname) gs_free_object(ctx->memory, cname, "pdfi_create_iccbased (profile name)"); + pdfi_countdown(Alt); pdfi_countdown(Name); pdfi_countdown(ICC_obj); return code; @@ -2353,7 +2688,7 @@ pdfi_create_colorspace_by_array(pdf_context *ctx, pdf_array *color_array, int in return_error(gs_error_syntaxerror); } code = pdfi_create_DeviceRGB(ctx, ppcs); - } else if (pdfi_name_is(space, "CMYK") || pdfi_name_is(space, "DeviceCMYK")) { + } else if (pdfi_name_is(space, "CMYK") || pdfi_name_is(space, "DeviceCMYK") || pdfi_name_is(space, "CalCMYK")) { if (pdfi_name_is(space, "CMYK") && !inline_image) { pdfi_set_warning(ctx, 0, NULL, W_PDF_BAD_INLINECOLORSPACE, "pdfi_create_colorspace_by_array", NULL); if (ctx->args.pdfstoponwarning) @@ -2420,7 +2755,7 @@ pdfi_create_colorspace_by_name(pdf_context *ctx, pdf_name *name, return_error(gs_error_syntaxerror); } code = pdfi_create_DeviceRGB(ctx, ppcs); - } else if (pdfi_name_is(name, "CMYK") || pdfi_name_is(name, "DeviceCMYK")) { + } else if (pdfi_name_is(name, "CMYK") || pdfi_name_is(name, "DeviceCMYK") || pdfi_name_is(name, "CalCMYK")) { if (pdfi_name_is(name, "CMYK") && !inline_image) { pdfi_set_warning(ctx, 0, NULL, W_PDF_BAD_INLINECOLORSPACE, "pdfi_create_colorspace_by_name", NULL); if (ctx->args.pdfstoponwarning) |