summaryrefslogtreecommitdiff
path: root/hw2/src/image.cpp
diff options
context:
space:
mode:
author53hornet <53hornet@gmail.com>2019-02-02 23:33:15 -0500
committer53hornet <53hornet@gmail.com>2019-02-02 23:33:15 -0500
commitdb072ad4dc181eca5a1458656b130beb43f475bf (patch)
treea3c03c7f5497cb70503e2486662fa85cfb53415a /hw2/src/image.cpp
downloadcsci427-master.tar.xz
csci427-master.zip
Diffstat (limited to 'hw2/src/image.cpp')
-rw-r--r--hw2/src/image.cpp122
1 files changed, 122 insertions, 0 deletions
diff --git a/hw2/src/image.cpp b/hw2/src/image.cpp
new file mode 100644
index 0000000..59434cc
--- /dev/null
+++ b/hw2/src/image.cpp
@@ -0,0 +1,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
+}