#!/usr/bin/perl $usage = < $imagefile") || die "Can't open image file $imagefile for writing: $!\n"; binmode (IMG); $header = pack ("a8II", $our_magic, $from_width, $from_height); print IMG $header; # need to swap endianness, since ImageJ writes it big-endian. # while (($bytes = read (RAW, $buffer, 65536)) != 0) # { # print IMG $buffer; # $totalbytes += $bytes; # } $adj0 = 0; $adj255 = 0; while (($bytes = read (RAW, $buffer, 4096)) != 0) { $buffer = pack ("V*", unpack ("N*", $buffer)); $buffer = pack ("f*", map ($_ > 254.99 ? scalar (++$adj255, 8191) : ($_ < .01 ? scalar (++$adj0, -8191) : $_), unpack ("f*", $buffer))) if $mask_mode; print IMG $buffer; $totalbytes += $bytes; } if ($mask_mode) { # the "alas, ImageJ" comment below for an explanation of this hack. seek (RAW, -8, 1); print RAW substr ($buffer, -12, 4) x 2; } close (IMG); close (RAW); $maskstr = ", adjusting $adj0+$adj255 mask values" if $mask_mode; print "Wrote $totalbytes bytes of data to $imagefile$maskstr, " . "swapping endianness\n"; $pixelcount = $from_width * $from_height; $expbytes = $pixelcount * 4; print "Hmmm, $from_width x $from_height at 4 bytes each should be " . "$expbytes...\n" if $totalbytes != $expbytes; } else { open (IMG, "< $imagefile") || die "Can't open image file $imagefile: $!\n"; binmode (IMG); read (IMG, $header, 16); ($magic, $width, $height) = unpack ("a8II", $header); # print "$imagefile: Magic = \"$magic\"; size is $width x $height\n"; if ($magic eq "DGCMimgF") { $valid = 1; } else { die "$outputfile: Does not appear to be a valid image file " . "produced by avidemux2's ComputeAverage filter.\n"; } print "$imagefile: size is $width x $height\n"; if ($toraw_mode) { die "Output file $rawfile already exists - remove it or use " . "--overwrite!\n" if !$overwrite_mode && -e $rawfile; $adj0 = 0; $adj255 = 0; open (RAW, "> $rawfile") || die "Can't open $rawfile for writing: $!\n"; binmode (RAW); if ($mask_mode) { while (($bytes = read (IMG, $buffer, 4096)) != 0) { $buffer = pack ("f*", map ($_ > 254.95 ? scalar (++$adj255, 254.95) : ($_ < 0.05 ? scalar (++$adj0, 0.05) : $_), unpack ("f*", $buffer))); print RAW $buffer; $totalbytes += $bytes; } # alas, ImageJ is "smart" and "helpful" and will observe the range # of input values, and assume that, for instance, white is the max # (or if you select, min) value that it observed. That's fine, # and in fact better than it assuming a range that happens to not # be what we want. However, if (as has been the case in my # testing) the averages wind up ranging from, say, 0.335417 to # 254.891663, then setting a pixel to white will set it to # 254.891663, which is not what the code below (that converts a # white pixel in mask mode to an insanely white pixel, in mask # mode) is expecting. So, we force the last two pixels to 0 and # 255, just to force the range to be 0..255. When converting # back, we copy the second-to-last pixel into the last two, so # that we don't introduce an apparent particle. seek (RAW, -8, 1); print RAW pack ("f2", 0, 255); } else { while (($bytes = read (IMG, $buffer, 65536)) != 0) { print RAW $buffer; $totalbytes += $bytes; } } close (RAW); $maskstr = ", avoiding $adj0+$adj255 mask value collisions" if $adj0 || $adj255; print "Wrote $totalbytes bytes of data to $rawfile$maskstr\n"; $pixelcount = $width * $height; $expbytes = $pixelcount * 4; print "Hmmm, $width x $height at 4 bytes each should be $expbytes...\n" if $totalbytes != $expbytes; } elsif ($histo_mode) { while (($bytes = read (IMG, $float, 4)) == 4) { ++$nfloats; $float = unpack ("f", $float); $minfloat = $float if $float < $minfloat || !defined ($minfloat); $maxfloat = $float if $float > $maxfloat; die "$nfloats: $float" if $float < 0 || $float > 4000; ++$hits[int ($float + .5)]; } for ($i = int ($minfloat + .5); $i <= int ($maxfloat +.5); $i++) { $maxhits = $hits [$i] if $hits [$i] > $maxhits; } printf ("pixels read: %d; min = %.6f, max = %.6f\n", $nfloats, $minfloat, $maxfloat); $effwidth = $histo_width - (3 + 1 + 1 + 6 + 1 + 2); $last_skipped = int ($minfloat + .5); for ($i = int ($minfloat + .5); $i <= int ($maxfloat +.5); $i++) { $hits = $hits [$i]; if ($hits == 0) { print "...\n" if $last_skipped != $i - 1; $last_skipped = $i; next; } printf ("%3d: %6d %s\n", $i, $hits, "*" x ($hits * $effwidth / $maxhits)); } } close (IMG); }