Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

file 95 lines (65 sloc) 1.916 kb
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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
#!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, "< $from"
  or die "Cannot open $from: $!\n";
binmode RAWVIDEO;
$data = do { local $/; <RAWVIDEO> };
close RAWVIDEO;

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

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

capture2image($im, $data);

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

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

  myfree(line_buf);
}
EOS

__END__

=head1 NAME

inline_capture2image.pl - convert captured C<BGR> 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 <tonyc@cpan.org>

=head1 REVISION

$Revision$

=cut
Something went wrong with that request. Please try again.