home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 365.lha / MapCol_v1.4 / MapCol.c < prev    next >
C/C++ Source or Header  |  1990-04-10  |  7KB  |  208 lines

  1. /**********************************************************************
  2.    M A P C O L   -  Change workbench object colors
  3.  
  4.                 Copyright (C) 1990, John Adam Thywissen
  5.  
  6.  $Header: Rockie:Projects/Programs/Utils/RCS/MapCol.c,v 1.4 90/03/21 14:48:42 thywiss Exp $
  7.  
  8.  **********************************************************************
  9.  *                      Description And History
  10.  * 
  11.  * DESCRIPTION:
  12.  *     This program changes the colors of an Amiga Workbench Object.
  13.  *      It is intended to run from the CLI only.
  14.  * 
  15.  * ENVIRONMENT:
  16.  *     Programming Language:    ANSI C
  17.  *     Operating System:    Amiga System Software Release 1.3.2
  18.  *     Compiler:        Lattice AmigaDOS C v5.05
  19.  * 
  20.  * AUTHOR:
  21.  *     John A. Thywissen
  22.  * 
  23.  *     4205 Tuckerman Street         7 Stonegate
  24.  *     University Pk, MD  20782-2144    Houston, TX  77024-2703
  25.  * 
  26.  *     Amiga Certified Developer          bix: thywiss
  27.  * 
  28.  * DATE CREATED:  3 Mar 1990
  29.  * 
  30.  * REVISION HISTORY:
  31.  * $Log:    MapCol.c,v $
  32.  * Revision 1.4  90/03/21  14:48:42  thywiss
  33.  * Forgot to change rev # in introductory message.  (Bother!)
  34.  * 
  35.  * Revision 1.3  90/03/21  14:35:39  thywiss
  36.  * Was freeing a DiskObject I didn't have (yet)
  37.  * 
  38.  * Revision 1.2  90/03/21  13:11:39  thywiss
  39.  * Made suitable for human consumption
  40.  * Added command line color option
  41.  * 
  42.  * Revision 1.1  90/03/04  00:00:41  thywiss
  43.  * Initial Revision
  44.  * 
  45.  * AVAILABILITY:
  46.  *                 COPYRIGHT 1990, JOHN ADAM THYWISSEN
  47.  * This program may be freely distributed under certain conditions:
  48.  * 1.  This program  must be  distributed  in a  completely  UNMODIFIED
  49.  *     form.
  50.  * 2.  This  program  shall  not  be  sold.    However,  public  domain
  51.  *     librarians may charge a small fee for distribution not exceeding
  52.  *     the cost of duplication, materials, and handling.
  53.  * 3.  This  program  may  be  incorporated  into  another  program  or
  54.  *     package.    However,  the notice  of copyright  should  be  made
  55.  *     visible to the user of such program or package.  (Simply stating
  56.  *     "MapCol  ©1990,  John  A.  Thywissen"  in the  documentation  is
  57.  *     sufficient.)
  58.  * 4.  The source  need  not be  distributed with  the binary, however,
  59.  *     this availabilty statement must  always accompany the binary and
  60.  *     source code.
  61.  **********************************************************************/
  62.  
  63.  
  64.  
  65. /**********************************************************************
  66.                           Global Declarations
  67.  **********************************************************************/
  68.  
  69.  
  70.                           /* Included Files */
  71.  
  72. #include <exec/types.h>
  73. #include <proto/exec.h>
  74. #include <intuition/intuition.h>
  75. #include <workbench/workbench.h>
  76. #include <workbench/icon.h>
  77. #include <proto/icon.h>
  78. #include <stddef.h>
  79. #include <stdlib.h>
  80. #include <stdio.h>
  81.  
  82.  
  83.  
  84. /**********************************************************************
  85.                               Main Routine
  86.  **********************************************************************/
  87.  
  88. void main(argc,argv)   /* void due to Lattice Dain-Bramage */
  89. int argc;
  90. char *argv[];
  91.  
  92. {
  93.                            /* main Storage */
  94.     struct DiskObject *DO;
  95.     struct Image *theimage;
  96.     int i,j,nw,planesize,map[4],old,new;
  97.     char inbuf[255];  /* KNOWN BUG: lines > 255 char will trash stack */
  98.     
  99.                          /* main Statements */
  100.     printf("MapCol 1.4 (21 Mar 1990)  ©1990, John A. Thywissen\n");
  101.     if ((argc != 2) && (argc != 6))
  102.     {
  103.         printf("Usage: MapCol objectname [color0 color1 color2 color3]\n");
  104.         exit(20);
  105.     }
  106.     IconBase = OpenLibrary(ICONNAME,LIBRARY_VERSION);
  107.     if (IconBase == NULL)
  108.     {
  109.         printf("MapCol: Cannot open the icon library\n");
  110.         exit(21);
  111.     }
  112.     
  113.     DO = GetDiskObject(argv[1]);
  114.     if (DO == NULL)
  115.     {
  116.         printf("MapCol: Cannot open workbench object %s\n",argv[1]);
  117.         CloseLibrary(IconBase);
  118.         exit(10);
  119.     }
  120.     if (DO->do_Version != WB_DISKVERSION)
  121.     {
  122.         printf("MapCol does not understand this Disk Object version.\n");
  123.         FreeDiskObject(DO);
  124.         CloseLibrary(IconBase);
  125.         exit(5);
  126.     }
  127.     
  128.     for (i = 0; i <= 3; i++)
  129.     {
  130.         if (argc != 6)
  131.         {
  132.             printf("Map color %d to ",i);
  133.             gets(inbuf);
  134.         }
  135.         if (sscanf(((argc!=6)?inbuf:argv[i+2]),"%d",&(map[i])) != 1)
  136.         {
  137.             printf("MapCol expects an integer for color %d, but it \
  138. found \"%s\".\n",i,((argc!=6)?inbuf:argv[i+2]));
  139.             FreeDiskObject(DO);
  140.             CloseLibrary(IconBase);
  141.             exit(10);
  142.          }
  143.     }
  144.     
  145.     theimage = (struct Image *)DO->do_Gadget.GadgetRender;
  146.     printf("Height = %d\n",theimage->Height);
  147.     printf("Width = %d\n",theimage->Width);
  148.     if (theimage->Depth != 2)
  149.     {
  150.         printf("MapCol: Image Depth is not 2 bitplanes!!\n");
  151.         FreeDiskObject(DO);
  152.         CloseLibrary(IconBase);
  153.         exit(5);
  154.     }
  155.     
  156.     nw = (theimage->Width % 16)==0?theimage->Width/16:theimage->Width/16+1;
  157.     planesize = theimage->Height * nw;
  158.     for (i = 0; i < theimage->Height; i++)
  159.     {
  160.         for (j = 0; j < theimage->Width; j++)
  161.         {
  162.             old = (((theimage->ImageData)[i*nw+j/16] & (1<<(15-j%16)))?1:0)+
  163.             (((theimage->ImageData)[i*nw+j/16+planesize] & (1<<(15-j%16)))?2:0);
  164.             new = map[old];
  165.             (theimage->ImageData)[i*nw+j/16] &= ~(1<<(15-j%16));
  166.             (theimage->ImageData)[i*nw+j/16] |= (new & 1)<<(15-j%16);
  167.             (theimage->ImageData)[i*nw+j/16+planesize] &= ~(1<<(15-j%16));
  168.             (theimage->ImageData)[i*nw+j/16+planesize] |=
  169.                 ((new>>1) & 1)<<(15-j%16);
  170.         }
  171.     }
  172.     
  173.     if (DO->do_Gadget.SelectRender != NULL)
  174.     {
  175.         theimage = (struct Image *)DO->do_Gadget.SelectRender;
  176.         nw = (theimage->Width % 16)==0?theimage->Width/16:theimage->Width/16+1;
  177.         planesize = theimage->Height * nw;
  178.         for (i = 0; i < theimage->Height; i++)
  179.         {
  180.             for (j = 0; j < theimage->Width; j++)
  181.             {                 /* Yes, it's slow, but it was free, no? */
  182.                               /* Actually, next rev I will not address */
  183.                               /* every byte 24 times. Besides, what */
  184.                               /* the Global Pessamizer (oops--Optimizer) */
  185.                               /* do without this kind of stuff? */
  186.                 old = 
  187.                         (((theimage->ImageData)[i*nw+j/16] & 
  188.                         (1<<(15-j%16)))?1:0) +
  189.                         (((theimage->ImageData)[i*nw+j/16+planesize] & 
  190.                         (1<<(15-j%16)))?2:0);
  191.                 new = map[old];
  192.                 (theimage->ImageData)[i*nw+j/16] &= ~(1<<(15-j%16));
  193.                 (theimage->ImageData)[i*nw+j/16] |= (new & 1)<<(15-j%16);
  194.                 (theimage->ImageData)[i*nw+j/16+planesize] &= 
  195.                         ~(1<<(15-j%16));
  196.                 (theimage->ImageData)[i*nw+j/16+planesize] |=
  197.                         ((new>>1) & 1)<<(15-j%16);
  198.             }
  199.         }
  200.     }
  201.     
  202.     PutDiskObject(argv[1],DO);
  203.     
  204.     FreeDiskObject(DO);
  205.     
  206.     CloseLibrary(IconBase);
  207. }
  208.