XFX3d opened this issue on Nov 13, 2005 ยท 83 posts
XFX3d posted Thu, 17 November 2005 at 5:33 AM
The following perl code will convert any image format supported by ImageMagick to an RSR. It will scale to 91px width. Really old versions of ImageMagick will cause it to scale like if you had 'constrain proportions' turned off in an image program, so make sure it's new (or add some code to calculate the height -- have fun, I've done that and it's annoying). New versions of ImageMagick scale it proportionally automatically unless forced otherwise, which is the behaviour one would want. You have to have Perl which you can get free from CPAN or, for Windows users, ActiveState Mac OSX users probably already have Perl installed. At least, as far as I know. If not, it's probably on your OSX CD, maybe under the BSD substytem stuff or something. You also have to have ImageMagick installed, as well as the PerlMagick (Image::Magick) libraries. Those can be found for free at The ImageMagick Site. This does support images taller than 91px, BTW. You may use and redistribute this code under the terms of the Perl Artistic Licence Use it like this (assuming you named it makeRSR.pl): perl makeRSR.pl image.ext > image.rsr
#!/usr/bin/perl
use strict;
use Image::Magick;
my $infile = shift;
die "Usage: $0 <infile>n" unless $infile;
die "File $infile does not existn" unless -e $infile;
my $image = Image::Magick->new;
my $ret = $image->Read($infile);
if ($ret) {
die "ImageMagick Error: $retn";
}
if (scalar @{$image} > 1) {
# Flatten layers and/or remove animation
$image = $image->Flatten;
}
$ret = $image->Scale(width => 91);
unless ($image->Get('magick') eq 'PICT') {
$image->Negate(channel => 'alpha');
}
$image->Set('magick' => 'PICT');
my $height = $image->Get('height');
my $width = 91;
my $data = $image->ImageToBlob;
my @data = split /n/, $data;
shift @data;
$data = join "n", @data;
my $size = length($data) + 304;
my $dpi = 72; # assume 72dpi of course
# Create the RSR header
my $header = pack 'NNNNx240Nnx4nnnCCCxCCNNx6nnx4nxa',
256, $size, $size - 256, 58, $size - 260, $size - 260,
$height, $width, 17, 2, 255, 12, 255, 254, $dpi, $dpi,
$height, $width, 1, "n";
# Create the RSR footer
my $footer = pack 'NNNNx8nnx2a4x3anx10Ca7',
256, $size, $size - 256, 58, 28, 50, 'PICT', "n", 128,
7, 'preview';
print $header, $data, $footer;
I'm the asshole. You wanna be a shit? You gotta go through ME.