/*
	WARNING: This file was generated by dkct.
	Changes you make here will be lost if dkct is run again!
	You should modify the original source and run dkct on it.
	Original source: dk3pixre.ctr
*/

/*
Copyright (C) 2011-2013, Dirk Krause

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 opyright 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 author nor the names of 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 OWNER OR CONTRIBUTORS 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.
*/

/**	@file dk3pixre.c The dk3pixre module.
*/


#line 59 "dk3pixre.ctr"

#include "dk3all.h"
#include "dk3pixre.h"





#line 66 "dk3pixre.ctr"



void
dk3pixre_set(dk3_pixel_resample_t *pp, size_t inw, size_t outw)
{
  size_t	i;
  if((pp) && (inw) && (outw)) {
    pp->insize = inw;
    pp->outsize = outw;
    pp->andmask = (dk3_bif_pixel_t)0;
    pp->ormask = (dk3_bif_pixel_t)0;
    pp->shiftw = 0;
    pp->direction = 0;
    if(outw > inw) {
      pp->direction = 1;
      pp->shiftw = outw - inw;
      for(i = 0; i < pp->shiftw; i++) {
        pp->ormask |= dk3bits_get(i);
      }
      for(i = inw; i < outw; i++) {
        pp->andmask |= dk3bits_get(i);
      }
    } else {
      if(outw < inw) {
        pp->direction = -1;
	pp->shiftw = inw - outw;
	for(i = 0; i < outw; i++) {
	  pp->andmask |= dk3bits_get(i);
	}
      }
    }
    pp->maxval = (dk3_bif_pixel_t)0U;
    for(i = 0; i < outw; i++) {
      pp->maxval |= dk3bits_get(i);
    }
  }
}



dk3_bif_pixel_t
dk3pixre_convert(dk3_pixel_resample_t *pp, dk3_bif_pixel_t inval)
{
  dk3_bif_pixel_t	back;
  

#line 112 "dk3pixre.ctr"
  back = inval;
  if(pp) {
    switch(pp->direction) {
      case 1: {
        back = (back << (pp->shiftw));
	if(inval & 1) {
	  back |= (pp->ormask);
	} else {
	  back &= (pp->andmask);
	}
      } break;
      case -1: {	

#line 124 "dk3pixre.ctr"
        back = (back >> (pp->shiftw));	

#line 125 "dk3pixre.ctr"
	back &= (pp->andmask);
      } break;
    }
  } 

#line 129 "dk3pixre.ctr"
  return back;
}



dk3_bif_pixel_t
dk3pixre_get_value_from_packed_bytes(
  unsigned char const	*row,
  dk3_bif_coord_t	 vi,
  size_t		 bpc
)
{
  unsigned short const	*us;
  unsigned char		 uc;
  dk3_bif_coord_t	 byteindex;
  dk3_bif_coord_t	 inbyte;

  dk3_bif_pixel_t	back = (dk3_bif_pixel_t)0;
  switch(bpc) {
    case 16: {
      us = (unsigned short const *)row;
      back = us[vi];
    } break;
    case 8: {
      back =  (dk3_bif_pixel_t)(row[vi]);
      back &= 0x00FFU;
    } break;
    case 4: {
      byteindex = vi / 2L;
      inbyte    = vi % 2L;
      uc = row[byteindex];
      switch((int)inbyte) {
        case 0: {
	  uc = uc >> 4;
	} break;
      }
      back = (dk3_bif_pixel_t)uc;
      back &= 0x000FU;
    } break;
    case 2: {
      byteindex = vi / 4L;
      inbyte    = vi % 4L;
      uc = row[byteindex];
      switch((int)inbyte) {
        case 0: {
	  uc = uc >> 6;
	} break;
	case 1: {
	  uc = uc >> 4;
	} break;
	case 2: {
	  uc = uc >> 2;
	} break;
      }
      back = (dk3_bif_pixel_t)uc;
      back &= 0x0003U;
    } break;
    case 1: {
      byteindex = vi / 8L;
      inbyte    = vi % 8L;
      uc = row[byteindex];
      switch((int)inbyte) {
        case 0: { uc = uc >> 7; } break;
	case 1: { uc = uc >> 6; } break;
	case 2: { uc = uc >> 5; } break;
	case 3: { uc = uc >> 4; } break;
	case 4: { uc = uc >> 3; } break;
	case 5: { uc = uc >> 2; } break;
	case 6: { uc = uc >> 1; } break;
      }
      back = (dk3_bif_pixel_t)uc;
      back &= 0x0001U;
    } break;
  }
  return back;
}



dk3_bif_pixel_t
dk3pixre_get_max_value(dk3_pixel_resample_t *pp)
{
  dk3_bif_pixel_t back = (dk3_bif_pixel_t)0U;
  if(pp) {
    back = pp->maxval;
  }
  return back;
}

