-- PDInterpOutputPanasonicImpl.mesa
-- Copyright (C) 1984, Xerox Corporation.  All rights reserved.
-- Michael Plass, 20-Sep-84 12:34:40
-- Tim Diebert,   4-Oct-85 16:07:13
-- 

DIRECTORY Environment, GPIB, Inline, PDFileFormat, PDInterpBasic, PDInterpOutput, PDInterpBitmap, PDInterpSysCalls, PDQueue, Process, String;
  
PDInterpOutputPanasonicImpl: PROGRAM
  IMPORTS GPIB, Inline, PDInterpBitmap, PDInterpSysCalls, PDQueue, Process, String
  EXPORTS PDInterpOutput
= BEGIN
  
bitsPerWord: NAT = Environment.bitsPerWord;
currentHerald: PDFileFormat.Herald;
currentStartImage: PDFileFormat.StartImage;
currentBandNumber: NAT ← 0;
bandWords: INT ← 0;
band: PDInterpBitmap.BitmapDesc ← [sOrigin: 0, fOrigin: 0, sMin: 0, fMin: 0, sSize: 0, fSize: 0, pointer: NIL, rast: 0, lines: 0];
bandWordsAllocated: CARDINAL ← 0;
feed: BOOL ← FALSE;
strip: BOOL ← FALSE;
deviceAddr: GPIB.DeviceAddr ← 16;
bandsSent: CARDINAL ← 0;
bandsToSend: CARDINAL ← 0;
zeros: PDInterpBitmap.BitmapDesc ← [sOrigin: 0, fOrigin: 0, sMin: 0, fMin: 0, sSize: 0, fSize: 0, pointer: NIL, rast: 0, lines: 0];
zerosWordsAllocated: CARDINAL ← 0;
quadWordCnt: CARDINAL ← 0;
  
CurrentBandDesc: PROC RETURNS [PDInterpBitmap.BitmapDesc] = {
  band.sOrigin ← currentHerald.bandSSize*(currentBandNumber+currentStartImage.passBands);
  PDInterpBitmap.Clear[band];
  RETURN [band]
  };
  
StartImage: PUBLIC PROC [herald: PDFileFormat.Herald, startImage: PDFileFormat.StartImage, request: PDQueue.Request]
  RETURNS [PDInterpBitmap.BitmapDesc] = {
  rast: NAT = (herald.imageFSize+bitsPerWord-1)/bitsPerWord;
  words: CARDINAL = rast * herald.bandSSize;  -- In the band.
  bytes: CARDINAL ← rast * 2;
  command: LONG STRING ← [200];
  linesToSend: CARDINAL ← MIN[herald.imageSSize, 5500];
  bandsToSend ← MIN[herald.imageSSize/herald.bandSSize, 110];
  command.length ← 0;
  currentHerald ← herald;
  currentStartImage ← startImage;
  currentBandNumber ← 0;
  feed ← startImage.feed;
  strip ← startImage.strip;
  quadWordCnt ← words/4;
  IF words > bandWordsAllocated OR bandWordsAllocated = 0 THEN {
    IF band.pointer # NIL THEN PDInterpSysCalls.FreeSpace[band.pointer];
    band.pointer ← PDInterpSysCalls.AllocateSpace[words];
    bandWordsAllocated ← words;
    };
  IF words > zerosWordsAllocated OR zerosWordsAllocated = 0 THEN {
    IF zeros.pointer # NIL THEN PDInterpSysCalls.FreeSpace[zeros.pointer];
    zeros.pointer ← PDInterpSysCalls.AllocateSpace[words];
    zerosWordsAllocated ← words;
    };
  zeros.sOrigin ← 0;
  zeros.fOrigin ← 0;
  zeros.sMin ← 0; zeros.fMin ← 0; zeros.sSize ← herald.bandSSize;
  zeros.fSize ← rast*bitsPerWord; zeros.rast ← rast; zeros.lines ← herald.bandSSize;
  PDInterpBitmap.Clear[zeros];
  IF feed THEN BEGIN
    numbers: LONG STRING ← [120];
    GPIB.DevicesClear[];
    String.AppendNumber[s: numbers, n: bytes];
    String.AppendChar[s: numbers, c: ',];
    String.AppendNumber[s: numbers, n: linesToSend];
    String.AppendString[to: numbers, from: ",0,0\n\l"];
    GPIB.SelectedDeviceClear[deviceAddr];
    String.AppendString[to: command, from: "AY"];
    String.AppendString[to: command, from: numbers];
    GPIB.WriteDevice[deviceAddr, command];
    command.length ← 0;
    String.AppendString[to: command, from: "AM"];
    String.AppendString[to: command, from: numbers];
    GPIB.WriteDevice[deviceAddr, command];
    command.length ← 0;
    String.AppendString[to: command, from: "AC"];
    String.AppendString[to: command, from: numbers];
    GPIB.WriteDevice[deviceAddr, command];
    command.length ← 0;
    String.AppendString[to: command, from: "AK"];
    String.AppendString[to: command, from: numbers];
    GPIB.WriteDevice[deviceAddr, command];
    command.length ← 0;
    GPIB.WriteDevice[deviceAddr, "PL\n\l"];
    GPIB.WriteDevice[deviceAddr, "C\n\l"];
    GPIB.WriteDevice[deviceAddr, "O900\n\l"];
    GPIB.WriteDeviceInitial[deviceAddr, "T\n\l"];
    END;
  FOR i: CARDINAL IN [0 .. startImage.passBands) DO
    GPIB.WriteDeviceBlock[deviceAddr, zeros.pointer, quadWordCnt, FALSE];
    Process.Yield[];
    ENDLOOP;
  bandsSent ← startImage.passBands;
  band.sOrigin ← 0;
  band.fOrigin ← startImage.fMinPage;
  band.sMin ← 0;
  band.fMin ← 0;
  band.sSize ← herald.bandSSize;
  band.fSize ← rast*bitsPerWord;
  band.rast ← rast;
  band.lines ← herald.bandSSize;
  bandWords ← words;
  RETURN [CurrentBandDesc[]]
  };
  -- 
EndBand: PUBLIC PROC RETURNS [PDInterpBitmap.BitmapDesc] = {
  GPIB.WriteDeviceBlock[deviceAddr, band.pointer, quadWordCnt, FALSE];
  Process.Yield[];
  bandsSent ← bandsSent + 1;
  currentBandNumber ← currentBandNumber + 1;
  RETURN [CurrentBandDesc[]]
  };
  
EndImage: PUBLIC PROC [request: PDQueue.Request] = {
  FOR i: CARDINAL IN [bandsSent .. bandsToSend) DO
    GPIB.WriteDeviceBlock[deviceAddr, zeros.pointer, quadWordCnt, FALSE];
    ENDLOOP;
  IF strip THEN Process.Pause[Process.SecondsToTicks[15]];
  };
  

ReprintLastPage: PUBLIC PROC [copies: CARDINAL] = {
  IF currentHerald.password # PDFileFormat.passwordValue THEN RETURN;
  currentHerald.copies ← copies;
--  PrintPage[];
  };
  
currentHerald.password ← 0;
  
END.