#!/usr/bin/perl -w # ppmtopgm.pl - Don Yang (uguu.org) # # Different versions of ppmtopgm produced slightly different outputs. # To make PGM tests deterministic, we use our own version of ppmtopgm. # # 12/25/11 use strict; my @lines = <>; # Check header if( $#lines < 3 || $lines[0] !~ /^P3/ || $lines[1] !~ /^\d+\s+\d+/ || $lines[2] !~ /^255/ ) { die "Error parsing header\n"; } # Copy header to output print "P2\n", $lines[1], $lines[2]; # Convert pixels for(my $i = 3; $i <= $#lines; $i++) { while( $lines[$i] =~ s/^(\d+)\s+(\d+)\s+(\d+)(\s+)(.*)/$5/s ) { my $p = int(($1 * 299 + $2 * 587 + $3 * 114) / 1000); if( $p > 255 ) { $p = 255; } print $p, $4; } }