int gs_pop_transparency_state(gs_state *pgs, bool force) { gs_pdf14trans_params_t params = { 0 }; gs_imager_state * pis = (gs_imager_state *)pgs; int code; if (check_for_nontrans_pattern(pgs, (unsigned char *)"gs_pop_transparency_state")) { return(0); } /* Check if flag is set, which indicates that we have an active softmask for the graphic state. We need to communicate to the compositor to pop the softmask */ if ( pis->trans_flags.xstate_change || force) { if_debug0m('v', pgs->memory, "[v]gs_pop_transparency_state sending\n"); params.pdf14_op = PDF14_POP_TRANS_STATE; code = gs_state_update_pdf14trans(pgs, ¶ms); if ( code < 0 ) return (code); } else { if_debug0m('v', pgs->memory, "[v]gs_pop_transparency_state NOT sending\n"); } /* There is no reason to reset any of the flags since they will be reset by the graphic state restore */ return(0); }
/* Common code shared between remap and concretize */ static int gx_ciea_to_icc(gs_color_space **ppcs_icc, gs_color_space *pcs, gs_memory_t *memory) { int code; gs_color_space *palt_cs = pcs->base_space; gx_cie_vector_cache *a_cache = &(pcs->params.a->caches.DecodeA); gx_cie_scalar_cache *lmn_caches = &(pcs->params.a->common.caches.DecodeLMN[0]); if_debug0m(gs_debug_flag_icc, memory, "[icc] Creating ICC profile from CIEA object"); /* build the ICC color space object */ code = gs_cspace_build_ICC(ppcs_icc, NULL, memory); if (code < 0) return gs_rethrow(code, "Failed to create ICC profile"); /* record the cie alt space as the icc alternative color space */ (*ppcs_icc)->base_space = palt_cs; rc_increment_cs(palt_cs); (*ppcs_icc)->cmm_icc_profile_data = gsicc_profile_new(NULL, memory, NULL, 0); if ((*ppcs_icc)->cmm_icc_profile_data == NULL) gs_throw(gs_error_VMerror, "Failed to create ICC profile"); code = gsicc_create_froma(pcs, &((*ppcs_icc)->cmm_icc_profile_data->buffer), &((*ppcs_icc)->cmm_icc_profile_data->buffer_size), memory, a_cache, lmn_caches); if (code < 0) return gs_rethrow(code, "Failed to create ICC profile from CIEA"); code = gsicc_init_profile_info((*ppcs_icc)->cmm_icc_profile_data); if (code < 0) return gs_rethrow(code, "Failed to build ICC profile from CIEDEF"); (*ppcs_icc)->cmm_icc_profile_data->default_match = CIE_A; /* Assign to the icc_equivalent member variable */ pcs->icc_equivalent = *ppcs_icc; pcs->icc_equivalent->cmm_icc_profile_data->data_cs = gsGRAY; return 0; }
int gx_push_transparency_state(gs_imager_state * pis, gx_device * pdev) { if_debug0m('v', pis->memory, "[v]gx_push_transparency_state\n"); if (dev_proc(pdev, push_transparency_state) != 0) return (*dev_proc(pdev, push_transparency_state)) (pdev, pis); else return 0; }
int gx_end_transparency_group(gs_imager_state * pis, gx_device * pdev) { if_debug0m('v', pis->memory, "[v]gx_end_transparency_group\n"); if (dev_proc(pdev, end_transparency_group) != 0) return (*dev_proc(pdev, end_transparency_group)) (pdev, pis); else return 0; }
int gs_update_trans_marking_params(gs_state * pgs) { gs_pdf14trans_params_t params = { 0 }; if_debug0m('v', pgs->memory, "[v]gs_update_trans_marking_params\n"); params.pdf14_op = PDF14_SET_BLEND_PARAMS; return gs_state_update_pdf14trans(pgs, ¶ms); }
int gx_pop_transparency_state(gs_gstate * pgs, gx_device * pdev) { if_debug0m('v', pgs->memory, "[v]gx_pop_transparency_state\n"); if (dev_proc(pdev, pop_transparency_state) != 0) return (*dev_proc(pdev, pop_transparency_state)) (pdev, pgs); else return 0; }
int gs_end_transparency_group(gs_state *pgs) { gs_pdf14trans_params_t params = { 0 }; if (pgs->is_gstate && check_for_nontrans_pattern(pgs, (unsigned char *)"gs_end_transparency_group")) { return(0); } if_debug0m('v', pgs->memory, "[v]gs_end_transparency_group\n"); params.pdf14_op = PDF14_END_TRANS_GROUP; /* Other parameters not used */ return gs_state_update_pdf14trans(pgs, ¶ms); }
int gs_push_transparency_state(gs_state *pgs) { gs_pdf14trans_params_t params = { 0 }; gs_imager_state * pis = (gs_imager_state *)pgs; int code; if (check_for_nontrans_pattern(pgs, (unsigned char *)"gs_push_transparency_state")) { return(0); } /* Set the pending flag to true, which indicates that we need to watch for end transparency soft masks when we are at this graphic state level */ /* pis->trans_flags.xstate_pending = true; */ /* Actually I believe the above flag is not needed. We really should be watching for the softmask even at the base level. What we need to watch for are q operations after a soft mask end has occured. */ /* Check if we have a change flag set to true. this indicates that a softmask is present. We will need to send a push state to save the current soft mask, so that we can restore it later */ if (pis->trans_flags.xstate_change) { if_debug0m('v', pgs->memory, "[v]gs_push_transparency_state sending\n"); params.pdf14_op = PDF14_PUSH_TRANS_STATE; code = gs_state_update_pdf14trans(pgs, ¶ms); if (code < 0) return(code); } else { if_debug0m('v', pgs->memory, "[v]gs_push_transparency_state NOT sending\n"); } return(0); }
/* <source> <dict> /JPXDecode <file> */ static int z_jpx_decode(i_ctx_t * i_ctx_p) { os_ptr op = osp; ref *sop = NULL; ref *csname = NULL; stream_jpxd_state state; /* it's our responsibility to call set_defaults() */ state.memory = imemory->non_gc_memory; if (s_jpxd_template.set_defaults) (*s_jpxd_template.set_defaults)((stream_state *)&state); if (r_has_type(op, t_dictionary)) { check_dict_read(*op); if ( dict_find_string(op, "Alpha", &sop) > 0) { check_type(*sop, t_boolean); if (sop->value.boolval) state.alpha = true; } if ( dict_find_string(op, "ColorSpace", &sop) > 0) { /* parse the value */ if (r_is_array(sop)) { /* assume it's the first array element */ csname = sop->value.refs; } else if (r_has_type(sop,t_name)) { /* use the name directly */ csname = sop; } else { dmprintf(imemory, "warning: JPX ColorSpace value is an unhandled type!\n"); } if (csname != NULL) { ref sref; /* get a reference to the name's string value */ name_string_ref(imemory, csname, &sref); /* request raw index values if the colorspace is /Indexed */ if (!ISTRCMP(&sref, "Indexed")) state.colorspace = gs_jpx_cs_indexed; /* tell the filter what output we want for other spaces */ else if (!ISTRCMP(&sref, "DeviceGray")) state.colorspace = gs_jpx_cs_gray; else if (!ISTRCMP(&sref, "DeviceRGB")) state.colorspace = gs_jpx_cs_rgb; else if (!ISTRCMP(&sref, "DeviceCMYK")) state.colorspace = gs_jpx_cs_cmyk; else if (!ISTRCMP(&sref, "ICCBased")) { /* The second array element should be the profile's stream dict */ ref *csdict = sop->value.refs + 1; ref *nref; ref altname; if (r_is_array(sop) && (r_size(sop) > 1) && r_has_type(csdict, t_dictionary)) { check_dict_read(*csdict); /* try to look up the alternate space */ if (dict_find_string(csdict, "Alternate", &nref) > 0) { name_string_ref(imemory, csname, &altname); if (!ISTRCMP(&altname, "DeviceGray")) state.colorspace = gs_jpx_cs_gray; else if (!ISTRCMP(&altname, "DeviceRGB")) state.colorspace = gs_jpx_cs_rgb; else if (!ISTRCMP(&altname, "DeviceCMYK")) state.colorspace = gs_jpx_cs_cmyk; } /* else guess based on the number of components */ if (state.colorspace == gs_jpx_cs_unset && dict_find_string(csdict, "N", &nref) > 0) { if_debug1m('w', imemory, "[w] JPX image has an external %"PRIpsint " channel colorspace\n", nref->value.intval); switch (nref->value.intval) { case 1: state.colorspace = gs_jpx_cs_gray; break; case 3: state.colorspace = gs_jpx_cs_rgb; break; case 4: state.colorspace = gs_jpx_cs_cmyk; break; } } } } } else { if_debug0m('w', imemory, "[w] Couldn't read JPX ColorSpace key!\n"); } } } /* we pass npop=0, since we've no arguments left to consume */ /* we pass 0 instead of the usual rspace(sop) which will allocate storage for filter state from the same memory pool as the stream it's coding. this causes no trouble because we maintain no pointers */ return filter_read(i_ctx_p, 0, &s_jpxd_template, (stream_state *) & state, 0); }
/* * <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; }
int gs_begin_transparency_mask(gs_state * pgs, const gs_transparency_mask_params_t * ptmp, const gs_rect * pbbox, bool mask_is_image) { gs_pdf14trans_params_t params = { 0 }; gs_pdf14trans_params_t params_color = { 0 }; const int l = sizeof(params.Background[0]) * ptmp->Background_components; int i, code; gs_color_space *blend_color_space; gsicc_manager_t *icc_manager = pgs->icc_manager; if (check_for_nontrans_pattern(pgs, (unsigned char *)"gs_pop_transparency_state")) { return(0); } params.pdf14_op = PDF14_BEGIN_TRANS_MASK; params.bbox = *pbbox; params.subtype = ptmp->subtype; params.Background_components = ptmp->Background_components; memcpy(params.Background, ptmp->Background, l); params.GrayBackground = ptmp->GrayBackground; params.transfer_function = ptmp->TransferFunction_data; params.function_is_identity = (ptmp->TransferFunction == mask_transfer_identity); params.mask_is_image = mask_is_image; params.replacing = ptmp->replacing; /* The eventual state that we want this smask to be moved to is always gray. This should provide us with a significant speed improvement over the old code. This does not keep us from having groups within the softmask getting blended in different color spaces, it just makes the final space be gray, which is what we will need to get to eventually anyway. In this way we avoid a final color conversion on a potentially large buffer. */ /* Also check if we have loaded in the transparency icc profiles. If not go ahead and take care of that now */ if (icc_manager->smask_profiles == NULL) { code = gsicc_initialize_iccsmask(icc_manager); if (code < 0) return(code); } /* A new soft mask group, make sure the profiles are set */ if_debug0m('v', pgs->memory, "[v]pushing soft mask color sending\n"); if (params.subtype != TRANSPARENCY_MASK_None) { params_color.pdf14_op = PDF14_PUSH_SMASK_COLOR; code = gs_state_update_pdf14trans(pgs, ¶ms_color); if (code < 0) return(code); blend_color_space = gs_cspace_new_DeviceGray(pgs->memory); blend_color_space->cmm_icc_profile_data = pgs->icc_manager->default_gray; rc_increment(blend_color_space->cmm_icc_profile_data); if_debug8m('v', pgs->memory, "[v](0x%lx)gs_begin_transparency_mask [%g %g %g %g]\n\ subtype = %d Background_components = %d %s\n", (ulong)pgs, pbbox->p.x, pbbox->p.y, pbbox->q.x, pbbox->q.y, (int)ptmp->subtype, ptmp->Background_components, (ptmp->TransferFunction == mask_transfer_identity ? "no TR" : "has TR")); /* Sample the transfer function */ for (i = 0; i < MASK_TRANSFER_FUNCTION_SIZE; i++) { float in = (float)(i * (1.0 / (MASK_TRANSFER_FUNCTION_SIZE - 1))); float out; ptmp->TransferFunction(in, &out, ptmp->TransferFunction_data); params.transfer_fn[i] = (byte)floor((double)(out * 255 + 0.5)); } /* Note: This function is called during the c-list writer side. */ if ( blend_color_space->cmm_icc_profile_data != NULL ) { /* Blending space is ICC based. If we are doing c-list rendering we will need to write this color space into the clist. */ params.group_color = ICC; params.group_color_numcomps = blend_color_space->cmm_icc_profile_data->num_comps; /* Get the ICC profile */ /* We don't reference count this - see comment in * pdf14_update_device_color_procs_pop_c() */ params.iccprofile = blend_color_space->cmm_icc_profile_data; params.icc_hash = blend_color_space->cmm_icc_profile_data->hashcode; } else { params.group_color = GRAY_SCALE; params.group_color_numcomps = 1; /* Need to check */ } /* Explicitly decrement the profile data since blend_color_space may not * be an ICC color space object. */ rc_decrement(blend_color_space->cmm_icc_profile_data, "gs_begin_transparency_mask"); rc_decrement_only_cs(blend_color_space, "gs_begin_transparency_mask"); }
/* * To device space */ int gx_remap_ICC(const gs_client_color * pcc, const gs_color_space * pcs, gx_device_color * pdc, const gs_gstate * pgs, gx_device * dev, gs_color_select_t select) { gsicc_link_t *icc_link; gsicc_rendering_param_t rendering_params; unsigned short psrc[GS_CLIENT_COLOR_MAX_COMPONENTS], psrc_cm[GS_CLIENT_COLOR_MAX_COMPONENTS]; unsigned short *psrc_temp; frac conc[GS_CLIENT_COLOR_MAX_COMPONENTS]; int k,i; #ifdef DEBUG int num_src_comps; #endif int num_des_comps; int code; cmm_dev_profile_t *dev_profile; code = dev_proc(dev, get_profile)(dev, &dev_profile); if (code < 0) return code; if (dev_profile == NULL) return gs_throw(gs_error_Fatal, "Attempting to do ICC remap with no profile"); num_des_comps = gsicc_get_device_profile_comps(dev_profile); rendering_params.black_point_comp = pgs->blackptcomp; rendering_params.graphics_type_tag = dev->graphics_type_tag; rendering_params.override_icc = false; rendering_params.preserve_black = gsBKPRESNOTSPECIFIED; rendering_params.rendering_intent = pgs->renderingintent; rendering_params.cmm = gsCMM_DEFAULT; /* Need to clear out psrc_cm in case we have separation bands that are not color managed */ memset(psrc_cm,0,sizeof(unsigned short)*GS_CLIENT_COLOR_MAX_COMPONENTS); /* This needs to be optimized */ if (pcs->cmm_icc_profile_data->data_cs == gsCIELAB || pcs->cmm_icc_profile_data->islab) { psrc[0] = (unsigned short) (pcc->paint.values[0]*65535.0/100.0); psrc[1] = (unsigned short) ((pcc->paint.values[1]+128)/255.0*65535.0); psrc[2] = (unsigned short) ((pcc->paint.values[2]+128)/255.0*65535.0); } else { for (k = 0; k < pcs->cmm_icc_profile_data->num_comps; k++){ psrc[k] = (unsigned short) (pcc->paint.values[k]*65535.0); } } /* Get a link from the cache, or create if it is not there. Need to get 16 bit profile */ icc_link = gsicc_get_link(pgs, dev, pcs, NULL, &rendering_params, pgs->memory); if (icc_link == NULL) { #ifdef DEBUG gs_warn("Could not create ICC link: Check profiles"); #endif return -1; } if (icc_link->is_identity) { psrc_temp = &(psrc[0]); } else { /* Transform the color */ psrc_temp = &(psrc_cm[0]); (icc_link->procs.map_color)(dev, icc_link, psrc, psrc_temp, 2); } #ifdef DEBUG if (!icc_link->is_identity) { num_src_comps = pcs->cmm_icc_profile_data->num_comps; if_debug0m(gs_debug_flag_icc, dev->memory, "[icc] remap [ "); for (k = 0; k < num_src_comps; k++) { if_debug1m(gs_debug_flag_icc, dev->memory, "%d ", psrc[k]); } if_debug0m(gs_debug_flag_icc, dev->memory, "] --> [ "); for (k = 0; k < num_des_comps; k++) { if_debug1m(gs_debug_flag_icc, dev->memory, "%d ", psrc_temp[k]); } if_debug0m(gs_debug_flag_icc, dev->memory, "]\n"); } #endif /* Release the link */ gsicc_release_link(icc_link); /* Now do the remap for ICC which amounts to the alpha application the transfer function and potentially the halftoning */ /* Right now we need to go from unsigned short to frac. I really would like to avoid this sort of stuff. That will come. */ for ( k = 0; k < num_des_comps; k++){ conc[k] = ushort2frac(psrc_temp[k]); } gx_remap_concrete_ICC(conc, pcs, pdc, pgs, dev, select); /* Save original color space and color info into dev color */ i = pcs->cmm_icc_profile_data->num_comps; for (i--; i >= 0; i--) pdc->ccolor.paint.values[i] = pcc->paint.values[i]; pdc->ccolor_valid = true; return 0; }
/* * Enter a key-value pair in a dictionary. * See idict.h for the possible return values. */ int dict_put(ref * pdref /* t_dictionary */ , const ref * pkey, const ref * pvalue, dict_stack_t *pds) { dict *pdict = pdref->value.pdict; gs_ref_memory_t *mem = dict_memory(pdict); gs_memory_t *pmem = dict_mem(pdict); int rcode = 0; int code; ref *pvslot, kname; /* Check the value. */ store_check_dest(pdref, pvalue); top:if ((code = dict_find(pdref, pkey, &pvslot)) <= 0) { /* not found *//* Check for overflow */ uint index; switch (code) { case 0: break; case gs_error_dictfull: if (!pmem->gs_lib_ctx->dict_auto_expand) return_error(gs_error_dictfull); code = dict_grow(pdref, pds); if (code < 0) return code; goto top; /* keep things simple */ default: /* gs_error_typecheck */ return code; } index = pvslot - pdict->values.value.refs; /* If the key is a string, convert it to a name. */ if (r_has_type(pkey, t_string)) { int code; if (!r_has_attr(pkey, a_read)) return_error(gs_error_invalidaccess); code = name_from_string(pmem, pkey, &kname); if (code < 0) return code; pkey = &kname; } if (dict_is_packed(pdict)) { ref_packed *kp; if (!r_has_type(pkey, t_name) || name_index(pmem, pkey) > packed_name_max_index ) { /* Change to unpacked representation. */ int code = dict_unpack(pdref, pds); if (code < 0) return code; goto top; } kp = pdict->keys.value.writable_packed + index; if (ref_must_save_in(mem, &pdict->keys)) { /* See initial comment for why it is safe */ /* not to save the change if the keys */ /* array itself is new. */ ref_do_save_in(mem, &pdict->keys, kp, "dict_put(key)"); } *kp = pt_tag(pt_literal_name) + name_index(pmem, pkey); } else { ref *kp = pdict->keys.value.refs + index; if_debug2m('d', (const gs_memory_t *)mem, "[d]0x%lx: fill key at 0x%lx\n", (ulong) pdict, (ulong) kp); store_check_dest(pdref, pkey); ref_assign_old_in(mem, &pdict->keys, kp, pkey, "dict_put(key)"); /* set key of pair */ } ref_save_in(mem, pdref, &pdict->count, "dict_put(count)"); pdict->count.value.intval++; /* If the key is a name, update its 1-element cache. */ if (r_has_type(pkey, t_name)) { name *pname = pkey->value.pname; if (pname->pvalue == pv_no_defn && CAN_SET_PVALUE_CACHE(pds, pdref, mem) ) { /* Set the cache. */ if_debug0m('d', (const gs_memory_t *)mem, "[d]set cache\n"); pname->pvalue = pvslot; } else { /* The cache can't be used. */ if_debug0m('d', (const gs_memory_t *)mem, "[d]no cache\n"); pname->pvalue = pv_other; } } rcode = 1; } if_debug8m('d', (const gs_memory_t *)mem, "[d]0x%lx: put key 0x%lx 0x%lx\n value at 0x%lx: old 0x%lx 0x%lx, new 0x%lx 0x%lx\n", (ulong) pdref->value.pdict, ((const ulong *)pkey)[0], ((const ulong *)pkey)[1], (ulong) pvslot, ((const ulong *)pvslot)[0], ((const ulong *)pvslot)[1], ((const ulong *)pvalue)[0], ((const ulong *)pvalue)[1]); ref_assign_old_in(mem, &pdref->value.pdict->values, pvslot, pvalue, "dict_put(value)"); return rcode; }
/* Process a buffer of PCL XL commands. */ int px_process(px_parser_state_t * st, px_state_t * pxs, stream_cursor_read * pr) { const byte *orig_p = pr->ptr; const byte *next_p = orig_p; /* start of data not copied to saved */ const byte *p; const byte *rlimit; px_value_t *sp = &st->stack[st->stack_count]; #define stack_limit &st->stack[max_stack - 1] gs_memory_t *memory = st->memory; int code = 0; uint left; uint min_left; px_tag_t tag; const px_tag_syntax_t *syntax = 0; st->args.parser = st; st->parent_operator_count = 0; /* in case of error */ /* Check for leftover data from the previous call. */ parse:if (st->saved_count) { /* Fill up the saved buffer so we can make progress. */ int move = min(sizeof(st->saved) - st->saved_count, pr->limit - next_p); memcpy(&st->saved[st->saved_count], next_p + 1, move); next_p += move; p = st->saved - 1; rlimit = p + st->saved_count + move; } else { /* No leftover data, just read from the input. */ p = next_p; rlimit = pr->limit; } top:if (st->data_left) { /* We're in the middle of reading an array or data block. */ if (st->data_proc) { /* This is a data block. */ uint avail = min(rlimit - p, st->data_left); uint used; st->args.source.available = avail; st->args.source.data = p + 1; code = (*st->data_proc) (&st->args, pxs); /* If we get a 'remap_color' error, it means we are dealing with a * pattern, and the device supports high level patterns. So we must * use our high level pattern implementation. */ if (code == gs_error_Remap_Color) { code = px_high_level_pattern(pxs->pgs); code = (*st->data_proc) (&st->args, pxs); } used = st->args.source.data - (p + 1); #ifdef DEBUG if (gs_debug_c('I')) { px_value_t data_array; data_array.type = pxd_ubyte; data_array.value.array.data = p + 1; data_array.value.array.size = used; trace_array_data(pxs->memory, "data:", &data_array); } #endif p = st->args.source.data - 1; st->data_left -= used; if (code < 0) { st->args.source.position = 0; goto x; } else if ((code == pxNeedData) || (code == pxPassThrough && st->data_left != 0)) { code = 0; /* exit for more data */ goto x; } else { st->args.source.position = 0; st->data_proc = 0; if (st->data_left != 0) { code = gs_note_error(errorExtraData); goto x; } clear_stack(); } } else { /* This is an array. */ uint size = sp->value.array.size; uint scale = value_size(sp); uint nbytes = size * scale; byte *dest = (byte *) sp->value.array.data + nbytes - st->data_left; left = rlimit - p; if (left < st->data_left) { /* We still don't have enough data to fill the array. */ memcpy(dest, p + 1, left); st->data_left -= left; p = rlimit; code = 0; goto x; } /* Complete the array and continue parsing. */ memcpy(dest, p + 1, st->data_left); trace_array(memory, sp); p += st->data_left; } st->data_left = 0; } else if (st->data_proc) { /* An operator is awaiting data. */ /* Skip white space until we find some. */ code = 0; /* in case we exit */ /* special case - VendorUnique has a length attribute which we've already parsed and error checked */ if (st->data_proc == pxVendorUnique) { st->data_left = st->stack[st->attribute_indices[pxaVUDataLength]].value.i; goto top; } else { while ((left = rlimit - p) != 0) { switch ((tag = p[1])) { case pxtNull: case pxtHT: case pxtLF: case pxtVT: case pxtFF: case pxtCR: ++p; continue; case pxt_dataLength: if (left < 5) goto x; /* can't look ahead */ st->data_left = get_uint32(st, p + 2); if_debug2m('i', memory, "tag= 0x%2x data, length %u\n", p[1], st->data_left); p += 5; goto top; case pxt_dataLengthByte: if (left < 2) goto x; /* can't look ahead */ st->data_left = p[2]; if_debug2m('i', memory, "tag= 0x%2x data, length %u\n", p[1], st->data_left); p += 2; goto top; default: { code = gs_note_error(errorMissingData); goto x; } } } } } st->args.source.position = 0; st->args.source.available = 0; while ((left = rlimit - p) != 0 && left >= (min_left = (syntax = &tag_syntax[tag = p[1]])->min_input) ) { int count; #ifdef DEBUG if (gs_debug_c('i')) { dmprintf1(memory, "tag= 0x%02x ", tag); if (tag == pxt_attr_ubyte || tag == pxt_attr_uint16) { px_attribute_t attr = (tag == pxt_attr_ubyte ? p[2] : get_uint16(st, p + 2)); const char *aname = px_attribute_names[attr]; if (aname) dmprintf1(memory, " @%s\n", aname); else dmprintf1(memory, " attribute %u ???\n", attr); } else { const char *format; const char *tname; bool operator = false; if (tag < 0x40) format = "%s\n", tname = px_tag_0_names[tag]; else if (tag < 0xc0) format = "%s", tname = px_operator_names[tag - 0x40], operator = true; else { tname = px_tag_c0_names[tag - 0xc0]; if (tag < 0xf0) format = " %s"; /* data values follow */ else format = "%s\n"; } if (tname) { dmprintf1(memory, format, tname); if (operator) dmprintf1(memory, " (%ld)\n", st->operator_count + 1); } else dmputs(memory, "???\n"); } } #endif if ((st->macro_state & syntax->state_mask) != syntax->state_value) { /* * We should probably distinguish here between * out-of-context operators and illegal tags, but it's too * much trouble. */ code = gs_note_error(errorIllegalOperatorSequence); if (tag >= 0x40 && tag < 0xc0) st->last_operator = tag; goto x; } st->macro_state ^= syntax->state_transition; switch (tag >> 3) { case 0: switch (tag) { case pxtNull: ++p; continue; default: break; } break; case 1: switch (tag) { case pxtHT: case pxtLF: case pxtVT: case pxtFF: case pxtCR: ++p; continue; default: break; } break; case 3: if (tag == pxt1b) { /* ESC *//* Check for UEL */ if (memcmp(p + 1, "\033%-12345X", min(left, 9))) break; /* not UEL, error */ if (left < 9) goto x; /* need more data */ p += 9; code = e_ExitLanguage; goto x; } break; case 4: switch (tag) { case pxtSpace: /* break; will error, compatible with lj */ /* ++p;continue; silently ignores the space */ ++p; continue; default: break; } break; case 8: case 9: case 10: case 11: case 12: case 13: case 14: case 15: case 16: case 17: case 18: case 19: case 20: case 21: case 22: case 23: /* Operators */ /* Make sure that we have all the required attributes, */ /* and no attributes that are neither required nor */ /* optional. (It's up to the operator to make any */ /* more precise checks than this. */ st->operator_count++; /* if this is a passthrough operator we have to tell the passthrough module if this operator was preceded by another passthrough operator or a different xl operator */ if (tag == pxtPassThrough) { pxpcl_passthroughcontiguous(st->last_operator == tag); } else if (st->last_operator == pxtPassThrough) { pxpcl_endpassthroughcontiguous(pxs); } st->last_operator = tag; { const px_operator_definition_t *pod = &px_operator_definitions[tag - 0x40]; int left = sp - st->stack; const byte /*px_attribute_t */ * pal = pod->attrs; px_value_t **ppv = st->args.pv; bool required = true; code = 0; /* * Scan the attributes. Illegal attributes take priority * over missing attributes, which in turn take priority * over illegal data types. */ for (;; ++pal, ++ppv) { px_attribute_t attr = *pal; uint index; if (!attr) { /* * We've reached the end of either the required or * the optional attribute list. */ if (!required) break; required = false; --ppv; /* cancel incrementing */ continue; } if ((index = st->attribute_indices[attr]) == 0) { if (required) code = gs_note_error(errorMissingAttribute); else *ppv = 0; } else { /* Check the attribute data type and value. */ px_value_t *pv = *ppv = &st->stack[index]; const px_attr_value_type_t *pavt = &px_attr_value_types[attr]; int acode; if ((~pavt->mask & pv->type & (pxd_structure | pxd_representation)) || (pavt->mask == (pxd_scalar | pxd_ubyte) && (pv->value.i < 0 || pv->value.i > pavt->limit)) ) { if (code >= 0) code = gs_note_error (errorIllegalAttributeDataType); } if (pavt->proc != 0 && (acode = (*pavt->proc) (pv)) < 0) { if (code >= 0) code = acode; } --left; } } /* Make sure there are no attributes left over. */ if (left) code = gs_note_error(errorIllegalAttribute); if (code >= 0) { st->args.source.phase = 0; code = (*pod->proc) (&st->args, pxs); /* If we get a 'remap_color' error, it means we are dealing with a * pattern, and the device supports high level patterns. So we must * use our high level pattern implementation. */ if (code == gs_error_Remap_Color) { code = px_high_level_pattern(pxs->pgs); if (code < 0) goto x; code = (*pod->proc) (&st->args, pxs); } } if (code < 0) goto x; /* Check whether the operator wanted source data. */ if (code == pxNeedData) { if (!pxs->data_source_open) { code = gs_note_error(errorDataSourceNotOpen); goto x; } st->data_proc = pod->proc; ++p; goto top; } } clear_stack(); ++p; continue; case 24: sp[1].type = pxd_scalar; count = 1; goto data; case 26: sp[1].type = pxd_xy; count = 2; goto data; case 28: sp[1].type = pxd_box; count = 4; goto data; /* Scalar, point, and box data */ data:{ int i; if (sp == stack_limit) { code = gs_note_error(errorInternalOverflow); goto x; } ++sp; sp->attribute = 0; p += 2; #ifdef DEBUG # define trace_scalar(mem, format, cast, alt)\ if ( gs_debug_c('i') )\ trace_data(mem, format, cast, sp->value.alt, count) #else # define trace_scalar(mem, format, cast, alt) DO_NOTHING #endif switch (tag & 7) { case pxt_ubyte & 7: sp->type |= pxd_ubyte; for (i = 0; i < count; ++p, ++i) sp->value.ia[i] = *p; dux:trace_scalar(pxs->memory, " %lu", ulong, ia); --p; continue; case pxt_uint16 & 7: sp->type |= pxd_uint16; for (i = 0; i < count; p += 2, ++i) sp->value.ia[i] = get_uint16(st, p); goto dux; case pxt_uint32 & 7: sp->type |= pxd_uint32; for (i = 0; i < count; p += 4, ++i) sp->value.ia[i] = get_uint32(st, p); goto dux; case pxt_sint16 & 7: sp->type |= pxd_sint16; for (i = 0; i < count; p += 2, ++i) sp->value.ia[i] = get_sint16(st, p); dsx:trace_scalar(pxs->memory, " %ld", long, ia); --p; continue; case pxt_sint32 & 7: sp->type |= pxd_sint32; for (i = 0; i < count; p += 4, ++i) sp->value.ia[i] = get_sint32(st, p); goto dsx; case pxt_real32 & 7: sp->type |= pxd_real32; for (i = 0; i < count; p += 4, ++i) sp->value.ra[i] = get_real32(st, p); trace_scalar(pxs->memory, " %g", double, ra); --p; continue; default: break; } } break; case 25: /* Array data */ { const byte *dp; uint nbytes; if (sp == stack_limit) { code = gs_note_error(errorInternalOverflow); goto x; } switch (p[2]) { case pxt_ubyte: sp[1].value.array.size = p[3]; dp = p + 4; break; case pxt_uint16: if (left < 4) { if_debug0m('i', memory, "...\n"); /* Undo the state transition. */ st->macro_state ^= syntax->state_transition; goto x; } sp[1].value.array.size = get_uint16(st, p + 3); dp = p + 5; break; default: st->last_operator = tag; /* for error message */ code = gs_note_error(errorIllegalTag); goto x; } nbytes = sp[1].value.array.size; if_debug1m('i', memory, "[%u]\n", sp[1].value.array.size); switch (tag) { case pxt_ubyte_array: sp[1].type = pxd_array | pxd_ubyte; array:++sp; if (st->big_endian) sp->type |= pxd_big_endian; sp->value.array.data = dp; sp->attribute = 0; /* Check whether we have enough data for the entire */ /* array. */ if (rlimit + 1 - dp < nbytes) { /* Exit now, continue reading when we return. */ uint avail = rlimit + 1 - dp; code = px_save_array(sp, pxs, "partial array", avail); if (code < 0) goto x; sp->type |= pxd_on_heap; st->data_left = nbytes - avail; st->data_proc = 0; p = rlimit; goto x; } p = dp + nbytes - 1; trace_array(memory, sp); continue; case pxt_uint16_array: sp[1].type = pxd_array | pxd_uint16; a16:nbytes <<= 1; goto array; case pxt_uint32_array: sp[1].type = pxd_array | pxd_uint32; a32:nbytes <<= 2; goto array; case pxt_sint16_array: sp[1].type = pxd_array | pxd_sint16; goto a16; case pxt_sint32_array: sp[1].type = pxd_array | pxd_sint32; goto a32; case pxt_real32_array: sp[1].type = pxd_array | pxd_real32; goto a32; default: break; } break; } break; case 31: { px_attribute_t attr; const byte *pnext; switch (tag) { case pxt_attr_ubyte: attr = p[2]; pnext = p + 2; goto a; case pxt_attr_uint16: attr = get_uint16(st, p + 2); pnext = p + 3; a:if (attr >= px_attribute_next) break; /* * We could check the attribute value type here, but * in order to match the behavior of the H-P printers, * we don't do it until we see the operator. * * It is legal to specify the same attribute more than * once; the last value has priority. If this happens, * since the order of attributes doesn't matter, we can * just replace the former value on the stack. */ sp->attribute = attr; if (st->attribute_indices[attr] != 0) { px_value_t *old_sp = &st->stack[st->attribute_indices[attr]]; /* If the old value is on the heap, free it. */ if (old_sp->type & pxd_on_heap) gs_free_object(memory, (void *)old_sp->value. array.data, "old value for duplicate attribute"); *old_sp = *sp--; } else st->attribute_indices[attr] = sp - st->stack; p = pnext; continue; case pxt_dataLength: /* * Unexpected data length operators are normally not * allowed, but there might be a zero-length data * block immediately following a zero-size image, * which doesn't ask for any data. */ if (uint32at(p + 2, true /*arbitrary */ ) == 0) { p += 5; continue; } break; case pxt_dataLengthByte: /* See the comment under pxt_dataLength above. */ if (p[2] == 0) { p += 2; continue; } break; default: break; } } break; default: break; } /* Unknown tag value. Report an error. */ st->last_operator = tag; /* for error message */ code = gs_note_error(errorIllegalTag); break; } x: /* Save any leftover input. */ left = rlimit - p; if (rlimit != pr->limit) { /* We were reading saved input. */ if (left <= next_p - orig_p) { /* We finished reading the previously saved input. */ /* Continue reading current input, unless we got an error. */ p = next_p -= left; rlimit = pr->limit; st->saved_count = 0; if (code >= 0) goto parse; } else { /* There's still some previously saved input left over. */ memmove(st->saved, p + 1, st->saved_count = left); p = next_p; rlimit = pr->limit; left = rlimit - p; } } /* Except in case of error, save any remaining input. */ if (code >= 0) { if (left + st->saved_count > sizeof(st->saved)) { /* Fatal error -- shouldn't happen! */ code = gs_note_error(errorInternalOverflow); st->saved_count = 0; } else { memcpy(&st->saved[st->saved_count], p + 1, left); st->saved_count += left; p = rlimit; } } pr->ptr = p; st->stack_count = sp - st->stack; /* Move to the heap any arrays whose data was being referenced */ /* directly in the input buffer. */ for (; sp > st->stack; --sp) if ((sp->type & (pxd_array | pxd_on_heap)) == pxd_array) { int code = px_save_array(sp, pxs, "px stack array to heap", sp->value.array.size * value_size(sp)); if (code < 0) break; sp->type |= pxd_on_heap; } if (code < 0 && syntax != 0) { /* Undo the state transition. */ st->macro_state ^= syntax->state_transition; } return code; }