#!/usr/bin/perl
#
# Rechercher le profil de couleurs utilisés dans un document PDF.
#
# perl pdf-profil.pl <fichier.pdf>
#
# Script par BoboTiG - 23 septembre 2009
# Révision : /
#


use strict;
use warnings;
use utf8;


# Sub to clear the console
sub clear_console {
	if ( $^O eq 'dos' || $^O eq 'MSWin32' || $^O eq 'os2' ) {
		system('cls');
	} else {
		system('clear');
	}
}


# Determine if the file is a PDF file
sub is_pdf_file {
	open(my $svg, '<', "@_") || die('Impossible d\'ouvrir le fichier.'."\n\n");
		while ( <$svg> ) {
			if ( $_ =~ '%PDF' )  {
				return 1;
			}
		}
	close($svg);
	
	return 0;
}


if (  @ARGV != 1  ) {
	die('perl pdf-profil.pl <fichier.pdf>'."\n");
} else {
	clear_console();
	print "\n".'*** Recherche le profil de couleurs d\'un document PDF ***'."\n\n";
	
	my $split_char;
	if ( $^O eq 'dos' || $^O eq 'MSWin32' || $^O eq 'os2' ) {
		$split_char = '\\\\';
	} else {
		$split_char = '/';
	}
	
	if ( $ARGV[0]=~ $split_char ) {
		my @file = split(/$split_char/, $ARGV[0]);
		print '[+] Fichier : '.$file[$#file]."\n";
	} else {
		print '[+] Fichier : '.$ARGV[0]."\n";
	}

	my $found = 0;
	my @polices;

	if ( is_pdf_file("$ARGV[0]") ) {
		open(my $pdf, '<', "$ARGV[0]");
			while ( <$pdf> ) {
				if ( $_ =~ '/ColorSpace' ) {
					$_ =~ '/ColorSpace(.+)';
					my $profil = $+;
					
					if ( $profil ) {
						if ( lc($profil) =~ 'rgb' ) {
							$found = 1;
							print '[+] Profil  : RGB.'."\n";
						} elsif ( lc($profil) =~ 'cmyk' || lc($profil) =~ 'cmjn' ) {
							$found = 1;
							print '[+] Profil  : CMJK.'."\n";
						} elsif ( lc($profil) =~ 'gray' ) {
							$found = 1;
							print '[+] Profil  : Niveaux de gris.'."\n";
						}
						last;
					}
				}
			}
		close($pdf);
		
		if ( !$found ) {
			print '[!] Profil  : introuvable.'."\n";
		}
	} else {
		print 'Il ne s\'agit pas d\'un fichier PDF.'."\n";
	}

	print "\n";
}

exit;

