HEX

Warning: set_time_limit() [function.set-time-limit]: Cannot set time limit - prohibited by configuration in /home/u547966/brikov.ru/www/wp-content/plugins/admin-menu-editor/menu-editor.php on line 745
Server: Apache
System: Linux 4.19.0-0.bpo.9-amd64 x86_64 at red40
User: u547966 (5490)
PHP: 5.3.29-mh2
Disabled: syslog, dl, popen, proc_open, proc_nice, proc_get_status, proc_close, proc_terminate, posix_mkfifo, chown, chgrp, accelerator_reset, opcache_reset, accelerator_get_status, opcache_get_status, pcntl_alarm, pcntl_fork, pcntl_waitpid, pcntl_wait, pcntl_wifexited, pcntl_wifstopped, pcntl_wifsignaled, pcntl_wifcontinued, pcntl_wexitstatus, pcntl_wtermsig, pcntl_wstopsig, pcntl_signal, pcntl_signal_dispatch, pcntl_get_last_error, pcntl_strerror, pcntl_sigprocmask, pcntl_sigwaitinfo, pcntl_sigtimedwait, pcntl_exec, pcntl_getpriority, pcntl_setpriority
Upload Files
File: //usr/share/doc/libimager-perl/examples/inline_capture2image.pl
#!/usr/bin/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