#!/usr/bin/perl -w # # Read an imlist (x y pathname) from stdin, choose images from it as specified. # See also: pickim. # - Cameron Simpson 16jun2004 # use strict qw(vars); use Getopt::Std; sub loadfrom($); sub offset2index($); ($::cmd=$0) =~ s:.*/::; $::Usage="Usage: $::cmd [-p path] [-owt] [-x lx] [-y ly] [-n nitems] [words...] 0 && $nitems > 0) { my $poff = rand($offset); my $pndx = offset2index($poff); my $impath = $::IM[$pndx]; $::IM[$pndx]=''; $nitems--; next PICK if ! length $impath; if ($::DoStat) { if (! stat($impath)) { warn "$::cmd: $impath: stat: $!, skipping\n"; next PICK; } if (! -f _ || ! -s _) { warn "$::cmd: $impath: not non-empty file\n"; next PICK; } } print "$impath\n"; $::PickN--; } sub offset2index($) { my($off)=@_; my($low,$high)=(0,$#::IM); while ($low+1 < $high) { my $mid = int(($low+$high)/2); my $ioff = $::IMOffset[$mid]; if ($ioff <= $off) { $low=$mid; } else { $high=$mid; } } return $low; } sub loadfrom($) { my($FILE)=@_; IM: while (<$FILE>) { chomp; if (! /^\s*(\d+)\s+(\d+)\s+(\S.*)/) { warn "$::cmd: stdin, line $.: bad data: $_\n"; next IM; } my ($dx,$dy,$impath)=($1,$2,$3); # skip small images next IM if $dx < $::MinX; next IM if $dy < $::MinY; # skip images that are the wrong shape next IM if defined $::Aspect && ( ( $::Aspect eq PORTRAIT && $dx > $dy ) || ( $::Aspect eq LANDSCAPE && $dy > $dx ) ); # skip !words REJECT: for my $re (@::NotWords) { next IM if $impath =~ $re; } # skip if missing required words if ($::AndWords) { TRY_AND: for my $re (@::Words) { next IM if $impath !~ $re; } } else { my $ok=0; TRY_OR: for my $re (@::Words) { if ($impath =~ $re) { $ok=1; last TRY_OR; } } next IM if !$ok; } # haven't needed image full path until now if (defined $::Context) { $impath="$::Context/$impath"; } # image is ok, add to list push(@::IM,$impath); push(@::IMOffset,$offset); $offset+=$dx*$dy; $nimages++; } }