/*
 * Copyright 2016 Jan Dorazil <deu439@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

#include "preprocess.hpp"
#include <opencv2/opencv.hpp>

#define GRAYSCALE

using namespace cv;

Preprocess::Preprocess(int size, bool flt)
  : mSize(size), mFlt(flt)
{
}

void Preprocess::process(DataUnit& data)
{
  assert(!data.mMat[TEMP].empty());
  assert(!data.mMat[INPU].empty());
 
#ifdef GRAYSCALE
  // If it has 3 channels (probably BGR) convert to grayscale
  if( data.mMat[TEMP].channels() == 3 )
    cvtColor(data.mMat[TEMP], data.mMat[TEMP], COLOR_BGR2GRAY);
  if( data.mMat[INPU].channels() == 3 )
    cvtColor(data.mMat[INPU], data.mMat[INPU], COLOR_BGR2GRAY);

  // Make sure the output has one channel
  assert( data.mMat[TEMP].channels() == 1);
  assert( data.mMat[INPU].channels() == 1);
#endif
  
  // Compensate for noise
  if( mSize > 0 ){
    GaussianBlur(data.mMat[TEMP], data.mMat[TEMP], Size(mSize, mSize), -1);
    GaussianBlur(data.mMat[INPU], data.mMat[INPU], Size(mSize, mSize), -1);
  }
  
//   // Convert to float
  if(mFlt){
    data.mMat[TEMP].convertTo(data.mMat[TEMP], CV_32F);
    data.mMat[INPU].convertTo(data.mMat[INPU], CV_32F);
  }
}
