%  TOPS-20 change file for PXtoPK by Tomas Rokicki.  Send bug reports to
%  ROKICKI@SU-SCORE.  The majority of these changes are to eliminate the
%  sign bit manipulations necessary on 32-bit machines but unnecessary on
%  the 36-bit DEC-20.

@x [1]
\pageno=\contentspagenumber \advance\pageno by 1
@y
\pageno=\contentspagenumber \advance\pageno by 1
\let\maybe=\iffalse
\def\ttw{{\mc TOPS-20}}
\def\title{PXtoPK changes for \ttw}
@z

@x [3]
@d banner=='This is PXtoPK, Version 2.3'
@y
@d banner=='This is PXtoPK, TOPS-20 Version 2.3'
@z

@x [5]
@d print_ln(#)==write_ln(output,#)
@d print(#)==write(output,#)
@y
@d print_ln(#)==write_ln(tty,#)
@d print(#)==write(tty,#)
@z

@x [5]
@p program PXtoPK(input, output);
@y
@p program PXtoPK;
@z

@x [7]
@<Constants...@>=
@!max_mem_size=200000; {the major array used for almost everything.}
@!name_length=80; {maximum length of a file name}
@!terminal_line_length=132; {maximum length of an input line}
@y
@<Constants...@>=
@!max_mem_size=100000; {the major array used for almost everything.}
@!name_length=80; {maximum length of a file name}
@!terminal_line_length=132; {maximum length of an input line}
@!max_rescan=200; {maximum length of a command line}
@z

@x [46]
@!pxl_file:word_file; {where the input comes from}
@y
@!pxl_file:byte_file; {where the input comes from}
@z

@x [47]
@p procedure open_pxl_file; {prepares to read packed bytes in a |pxl_file|}
begin reset(pxl_file,pxl_name);
eof_pixel:=eof(pxl_file);
end;
@#
procedure open_pk_file; {prepares the output for writing}
begin rewrite(pk_file,pk_name);
end;
@y
@p procedure open_pxl_file; {prepares to read packed bytes in a |pxl_file|}
begin reset(pxl_file,pxl_name,'/O/B:8');
eof_pixel:=eof(pxl_file);
end;
@#
procedure make_pk_name;
var res, i, j : integer ;
begin
  @<Make pk name@> ;
end;
@#
procedure open_pk_file; {prepares the output for writing}
begin if startup then make_pk_name;
rewrite(pk_file,pk_name,'/O/B:8');
end;
@z

@x [50]
@p function pixel_integer : integer ;
var i:integer;
begin i := pxl_file^ ;
get(pxl_file) ;
eof_pixel:=eof(pxl_file);
pixel_integer:=i;
end;
@y
@p function pixel_integer : integer ;
var i:integer;
begin i:=pxl_file^ ; get(pxl_file);
i:=i*256+pxl_file^ ; get(pxl_file);
i:=i*256+pxl_file^ ; get(pxl_file);
i:=i*256+pxl_file^ ; get(pxl_file);
eof_pixel:=eof(pxl_file);
pixel_integer:=i;
end;
@z

@x [57]
@p function hi(a:integer) : integer ;
begin
   hi := a div 65536 ;
end ;
@#
function lo(a:integer) : integer ;
begin
   lo := a mod 65536 ;
end ;
@#
function his(a:integer) : integer ;
begin
   if a < 0 then
      his := ( a + one_fourth + one_fourth ) div 65536 - 32768
   else
      his := a div 65536 ;
end ;
@#
function hip(a:integer) : integer ;
begin
   if a < 0 then
      hip := ( a + one_fourth + one_fourth ) div 65536 + 32768
   else
      hip := a div 65536 ;
end ;
@#
function lop(a:integer) : integer ;
begin
   lop := a - 65536 * his(a) ;
end ;
@#
function los(a:integer) : integer ;
var b : integer ;
begin
   b := lop(a) ;
   if b > 32767 then
      los := b - 65536
   else
      los := b ;
end ;
@y
@p function hi(a:integer) : integer ;
begin
   hi := a div 65536 ;
end ;
@#
function lo(a:integer) : integer ;
begin
   lo := a mod 65536 ;
end ;
@#
function his(a:integer) : integer ;
begin
   a := a div 65536 ;
   if a > 32767 then his := a - 65536
   else his := a ;
end ;
@#
function hip(a:integer) : integer ;
begin
   hip := a div 65536 ;
end ;
@#
function lop(a:integer) : integer ;
begin
   lop := a mod 65536 ;
end ;
@#
function los(a:integer) : integer ;
var b : integer ;
begin
   b := a mod 65536 ;
   if b > 32767 then
      los := b - 65536
   else
      los := b ;
end ;
@z

@x [69]
for i := ones_row to repeat_pointer - 2 do mem[i] := -1 ;
@y
for i := ones_row to repeat_pointer - 2 do mem[i] := power[32] - 1 ;
@z

@x [69]
if i = 0 then
   mem[repeat_pointer - 1] := -1
else if i = 1 then
   mem[repeat_pointer - 1] := - one_fourth - one_fourth
else
   mem[repeat_pointer - 1] := - power[32 - i] ;
@y
if i = 0 then
   mem[repeat_pointer - 1] := power[32] - 1
else
   mem[repeat_pointer - 1] := power[32] - power[32 - i] ;
@z

@x [72]
@!power : array [0..31] of integer ;
@y
@!power : array [0..32] of integer ;
@z

@x [72]
power[0] := 1 ;
for i := 1 to 30 do
   power[i] := power[i-1] * 2 ;
power[31] := - power[30] - power[30] ;
@y
power[0] := 1 ;
for i := 1 to 32 do power[i] := power[i-1] + power[i-1] ;
@z

@x [77]
else if bit_mod_32 = 31 then begin
   if word < 0 then begin
      bit := black ;
      word := word + one_fourth + one_fourth ;
   end else
      bit := white ;
end else begin
   if word >= power[bit_mod_32] then begin
      word := word - power[bit_mod_32] ;
      bit := black ;
   end else
      bit := white ;
@y
else begin if word >= power[bit_mod_32] then begin
   word := word - power[bit_mod_32] ;
   bit := black ;
end else bit := white ;
@z

@x [94]
@d get_line(#) == if eoln(input) then read_ln(input) ;
   i := 1 ;
   while not (eoln(input) or eof(input)) do begin
      #[i] := input^ ;
      incr(i) ;
      get(input) ;
   end ;
   #[i] := ' '
@y
@d get_line(#) == if eoln(tty) then read_ln(tty) ;
   i := 1 ;
   while not (eoln(tty) or eof(tty)) do begin
      #[i] := tty^ ;
      incr(i) ;
      get(tty) ;
   end ;
   #[i] := ' '
@z

@x [96]
initialize ;
dialog ;
@y
initialize ;
startup := true ;
@<Get command line and check for file names@> ;
if not startup then dialog ;
@z

@x [98]
@* System-dependent changes.
This section should be replaced, if necessary, by changes to the program
that are necessary to make \.{PXtoPK} work at a particular installation.
Any additional routines should be inserted here.
@^system dependencies@>
@y
@* System-dependent changes.
This section should be replaced, if necessary, by changes to the program
that are necessary to make \.{PXtoPK} work at a particular installation.
Any additional routines should be inserted here.
@^system dependencies@>

We call a system procedure to put the command line into the input buffer.
We then read it in, skipping over the program name to the first blank.

@d RSCAN=@'500 {ReSCAN buffer JSYS}

@<Get command line and check for file names@>=
jsys(RSCAN,1,i;0;j); {get the command line}
if (i<>2) or (j<=0) then startup := false
else begin
  if eoln(tty) then read_ln(tty); {for some TOPS-20's}
  read(tty,rescan_buffer:rescan_len); {read in rescan buffer}
  if rescan_len>max_rescan then abort('command line too long!');
  read_ln(tty);
  if rescan_len=j-2 then startup := false
  else begin
    i:=1; while rescan_buffer[i]>' 'do incr(i); {skip invocation}
    while(i<=rescan_len) and (rescan_buffer[i]=' ')do incr(i); {skip spaces}
    if i>rescan_len then startup := false
    else begin
      for j := 1 to name_length do begin
        pk_name[j] := ' ' ; pxl_name[j] := ' ' ;
      end ;
      j := 1 ;
      last_ext := -1 ;
      while ( i <= rescan_len ) and ( rescan_buffer[i] <> ' ' ) and
        ( rescan_buffer[i] <> '/' ) do begin
        pk_name[j] := rescan_buffer[i] ;
        pxl_name[j] := rescan_buffer[i] ;
        if (rescan_buffer[i] = '.') and (last_ext = -1) then last_ext := j ;
        if rescan_buffer[i] in [':',']','>'] then last_ext := -1 ;
        incr(j) ; incr(i) ;
      end ;
      if last_ext = -1 then begin
        last_ext := j ;
        pxl_name[j] := '.' ; pxl_name[j+1] := 'p' ; pxl_name[j+2] := 'x' ;
        pxl_name[j+3] := 'l' ;
      end ;
      pk_name[last_ext] := '.' ;
    end ;
  end ;
end

@ Next, a procedure to put the magnification in the packed file name.

@<Make pk name@>=
res := (magnification + 3) div 5 ;
i := 1 ;
while res >= i * 10 do i := i * 10 ;
if last_ext + 7 > name_length then abort('PK file name too long!');
j := last_ext + 1 ;
while i > 0 do begin
  pk_name[j] := xchr["0"+res div i] ;
  res := res mod i ; i := i div 10 ;
  incr(j) ;
end ;
pk_name[j] := 'p' ;
pk_name[j+1] := 'k' ;
for i := j+2 to name_length do pk_name[i] := ' '

@ @<Glob...@>=
@!rescan_buffer:packed array[1..max_rescan] of char;
@!rescan_len:integer;
@!i, @!j : integer ; {general purpose indices}
@!startup : boolean ; {did we have a file name?}
@!last_ext : integer ; {where was the dot?}
@z