Newer
Older
Import / research / 3d-z-maps / mview-0.3.3 / mview / image.cpp
//
//    File: image.cc
//
//    (C) 2000-2008 Helmut Cantzler
//
//    Licensed under the terms of the Lesser General Public License.
//

#include "image.h"

Image::Image()
{
  image = NULL;
  imageData = NULL;
  imageHeight = imageWidth = 0;
}

Image::~Image()
{
  delete image;
  delete imageData;
}

int Image::read(const char *filename)
{
  delete image;
  image = new QImage(filename);

  if (image->isNull())
  {
    delete image;
    image = NULL;
    return 1;
  }

  imageHeight = image->width();
  imageWidth = image->height();

  return 0;
}

void Image::scale(int height, int width)
{
  if (image == NULL)
    return;

  // scale image
  QImage scaledImage = image->scaled(width, height);

  delete image;
  image = new QImage(scaledImage);

  imageHeight=height;
  imageWidth=width;
}

int Image::height(void) const
{
  return imageHeight;
}

int Image::width(void) const
{
  return imageWidth;
}

const unsigned char* Image::data(void)
{
  QRgb pixel;
  int y, x;

  if (image == NULL)
    return NULL;

  // Create the texture data structures
  delete imageData;
  imageData = new Array3D<unsigned char>(imageWidth, imageHeight, 3);

  // copy in data structure
  for (y=0; y < imageHeight; y++)
    for (x=0; x < imageWidth; x++)
    {
      pixel = image->pixel( x, imageHeight-y-1 );
      imageData->set(x, y, 0, qRed(pixel) );
      imageData->set(x, y, 1, qGreen(pixel) );
      imageData->set(x, y, 2, qBlue(pixel) );
    }

  return imageData->getData();
}