<?php
/* $Id: image_resources.php,v 1.2.2.1 2010/07/28 12:38:55 rjs Exp $
 *
 * Resource-based image extractor based on PDFlib TET
 */


/* global option list */
$globaloptlist = "searchpath={{../data}}";

/* document-specific option list */
$docoptlist = "";

/* page-specific option list */
$pageoptlist = "";

/* here you can insert basic image extract options (more below) */
$baseimageoptlist = "";

try {
    $infilename = "FontReporter.pdf";


    $tet = new TET();

    $outfilebase = $infilename;

    $tet->set_option($globaloptlist);

    $doc = $tet->open_document($infilename, $docoptlist);

    if ($doc == -1) {
	die("Error ". $tet->get_errnum() . " in " . $tet->get_apiname()
	    . "(): " . $tet->get_errmsg() . "\n");
    }

    /* Images will only be merged upon opening a page.
     * In order to enumerate all merged image resources
     * we open all pages before extracting the images.
     */

    /* get number of pages in the document */
    $n_pages = $tet->pcos_get_number($doc, "length:pages");

    /* loop over pages in the document */
    for ($pageno = 1; $pageno <= $n_pages; ++$pageno) {

	$page = $tet->open_page($doc, $pageno, $pageoptlist);

	if ($page == -1) {
	    print("Error ". $tet->get_errnum() ." in ". $tet->get_apiname()
		. "(): " . $tet->get_errmsg() . "\n");
	    next;                        /* try next page */
	}

	if ($tet->get_errnum() != 0) {
	    print("Error ". $tet->get_errnum() . " in " . 
		    $tet->get_apiname() . "(): on page $pageno" 
		    . $tet->get_errmsg() . "\n");
	}

	$tet->close_page($page);
    }

    /* get number of image resources in the document */
    $n_images = $tet->pcos_get_number($doc, "length:images");

    /* loop over pages in the document */
    for ($imageid = 0; $imageid < $n_images; ++$imageid) {

	/* examine image type */
	$mergetype = $tet->pcos_get_number($doc, "images[$imageid]/mergetype");

	/* skip mage resourcesimages which have been consumed by merging */
	if ($mergetype == 0 || $mergetype == 1)
	{

	    /* Print the following information for each image:
	     * - page and image number
	     * - pCOS id (required for indexing the images[] array)
	     * - physical size of the placed image on the page
	     * - pixel size of the underlying PDF image
	     * - number of components, bits per component, and colorspace
	     */
	    $width = $tet->pcos_get_number($doc, "images[$imageid]/Width");
	    $height = $tet->pcos_get_number($doc, "images[$imageid]/Height");
	    $bpc = $tet->pcos_get_number($doc, "images[$imageid]/bpc");
	    $cs = $tet->pcos_get_number($doc, "images[$imageid]/colorspaceid");

	    printf("image I%d: %dx%d pixel, ", $imageid, $width, $height);

	    if ($cs != -1) {
		printf("%dx%d bit %s",
		    $tet->pcos_get_number($doc,
			    "colorspaces[$cs]/components"), $bpc,
		    $tet->pcos_get_string($doc, "colorspaces[$cs]/name"));
	    } else {
		/* cs==-1 may happen for some JPEG 2000 images. bpc,
		 * colorspace name and number of components are not
		 * available in this case.
		 */
		printf("JPEG2000");
	    }
	    if ($mergetype != 0) {
		printf(", mergetype=");

		if (mergetype == 1) {
		    printf("artificial");
		} else {
		    printf("consumed");
		}
	    }
	    printf("\n");

	}

	/*
	 * Fetch the image data and write it to a disk file. The
	 * output filenames are generated from the input filename
	 * by appending page number and image number.
	 */
	$imageoptlist = sprintf("%s filename={%s_I%d}",
	    $baseimageoptlist, $outfilebase, $imageid);

	if ($tet->write_image_file($doc, $imageid, $imageoptlist) == -1) {
	    print("Error ". $tet->get_errnum() . " in " . 
		    $tet->get_apiname() . "(): " 
		    . $tet->get_errmsg() . "\n");
	    next;                  /* process next image */
	}
    }


    $tet->close_document($doc);
}
catch (TETException $e) {
    if ($pageno == 0) {
	die("TET exception occurred in extractor sample:\n" .
	    "[" . $e->get_errnum() . "] " . $e->get_apiname() . ": " .
	    $e->get_errmsg() . "\n");
    } else {
	die("TET exception occurred in extractor sample:\n" .
	    "[" . $e->get_errnum() . "] " . $e->get_apiname() .
	    "on page $pageno: " .  $e->get_errmsg() . "\n");
    }
}
catch (Exception $e) {
    die($e);
}

$tet = 0;
?>
