Esempio n. 1
0
/*
 * Create a PDF color space corresponding to a PostScript color space.
 * For parameterless color spaces, set *pvalue to a (literal) string with
 * the color space name; for other color spaces, create a cos_array_t if
 * necessary and set *pvalue to refer to it.  In the latter case, if
 * by_name is true, return a string /Rxxxx rather than a reference to
 * the actual object.
 *
 * If ppranges is not NULL, then if  the domain of the color space had
 * to be scaled (to convert a CIEBased space to ICCBased), store a pointer
 * to the ranges in *ppranges, otherwise set *ppranges to 0.
 */
int
pdf_color_space_named(gx_device_pdf *pdev, cos_value_t *pvalue,
                      const gs_range_t **ppranges,
                      const gs_color_space *pcs_in,
                      const pdf_color_space_names_t *pcsn,
                      bool by_name, const byte *res_name, int name_length)
{
    const gs_color_space *pcs;
    gs_color_space_index csi;
    cos_array_t *pca;
    cos_dict_t *pcd;
    cos_value_t v;
    const gs_cie_common *pciec;
    gs_function_t *pfn;
    const gs_range_t *ranges = 0;
    uint serialized_size;
    byte *serialized = NULL, serialized0[100];
    pdf_resource_t *pres = NULL;
    int code;
#if 0
    bool islab = false;
#endif

    /* If color space is CIE based and we have compatibility then go ahead and use the ICC alternative */
    if ((pdev->CompatibilityLevel < 1.3) || !gs_color_space_is_PSCIE(pcs_in) ) {
        pcs = pcs_in;
    } else {
        pcs = pcs_in;
        /* The snippet below creates an ICC equivalent profile for the PS
           color space.  This is disabled until I add the capability to
           specify the profile version to ensure compatability with
           the PDF versions */
#if 0
        if (pcs_in->icc_equivalent != NULL) {
            pcs = pcs_in->icc_equivalent;
        } else {
            /* Need to create the equivalent object */
            gs_colorspace_set_icc_equivalent((gs_color_space *)pcs_in, &islab, pdev->memory);
            pcs = pcs_in->icc_equivalent;
        }
#endif
    }
    csi = gs_color_space_get_index(pcs);
    /* Note that if csi is ICC, check to see if this was one of
       the default substitutes that we introduced for DeviceGray,
       DeviceRGB or DeviceCMYK.  If it is, then just write
       the default color.  Depending upon the flavor of PDF,
       or other options, we may want to actually have all
       the colors defined by ICC profiles and not do the following
       substituion of the Device space. */
    if (csi == gs_color_space_index_ICC) {
        csi = gsicc_get_default_type(pcs->cmm_icc_profile_data);
    }
    if (ppranges)
        *ppranges = 0;		/* default */
    switch (csi) {
    case gs_color_space_index_DeviceGray:
        cos_c_string_value(pvalue, pcsn->DeviceGray);
        return 0;
    case gs_color_space_index_DeviceRGB:
        cos_c_string_value(pvalue, pcsn->DeviceRGB);
        return 0;
    case gs_color_space_index_DeviceCMYK:
        cos_c_string_value(pvalue, pcsn->DeviceCMYK);
        return 0;
    case gs_color_space_index_Pattern:
        if (!pcs->params.pattern.has_base_space) {
            cos_c_string_value(pvalue, "/Pattern");
            return 0;
        }
        break;
    case gs_color_space_index_ICC:
        /*
         * Take a special early exit for unrecognized ICCBased color spaces,
         * or for PDF 1.2 output (ICCBased color spaces date from PDF 1.3).
         */

        if (pcs->cmm_icc_profile_data == NULL ||
                pdev->CompatibilityLevel < 1.3
           ) {
            if (res_name != NULL)
                return 0; /* Ignore .includecolorspace */
            if (pcs->base_space != NULL) {
                return pdf_color_space_named( pdev, pvalue, ppranges,
                                              pcs->base_space,
                                              pcsn, by_name, NULL, 0);
            } else {
                /* Base space is NULL, use appropriate device space */
                switch( cs_num_components(pcs) )  {
                case 1:
                    cos_c_string_value(pvalue, pcsn->DeviceGray);
                    return 0;
                case 3:
                    cos_c_string_value(pvalue, pcsn->DeviceRGB);
                    return 0;
                case 4:
                    cos_c_string_value(pvalue, pcsn->DeviceCMYK);
                    return 0;
                default:
                    break;
                }
            }
        }

        break;
    default:
        break;
    }
    if (pdev->params.ColorConversionStrategy == ccs_CMYK &&
            csi != gs_color_space_index_DeviceCMYK &&
            csi != gs_color_space_index_DeviceGray &&
            csi != gs_color_space_index_Pattern)
        return_error(gs_error_rangecheck);
    if (pdev->params.ColorConversionStrategy == ccs_sRGB &&
            csi != gs_color_space_index_DeviceRGB &&
            csi != gs_color_space_index_DeviceGray &&
            csi != gs_color_space_index_Pattern)
        return_error(gs_error_rangecheck);
    if (pdev->params.ColorConversionStrategy == ccs_Gray &&
            csi != gs_color_space_index_DeviceGray &&
            csi != gs_color_space_index_Pattern)
        return_error(gs_error_rangecheck);
    /* Check whether we already have a PDF object for this color space. */
    if (pcs->id != gs_no_id)
        pres = pdf_find_resource_by_gs_id(pdev, resourceColorSpace, pcs->id);
    if (pres == NULL) {
        stream s;

        s_init(&s, pdev->memory);
        swrite_position_only(&s);
        code = cs_serialize(pcs, &s);
        if (code < 0)
            return_error(gs_error_unregistered); /* Must not happen. */
        serialized_size = stell(&s);
        sclose(&s);
        if (serialized_size <= sizeof(serialized0))
            serialized = serialized0;
        else {
            serialized = gs_alloc_bytes(pdev->pdf_memory, serialized_size, "pdf_color_space");
            if (serialized == NULL)
                return_error(gs_error_VMerror);
        }
        swrite_string(&s, serialized, serialized_size);
        code = cs_serialize(pcs, &s);
        if (code < 0)
            return_error(gs_error_unregistered); /* Must not happen. */
        if (stell(&s) != serialized_size)
            return_error(gs_error_unregistered); /* Must not happen. */
        sclose(&s);
        pres = pdf_find_cspace_resource(pdev, serialized, serialized_size);
        if (pres != NULL) {
            if (serialized != serialized0)
                gs_free_object(pdev->pdf_memory, serialized, "pdf_color_space");
            serialized = NULL;
        }
    }
    if (pres) {
        const pdf_color_space_t *const ppcs =
            (const pdf_color_space_t *)pres;

        if (ppranges != 0 && ppcs->ranges != 0)
            *ppranges = ppcs->ranges;
        pca = (cos_array_t *)pres->object;
        goto ret;
    }

    /* Space has parameters -- create an array. */
    pca = cos_array_alloc(pdev, "pdf_color_space");
    if (pca == 0)
        return_error(gs_error_VMerror);

    switch (csi) {

    case gs_color_space_index_ICC:
        code = pdf_iccbased_color_space(pdev, pvalue, pcs, pca);
        break;

    case gs_color_space_index_CIEA: {
        /* Check that we can represent this as a CalGray space. */
        const gs_cie_a *pcie = pcs->params.a;
        bool unitary = cie_ranges_are_0_1(&pcie->RangeA, 1);
        bool identityA = (pcie->MatrixA.u == 1 && pcie->MatrixA.v == 1 &&
                          pcie->MatrixA.w == 1);
        gs_vector3 expts;

        pciec = (const gs_cie_common *)pcie;
        if (!pcie->common.MatrixLMN.is_identity) {
            code = pdf_convert_cie_space(pdev, pca, pcs, "GRAY", pciec,
                                         &pcie->RangeA, ONE_STEP_NOT, NULL,
                                         &ranges);
            break;
        }
        if (unitary && identityA &&
                CIE_CACHE_IS_IDENTITY(&pcie->caches.DecodeA) &&
                CIE_SCALAR3_CACHE_IS_EXPONENTIAL(pcie->common.caches.DecodeLMN, expts) &&
                expts.v == expts.u && expts.w == expts.u
           ) {
            DO_NOTHING;
        } else if (unitary && identityA &&
                   CIE_CACHE3_IS_IDENTITY(pcie->common.caches.DecodeLMN) &&
                   cie_vector_cache_is_exponential(&pcie->caches.DecodeA, &expts.u)
                  ) {
            DO_NOTHING;
        } else {
            code = pdf_convert_cie_space(pdev, pca, pcs, "GRAY", pciec,
                                         &pcie->RangeA, ONE_STEP_NOT, NULL,
                                         &ranges);
            break;
        }
        code = cos_array_add(pca, cos_c_string_value(&v, "/CalGray"));
        if (code < 0)
            return code;
        pcd = cos_dict_alloc(pdev, "pdf_color_space(dict)");
        if (pcd == 0)
            return_error(gs_error_VMerror);
        if (expts.u != 1) {
            code = cos_dict_put_c_key_real(pcd, "/Gamma", expts.u);
            if (code < 0)
                return code;
        }
    }
cal:
        /* Finish handling a CIE-based color space (Calxxx or Lab). */
    if (code < 0)
        return code;
    code = pdf_finish_cie_space(pca, pcd, pciec);
    break;

    case gs_color_space_index_CIEABC: {
        /* Check that we can represent this as a CalRGB space. */
        const gs_cie_abc *pcie = pcs->params.abc;
        bool unitary = cie_ranges_are_0_1(pcie->RangeABC.ranges, 3);
        gs_vector3 expts;
        const gs_matrix3 *pmat = NULL;
        cie_cache_one_step_t one_step =
            cie_cached_abc_is_one_step(pcie, &pmat);

        pciec = (const gs_cie_common *)pcie;
        if (unitary) {
            switch (one_step) {
            case ONE_STEP_ABC:
                if (CIE_VECTOR3_CACHE_IS_EXPONENTIAL(pcie->caches.DecodeABC.caches, expts))
                    goto calrgb;
                break;
            case ONE_STEP_LMN:
                if (CIE_SCALAR3_CACHE_IS_EXPONENTIAL(pcie->common.caches.DecodeLMN, expts))
                    goto calrgb;
            default:
                break;
            }
        }
        if (cie_is_lab(pcie)) {
            /* Represent this as a Lab space. */
            pcd = cos_dict_alloc(pdev, "pdf_color_space(dict)");
            if (pcd == 0)
                return_error(gs_error_VMerror);
            code = pdf_put_lab_color_space(pca, pcd, pcie->RangeABC.ranges);
            goto cal;
        } else {
            code = pdf_convert_cie_space(pdev, pca, pcs, "RGB ", pciec,
                                         pcie->RangeABC.ranges,
                                         one_step, pmat, &ranges);
            break;
        }
calrgb:
        code = cos_array_add(pca, cos_c_string_value(&v, "/CalRGB"));
        if (code < 0)
            return code;
        pcd = cos_dict_alloc(pdev, "pdf_color_space(dict)");
        if (pcd == 0)
            return_error(gs_error_VMerror);
        if (expts.u != 1 || expts.v != 1 || expts.w != 1) {
            code = cos_dict_put_c_key_vector3(pcd, "/Gamma", &expts);
            if (code < 0)
                return code;
        }
        if (!pmat->is_identity) {
            cos_array_t *pcma =
                cos_array_alloc(pdev, "pdf_color_space(Matrix)");

            if (pcma == 0)
                return_error(gs_error_VMerror);
            if ((code = cos_array_add_vector3(pcma, &pmat->cu)) < 0 ||
                    (code = cos_array_add_vector3(pcma, &pmat->cv)) < 0 ||
                    (code = cos_array_add_vector3(pcma, &pmat->cw)) < 0 ||
                    (code = cos_dict_put(pcd, (const byte *)"/Matrix", 7,
                                         COS_OBJECT_VALUE(&v, pcma))) < 0
               )
                return code;
        }
    }
    goto cal;

    case gs_color_space_index_CIEDEF:
        code = pdf_convert_cie_space(pdev, pca, pcs, "RGB ",
                                     (const gs_cie_common *)pcs->params.def,
                                     pcs->params.def->RangeDEF.ranges,
                                     ONE_STEP_NOT, NULL, &ranges);
        break;

    case gs_color_space_index_CIEDEFG:
        code = pdf_convert_cie_space(pdev, pca, pcs, "CMYK",
                                     (const gs_cie_common *)pcs->params.defg,
                                     pcs->params.defg->RangeDEFG.ranges,
                                     ONE_STEP_NOT, NULL, &ranges);
        break;

    case gs_color_space_index_Indexed:
        code = pdf_indexed_color_space(pdev, pvalue, pcs, pca);
        break;

    case gs_color_space_index_DeviceN:
        if (!pdev->PreserveDeviceN)
            return_error(gs_error_rangecheck);
        if (pdev->CompatibilityLevel < 1.3)
            return_error(gs_error_rangecheck);
        pfn = gs_cspace_get_devn_function(pcs);
        /****** CURRENTLY WE ONLY HANDLE Functions ******/
        if (pfn == 0)
            return_error(gs_error_rangecheck);
        {
            cos_array_t *psna =
                cos_array_alloc(pdev, "pdf_color_space(DeviceN)");
            int i;
            byte *name_string;
            uint name_string_length;
            cos_value_t v_attriburtes, *va = NULL;

            if (psna == 0)
                return_error(gs_error_VMerror);
            for (i = 0; i < pcs->params.device_n.num_components; ++i) {
                if ((code = pcs->params.device_n.get_colorname_string(
                                pdev->memory,
                                pcs->params.device_n.names[i], &name_string,
                                &name_string_length)) < 0 ||
                        (code = pdf_string_to_cos_name(pdev, name_string,
                                                       name_string_length, &v)) < 0 ||
                        (code = cos_array_add_no_copy(psna, &v)) < 0)
                    return code;
            }
            COS_OBJECT_VALUE(&v, psna);
            if (pcs->params.device_n.colorants != NULL) {
                cos_dict_t *colorants  = cos_dict_alloc(pdev, "pdf_color_space(DeviceN)");
                cos_value_t v_colorants, v_separation, v_colorant_name;
                const gs_device_n_attributes *csa;
                pdf_resource_t *pres_attributes;

                if (colorants == NULL)
                    return_error(gs_error_VMerror);
                code = pdf_alloc_resource(pdev, resourceOther, 0, &pres_attributes, -1);
                if (code < 0)
                    return code;
                cos_become(pres_attributes->object, cos_type_dict);
                COS_OBJECT_VALUE(&v_colorants, colorants);
                code = cos_dict_put((cos_dict_t *)pres_attributes->object,
                                    (const byte *)"/Colorants", 10, &v_colorants);
                if (code < 0)
                    return code;
                for (csa = pcs->params.device_n.colorants; csa != NULL; csa = csa->next) {
                    code = pcs->params.device_n.get_colorname_string(pdev->memory,
                            csa->colorant_name, &name_string, &name_string_length);
                    if (code < 0)
                        return code;
                    code = pdf_color_space_named(pdev, &v_separation, NULL, csa->cspace, pcsn, false, NULL, 0);
                    if (code < 0)
                        return code;
                    code = pdf_string_to_cos_name(pdev, name_string, name_string_length, &v_colorant_name);
                    if (code < 0)
                        return code;
                    code = cos_dict_put(colorants, v_colorant_name.contents.chars.data,
                                        v_colorant_name.contents.chars.size, &v_separation);
                    if (code < 0)
                        return code;
                }
                code = pdf_substitute_resource(pdev, &pres_attributes, resourceOther, NULL, true);
                if (code < 0)
                    return code;
                pres_attributes->where_used |= pdev->used_mask;
                va = &v_attriburtes;
                COS_OBJECT_VALUE(va, pres_attributes->object);
            }
            if ((code = pdf_separation_color_space(pdev, pca, "/DeviceN", &v,
                                                   pcs->base_space,
                                                   pfn, &pdf_color_space_names, va)) < 0)
                return code;
        }
        break;

    case gs_color_space_index_Separation:
        if (!pdev->PreserveSeparation)
            return_error(gs_error_rangecheck);
        pfn = gs_cspace_get_sepr_function(pcs);
        /****** CURRENTLY WE ONLY HANDLE Functions ******/
        if (pfn == 0)
            return_error(gs_error_rangecheck);
        {
            byte *name_string;
            uint name_string_length;
            if ((code = pcs->params.separation.get_colorname_string(
                            pdev->memory,
                            pcs->params.separation.sep_name, &name_string,
                            &name_string_length)) < 0 ||
                    (code = pdf_string_to_cos_name(pdev, name_string,
                                                   name_string_length, &v)) < 0 ||
                    (code = pdf_separation_color_space(pdev, pca, "/Separation", &v,
                            pcs->base_space,
                            pfn, &pdf_color_space_names, NULL)) < 0)
                return code;
        }
        break;

    case gs_color_space_index_Pattern:
        if ((code = pdf_color_space_named(pdev, pvalue, ppranges,
                                          pcs->base_space,
                                          &pdf_color_space_names, false, NULL, 0)) < 0 ||
                (code = cos_array_add(pca,
                                      cos_c_string_value(&v, "/Pattern"))) < 0 ||
                (code = cos_array_add(pca, pvalue)) < 0
           )
            return code;
        break;

    default:
        return_error(gs_error_rangecheck);
    }

    /*
     * Register the color space as a resource, since it must be referenced
     * by name rather than directly.
     */
    {
        pdf_color_space_t *ppcs;

        if (code < 0 ||
                (code = pdf_alloc_resource(pdev, resourceColorSpace, pcs->id,
                                           &pres, -1)) < 0
           ) {
            COS_FREE(pca, "pdf_color_space");
            return code;
        }
        pdf_reserve_object_id(pdev, pres, 0);
        if (res_name != NULL) {
            int l = min(name_length, sizeof(pres->rname) - 1);

            memcpy(pres->rname, res_name, l);
            pres->rname[l] = 0;
        }
        ppcs = (pdf_color_space_t *)pres;
        if (serialized == serialized0) {
            serialized = gs_alloc_bytes(pdev->pdf_memory, serialized_size, "pdf_color_space");
            if (serialized == NULL)
                return_error(gs_error_VMerror);
            memcpy(serialized, serialized0, serialized_size);
        }
        ppcs->serialized = serialized;
        ppcs->serialized_size = serialized_size;
        if (ranges) {
            int num_comp = gs_color_space_num_components(pcs);
            gs_range_t *copy_ranges = (gs_range_t *)
                                      gs_alloc_byte_array(pdev->pdf_memory, num_comp,
                                              sizeof(gs_range_t), "pdf_color_space");

            if (copy_ranges == 0) {
                COS_FREE(pca, "pdf_color_space");
                return_error(gs_error_VMerror);
            }
            memcpy(copy_ranges, ranges, num_comp * sizeof(gs_range_t));
            ppcs->ranges = copy_ranges;
            if (ppranges)
                *ppranges = copy_ranges;
        } else
            ppcs->ranges = 0;
        pca->id = pres->object->id;
        COS_FREE(pres->object, "pdf_color_space");
        pres->object = (cos_object_t *)pca;
        cos_write_object(COS_OBJECT(pca), pdev, resourceColorSpace);
    }
ret:
    if (by_name) {
        /* Return a resource name rather than an object reference. */
        discard(COS_RESOURCE_VALUE(pvalue, pca));
    } else
        discard(COS_OBJECT_VALUE(pvalue, pca));
    if (pres != NULL) {
        pres->where_used |= pdev->used_mask;
        code = pdf_add_resource(pdev, pdev->substream_Resources, "/ColorSpace", pres);
        if (code < 0)
            return code;
    }
    return 0;
}
Esempio n. 2
0
File: zicc.c Progetto: hackqiang/gs
/*
 *   <dict>  .set_outputintent  -
 *
 * Set and use the specified output intent.  
 *
 */
