Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.16 KB | None | 0 0
  1. void putPixel(png_byte *cur_row, int x, int y, int r, int g, int b, int a = 255) {
  2. png_byte *ptr = &(cur_row[(x*4)]);
  3. ptr[0] = static_cast<png_byte>(r);
  4. ptr[1] = static_cast<png_byte>(g);
  5. ptr[2] = static_cast<png_byte>(b);
  6. ptr[3] = static_cast<png_byte>(a);
  7. }
  8.  
  9. void drawLine(struct Png *image, int x1, int y1, int x2, int y2, int r, int g, int b) {
  10. const int deltaX = abs(x2 - x1);
  11. const int deltaY = abs(y2 - y1);
  12. const int signX = x1 < x2 ? 1 : -1;
  13. const int signY = y1 < y2 ? 1 : -1;
  14. int error = deltaX - deltaY;
  15. if (x2 >= 0 && x2 < image -> width && y2 >= 0 && y2 < image -> height)
  16. putPixel(image -> row_pointers[y2], x2, y2, r, g, b);
  17. while(x1 != x2 || y1 != y2)
  18. {
  19. if (x1 >= 0 && x1 < image -> width && y1 >= 0 && y1 < image -> height)
  20. putPixel(image -> row_pointers[y1], x1, y1, r, g, b);
  21. const int error2 = error * 2;
  22. if(error2 > -deltaY)
  23. {
  24. error -= deltaY;
  25. x1 += signX;
  26. }
  27. if(error2 < deltaX)
  28. {
  29. error += deltaX;
  30. y1 += signY;
  31. }
  32. }
  33. }
  34.  
  35. void drawCircle(struct Png *image, int xc, int yc, int inner, int outer, int R, int G, int B)
  36. {
  37. int xo = outer;
  38. int xi = inner;
  39. int y = 0;
  40. int erro = 1 - xo;
  41. int erri = 1 - xi;
  42. while(xo >= y) {
  43. drawLine(image, xc + xi, yc + y, xc + xo, yc + y, R, G, B);
  44. drawLine(image, xc + y, yc + xi, xc + y, yc + xo, R, G, B);
  45. drawLine(image, xc - xo, yc + y, xc - xi, yc + y, R, G, B);
  46. drawLine(image, xc - y, yc + xi, xc - y, yc + xo, R, G, B);
  47. drawLine(image, xc - xo, yc - y, xc - xi, yc - y, R, G, B);
  48. drawLine(image, xc - y, yc - xo, xc - y, yc - xi, R, G, B);
  49. drawLine(image, xc + xi, yc - y, xc + xo, yc - y, R, G, B);
  50. drawLine(image, xc + y, yc - xo, xc + y, yc - xi, R, G, B);
  51. y++;
  52. if (erro < 0) {
  53. erro += 2 * y + 1;
  54. } else {
  55. xo--;
  56. erro += 2 * (y - xo + 1);
  57. }
  58. if (y > inner) {
  59. xi = y;
  60. } else {
  61. if (erri < 0) {
  62. erri += 2 * y + 1;
  63. } else {
  64. xi--;
  65. erri += 2 * (y - xi + 1);
  66. }
  67. }
  68. }
  69. }
  70.  
  71. void frameType1(struct Png *image, int width, int R, int G, int B) {
  72. for (int y = image -> height - width;;y -= width) {
  73. for (int j = y; j >= max(0, y - width); j--) {
  74. drawLine(image, 0, j, image -> width -1 - j, image -> height - 1, 255 - R, 255 - G, 255 - B);
  75. }
  76. y -= width;
  77. if (y < 0)
  78. break;
  79. }
  80. int ty = image -> height - 1;
  81. for (int j = ty; j >= max(0, ty - width + (image -> width % width)); j--) {
  82. drawLine(image, image -> width - 1, j, image -> width -1 - j, 0, 255 - R, 255 - G, 255 - B);
  83. }
  84. //ty -= width + (image -> width % width);
  85. for (int y = image -> width - 1;;y -= width) {
  86. for (int j = y; j >= max(0, y - width); j--) {
  87. drawLine(image, image -> width - 1, j, image -> width -1 - j, 0, 255 - R, 255 - G, 255 - B);
  88. }
  89. y -= width;
  90. if (y < 0)
  91. break;
  92. }
  93. }
  94.  
  95. void frameType2(struct Png *image, int width, int R, int G, int B) {
  96. for (int y = 0;; y += width / 2) {
  97. for (int x = 0;; x += width / 2) {
  98. drawCircle(image, x, y, width / 2 - width / 20, width / 2, 255 - R, 255 - G, 255 - B);
  99. if (x >= image -> width)
  100. break;
  101. }
  102. if (y >= image -> height)
  103. break;
  104. }
  105. }
  106.  
  107. void frameType3(struct Png *image, int width) {
  108. int step = width / 7;
  109. for (int y = 0;;y += step) {
  110. for (int x = 0;; x += step) {
  111. int r = rand() % 256;
  112. int g = rand() % 256;
  113. int b = rand() % 256;
  114. for (int i = y; i < min(y + step, image -> height); i++)
  115. for (int j = x; j < min(x + step, image -> width); j++) {
  116. putPixel(image->row_pointers[i], j, i, r, g, b);
  117. }
  118. if (x >= image -> width)
  119. break;
  120. }
  121. if (y >= image -> height)
  122. break;
  123. }
  124. }
  125.  
  126. void drawFrame(struct Png *image, int R, int G, int B, int width, int type) {
  127. int wid = image -> width;
  128. int hei = image -> height;
  129. png_bytep *new_row_pointers =(png_bytep *) malloc(sizeof(png_bytep) * hei);
  130. for(int y = 0; y < hei; y++)
  131. {
  132. new_row_pointers[y] = (png_byte *)malloc(sizeof(png_byte)*4 * wid);
  133. png_byte *new_row = new_row_pointers[y];
  134. png_byte *cur_row = image->row_pointers[y];
  135. for (int x = 0; x < wid; x++)
  136. {
  137. png_byte *ptr = &(cur_row[(x*4)]);
  138. putPixel(new_row, x, y, ptr[0], ptr[1], ptr[2], ptr[3]);
  139. }
  140. }
  141. int lenX = image -> width + 2 * width;
  142. int lenY = image -> height + 2 * width;
  143. image ->row_pointers = (png_bytep *) realloc(image -> row_pointers, lenY * sizeof(png_bytep));
  144. for (int i = 0; i < lenY; i++) {
  145. image->row_pointers[i] = (png_byte *) realloc(image->row_pointers[i], sizeof(png_byte)*4 * lenX);
  146. }
  147. image -> width = lenX;
  148. image -> height = lenY;
  149. for (int i = 0; i < image -> height; i++) {
  150. png_byte *cur_row = image->row_pointers[i];
  151. for (int j = 0; j < image -> width; j++) {
  152. putPixel(cur_row, j, i, R, G, B, 255);
  153. }
  154. }
  155.  
  156. switch (type) {
  157. case 1:
  158. frameType1(image, width, R, G, B);
  159. break;
  160. case 2:
  161. frameType2(image, width, R, G, B);
  162. break;
  163. case 3:
  164. frameType3(image, width);
  165. break;
  166. }
  167.  
  168.  
  169. for(int y = 0; y < hei; y++) {
  170. png_byte *new_row = new_row_pointers[y];
  171. png_byte *ch_row = image->row_pointers[width + y];
  172. for (int x = 0; x < wid; x++)
  173. {
  174. png_byte *toptr = &(new_row[x*4]);
  175. putPixel(ch_row, width + x, y, toptr[0], toptr[1], toptr[2], toptr[3]);
  176. }
  177. }
  178.  
  179. for (int y = 0; y < hei; y++)
  180. free(new_row_pointers[y]);
  181. free(new_row_pointers);
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement