//	This source and Enthropy quotioent method apply to GNU General Public License. 
//			Copyright (C) 2012  Jasenko Dzinleski 

//	This source applies to the GNU General Public License as
//	published by the Free Software Foundation 
//	and can not be used, copied, sold, redistributed or 
//	used in any other way but only by written permission by Jasenko Dzinleski . 
//	Copyright (C) from 2001 - 2012 and later by Jasenko Dzinleski 

//	This program is distributed in the hope that it will be useful, but
//	WITHOUT ANY WARRANTY; without even the implied warranty of
//	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
//	General Public License for more details. 

//	You should have received a copy of the GNU General Public License along
//	with this program; if not, write to the Free Software Foundation, Inc.,
//	51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

//
//			Enthropy quotioent
//			written by Dzinleski Jasenko Jun , August , 2012
//

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

FILE	*f1,*f2;

//char	infn[256]="a_j.JPG";
//char	infn[256]="gi_list_2_2_chrX.jpg";
//char	infn[256]="r311.tar.gz";
//char	infn[256]="index.html";
//char	infn[256]="DSC00002.JPG";
//char	infn[256]="book_sd.txt";
char	infn[256]="War_and_Peace_NT.txt";
//char	infn[256]="ok-pg-comma.pdf";
//char	infn[256]="Disc Brakes Rear Wheel Front Disc Brakes System .pdf";

char	outfn[256]="/mnt/sdcard/DCIM/out.txt";
int 	ba16[16][16][16][16][9];int bai;
int	b8o,b8e;
bool	df=false;

bool	bt32_1[32];	int	bt32i_1=0;
bool	bt32_2[32];	int	bt32i_2=0;

int	pr24_(long e,bool b_[32],int b_i,bool af)
{

	int	a,b=0,d=-1,c;
	b_i=0;
	e&=0x00ffffff;
	for(a=24-1;a>=0;--a)
	{
		c=0;c=1<<a;
		if(((c&e)>>a)==1){if(d==-1){d=a;}if(af){b_[b_i]=true;++b_i;}}else{
			if(d!=-1){if(af){b_[b_i]=false;++b_i;}}
		}
	}
	//printf("!%d %d!",e,d);
	return(d+1);

}

