home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 314.lha / TMP_to_DP / TmpToDP.mod < prev    next >
Encoding:
Text File  |  1989-12-01  |  8.9 KB  |  251 lines

  1. MODULE TmpToDP;
  2.  
  3. (*======================================================================*)
  4. (*                        TmpToDP v1.0                                  *)
  5. (*======================================================================*)
  6. (*       Copyright (c) 1989 Chris Bailey, All Rights Reserved           *)
  7. (*======================================================================*)
  8. (*      Version: 1.00           Author : Chris Bailey                   *)
  9. (*      Date   : 23-Oct-89                                              *)
  10. (*======================================================================*)
  11. (*  Source note : there are a few conventions in here which I am not    *)
  12. (*  really sure of, but the program is very very stable the way it is.  *)
  13. (*  The main thing that gave me problems was getting the message format *)
  14. (*  right.  One of the keys to this seemed to be setting the reply port *)
  15. (*  name to "REMO".  This may or may not be necessary, but it works,    *)
  16. (*  and the program as it stands meets my needs.                        *)
  17. (*======================================================================*)
  18. (*  Link with RT.lnk                                                    *)
  19. (*======================================================================*)
  20.  
  21. FROM SYSTEM             IMPORT  ADR, TSIZE, BYTE, SHIFT, ADDRESS, STRPTR,
  22.                                 WORD;
  23. FROM Tasks              IMPORT  Wait, SetSignal, SignalSet;
  24. FROM DOS                IMPORT  SIGBreakC;
  25. FROM Nodes              IMPORT  NodeType;
  26. FROM Ports              IMPORT  WaitPort, GetMsg, ReplyMsg, PutMsg,
  27.                                 MsgPortPtr, FindPort, Message, MessagePtr;
  28. FROM Memory             IMPORT  AllocMem, FreeMem, MemReqSet, MemReqs;
  29. FROM Conversions        IMPORT  ConvNumToStr;
  30. FROM TermOut            IMPORT  WriteString, WriteCard, WriteLongInt, WriteHex;
  31. FROM Strings            IMPORT  LengthStr, AppendSubStr, AssignStr;
  32. FROM PortUtils          IMPORT  CreatePort, DeletePort;
  33. FROM BufferedDOS        IMPORT  BufHandle, BufOpen, BufClose, ModeOldFile,
  34.                                 BufRead;
  35. FROM CmdLineUtils       IMPORT  argc, argv;
  36.  
  37. CONST
  38.   BANNER = "\x9b33;1mTmpToDP\x9b0m V1.0, 23-Oct-89\nCopyright © 1989 Chris Bailey, All Rights Reserved\n\n";
  39.  
  40. TYPE rgbs = (red, green, blue);
  41.  
  42.      (* header format for temp file *)
  43.      HeaderType = RECORD
  44.         Width, Height : CARDINAL;
  45.         NumRegisters : CARDINAL;
  46.      END;
  47.  
  48.      (* we need to do this so that we don't run into             *)
  49.      (* alignment problems.  This way we have an even # of bytes *)
  50.      Colors = RECORD
  51.        r, g, b : BYTE;
  52.        r2,g2,b2: BYTE;
  53.      END;
  54.  
  55.      (* max of 256 colors *)
  56.      ColorType = RECORD
  57.        colors : ARRAY[0..127] OF Colors;
  58.      END;
  59.  
  60.      (* our extension part of the message *)
  61.      Mess = RECORD
  62.        Header : ARRAY[0..7] OF CHAR;  (* REMODlva *)
  63.        Count  : WORD; (* how many RGB triplets     *)
  64.        Line   : WORD; (* which line # to render on *)
  65.        RGBs   : ARRAY[0..1023] OF ARRAY[red..blue] OF WORD;
  66.        (* that's the image data, max of 1024 pixels wide *)
  67.      END;
  68.  
  69.      (* message we will send to DigiPaint *)
  70.      SendMsgPtr = POINTER TO SendMsg;
  71.      SendMsg = RECORD
  72.        header : Message; (* normal exec message *)
  73.        stuff  : Mess;    (* our extension       *)
  74.       END;
  75.  
  76. VAR file : BufHandle;              (* filehandle for input file      *)
  77.     len  : LONGINT;                (* used for dos reads             *)
  78.     header : HeaderType;           (* file header                    *)
  79.     ColorTable : ColorType;        (* colortable from the file       *)
  80.     line : ARRAY[0..1023] OF BYTE; (* one line of data               *)
  81.     j, LineNum : CARDINAL;         (* current line number, tmp value *)
  82.     OurPort : MsgPortPtr;          (* msg port to communicate w/DP   *)
  83.     Stuff : Mess;                  (* a temp message to fill in      *)
  84.     DPPort : STRPTR;               (* portname for DigiPaint         *)
  85.  
  86. (*******************
  87.  * Send one line of data to DigiPaint 3's port which has already
  88.  * been filled in by previous procedures
  89.  *)
  90.  
  91. PROCEDURE SendData();
  92. VAR
  93.     HostPort : MsgPortPtr;  (* digipaint host pointer     *)
  94.     rep, msg : SendMsgPtr;  (* our message, reply message *)
  95. BEGIN
  96.   (* could speed up program by only finding the port once the   *)
  97.   (* entire program, but this way somebody doesn't pull the rug *)
  98.   (* out from under us.                                         *)
  99.  
  100.   HostPort := FindPort(DPPort);
  101.   IF HostPort # NIL THEN
  102.     msg := AllocMem(TSIZE(SendMsg),MemReqSet{MemPublic,MemClear});
  103.     IF msg # NIL THEN
  104.       msg^.stuff := Stuff;
  105.       msg^.header.mnNode.lnName := ADR("REMO");
  106.       msg^.header.mnNode.lnType := NTMessage;
  107.       msg^.header.mnReplyPort := OurPort;
  108.       msg^.header.mnLength := TSIZE(SendMsg);
  109.       PutMsg(HostPort,msg);
  110.  
  111.       (* wait for a reply to our message *)
  112.       LOOP
  113.         rep := WaitPort(OurPort);
  114.         rep := GetMsg(OurPort);
  115.         IF rep # NIL THEN
  116.           IF (rep = msg) & (rep^.header.mnNode.lnType = NTReplyMsg) THEN
  117.             EXIT;
  118.           ELSE
  119.             ReplyMsg(rep);
  120.           END;
  121.         END; (* reply # NIL *)
  122.       END; (* loop *)
  123.  
  124.       FreeMem(msg,TSIZE(SendMsg));  (* dealloc message *)
  125.     ELSE
  126.       WriteString("\n** Couldn't allocate message.\n");
  127.     END;
  128.   ELSE
  129.     WriteString("\n** Can't find DigiPaint 3's Port\n");
  130.   END;
  131.  
  132. END SendData;
  133.  
  134. (*******************
  135.  * Figure out an Amiga RGB value from the data we read in
  136.  *)
  137.  
  138. PROCEDURE GetColor(col : BYTE; rgb : rgbs) : CARDINAL;
  139. VAR which : CARDINAL;
  140.     temp : BYTE;
  141.     ret : CARDINAL;
  142. BEGIN
  143.     which := CARDINAL(col);
  144.     IF (which MOD 2) = 0 THEN
  145.       CASE rgb OF
  146.        |red  : temp := ColorTable.colors[which DIV 2].r;
  147.        |green: temp := ColorTable.colors[which DIV 2].g;
  148.        |blue : temp := ColorTable.colors[which DIV 2].b;
  149.       END;
  150.     ELSE
  151.       CASE rgb OF
  152.        |red  : temp := ColorTable.colors[which DIV 2].r2;
  153.        |green: temp := ColorTable.colors[which DIV 2].g2;
  154.        |blue : temp := ColorTable.colors[which DIV 2].b2;
  155.       END;
  156.     END;
  157.  
  158.   ret := CARDINAL(temp);
  159.   ret := ret * 4;       (* Convert from 0..1023 to 0..4095 the easy way *)
  160.   RETURN(ret);
  161. END GetColor;
  162.  
  163. (*******************
  164.  * Convert and send one line of data from the tmp file to DigiPaint 3
  165.  *)
  166.  
  167. PROCEDURE WriteLine();
  168. VAR i : CARDINAL;
  169.     bool : BOOLEAN;
  170. BEGIN
  171.   (* fill in our temp message *)
  172.   Stuff.Header := "REMODlvb";
  173.   Stuff.Count := header.Width;
  174.   Stuff.Line := LineNum;
  175.   FOR i := 0 TO (header.Width-1) DO
  176.     Stuff.RGBs[i][red] := INTEGER(GetColor(line[i],red));
  177.     Stuff.RGBs[i][green] := INTEGER(GetColor(line[i],green));
  178.     Stuff.RGBs[i][blue] := INTEGER(GetColor(line[i],blue));
  179.    END;
  180.   i := header.Width;
  181.   (* fill things out to an even multiple of 32, like DigiPaint needs *)
  182.   WHILE((i MOD 32) # 0) DO
  183.     Stuff.RGBs[i][red] := 0;
  184.     Stuff.RGBs[i][green] := 0;
  185.     Stuff.RGBs[i][blue] := 0;
  186.     INC(i); INC(INTEGER(Stuff.Count));
  187.   END;
  188.   WriteString("\x9b13\x44\x9b\x4aSending....");
  189.   SendData();
  190.   WriteString("\x9b8\x44\x9b\x4at.\r");
  191. END WriteLine;
  192.  
  193. (*******************
  194.  * See if the user pressed ctrl-C
  195.  *)
  196.  
  197. PROCEDURE Break(): BOOLEAN;
  198. BEGIN
  199.   RETURN SIGBreakC IN SetSignal(SignalSet{},SignalSet{SIGBreakC});
  200. END Break;
  201.  
  202. BEGIN
  203.   WriteString(BANNER);
  204.  
  205.   IF argc = 3 THEN  (* only 1 way to call this *)
  206.     DPPort := argv[2];
  207.     IF BufOpen(file,argv[1],1024,ModeOldFile) THEN  (* open the file *)
  208.       OurPort := CreatePort(ADR("REMO"),0);
  209.       IF FindPort(DPPort) # NIL THEN  (* quick check to see if port exists *)
  210.         IF OurPort # NIL THEN (* now we're ready to start *)
  211.           len := BufRead(file,ADR(header),TSIZE(HeaderType));
  212.           WriteString("Picture is ");
  213.           WriteCard(header.Width,1);
  214.           WriteString("x");
  215.           WriteCard(header.Height,1);
  216.           WriteString("x");
  217.           WriteCard(header.NumRegisters,1);
  218.           WriteString(" colors.\n");     (* 1 record per 2 colors, 6 bytes per record *)
  219.           len := BufRead(file,ADR(ColorTable),(header.NumRegisters DIV 2) * 6);
  220.           WriteString("Color table loaded\n\n");
  221.           LineNum := 0;
  222.           REPEAT
  223.             len := BufRead(file,ADR(line),header.Width);
  224.             WriteString("Line "); WriteCard(LineNum,1); WriteString(" converting...");
  225.             WriteLine();
  226.             INC(LineNum);
  227.           UNTIL(LineNum = header.Height) OR Break();
  228.           IF (LineNum = header.Height) THEN
  229.             WriteString("\nLooks like everything functioned normally.\n");
  230.           ELSE
  231.             WriteString("^C\nProgram halted.\n");
  232.           END;
  233.           DeletePort(OurPort);
  234.         ELSE
  235.           WriteString("Can't open required message port.\n");
  236.         END;
  237.         IF BufClose(file) THEN END;
  238.       ELSE
  239.         WriteString("Can't find DigiPaint's port.\n");
  240.       END;
  241.     ELSE
  242.       WriteString("Error opening file.\n");
  243.     END;
  244.   ELSE
  245.     WriteString("Invalid arguments.\nUsage: ");
  246.     WriteString(argv[0]^);
  247.     WriteString(" <temp file> <digipaint portname>\n");
  248.   END;
  249.  
  250. END TmpToDP.
  251.