static int
zset_outputintent(i_ctx_t * i_ctx_p)
{
    os_ptr                  op = osp;
    int                     code = 0;
    gx_device *dev = gs_currentdevice(igs);
    gs_imager_state *pis = (gs_imager_state *)igs;
    cmm_dev_profile_t       *dev_profile;
    stream *                s = 0L;
    ref *                   pnval;
    ref *                   pstrmval;
    int                     ncomps, dev_comps;
    cmm_profile_t           *picc_profile;
    int                     expected = 0;
    gs_color_space_index    index;
    gsicc_manager_t         *icc_manager = pis->icc_manager;
    cmm_profile_t           *source_profile = NULL;

    check_type(*op, t_dictionary);
    check_dict_read(*op);
    if_debug0m(gs_debug_flag_icc, imemory, "[icc] Using OutputIntent\n");

    /* Get the device structure */
    code = dev_proc(dev, get_profile)(dev,  &dev_profile);
    if (dev_profile == NULL) { 
        code = gsicc_init_device_profile_struct(dev, NULL, 0);
        code = dev_proc(dev, get_profile)(dev,  &dev_profile);
    }
    if (dev_profile->oi_profile != NULL) {
        return 0;  /* Allow only one setting of this object */
    }
    code = dict_find_string(op, "N", &pnval);
    if (code < 0)
        return code;
    ncomps = pnval->value.intval;

    /* verify the DataSource entry. Creat profile from stream */
    if (dict_find_string(op, "DataSource", &pstrmval) <= 0)
        return_error(e_undefined);
    check_read_file(i_ctx_p, s, pstrmval);

    picc_profile = gsicc_profile_new(s, gs_state_memory(igs), NULL, 0);
    picc_profile->num_comps = ncomps;
    picc_profile->profile_handle =
        gsicc_get_profile_handle_buffer(picc_profile->buffer,
                                        picc_profile->buffer_size,
                                        gs_state_memory(igs));
    if (picc_profile->profile_handle == NULL) {
        rc_decrement(picc_profile,"zset_outputintent");
        return -1;
    }
    picc_profile->data_cs = gscms_get_profile_data_space(picc_profile->profile_handle);
    switch (picc_profile->data_cs) {
        case gsCIEXYZ:
        case gsCIELAB:
        case gsRGB:
            expected = 3;
            source_profile = icc_manager->default_rgb;
            break;
        case gsGRAY:
            expected = 1;
            source_profile = icc_manager->default_gray;
            break;
        case gsCMYK:
            expected = 4;
            source_profile = icc_manager->default_cmyk;
            break;
        case gsNCHANNEL:
            expected = 0;
            break;
        case gsNAMED:            
        case gsUNDEFINED:        
            break;
    }
    if (expected && ncomps != expected) {
        rc_decrement(picc_profile,"zset_outputintent");
        return_error(e_rangecheck);
    }
    gsicc_init_hash_cs(picc_profile, pis);

    /* All is well with the profile.  Lets set the stuff that needs to be set */
    dev_profile->oi_profile = picc_profile;
    picc_profile->name = (char *) gs_alloc_bytes(picc_profile->memory, 
                                                 MAX_DEFAULT_ICC_LENGTH,
                                                 "zset_outputintent");
    strncpy(picc_profile->name, OI_PROFILE, strlen(OI_PROFILE));
    picc_profile->name[strlen(OI_PROFILE)] = 0;
    picc_profile->name_length = strlen(OI_PROFILE);
    /* Set the range of the profile */
    gscms_set_icc_range(&picc_profile);

    /* If the output device has a different number of componenets, then we are 
       going to set the output intent as the proofing profile, unless the 
       proofing profile has already been set. 

       If the device has the same number of components (and color model) then as 
       the profile we will use this as the output profile, unless someone has 
       explicitly set the output profile.

       Finally, we will use the output intent profile for the default profile
       of the proper Device profile in the icc manager, again, unless someone
       has explicitly set this default profile. */

    dev_comps = dev_profile->device_profile[0]->num_comps;
    index = gsicc_get_default_type(dev_profile->device_profile[0]);
    if (ncomps == dev_comps && index < gs_color_space_index_DevicePixel) {
        /* The OI profile is the same type as the profile for the device and a 
           "default" profile for the device was not externally set. So we go 
           ahead and use the OI profile as the device profile.  Care needs to be 
           taken here to keep from screwing up any device parameters.   We will 
           use a keyword of OIProfile for the user/device parameter to indicate 
           its usage.  Also, note conflicts if one is setting object dependent 
           color management */
        rc_assign(dev_profile->device_profile[0], picc_profile, 
                  "zset_outputintent");
        if_debug0m(gs_debug_flag_icc, imemory, "[icc] OutputIntent used for device profile\n");
    } else {
        if (dev_profile->proof_profile == NULL) {
            /* This means that we should use the OI profile as the proofing 
               profile.  Note that if someone already has specified a 
               proofing profile it is unclear what they are trying to do
               with the output intent.  In this case, we will use it 
               just for the source data below */
            dev_profile->proof_profile = picc_profile;
            rc_increment(picc_profile);
            if_debug0m(gs_debug_flag_icc, imemory, "[icc] OutputIntent used for proof profile\n");
        }
    }
    /* Now the source colors.  See which source color space needs to use the
       output intent ICC profile */
    index = gsicc_get_default_type(source_profile);
    if (index < gs_color_space_index_DevicePixel) {
        /* source_profile is currently the default.  Set it to the OI profile */
        switch (picc_profile->data_cs) {
            case gsGRAY:
                if_debug0m(gs_debug_flag_icc, imemory, "[icc] OutputIntent used source Gray\n");
                rc_assign(icc_manager->default_gray, picc_profile, 
                          "zset_outputintent");
                break;
            case gsRGB:
                if_debug0m(gs_debug_flag_icc, imemory, "[icc] OutputIntent used source RGB\n");
                rc_assign(icc_manager->default_rgb, picc_profile, 
                          "zset_outputintent");
                break;
            case gsCMYK:
                if_debug0m(gs_debug_flag_icc, imemory, "[icc] OutputIntent used source CMYK\n");
                rc_assign(icc_manager->default_cmyk, picc_profile, 
                          "zset_outputintent");
                break;
            default:
                break;
        }
    }
    /* Remove the output intent dict from the stack */
    pop(1);
    return code;
}
Esempio n. 3
0
/* Create a Separation or DeviceN color space (internal). */
static int
pdf_separation_color_space(gx_device_pdf *pdev,
                           cos_array_t *pca, const char *csname,
                           const cos_value_t *snames,
                           const gs_color_space *alt_space,
                           const gs_function_t *pfn,
                           const pdf_color_space_names_t *pcsn,
                           const cos_value_t *v_attributes)
{
    cos_value_t v;
    const gs_range_t *ranges;
    int code, csi;

    /* We need to think about the alternate space. If we are producing
     * PDF/X or PDF/A we can't produce some device spaces, and the code in
     * pdf_color_space_named always allows device spaces. We could alter
     * that code, but by then we don't know its an Alternate space, and have
     * lost the tin transform procedure. So instead we check here.
     */
    csi = gs_color_space_get_index(alt_space);
    /* Note that if csi is ICC, check to see if this was one of
       the default substitutes that we introduced for DeviceGray,
       DeviceRGB or DeviceCMYK.  If it is, then just write
       the default color.  Depending upon the flavor of PDF,
       or other options, we may want to actually have all
       the colors defined by ICC profiles and not do the following
       substituion of the Device space. */
    if (csi == gs_color_space_index_ICC) {
        csi = gsicc_get_default_type(alt_space->cmm_icc_profile_data);
    }
    if (csi == gs_color_space_index_DeviceRGB && (pdev->PDFX ||
            (pdev->PDFA && (pdev->pcm_color_info_index == gs_color_space_index_DeviceCMYK)))) {

        /* We have a DeviceRGB alternate, but are producing either PDF/X or
         * PDF/A with a DeviceCMYK process color model. So we need to convert
         * the alternate space into CMYK. We do this by evaluating the function
         * at each end of the Separation space (0 and 1), convert the resulting
         * RGB colours into CMYK and create a new function which linearly
         * interpolates between these points.
         */
        gs_function_t *new_pfn = 0;
        float in[1] = {0.0f};
        float out_low[4];
        float out_high[4];

        code = gs_function_evaluate(pfn, in, out_low);
        if (code < 0)
            return code;
        pdf_SepRGB_ConvertToCMYK((float *)&out_low, (float *)&out_low);

        in[0] = 1.0f;
        code = gs_function_evaluate(pfn, in, out_high);
        if (code < 0)
            return code;
        pdf_SepRGB_ConvertToCMYK((float *)&out_high, (float *)&out_high);

        code = pdf_make_base_space_function(pdev, &new_pfn, 4, out_low, out_high);
        if (code < 0)
            return code;
        if ((code = cos_array_add(pca, cos_c_string_value(&v, csname))) < 0 ||
                (code = cos_array_add_no_copy(pca, snames)) < 0 ||
                (code = (int)cos_c_string_value(&v, (const char *)pcsn->DeviceCMYK)) < 0 ||
                (code = cos_array_add(pca, &v)) < 0 ||
                (code = pdf_function_scaled(pdev, new_pfn, 0x00, &v)) < 0 ||
                (code = cos_array_add(pca, &v)) < 0 ||
                (v_attributes != NULL ? code = cos_array_add(pca, v_attributes) : 0) < 0
           ) {}
        pdf_delete_base_space_function(pdev, new_pfn);
        return code;
    }
    if (csi == gs_color_space_index_DeviceCMYK &&
            (pdev->PDFA && (pdev->pcm_color_info_index == gs_color_space_index_DeviceRGB))) {
        /* We have a DeviceCMYK alternate, but are producingPDF/A with a
         * DeviceRGB process color model. See comment above re DviceRGB.
         */
        gs_function_t *new_pfn = 0;
        float in[1] = {0.0f};
        float out_low[4];
        float out_high[4];

        code = gs_function_evaluate(pfn, in, out_low);
        if (code < 0)
            return code;
        pdf_SepCMYK_ConvertToRGB((float *)&out_low, (float *)&out_low);

        in[0] = 1.0f;
        code = gs_function_evaluate(pfn, in, out_high);
        if (code < 0)
            return code;
        pdf_SepCMYK_ConvertToRGB((float *)&out_high, (float *)&out_high);

        code = pdf_make_base_space_function(pdev, &new_pfn, 3, out_low, out_high);
        if (code < 0)
            return code;
        if ((code = cos_array_add(pca, cos_c_string_value(&v, csname))) < 0 ||
                (code = cos_array_add_no_copy(pca, snames)) < 0 ||
                (code = (int)cos_c_string_value(&v, pcsn->DeviceRGB)) < 0 ||
                (code = cos_array_add(pca, &v)) < 0 ||
                (code = pdf_function_scaled(pdev, new_pfn, 0x00, &v)) < 0 ||
                (code = cos_array_add(pca, &v)) < 0 ||
                (v_attributes != NULL ? code = cos_array_add(pca, v_attributes) : 0) < 0
           ) {}
        pdf_delete_base_space_function(pdev, new_pfn);
        return code;
    }

    if ((code = cos_array_add(pca, cos_c_string_value(&v, csname))) < 0 ||
            (code = cos_array_add_no_copy(pca, snames)) < 0 ||
            (code = pdf_color_space_named(pdev, &v, &ranges, alt_space, pcsn, false, NULL, 0)) < 0 ||
            (code = cos_array_add(pca, &v)) < 0 ||
            (code = pdf_function_scaled(pdev, pfn, ranges, &v)) < 0 ||
            (code = cos_array_add(pca, &v)) < 0 ||
            (v_attributes != NULL ? code = cos_array_add(pca, v_attributes) : 0) < 0
       )
        return code;
    return 0;
}