/*
-----------------------------------------------------------------------------
This source file is part of OpenSpace3D
For the latest info, see http://www.openspace3d.com

Copyright (c) 2010 I-maginer

This program is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free Software
Foundation; either version 2 of the License, or (at your option) any later
version.

This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along with
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
Place - Suite 330, Boston, MA 02111-1307, USA, or go to
http://www.gnu.org/copyleft/lesser.txt

You may alternatively use this source under the terms of a specific version of
the OpenSpace3D Unrestricted License provided you have obtained such a license from
I-maginer.
-----------------------------------------------------------------------------
*/

#include <opencv2/opencv.hpp>
#include "ScolConvert.h"

/*!
Scol to OpenCV data types conversions tools.
*/

ConversionTools::ConversionTools()
{
  // Forbiden (private). Use static functions instead.
}

void ConversionTools::ScolBitmapToMatRGB(PtrObjBitmap scolBitmap, cv::Mat mat)
{
  if (scolBitmap == 0)
    return;

  //Scol bitmap are 32bits aligned
  int sbpl = scolBitmap->BPL;
  int dbpl = scolBitmap->TailleW * 3;

  if (sbpl == dbpl)
    memcpy(mat.data, scolBitmap->bits, scolBitmap->TailleW * scolBitmap->TailleH * scolBitmap->BytesPP);
  else
    for (long y=0; y<scolBitmap->TailleH; y++)
    {
      for (long x=0; x<scolBitmap->TailleW; x++)
      {
        unsigned long srcByte = (x * scolBitmap->BytesPP) + (sbpl * y);
        unsigned long destByte = (x * 3) + (dbpl * y);

        mat.data[destByte] = scolBitmap->bits[srcByte];
        mat.data[destByte + 1] = scolBitmap->bits[srcByte + 1];
        mat.data[destByte + 2] = scolBitmap->bits[srcByte + 2];
      }
    }
}

void ConversionTools::MatToScolBitmapRGB(cv::Mat mat, PtrObjBitmap scolBitmap)
{
  if (scolBitmap == 0)
    return;

  //Scol bitmap are 32bits aligned
  int sbpl = scolBitmap->TailleW * 3;
  int dbpl = scolBitmap->BPL;

  if (sbpl == dbpl)
    memcpy(scolBitmap->bits, mat.data, scolBitmap->TailleW * scolBitmap->TailleH * scolBitmap->BytesPP);
  else
    for (long y=0; y<scolBitmap->TailleH; y++)
    {
      for (long x=0; x<scolBitmap->TailleW; x++)
      {
        unsigned long srcByte = (x * 3) + (sbpl * y);
        unsigned long destByte = (x * scolBitmap->BytesPP) + (dbpl * y);

        scolBitmap->bits[destByte] = mat.data[srcByte];
        scolBitmap->bits[destByte + 1] = mat.data[srcByte + 1];
        scolBitmap->bits[destByte + 2] = mat.data[srcByte + 2];
      }
    }
}