/************************************************************
**
** Application: Snapper
**
** Title:       c.mask
**
*************************************************************/

/*
*
* Copyright (c) 2017, David Pilling and Chris Johnson
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*   * Redistributions of source code must retain the above copyright
*     notice, this list of conditions and the following disclaimer.
*   * Redistributions in binary form must reproduce the above
*     copyright notice, this list of conditions and the following
*     disclaimer in the documentation and/or other materials provided
*     with the distribution.
*   * Neither the name of the copyright holder nor the names of their
*     contributors may be used to endorse or promote products derived
*     from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* HOLDER BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/

/* Include files */


#include "h.includes"

#include <math.h>


/* from TaskLib */

#include "TaskLib:wimpt.h"
#include "TaskLib:flex.h"
#include "TaskLib:transform.h"
#include "TaskLib:fsx.h"
#include "TaskLib:etc.h"

/* from application */

#include "str.h"
#include "main.h"
#include "im.h"
#include "mask.h"





/************************************************************/

void maskcut(int * s,int sbpp,int * d,int dbpp,int n)
{
 int  smask;
 int  sshift;
 int  dmask;
 int  dshift;
 int  ss;
 int  dd;
 int  m;
 char map[256];
 int  i;
 int  delta;

 smask=(1<<sbpp)-1;
 dmask=(1<<dbpp)-1;

 delta=smask/2-1;
 if(delta<0) delta=0;

 for(i=0;i<=smask;i++)
 {
  map[i]=(i*dmask+delta)/smask;
 }


 ss=*s++;
 dd=0;
 sshift=dshift=0;

 while(1)
 {
  m=(ss>>sshift) & smask;
  sshift+=sbpp;
  m=map[m];
  dd|=(m<<dshift);
  dshift+=dbpp;

  if(--n==0) break;

  if(sshift>=32)
  {
   ss=*s++;
   sshift=0;
  }

  if(dshift>=32)
  {
   *d++=dd;
   dshift=dd=0;
  }
 }


 if(dshift>0)
 {
  *d++=dd;
 }
}










#if 0

os_error * maskcolour(imagestr * im,imagestr * mim,int * ncol,char * index)
{
 os_error * err;
 int   x;
 int   y;
 int   mbpp;
 int   bpp;
 int * idata;
 int * mdata;
 int   xpix;
 int   ypix;
 int   word;
 int   mword;
 int   shift;
 int   mshift;
 int   mask;
 int   mmask;
 char  used[256];
 int   sbyte;
 int   mbyte;


 err=NULL;

 memset(used,0,sizeof(used));

 xpix=im->xpix;
 ypix=im->ypix;
 bpp=im->bpp;
 mbpp=(mim==NULL)?0:mim->bpp;


 word=mword=0;
 shift=mshift=32;
 mmask=0;

 if(bpp==1) mask=0x1;
 else
 if(bpp==2) mask=0x3;
 else
 if(bpp==4) mask=0xF;
 else       mask=0xFF;

 if(mbpp)
 {
  if(mbpp==1) mmask=0x1;
  else
  if(mbpp==2) mmask=0x3;
  else
  if(mbpp==4) mmask=0xF;
  else        mmask=0xFF;
 }



 for(y=0;y<ypix;y++)
 {
  if(mbpp) err=imfind2rr(im,y,&idata,mim,y,&mdata);
  else     err=imfind1r(im,y,&idata);

  for(x=0;x<xpix;x++)
  {
   if(shift==32)
   {
    word=*idata++;
    shift=0;
   }
   sbyte=(word>>shift)&mask;
   shift+=bpp;

   if(mbpp)
   {
    if(mshift==32)
    {
     mword=*mdata++;
     mshift=0;
    }
    mbyte=(mword>>mshift)&mmask;
    mshift+=mbpp;

    if(mbyte!=0) used[sbyte]++;
   }
   else
   {
    used[sbyte]++;
   }
  }
 }

 *ncol=0;

 for(x=0;x<(1<<bpp);x++)
 {
  if(used[x]==0)
  {
   *ncol=(*ncol)+1;
   *index++=x;
  }
 }

 return(err);
}


#endif






/*************  End of c.mask  ***********************/

