Advertisement
Guest User

Untitled

a guest
Feb 18th, 2020
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.24 KB | None | 0 0
  1. #include "StickerSheet.h"
  2. #include <cstdint>
  3. #include <iostream>
  4. #include "cs225/PNG.h"
  5. #include "cs225/HSLAPixel.h"
  6. #include <cmath>
  7. #include <cstdlib>
  8. #include "Image.h"
  9.  
  10. using namespace cs225;
  11. using namespace std;
  12.  
  13. StickerSheet::StickerSheet(const Image &picture, unsigned newMax){
  14. max = newMax;
  15. index = 0;
  16. tempImage = new Image(picture);
  17. pic = new Image*[max];
  18. xCoordinate = new int[max];
  19. yCoordinate = new int[max];
  20. for(unsigned i = 0; i < max; i++){
  21. pic[i] = NULL;
  22. }
  23.  
  24. }
  25.  
  26. StickerSheet::~StickerSheet(){
  27. clear();
  28. }
  29.  
  30. void StickerSheet::clear(){
  31. for(unsigned i = 0; i < max; i++){
  32. if(pic[i] != NULL){
  33. delete pic[i];
  34. pic[i] = NULL;
  35. }
  36. }
  37. delete[] pic;
  38. delete[] xCoordinate;
  39. delete[] yCoordinate;
  40. pic = NULL;
  41. xCoordinate = NULL;
  42. yCoordinate = NULL;
  43. }
  44.  
  45. StickerSheet::StickerSheet(const StickerSheet & other){
  46. copy(other);
  47. }
  48.  
  49. void StickerSheet::copy(const StickerSheet & other){
  50. index = other.index;
  51. max = other.max;
  52. tempImage = new Image(*other.tempImage);
  53. pic = new Image*[max];
  54. xCoordinate = new int[max];
  55. yCoordinate = new int[max];
  56. for(unsigned i = 0; i < max; i++){
  57. if(other.pic[i] != NULL)
  58. pic[i] = new Image(*(other.pic[i]));
  59. else
  60. pic[i] = NULL;
  61. xCoordinate[i] = other.xCoordinate[i];
  62. yCoordinate[i] = other.yCoordinate[i];
  63. }
  64. }
  65.  
  66. const StickerSheet& StickerSheet::operator= (const StickerSheet & other){
  67. if (this != &other){
  68. clear();
  69. copy(other);
  70. }
  71. return *this;
  72. }
  73.  
  74.  
  75. void StickerSheet::changeMaxStickers(unsigned newMax){
  76. if(newMax == max)
  77. return;
  78. if(newMax>max){
  79. Image** newpic = new Image*[newMax];
  80. for (unsigned i = 0; i < newMax; i++) {
  81. newpic[i] = NULL;
  82. }
  83. int* newxCoordinate = new int[newMax];
  84. int* newyCoordinate = new int[newMax];
  85. for(unsigned i = 0; i < index; i++){
  86. if(pic[i] != NULL){
  87. newpic[i] = new Image(*pic[i]);
  88. newxCoordinate[i] = xCoordinate[i];
  89. newyCoordinate[i] = yCoordinate[i];
  90. }
  91. }
  92. }
  93. else{
  94. for(unsigned i = newMax; i < max; i++){
  95. removeSticker(i);
  96. }
  97. }
  98. }
  99. int StickerSheet::addSticker(Image & sticker, unsigned x, unsigned y){
  100. if(index < max){
  101. for (unsigned i = 0; i < max; i++) {
  102. if(pic[i] == NULL){
  103. xCoordinate[i] = x;
  104. yCoordinate[i] = y;
  105. pic[i] = new Image(sticker);
  106. index++;
  107. return i;
  108. }
  109. }
  110. }
  111. return -1;
  112. }
  113.  
  114. bool StickerSheet::translate(unsigned index, unsigned x, unsigned y){
  115. if(index < 1 || index >= max || pic[index] == NULL){
  116. return false;
  117. }
  118. else{
  119. xCoordinate[index] = x;
  120. yCoordinate[index] = y;
  121. return true;
  122. }
  123. }
  124.  
  125. void StickerSheet::removeSticker(unsigned index){
  126. if(index >= max || pic[index] == NULL){
  127. return;
  128. }
  129. else{
  130. delete pic[index];
  131. pic[index] = NULL;
  132. xCoordinate[index] = 0;
  133. yCoordinate[index] = 0;
  134. }
  135. }
  136.  
  137. Image* StickerSheet::getSticker(unsigned index) const{
  138. if (index < max) return pic[index];
  139. else return NULL;
  140. }
  141.  
  142. Image StickerSheet::render() const{
  143. Image* base = new Image(*tempImage);
  144. unsigned xMax = base->width();
  145. unsigned yMax = base->height();
  146. for (unsigned i = 0; i < index; i++) {
  147. if(pic[i] != NULL){
  148. unsigned x = xCoordinate[i] + pic[i]->width();
  149. unsigned y = yCoordinate[i] + pic[i]->height();
  150. if(x > xMax)
  151. xMax = x;
  152. if(y > yMax)
  153. yMax = y;
  154. }
  155. }
  156. base->resize(xMax, yMax);
  157.  
  158. for(unsigned i = 0; i < index; i++){
  159. if(pic[i] != NULL){
  160. for(unsigned x = xCoordinate[i]; x < xCoordinate[i] + pic[i]->width(); x++){
  161. for (unsigned y = yCoordinate[i]; y < yCoordinate[i] + pic[i]->height(); y++) {
  162. HSLAPixel basePixel = base->getPixel(x,y);
  163. HSLAPixel pixel = pic[i]->getPixel(x - xCoordinate[i],y-yCoordinate[i]);
  164. if(pixel.a != 0){
  165. basePixel.h = pixel.h;
  166. basePixel.s = pixel.s;
  167. basePixel.l = pixel.l;
  168. basePixel.a = pixel.a;
  169. }
  170. }
  171. }
  172. }
  173. }
  174. Image result = *base;
  175. return result;
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement