// CppDemo.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include "LoadBMP.h"
#include "MicroPDF417_Types.h"

const int MAX_ROW = 3000;
const int MAX_COL = 3000;

static void ExitMsg();

int main(int argc, char **argv)
{
	TMPDF417_OptMode opts; // Decoder options
	PMPDF417_Options opt;  // Options object
	PMPDF417_Decoder dec;  // Decoder 

	char name[] = ".\\image\\YM994730Z12.bmp";  // Image file name

	// Buffers for image data
	unsigned char* pbits[MAX_ROW];
	unsigned char* image = new unsigned char[MAX_ROW*MAX_COL];

	// Set pointers to image rows
	for (int i = 0; i < MAX_ROW; i++)
		pbits[i] = &(image[i*MAX_COL]);

	int row = MAX_ROW;
	int col = MAX_COL;

	// Image file load. Supported only BMP
	if (LoadBMP(name, pbits, row, col) != 0)
	{
		printf("FAILED LoadBMP\n");
		ExitMsg();
		return 0;
	}

	printf("Image: %s\n", name);

	// Create decoder.
	dec = Connect_MPDF417_Decoder(MAX_ROW, MAX_COL);
	if (!dec)
	{
		printf("FAILED Connect_MPDF417_Decoder\n");
		ExitMsg();
		return 0;
	}
	
	opts.DirectMode = DM_All;   // RESERVED FOR FUTURE!
	opts.SpeedMode = SP_Robust; // RESERVED FOR FUTURE!

	// Create object of options.
	opt = Create_MPDF417_Options(dec, opts);
	if (!opt)
	{
		printf("FAILED Create_MPDF417_Options\n");
		Disconnect_MPDF417_Decoder(dec);
		ExitMsg();
		return 0;
	}

	// Decode the image.
	Decode_MPDF417_Bits(opt, row, col, pbits);

	// Get result of decoding
	TMPDF417_ImageInfo* inf = Get_MPDF417_ImageInfo(opt);
	if (inf->RejectionReason == 0) // Decoding OK
	{
		// Get decoded data
		TMPDF417_Info* info = Get_MPDF417_Info(opt);
		printf("OK\n%s\n", info->pch);
		
		printf("\nSymbol rect: ");
		for (int i = 0; i < 4; i++)
			printf("%d:%d ", info->rowcols[i*2], info->rowcols[i * 2 + 1]);
		printf("\n");
	}
	else
	{
		printf("Not found. RR=%d\n", inf->RejectionReason);
	}
	
	// Free memory
	Delete_MPDF417_Options(opt);
	Disconnect_MPDF417_Decoder(dec);
	
	delete image;
	ExitMsg();
	return 0;
}

void ExitMsg()
{
	char str[16];
	printf("\nPress ENTER to exit.\n");
	scanf("%c", str);
}