int p_1()
{

int	a,b,c,d,e;
int	i,j,k,l,m;
int	d1,d2,d3;
int	d1_,d2_,d3_;
int	b1,b2,b3,b1_,b2_,b3_;
int	fbyte,bc=0,bitc=0;
int	mc=0,unmc=0;


	for(i=0;i<15;++i){
	for(j=0;j<15;++j){
	for(k=0;k<15;++k){
	for(l=0;l<15;++l){
		ba16[i][j][k][l][0]=-1;
	}}}}bai=0;

	f1=fopen(infn,"rb");
	//f2=fopen(outfn,"w");

	b1_=getc(f1);
	b2_=getc(f1);
	b3_=getc(f1);
	bc+=3;if(df){printf("%d\n",bc);}

	while(!feof(f1))
	{

		b=((b1_<<8)&0xff00)|(b3_&(0xff));

		b1=b1_<<1;
		b2=b2_<<1;
		b3=b3_<<1;

		d1=((((b1&b8e)>>1)^(b2&b8o))|((b1&b8e)^((b2&b8o)<<1)));	
		d2=((((b1&b8e)>>1)^(b3&b8o))|((b1&b8e)^((b3&b8o)<<1)));
		d3=((((d1&b8e)>>1)^(d2&b8o))|((d1&b8e)^((d2&b8o)<<1)));

		d1_=((((b2&b8e)>>1)^(b1&b8o))|((b2&b8e)^((b1&b8o)<<1)));	
		d2_=((((b2&b8e)>>1)^(b3&b8o))|((b2&b8e)^((b3&b8o)<<1)));
		d3_=((((d2_&b8e)>>1)^(d1_&b8o))|((d2_&b8e)^((d1_&b8o)<<1)));

		if(ba16[(b&0xf000)>>12][(b&0x0f00)>>8][(b&0x00f0)>>4][b&0x000f][0]==-1)
		{
			ba16[(b&0xf000)>>12][(b&0x0f00)>>8][(b&0x00f0)>>4][b&0x000f][0]=d1;
			ba16[(b&0xf000)>>12][(b&0x0f00)>>8][(b&0x00f0)>>4][b&0x000f][1]=d2;
			ba16[(b&0xf000)>>12][(b&0x0f00)>>8][(b&0x00f0)>>4][b&0x000f][2]=d3;
			ba16[(b&0xf000)>>12][(b&0x0f00)>>8][(b&0x00f0)>>4][b&0x000f][3]=d1_;
			ba16[(b&0xf000)>>12][(b&0x0f00)>>8][(b&0x00f0)>>4][b&0x000f][4]=d2_;
			ba16[(b&0xf000)>>12][(b&0x0f00)>>8][(b&0x00f0)>>4][b&0x000f][5]=d3_;
			ba16[(b&0xf000)>>12][(b&0x0f00)>>8][(b&0x00f0)>>4][b&0x000f][6]=bai;
			ba16[(b&0xf000)>>12][(b&0x0f00)>>8][(b&0x00f0)>>4][b&0x000f][7]=(b2>>1);
			ba16[(b&0xf000)>>12][(b&0x0f00)>>8][(b&0x00f0)>>4][b&0x000f][8]=1;

			bitc+=pr24_((long)bai,bt32_1,bt32i_1,false);++bai;

		}else{
			e=ba16[(b&0xf000)>>12][(b&0x0f00)>>8][(b&0x00f0)>>4][b&0x000f][6];
			bitc+=pr24_((long)e,bt32_1,bt32i_1,false);
			
			if(ba16[(b&0xf000)>>12][(b&0x0f00)>>8][(b&0x00f0)>>4][b&0x000f][2]==d3)			
			{

				++ba16[(b&0xf000)>>12][(b&0x0f00)>>8][(b&0x00f0)>>4][b&0x000f][8];

				if(df)
				{
				printf("3+\n");

				printf(" %d %d %d\n",d1,d2,d3);
				printf(" %d %d %d\n",
				ba16[(b&0xf000)>>12][(b&0x0f00)>>8][(b&0x00f0)>>4][b&0x000f][0],
				ba16[(b&0xf000)>>12][(b&0x0f00)>>8][(b&0x00f0)>>4][b&0x000f][1],
				ba16[(b&0xf000)>>12][(b&0x0f00)>>8][(b&0x00f0)>>4][b&0x000f][2]
				);
				}
			}else{
				if(df)
				{
				printf("!3+\n");
				printf("!1. %d %d %d\n",d1,d2,d3);
				printf(" 1. %d %d %d\n",
				ba16[(b&0xf000)>>12][(b&0x0f00)>>8][(b&0x00f0)>>4][b&0x000f][0],
				ba16[(b&0xf000)>>12][(b&0x0f00)>>8][(b&0x00f0)>>4][b&0x000f][1],
				ba16[(b&0xf000)>>12][(b&0x0f00)>>8][(b&0x00f0)>>4][b&0x000f][2]
				);
				}
			}

			if(ba16[(b&0xf000)>>12][(b&0x0f00)>>8][(b&0x00f0)>>4][b&0x000f][5]==d3_)			
			{
				if(df)
				{
				printf("3_+\n");

				printf(" %d %d %d\n",d1_,d2_,d3_);
				printf(" %d %d %d\n",
				ba16[(b&0xf000)>>12][(b&0x0f00)>>8][(b&0x00f0)>>4][b&0x000f][3],
				ba16[(b&0xf000)>>12][(b&0x0f00)>>8][(b&0x00f0)>>4][b&0x000f][4],
				ba16[(b&0xf000)>>12][(b&0x0f00)>>8][(b&0x00f0)>>4][b&0x000f][5]
				);
				}
			}else{
				if(df)
				{
				printf("!3_+\n");
				printf("!2. %d %d %d\n",d1_,d2_,d3_);
				printf(" 2. %d %d %d\n",
				ba16[(b&0xf000)>>12][(b&0x0f00)>>8][(b&0x00f0)>>4][b&0x000f][3],
				ba16[(b&0xf000)>>12][(b&0x0f00)>>8][(b&0x00f0)>>4][b&0x000f][4],
				ba16[(b&0xf000)>>12][(b&0x0f00)>>8][(b&0x00f0)>>4][b&0x000f][5]
				);
				}
			}
			if((ba16[(b&0xf000)>>12][(b&0x0f00)>>8][(b&0x00f0)>>4][b&0x000f][2]!=d3)||	
			   (ba16[(b&0xf000)>>12][(b&0x0f00)>>8][(b&0x00f0)>>4][b&0x000f][5]!=d3_)){++unmc;}
			if((ba16[(b&0xf000)>>12][(b&0x0f00)>>8][(b&0x00f0)>>4][b&0x000f][2]==d3)&&	
			   (ba16[(b&0xf000)>>12][(b&0x0f00)>>8][(b&0x00f0)>>4][b&0x000f][5]==d3_)){++mc;}

		}

		b1_=b2_;
		b2_=b3_;
		b3_=getc(f1);
		++bc;if(df){printf("%d\n",bc);}

	}
	fclose(f1);
	//fclose(f2);
	if(df){printf("!%d %d %d!\n",bai,bitc,(bitc/8));}

	//printf("!%d %d %e!\n",unmc,mc,(100*((double)unmc/(double)mc)));
	printf("\t%e\n\n",(100*((double)unmc/(double)mc)));

        return(0);
}

int main(int argc,char *argv[])
{

	int	a,b,c,d;
	int	i,j,k,l,m;

	printf("\n\tEnthropy quotioent\n");
	printf("\twritten by Dzinleski Jasenko Jun , August , 2012\n\n");



	if(argv[1]==NULL){return(0);}else{strcpy(infn,argv[1]);f1=fopen(infn,"rb");if(f1==NULL){return(0);}else{fclose(f1);}}

	b8o=0;
	b8o|=1<<0;
	b8o|=1<<2;
	b8o|=1<<4;
	b8o|=1<<6;

	b8e=0;
	b8e|=1<<1;
	b8e|=1<<3;
	b8e|=1<<5;
	b8e|=1<<7;

	p_1();

	return(0);

}