/* Blinker
   Copyright 2011-2019 Stefan Schuermans <stefan@blinkenarea.org>
   Copyleft GNU public license - http://www.gnu.org/copyleft/gpl.html
   a blinkenarea.org project */

#include <stdlib.h>
#include <string>

#include <BlinkenLib/BlinkenFrame.h>

#include "Directory.h"
#include "File.h"
#include "Format.h"
#include "FormatFile.h"
#include "InStreamFile.h"
#include "Mgrs.h"
#include "Module.h"
#include "OutStreamFile.h"
#include "Resizer.h"
#include "StreamRecv.h"

namespace Blinker {

/**
 * @brief constructor
 * @param[in] name module name
 * @param[in] mgrs managers
 * @param[in] dirBase base directory
 */
Resizer::Resizer(const std::string &name, Mgrs &mgrs,
                 const Directory &dirBase):
  Module(name, mgrs, dirBase),
  m_fileInStream(dirBase.getFile("instream"), mgrs.m_streamMgr),
  m_fileFormat(dirBase.getFile("format")),
  m_fileOutStream(dirBase.getFile("outstream"), mgrs.m_streamMgr)
{
  // set up
  getFormat();
  m_fileInStream.setStreamRecv(this);
}

/// virtual destructor
Resizer::~Resizer()
{
  // clean up
  m_fileInStream.setStreamRecv(NULL);
}

/// check for update of configuration
void Resizer::updateConfig()
{
  // stream name or format file was modified -> re-get stream or format
  if (m_fileInStream.checkModified())
    m_fileInStream.update();
  if (m_fileFormat.checkModified())
    getFormat();
  if (m_fileOutStream.checkModified())
    m_fileOutStream.update();
}

/**
 * @brief set current frame
 * @param[in] stream stream name
 * @param[in] pFrame current frame (NULL for none)
 */
void Resizer::setFrame(const std::string &stream, stBlinkenFrame *pFrame)
{
  procFrame(pFrame);
  (void)stream; // unused
}

/// (re-)get format to resize to
void Resizer::getFormat()
{
  // read format from format file
  m_fileFormat.update();

  // send current frame to output stream
  sendFrame();
}

/// send current frame to output stream
void Resizer::sendFrame()
{
  // get current frame from input stream and process it
  procFrame(m_fileInStream.getCurFrame());
}

/**
 * @brief process frame
 * @param[in] pFrame frame to process (NULL for none)
 */
void Resizer::procFrame(stBlinkenFrame *pFrame)
{
  int width, height, channels, maxval;
  stBlinkenFrame *pProcFrame;

  // no frame or no format -> pass "no frame"
  if (!pFrame || !m_fileFormat.m_valid) {
    m_fileOutStream.setFrame(NULL);
    return;
  }
  width = m_fileFormat.m_obj.m_width;
  height = m_fileFormat.m_obj.m_height;
  channels = m_fileFormat.m_obj.m_channels;
  maxval = m_fileFormat.m_obj.m_maxval;

  // format matches -> pass frame directly
  if (BlinkenFrameGetWidth(pFrame) == width &&
      BlinkenFrameGetHeight(pFrame) == height &&
      BlinkenFrameGetChannels(pFrame) == channels &&
      BlinkenFrameGetMaxval(pFrame) == maxval) {
    m_fileOutStream.setFrame(pFrame);
    return;
  }

  // clone frame
  pProcFrame = BlinkenFrameClone(pFrame);
  if (!pProcFrame){
    m_fileOutStream.setFrame(NULL);
    return;
  }

  // resize frame
  BlinkenFrameResize(pProcFrame, height, width, channels, maxval);

  // pass processed frame to output stream
  m_fileOutStream.setFrame(pProcFrame);

  // free cloned frame
  BlinkenFrameFree(pProcFrame);
}

} // namespace Blinker

