#!/usr/bin/perl
# $Id: image_resources.pl,v 1.3 2010/07/22 19:54:50 rjs Exp $
#
# Resource-based image extractor based on PDFlib TET
#

use PDFlib::TET 3.0;
use strict;


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

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

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

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

# set this to 1 to generate image data in memory */

eval  {
    my $tet;

    if ($#ARGV < 0 || $#ARGV > 1) {
	die("usage: image_resource.pl <filename>\n");
    }

    $tet = new PDFlib::TET;

    my $outfilebase = $ARGV[0];

    $tet->set_option($globaloptlist);

    my $doc = $tet->open_document($ARGV[0], $docoptlist);

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

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

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

	$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 */
    my $n_images = $tet->pcos_get_number($doc, "length:images");

    # loop over image resources in the document */
    for (my $imageid = 0; $imageid < $n_images; ++$imageid)
    {
	# examine image type 
	my $mergetype = $tet->pcos_get_number($doc,
			    "images[$imageid]/mergetype");

	# skip images which have been consumed by merging
	if ($mergetype == 0 || $mergetype == 1) {
	    my $imageoptlist;
	    my ($width, $height, $bpc, $cs);

	    # 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
	    # - mergetype if different from "normal", i.e. "artificial"
	    #   (=merged) or "consumed"
	    #/
	    $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);
};

if ($@) {
    printf("PDFlib Exception occurred:\n");
    printf("Error $@\n");
    exit;
}
