]>
Commit | Line | Data |
---|---|---|
c64eee30 TC |
1 | #!perl -w |
2 | use strict; | |
3 | use Imager; | |
4 | ||
5 | # this is just to exercise the code, see the capture2image | |
6 | # function below for the meat | |
7 | my $from = shift; | |
8 | ||
9 | my $to = shift; | |
10 | ||
11 | my $width = shift || 320; | |
12 | ||
13 | my $height = shift || 240; | |
14 | ||
15 | $to or die "Usage: $0 from to [width [height]]\n"; | |
16 | ||
17 | my $data; | |
18 | open RAWVIDEO, "< $from" | |
19 | or die "Cannot open $from: $!\n"; | |
20 | binmode RAWVIDEO; | |
21 | $data = do { local $/; <RAWVIDEO> }; | |
22 | close RAWVIDEO; | |
23 | ||
24 | length $data >= $width * $height * 3 | |
25 | or die "Not enough data for video frame\n"; | |
26 | ||
27 | my $im = Imager->new(xsize=>$width, ysize=>$height); | |
28 | ||
29 | capture2image($im, $data); | |
30 | ||
31 | $im->write(file=>$to) | |
32 | or die "Cannot save $to: $!\n"; | |
33 | ||
34 | use Inline C => <<'EOS' => WITH => 'Imager'; | |
35 | void | |
36 | capture2image(Imager::ImgRaw out, unsigned char *data) { | |
37 | i_color *line_buf = mymalloc(sizeof(i_color) * out->xsize); | |
38 | i_color *pixelp; | |
39 | int x, y; | |
40 | ||
41 | for (y = 0; y < out->ysize; ++y) { | |
42 | pixelp = line_buf; | |
43 | for (x = 0; x < out->xsize; ++x) { | |
44 | pixelp->rgba.b = *data++; | |
45 | pixelp->rgba.g = *data++; | |
46 | pixelp->rgba.r = *data++; | |
47 | ++pixelp; | |
48 | } | |
49 | i_plin(out, 0, out->xsize, y, line_buf); | |
50 | } | |
51 | ||
52 | myfree(line_buf); | |
53 | } | |
54 | EOS | |
55 | ||
56 | __END__ | |
57 | ||
58 | =head1 NAME | |
59 | ||
5715f7c3 | 60 | inline_capture2image.pl - convert captured C<BGR> data to any Imager supported format |
c64eee30 TC |
61 | |
62 | =head1 SYNOPSIS | |
63 | ||
64 | perl inline_capture2image.pl rawbgr foo.ext | |
65 | perl inline_capture2image.pl rawbgr foo.ext width | |
66 | perl inline_capture2image.pl rawbgr foo.ext width height | |
67 | ||
68 | =head1 DESCRIPTION | |
69 | ||
70 | This was inspired by the discussion at | |
71 | http://www.perlmonks.org/?node_id=539316 (Feeding video data to | |
72 | Imager). | |
73 | ||
74 | inline_capture2image.pl takes V4L raw captured image data and outputs | |
75 | an image in any image format supported by Imager. | |
76 | ||
77 | =head1 SEE ALSO | |
78 | ||
79 | Imager, Imager::API | |
80 | ||
81 | Perl and Video Capture | |
82 | http://www.perlmonks.org/?node=474047 | |
83 | ||
84 | Feeding video data to Imager | |
85 | http://www.perlmonks.org/?node_id=539316 | |
86 | ||
87 | =head1 AUTHOR | |
88 | ||
89 | Tony Cook <tony@imager.perl.org> | |
90 | ||
91 | =head1 REVISION | |
92 | ||
93 | $Revision$ | |
94 | ||
95 | =cut |