File: makescale

package info (click to toggle)
grunch 1.3-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 204 kB
  • ctags: 25
  • sloc: sh: 830; ansic: 155; perl: 91; makefile: 69
file content (69 lines) | stat: -rwxr-xr-x 1,400 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/perl

# Syntax: makescale left.pgm right.pgm > matrix.pgm

sub loadhistogram {
	local ($filename,*array) = @_;
	local ($i, $skip);
	open HISTOGRAM, "pgmhist $filename |" || 
		die "Can't run 'pgmhist $filename'\n";
	for (0..255) {
		@array[$_] = 0;
        }
	$i = <HISTOGRAM>;
	while (<HISTOGRAM>) {
		chop;
		@words = split;
		$array[$words[0]] = $words[1];
	}
	close HISTOGRAM;
}

sub sumhistogram {
	local(*array) = @_;
	local ($minval, $maxval, $i);
	$minval = 0;
	while ($minval < 255 && $array[$minval] == 0) {
	    $minval ++;
        }
	$maxval = 255;
	while ($maxval >= $minval && $array[$maxval] == 0) {
	    $maxval --;
	}
	for (1 .. 255) {
	    $array[$_] += $array[$_ - 1];
        }
	return ($minval, $maxval);
}

loadhistogram($ARGV[0], *leftarray);
loadhistogram($ARGV[1], *rightarray);

($leftmin, $leftmax) = sumhistogram(*leftarray);
($rightmin, $rightmax) = sumhistogram(*rightarray);

$leftresidue = 255 - $leftmax;
$rightresidue = 255 - $rightmax;

for (0 .. $rightmin - 1) {
  $xlate[$_] = int (($leftmin * $_) / $rightmin);
}

for ($rightmax + 1 .. 255) {
  $xlate[$_] = int($leftmax + ($leftresidue * ($_ - $rightmax)) / $rightresidue);
}

$hit = $leftmin;

for ($rightmin .. $rightmax) {
  while ($hit < $leftmax && $rightarray[$_] > $leftarray[$hit]) {
    $hit ++;
  }
  $xlate[$_] = $hit;
}

print "P2\n256 1 255\n";

for (0..255) {
  print "$xlate[$_]\n";
}