]> git.imager.perl.org - imager.git/commitdiff
add inline_capture2image.pl sample
authorTony Cook <tony@develop=help.com>
Mon, 22 May 2006 03:46:15 +0000 (03:46 +0000)
committerTony Cook <tony@develop=help.com>
Mon, 22 May 2006 03:46:15 +0000 (03:46 +0000)
MANIFEST
samples/README
samples/inline_capture2image.pl [new file with mode: 0644]

index 3389b4783274e49b2b86616316c4c096f70e6e18..96fd422f2fa27f5929bd82b1e39c3decae6af904 100644 (file)
--- 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()
index 3752a900321460e80bf0329d2fddef1c46796b71..b4edfe95db9c5be23b8369d8773100dad7959f1b 100644 (file)
@@ -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 (file)
index 0000000..825da3d
--- /dev/null
@@ -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 $/; <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 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 <tony@imager.perl.org>
+
+=head1 REVISION
+
+$Revision$
+
+=cut