/usr/share/perl5/Gscan2pdf/NetPBM.pm is in gscan2pdf 2.1.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | package Gscan2pdf::NetPBM;
use strict;
use warnings;
use Readonly;
Readonly my $BINARY_BITMAP => 4;
Readonly my $BINARY_GRAYMAP => 5;
Readonly my $BITS_PER_BYTE => 8;
Readonly my $BITMAP_BYTES_PER_PIXEL => 1 / $BITS_PER_BYTE;
Readonly my $GRAYMAP_CHANNELS => 1;
Readonly my $PIXMAP_CHANNELS => 3;
our $VERSION = '2.1.0';
# Return file size expected by PNM header
sub file_size_from_header {
my $filename = shift;
my ( $magic_value, $width, $height, $bytes_per_channel, $header ) =
read_header($filename);
if ( not defined $magic_value ) { return 0 }
if ( $magic_value == $BINARY_BITMAP ) {
my $mod = $width % $BITS_PER_BYTE;
if ( $mod > 0 ) { $width += $BITS_PER_BYTE - $mod }
}
my $datasize = $width * $height * $bytes_per_channel * (
$magic_value == $BINARY_BITMAP ? $BITMAP_BYTES_PER_PIXEL
: (
$magic_value == $BINARY_GRAYMAP ? $GRAYMAP_CHANNELS
: $PIXMAP_CHANNELS
)
);
return $header + $datasize;
}
sub read_header {
my $filename = shift;
open my $fh, '<', $filename or return;
my $header = <$fh>;
my ( $magic_value, $width, $height, $line );
my $bytes_per_channel = 1;
if ( defined $header and $header =~ /^P(\d)\n/xsm ) {
$magic_value = $1;
}
else {
close $fh or return;
return;
}
if ( $magic_value < $BINARY_BITMAP ) {
close $fh or return;
return;
}
while ( $line = <$fh> ) {
$header .= $line;
if ( $line =~ /^(\#|\s*\n)/xsm ) { next }
if ( $line =~ /(\d*)[ ](\d*)\n/xsm ) {
( $width, $height ) = ( $1, $2 );
if ( $magic_value == $BINARY_BITMAP ) { last }
}
elsif ( $magic_value > $BINARY_BITMAP and $line =~ /(\d+)\n/xsm ) {
my $maxval = $1;
$bytes_per_channel = log( $maxval + 1 ) / log(2) / $BITS_PER_BYTE;
last;
}
else {
close $fh or return;
return;
}
}
close $fh or return;
return $magic_value, $width, $height, $bytes_per_channel, length $header;
}
1;
__END__
|