
#include "FloatImage.h"
#include <QImage>

FloatImage::FloatImage(const char* filename)
{
  QImage img(filename);
  m_width   = img.width();
  m_height  = img.height();
  m_data.resize(m_width * m_height);

  for(int j=0; j<m_height; ++j)
    for(int i=0; i<m_width; ++i)
    {
      QRgb rgb = img.pixel(i,j);
      m_data[i+j*m_width] = Vec3(qRed(rgb)/255., qGreen(rgb)/255., qBlue(rgb)/255.);
    }
}

void FloatImage::save(const char* filename) const
{
  QImage img(m_width, m_height, QImage::Format_ARGB32);

  for(int j=0; j<m_height; ++j)
    for(int i=0; i<m_width; ++i)
    {
      img.setPixel(i, j, qRgb(m_data[i+j*m_width].x*255,
                              m_data[i+j*m_width].y*255,
                              m_data[i+j*m_width].z*255));
    }

  img.save(filename);
}

FloatImageGray::FloatImageGray(int width, int height)
{
  m_width = width;
  m_height = height;
  m_data.resize(m_width * m_height, 0);
}

FloatImageGray::FloatImageGray(const char* filename)
{
  QImage img(filename);
  m_width = img.width();
  m_height = img.height();
  m_data.resize(m_width * m_height);

  for(int j=0; j<m_height; ++j)
    for(int i=0; i<m_width; ++i)
    {
      QRgb rgb = img.pixel(i,j);
      m_data[i+j*m_width] = (qRed(rgb) * 11.0 + qGreen(rgb) * 16.0 + qBlue(rgb) * 5.0) / (32.*255.);
    }
}

void FloatImageGray::save(const char* filename) const
{
  QImage img(m_width, m_height, QImage::Format_ARGB32);

  for(int j=0; j<m_height; ++j)
    for(int i=0; i<m_width; ++i)
    {
      int v = m_data[i+j*m_width]*255;
      img.setPixel(i, j, qRgb(v,v,v));
    }

  img.save(filename);
}
