/////////////////////////////////////////////////////////////////////////
//
// formatinfo.cpp --a part of libdecodeqr
//
// Copyright(C) 2007 NISHI Takao <zophos@koka-in.org>
//                   JMA  (Japan Medical Association)
//                   NaCl (Network Applied Communication Laboratory Ltd.)
//
// This is free software with ABSOLUTELY NO WARRANTY.
// You can redistribute and/or modify it under the terms of LGPL.
//
// $Id: formatinfo.cpp 36 2007-02-21 23:22:03Z zophos $
//
#include "formatinfo.h"
#include "stdlib.h"

namespace Qr{
int FormatInfo::FORMAT_INFO_MASK_QR = 0x5412;
int FormatInfo::FORMAT_INFO_DECODE_LOOKUP[][2] = { { 0x5412, 0x00 }, { 0x5125, 0x01 }, { 0x5E7C, 0x02 }, {
    0x5B4B, 0x03 }, { 0x45F9, 0x04 }, { 0x40CE, 0x05 }, { 0x4F97, 0x06 }, { 0x4AA0, 0x07 }, { 0x77C4, 0x08 }, {
    0x72F3, 0x09 }, { 0x7DAA, 0x0A }, { 0x789D, 0x0B }, { 0x662F, 0x0C }, { 0x6318, 0x0D }, { 0x6C41, 0x0E }, {
    0x6976, 0x0F }, { 0x1689, 0x10 }, { 0x13BE, 0x11 }, { 0x1CE7, 0x12 }, { 0x19D0, 0x13 }, { 0x0762, 0x14 }, {
    0x0255, 0x15 }, { 0x0D0C, 0x16 }, { 0x083B, 0x17 }, { 0x355F, 0x18 }, { 0x3068, 0x19 }, { 0x3F31, 0x1A }, {
    0x3A06, 0x1B }, { 0x24B4, 0x1C }, { 0x2183, 0x1D }, { 0x2EDA, 0x1E }, { 0x2BED, 0x1F },
};
int FormatInfo::N_FORMAT_INFO_DECODE_LOOKUPS = 32;

int FormatInfo::BITS_SET_IN_HALF_BYTE[] = { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4 };
    unsigned char MaskPatterner000::pixel(int i,int j)
    {
        return((i+j)%2?0:255);
    }
    unsigned char MaskPatterner001::pixel(int i,int j)
    {
        return(i%2?0:255);
    }
    unsigned char MaskPatterner010::pixel(int i,int j)
    {
        return(j%3?0:255);
    }
    unsigned char MaskPatterner011::pixel(int i,int j)
    {
        return((i+j)%3?0:255);
    }
    unsigned char MaskPatterner100::pixel(int i,int j)
    {
       return(((i>>2)+j/3)%2?0:255);//andy opti  return((i/2+j/3)%2?0:255);
    }
    unsigned char MaskPatterner101::pixel(int i,int j)
    {
    	int temp = i*j;
        return(((temp%2)+(temp%3))?0:255);
    }
    unsigned char MaskPatterner110::pixel(int i,int j)
    {
  	  int temp = i*j;
         return((((temp%2)+(temp%3))%2)?0:255);//  return((((i*j)%2)+((i*j)%3))%2?0:255);
    }
    unsigned char MaskPatterner111::pixel(int i,int j)
    {
        return(((((i+j)%3)+((i*j)%2))%2)?0:255);//fix the bug; return((((i*j)%3)+((i*j)%2))%2?0:255);
    }

    FormatInfo::FormatInfo()
    {
        this->level=0;
        this->mask_pattern=0;
        this->status=0;

        this->_patterner=NULL;
    }
    FormatInfo::~FormatInfo()
    {
        if(this->_patterner)
            delete this->_patterner;
    }

