#!/usr/local/bin/perl # # pgm2k.pl --- make kanji art from pgm image. # # NAKANO Taro # taro@hauN.org # # Usage: pgm2k.pl [-nv] < chizuru.pgm > chizuru.txt # pgmfile should be pgm-ascii format. # # Option: -n: negative output 白黒反転(黒地に白) # -v: verbose mode # # 履歴: # 2001/Apr/22 最初のバージョン # 2001/Sep/03 コメントが複数行ある pgm file を読めなかったバグを修正。 # -n, -v オプションを付けた。(これにより Perl 4 非対応になった。) # use Getopt::Std; # コマンドラインオプション処理モジュールを使う ### コマンドラインオプション処理 getopts("nv"); $charlistfile = "k14kanjilist"; # 濃度--漢字 変換表 $charmin = 4; # 漢字の濃度の最小値 $charmax = 116; # 漢字の濃度の最大値 $| = 1; $hpos = 0; # 横方向のピクセル位置 ### 画像内で使われている画素の最大濃度、最小濃度 $pixelmax = 0; $pixelmin = 255; $finishheader = 0; $linenum = 0; ### pgm 画像ヘッダ部の処理 while (<>) { chop; # なんとなく if (/P([0-9])/) { $ptype = $1; if ($opt_v) {print "picture type = P$ptype\n";} next; } elsif (/^#(.*)$/) { if ($opt_v) {print "comment: $1\n";} next; } elsif (/^([0-9]+)\s+([0-9]+)$/) { $width = $1; $height = $2; if ($opt_v) {print "width = $1, height = $2\n";} next; } elsif (/^([0-9]+)$/) { $pmax = $1; if ($opt_v) {print "pmax = $1\n";} $finishheader = 1; next; } else { $bitmap[$linenum++] = $_; # 画像内で使われている画素の最大濃度、最小濃度を求める @pixelarray = split(' ', $_); foreach $pixel (@pixelarray) { if ($pixel > $pixelmax) { $pixelmax = $pixel; } if ($pixel < $pixelmin) { $pixelmin = $pixel; } } } } if ($opt_v) { print "pixelmin = $pixelmin\n"; print "pixelmax = $pixelmax\n"; } ### 画素を漢字に変換 foreach $line (@bitmap) { @pixelarray = split(' ', $line); foreach $pixel (@pixelarray) { # print "pixel = $pixel\n"; open(CHARLIST, "<$charlistfile") || die "can't open CHARLIST\n"; ### $pixelmin 〜 $pixelmax のピクセル値を ### $charmin 〜 $charmax にスケールする ### $scaledpixel = int( (($pixel - $pixelmin)/($pixelmax - $pixelmin)) *($charmax - $charmin) + $charmin ); ### 白地に黒なので反転 if (!$opt_n) {$scaledpixel = $charmax - ($scaledpixel - $charmin);} # print "scaled pixel = $scaledpixel\n"; while () { ($density, $char) = split(' ', $_); # print "$density $char\n"; if ($scaledpixel == $density) { print "$char"; close(CHARLIST); last; } } $hpos++; if ($hpos == $width) { ### 漢字を $width 個出力したら改行 print "\n"; $hpos = 0; } } }