392
Min=1.e20 ;
for (i=0; i<M; i++)
histo[i]=0.0;
for(i=0; i<N1; i++)
{
fread(buffi,sizeof(float),NS,fptri);
for(j=0;j<N1;j++)
{
kk=j*NB ;
for(k1=0;k1<NB;k1++)
{
loc1=kk+k1*N;
kk2=k1*NB;
for(k2=0;k2<NB;k2++)
{
loc=loc1+k2;
k=kk2+k2 ;
buffi[loc]-(buffi[loc]-mu[k])/sigma[k];
if(buffi[loc]<Min) Min=buffi[loc];
if(buffi[loc]>Max) Max=buffi[loc];
}/* k2 loop. */
}/* k1 loop.*/
}/* j-loop.*/
}/* i-loop.*/
rewind(fptri); /*Rewind input file.*/
xt=wherex();
yt=wherey();
gotoxy(70,25);
textattr(WHITE+(BLACK<<4));
cputs ( " ");
gotoxy(xt,yt+1);
printf("\n\nEnter fraction of DCT coefficients to
be");
printf("\n retained per block (e.g. 0.25, 0.5, etc.)
--->");
scanf ( "%f", &fdct);
xt=wherex();
yt=wherey();
gotoxy(70,25);
textattr(WHITE+(GREEN<<4)+BLINK);
cputs("WAIT");
gotoxy(xt,yt);
k=fdct*Nt;
393
T=(int *)malloc(Nt*sizeof(int));
sum=0.0;
for(i=0;i<Nt;i++)
T[i]=0;
T[0]=1; /* dc component.*/
while(k>1)
{
smax=-1.0;
for(i=0;i<Nt;i++)
{
if(T[i]==1) continue;
if(sigma[i]>smax)
{
smax=sigma[i];
loc=i;
}
}
T[loc]=1;
k--;
}
h=(Max-Min)/(float)m;
d[0]=Min;
for(i=0;i<(m-1);i++)
d[i+1]=d[i]+h;
d[M]=Max;
for(i=0;i<N1;i++)
{
fread(buffi,sizeof(float),NS,fptri);
for(j=0;j<N1;j++)
{
kk=j*NB ;
for(k1=0;k1<NB;k1++)
{
loc1=kk+k1*N;
kk2=k1*NB ;
for(k2=0;k2<NB;k2++)
{
loc=loc1+k2;
k=kk2+k2 ;
if(T[k]==0) continue;
buffi[loc]=(buffi[loc]-mu[k])/sigma[k];
for(kt=0;kt<M;kt++)
{
394
if((buffi[loc]<d[kt+1])&&(buffi[loc]>=d[kt]))
{
histo[kt]+=1.0;
break;
}
}
} /* k2 loop. */
} /* k1 loop.*/
}/* j-loop.*/
} /* i-loop.*/
rewind(fptri ) ; /*Rewind input file. */
for( i=0; i<M; i++)
x[i]=(d[i]+d[i+1])/2.0;
NQ=(long int)(Nt-1)*(long int)(N1*N1);
if (ch=='y' )
{
printf ( "\n\nPrepari ng data for output
plotting.");
/* Preparing data for X-Y plotting.
Data is prepared in a format suitable for plotting
by GRAFTOOL,
a graphical analysis software tool by 3-D Visions ,
(412 S.Pacific Coast Hwy. Suite 201, Redondo Beach,
CA. 90277).*/
fprintf(fptro,"%d %d\n",M,2);
for(i=0;i<M;i++)
{
fprintf(fptro,"%e %e\n",x[i],histo[i]);
}
fclose(fptro);
}
xt=wherex();
yt=wherey();
gotoxy(70, 25);
textattr(WHITE+(BLACK<<4));
cputs ( " " );
gotoxy (xt, yt+1);
free (d);
printf(" \nDesigning the quantizer.");
mut=0.0;
sigmat=0.0;
for(j=0; j<M; j++)
mut+=histo[j]*x[j];
395
sum=0.0;
for(j=0; j<M ; j++)
sum+=x[j]*x[j]*histo[j];
sigmat= NQ*sum-mut*mut;
sigmat/=(float)NQ*(float)(NQ-1);
sigmat=(float)sqrt((double) sigmat);
printf("\n Enter file name containing the");
printf("\n Lloyd-Max quantizer--> ");
scanf("%s",file_name);
fptro=fopen(file_name,"r");
if(fptro==NULL)
{
printf("\n That file does not exist. Run the ");
printf("\n program for designing the Lloyd-Max
quantizer.\n");
exit(1);
}
i=0;
while(1)
{
k=fscanf(fptro," %f %f",&dt,&rt);
i++;
if(k==EOF) break;
}
NQ=i-1;
d=(float *)malloc(NQ*sizeof(float));
r=(float *)malloc(NQ*sizeof(float));
rewind(fptro);
i=0;
while(1)
{
k=fscanf(fptro," %f %f",&d[i],&r[i]);
if(k==EOF) break;
d[i]=d[i]*sigmat;
r[i]=r[i]*sigmat;
i++;
}
fclose(fptro); /*Close file containing
distribution.*/
/* Header information:
Block size, image size, number of quantization
levels,
396
zonal sampling matrix, standard deviations &
means,
reconstruction levels. */
printf("\nEnter file name to store quantized
image,\n ");
textattr(BLUE+(YELLOW<<4)+BLINK);
cputs(" Do NOT give the name an extension --> ");
scanf("%s",file_name);
textattr(WHITE+(BLACK<<4));
imaget=strchr(file_name,'.');
if(imaget!=NULL)
{
j=strcspn(file_name,".");
file_name[j]='\0';
}
imaget=strcpy(temp,file_name);
imaget=strcat(temp,".hdr");
fptro=fopen(temp,"wb");
/*save size of block,
size of image and # of quant. levels .*/
putc(NB,fptro);
putc(NQ,fptro);
k=(255)&N;
putc(k,fptro);
k=(255)&(N>>8);
putc(k,fptro);
/* Zonal Sampling matrix. */
j=0; k=0; loc=0;
for(i=0;i<Nt;i++)
{
j=(j<<1)|T[i];
k++;
if(k==8)
{
putc(j,fptro);
k=0;
}
}
/* standard deviations and means.*/
for(i=0; i<Nt; i++)
{
k=i<<1;
buffi[k]=sigma[i];
buffi[k+1]=mu[i];