Tie-Array-Packed

 view release on metacpan or  search on metacpan

sample/image2rand.pl  view on Meta::CPAN

#!/usr/bin/perl

# See http://perlmonks.org/?node_id=1065851

use strict;
use warnings;
use 5.010;

use Tie::Array::Packed;
use Image::Magick;

my $file = shift @ARGV // die <<EOU;
Usage:
  $0 image_file [n [exp]]

EOU

my $n = shift @ARGV || 100;
my $exp = shift @ARGV || 1;

my $img = Image::Magick->new;
$img->Read($file);

my $w = $img->Get('Width');
my $h = $img->Get('Height');

my $out = Image::Magick->new(size => join('x', $w * 3, $h));
$out->Read("xc:black");

tie my(@acu), 'Tie::Array::Packed::DoubleNative';
$#acu = $h * $w; # preallocate
$#acu = 1;

for my $j (0..$h-1) {
    for my $i (0..$w-1) {
        my @c = $img->GetPixel(x => $i, y => $j);
        my $c = sqrt($c[2] * $c[2] + $c[1] * $c[1] + $c[0] * $c[0]) ** $exp;



( run in 0.526 second using v1.01-cache-2.11-cpan-beeb90c9504 )