From c64eee30379210cbbad862ed4379926637b02a5f Mon Sep 17 00:00:00 2001 From: Tony Cook Date: Mon, 22 May 2006 03:46:15 +0000 Subject: [PATCH] add inline_capture2image.pl sample --- MANIFEST | 1 + samples/README | 5 ++ samples/inline_capture2image.pl | 95 +++++++++++++++++++++++++++++++++ 3 files changed, 101 insertions(+) create mode 100644 samples/inline_capture2image.pl diff --git a/MANIFEST b/MANIFEST index 3389b478..96fd422f 100644 --- a/MANIFEST +++ b/MANIFEST @@ -162,6 +162,7 @@ samples/README samples/align-string.pl Demonstrate align_string method. samples/anaglyph.pl samples/border.pl Demonstrate adding a border +samples/inline_capture2image.pl convert captured BGR data to an image samples/inline_replace_color.pl replace colors using Inline::C samples/interleave.pl samples/replace_color.pl replace colors using transform2() diff --git a/samples/README b/samples/README index 3752a900..b4edfe95 100644 --- a/samples/README +++ b/samples/README @@ -92,3 +92,8 @@ align-string.pl This demonstrates how the various values of halign and valign behave. + +inline_capture2image.pl + + Demonstrates using Inline and Imager's API to convert captured BGR + image data into an Imager image. diff --git a/samples/inline_capture2image.pl b/samples/inline_capture2image.pl new file mode 100644 index 00000000..825da3d0 --- /dev/null +++ b/samples/inline_capture2image.pl @@ -0,0 +1,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 $/; }; +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 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 + +=head1 REVISION + +$Revision$ + +=cut -- 2.39.5