summaryrefslogtreecommitdiff
path: root/hw2/src/image.cpp
blob: 59434cc61a69e369495255a55118e8b89999d803 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/******************************************************************/
/* 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
}