/*------------------------------------------------------------------------------
 src/plugin/djImageIoVlut/base.cxx
 
 Copyright (c) 2004-2006 Darby Johnston.
 All rights reserved.
 Use is subject to license terms.
------------------------------------------------------------------------------*/

//#define DJ_DBG
#include "base.h"

namespace djImageIoVlut {

XHandle::XHandle(const Str & displayName) :
  display(0), screen(0)
{
  display = XOpenDisplay(displayName.toAscii());
  if (! display) return;
  screen = DefaultScreen(display);
}

XHandle::~XHandle()
{
  if (display) XCloseDisplay(display);
}

#if defined(DJ_VLUT_XORG)

VlutHandle::VlutHandle(int size, int channels) :
  data(0), size(size), channels(channels)
{
  if (size) {
    data = new unsigned short * [channels];
    for (int c = 0; c < channels; ++c) {
      data[c] = new unsigned short [size];
      memZero(data[c], size * sizeof(unsigned short));
    }
  }
}

VlutHandle::~VlutHandle()
{
  if (data) {
    for (int c = 0; c < channels; ++c)
      delete [] data[c];
    delete [] data;
  }
}

#elif defined(DJ_VLUT_XSGI)

VlutHandle::VlutHandle(int size, int channels) :
  data(0), size(size), channels(channels)
{
  if (channels) {
    data = new unsigned short * [channels];
    for (int c = 0; c < channels; ++c) data[c] = 0;
    if (size) {
      _internal = true;
      for (int c = 0; c < channels; ++c) {
        data[c] = new unsigned short [size];
        memZero(data[c], size * sizeof(unsigned short));
      }
    }
  }
}

VlutHandle::~VlutHandle()
{
  if (data) {
    if (_internal)
      for (int c = 0; c < channels; ++c)
        delete [] data[c];
    delete [] data;
  }
}

#endif

}
