// MSVS_Demo.cpp : Defines the entry point for the console application.
//
#define _CRT_SECURE_NO_WARNINGS
#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 "QRPro_Types.h"
#include "LoadBMP.cpp"

//---------------------------------------------------------------------------

char* files[] =
{
 "im1.bmp"
,"im2.bmp"
,"im3.bmp"
,NULL   // last member of array must be NULL 
};
unsigned char *pbits[3000];

int _cdecl _tmain(int argc, _TCHAR* argv[])
{
  PQR_Decoder    pdecoder = NULL;
  PQR_Options    pdec = NULL;
  TQR_OptMode    optmode;
  PQR_ImageInfo  pimageinfo;
  PQR_Info       pqrinfo;
  PQR_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 qrc, n;
  char GRADE[] = "FDCBA";

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

  HINSTANCE dllinstance;

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

  TConnect_QR_Decoder     Connect_QR_Decoder;
  TDisconnect_QR_Decoder  Disconnect_QR_Decoder;
  TCreate_QR_Options      Create_QR_Options;
  TDelete_QR_Options      Delete_QR_Options;
  TDecodeQR_Bits          DecodeQR_Bits;
  TGetQR_ImageInfo        GetQR_ImageInfo;
  TGetQR_Info             GetQR_Info;

  totaltime = 0;
  dllinstance = LoadLibrary(L"QRC_Pro.dll");
  if (dllinstance==NULL)
     goto doExit;
  Connect_QR_Decoder    = (TConnect_QR_Decoder    )GetProcAddress(dllinstance,"Connect_QR_Decoder");
  Disconnect_QR_Decoder = (TDisconnect_QR_Decoder )GetProcAddress(dllinstance,"Disconnect_QR_Decoder");
  Create_QR_Options     = (TCreate_QR_Options     )GetProcAddress(dllinstance,"Create_QR_Options");
  Delete_QR_Options     = (TDelete_QR_Options     )GetProcAddress(dllinstance,"Delete_QR_Options");
  DecodeQR_Bits         = (TDecodeQR_Bits         )GetProcAddress(dllinstance,"DecodeQR_Bits");
  GetQR_ImageInfo       = (TGetQR_ImageInfo       )GetProcAddress(dllinstance,"GetQR_ImageInfo");
  GetQR_Info            = (TGetQR_Info            )GetProcAddress(dllinstance,"GetQR_Info");

  if (Connect_QR_Decoder==NULL) 
     goto doExit;
  
  pdecoder = Connect_QR_Decoder(3000,3000);

  optmode.maxQRCount = 10;
  optmode.timeOut    = 0; // 0 ms => don't check time-out
  optmode.qualityMask= QR_QM_ALL;
  optmode.symbology  = QRC_ANY;
  optmode.cellColor  = QRCL_ANY;
  optmode.filterMode = QRFM_NON;
  optmode.mirrorMode = QRMM_ANY;
  optmode.qzMode     = QRQZ_STANDARD;

  pdec   = Create_QR_Options(pdecoder,optmode);
  pimageinfo = NULL;
  pqrinfo    = 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 = (int)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,files[i]);
//printf("\n%s",fn);
      res = LoadBMP(fn,pbits,rc,cc);
      if (res==0){
        totalfiles++;
        dectime = clock();
        DecodeQR_Bits  (pdec,rc,cc,pbits); // res = QRCount
        dectime = clock()-dectime;
        totaltime += dectime;
        pimageinfo = GetQR_ImageInfo(pdec);
        res   = pimageinfo->RejectionReason;
		qrc = pimageinfo->QRCount;
        printf("\n\n%20s ,  Time=%4d ms,  RR=%d,  QRCount=%2d"
			  ,files[i], dectime, res, qrc);
        if (qrc>0)
        for (n=0;n<qrc;n++){
          pqrinfo    = GetQR_Info(pdec, n);
          qp         = &(pqrinfo->quality);
	      if (pqrinfo->Micro_QRC)
		    printf("\n  <M%d, %c>" ,pqrinfo->Version,pqrinfo->level); 
		  else
		    printf("\n  < %d, %c>" ,pqrinfo->Version,pqrinfo->level);
		  if (pqrinfo->Mirrored)
            strcpy(mirrored,"Yes");
		  else
            strcpy(mirrored,"No");
		  printf(",   Mirrored: %s",mirrored); 
		  if (pqrinfo->pchlen>0){
		    Symbology_Id[0] = (char)pqrinfo->pch2[-3];
		    Symbology_Id[1] = (char)pqrinfo->pch2[-2];
		    Symbology_Id[2] = (char)pqrinfo->pch2[-1];
            Symbology_Id[3]=0;
            printf(",   Symbology: %s" ,Symbology_Id); 
		  }
          if (res==0){
            nOK++;
			total++;
			wprintf(L"\n  DecRes: %s",(wchar_t*)(pqrinfo->pch2));
		  }
          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(", FrmG = (%c) " ,GRADE[int(qp->frmG)]);
          printf(", VerG = (%c) " ,GRADE[int(qp->verG)]);
          printf(", ScanG = (%c) ",GRADE[int(qp->scanG)]);

          printf("\n");
        }
	  }else{
		printf("\n Error LoadBMP! ");
	  }
  }
  printf("\n ----------------------------------");
  printf("\n well decoded images: %d\n\n",total);

  if (pdec!=NULL)
    Delete_QR_Options (pdec);
  if (pdecoder!=NULL)
    Disconnect_QR_Decoder (pdecoder);

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

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

