<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;

# this is just to exercise the code, see the capture2image
# function below for the meat
my $from = shift;

my $to = shift;

my $width = shift || 320;

my $height = shift || 240;

$to or die "Usage: $0 from to [width [height]]\n";

my $data;
open RAWVIDEO, "&lt; $from"
  or die "Cannot open $from: $!\n";
binmode RAWVIDEO;
$data = do { local $/; &lt;RAWVIDEO&gt; };
close RAWVIDEO;

length $data &gt;= $width * $height * 3
  or die "Not enough data for video frame\n";

my $im = Imager-&gt;new(xsize=&gt;$width, ysize=&gt;$height);

capture2image($im, $data);

$im-&gt;write(file=&gt;$to)
  or die "Cannot save $to: $!\n";

use Inline C =&gt; &lt;&lt;'EOS' =&gt; WITH =&gt; 'Imager';
void
capture2image(Imager::ImgRaw out, unsigned char *data) {
  i_color *line_buf = mymalloc(sizeof(i_color) * out-&gt;xsize);
  i_color *pixelp;
  int x, y;
  
  for (y = 0; y &lt; out-&gt;ysize; ++y) {
    pixelp = line_buf;
    for (x = 0; x &lt; out-&gt;xsize; ++x) {
      pixelp-&gt;rgba.b = *data++;
      pixelp-&gt;rgba.g = *data++;
      pixelp-&gt;rgba.r = *data++;
      ++pixelp;
    }
    i_plin(out, 0, out-&gt;xsize, y, line_buf);
  }

  myfree(line_buf);
}
EOS

__END__

=head1 NAME

inline_capture2image.pl - convert captured C&lt;BGR&gt; data to any Imager supported format

=head1 SYNOPSIS

  perl inline_capture2image.pl rawbgr foo.ext
  perl inline_capture2image.pl rawbgr foo.ext width
  perl inline_capture2image.pl rawbgr foo.ext width height

=head1 DESCRIPTION

This was inspired by the discussion at
http://www.perlmonks.org/?node_id=539316 (Feeding video data to
Imager).

inline_capture2image.pl takes V4L raw captured image data and outputs
an image in any image format supported by Imager.

=head1 SEE ALSO

Imager, Imager::API

Perl and Video Capture
http://www.perlmonks.org/?node=474047

Feeding video data to Imager
http://www.perlmonks.org/?node_id=539316

=head1 AUTHOR

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

=head1 REVISION

$Revision$

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