// Demo_AztecCpp.cpp: определяет точку входа для консольного приложения.
//

#include "stdafx.h"
//
#include <Windows.h>
//
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <direct.h>


#include "LoadBMP.h"
#include "Az_Types.h"


TRow pbits[3000];


/*

*/
char* files[] =
{
 
 "2-10.bmp",
 "2-15.bmp",
 "2-20.bmp",
 "2-5.bmp",
 "2.bmp",
 "3-1 dam2.bmp",
 "4.BMP",
 "az1.bmp",////*/////

 NULL   // last member of array must be NULL 
};

 

int _tmain(int argc, _TCHAR* argv[])
{
  PAZ_Decoder    pdecoder = NULL;
  PAZ_Options    pdec = NULL;
  TAZ_OptMode    optmode;
  PAZ_ImageInfo  pimageinfo;
  PAZ_Info       pazinfo;
  TAZ_Quality    qp;


  int i;
  int res;
  int rc, cc;
  char fn[MAX_PATH];

  unsigned char *MemBits;
  char ch;
  int iAttributes = 0;
  int totalfiles = 0;
  int total=0,nOK = 0;
  int azc, n, k;
  char GRADE[] = "FDCBA";

  clock_t totaltime, dectime;
  char dllPath[MAX_PATH] = {0};

  HINSTANCE dllinstance;

  char Symbology_Id[4];
  char mirrored [4];


  TConnect_AZ_Decoder     Connect_AZ_Decoder;
  TDisconnect_AZ_Decoder  Disconnect_AZ_Decoder;
  TCreate_AZ_Options      Create_AZ_Options;
  TDelete_AZ_Options      Delete_AZ_Options;
  TDecode_AZ_Bits          Decode_AZ_Bits;
  TGet_AZ_ImageInfo        Get_AZ_ImageInfo;
  TGet_AZ_Info             Get_AZ_Info;

  totaltime = 0;
  dllinstance = LoadLibrary(L"icDecLibAZ.dll");
  if (dllinstance==NULL)
     goto doExit;
  Connect_AZ_Decoder    = (TConnect_AZ_Decoder    )GetProcAddress(dllinstance,"Connect_AZ_Decoder");
  Disconnect_AZ_Decoder = (TDisconnect_AZ_Decoder )GetProcAddress(dllinstance,"Disconnect_AZ_Decoder");
  Create_AZ_Options     = (TCreate_AZ_Options     )GetProcAddress(dllinstance,"Create_AZ_Options");
  Delete_AZ_Options     = (TDelete_AZ_Options     )GetProcAddress(dllinstance,"Delete_AZ_Options");
  Decode_AZ_Bits        = (TDecode_AZ_Bits       )GetProcAddress(dllinstance,"Decode_AZ_Bits");
  Get_AZ_ImageInfo      = (TGet_AZ_ImageInfo     )GetProcAddress(dllinstance,"Get_AZ_ImageInfo");
  Get_AZ_Info           = (TGet_AZ_Info          )GetProcAddress(dllinstance,"Get_AZ_Info");

  if (Connect_AZ_Decoder==NULL) 
     goto doExit;


pdecoder = Connect_AZ_Decoder(3000,3000);

  optmode.maxAZCount = 10;
  optmode.timeOut    = 0; // 0 ms => don't check time-out
  optmode.speedMode  = 0;
  optmode.qualityMask= AZ_QM_NO;
  optmode.symbology  = AZ_S_NORMAL;
  optmode.filterMode = 0;
  optmode.labelMode  = 0;
  optmode.qzMode     = AZ_QZ_STANDARD;

  pdec   = Create_AZ_Options(pdecoder,optmode);
  pimageinfo = NULL;
  pazinfo    = NULL;

  MemBits =(unsigned char*) malloc(3000*3000);
  for (i=0;i<3000;i++){
    pbits[i] = MemBits+3000*i;
  }
  GetModuleFileNameA( dllinstance, dllPath, MAX_PATH+1);
  for (i=strlen(dllPath);i>=0;i--){
    if (dllPath[i]=='\\')
      break;
  }

    dllPath[i+1] = 0;
//  const string dllp(dllPath);
  for (i=0;files[i]!=NULL;i++){
      rc = cc = 3000;
	  strcpy(fn,dllPath);
	  strcat(fn,"..\\Images\\");
	  strcat(fn,files[i]);
//printf("\n%s",fn);
      res = LoadBMP(fn,pbits,rc,cc);
      if (res==0){
        totalfiles++;
        dectime = clock();
        Decode_AZ_Bits  (pdec,rc,cc,pbits); // res = AZCount
        dectime = clock()-dectime;
        totaltime += dectime;
        pimageinfo = Get_AZ_ImageInfo(pdec);
        res   = pimageinfo->RejectionReason;
		azc = pimageinfo->AZCount;
        printf("\n\n%20s ,  Time=%4d ms,  RR=%d,  AZCount=%2d"
			  ,files[i], dectime, res, azc);
        if (azc>0)
        for (n=0;n<azc;n++){
          pazinfo    = Get_AZ_Info(pdec, n);
          qp         = (pazinfo->quality);

		  if (pazinfo->Rune_AZS==0)
		    printf("\n  fullsize" ); 
		  else
		  if (pazinfo->Rune_AZS==1)
		    printf("\n  compact" ); 
		  else
		  if (pazinfo->Rune_AZS==2)
		    printf("\n  rune" ); 
		  

		   
		  /*if (pazinfo->pchlen>0){
		    Symbology_Id[0]=pazinfo->pch2[-3];
		    Symbology_Id[1]=pazinfo->pch2[-2];
		    Symbology_Id[2]=pazinfo->pch2[-1];
            Symbology_Id[3]=0;
            printf(",   Symbology: %s" ,Symbology_Id); 
		  }*/
          if (res==0){
            nOK++;
			total++;
			printf("\n  DecRes: %s",pazinfo->pch);
		  }
		  if(optmode.qualityMask!=0)
		  {
          printf("\nSC = %7.2f (%c) "   ,qp.sc,GRADE[int(qp.scG)]);
          printf(", AxNU = %7.2f (%c) " ,qp.an,GRADE[int(qp.anG)]);
          printf(", GrNU = %7.2f (%c) " ,qp.gn,GRADE[int(qp.gnG)]);
          printf(", UEC = %7.2f (%c) "  ,qp.uec,GRADE[int(qp.uecG)]);
          printf(", FPD = (%c) "  ,GRADE[int(qp.fpdG)]);
          printf(", ModG = (%c) " ,GRADE[int(qp.modG)]);
          printf(", DecG = (%c) " ,GRADE[int(qp.deG)]);
         printf(", ScanG = (%c) ",GRADE[int(qp.scanG)]);
		  }
          printf("\n");
        }
	  }else{
		printf("\n Error LoadBMP! %s", fn);
	  }
  }
  printf("\n ----------------------------------");
  printf("\n well decoded images: %d\n\n",total);

  
doExit:

  if (pdec!=NULL)
    Delete_AZ_Options (pdec);
  if (pdecoder!=NULL)
    Disconnect_AZ_Decoder (pdecoder);

  if (totalfiles==0) totalfiles=1;
  printf("\n total files %d,   Aztec Count= %d,    Avr.time= %d"
	    ,totalfiles,nOK,totaltime/totalfiles);

  printf("\n test executed, press 'Enter' to exit>");
  scanf("%c",&ch);

	return 0;
}

