Advertisement
Guest User

Untitled

a guest
Jun 30th, 2015
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.11 KB | None | 0 0
  1. #include "scoped_ptr.h"
  2. #include "dynamic_array.h"
  3. #include "epng.h"
  4. #include "collage.h"
  5. #include <stdexcept>
  6. #include <iostream>
  7. #include <string>
  8. namespace cs225
  9. {
  10. //size initializer
  11. collage::collage(uint64_t size)
  12. :img_arr_{size}, x_cords_{size}, y_cords_{size}
  13. {
  14. //empty
  15.  
  16. }
  17.  
  18. //copy constructor
  19. collage::collage(const collage& other)
  20. :x_cords_{other.x_cords_}, y_cords_{other.y_cords_}
  21. {//test fails...
  22. img_arr_ = other.img_arr_.size();
  23. for(uint64_t i = 0; i < other.img_arr_.size(); ++i){
  24. if(!other.img_arr_.at(i).empty()){
  25. x_cords_.at(i) = other.x_cords_.at(i);
  26. y_cords_.at(i) = other.y_cords_.at(i);
  27. epng::png* newimg = new epng::png{*other.img_arr_.at(i).get()};
  28. img_arr_.at(i) = newimg;
  29. }
  30. }
  31.  
  32.  
  33. }
  34.  
  35. //move constructor
  36. collage::collage(collage&& other)
  37. :img_arr_{}, x_cords_{}, y_cords_{}
  38. {
  39. swap(other);
  40.  
  41. }
  42.  
  43. //assignment operator
  44. collage& collage::operator=(collage rhs){
  45. swap(rhs);
  46. return *this;
  47. }
  48.  
  49. //destructor
  50. collage::~collage()
  51. {
  52. clear();
  53. }
  54.  
  55. void collage::clear()
  56. {
  57. x_cords_.clear();
  58. y_cords_.clear();
  59. for(uint64_t i = 0; i < img_arr_.size(); ++i){
  60. img_arr_.at(i).clear();
  61. }
  62. img_arr_.clear();
  63. }
  64.  
  65.  
  66. //swap function
  67. void collage::swap(collage& other){
  68. img_arr_.swap(other.img_arr_);
  69. x_cords_.swap(other.x_cords_);
  70. y_cords_.swap(other.y_cords_);
  71. }
  72.  
  73. //changes # layers in collage
  74. void collage::layers(uint64_t max){
  75. img_arr_.resize(max);
  76. x_cords_.resize(max);
  77. y_cords_.resize(max);
  78. }
  79.  
  80. //returns max # layers
  81. uint64_t collage::layers() const{
  82. return img_arr_.size();
  83. }
  84.  
  85. //returns # filled layers in collage
  86. uint64_t collage::filled() const{
  87. uint64_t count = 0;
  88. //for loop of img_arr_ checking if scoped ptr is empty?
  89. for(uint64_t i = 0; i < img_arr_.size(); ++i){
  90. if (!img_arr_.at(i).empty())
  91. count = count + 1;
  92.  
  93. }
  94. return count;
  95. }
  96.  
  97. // adds a new picture to collage at given index
  98. void collage::emplace_picture(const std::string & filename, uint64_t index, uint64_t x, uint64_t y){
  99. x_cords_.at(index) = x;
  100. y_cords_.at(index) = y;
  101. epng::png* original = new epng::png{};
  102. original->load(filename);
  103. img_arr_.at(index) = original;
  104.  
  105. }
  106.  
  107. //moves an image between layers
  108. void collage::change_layer(uint64_t src, uint64_t dest){
  109. img_arr_.at(dest).clear();
  110. x_cords_.at(dest) = x_cords_.at(src);
  111. y_cords_.at(dest) = y_cords_.at(src);
  112.  
  113. img_arr_.at(src).swap(img_arr_.at(dest));
  114.  
  115. }
  116.  
  117. //sets the position of the image at the given index
  118. void collage::position(uint64_t index, uint64_t x, uint64_t y){
  119. x_cords_.at(index) = x;
  120. y_cords_.at(index) = y;
  121. }
  122.  
  123. //removes the image at the given index
  124. void collage::erase(uint64_t index){
  125. img_arr_.at(index).clear();
  126. x_cords_.at(index) = 0;
  127. y_cords_.at(index) = 0;
  128. }
  129.  
  130. //accesor function for the underlying images
  131. const epng::png* collage::at(uint64_t index) const{
  132. return img_arr_.at(index).get();
  133. }
  134.  
  135. //accesor function for the underlying images
  136. epng::png* collage::at(uint64_t index){
  137. return img_arr_.at(index).get();
  138.  
  139. }
  140.  
  141. //draws the collage onto a new images and returns the new image
  142. epng::png collage::draw() const{
  143. uint64_t hmax = 0;
  144. uint64_t wmax = 0;
  145. //get max size of canvas to be made
  146. for(uint64_t i = 0; i < img_arr_.size(); ++i){
  147. if(!img_arr_.at(i).empty()){
  148. if(x_cords_.at(i) + img_arr_.at(i)->width() > wmax)
  149. wmax = x_cords_.at(i) + img_arr_.at(i)->width();
  150. if(y_cords_.at(i) + img_arr_.at(i)->height() > hmax)
  151. hmax = y_cords_.at(i) + img_arr_.at(i)->height();
  152. }
  153. }
  154.  
  155. epng::png canvas = epng::png(wmax, hmax); //max size?
  156.  
  157. //copy pixel by pixel over from each img?
  158. for(uint64_t i = 0; i < img_arr_.size(); ++i){
  159. if(!img_arr_.at(i).empty()){
  160. for(uint64_t w =0; w< img_arr_.at(i)->width(); ++w){
  161. for(uint64_t h =0; h< img_arr_.at(i)->height(); ++h){
  162.  
  163. *(canvas)(w+x_cords_.at(i),h+y_cords_.at(i)) = *(*img_arr_.at(i).get())(w,h);
  164. }
  165. }
  166. }
  167. }
  168. return canvas;
  169. }
  170.  
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement