DrawTest.cpp 7.8 KB

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