    int FormatInfo::set_level(int l)
    {
        this->level=l;
        if(l<0||l>3){
            this->status|=QR_FORMATINFO_INVALID_LEVEL;
            this->level=0;
        }
        return(this->level);
    }
    int FormatInfo::set_mask_pattern(int m)
    {
        if(this->_patterner)
            delete this->_patterner;

        this->mask_pattern=m;
        switch(this->mask_pattern){
        case 0:
            this->_patterner=new MaskPatterner000();
            break;
        case 1:
            this->_patterner=new MaskPatterner001();
            break;
        case 2:
            this->_patterner=new MaskPatterner010();
            break;
        case 3:
            this->_patterner=new MaskPatterner011();
            break;
        case 4:
            this->_patterner=new MaskPatterner100();
            break;
        case 5:
            this->_patterner=new MaskPatterner101();
            break;
        case 6:
            this->_patterner=new MaskPatterner110();
            break;
        case 7:
            this->_patterner=new MaskPatterner111();
            break;
        }

        return(this->mask_pattern);
    }
	int FormatInfo::numBitsDiffering(unsigned int a, unsigned int b) {
  	a ^= b;
  	return BITS_SET_IN_HALF_BYTE[a & 0x0F] + BITS_SET_IN_HALF_BYTE[(a >> 4 & 0x0F)] + BITS_SET_IN_HALF_BYTE[(a >> 8
         & 0x0F)] + BITS_SET_IN_HALF_BYTE[(a >> 12 & 0x0F)] + BITS_SET_IN_HALF_BYTE[(a >> 16 & 0x0F)]
         + BITS_SET_IN_HALF_BYTE[(a >> 20 & 0x0F)] + BITS_SET_IN_HALF_BYTE[(a >> 24 & 0x0F)]
         + BITS_SET_IN_HALF_BYTE[(a >> 28 & 0x0F)];
	}
    int FormatInfo::decode_formatinfo(unsigned short data, unsigned short data2)
    {
        unsigned short t;
       // data^=QR_FORMAT_INFO_XOR_MASK;
  	data2 = data2& 0x7FFF;
  	//data2^= QR_FORMAT_INFO_XOR_MASK;
        this->status=0;
        //int ret=this->_error_correct(&data);

        /*this->set_level(data>>13);
        this->set_mask_pattern((data>>10)&0x7);

        if(ret<0)
            this->status|=QR_FORMATINFO_UNRECOVERABLE;

        return(ret);*/
        int bestDifference = 65535;
  	int bestFormatInfo = 0,ret =0;
  	for (int i = 0; i < N_FORMAT_INFO_DECODE_LOOKUPS; i++) {
    		int* decodeInfo = FORMAT_INFO_DECODE_LOOKUP[i];
    		int targetInfo = decodeInfo[0];
    		if (targetInfo == data || targetInfo == data2) {
      // Found an exact match
     		  //ret = this ->_error_correct(&data);
                  t = decodeInfo[1];
      		  this->set_level((t>>3)&0x3);
        	  this->set_mask_pattern(t&0x7);
        	  return 0;
    		}
    	int bitsDifference = numBitsDiffering(data, targetInfo);
    		if (bitsDifference < bestDifference) {
     		 bestFormatInfo = decodeInfo[1];
      		bestDifference = bitsDifference;
    		}
   		 if (data != data2) {
        // also try the other option
        bitsDifference = numBitsDiffering(data2, targetInfo);
        if (bitsDifference < bestDifference) {
            bestFormatInfo = decodeInfo[1];
          bestDifference = bitsDifference;
        }
      }
  }
  if (bestDifference <= 3) {
      t = bestFormatInfo;
      //ret= this ->_error_correct(&data);
      ret = bestDifference;
      this->set_level((t>>3)&0x3);
      this->set_mask_pattern(t&0x7);
      //if(ret >=0)
    	return ret;
	  }
   //if(ret<0)
            this->status|=QR_FORMATINFO_UNRECOVERABLE;
        return(-1);
    }

    int FormatInfo::_error_correct(unsigned short *src)
    {
        Galois::Field *gf=new Galois::Field(4);
        Galois::BCH *bch=new Galois::BCH(gf,15,3);
        unsigned short mask=0x01;
        int i;
        for(i=0;i<15;i++,mask<<=1){
            if(*src&mask)
                bch->set(i,gf->exp2nomial(0));
            else
                bch->set(i,gf->zero());
        }
        int errors=bch->decode();
        if(errors>0){
            mask=0x01;
            for(i=0;i<errors;i++)
                (*src)^=(mask<<bch->error_pos[i]);
        }
        
        delete bch;
        delete gf;
        return(errors);
    }

    unsigned char FormatInfo::mask_pixel(int i,int j){
        return(this->_patterner->pixel(i,j));
    }
    
    FormatInfo *FormatInfo::init_each_pattern_pixel()
    {
        this->_pattern_c=0;
        return(this);
    }
    FormatInfo *FormatInfo::each_pattern_pixel(int *x,int *y)
    {
        if(this->_pattern_c<16){
            *x=format_info_addr[0][this->_pattern_c][0];
            *y=format_info_addr[0][this->_pattern_c][1];
        }
        else if(this->_pattern_c<32){
            *x=format_info_addr[1][this->_pattern_c-16][0];
            *y=format_info_addr[1][this->_pattern_c-16][1];
        }
        else
            return(NULL);

        this->_pattern_c++;
        return(this);
    }
    FormatInfo *FormatInfo::each_pattern_pixel(int pos,int *x,int *y)
    {
        switch(pos){
        case 0:
            if(this->_pattern_c>14)
                return(NULL);
            break;
        case 1:
            if(this->_pattern_c>15)
                return(NULL);
            break;
        default:
            return(NULL);
        }
        
        *x=format_info_addr[pos][this->_pattern_c][0];
        *y=format_info_addr[pos][this->_pattern_c][1];
        
        this->_pattern_c++;
        return(this);
    }
}
