random-clips.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. * Copyright © 2006 M Joonas Pihlaja
  3. * Copyright © 2011 Chris Wilson
  4. *
  5. * Permission is hereby granted, free of charge, to any person
  6. * obtaining a copy of this software and associated documentation
  7. * files (the "Software"), to deal in the Software without
  8. * restriction, including without limitation the rights to use, copy,
  9. * modify, merge, publish, distribute, sublicense, and/or sell copies
  10. * of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be
  14. * included in all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  17. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  18. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  19. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  20. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  21. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  22. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23. * SOFTWARE.
  24. *
  25. * Authors:
  26. * M Joonas Pihlaja <jpihlaja@cc.helsinki.fi>
  27. * Chris Wilson <chris@chris-wilson.co.uk>
  28. */
  29. #include "cairo-test.h"
  30. #define SIZE 512
  31. #define STEP (512+2)
  32. #define NUM_SEGMENTS 128
  33. static uint32_t state;
  34. static double
  35. uniform_random (double minval, double maxval)
  36. {
  37. static uint32_t const poly = 0x9a795537U;
  38. uint32_t n = 32;
  39. while (n-->0)
  40. state = 2*state < state ? (2*state ^ poly) : 2*state;
  41. return minval + state * (maxval - minval) / 4294967296.0;
  42. }
  43. static void nz_path (cairo_t *cr)
  44. {
  45. int i;
  46. state = 0xc0ffee;
  47. cairo_move_to (cr, 0, 0);
  48. for (i = 0; i < NUM_SEGMENTS; i++) {
  49. double x = uniform_random (0, SIZE);
  50. double y = uniform_random (0, SIZE);
  51. cairo_line_to (cr, x, y);
  52. }
  53. cairo_close_path (cr);
  54. }
  55. static void region_path (cairo_t *cr)
  56. {
  57. int i;
  58. state = 0xc0ffee;
  59. for (i = 0; i < NUM_SEGMENTS; i++) {
  60. int x = uniform_random (0, SIZE);
  61. int y = uniform_random (0, SIZE);
  62. int w = uniform_random (0, 40);
  63. int h = uniform_random (0, 40);
  64. cairo_rectangle (cr, x, y, w, h);
  65. }
  66. }
  67. static void rectangle_path (cairo_t *cr)
  68. {
  69. int i;
  70. state = 0xc0ffee;
  71. for (i = 0; i < NUM_SEGMENTS; i++) {
  72. double x = uniform_random (0, SIZE);
  73. double y = uniform_random (0, SIZE);
  74. double w = uniform_random (0, 40);
  75. double h = uniform_random (0, 40);
  76. cairo_rectangle (cr, x, y, w, h);
  77. }
  78. }
  79. static void arc_path (cairo_t *cr)
  80. {
  81. int i;
  82. state = 0xc0ffee;
  83. for (i = 0; i < NUM_SEGMENTS; i++) {
  84. double x = uniform_random (0, SIZE);
  85. double y = uniform_random (0, SIZE);
  86. double r = uniform_random (0, 20);
  87. cairo_new_sub_path (cr);
  88. cairo_arc (cr, x, y, r, 0, 2*M_PI);
  89. }
  90. }
  91. static void nz_fill_stroke (cairo_t *cr)
  92. {
  93. nz_path (cr);
  94. cairo_set_source_rgb (cr, 1, 0, 0);
  95. cairo_fill_preserve (cr);
  96. cairo_set_source_rgb (cr, 0, 1, 0);
  97. cairo_set_line_width (cr, 1.0);
  98. cairo_stroke (cr);
  99. }
  100. static void clip_to_quadrant (cairo_t *cr)
  101. {
  102. cairo_rectangle (cr, 0, 0, SIZE, SIZE);
  103. cairo_clip (cr);
  104. }
  105. static cairo_test_status_t
  106. draw (cairo_t *cr, int width, int height)
  107. {
  108. cairo_set_source_rgb (cr, 0, 0, 0);
  109. cairo_paint (cr);
  110. cairo_set_fill_rule (cr, CAIRO_FILL_RULE_WINDING);
  111. state = 0xc0ffee;
  112. cairo_translate (cr, 1, 1);
  113. /* no clipping */
  114. cairo_save (cr); {
  115. clip_to_quadrant (cr);
  116. nz_fill_stroke (cr);
  117. } cairo_restore (cr);
  118. cairo_translate (cr, STEP, 0);
  119. /* random clipping */
  120. cairo_save (cr); {
  121. clip_to_quadrant (cr);
  122. nz_path (cr);
  123. cairo_clip (cr);
  124. nz_fill_stroke (cr);
  125. cairo_set_source_rgba (cr, 1, 1, 1, 0.5);
  126. cairo_paint (cr);
  127. } cairo_restore (cr);
  128. cairo_translate (cr, STEP, 0);
  129. /* regional clipping */
  130. cairo_save (cr); {
  131. clip_to_quadrant (cr);
  132. region_path (cr);
  133. cairo_clip (cr);
  134. nz_fill_stroke (cr);
  135. cairo_set_source_rgba (cr, 1, 1, 1, 0.5);
  136. cairo_paint (cr);
  137. } cairo_restore (cr);
  138. cairo_translate (cr, -2*STEP, STEP);
  139. /* rectangular clipping */
  140. cairo_save (cr); {
  141. clip_to_quadrant (cr);
  142. rectangle_path (cr);
  143. cairo_clip (cr);
  144. nz_fill_stroke (cr);
  145. cairo_set_source_rgba (cr, 1, 1, 1, 0.5);
  146. cairo_paint (cr);
  147. } cairo_restore (cr);
  148. cairo_translate (cr, STEP, 0);
  149. /* circular clipping */
  150. cairo_save (cr); {
  151. clip_to_quadrant (cr);
  152. arc_path (cr);
  153. cairo_clip (cr);
  154. nz_fill_stroke (cr);
  155. cairo_set_source_rgba (cr, 1, 1, 1, 0.5);
  156. cairo_paint (cr);
  157. } cairo_restore (cr);
  158. cairo_translate (cr, STEP, 0);
  159. /* all-of-the-above clipping */
  160. cairo_save (cr); {
  161. clip_to_quadrant (cr);
  162. nz_path (cr);
  163. cairo_clip (cr);
  164. region_path (cr);
  165. cairo_clip (cr);
  166. rectangle_path (cr);
  167. cairo_clip (cr);
  168. arc_path (cr);
  169. cairo_clip (cr);
  170. nz_fill_stroke (cr);
  171. cairo_set_source_rgba (cr, 1, 1, 1, 0.5);
  172. cairo_paint (cr);
  173. } cairo_restore (cr);
  174. return CAIRO_TEST_SUCCESS;
  175. }
  176. CAIRO_TEST (random_clip,
  177. "Tests the clip generation and intersection computation",
  178. "trap, clip", /* keywords */
  179. NULL, /* requirements */
  180. 3*STEP+2, 2*STEP+2,
  181. NULL, draw)