2013-07-19 83 views
0

我試圖編寫程序來讀取pbm,pgm或ppm文件,並使用image運算符將圖像呈現到postscript輸出設備。只需測試P4輸入(二進制便攜式(1位)位圖)路徑,但是我的輸出全是扭曲的。.pbm文件圖像渲染 - [vhold] scanline-wiggles?

%! 
% cf. http://en.wikipedia.org/wiki/Netpbm_format 
% cf. http://en.wikipedia.org/wiki/Computer_Graphics (origin of image) 
% $ wget http://upload.wikimedia.org/wikipedia/commons/thumb/2/23/Spacewar%21-PDP-1-20070512.jpg/320px-Spacewar%21-PDP-1-20070512.jpg 
% $ convert 320px-Spacewar%21-PDP-1-20070512.jpg spacewar.pbm 
% $ convert 320px-Spacewar%21-PDP-1-20070512.jpg spacewar.pgm 
% $ convert 320px-Spacewar%21-PDP-1-20070512.jpg spacewar.ppm 

/filename (spacewar.pbm) def 
%/filename (spacewar.pgm) def 
%/filename (spacewar.ppm) def 

/infile filename (r) file def 
/readscale false def 

% Read magic number 
infile token pop << 
    /P1 { /depth 1 def 
     /readscale false def 
     /filetype /ascii def } 
    /P2 { /depth 8 def 
     /readscale true def 
     /filetype /ascii def } 
    /P3 { /depth 24 def 
     /readscale true def 
     /filetype /ascii def } 
    /P4 { /depth 1 def 
     /readscale false def 
     /filetype /binary def } 
    /P5 { /depth 8 def 
     /readscale true def 
     /filetype /binary def } 
    /P6 { /depth 24 def 
     /readscale true def 
     /filetype /binary def } 
>> exch 2 copy known not{pop/default}if get exec 

% Read header 
/buf 256 string def 
infile buf readline pop % line 
(1:)print dup == 
(#) { % line (#) 
    (2a:)print 1 index = 
    search { % post (#) pre 
     pop pop pop % 
     infile buf readline pop % (#) next-line 
     (#) % next-line (#) 
     (2b pstack\n)print pstack()= 
    }{ % line 
     (2c:)print dup == 
     exit 
    } ifelse 
} loop % line 
pstack()= 
token pop /height exch def 
token pop /width exch def 
readscale { 
    token pop /scale exch def 
}{ 
    pop 
}ifelse 
/buf width 
    depth mul 
    8 div ceiling cvi 
    string def 
(bufsize:)print buf length = 
/pad 
    buf length 8 mul 
    width sub def 
(pad:)print pad = 

/readdata << 
    /ascii { % file buf 
     0 1 width 1 sub { % file buf i 
      2 index token pop % file buf i 
     } for 
    } 
    /binary { % file buf 
     readstring pop 
     %dup length 0 ne { 0 1 index length pad sub getinterval } if 
     dup == flush 
     %(bin)= flush 
    } 
>> filetype get def 
%errordict/rangecheck{pstack dup length = quit}put 

width 
height 
depth 
[ 1 0 0 -1 0 height ] 
{ 
    infile buf readdata 
} image 

showpage 

我敢肯定,問題是我行的字節寬度和預期填充的計算:

/buf width 
    depth mul 
    8 div ceiling cvi 
    string def 
(bufsize:)print buf length = 
/pad 
    buf length 8 mul 
    width sub def 
(pad:)print pad = 

但似乎,沒事的時候,我通過它一步。對於這個215位寬的位圖,每行得到27個字節。

編輯:刪除「pad」-chop幫助。也許我需要添加額外的填充?

的問題表現在輸出:

improved but still bad

凡從this answer末類似,但更簡單的程序渲染確定。

ok image

回答

0

哦,小寶貝我,那是愚蠢的。

我有這兩條線失敗告終:

55 token pop /width exch def 
56 token pop /height exch def 

這一切最後的編輯,我打字的時候,有人對我說:

對於這215位寬的位圖,我得到27-每行字節數。

然後我再看看identify輸出。果然,215是高度,而不是寬度。

image fixed