Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "scoped_ptr.h"
- #include "dynamic_array.h"
- #include "epng.h"
- #include "collage.h"
- #include <stdexcept>
- #include <iostream>
- #include <string>
- namespace cs225
- {
- //size initializer
- collage::collage(uint64_t size)
- :img_arr_{size}, x_cords_{size}, y_cords_{size}
- {
- //empty
- }
- //copy constructor
- collage::collage(const collage& other)
- :x_cords_{other.x_cords_}, y_cords_{other.y_cords_}
- {//test fails...
- img_arr_ = other.img_arr_.size();
- for(uint64_t i = 0; i < other.img_arr_.size(); ++i){
- if(!other.img_arr_.at(i).empty()){
- x_cords_.at(i) = other.x_cords_.at(i);
- y_cords_.at(i) = other.y_cords_.at(i);
- epng::png* newimg = new epng::png{*other.img_arr_.at(i).get()};
- img_arr_.at(i) = newimg;
- }
- }
- }
- //move constructor
- collage::collage(collage&& other)
- :img_arr_{}, x_cords_{}, y_cords_{}
- {
- swap(other);
- }
- //assignment operator
- collage& collage::operator=(collage rhs){
- swap(rhs);
- return *this;
- }
- //destructor
- collage::~collage()
- {
- clear();
- }
- void collage::clear()
- {
- x_cords_.clear();
- y_cords_.clear();
- for(uint64_t i = 0; i < img_arr_.size(); ++i){
- img_arr_.at(i).clear();
- }
- img_arr_.clear();
- }
- //swap function
- void collage::swap(collage& other){
- img_arr_.swap(other.img_arr_);
- x_cords_.swap(other.x_cords_);
- y_cords_.swap(other.y_cords_);
- }
- //changes # layers in collage
- void collage::layers(uint64_t max){
- img_arr_.resize(max);
- x_cords_.resize(max);
- y_cords_.resize(max);
- }
- //returns max # layers
- uint64_t collage::layers() const{
- return img_arr_.size();
- }
- //returns # filled layers in collage
- uint64_t collage::filled() const{
- uint64_t count = 0;
- //for loop of img_arr_ checking if scoped ptr is empty?
- for(uint64_t i = 0; i < img_arr_.size(); ++i){
- if (!img_arr_.at(i).empty())
- count = count + 1;
- }
- return count;
- }
- // adds a new picture to collage at given index
- void collage::emplace_picture(const std::string & filename, uint64_t index, uint64_t x, uint64_t y){
- x_cords_.at(index) = x;
- y_cords_.at(index) = y;
- epng::png* original = new epng::png{};
- original->load(filename);
- img_arr_.at(index) = original;
- }
- //moves an image between layers
- void collage::change_layer(uint64_t src, uint64_t dest){
- img_arr_.at(dest).clear();
- x_cords_.at(dest) = x_cords_.at(src);
- y_cords_.at(dest) = y_cords_.at(src);
- img_arr_.at(src).swap(img_arr_.at(dest));
- }
- //sets the position of the image at the given index
- void collage::position(uint64_t index, uint64_t x, uint64_t y){
- x_cords_.at(index) = x;
- y_cords_.at(index) = y;
- }
- //removes the image at the given index
- void collage::erase(uint64_t index){
- img_arr_.at(index).clear();
- x_cords_.at(index) = 0;
- y_cords_.at(index) = 0;
- }
- //accesor function for the underlying images
- const epng::png* collage::at(uint64_t index) const{
- return img_arr_.at(index).get();
- }
- //accesor function for the underlying images
- epng::png* collage::at(uint64_t index){
- return img_arr_.at(index).get();
- }
- //draws the collage onto a new images and returns the new image
- epng::png collage::draw() const{
- uint64_t hmax = 0;
- uint64_t wmax = 0;
- //get max size of canvas to be made
- for(uint64_t i = 0; i < img_arr_.size(); ++i){
- if(!img_arr_.at(i).empty()){
- if(x_cords_.at(i) + img_arr_.at(i)->width() > wmax)
- wmax = x_cords_.at(i) + img_arr_.at(i)->width();
- if(y_cords_.at(i) + img_arr_.at(i)->height() > hmax)
- hmax = y_cords_.at(i) + img_arr_.at(i)->height();
- }
- }
- epng::png canvas = epng::png(wmax, hmax); //max size?
- //copy pixel by pixel over from each img?
- for(uint64_t i = 0; i < img_arr_.size(); ++i){
- if(!img_arr_.at(i).empty()){
- for(uint64_t w =0; w< img_arr_.at(i)->width(); ++w){
- for(uint64_t h =0; h< img_arr_.at(i)->height(); ++h){
- *(canvas)(w+x_cords_.at(i),h+y_cords_.at(i)) = *(*img_arr_.at(i).get())(w,h);
- }
- }
- }
- }
- return canvas;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement