Advertisement
Guest User

uarray2b.c

a guest
Oct 20th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.03 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdbool.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include "assert.h"
  6. #include "array.h"
  7. #include "uarray2b.h"
  8. #include "mem.h"
  9. #include "uarray2.h"
  10. #include "pnm.h"
  11.  
  12. #define T Array2b_T
  13.  
  14. struct T {
  15. int width;
  16. int height;
  17. int size;
  18. int blocksize;
  19. UArray2_T uarray2;
  20. };
  21.  
  22. T UArray2b_new(int width, int height, int size, int blocksize)
  23. {
  24. T array2b;
  25. NEW(array2b);
  26.  
  27. array2b->width = width;
  28. array2b->height = height;
  29. array2b->size = size;
  30. array2b->blocksize = blocksize;
  31. int width_overflow = (width % blocksize) ? 1 : 0;
  32. int height_overflow = (height % blocksize) ? 1 : 0;
  33.  
  34. // printf("\nWidth overflow: %d Height overflow: %d\n", width_overflow, height_overflow);
  35. // Defining array2b to be a UArray2 with elements of size Array_T
  36. array2b->uarray2 = UArray2_new(width/blocksize + width_overflow, height/blocksize + height_overflow, sizeof(Array_T));
  37.  
  38.  
  39. // printf("\nShape of uarray2: (%d, %d)\n", width/blocksize + width_overflow, height/blocksize + height_overflow);
  40.  
  41. // loop while i < the number of Array_Ts in uarray2
  42. for(int i = 0; i < UArray2_length(array2b->uarray2); i++)
  43. {
  44. //each inner array (block) is of size blocksize*blocksize
  45. Array_T inner_array = Array_new(blocksize*blocksize, size);
  46.  
  47. //define each cell in the inner array to be a pointer to an int
  48. for(int j = 0; j < Array_length(inner_array); j++)
  49. {
  50. void *elemp = Array_get(inner_array, j);
  51. Pnm_rgb rgb = elemp;
  52. rgb->red = 0;
  53. rgb->blue = 0;
  54. rgb->green = 0;
  55. //initialize element to be index within blocked array
  56. // *elemp = j + (i*(blocksize*blocksize));
  57. }
  58. Array_T *innerp = UArray2_at_flat(array2b->uarray2, i);
  59. *innerp = inner_array;
  60. }
  61.  
  62. return array2b;
  63. }
  64.  
  65. T UArray2b_new_64K_block(int width, int height, int size)
  66. {
  67. T array2b;
  68. NEW(array2b);
  69.  
  70. // array2b->width = 1368;
  71. array2b->width = width;
  72. // array2b->height = 936;
  73. array2b->height = height;
  74. array2b->size = size;
  75. printf("\nImage Height: %d width: %d size: %d", height, width, size);
  76.  
  77. //Exactly 65,536 bits in 64KB. 65,536/sizeof(int) = 16384. sqrt(16384) = 128. blocksize = 128
  78. // Exactly 65,536 bits in 64KB. 65,536/3*sizeof(int) = 5461. sqrt(5461) = 72. blocksize = 72
  79. array2b->blocksize = 72;
  80. // array2b->blocksize = 128;
  81. int blocksize = array2b->blocksize;
  82. int width_overflow = (width % blocksize) ? 1 : 0;
  83. int height_overflow = (height % blocksize) ? 1 : 0;
  84.  
  85. // printf("\nWidth overflow: %d Height overflow: %d\n", width_overflow, height_overflow);
  86. // Defining array2b to be a UArray2 with elements of size Array_T
  87. // array2b->uarray2 = UArray2_new(((height/blocksize) + height_overflow), ((width/blocksize) + width_overflow), sizeof(Array_T));
  88. array2b->uarray2 = UArray2_new(((width/blocksize) + width_overflow), ((height/blocksize) + height_overflow), sizeof(Array_T));
  89.  
  90.  
  91. printf("\n2D Blocks height: %d Width: %d length: %d\n", UArray2_rows(array2b->uarray2), UArray2_cols(array2b->uarray2), UArray2_length(array2b->uarray2));
  92.  
  93. // loop while i < the number of Array_Ts in uarray2
  94. for(int i = 0; i < UArray2_length(array2b->uarray2); i++)
  95. {
  96. //each inner array (block) is of size blocksize*blocksize
  97. Array_T inner_array = Array_new(blocksize*blocksize, size);
  98.  
  99. //define each cell in the inner array to be a pointer to an int
  100. for(int j = 0; j < Array_length(inner_array); j++)
  101. {
  102. void *elemp = Array_get(inner_array, j);
  103. Pnm_rgb rgb = elemp;
  104. rgb->red = 0;
  105. rgb->blue = 0;
  106. rgb->green = 0;
  107. // int *elemp = Array_get(inner_array, j);
  108. //initialize element to be index within blocked array
  109. // *elemp = 0;
  110. // *elemp = j + (i*(blocksize*blocksize));
  111. // printf("\ni: %d j: %d\n", i, j);
  112. }
  113. // printf("\ni: %d\n", i);
  114. Array_T *innerp = UArray2_at_flat(array2b->uarray2, i);
  115. *innerp = inner_array;
  116. }
  117.  
  118. // UArray2b_print_block(array2b, 0,0);
  119. // UArray2b_print(array2b);
  120. printf("\nreached\n");
  121.  
  122. return array2b;
  123. }
  124.  
  125. void UArray2b_free(T *array2b)
  126. {
  127. FREE(*array2b);
  128. }
  129.  
  130. void *UArray2b_at(T array2b, int i, int j)
  131. {
  132. //height = 1001
  133. //width = 1419
  134. // int height_overflow = UArray2b_height_overflow(array2b);
  135. // int width_overflow = UArray2b_width_overflow(array2b);
  136.  
  137. //check to see if we are working with the final row of i, the overflow
  138. //if that is the case subtract the height overflow
  139. // printf("\nheight overflow: %d width overflow: %d", height_overflow, width_overflow);
  140.  
  141.  
  142. // int width_overflow = UArray2b_width_overflow(array2b);
  143. int block_index_i = (i / array2b->blocksize);
  144. int block_index_j = (j / array2b->blocksize);
  145.  
  146. // if(block_index_i == UArray2_rows(array2b->uarray2))
  147. // {
  148. // // printf("Reached");
  149. // i -= height_overflow;
  150. // }
  151.  
  152. // printf("\ni: %d j:%d", i, j);
  153. // printf("\nBlock found at: (%d, %d).\n", block_index_i, block_index_j);
  154. // printf("\ni: %d j:%d", i, j);
  155.  
  156.  
  157. int element_index = array2b->blocksize * (j % array2b->blocksize) + (i % array2b->blocksize);
  158.  
  159.  
  160. // printf("\nCell within block found at index: %d", element_index);
  161. // Array_T block = UArray2_get_block(array2b->uarray2, 0, 0);
  162. Array_T block = UArray2_get_block(array2b->uarray2, block_index_j, block_index_i);
  163. // Array_T block = UArray2_get_block(array2b->uarray2, block_index_i, block_index_j);
  164. // Array_T block = UArray2_get_block(array2b->uarray2, block_index_i, block_index_j);
  165.  
  166. // printf("\ni: %d j:%d element_index %d", i, j, element_index);
  167.  
  168. // void *elemp = Array_get(block, 0);
  169. void *elemp = Array_get(block, element_index);
  170.  
  171. // Pnm_rgb rgb = Array_get(block, element_index);
  172.  
  173.  
  174. // printf("\n%d %d %d", rgb->red, rgb->green, rgb->blue);
  175.  
  176. return elemp;
  177.  
  178. }
  179.  
  180. void UArray2b_print(T array2b)
  181. {
  182. printf("\n");
  183. for(int i = 0; i < array2b->height; i++)
  184. {
  185. //Print the column indices
  186. if(i == 0)
  187. {
  188. for(int k = 0; k < array2b->width; k++)
  189. {
  190. if(k == 0) printf(" ");
  191. if(k % array2b->blocksize == 0 && k != 0) printf(" %d ", k);
  192. else printf("%d ", k);
  193. }
  194. }
  195. //separate blocks by a new lines
  196. if (i % array2b->blocksize == 0)
  197. {
  198. printf("\n\n%d| ", i);
  199. }
  200. //print the current row index
  201. if (i != 0 && i % array2b->blocksize != 0)
  202. {
  203. printf("\n%d| ", i);
  204. }
  205. //print each of the blocks and their cells
  206. for(int j = 0 ; j < array2b->width; j++)
  207. {
  208. //get the current cell
  209. void *cell = UArray2b_at(array2b, i, j);
  210.  
  211. //print whitespace to seperate blocks
  212. if(j % array2b->blocksize == 0 && j > 0)
  213. {
  214. printf(" %d ", *(int*)cell);
  215. }
  216. else
  217. {
  218. //account for single digit cell index spacing
  219. if(*(int*)cell < 10) printf(" %d ", *(int*)cell);
  220. else if(*(int*)cell < 100) printf(" %d ", *(int*)cell);
  221. else printf(" %d ", *(int*)cell);
  222. }
  223. }
  224. }
  225. printf("\n");
  226. }
  227.  
  228. void UArray2b_print_block(T array2b, int i, int j)
  229. {
  230. // int initial_index;
  231. printf("\nDisplaying block found at (%d, %d):\n\n", i, j);
  232.  
  233. Array_T block = UArray2_get_block(array2b->uarray2, i, j);
  234.  
  235. for(int i = 0; i < Array_length(block); i++)
  236. {
  237. void *cell = Array_get(block, i);
  238. if(i % array2b->blocksize == 0 && i > 0)
  239. {
  240. printf("\n%d ", *(int*)cell);
  241. }
  242. else
  243. {
  244. printf("%d ", *(int*)cell);
  245. }
  246. }
  247. printf("\n");
  248.  
  249. }
  250.  
  251. void UArray2b_modify_block(T array2b, int i, int j, int val)
  252. {
  253. printf("\nChanging all values of the block found at (%d, %d) to %d:\n", i, j, val);
  254. Array_T block = UArray2_get_block(array2b->uarray2, i, j);
  255.  
  256. for(int i = 0; i < Array_length(block); i++)
  257. {
  258. void *cell = Array_get(block, i);
  259. *(int*)cell = val;
  260. }
  261. }
  262.  
  263. int UArray2b_width(T array2b)
  264. {
  265. return array2b->width;
  266. }
  267.  
  268. int UArray2b_height(T array2b)
  269. {
  270. return array2b->height;
  271. }
  272. int UArray2b_size (T array2b)
  273. {
  274. return array2b->size;
  275. }
  276. int UArray2b_blocksize(T array2b)
  277. {
  278. return array2b->blocksize;
  279. }
  280. int UArray2b_length(T array2b)
  281. {
  282. int width_overflow = (array2b->width % array2b->blocksize) ? 1 : 0;
  283. int height_overflow = (array2b->height % array2b->blocksize) ? 1 : 0;
  284.  
  285. return (array2b->width/array2b->blocksize + width_overflow) * (array2b->height/array2b->blocksize + height_overflow);
  286. }
  287.  
  288. int UArray2b_width_overflow(T array2b)
  289. {
  290. return array2b->width % array2b->blocksize;
  291. }
  292.  
  293. int UArray2b_height_overflow(T array2b)
  294. {
  295. return array2b->height % array2b->blocksize;
  296. }
  297.  
  298.  
  299. void UArray2b_map(T array2b, void (*apply)(int, int, T, void *, void *), void *cl)
  300. {
  301. // printf("\nReached mapping\n");
  302. //defining closure to be the Array2b_T being mapped over
  303. Array2b_T *original = cl;
  304.  
  305. int height_overflow = UArray2b_height_overflow(array2b);
  306. int width_overflow = UArray2b_width_overflow(array2b);
  307.  
  308. // printf("\nWidth overflow: %d Height overflow: %d\n", width_overflow, height_overflow);
  309. //initialization of variables used in mapping
  310. int row_num = 0;
  311. int blocksize = array2b->blocksize;
  312. int uarray2_rows = UArray2_rows(array2b->uarray2);
  313. int uarray2_cols = UArray2_cols(array2b->uarray2);
  314.  
  315. //Loop through each of the blocks
  316. for(int k = 0; k < UArray2b_length(*original); k++)
  317. {
  318. // printf("\nk = %d\n", k);
  319.  
  320. //variables storing intial row,col of block
  321. int initial_i, initial_j;
  322.  
  323. //variables storing final row, col of block
  324. int final_i, final_j;
  325.  
  326.  
  327.  
  328. //i is always 0 while we are mapping over the first row of blocks
  329. if(k < uarray2_rows) initial_i = 0;
  330. else
  331. {
  332. //if there is no remainder, we are in first column
  333. if(!(k % uarray2_rows)) row_num += 1;
  334. //initial row index of blocks is found at row_num*blocksize
  335. initial_i = row_num*blocksize;
  336. }
  337.  
  338. //if there is no remainder we are mapping over first column of blocks
  339. if(k % uarray2_cols == 0) initial_j = 0;
  340. //if there is a remainder, intial col index found at remainder*blocksize
  341. else initial_j = (k % uarray2_cols)*blocksize;
  342.  
  343. //if we are working within the last row of blocks, final_i = number of rows in final block = height_overflow
  344. if(height_overflow && (initial_i + height_overflow == array2b->height)) final_i = height_overflow;
  345. //if there is no height overflow, or there is but we are not yet in the final row of blocks, final_i = blocksize
  346. else final_i = blocksize;
  347.  
  348. //if we are working within the last col of blocks, final_j = number of cols in final block = width_overflow
  349. if(width_overflow && ((k + 1) % uarray2_cols == 0)) final_j = width_overflow;
  350. //if there is no width overflow, or there is but we are not yet in the final column of blocks, final_j = blocksize
  351. else final_j = blocksize;
  352.  
  353. //loop through each row of block
  354. for(int i = initial_i; i < initial_i+final_i; i++)
  355. {
  356. //loop through each col of block
  357. for(int j = initial_j; j < initial_j+final_j; j++)
  358. {
  359. //retrieve cell from Array2b_T we are modifying and call apply
  360. void *cell = UArray2b_at(*original, i, j);
  361. (*apply)(i, j, array2b, cell, cl);
  362. }
  363. }
  364. }
  365. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement