summaryrefslogblamecommitdiff
path: root/hw5/src/image.cpp
blob: 59434cc61a69e369495255a55118e8b89999d803 (plain) (tree)

























































































































                                                                                                         
/******************************************************************/
/* This file is part of the homework assignments for CSCI-427/527 */
/* at The College of William & Mary and authored by Pieter Peers. */
/* No part of this file, whether altered or in original form, can */
/* be distributed or used outside the context of CSCI-427/527     */
/* without consent of either the College of William & Mary or     */
/* Pieter Peers.                                                  */
/******************************************************************/
#include "image.h"

#include <algorithm>
#include <cassert>

//////////////////
// Constructors //
//////////////////
image::image(image::size_type width, image::size_type height) : _width(width), _height(height), _data()
{
  if(width != 0 && height != 0)
    _data.reset(new value_type[width*height]);
}


image::image(image::size_type width, image::size_type height, const_reference col) : image(width, height)
{
  std::fill(begin(), end(), col);
}


image::image(const image& src) : image(src.width(), src.height())
{
  std::copy(src.begin(), src.end(), begin());
}


image::image(image&& src)
{
  _swap(src);
}


////////////////
// Inspectors //
////////////////
image::iterator image::begin(void)
{
  return _data.get();
}


image::const_iterator image::begin(void) const
{
  return _data.get();
}


image::iterator image::end(void)
{
  return begin() + size();
}


image::const_iterator image::end(void) const
{
  return begin() + size();
}


///////////////
// Operators //
///////////////
image& image::operator=(const image& src)
{
  _assign(src);
  return *this;
}


image& image::operator=(image&& src)
{
  _swap(src);
  return *this;
}

image::reference image::operator()(image::size_type x, image::size_type y)
{
  assert(x >= 0 && x < width());
  assert(y >= 0 && y < height());
  return begin()[y*width() + x];
}


image::const_reference image::operator()(image::size_type x, image::size_type y) const
{
  assert(x >= 0 && x < width());
  assert(y >= 0 && y < height());
  return begin()[y*width() + x];
}


/////////////////////
// Private Methods //
/////////////////////
void image::_swap(image& img)
{
  std::swap(_width, img._width);
  std::swap(_height, img._height);
  std::swap(_data, img._data);
}


void image::_assign(const image& src)
{
  // sanity check
  if(&src == this) return;

  // make copy
  image temp(src);
  _swap(temp);

  // Done
}