/************************************************************************************/
/*
 * File name: InteractiveBayer.cpp
 * Location:  ...\Matrox Imaging\MILxxx\Examples\Processing\Preprocessing\InteractiveBayer\C++
 *             
 *
 * Synopsis:  This program shows how to perform Bayer-to-Color conversion.
 *
 * Copyright (C) Matrox Electronic Systems Ltd., 1992-2015.
 * All Rights Reserved
 */

#include "mil.h"

int MosMain(void)
   {
   MosPrintf(MIL_TEXT("[EXAMPLE NAME]\nInteractiveBayer\n\n"));

   MIL_ID   MilApplication,
            MilSystem,
            MilDigitizer,
            MilDisplay,
            MilWBCoefficients,
            MilImageDisp,
            MilImageGrab;

   /* User array for white balance coefficients. */
   float WBCoefficients[3];

   /* Specify the Bayer pattern of your camera. */
   MIL_INT ConversionType;

   /* Specify the Bayer conversion is done by the digitizer. */
   MIL_INT BayerConversion;

   /* Buffer characteristics. */
   MIL_INT XSize;
   MIL_INT YSize;

   /* Allocate an application. */
   MappAllocDefault(M_DEFAULT, &MilApplication, &MilSystem, &MilDisplay, &MilDigitizer, M_NULL);

   if ((MdigInquire(MilDigitizer, M_EXTENDED_INIT_FLAG, M_NULL) & M_EMULATED) == M_EMULATED)
      {
      /* If the digitizer is in emulated mode, release the allocated object and quit. */
      MosPrintf(MIL_TEXT("The example does not run with a digitizer in emulated mode.\n\n"));
      MappFreeDefault(MilApplication, MilSystem, MilDisplay, MilDigitizer, M_NULL);
      MosPrintf(MIL_TEXT("Press <ENTER> to end\n"));
      MosGetchar();
      return 1;
      }

   /* Inquire about the Bayer properties. */ 
   MdigInquire(MilDigitizer, M_BAYER_PATTERN, &ConversionType);
   MdigInquire(MilDigitizer, M_BAYER_CONVERSION, &BayerConversion);

   if ((ConversionType==M_NULL) || (BayerConversion==M_ENABLE))
      {
      /* If there is no Bayer pattern used by the camera,    */
      /* or if the Bayer conversion is done by the digitizer */
      /* then release the allocated objects and quit.        */
      MosPrintf(MIL_TEXT("The example requires acquiring a raw Bayer image.\n\n"));
      MappFreeDefault(MilApplication, MilSystem, MilDisplay, MilDigitizer, M_NULL);
      MosPrintf(MIL_TEXT("Press <ENTER> to end\n"));
      MosGetchar();
      return 1;
      }

   XSize = MdigInquire(MilDigitizer, M_SIZE_X, M_NULL);
   YSize = MdigInquire(MilDigitizer, M_SIZE_Y, M_NULL);

   /* Allocate a display buffer. */
   MbufAllocColor(MilSystem, 3, XSize, YSize, 8L+M_UNSIGNED, M_PROC+M_IMAGE+M_DISP,&MilImageDisp);

   /* Allocate a grab buffer. */
   MbufAllocColor(MilSystem, 1, XSize, YSize, 8L+M_UNSIGNED, M_IMAGE+M_DISP+M_GRAB+M_PROC, &MilImageGrab);

   /* Allocate an array for the white balance coefficients. */
   MbufAlloc1d(MilSystem, 3, 32+M_FLOAT, M_ARRAY, &MilWBCoefficients);

   /* Display the image. */
   MbufClear(MilImageDisp, M_RGB888(0, 0, 0));
   MdispSelect(MilDisplay, MilImageDisp);
   
   /* Ask the user for a white image for white balance. */
   MosPrintf(MIL_TEXT("Place a white reference in front of the\n"));
   MosPrintf(MIL_TEXT("camera and press <ENTER> when ready.\n"));

   do
      {
      /* Grab a white Bayer image. */
      MdigGrab(MilDigitizer, MilImageGrab);

      /* Convert the white Bayer image to color without white balance. */
      MbufBayer(MilImageGrab, MilImageDisp, M_DEFAULT, ConversionType);
      }
   while (!MosKbhit());

   /* Determine the white balance coefficients. */
   MbufBayer(MilImageGrab, MilImageDisp, MilWBCoefficients,
      ConversionType+M_WHITE_BALANCE_CALCULATE);
   
   /* Print the computed coefficients. */
   MbufGet(MilWBCoefficients, (void *) &WBCoefficients[0]);
   MosPrintf(MIL_TEXT("\nWhite balance correction coefficients : %f, %f, %f\n\n"),
      WBCoefficients[0], WBCoefficients[1], WBCoefficients[2]);
   
   /* Grab a new Bayer image with white balance correction. */
   MosPrintf(MIL_TEXT("Press <ENTER> to grab white balanced images\n"));
   MosGetchar();
   
   do
      {
      /* Grab a Bayer image. */
      MdigGrab(MilDigitizer, MilImageGrab);

      /* Convert the Bayer image to color. */
      MbufBayer(MilImageGrab, MilImageDisp, MilWBCoefficients, ConversionType);
      }
   while (!MosKbhit());

   MosPrintf(MIL_TEXT("Press <ENTER> to end\n"));
   MosGetchar();

   /* Terminate and free allocated resources. */
   MbufFree(MilImageGrab);
   MbufFree(MilImageDisp);
   MbufFree(MilWBCoefficients);
   MappFreeDefault(MilApplication, MilSystem, MilDisplay, MilDigitizer, M_NULL);
   
   return 0;
   }