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

#undef DPRINTF
#include "WImage/ImageArray.hh"
#include "WImage/GreymapWriter.hh"
#include "WImage/debug.h"
#include "WImage/tools.h"
#include <cmath>
#include <iostream>
#include <stdexcept>
#include <fstream>
#include <cassert>
#include <cfloat>

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

void
GreymapWriter::write (void)
{
  int success = 0;


  /*
   * First check whether file is writeable...
   */
  if (m_fname != NULL)
    {
      ofstream ofs (m_fname);
      if (!ofs)
        {
          success = -1;
        }
    }

  if (success == 0)
    {
      m_pixels->resize (m_coeffs->rows (), m_coeffs->cols ());

      pixelize ();

      success = writefmt ();
    }

  if (success < 0)
    {
      throw ios_base::failure ("write: error writing file.");
    }
}

void
GreymapWriter::pixelize (void)
{
  for (int i = 0; i < m_coeffs->size (); i++)
    {
      m_pixels->to (i, tools_coeff2pixel (m_coeffs->at (i)));
    }
}



