Fading to color

Source code

//========================================================
//
// "Fade To Color" effect - QUAGLIOZZI ERIC - 2006
// Modified January 2007
//========================================================

/*
   Usable for low res (72 dpi) and double res (144 dpi)
   bitmap using ONLY 16 bits  Colors.
 ---------------------------------------------------------
 | Do not use with other resolutions, may system crash ! |
 ---------------------------------------------------------
*/



//---------------- Macros and constants ------------------

#if defined(_MSC_VER)     // If compiling with VC++...

    #define private        static __inline
    #define longint        __int64
    
#elif defined(__GNUC__)  // If compiling with GCC...

    #define private        static inline
    #define longint        long long int
    
#else

    #error "Unknown Compiler!"
    
#endif


//------------------- Types ------------------------------


typedef unsigned long Call68KFuncType(const void * emulStateP,
                                      unsigned long trapOrFunction,
                                      const void * argsOnStackP,
                                      unsigned long argsSizeAndwantA0);

typedef unsigned long NativeFuncType(const void * emulStateP,
                                     void * userData68KP,
                                     Call68KFuncType * call68KFuncP);

typedef struct {
    char hb_object_header[14];
    short DebugShort;            // for debug mode and padding
    short w;                     // real width in pixel
    short h;                     // real height in pixel
    long fade;                   // fading coeff.
    unsigned char   * pSrc;      // bitmap source pointer
    unsigned char r,g,b;         // color to fade    
} FadeStruct;

typedef struct _ParamsType
  {
   FadeStruct       * pFade;     // Access to FadeToColor HB++ object 
  }  ParamsType;


//---------------- "Fade To Color" Function --------------


unsigned long FadeToColor(const void * emulStateP, void * userData68KP,
                                         Call68KFuncType * call68KFuncP)
{
  ParamsType    * p;             //pointer to params structure
  FadeStruct    * Fade;          //pointer to Fade structure
  unsigned char * src;
  short     w, h, wlimit;
  short     i, j;
  long      R,G,B,fd;
  long      Rl,Gl,Bl;
  short     RGB;

  // Do local copy of params (not obliged)
  p=(ParamsType *) userData68KP;
  Fade = p->pFade;
  src = Fade->pSrc; 
  w = Fade->w; 
  h = Fade->h; 
  fd = Fade->fade; 
  Rl = Fade->r;
  Gl = Fade->g;
  Bl = Fade->b;
  
  // some init.
  wlimit=w;
  if (wlimit & 0x0001) wlimit += 1;
  if (fd>255) fd=255;
  if (fd<8) fd=8;

  // do the job!
  for (j=0;j<h;j++)  
    {
       for (i=0; i<w; i+=2)
        {
           RGB = (src[i]<<8)|src[i+1];
           R=(RGB >> 8) & 0x00F8;
           G=(RGB >> 3) & 0x00FC;
           B= (RGB <<3)& 0x00F8;
           // fade to color !
           if (R<Rl)
            { R=R+fd;
              if (R>Rl) R=Rl; }
           else if (R>Rl)
            { R=R-fd;
              if (R<Rl) R=Rl; }
           if (G<Gl)
            { G=G+fd;
              if (G>Gl) G=Gl; }
           else if (G>Gl)
            { G=G-fd;
              if (G<Gl) G=Gl; }
           if (B<Bl)
            { B=B+fd;
              if (B>Bl) B=Bl; }
           else if (B>Bl)
            { B=B-fd;
              if (B<Bl) B=Bl; }
           R=R>>3; G=G>>2; B=B>>3;
           if (R > 0x001F) R=0x001F;
           if (G > 0x003F) G=0x003F;
           if (B > 0x001F) B=0x001F;
           //new 16 bits "565" format pixel color
           src[i]  = (R<<3)|(G>>3);
           src[i+1]= (G<<5)|(B);
        }
        src+=wlimit;
    }
  return 0;
}

//========================================================