home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / archives / ucsdmagiscan2.zip / disk.text < prev    next >
Text File  |  2011-08-11  |  6KB  |  253 lines

  1. (*$S+*)
  2.  
  3.  This Unit is based on the SLVDIMS of Joyce Loebl 
  4.  Created by H Balen 22-Aug-84 
  5.  Modified by H Balen 13-May-85 
  6.  
  7. Unit DiskUnit;
  8.  
  9.    Interface
  10.  
  11.  
  12.    Uses
  13.       M2Types,M2IpRoot,M2Sys;
  14.  
  15.    type
  16.      GreyVal = 0..255;
  17.      LType = packed array[0..255] of GreyVal;
  18.      L2Type = packed array[0..255] of char;
  19.      LineType = record
  20.                   case Boolean of
  21.                     True :(i : LType);
  22.                     False:(b : L2Type)
  23.                     end;
  24.      BufferType = record
  25.                    case integer of
  26.                     0 :(i : packed array[0..511] of GreyVal);
  27.                     1 :(b : packed array[0..1] of L2Type);
  28.                     2 :(Im : Image )
  29.                     end;
  30.    var
  31.      Fl : File;
  32.  
  33.    procedure ImSve( Im : Image;
  34.                      FName : String );
  35.    procedure ImLd( var Im : Image;
  36.                      FName  : String );
  37.  
  38. Implementation
  39.  
  40. procedure ImSve;
  41.  This procedure saves an image, up to eight bits 
  42.  
  43. var
  44.    Line  : LineType;
  45.    Buffer: BufferType;
  46.    A,B,C,D : Image;
  47.    Blk   : integer;
  48.  
  49.  
  50. procedure Deposit( Im : Image );
  51. { This procedure writes the necessary data to the disk
  52.   in units of 512 bytes,and Images of Half size       }
  53.  
  54. var
  55.   Blks,RowNum : Integer;
  56.   Row    : PointSet;
  57.  
  58.   procedure GetLine( LinePs : PointSet;
  59.                      Im     : Image ;
  60.                     var GVal: LType );
  61.   { This procedure gets a 256 byte line from the picture }
  62.  
  63.   type
  64.      Idynarray = array[1..1]of Integer;
  65.  
  66.   var
  67.      Mrk : ^Integer;
  68.      Idyn: ^Idynarray;
  69.      i   : integer;
  70.  
  71.   begin
  72.   { Mark the Heap, and create space }
  73.   mark(Mrk);
  74.   New(Idyn);
  75.   { Sample the image over the pointset and collect data }
  76.   ImSmp(LinePs,Im,Idyn^[0],i);
  77.   { Transfer the sampled data to the array for returning }
  78.   for i := 0 to 255 do
  79.      GVal[i] := Idyn^[i];
  80.   { Clear the heap }
  81.   Release(Mrk)
  82.   end{ GetLine };
  83.  
  84. begin
  85.  Define a pointset for sampling purposes 
  86. DefWindow(Row,0,0,256,1);
  87.  Get the necessary part of the image and save it 
  88. for RowNum := 0 to 255 do
  89.    begin
  90.    { Move pointset to current sample line }
  91.    Row.Origin.Y := RowNum;
  92.    { Sample the current line / collect the Data Values }
  93.    GetLine(Row,Im,Line.i);
  94.    if Odd(RowNum) then
  95.      begin{ Write to the Disk }
  96.      { Copy to buffer }
  97.      Buffer.b[1] := Line.b;
  98.      { Actual write to disk }
  99.      Blks := BlockWrite(Fl,Buffer.i,1)
  100.      end
  101.     else{ Still to fill the Buffer }
  102.       Buffer.b[0] := Line.b
  103.    end
  104. end{ Deposit };
  105.  
  106. begin{ Save }
  107.  Open the file 
  108. Rewrite(Fl,FName);
  109.  Collect the attributes of the image 
  110. Buffer.Im := Im;
  111.  Put image attributes at the beginning of the file 
  112. Blk := BlockWrite(Fl,Buffer.Im,1);
  113.  Deal with necessary image size 
  114. case Im.Res of
  115.    Half: Deposit(Im);
  116.    Full: begin
  117.          with Im do
  118.            begin
  119.            { Split the image into 4 Half size images }
  120.            DefImage(A,Origin.X,Origin.Y,Half,LsBit,NoBits);
  121.            DefImage(B,Origin.X+256,Origin.Y,Half,LsBit,NoBits);
  122.            DefImage(C,Origin.X+256,Origin.Y+256,Half,LsBit,NoBits);
  123.            DefImage(D,Origin.X,Origin.Y+256,Half,LsBit,NoBits);
  124.            { Save the image on disk }
  125.            Deposit(A);
  126.            Deposit(B);
  127.            Deposit(C);
  128.            Deposit(D)
  129.            end{ with }
  130.          end
  131.    end{ Case };
  132.  Close the file 
  133. Close(Fl,Lock)
  134. end{ Save };
  135.  
  136.  
  137. procedure ImLd;
  138.  This procedure ReLoads a previously saved image 
  139.  
  140. var
  141.    Buffer : BufferType;
  142.    Line   : LineType;
  143.    A,B,C,D: Image;
  144.    L,N,Blk: Integer;
  145.    Error  : Boolean;
  146.  
  147.  
  148. procedure ReDraw( var Im : Image );
  149.  This procedure draws a Half size image on the screen 
  150.  
  151. var
  152.   RowNum,Blks : integer;
  153.   Row    : PointSet;
  154.  
  155.  
  156.   procedure PutRow( LinePs : PointSet;
  157.                    var Im  : Image;
  158.                    var GVal: LType );
  159.   { This procedure gets the current row and draws it }
  160.  
  161.   type
  162.     Idynarray = array[1..1] of integer;
  163.  
  164.   var
  165.     Mrk : ^integer;
  166.     Idyn: ^Idynarray;
  167.     i   : integer;
  168.  
  169.   begin
  170.   { Mark Heap and make room }
  171.   mark(Mrk);
  172.   New(Idyn);
  173.   { Get the current line }
  174.   for i := 0 to 255 do
  175.     Idyn^[i] := GVal[i];
  176.   { Draw the line }
  177.   DrawFn(LinePs,Im,Idyn^[0]);
  178.   { Tidy the Heap }
  179.   release(Mrk)
  180.   end{ PutRow };
  181.  
  182.  
  183. begin
  184.  Define a PointSet for the current line 
  185. DefWindow(Row,0,0,256,1);
  186.  Draw the Half image to screen 
  187. for RowNum := 0 to 255 do
  188.   begin
  189.   { Move the PointSet to the current Line position }
  190.   Row.Origin.Y := RowNum;
  191.   if Odd(RowNum) then
  192.     begin{ Read the Buffer }
  193.     Line.b := Buffer.b[1];
  194.     { and put on screen }
  195.     PutRow(Row,Im,Line.i)
  196.     end
  197.    else
  198.      begin{ Fill the Buffer from the Disk }
  199.      Blks := BlockRead(Fl,Buffer.i,1);
  200.      { Then read it and put on screen }
  201.      Line.b := Buffer.b[0];
  202.      PutRow(Row,Im,Line.i)
  203.      end
  204.   end
  205. end{ ReDraw };
  206.  
  207. begin
  208.  Take care of possible file name fault 
  209. (*$I-*)
  210. Reset(Fl,FName);
  211. Error := IOResult <> 0;
  212. (*$I+*)
  213.  If we have the correct file then 
  214. if not Error then
  215.   begin{ Get the details of the stored image }
  216.   Blk := BlockRead(Fl,Buffer.Im,1);
  217.   { If the stored image does not match the declared image }
  218.   if (Buffer.Im.Res <> Im.Res) then{ error }
  219.     writeln(' ReLoad : Image Resolution incompatible ')
  220.    else{ Everything ok }
  221.     begin
  222.     { Take care of image size }
  223.     case Im.Res of
  224.      Half: ReDraw(Im);
  225.      Full: begin
  226.            with Im do
  227.             begin
  228.             { Split image into 4 Half size images }
  229.             L := LsBit;N := NoBits;
  230.             DefImage(A,Origin.X,Origin.Y,Half,L,N);
  231.             DefImage(B,Origin.X+256,Origin.Y,Half,L,N);
  232.             DefImage(C,Origin.X+256,Origin.Y+256,Half,L,N);
  233.             DefImage(D,Origin.X,Origin.Y+256,Half,L,N);
  234.             { Get each image and draw it }
  235.             ReDraw(A);
  236.             ReDraw(B);
  237.             ReDraw(C);
  238.             ReDraw(D);
  239.             end{ With };
  240.            end;
  241.      end{ Case }
  242.     end;
  243.    Close(Fl)
  244.    end{ Not Error }
  245.   else{ Error in file name }
  246.     writeln(' ReLoad : Image file open error ')
  247. end{ ReLoad };
  248.  
  249.  
  250.  
  251. end{ Save }.
  252.  
  253.