Scrambling effect

Source code

/*=======================================================
   Scramble Effect - QUAGLIOZZI ERIC - 2006

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

#include "MathLibFixed.c"


//--------------------------- 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 swTransparent;
    unsigned char  * pData;
    short w;
    short wl;
    short h;
    short bitdepth;
    unsigned char swTranspColorH;
    unsigned char swTranspColorL;
    unsigned char swTranspIndex;
} ImageData;


typedef struct {
    char hb_object_header[14];
    short  DebugShort;
    long   DebugLong;
    ImageData  * swImagePointer;
    long   SeedInit;
    short  Padding;
    unsigned char BkIndex;  // Index Palette Couleur de fond
    unsigned char BkRed;    // Composante Rouge Couleur de fond
    unsigned char BkGreen;  // Composante Verte Couleur de fond
    unsigned char BkBlue;   // Composante Bleue Couleur de fond
} SrambleEffect;  


typedef struct _ParamsType
  {
    SrambleEffect          * scb;
  } ParamsType;


//--------------------------- Alpha blending Function ---------------

unsigned long SCRAMBLEeffect(const void * emulStateP, 
                             void * userData68KP, 
                             Call68KFuncType * call68KFuncP)

{
  ParamsType     * p;         //pointer: params structure
  SrambleEffect  * pScb;      //pointer: HB++ objet clsScrambleEffect 
  ImageData      * lpImage;   //pointer: Image structure
  unsigned char  * P0;        //pointer: pixels;
  short          w,wl,h;
  short          i,j;
  unsigned short  id;
  unsigned short  RGB1, RGB2, R, G, B;
  long            seed;
  short           random;
  short           Pad;
  
  // Do local copy of params

  p=(ParamsType *) userData68KP;
  pScb = p->scb;
  lpImage = pScb->swImagePointer;
  w = lpImage->w;
  wl = lpImage->wl;
  h = lpImage->h;
  seed=pScb->SeedInit;
  P0=lpImage->pData;
  R=pScb->BkRed>>3;   // (5 bits)
  G=pScb->BkGreen>>2; // (6 bits)
  B=pScb->BkBlue>>3;  // (5 bits)
  RGB1 = (R<<3)|(G>>3);
  RGB2 = (G<<5)|(B); 
  Pad=pScb->Padding;
  
  if (lpImage->bitdepth==1)
   {
      for (j=0;j<h;j++)  
      {
        seed = (0x015A4E35L * seed)+1; 
        random = ((seed >> 15) & 0x3FFF)+1;
        random = ((long)random*(long)Pad)>>14;
        random = (Pad>>1)-random;
        if (random<0)
        {   
            id = -random;
            for (i=0; i<(w-id); i++){ P0[i]=P0[i+id]; }
            for (i=w-id; i<w; i++) { P0[i]=pScb->BkIndex; }
        }
        else
        {   
            id = random;
            for (i=w-1; i<=random; i--) { P0[i]=P0[i-id]; }
            for (i=0; i<random; i++) { P0[i]=pScb->BkIndex; }
        }
        P0+=wl;
      }
   }  // end of 8 bits case

  else

   {
      for (j=0;j<h;j++)  
      {
        seed = (0x015A4E35L * seed)+1; 
        random = ((seed >> 15) & 0x3FFF)+1;
        random = ((long)random*(long)Pad)>>14;
        random = (Pad>>1)-random;
        if (random<0)
        {   
            id = -random;
            for (i=0; i<(w-id); i++) { P0[i<<1]=P0[(i+id)<<1];
                                       P0[(i<<1)+1]=P0[((i+id)<<1)+1]; }
            for (i=w-id; i<w; i++) { P0[i<<1]=RGB1; P0[(i<<1)+1]=RGB2; }
        }
        else
        {   
            id = random;
            for (i=w-1; i<=random; i--) { P0[i<<1]=P0[((i-id)<<1)];
                                          P0[(i<<1)+1]=P0[((i-id)<<1)+1];}
            for (i=0; i<random; i++) { P0[i<<1]=RGB1; P0[(i<<1)+1]=RGB2; }
        }
        P0+=wl;
      }
   }  // end of 16 bits case  
  pScb->SeedInit = seed;
  return 0;
}

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