示例#1
0
static pdf_obj *
create_cspace_ICCBased (png_structp png_ptr, png_infop info_ptr)
{
  pdf_obj   *colorspace;
  int        csp_id, colortype;
  png_byte   color_type;
  png_charp  name;
  int        compression_type;  /* Manual page for libpng does not
				 * clarify whether profile data is inflated by libpng.
				 */
#if PNG_LIBPNG_VER_MINOR < 5
  png_charp   profile;
#else
  png_bytep   profile;
#endif
  png_uint_32 proflen;

  if (!png_get_valid(png_ptr, info_ptr, PNG_INFO_iCCP) ||
      !png_get_iCCP(png_ptr, info_ptr, &name, &compression_type, &profile, &proflen))
    return NULL;

  color_type = png_get_color_type(png_ptr, info_ptr);

  if (color_type & PNG_COLOR_MASK_COLOR) {
    colortype = PDF_COLORSPACE_TYPE_RGB;
#if 0
    alternate = create_cspace_CalRGB(png_ptr, info_ptr);
#endif
  } else {
    colortype = PDF_COLORSPACE_TYPE_GRAY;
#if 0
    alternate = create_cspace_CalGray(png_ptr, info_ptr);
#endif
  }

#if 0
  if (alternate)
    pdf_add_dict(dict, pdf_new_name("Alternate"), alternate);
#endif

  if (iccp_check_colorspace(colortype, profile, proflen) < 0)
    colorspace = NULL;
  else {
    csp_id = iccp_load_profile(name, profile, proflen);
    if (csp_id < 0) {
      colorspace = NULL;
    } else {
      colorspace = pdf_get_colorspace_reference(csp_id);
    }
  }

  /* Rendering intent ... */

  return colorspace;
}
示例#2
0
int
jpeg_include_image (pdf_ximage *ximage, FILE *fp)
{
  pdf_obj         *stream;
  pdf_obj         *stream_dict;
  pdf_obj         *colorspace;
  int              colortype;
  ximage_info      info;
  struct JPEG_info j_info;

  if (!texpdf_check_for_jpeg(fp)) {
    WARN("%s: Not a JPEG file?", JPEG_DEBUG_STR);
    rewind(fp);
    return -1;
  }
  /* File position is 2 here... */

  texpdf_ximage_init_image_info(&info);

  JPEG_info_init(&j_info);

  if (JPEG_scan_file(&j_info, fp) < 0) {
    WARN("%s: Not a JPEG file?", JPEG_DEBUG_STR);
    JPEG_info_clear(&j_info);
    return -1;
  }

  switch (j_info.num_components) {
  case 1:
    colortype = PDF_COLORSPACE_TYPE_GRAY;
    break;
  case 3:
    colortype = PDF_COLORSPACE_TYPE_RGB;
    break;
  case 4:
    colortype = PDF_COLORSPACE_TYPE_CMYK;
    break;
  default:
    WARN("%s: Unknown color space (num components: %d)", JPEG_DEBUG_STR, info.num_components);
    JPEG_info_clear(&j_info);
    return -1;
  }

  /* JPEG image use DCTDecode. */
  stream      = texpdf_new_stream (0);
  stream_dict = texpdf_stream_dict(stream);
  texpdf_add_dict(stream_dict,
	       texpdf_new_name("Filter"), texpdf_new_name("DCTDecode"));

  /* XMP Metadata */
  if (texpdf_get_version() >= 4) {
    if (j_info.flags & HAVE_APPn_XMP) {
      pdf_obj *XMP_stream;

      XMP_stream = JPEG_get_XMP(&j_info);
      texpdf_add_dict(stream_dict,
                   texpdf_new_name("Metadata"), texpdf_ref_obj(XMP_stream));
      texpdf_release_obj(XMP_stream);
    }
  }

  /* Check embedded ICC Profile */
  colorspace  = NULL;
  if (j_info.flags & HAVE_APPn_ICC) {
    pdf_obj *icc_stream, *intent;
    int      cspc_id;

    icc_stream = JPEG_get_iccp(&j_info);
    if (!icc_stream)
      colorspace = NULL;
    else {
      if (iccp_check_colorspace(colortype,
          pdf_stream_dataptr(icc_stream), pdf_stream_length (icc_stream)) < 0)
        colorspace = NULL;
      else {
        cspc_id = iccp_load_profile(NULL, /* noname */
                                    pdf_stream_dataptr(icc_stream),
                                    pdf_stream_length (icc_stream));
        if (cspc_id < 0)
          colorspace = NULL;
        else {
          colorspace = texpdf_get_colorspace_reference(cspc_id);
          intent     = iccp_get_rendering_intent(pdf_stream_dataptr(icc_stream),
                                                 pdf_stream_length (icc_stream));
          if (intent)
            texpdf_add_dict(stream_dict, texpdf_new_name("Intent"), intent);
        }
      }
      texpdf_release_obj(icc_stream);
    }
  }
  /* No ICC or invalid ICC profile. */
  if (!colorspace) {
    switch (colortype) {
    case PDF_COLORSPACE_TYPE_GRAY:
      colorspace = texpdf_new_name("DeviceGray");
      break;
    case PDF_COLORSPACE_TYPE_RGB:
      colorspace = texpdf_new_name("DeviceRGB");
      break;
    case PDF_COLORSPACE_TYPE_CMYK:
      colorspace = texpdf_new_name("DeviceCMYK");
      break;
    }
  }
  texpdf_add_dict(stream_dict, texpdf_new_name("ColorSpace"), colorspace);

#define IS_ADOBE_CMYK(j) (((j).flags & HAVE_APPn_ADOBE) && (j).num_components == 4)
  if (IS_ADOBE_CMYK(j_info)) {
    pdf_obj *decode;
    int      i;

    WARN("Adobe CMYK JPEG: Inverted color assumed.");
    decode = texpdf_new_array();
    for (i = 0; i < j_info.num_components; i++) {
      texpdf_add_array(decode, texpdf_new_number(1.0));
      texpdf_add_array(decode, texpdf_new_number(0.0));
    }
    texpdf_add_dict(stream_dict, texpdf_new_name("Decode"), decode);
  }

  /* Copy file */
  JPEG_copy_stream(&j_info, stream, fp);

  info.width              = j_info.width;
  info.height             = j_info.height;
  info.bits_per_component = j_info.bits_per_component;
  info.num_components     = j_info.num_components;

  jpeg_get_density(&j_info, &info.xdensity, &info.ydensity);

  texpdf_ximage_set_image(ximage, &info, stream);
  JPEG_info_clear(&j_info);

  return 0;
}