Advertisement
kknndd_

MIREK SA POSRAL

Apr 27th, 2021
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.65 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h.>
  4. #include <stdbool.h>
  5.  
  6. #include "transformations.h"
  7.  
  8. /*
  9. // Online C compiler to run C program online
  10. #include <stdio.h>
  11.  
  12. int main() {
  13.  
  14. int arr[] = {1,2,3,4,5,6,7,8,9};
  15.  
  16. int w = 3;
  17. int h = 3;
  18.  
  19. for(int i = 0; i < w*h; i++){
  20. printf("%d", arr[i]);
  21. }
  22. printf("\n");
  23.  
  24. int new_arr[w*h];
  25.  
  26. int idx = 0;
  27.  
  28. for(int i = 0; i < w; i++){
  29. for(int j = 0; j < h; j++){
  30. new_arr[idx] = arr[ ((j+1)*w)-i-1 ];
  31. idx++;
  32. }
  33. }
  34.  
  35. for(int i = 0; i < w*h; i++){
  36. printf("%d", new_arr[i]);
  37. }
  38. printf("\n");
  39.  
  40. return 0;
  41. }
  42. */
  43.  
  44. struct bmp_image* rotate_left(const struct bmp_image* image){
  45.  
  46. if(image == NULL || image->header == NULL || image->data == NULL){
  47. return NULL;
  48. }
  49.  
  50. struct bmp_image* new_image = malloc(sizeof (struct bmp_image*));
  51.  
  52. new_image = rotate_right(image);
  53. struct bmp_image* new_image2 = rotate_right(new_image);
  54. struct bmp_image* new_image3 = rotate_right(new_image2);
  55.  
  56. return new_image3;
  57.  
  58. // if(image == NULL || image->header == NULL || image->data == NULL){
  59. // return NULL;
  60. // }
  61. //
  62. // struct bmp_image* new_image = malloc(sizeof (struct bmp_image*));
  63. //
  64. // new_image->header = image->header;
  65. // new_image->data = malloc(sizeof (struct pixel*)*image->header->width*image->header->height);
  66. //
  67. // uint32_t old_width = image->header->width;
  68. // uint32_t old_height = image->header->height;
  69. //
  70. // new_image->header->width = old_height;
  71. // new_image->header->height = old_width;
  72. //
  73. // uint32_t idx = 0;
  74. //
  75. // for(uint32_t i = 0; i < old_width; i++){
  76. // for(uint32_t j = 0; j < old_height; j++){
  77. // new_image->data[idx] = image->data[ ((j)*old_width)+i ];
  78. // idx++;
  79. // }
  80. // }
  81. //
  82. // return new_image;
  83.  
  84. }
  85.  
  86. struct bmp_image* rotate_right(const struct bmp_image* image){
  87.  
  88. if(image == NULL || image->header == NULL || image->data == NULL){
  89. return NULL;
  90. }
  91.  
  92. struct bmp_image* new_image = malloc(sizeof (struct bmp_image*));
  93.  
  94. new_image->header = image->header;
  95. new_image->data = malloc(sizeof (struct pixel*)*image->header->width*image->header->height);
  96.  
  97. uint32_t old_width = image->header->width;
  98. uint32_t old_height = image->header->height;
  99.  
  100. new_image->header->width = old_height;
  101. new_image->header->height = old_width;
  102.  
  103. uint32_t idx = 0;
  104.  
  105.  
  106. for(uint32_t i = 0; i < old_width; i++){
  107. for(uint32_t j = 0; j < old_height; j++){
  108. new_image->data[idx] = image->data[ ((j+1)*old_width)-i-1 ];
  109. idx++;
  110. }
  111. }
  112.  
  113. return new_image;
  114.  
  115. }
  116.  
  117. /*
  118. * for(int i = 0; i < w*h; i++){
  119. if( i % w == 0 ){
  120. int temp = i;
  121. for(int j = 0; j < w; j++){
  122. mirek[temp+j] = obr[i-j+w-1];
  123. }
  124. }
  125. }
  126. * */
  127.  
  128. struct bmp_image* flip_horizontally(const struct bmp_image* image){
  129.  
  130. if(image == NULL || image->header == NULL || image->data == NULL){
  131. return NULL;
  132. }
  133.  
  134. struct bmp_image* new_image = malloc(sizeof (struct bmp_image*));
  135.  
  136. uint32_t size = image->header->width*image->header->height;
  137. uint32_t w = image->header->width;
  138.  
  139. new_image->header = image->header;
  140. new_image->data = malloc(sizeof (struct pixel*)*size);
  141.  
  142. for(uint32_t i = 0; i < size; i++){
  143. if( i % w == 0){
  144. uint32_t temp = i;
  145. for(uint32_t j = 0; j < image->header->width; j++){
  146. new_image->data[temp+j] = image->data[i-j+w-1];
  147. }
  148. }
  149. }
  150.  
  151. return new_image;
  152.  
  153. }
  154.  
  155. struct bmp_image* flip_vertically(const struct bmp_image* image){
  156.  
  157. if(image == NULL || image->header == NULL || image->data == NULL){
  158. return NULL;
  159. }
  160.  
  161. struct bmp_image* new_image = malloc(sizeof (struct bmp_image*));
  162.  
  163. new_image = rotate_right(image);
  164. struct bmp_image* new_image2 = rotate_right(new_image);
  165. struct bmp_image* new_image3 = flip_horizontally(new_image2);
  166.  
  167. // return new_image2;
  168. return new_image3;
  169. }
  170.  
  171. struct bmp_image* extract(const struct bmp_image* image, const char* colors_to_keep){
  172.  
  173. if(image == NULL || image->header == NULL || image->data == NULL || colors_to_keep == NULL || strlen(colors_to_keep) == 0){
  174. return NULL;
  175. }
  176.  
  177. struct bmp_image* new_image = malloc(sizeof (struct bmp_image*));
  178.  
  179. uint32_t size = image->header->width*image->header->height;
  180.  
  181. new_image->header = image->header;
  182. new_image->data = malloc(sizeof (struct pixel*)*size);
  183.  
  184.  
  185. bool r = true;
  186. bool g = true;
  187. bool b = true;
  188.  
  189. for(int i = 0; i < strlen(colors_to_keep); i++){
  190. if(colors_to_keep[i] == 'r'){
  191. r = false;
  192. break;
  193. }
  194. }
  195.  
  196. for(int i = 0; i < strlen(colors_to_keep); i++){
  197. if(colors_to_keep[i] == 'g'){
  198. g = false;
  199. break;
  200. }
  201. }
  202.  
  203. for(int i = 0; i < strlen(colors_to_keep); i++){
  204. if(colors_to_keep[i] == 'b'){
  205. b = false;
  206. break;
  207. }
  208. }
  209.  
  210. for(uint32_t i = 0; i < size; i++){
  211.  
  212. if(r){
  213. new_image->data[i].red = 0;
  214. } else {
  215. new_image->data[i].red = image->data[i].red;
  216. }
  217.  
  218. if(g){
  219. new_image->data[i].green = 0;
  220. } else {
  221. new_image->data[i].green = image->data[i].green;
  222. }
  223.  
  224. if(b){
  225. new_image->data[i].blue = 0;
  226. } else {
  227. new_image->data[i].blue = image->data[i].blue;
  228. }
  229.  
  230. }
  231.  
  232. return new_image;
  233.  
  234. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement