Exemple #1
0
PDFLIB_API int PDFLIB_CALL
PDF_open_image_file(
    PDF *p,
    const char *type,
    const char *filename,
    const char *stringparam,
    int intparam)
{
    static const char fn[] = "PDF_open_image_file";
    int imageslot;
    int ret;

    PDF_TRACE(("%s(pdf[%p], \"%s\", \"%s\", \"%s\", %d);",
	fn, (void *) p, type, filename, stringparam, intparam));

    if (PDF_SANITY_CHECK_FAILED(p))
	return -1;

    PDF_CHECK_SCOPE(p, fn, pdf_state_document | pdf_state_page);

    if (type == NULL || *type == '\0')
	pdf_error(p, PDF_ValueError,
	    "Bad image file type for image %s", filename);

    if (filename == NULL || *filename == '\0')
	pdf_error(p, PDF_ValueError, "Bad image file name for %s image", type);

    if (stringparam &&
    	p->compatibility == PDF_1_2 && !strcmp(stringparam, "masked"))
	pdf_error(p, PDF_RuntimeError,
		"Masked images are not supported in PDF 1.2");

    for (imageslot = 0; imageslot < p->images_capacity; imageslot++)
	if (!p->images[imageslot].in_use)		/* found free slot */
	    break;

    if (imageslot == p->images_capacity) 
	pdf_grow_images(p);

    if (!strcmp(type, "png") || !strcmp(type, "PNG")) {
	ret = pdf_open_PNG_data(p, imageslot, filename, stringparam, intparam);

	if (ret == -1)
	    pdf_init_image_struct(p, &p->images[imageslot]);
	PDF_TRACE((" [%d]\n", ret));
	return ret;
    }

    if (!strcmp(type, "gif") || !strcmp(type, "GIF")) {
	ret = pdf_open_GIF_data(p, imageslot, filename, stringparam, intparam);

	if (ret == -1)
	    pdf_init_image_struct(p, &p->images[imageslot]);
	PDF_TRACE((" [%d]\n", ret));
	return ret;
    }

    if (!strcmp(type, "tiff") || !strcmp(type, "TIFF")) {
	ret = pdf_open_TIFF_data(p, imageslot, filename, stringparam, intparam);

	if (ret == -1)
	    pdf_init_image_struct(p, &p->images[imageslot]);
	PDF_TRACE((" [%d]\n", ret));
	return ret;
    }

    if (!strcmp(type, "jpeg") || !strcmp(type, "JPEG")) {
	ret = pdf_open_JPEG_data(p, imageslot, filename, stringparam, intparam);

	if (ret == -1)
	    pdf_init_image_struct(p, &p->images[imageslot]);
	PDF_TRACE((" [%d]\n", ret));
	return ret;
    }

    pdf_error(p, PDF_ValueError,
	"Image type %s for image file %s not supported", type, filename);

    /* never reached */
    PDF_TRACE((" [%d]\n", -1));
    return -1;
}
Exemple #2
0
PDFLIB_API int PDFLIB_CALL
PDF_open_CCITT(PDF *p, const char *filename, int width, int height,
		int BitReverse, int K, int BlackIs1)
{
    pdf_image *image;
    int im;
    char scratch[30];

    if (PDF_SANITY_CHECK_FAILED(p))
	return -1;

    for (im = 0; im < p->images_capacity; im++)
	if (!p->images[im].in_use)		/* found free slot */
	    break;

    if (im == p->images_capacity) 
	pdf_grow_images(p);

    image = &p->images[im];

    if ((image->fp = fopen(filename, READMODE)) == NULL) {
	if (p->debug['i']) {
	    pdf_error(p, PDF_NonfatalError, "Couldn't open CCITT file '%s'", filename);
	}
        return -1;
    }

    /* Grab the image parameters and pack them into image struct */
    image->filename     		= pdf_strdup(p, filename);

    /* CCITT specific information */
    image->width          		= width;
    image->height         		= height;
    image->info.ccitt.BitReverse	= BitReverse;

    if (BlackIs1 == 0 && K == 0)	/* default values */
	image->params = NULL;
    else {
	scratch[0] = '\0';
	if (K != 0) {
	    sprintf(scratch, "/K %d", K);
	}
	if (BlackIs1 == 1)
	    strcat(scratch, "/BlackIs1 true");
	image->params = pdf_strdup(p, scratch);
    }

    /* The following are fixed for CCITT images */
    image->compression  	= ccitt;
    image->colorspace		= DeviceGray;
    image->components		= 1;
    image->bpc			= 1;

    image->src.init             = pdf_data_source_CCITT_init;
    image->src.fill             = pdf_data_source_CCITT_fill;
    image->src.terminate        = pdf_data_source_CCITT_terminate;
    image->src.private_data     = (void *) image;

    image->in_use 		= pdf_true;		/* mark slot as used */

    pdf_put_image(p, im, pdf_true);
    fclose(image->fp);

    return im;
}
Exemple #3
0
PDFLIB_API int PDFLIB_CALL
PDF_open_image(
    PDF *p,
    const char *type,
    const char *source,
    const char *data,
    long length,
    int width,
    int height,
    int components,
    int bpc,
    const char *params)
{
    static const char fn[] = "PDF_open_image";
    pdf_image *image;
    int im;

    PDF_TRACE(("%s\t(pdf[%p], \"%s\", \"%s\", source[%p], ",
    	fn, (void *) p, type, source, (void *) data));

    PDF_TRACE(("%ld, %d, %d, %d, %d, \"%s\");",
    	length, width, height, components, bpc, params));

    if (PDF_SANITY_CHECK_FAILED(p))
	return -1;

    PDF_CHECK_SCOPE(p, fn, pdf_state_document | pdf_state_page);

    if (type == NULL || *type == '\0')
    	pdf_error(p, PDF_ValueError, "No image type in PDF_open_image");

    if (source == NULL || *source == '\0')
    	pdf_error(p, PDF_ValueError, "No image source in PDF_open_image");

    if (!strcmp(type, "raw") && data == NULL)
    	pdf_error(p, PDF_ValueError, "Bad raw image pointer in PDF_open_image");

    if (strcmp(type, "ccitt") && strcmp(type, "raw")
	    && params != NULL && *params != '\0')
    	pdf_error(p, PDF_NonfatalError,
	    "Unnecessary CCITT parameter in PDF_open_image");

    for (im = 0; im < p->images_capacity; im++)
	if (!p->images[im].in_use)		/* found free slot */
	    break;

    if (im == p->images_capacity) 
	pdf_grow_images(p);

    image = &p->images[im];

    if (!strcmp(type, "jpeg")) {
	image->compression = dct;
	image->use_raw	= pdf_true;

    } else if (!strcmp(type, "ccitt")) {
	image->compression = ccitt;
	image->use_raw	= pdf_true;

	if (length < 0L) {
	    image->info.ccitt.BitReverse = pdf_true;
	    length = -length;
	}

	if (params != NULL && *params != '\0')
	    image->params = pdf_strdup(p, params);
	else
	    image->params = NULL;

    } else if (!strcmp(type, "raw")) {
	image->compression = none;

    } else
	pdf_error(p, PDF_ValueError,
	    "Unknown image type '%s' in PDF_open_image", type);

    switch (components) {
	case 1:
	    if (params && !strcmp(params, "mask")) {
		if (strcmp(type, "raw") || bpc != 1)
		    pdf_error(p, PDF_ValueError,
			"Unsuitable image mask in PDF_open_image");
		image->colorspace = ImageMask;
	    } else
		image->colorspace = DeviceGray;
	    break;

	case 3:
	    image->colorspace = DeviceRGB;
	    break;

	case 4:
	    image->colorspace = DeviceCMYK;
	    break;

	default:
	    pdf_error(p, PDF_ValueError,
	    	"Bogus number of components (%d) in PDF_open_image",
		components);
    }

    image->width		= (float) width;
    image->height		= (float) height;
    image->bpc			= bpc;
    image->components		= components;
    image->in_use		= pdf_true;		/* mark slot as used */

    if (!strcmp(source, "memory")) {	/* in-memory image data */
	if (image->compression == none && length != (long)
	    	(height * ((width * components * bpc + 7) / 8)))
	    pdf_error(p, PDF_ValueError,
		"Bogus image data length '%ld' in PDF_open_image", length);

	image->src.init		= pdf_noop;
	image->src.fill		= pdf_data_source_buf_fill;
	image->src.terminate	= pdf_noop;

	image->src.buffer_start	= (unsigned char *) data;
	image->src.buffer_length= (size_t) length;

	image->src.bytes_available = 0;
	image->src.next_byte	= NULL;

    } else if (!strcmp(source, "memory32")) {
	/* 32 bit aligned in-memory image data */
	FILL_ALIGN fa;

	if (image->compression == none && length != (long)
	    	(height * ((width * components * bpc + 7) / 8)))
	    pdf_error(p, PDF_ValueError,
		"Bogus image data length '%ld' in PDF_open_image", length);

	image->src.buffer_start	= (unsigned char *) data;
	image->src.buffer_length= (size_t) length;

	image->src.init		= pdf_noop;
	image->src.fill		= pdf_data_source_buf_fill_aligned;
	image->src.terminate	= pdf_noop;

	fa.cur_scanline = 0;
	fa.num_scanlines = (size_t) height;
	fa.scanline_widthbytes = (size_t) ((width * components * bpc + 7) / 8);
	/* dword align */
	fa.scanline_widthbytes_aligned = (fa.scanline_widthbytes + 3) & ~0x3;

	image->src.private_data = (void *) &fa;
	image->src.bytes_available = 0;
	image->src.next_byte	= NULL;

    } else if (!strcmp(source, "fileref")) {	/* file reference */
	if (p->compatibility == PDF_1_2)
	    pdf_error(p, PDF_RuntimeError,
		"External image file references are not supported in PDF 1.2");

	image->reference	= pdf_ref_file;
	image->filename		= pdf_strdup(p, data);

    } else if (!strcmp(source, "url")) {	/* url reference */
	if (p->compatibility == PDF_1_2)
	    pdf_error(p, PDF_RuntimeError,
		"External image URLs are not supported in PDF 1.2");

	image->reference	= pdf_ref_url;
	image->filename		= pdf_strdup(p, data);

    } else			/* error */
	pdf_error(p, PDF_ValueError,
	    "Bogus image data source '%s' in PDF_open_image", source);

    pdf_put_image(p, im, pdf_true);

    PDF_TRACE((" [%d]\n", im));

    return im;
}