/*
 * ImageRotator.cpp
 *
 * (c) 2002 by Thorsten Scheuermann <tscheuermann@gmx.net>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 */

#include "ImageRotator.h"
#include "Image.h"

ImageRotator::ImageRotator()
: destImage(NULL)
{
  rotation = ROTATION_NONE;
}

ImageRotator::~ImageRotator()
{
}

void ImageRotator::setRotation(RotationMode rot)
{
  rotation = rot;
}

void ImageRotator::setImageDimension(int w, int h)
{
  inWidth = w;
  inHeight = h;

  if((rotation == ROTATION_90) || (rotation == ROTATION_270))
  {
    outWidth = inHeight;
    outHeight = inWidth;
  }
  else
  {
    outWidth = inWidth;
    outHeight = inHeight;
  }
}

void ImageRotator::setNumImageChannels(int c)
{
}

void ImageRotator::startImage()
{
  if(destImage)
  {
    destImage->setImageDimension(outWidth, outHeight);
    destImage->setNumImageChannels(3);
    destImage->startImage();
  }

  switch(rotation)
  {
  case ROTATION_NONE:
  case ROTATION_270:
    count = 0;
    break;

  case ROTATION_90:
    count = outWidth - 1;
    break;

  case ROTATION_180:
    count = outHeight - 1;
    break;
  }
}

void ImageRotator::endImage()
{
  if(destImage)
    destImage->endImage();
}

void ImageRotator::imageRow(unsigned char *rowData)
{
  if(!destImage)
    return;

  switch(rotation)
  {
  case ROTATION_NONE:
    destImage->imageRow(rowData);
    break;

  case ROTATION_90:
    destImage->imageColumn(count, rowData);
    count--;
    break;

  case ROTATION_180:
    destImage->imageRowReverse(count, rowData);
    count--;
    break;
    
  case ROTATION_270:
    destImage->imageColumnReverse(count, rowData);
    count++;
    break;
  }
}

void ImageRotator::imageRow(fixed *rowData)
{
  if(!destImage)
    return;

  switch(rotation)
  {
  case ROTATION_NONE:
    destImage->imageRow(rowData);
    break;

  case ROTATION_90:
    destImage->imageColumn(count, rowData);
    count--;
    break;

  case ROTATION_180:
    destImage->imageRowReverse(count, rowData);
    count--;
    break;

  case ROTATION_270:
    destImage->imageColumnReverse(count, rowData);
    count++;
    break;
  }
}

void ImageRotator::setDestImage(Image *img)
{
  destImage = img;
}
