/*
 * Implementation of class GreymapReader
 *
 * $Date$
 * $Revision$
 *
 */

#include "WImage/ImageArray.hh"
#include "WImage/GreymapReader.hh"
#include "WImage/debug.h"
#include <stdexcept>
#include <fstream>

#if !(defined __WATCOMC__ && __WATCOMC__ < 1230)
using namespace std;
#endif

void
GreymapReader::read (void)
{
  int success = 0;
  
  if (m_fname != NULL)
    {
      ifstream ifs (m_fname);
      if (!ifs)
        {
          success = -1;
        }
    }
  
  if (success == 0)
    {
      success = readfmt ();
    }
  
  switch (success)
    {
    case -1: throw ios_base::failure ("read: could not open file.");
    case -2: 
      throw invalid_argument ("read: invalid file format.");
    case 0: 
      if (m_pixels->size () > 0)
        {
          m_coeffs->resize (m_pixels->rows (), m_pixels->cols ());
          unpixel ();
        }
      break;
    default: 
      {
      }
    }

}

void
GreymapReader::unpixel (void)
{
  for (int i = 0; i < m_pixels->size (); i++)
    {
      m_coeffs->to (i, (coeff)m_pixels->at (i));
    }
}

