home *** CD-ROM | disk | FTP | other *** search
/ Amiga MA Magazine 1998 #6 / amigamamagazinepolishissue1998.iso / coders / iffconverter / palettemaintenance.c < prev    next >
C/C++ Source or Header  |  1997-01-07  |  5KB  |  169 lines

  1. /*
  2. **     $VER: PaletteMaintenance.c V0.01 (21-06-95)
  3. **
  4. **     Author:  Gerben Venekamp
  5. **     Updates: 21-06-95  Version 0.02     Intial module
  6. **
  7. **  PaletteMaintenanace.c cointains function to control and maintain
  8. **  palettes.
  9. **
  10. */
  11.  
  12.  
  13. #include <exec/memory.h>
  14. #include <graphics/view.h>
  15. #include <intuition/screens.h>
  16. #include <proto/dos.h>
  17. #include <proto/graphics.h>
  18.  
  19. #include "IFFConverter.h"
  20.  
  21.  
  22. // Defining protos
  23.  
  24. void FadeColours(enum Fade, UWORD, struct Screen*);
  25. BOOL GetNewColourMap(UBYTE *, UWORD);
  26.  
  27.  
  28. /*
  29. **  Result = GetNewColourMap(CMapData, PaletteDepth)
  30. **
  31. **     GetNewColourMap makes from 'CMapData' a colour palette, which
  32. **     can be used by 'LoadRGB32.
  33. **
  34. **  pre:  CMapData - Pointer to a ILBM CMAP Chunk.
  35. **        PaletteDepth - Depth of palette. (in planes).
  36. **  post: Result - TRUE if a new colour paltte could be generated,
  37. **                 FALSE if generaion failed.
  38. **
  39. */
  40. BOOL GetNewColourMap(UBYTE *CMapData, UWORD PaletteDepth)
  41. {
  42.    UWORD i;
  43.    UWORD NumberOfColours = 1<<PaletteDepth;
  44.    UBYTE TColourComponent;
  45.    
  46.    FreeThisMem(&ColourMap,  ColourMapSize);
  47.    FreeThisMem(&SColourMap, ColourMapSize);
  48.    
  49.    ColourMapSize = (NumberOfColours*3*4)+4;
  50.    
  51.    if( AllocThisMem(&ColourMap, ColourMapSize, MEMF_CLEAR) )
  52.    {
  53.       if( AllocThisMem(&SColourMap, ColourMapSize, MEMF_CLEAR) )
  54.       {
  55.          register UBYTE *TColourMap = (UBYTE *)ColourMap;
  56.          register UBYTE *TCMapData = CMapData;
  57.          
  58.          // For a bit of efficiency: First word is the number of colours
  59.          // in your colourmap. Second word is the first colour to use.
  60.          // (See Autodocs3:Graphics/LoadRGB32 for more information)
  61.          // Anyway, Shifting the 'NumberOfColours' 16 times to the left,
  62.          // makes a longword with the lower 16 bit cleared. In other words,
  63.          // this long says the number of colour to use and that the first
  64.          // colour is colour 0. This could be done in two words, now it's
  65.          // done in one longword.
  66.          *(ULONG *)TColourMap = (ULONG) NumberOfColours<<16;
  67.          TColourMap += 4;
  68.          
  69.          for(i = 0; i < NumberOfColours; i++)
  70.          {            
  71.             // Make RED component            
  72.             TColourComponent = *TCMapData++;
  73.             *TColourMap++ = TColourComponent;
  74.             *TColourMap++ = TColourComponent;
  75.             *TColourMap++ = TColourComponent;
  76.             *TColourMap++ = TColourComponent;
  77.             
  78.             // Make GREEN component
  79.             TColourComponent = *TCMapData++;
  80.             *TColourMap++ = TColourComponent;
  81.             *TColourMap++ = TColourComponent;
  82.             *TColourMap++ = TColourComponent;
  83.             *TColourMap++ = TColourComponent;
  84.             
  85.             // Make BLUE component
  86.             TColourComponent = *TCMapData++;
  87.             *TColourMap++ = TColourComponent;
  88.             *TColourMap++ = TColourComponent;
  89.             *TColourMap++ = TColourComponent;
  90.             *TColourMap++ = TColourComponent;
  91.          }
  92.       }
  93.       else
  94.       {
  95.          // Not enough memory for 'SColourMap'
  96.          ErrorHandler( IFFerror_NoMemoryDoReturn, (APTR)ColourMapSize );
  97.          return(FALSE);
  98.       }      
  99.    }
  100.    else
  101.    {
  102.       // Not enough memory for 'ColourMap'
  103.       ErrorHandler( IFFerror_NoMemoryDoReturn, (APTR)ColourMapSize );
  104.       return(FALSE);
  105.    }
  106.    return(TRUE);
  107. }
  108.  
  109.  
  110. /*
  111. **  FadeColours(FadeType, Steps, ScreenToFade)
  112. **
  113. **     will fade the colours to the desired values.
  114. **
  115. **  pre:  FadeType - If FADE_UP,   colours will be faded to the desired values.
  116. **                   If FADE_DOWN, colours will be faded to the background colour.
  117. **        Steps - Number of steps to complete the fade process.
  118. **        ScreenToFade - Which screen to fade the colours
  119. **  post: None
  120. **
  121. */
  122. void FadeColours(enum Fade FadeType, UWORD Steps, struct Screen * ScreenToFade)
  123. {
  124.    UWORD NumberOfColours = 1<<(ScreenToFade->ViewPort.RasInfo->BitMap->Depth);
  125.    struct ViewPort *ScreenViewPort = &(ScreenToFade->ViewPort);
  126.    WORD i, j;
  127.    
  128.    switch(FadeType)
  129.    {
  130.       case FADE_UP:
  131.          for(i=0; i<Steps; i++)
  132.          {
  133.             ULONG *col  = (ULONG*) ColourMap+1;   // Skip first two words *ONE LONG!* (Number of Colours, First Colour).
  134.             ULONG *scol = (ULONG*)SColourMap+1;   // Skip first two words *ONE LONG!* (Number of Colours, First Colour).
  135.  
  136.             for(j=0; j<NumberOfColours; j++)
  137.             {
  138.                *col++ = *scol++;   // Do Red   Component
  139.                *col++ = *scol++;   // Do Green Component
  140.                *col++ = *scol++;   // Do Blue  Component
  141.             }
  142.             
  143.             LoadRGB32(ScreenViewPort, ColourMap);
  144.             Delay(0);
  145.          }
  146.          break;
  147.       case FADE_DOWN:
  148.          for(i=Steps; i>0; i--)
  149.          {
  150.             ULONG *col  = (ULONG*) ColourMap;
  151.             ULONG *scol = (ULONG*)SColourMap;
  152.             ULONG DestColour;
  153.             
  154.             *scol++ = *col++;
  155.  
  156.             for(j=0; j<NumberOfColours; j++)
  157.             {
  158.                *scol++ = ((*col++)/Steps)*i;   // Do Red   Component
  159.                *scol++ = ((*col++)/Steps)*i;   // Do Green Component
  160.                *scol++ = ((*col++)/Steps)*i;   // Do Blue  Component
  161.             }
  162.             
  163.             LoadRGB32(ScreenViewPort, SColourMap);
  164.             Delay(0);
  165.          }
  166.          break;
  167.    }
  168. }
  169.