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

#include "WImage/PfcWriter.hh"
#include "WImage/debug.h"
#include <cstdio>
#include <pfi.h>

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

int
PfcWriter::writefmt (void)
{
  int rc = 0;
  
  int cmodel;
  switch (colormodel ())
    {
    case cm_rgb:
      cmodel = pfi_rgb;
      break;
    case cm_yuv:
      cmodel = pfi_yuv;
      break;
    default:
      return -1;
    }

  int xysize = m_images[0]->size ();
  
  coeff *allcoeffs = NEW (coeff [m_channels * xysize]);

  /* import */
  for (int i = 0; i < xysize; i++)
    {
      for (int j = 0; j < m_channels; j++)
        {
          allcoeffs[m_channels * i + j] = m_images[j]->at (i);
        }
    }

  /*
   * And the rest...
   */
  rc = pfi_write_name ((char*)m_fname, allcoeffs, m_images[0]->rows (), 
                       m_images[0]->cols (), m_channels, cmodel);
  
  /* cleanup */
  DELETEAR (allcoeffs);
  return rc;
} 
