home *** CD-ROM | disk | FTP | other *** search
/ Fish 'n' More 2 / fishmore-publicdomainlibraryvol.ii1991xetec.iso / disks / disk372.lzh / RemapIcon / RemapIcon.c < prev    next >
C/C++ Source or Header  |  1990-10-08  |  11KB  |  434 lines

  1. /* $Revision Header * Header built automatically - do not edit! *************
  2.  *
  3.  *    (C) Copyright 1990 by MXM
  4.  *
  5.  *    Name .....: RemapIcon.c
  6.  *    Created ..: Tuesday 26-Jun-90 14:19
  7.  *    Revision .: 1
  8.  *
  9.  *    Date            Author          Comment
  10.  *    =========       ========        ====================
  11.  *    26-Jun-90       Olsen           Added Arp interface
  12.  *    26-Jun-90       Olsen           Created this file!
  13.  *
  14.  * $Revision Header ********************************************************/
  15.  
  16. #define __NO_PRAGMAS 1
  17.  
  18. #include <workbench/workbench.h>
  19. #include <libraries/arpbase.h>
  20. #include <hardware/blit.h>
  21. #include <exec/memory.h>
  22. #include <functions.h>
  23. #include <fcntl.h>
  24.  
  25.     /* Arp command line data. */
  26.  
  27. char *CLI_Template    = "File,DIR/K,TO/K,INFO/S";
  28. char *CLI_Help        = "\nUsage: RemapIcon [FILE <Filename without .info>] [TO <Directory name>]\
  29. \n                 [DIR <Directory name>] [TO <Directory name>]\
  30. \n                 [INFO]\
  31. \n\n                 Note: if the TO-argument is omitted, the icons will\
  32. \n                       be written to the source directory.\n\n";
  33.  
  34.     /* Argument vector offsets. */
  35.  
  36. #define ARG_FILE    1
  37. #define ARG_DIR        2
  38. #define ARG_TO        3
  39. #define ARG_INFO    4
  40.  
  41.     /* Icon.library base. */
  42.  
  43. struct Library *IconBase;
  44.  
  45.     /* Calculate the amount of memory needed for a row of pixels. */
  46.  
  47. #define byte(width) (((width + 15) >> 4) << 1)
  48.  
  49.     /* This is where the colour will go to. */
  50.  
  51. struct RastPort    ColourRPort;
  52. struct BitMap    ColourBitMap;
  53.  
  54.     /* The icon image will be turned into a RastPort. */
  55.  
  56. struct RastPort    ImageRPort;
  57. struct BitMap    ImageBitMap;
  58.  
  59.     /* Temporary mask and masks for both colours (1 and 2). */
  60.  
  61. struct BitMap    TempMask,ColourMask1,ColourMask2;
  62.  
  63.     /* RemapImage(struct Image *Icon):
  64.      *
  65.      *    This routine will exchange the colours of the white
  66.      *    and the black pixels in an image.
  67.      */
  68.  
  69. BYTE
  70. RemapImage(struct Image *Icon)
  71. {
  72.     BYTE    Success = FALSE;
  73.     SHORT    i;
  74.  
  75.         /* Transform the image into a RastPort. */
  76.  
  77.     InitRastPort(&ImageRPort);
  78.     InitBitMap(&ImageBitMap,Icon -> Depth,Icon -> Width,Icon -> Height);
  79.     ImageRPort . BitMap = &ImageBitMap;
  80.  
  81.         /* Set the planes accordingly. */
  82.  
  83.     for(i = 0 ; i < Icon -> Depth ; i++)
  84.         ImageBitMap . Planes[i] = (PLANEPTR)((ULONG)Icon -> ImageData + i * byte(Icon -> Width) * Icon -> Height);
  85.  
  86.         /* Alas, you cannot perform an area fill through a mask wider
  87.          * than sixteen pixels. For this reason we will create a
  88.          * RastPort whose BitMap has the colour we want the
  89.          * mask area to be filled with.
  90.          */
  91.  
  92.     InitRastPort(&ColourRPort);
  93.     InitBitMap(&ColourBitMap,Icon -> Depth,Icon -> Width,Icon -> Height);
  94.     ColourRPort . BitMap = &ColourBitMap;
  95.  
  96.         /* Initialize the mask BitMaps. */
  97.  
  98.     InitBitMap(&TempMask,Icon -> Depth,Icon -> Width,Icon -> Height);
  99.     InitBitMap(&ColourMask1,Icon -> Depth,Icon -> Width,Icon -> Height);
  100.     InitBitMap(&ColourMask2,Icon -> Depth,Icon -> Width,Icon -> Height);
  101.  
  102.         /* Allocate memory for the colour BitMap. */
  103.  
  104.     if(ColourBitMap . Planes[0] = (PLANEPTR)AllocMem(Icon -> Depth * byte(Icon -> Width) * Icon -> Height,MEMF_PUBLIC | MEMF_CHIP | MEMF_CLEAR))
  105.     {
  106.         for(i = 1 ; i < Icon -> Depth ; i++)
  107.             ColourBitMap . Planes[i] = (PLANEPTR)((ULONG)ColourBitMap . Planes[0] + i * byte(Icon -> Width) * Icon -> Height);
  108.  
  109.             /* Allocate memory for the mask for colour 1. */
  110.  
  111.         if(ColourMask1 . Planes[0] = (PLANEPTR)AllocMem(byte(Icon -> Width) * Icon -> Height,MEMF_PUBLIC | MEMF_CHIP | MEMF_CLEAR))
  112.         {
  113.             for(i = 1 ; i < Icon -> Depth ; i++)
  114.                 ColourMask1 . Planes[i] = ColourMask1 . Planes[0];
  115.  
  116.                 /* Allocate memory for the mask for colour 2. */
  117.  
  118.             if(ColourMask2 . Planes[0] = (PLANEPTR)AllocMem(byte(Icon -> Width) * Icon -> Height,MEMF_PUBLIC | MEMF_CHIP | MEMF_CLEAR))
  119.             {
  120.                 for(i = 1 ; i < Icon -> Depth ; i++)
  121.                     ColourMask2 . Planes[i] = ColourMask2 . Planes[0];
  122.  
  123.                     /* Allocate memory for the temporary mask. */
  124.  
  125.                 if(TempMask . Planes[0] = (PLANEPTR)AllocMem(byte(Icon -> Width) * Icon -> Height,MEMF_PUBLIC | MEMF_CHIP | MEMF_CLEAR))
  126.                 {
  127.                     Success = TRUE;
  128.  
  129.                     for(i = 1 ; i < Icon -> Depth ; i++)
  130.                         TempMask . Planes[i] = TempMask . Planes[0];
  131.  
  132.                         /* Perform an OR-operation on all the planes associated with colour 1. */
  133.  
  134.                     BltBitMap(&ImageBitMap,0,0,&ColourMask1,0,0,Icon -> Width,Icon -> Height,0xE0,1,NULL);
  135.  
  136.                         /* Clear all pixels in the mask which belong to other colours. */
  137.  
  138.                     BltBitMap(&ImageBitMap,0,0,&ColourMask1,0,0,Icon -> Width,Icon -> Height,0x80,1,NULL);
  139.  
  140.                         /* Perform an OR-operation on all planes which do not belong to colour 1. */
  141.  
  142.                     BltBitMap(&ImageBitMap,0,0,&TempMask,0,0,Icon -> Width,Icon -> Height,0xE0,0xFF ^ 1,NULL);
  143.  
  144.                         /* Clear all pixels which do not belong to colour 1. */
  145.  
  146.                     BltBitMap(&TempMask,0,0,&ColourMask1,0,0,Icon -> Width,Icon -> Height,0x20,0xFF,NULL);
  147.  
  148.                         /* Clear the temporary mask. */
  149.  
  150.                     BltClear(TempMask . Planes[0],byte(Icon -> Width) * Icon -> Height,1);
  151.  
  152.                         /* Perform an OR-operation on all the planes associated with colour 2. */
  153.  
  154.                     BltBitMap(&ImageBitMap,0,0,&ColourMask2,0,0,Icon -> Width,Icon -> Height,0xE0,2,NULL);
  155.  
  156.                         /* Clear all pixels in the mask which belong to other colours. */
  157.  
  158.                     BltBitMap(&ImageBitMap,0,0,&ColourMask2,0,0,Icon -> Width,Icon -> Height,0x80,2,NULL);
  159.  
  160.                         /* Perform an OR-operation on all planes which do not belong to colour 2. */
  161.  
  162.                     BltBitMap(&ImageBitMap,0,0,&TempMask,0,0,Icon -> Width,Icon -> Height,0xE0,0xFF ^ 2,NULL);
  163.  
  164.                         /* Clear all pixels which do not belong to colour 1. */
  165.  
  166.                     BltBitMap(&TempMask,0,0,&ColourMask2,0,0,Icon -> Width,Icon -> Height,0x20,0xFF,NULL);
  167.  
  168.                         /* Replace colour 2 with colour 1. */
  169.  
  170.                     SetRast(&ColourRPort,1);
  171.  
  172.                     BltMaskBitMapRastPort(&ColourBitMap,0,0,&ImageRPort,0,0,Icon -> Width,Icon -> Height,(ABC|ABNC|ANBC),(APTR)ColourMask2 . Planes[0]);
  173.  
  174.                         /* Replace colour 1 with colour 2. */
  175.  
  176.                     SetRast(&ColourRPort,2);
  177.  
  178.                     BltMaskBitMapRastPort(&ColourBitMap,0,0,&ImageRPort,0,0,Icon -> Width,Icon -> Height,(ABC|ABNC|ANBC),(APTR)ColourMask1 . Planes[0]);
  179.  
  180.                     FreeMem(TempMask . Planes[0],byte(Icon -> Width) * Icon -> Height);
  181.                 }
  182.  
  183.                 FreeMem(ColourMask2 . Planes[0],byte(Icon -> Width) * Icon -> Height);
  184.             }
  185.  
  186.             FreeMem(ColourMask1 . Planes[0],byte(Icon -> Width) * Icon -> Height);
  187.         }
  188.  
  189.         FreeMem(ColourBitMap . Planes[0],Icon -> Depth * byte(Icon -> Width) * Icon -> Height);
  190.     }
  191.  
  192.     return(Success);
  193. }
  194.  
  195.     /* RemapIcon(char *Source,char *Dest):
  196.      *
  197.      *    This routine loads the approriate icon file, causes
  198.      *    the image(s) to be remapped and saves the resulting
  199.      *    file back to disk.
  200.      */
  201.  
  202. BYTE
  203. RemapIcon(char *Source,char *Dest)
  204. {
  205.     struct DiskObject    *Icon;
  206.     BYTE             Success = FALSE;
  207.  
  208.     Printf("Remapping icon %s.info to %s.info... ",Source,Dest);
  209.  
  210.         /* Read the icon file. */
  211.  
  212.     if(Icon = GetDiskObject(Source))
  213.     {
  214.             /* Remap the standard image. */
  215.  
  216.         if(RemapImage((struct Image *)Icon -> do_Gadget . GadgetRender))
  217.         {
  218.             Success = TRUE;
  219.  
  220.                 /* If it has an alternate image, remap it as well. */
  221.  
  222.             if(Icon -> do_Gadget . SelectRender)
  223.             {
  224.                 if(!RemapImage((struct Image *)Icon -> do_Gadget . SelectRender))
  225.                 {
  226.                     Printf("ERROR (%ld): Cannot remap icon select image.\a\n",ERROR_NO_FREE_STORE);
  227.                     Success = FALSE;
  228.                 }
  229.             }
  230.  
  231.                 /* If the remapping process succeeded, save
  232.                  * the icon back to disk.
  233.                  */
  234.  
  235.             if(Success)
  236.             {
  237.                 if(!PutDiskObject(Dest,Icon))
  238.                 {
  239.                     Printf("ERROR (%ld): Cannot create file \"%s.info\".\a\n",IoErr(),Dest);
  240.                     Success = FALSE;
  241.                 }
  242.             }
  243.         }
  244.         else
  245.             Printf("ERROR (%ld): Cannot remap icon image.\a\n",ERROR_NO_FREE_STORE);
  246.  
  247.             /* Release the icon. */
  248.  
  249.         FreeDiskObject(Icon);
  250.     }
  251.     else
  252.         Printf("ERROR (%ld): Cannot read file \"%s.info\".\a\n",IoErr(),Source);
  253.  
  254.     if(Success)
  255.         Puts("done.");
  256.  
  257.     return(Success);
  258. }
  259.  
  260. LONG Chk_Abort(VOID) { return(0); }
  261. VOID _wb_parse(VOID) {}
  262.  
  263. VOID
  264. main(int argc,char **argv)
  265. {
  266.     BYTE Success = RETURN_OK;
  267.  
  268.         /* Disable the standard ^C checking. */
  269.  
  270.     Enable_Abort = FALSE;
  271.  
  272.         /* Display program version. */
  273.  
  274.     Puts("\n\33[1m\33[33mRemapIcon V1.1 \33[0m\33[31mCopyright © 1990 by MXM, all rights reserved\n");
  275.  
  276.         /* User wants info? */
  277.  
  278.     if(argv[ARG_INFO])
  279.     {
  280.         Puts("This  program remaps icon images to fit the different colour");
  281.         Puts("palettes used by Amiga computers equipped with Kickstart 2.x");
  282.         Puts("and  1.3.   The  colours of pixels painted in colour 1 and 2");
  283.         Puts("are  swapped,   so  pre-2.x  icons   and  2.x  icons  can be");
  284.         Puts("exchanged between both Workbench releases.\n");
  285.  
  286.         Puts("Author: Olaf Barthel, MXM");
  287.         Puts("        Brabeckstrasse 35");
  288.         Puts("        D-3000 Hannover 71\n");
  289.  
  290.         Puts("        Federal Republic of Germany\n");
  291.  
  292.         Puts("Written at Hannover, Monday 25-Jun-90.\n");
  293.  
  294.         Puts("This  program  is  Share-Ware,  if  you  like  it and use it");
  295.         Puts("frequently  a  small  donation  will  entitle you to receive");
  296.         Puts("updates and new programs from MXM.\n");
  297.  
  298.         exit(RETURN_OK);
  299.     }
  300.  
  301.         /* Do we have a valid command line? */
  302.  
  303.     if(!argv[ARG_FILE] && !argv[ARG_DIR])
  304.     {
  305.         Puts(CLI_Help);
  306.         exit(RETURN_WARN);
  307.     }
  308.  
  309.         /* Open icon.library. */
  310.  
  311.     if(IconBase = OpenLibrary("icon.library",0))
  312.     {
  313.         char DestName[256];
  314.  
  315.             /* Convert a single file? */
  316.  
  317.         if(argv[ARG_FILE])
  318.         {
  319.                 /* Do we have a path to write it to? */
  320.  
  321.             if(argv[ARG_TO])
  322.             {
  323.                 strcpy(DestName,argv[ARG_TO]);
  324.                 TackOn(DestName,BaseName(argv[ARG_FILE]));
  325.             }
  326.             else
  327.             {
  328.                 SHORT i;
  329.  
  330.                 strcpy(DestName,argv[ARG_FILE]);
  331.  
  332.                 for(i = strlen(DestName) - 1 ; i >= 0 ; i--)
  333.                 {
  334.                     if(DestName[i] == ':' || DestName[i] == '/')
  335.                         break;
  336.                     else
  337.                         DestName[i] = 0;
  338.                 }
  339.  
  340.                 strcat(DestName,BaseName(argv[ARG_FILE]));
  341.             }
  342.  
  343.                 /* Remap the icon. */
  344.  
  345.             if(!RemapIcon(argv[ARG_FILE],DestName))
  346.                 Success = RETURN_FAIL;
  347.         }
  348.         else
  349.         {
  350.             char SourceName[256],Pattern[256],*Pointer;
  351.  
  352.                 /* Build search path and pattern. */
  353.  
  354.             strcpy(Pattern,argv[ARG_DIR]);
  355.             TackOn(Pattern,"*.info");
  356.  
  357.                 /* Scan the directory for .info files. */
  358.  
  359.             while(Pointer = scdir(Pattern))
  360.             {
  361.                     /* ^C = Abort scanning. */
  362.  
  363.                 if(SetSignal(0,0) & SIGBREAKF_CTRL_C)
  364.                 {
  365.                     SetSignal(0,SIGBREAKF_CTRL_C);
  366.  
  367.                     Puts("*** Break\a");
  368.                     break;
  369.                 }
  370.  
  371.                     /* ^D skip current file. */
  372.  
  373.                 if(SetSignal(0,0) & SIGBREAKF_CTRL_D)
  374.                 {
  375.                     SetSignal(0,SIGBREAKF_CTRL_D);
  376.                     continue;
  377.                 }
  378.  
  379.                     /* Is it an icon file? */
  380.  
  381.                 if(strlen(BaseName(Pointer)) > 5)
  382.                 {
  383.                     strcpy(SourceName,Pointer);
  384.  
  385.                         /* Skip the .info. */
  386.  
  387.                     SourceName[strlen(SourceName) - 5] = 0;
  388.  
  389.                         /* Do we have a path to write it to? */
  390.  
  391.                     if(argv[ARG_TO])
  392.                     {
  393.                         strcpy(DestName,argv[ARG_TO]);
  394.                         TackOn(DestName,BaseName(SourceName));
  395.                     }
  396.                     else
  397.                     {
  398.                         SHORT i;
  399.  
  400.                         strcpy(DestName,SourceName);
  401.  
  402.                         for(i = strlen(DestName) - 1 ; i >= 0 ; i--)
  403.                         {
  404.                             if(DestName[i] == ':' || DestName[i] == '/')
  405.                                 break;
  406.                             else
  407.                                 DestName[i] = 0;
  408.                         }
  409.  
  410.                         strcat(DestName,BaseName(SourceName));
  411.                     }
  412.  
  413.                         /* Remap the icon. */
  414.  
  415.                     RemapIcon(SourceName,DestName);
  416.                 }
  417.             }
  418.         }
  419.  
  420.             /* Close the library. */
  421.  
  422.         CloseLibrary(IconBase);
  423.     }
  424.     else
  425.     {
  426.         Printf("ERROR (%ld): Cannot open icon.library.\a\n",IoErr());
  427.         Success = RETURN_FAIL;
  428.     }
  429.  
  430.         /* And return. */
  431.  
  432.     exit(Success);
  433. }
  434.