pbm file
Linux上で動くフリーのソフトで2次元配列を濃淡図で表示するソフトを探
していますが,ご存知の方がいらっしゃいましたら,教えて下さい。目的
は,例えば,
1 2 3 4 5 6
2 2 3 3 3 5
2 3 3 3 4 5
3 4 4 4 5 6
2 3 4 5 6 5
という内容のテキストファイルを読み込んで,濃淡図を得ることです。テ
キストファイルはFortranの計算で得られるものです。配列数は最大
900x900です。なお,ディスプレイへの描画やプリントアウトでは,配列数
ほど解像度がなくても表示あるいはプリントアウトできる必要がありま
す。Macの方では,SpyGlassやMathematicaでできるそうですが,手元にな
画像フォーマットの PGM にしちゃうのはどうでしょう.
1 2 3 4 5 6 2 2 3 3 3 5 2 3 3 3 4 5 3 4 4 4 5 6 2 3 4 5 6 5
が元ネタなら、
P2 6 5 6 1 2 3 4 5 6 2 2 3 3 3 5 2 3 3 3 4 5 3 4 4 4 5 6 2 3 4 5 6 5
はい、これで立派な PGM 画像ファイルのできあがりです。 __
やってみるなら、netpbm をインストールしておいてください。 メジャーなものなので、すでに入ってるかもしれません。
netpbm は、PPM (portable pixmap format, カラー画像), PGM (portable graymap format, 無彩濃淡画像), PBM (portable bitmap format, 二値画像) というファイルフォーマットたちを、エスペラント語のごとく、あらゆる 画像フォーマットの中継に使って、フォーマット変換や画像処理を やってしまおうという、画像フィルタ群です。
で、PPM, PGM, PBM (総称して PNM - portable anymap) には、それぞれバイ ナリによる表現とテキスト表現が許されています。前者は効率がよいが、 後者は簡単でいじりやすい。 __
上記の PGM 画像ファイルの中身の意味は:
P2 # テキスト表現 PGM フォーマットを示すヘッダ/マジック 6 5 # 幅 高さ 6 # 画素値の最大値 1 2 3 4 5 6 # データ... 2 2 3 3 3 5 2 3 3 3 4 5 3 4 4 4 5 6 2 3 4 5 6 5
となります。テキスト表現の PGM には # 以降コメントを書いちゃっていいの で、上記も立派な PGM 画像ファイルです。man pgm 参照. __
自動変換するなら.. array2pgm とかいうファイルにこんなの ↓ を入れてお いて、
#!/bin/sh WIDTH=`head -1 $1 | wc | awk '{print $2}'` HEIGHT=`wc -l $1` echo P2 echo $WIDTH $HEIGHT cat $1 | tr ' \t' '\n' | sort | uniq | tail -1 cat $1
./array2pgm testfile > test.pgm
とかでいけるでしょう。
上は無理に :-) sh で書きましたが、データが大きいのなら perl か何かの ほうがよさそう。
テキストファイルとして見たときに、一行は 70 文字以下にすべし。 ということで、画像としてのデータ本体の解釈は、改行位置は意識されません。
P2 6 5 6 1 2 3 6 5 6 2 2 3 3 3 5 2 3 3 3 4 5 3 4 4 4 5 6 2 3 4 5 6 5
の場合は、各ラスターが一行ずつに収まっているので視覚的にわかりやすいで すが、別に
P2 6 5 6 1 2 3 6 5 6 2 2 3 3 3 5 2 3 3 3 4 5 3 4 4 4 5 6 2 3 4 5 6 5
でも、
P2 6 5 6 1 2 3 6 5 6 2 2 3 3 3 5 2 3 3 3 4 5 3 4 4 4 5 6 2 3 4 5 6 5
でも同じです。
========================================================== pbm(5) FILE FORMATS pbm(5)
NAME pbm - portable bitmap file format
DESCRIPTION The portable bitmap format is a lowest common denominator monochrome file format. It was originally designed to make it reasonable to mail bitmaps between different types of machines using the typical stupid network mailers we have today. Now it serves as the common language of a large fam- ily of bitmap conversion filters. The definition is as fol- lows:
- A "magic number" for identifying the file type. A pbm file's magic number is the two characters "P1".
- Whitespace (blanks, TABs, CRs, LFs).
- A width, formatted as ASCII characters in decimal.
- Whitespace.
- A height, again in ASCII decimal.
- Whitespace.
- Width * height bits, each either '1' or '0', starting at the top-left corner of the bitmap, proceeding in normal English reading order.
- The character '1' means black, '0' means white.
- Whitespace in the bits section is ignored.
- Characters from a "#" to the next end-of-line are ignored (comments).
- No line should be longer than 70 characters.
Here is an example of a small bitmap in this format: P1
# feep.pbm 24 7 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0 1 1 1 1 0 0 1 1 1 1 0 0 1 1 1 1 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 1 0 0 1 1 1 0 0 0 1 1 1 0 0 0 1 1 1 0 0 0 1 1 1 1 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 1 1 1 0 0 1 1 1 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Programs that read this format should be as lenient as pos- sible, accepting anything that looks remotely like a bitmap.
There is also a variant on the format, available by setting the RAWBITS option at compile time. This variant is dif- ferent in the following ways:
- The "magic number" is "P4" instead of "P1".
- The bits are stored eight per byte, high bit first low bit last.
- No whitespace is allowed in the bits section, and only a single character of whitespace (typically a newline) is allowed after the height.
- The files are eight times smaller and many times faster to read and write.
AUTHOR Copyright (C) 1989, 1991 by Jef Poskanzer.
ppm(5) FILE FORMATS ppm(5)
NAME ppm - portable pixmap file format
DESCRIPTION The portable pixmap format is a lowest common denominator color image file format. The definition is as follows:
- A "magic number" for identifying the file type. A ppm file's magic number is the two characters "P3".
- Whitespace (blanks, TABs, CRs, LFs).
- A width, formatted as ASCII characters in decimal.
- Whitespace.
- A height, again in ASCII decimal.
- Whitespace.
- The maximum color-component value, again in ASCII decimal.
- Whitespace.
- Width * height pixels, each three ASCII decimal values between 0 and the specified maximum value, starting at the top-left corner of the pixmap, proceeding in normal English reading order. The three values for each pixel represent red, green, and blue, respectively; a value of 0 means that color is off, and the maximum value means that color is maxxed out.
- Characters from a "#" to the next end-of-line are ignored (comments).
- No line should be longer than 70 characters.
Here is an example of a small pixmap in this format: P3
# feep.ppm 4 4 15 0 0 0 0 0 0 0 0 0 15 0 15 0 0 0 0 15 7 0 0 0 0 0 0 0 0 0 0 0 0 0 15 7 0 0 0 15 0 15 0 0 0 0 0 0 0 0 0
Programs that read this format should be as lenient as pos- sible, accepting anything that looks remotely like a pixmap.
There is also a variant on the format, available by setting the RAWBITS option at compile time. This variant is different in the following ways:
- The "magic number" is "P6" instead of "P3".
- The pixel values are stored as plain bytes, instead of ASCII decimal.
- Whitespace is not allowed in the pixels area, and only a single character of whitespace (typically a newline) is allowed after the maxval.
- The files are smaller and many times faster to read and write.
Note that this raw format can only be used for maxvals less than or equal to 255. If you use the _p_p_m library and try to write a file with a larger maxval, it will automatically fall back on the slower but more general plain format.
AUTHOR Copyright (C) 1989, 1991 by Jef Poskanzer.
pgm(5) FILE FORMATS pgm(5)
NAME pgm - portable graymap file format
DESCRIPTION The portable graymap format is a lowest common denominator grayscale file format. The definition is as follows:
- A "magic number" for identifying the file type. A pgm file's magic number is the two characters "P2".
- Whitespace (blanks, TABs, CRs, LFs).
- A width, formatted as ASCII characters in decimal.
- Whitespace.
- A height, again in ASCII decimal.
- Whitespace.
- The maximum gray value, again in ASCII decimal.
- Whitespace.
- Width * height gray values, each in ASCII decimal, between 0 and the specified maximum value, separated by whi- tespace, starting at the top-left corner of the graymap, proceeding in normal English reading order. A value of 0 means black, and the maximum value means white.
- Characters from a "#" to the next end-of-line are ignored (comments).
- No line should be longer than 70 characters.
Here is an example of a small graymap in this format: P2
# feep.pgm 24 7 15 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3 3 3 3 0 0 7 7 7 7 0 0 11 11 11 11 0 0 15 15 15 15 0 0 3 0 0 0 0 0 7 0 0 0 0 0 11 0 0 0 0 0 15 0 0 15 0 0 3 3 3 0 0 0 7 7 7 0 0 0 11 11 11 0 0 0 15 15 15 15 0 0 3 0 0 0 0 0 7 0 0 0 0 0 11 0 0 0 0 0 15 0 0 0 0 0 3 0 0 0 0 0 7 7 7 7 0 0 11 11 11 11 0 0 15 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Programs that read this format should be as lenient as pos- sible, accepting anything that looks remotely like a gray- map.
There is also a variant on the format, available by setting the RAWBITS option at compile time. This variant is dif- ferent in the following ways:
- The "magic number" is "P5" instead of "P2".
- The gray values are stored as plain bytes, instead of ASCII decimal.
- No whitespace is allowed in the grays section, and only a single character of whitespace (typically a newline) is allowed after the maxval.
- The files are smaller and many times faster to read and write.
Note that this raw format can only be used for maxvals less than or equal to 255. If you use the _p_g_m library and try to write a file with a larger maxval, it will automatically fall back on the slower but more general plain format.
AUTHOR Copyright (C) 1989, 1991 by Jef Poskanzer.
Sun Release 4.1 Last change: 27 September 1991 2