home *** CD-ROM | disk | FTP | other *** search
/ World of A1200 / World_Of_A1200.iso / datafiles / text / c_manual / amiga / tools / swapcols / swapcols.c < prev    next >
C/C++ Source or Header  |  1995-02-27  |  10KB  |  235 lines

  1. /****************************************************/
  2. /*                                                  */
  3. /*     (C) - Wheelbarrow Software Ltd 1991          */
  4. /*                                                  */
  5. /*  Program     -  SwapCols                         */
  6. /*  Description -  This program swaps colours 1 & 2 */
  7. /*                 of any icons that are passed in  */
  8. /*                 as workbench parameters          */
  9. /*  Purpose     -  With the advent of WB2.0 the     */
  10. /*                 system colours changed from      */
  11. /*                 orange/black/white/blue to       */
  12. /*                 grey/black/white/blue. Icons of  */
  13. /*                 the old type appeared weird      */ 
  14. /*                 under the new colour scheme.     */
  15. /*                 After changing colours 1 & 2     */
  16. /*                 things look normal again.        */
  17. /*  Programmer  -  Paul Hammant                     */
  18. /*                 (Wheelbarrow Software)           */
  19. /*  Date        -  03/11/91                         */
  20. /*  Notes       -  This is my first useful program  */
  21. /*                 Everything else to date has been */
  22. /*                 of the "Hello World" type.       */
  23. /****************************************************/
  24. /*               **   Changes   **                  */
  25. /*                                                  */
  26. /*  Programmer  -                                   */
  27. /*  Date/Time   -                                   */
  28. /*  Changes     -                                   */
  29. /*                                                  */
  30. /****************************************************/
  31. #include <exec/types.h>
  32. #include <proto/dos.h>
  33. #include <proto/intuition.h>
  34. #include <workbench/startup.h>
  35. #include <workbench/workbench.h>
  36. #include <intuition/intuition.h>
  37. #include <stdio.h>
  38. #include <libraries/dos.h>
  39. #include <exec/memory.h>
  40.                 
  41. extern struct WBStartup *WBenchMsg;
  42. struct IconBase *IconBase;
  43.  
  44. /****************************************************/
  45. /*                                                  */
  46. /*  Function    -  SwapCols                         */
  47. /*  Description -  Colours 1 & 2 of a two bitplane  */
  48. /*                 image are swapped. The image is  */
  49. /*                 passed in as a parameter.        */
  50. /*  Used by     -  DoSwap                           */
  51. /*                                                  */
  52. /****************************************************/
  53. void SwapCols(Img)
  54. struct Image *Img;
  55. {
  56.    UWORD widint, widmod, h, w;
  57.    WORDBITS mmask, *plane0, *plane1;
  58.    if(Img){
  59.       /*  round image width up to whole number of words (16bits) */
  60.       /*  ------------------------------------------------------ */
  61.       widint = Img->Width / 16;
  62.       widmod = Img->Width % 16;
  63.       if(widmod) widint = widint +1;
  64.       /* point to the beginning of each plane */
  65.       /* ------------------------------------ */
  66.       plane0 = Img->ImageData;
  67.       plane1 = plane0 + (Img->Height * widint);
  68.       /* Loop through all the lines of the image */
  69.       /* --------------------------------------- */
  70.       for(h = 0; h < Img->Height; h += 1)
  71.       {
  72.          /* and all of the pixels across the image (16 at time) */
  73.          /* --------------------------------------------------- */
  74.          for(w = 0; w < widint; w += 1)
  75.          {
  76.             /* the bits in the 16bit work variable mmask will have a     */
  77.             /* 1 to represent where the image colour was either 1 or 2   */
  78.             /* and a 0 to represent either 0 or 3.  When this is         */
  79.             /* exclusive-ORed (^) back onto the bitplane again it        */
  80.             /* essentially flips the bits of the colours 1 & 2.          */
  81.             /* -------------------------------------------------------   */
  82.             mmask = *plane0 ^ *plane1;
  83.             *plane0 = *plane0 ^ mmask;
  84.             *plane1 = *plane1 ^ mmask;
  85.             plane0 += 1;
  86.             plane1 += 1;
  87.          }
  88.       }
  89.    }
  90. }
  91. /****************************************************/
  92. /*                                                  */
  93. /*  Function    -  DoSwap                           */
  94. /*  Description -  Initiate the colour swap for     */
  95. /*                 both the normal image and the    */
  96. /*                 selected image (not always       */
  97. /*                 present).                        */
  98. /*  Used by     -  ProcessArg                       */
  99. /*                                                  */
  100. /****************************************************/
  101. void DoSwap(GadG)
  102. struct Gadget GadG;
  103. {
  104.    SwapCols(GadG.GadgetRender);
  105.    SwapCols(GadG.SelectRender);
  106. }
  107. /****************************************************/
  108. /*                                                  */
  109. /*  Function    -  ProcessArg                       */
  110. /*  Description -  This function processes a WB     */
  111. /*                 argument passed in as a parm     */
  112. /*                 and read the relevent .info      */
  113. /*                 file (Icon) off disk. This is    */
  114. /*                 then processed and written back  */
  115. /*                 to disk.                         */
  116. /*  Used by     -  ProcessWBArgs                    */
  117. /*                                                  */
  118. /****************************************************/
  119. void ProcessArg(ArgLock,ArgName)
  120. struct FileLock *ArgLock;
  121. STRPTR ArgName;
  122. {
  123.    BOOL Written;
  124.    struct FileLock *ParLock, *OurLock;
  125.    STRPTR OurName;
  126.    struct DiskObject *DObj;
  127.    struct FileInfoBlock *fib_ptr;
  128.    fib_ptr = ParLock = NULL;
  129.    OurLock = ArgLock;
  130.    OurName = ArgName;
  131.    /* if no name passed in as parm then it must be a drawer or disk icon */
  132.    /* ------------------------------------------------------------------ */
  133.    if(! *OurName){
  134.       /* Get enough memory for a File Info Block structure */
  135.       /* ------------------------------------------------- */
  136.       fib_ptr = (struct FileInfoBlock *)
  137.          AllocMem( sizeof(struct FileInfoBlock),
  138.          MEMF_PUBLIC|MEMF_CLEAR);
  139.       /* Examine the lock ( results are now at fib_ptr ) */
  140.       /* ----------------------------------------------- */
  141.       Examine( OurLock, fib_ptr );
  142.       /* The file name ( without path ) is in the fileinfoblock */
  143.       /* ------------------------------------------------------ */
  144.       OurName = fib_ptr->fib_FileName;
  145.       /* As the file is without path we will have to get the lock */
  146.       /* for the parent directory                                 */
  147.       /* ---------------------------------------------------------*/
  148.       if(ParLock = (struct FileLock *) ParentDir(OurLock)){
  149.          OurLock = ParLock;
  150.       }
  151.       else
  152.       {
  153.          /* no parent dir exists so we must be dealing with the disk icon*/
  154.          /* so leave the lock where it is and "hardcode" the name        */
  155.          OurName = (STRPTR)"Disk";
  156.       }
  157.    }
  158.    printf("%s .",OurName);
  159.    /* Set the current dir ( All our file names don't have full paths */
  160.    /* -------------------------------------------------------------- */
  161.    CurrentDir(OurLock);
  162.    /* Get the DiskObjectstructure for our object */
  163.    /* ------------------------------------------ */
  164.    if(!(DObj = (struct DiskObject *) GetDiskObject(OurName))){
  165.       printf("\10not retrieved for some reason.\n");
  166.    }
  167.    else
  168.    {
  169.       /* Process the disk object */
  170.       /* ----------------------- */
  171.       DoSwap(DObj->do_Gadget);
  172.       /* Put it back */
  173.       /* ----------- */
  174.       Written = PutDiskObject(OurName,DObj);
  175.       /* Free up the memory that GetDiskObject Allocated */
  176.       /* ----------------------------------------------- */
  177.       FreeDiskObject(DObj);
  178.       if(!Written) printf("\10not updated.\n");
  179.       else printf("\10changed.\n");
  180.    }
  181.    /* If we had a parent lock the unlock it */
  182.    /* ------------------------------------- */
  183.    if(ParLock) UnLock(ParLock);
  184.    /* If we had a file info block then free the memory */
  185.    /* ------------------------------------------------ */
  186.    if(fib_ptr) FreeMem(fib_ptr, sizeof(struct FileInfoBlock));
  187. }
  188. /****************************************************/
  189. /*                                                  */
  190. /*  Function    -  ProcessWBArgs                    */
  191. /*  Description -  This function loops through all  */
  192. /*                 the workbench args passed in.    */
  193. /*  Used by     -  main                             */
  194. /*                                                  */
  195. /****************************************************/
  196. void ProcessWBArgs(msg)
  197. struct WBStartup *msg;
  198. {
  199.    struct WBArg *arg;
  200.    UWORD i;
  201.    IconBase = (struct IconBase *) OpenLibrary("icon.library",0);
  202.    for(i=0,arg=msg->sm_ArgList;i<msg->sm_NumArgs;i++,arg++){
  203.       if(i!=0) {
  204.          ProcessArg(arg->wa_Lock,arg->wa_Name);
  205.       }
  206.    }
  207.    if(msg->sm_NumArgs == 1){
  208.       printf("To Use:\n1. Click once on this tool.\n2. Press (and hold down) the shift key.\n3. Click once on each icon to change.\n4. Double click on the last icon.\n5. Close & reopen the affected windows.");
  209.       Delay(3*50);
  210.    }
  211.    CloseLibrary(IconBase);
  212. }
  213. /****************************************************/
  214. /*                                                  */
  215. /*  Function    -  main                             */
  216. /*  Description -  This returns an error message    */
  217. /*                 if invoked from the CLI or       */
  218. /*                 process the WB arguments.        */
  219. /*                                                  */
  220. /****************************************************/
  221. main(argc,argv)
  222. int argc;
  223. char **argv;
  224. {
  225.    if(argc>0){
  226.       printf("Sorry Folks This Is a WorkBench Only Tool\n");
  227.       exit(10);
  228.    }
  229.    else{
  230.       ProcessWBArgs(WBenchMsg);
  231.       Delay(2*50);
  232.    }
  233. }
  234.   /******************  End Of Source ***********************/
  235.