DrawTest.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. 
  2. #include "../testTools.h"
  3. #include "../../DFPSR/api/imageAPI.h"
  4. #include "../../DFPSR/api/drawAPI.h"
  5. START_TEST(Draw)
  6. // Resources
  7. ImageU8 imageBall = image_fromAscii(
  8. "< .x>"
  9. "< .xx. >"
  10. "<.xxxx.>"
  11. "<xxxxxx>"
  12. "<xxxxxx>"
  13. "<.xxxx.>"
  14. "< .xx. >"
  15. );
  16. { // 8-bit monochrome drawing
  17. int black = 0;
  18. int gray = 127;
  19. int white = 255;
  20. ImageU8 imageA = image_create_U8(16, 16);
  21. // New images begin with all black pixels
  22. ASSERT_EQUAL(image_maxDifference(imageA, image_fromAscii(
  23. "< .x>"
  24. "< >"
  25. "< >"
  26. "< >"
  27. "< >"
  28. "< >"
  29. "< >"
  30. "< >"
  31. "< >"
  32. "< >"
  33. "< >"
  34. "< >"
  35. "< >"
  36. "< >"
  37. "< >"
  38. "< >"
  39. "< >"
  40. )), 0);
  41. // Filling an image sets all pixels to the new color
  42. image_fill(imageA, white);
  43. ASSERT_EQUAL(image_maxDifference(imageA, image_fromAscii(
  44. "< .x>"
  45. "<xxxxxxxxxxxxxxxx>"
  46. "<xxxxxxxxxxxxxxxx>"
  47. "<xxxxxxxxxxxxxxxx>"
  48. "<xxxxxxxxxxxxxxxx>"
  49. "<xxxxxxxxxxxxxxxx>"
  50. "<xxxxxxxxxxxxxxxx>"
  51. "<xxxxxxxxxxxxxxxx>"
  52. "<xxxxxxxxxxxxxxxx>"
  53. "<xxxxxxxxxxxxxxxx>"
  54. "<xxxxxxxxxxxxxxxx>"
  55. "<xxxxxxxxxxxxxxxx>"
  56. "<xxxxxxxxxxxxxxxx>"
  57. "<xxxxxxxxxxxxxxxx>"
  58. "<xxxxxxxxxxxxxxxx>"
  59. "<xxxxxxxxxxxxxxxx>"
  60. "<xxxxxxxxxxxxxxxx>"
  61. )), 0);
  62. // Drawing a gray rectangle near the upper left corner
  63. draw_rectangle(imageA, IRect(1, 1, 6, 6), gray);
  64. ASSERT_EQUAL(image_maxDifference(imageA, image_fromAscii(
  65. "< .x>"
  66. "<xxxxxxxxxxxxxxxx>"
  67. "<x......xxxxxxxxx>"
  68. "<x......xxxxxxxxx>"
  69. "<x......xxxxxxxxx>"
  70. "<x......xxxxxxxxx>"
  71. "<x......xxxxxxxxx>"
  72. "<x......xxxxxxxxx>"
  73. "<xxxxxxxxxxxxxxxx>"
  74. "<xxxxxxxxxxxxxxxx>"
  75. "<xxxxxxxxxxxxxxxx>"
  76. "<xxxxxxxxxxxxxxxx>"
  77. "<xxxxxxxxxxxxxxxx>"
  78. "<xxxxxxxxxxxxxxxx>"
  79. "<xxxxxxxxxxxxxxxx>"
  80. "<xxxxxxxxxxxxxxxx>"
  81. "<xxxxxxxxxxxxxxxx>"
  82. )), 0);
  83. // Drawing a gray rectangle near the lower right corner
  84. draw_rectangle(imageA, IRect(9, 9, 6, 6), gray);
  85. ASSERT_EQUAL(image_maxDifference(imageA, image_fromAscii(
  86. "< .x>"
  87. "<xxxxxxxxxxxxxxxx>"
  88. "<x......xxxxxxxxx>"
  89. "<x......xxxxxxxxx>"
  90. "<x......xxxxxxxxx>"
  91. "<x......xxxxxxxxx>"
  92. "<x......xxxxxxxxx>"
  93. "<x......xxxxxxxxx>"
  94. "<xxxxxxxxxxxxxxxx>"
  95. "<xxxxxxxxxxxxxxxx>"
  96. "<xxxxxxxxx......x>"
  97. "<xxxxxxxxx......x>"
  98. "<xxxxxxxxx......x>"
  99. "<xxxxxxxxx......x>"
  100. "<xxxxxxxxx......x>"
  101. "<xxxxxxxxx......x>"
  102. "<xxxxxxxxxxxxxxxx>"
  103. )), 0);
  104. // Drawing out of bound at the upper right corner, which is safely clipped to only affect pixels within the current image's view
  105. draw_rectangle(imageA, IRect(7, -11, 20, 20), black);
  106. draw_rectangle(imageA, IRect(8, -12, 20, 20), white);
  107. ASSERT_EQUAL(image_maxDifference(imageA, image_fromAscii(
  108. "< .x>"
  109. "<xxxxxxx xxxxxxxx>"
  110. "<x...... xxxxxxxx>"
  111. "<x...... xxxxxxxx>"
  112. "<x...... xxxxxxxx>"
  113. "<x...... xxxxxxxx>"
  114. "<x...... xxxxxxxx>"
  115. "<x...... xxxxxxxx>"
  116. "<xxxxxxx xxxxxxxx>"
  117. "<xxxxxxx >"
  118. "<xxxxxxxxx......x>"
  119. "<xxxxxxxxx......x>"
  120. "<xxxxxxxxx......x>"
  121. "<xxxxxxxxx......x>"
  122. "<xxxxxxxxx......x>"
  123. "<xxxxxxxxx......x>"
  124. "<xxxxxxxxxxxxxxxx>"
  125. )), 0);
  126. // Draw diagonal lines from upper left side to lower right side
  127. draw_line(imageA, 1, 2, 12, 13, 0);
  128. draw_line(imageA, 2, 2, 13, 13, 255);
  129. draw_line(imageA, 3, 2, 14, 13, 0);
  130. ASSERT_EQUAL(image_maxDifference(imageA, image_fromAscii(
  131. "< .x>"
  132. "<xxxxxxx xxxxxxxx>"
  133. "<x...... xxxxxxxx>"
  134. "<x x ... xxxxxxxx>"
  135. "<x. x .. xxxxxxxx>"
  136. "<x.. x . xxxxxxxx>"
  137. "<x... x xxxxxxxx>"
  138. "<x.... x xxxxxxxx>"
  139. "<xxxxxx x xxxxxxx>"
  140. "<xxxxxxx x >"
  141. "<xxxxxxxx x ....x>"
  142. "<xxxxxxxxx x ...x>"
  143. "<xxxxxxxxx. x ..x>"
  144. "<xxxxxxxxx.. x .x>"
  145. "<xxxxxxxxx... x x>"
  146. "<xxxxxxxxx......x>"
  147. "<xxxxxxxxxxxxxxxx>"
  148. )), 0);
  149. draw_copy(imageA, imageBall, 4, 2);
  150. ASSERT_EQUAL(image_maxDifference(imageA, image_fromAscii(
  151. "< .x>"
  152. "<xxxxxxx xxxxxxxx>"
  153. "<x...... xxxxxxxx>"
  154. "<x x .xx. xxxxxx>"
  155. "<x. x.xxxx.xxxxxx>"
  156. "<x.. xxxxxxxxxxxx>"
  157. "<x...xxxxxxxxxxxx>"
  158. "<x....xxxx.xxxxxx>"
  159. "<xxxx .xx. xxxxxx>"
  160. "<xxxxxxx x >"
  161. "<xxxxxxxx x ....x>"
  162. "<xxxxxxxxx x ...x>"
  163. "<xxxxxxxxx. x ..x>"
  164. "<xxxxxxxxx.. x .x>"
  165. "<xxxxxxxxx... x x>"
  166. "<xxxxxxxxx......x>"
  167. "<xxxxxxxxxxxxxxxx>"
  168. )), 0);
  169. }
  170. { // RGBA silhouette drawing (Giving color to grayscale images by treating silhouette luma as source opacity and the uniform color as source RGB)
  171. ImageRgbaU8 imageA = image_create_RgbaU8(8, 8);
  172. draw_rectangle(imageA, IRect(4, 0, 4, 8), ColorRgbaI32(255, 255, 255, 255));
  173. //printText("imageA R:\n", image_toAscii(image_get_red(imageA), " ,.-x"), "\n");
  174. //printText("imageA G:\n", image_toAscii(image_get_green(imageA), " ,.-x"), "\n");
  175. //printText("imageA B:\n", image_toAscii(image_get_blue(imageA), " ,.-x"), "\n");
  176. ASSERT_LESSER_OR_EQUAL(image_maxDifference(image_get_red(imageA), image_fromAscii(
  177. "< ,.-x>"
  178. "< xxxx>"
  179. "< xxxx>"
  180. "< xxxx>"
  181. "< xxxx>"
  182. "< xxxx>"
  183. "< xxxx>"
  184. "< xxxx>"
  185. "< xxxx>"
  186. )), 0);
  187. ASSERT_LESSER_OR_EQUAL(image_maxDifference(image_get_green(imageA), image_fromAscii(
  188. "< ,.-x>"
  189. "< xxxx>"
  190. "< xxxx>"
  191. "< xxxx>"
  192. "< xxxx>"
  193. "< xxxx>"
  194. "< xxxx>"
  195. "< xxxx>"
  196. "< xxxx>"
  197. )), 0);
  198. ASSERT_LESSER_OR_EQUAL(image_maxDifference(image_get_blue(imageA), image_fromAscii(
  199. "< ,.-x>"
  200. "< xxxx>"
  201. "< xxxx>"
  202. "< xxxx>"
  203. "< xxxx>"
  204. "< xxxx>"
  205. "< xxxx>"
  206. "< xxxx>"
  207. "< xxxx>"
  208. )), 0);
  209. // Draw a fully opaque orange ball
  210. draw_silhouette(imageA, imageBall, ColorRgbaI32(255, 127, 0, 255), 1, 1);
  211. //printText("imageA R:\n", image_toAscii(image_get_red(imageA), " ,.-x"), "\n");
  212. //printText("imageA G:\n", image_toAscii(image_get_green(imageA), " ,.-x"), "\n");
  213. //printText("imageA B:\n", image_toAscii(image_get_blue(imageA), " ,.-x"), "\n");
  214. ASSERT_LESSER_OR_EQUAL(image_maxDifference(image_get_red(imageA), image_fromAscii(
  215. "< ,.-x>"
  216. "< xxxx>"
  217. "< .xxxxx>"
  218. "< .xxxxxx>"
  219. "< xxxxxxx>"
  220. "< xxxxxxx>"
  221. "< .xxxxxx>"
  222. "< .xxxxx>"
  223. "< xxxx>"
  224. )), 1);
  225. ASSERT_LESSER_OR_EQUAL(image_maxDifference(image_get_green(imageA), image_fromAscii(
  226. "< ,.-x>"
  227. "< xxxx>"
  228. "< ,..-xx>"
  229. "< ,....-x>"
  230. "< ......x>"
  231. "< ......x>"
  232. "< ,....-x>"
  233. "< ,..-xx>"
  234. "< xxxx>"
  235. )), 1);
  236. ASSERT_LESSER_OR_EQUAL(image_maxDifference(image_get_blue(imageA), image_fromAscii(
  237. "< ,.-x>"
  238. "< xxxx>"
  239. "< .xx>"
  240. "< .x>"
  241. "< x>"
  242. "< x>"
  243. "< .x>"
  244. "< .xx>"
  245. "< xxxx>"
  246. )), 1);
  247. // Draw a half opaque blue ball in the lower right corner
  248. draw_silhouette(imageA, imageBall, ColorRgbaI32(0, 0, 255, 127), 3, 3);
  249. //printText("imageA R:\n", image_toAscii(image_get_red(imageA)), "\n");
  250. //printText("imageA G:\n", image_toAscii(image_get_green(imageA)), "\n");
  251. //printText("imageA B:\n", image_toAscii(image_get_blue(imageA)), "\n");
  252. ASSERT_LESSER_OR_EQUAL(image_maxDifference(image_get_red(imageA), image_fromAscii(
  253. "< .,-_':;!+~=^?*abcdefghijklmnopqrstuvwxyz()[]{}|&@#0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ>"
  254. "< ZZZZ>"
  255. "< [ZZZZZ>"
  256. "< [ZZZZZZ>"
  257. "< ZZZE[[E>"
  258. "< ZZE[[[[>"
  259. "< [Z[[[[[>"
  260. "< [[[[[[>"
  261. "< [[[[>"
  262. )), 2);
  263. ASSERT_LESSER_OR_EQUAL(image_maxDifference(image_get_green(imageA), image_fromAscii(
  264. "< .,-_':;!+~=^?*abcdefghijklmnopqrstuvwxyz()[]{}|&@#0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ>"
  265. "< ZZZZ>"
  266. "< g[[DZZ>"
  267. "< g[[[[DZ>"
  268. "< [[[rhhE>"
  269. "< [[rhhh[>"
  270. "< g[hhhr[>"
  271. "< ghhr[[>"
  272. "< [[[[>"
  273. )), 2);
  274. ASSERT_LESSER_OR_EQUAL(image_maxDifference(image_get_blue(imageA), image_fromAscii(
  275. "< .,-_':;!+~=^?*abcdefghijklmnopqrstuvwxyz()[]{}|&@#0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ>"
  276. "< ZZZZ>"
  277. "< [ZZ>"
  278. "< [Z>"
  279. "< g[[Z>"
  280. "< g[[[Z>"
  281. "< [[[DZ>"
  282. "< [[DZZ>"
  283. "< gZZZZ>"
  284. )), 2);
  285. }
  286. END_TEST