<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">#!perl -w
use strict;
use Imager;

my $in0_name = shift;
my $in1_name = shift;
my $out_name = shift
  or usage();

my $in0 = Imager-&gt;new;
$in0-&gt;read(file=&gt;$in0_name)
  or die "Cannot load $in0_name: ", $in0-&gt;errstr, "\n";

my $in1 = Imager-&gt;new;
$in1-&gt;read(file=&gt;$in1_name)
  or die "Cannot load $in1_name: ", $in1-&gt;errstr, "\n";

$in0-&gt;getwidth == $in1-&gt;getwidth
  &amp;&amp; $in0-&gt;getheight == $in1-&gt;getheight
  or die "Images must be the same width and height\n";

$in0-&gt;getwidth == $in1-&gt;getwidth
  or die "Images must have the same number of channels\n";

my $out = interleave_images3($in0, $in1);

$out-&gt;write(file=&gt;$out_name)
  or die "Cannot write $out_name: ", $out-&gt;errstr, "\n";

sub usage {
  print &lt;&lt;EOS;
Usage: $0 even_image odd_image out_image
EOS
  exit;
}

# this one uses transform2()
# see perldoc Imager::Engines
sub interleave_images {
  my ($even, $odd) = @_;

  my $width = $even-&gt;getwidth;
  my $height = 2 * $even-&gt;getheight;
  my $expr = &lt;&lt;EXPR; # if odd get pixel from img2[x,y/2] else from img1[x,y/2]
y 2 % x y 2 / getp2 x y 2 / getp1 ifp
EXPR
  my $out = Imager::transform2
    ({ 
      rpnexpr=&gt;$expr, 
      width =&gt;$width, 
      height=&gt;$height 
     },
     $even, $odd) or die Imager-&gt;errstr;

  $out;
}

# i_copyto()
# this should really have been possible through the paste method too,
# but the paste() interface is too limited for this
# so we call i_copyto() directly
# http://rt.cpan.org/NoAuth/Bug.html?id=11858
# the code as written here does work though
sub interleave_images2 {
  my ($even, $odd) = @_;

  my $width = $even-&gt;getwidth;
  my $out = Imager-&gt;new(xsize=&gt;$width, ysize=&gt;2 * $even-&gt;getheight,
			channels =&gt; $even-&gt;getchannels);

  for my $y (0 .. $even-&gt;getheight-1) {
    Imager::i_copyto($out-&gt;{IMG}, $even-&gt;{IMG}, 0, $y, $width, $y+1,
		     0, $y*2);
    Imager::i_copyto($out-&gt;{IMG}, $odd-&gt;{IMG}, 0, $y, $width, $y+1,
		     0, 1+$y*2);
  }

  $out;
}

# this version uses the internal i_glin() and i_plin() functions
# as of 0.44 the XS for i_glin() has a bug in that it doesn't copy
# the returned colors into the returned color objects
# http://rt.cpan.org/NoAuth/Bug.html?id=11860
sub interleave_images3 {
  my ($even, $odd) = @_;

  my $width = $even-&gt;getwidth;
  my $out = Imager-&gt;new(xsize=&gt;$width, ysize=&gt;2 * $even-&gt;getheight,
			channels =&gt; $even-&gt;getchannels);

  for my $y (0 .. $even-&gt;getheight-1) {
    my @row = Imager::i_glin($even-&gt;{IMG}, 0, $width, $y);
    Imager::i_plin($out-&gt;{IMG}, 0, $y*2, @row);

    @row = Imager::i_glin($odd-&gt;{IMG}, 0, $width, $y);
    Imager::i_plin($out-&gt;{IMG}, 0, 1+$y*2, @row);
  }

  $out;
}

=head1 NAME

interleave.pl - given two identically sized images create an image twice the height with interleaved rows from the source images.

=head1 SYNOPSIS

  perl interleave.pl even_input odd_input output

=head1 DESCRIPTION

This sample produces an output image with interleaved rows from the
two input images.

Multiple implementations are included, including two that revealed
bugs or limitations in Imager, to demonstrate some different
approaches.

See http://www.3dexpo.com/interleaved.htm for an example where this
might be useful.

=head1 AUTHOR

Tony Cook &lt;tonyc@cpan.org&gt;

=for stopwords Oppenheim

Thanks to Dan Oppenheim, who provided the impetus for this sample.

=head1 REVISION

$Revision$

=cut
</pre></body></html>