scale-offset-image.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * Copyright 2008 Chris Wilson
  3. *
  4. * Permission to use, copy, modify, distribute, and sell this software
  5. * and its documentation for any purpose is hereby granted without
  6. * fee, provided that the above copyright notice appear in all copies
  7. * and that both that copyright notice and this permission notice
  8. * appear in supporting documentation, and that the name of
  9. * Chris Wilson not be used in advertising or publicity pertaining to
  10. * distribution of the software without specific, written prior
  11. * permission. Chris Wilson makes no representations about the
  12. * suitability of this software for any purpose. It is provided "as
  13. * is" without express or implied warranty.
  14. *
  15. * CHRIS WILSON DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
  16. * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  17. * FITNESS, IN NO EVENT SHALL CHRIS WILSON BE LIABLE FOR ANY SPECIAL,
  18. * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
  19. * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  20. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
  21. * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  22. *
  23. * Author: Chris Wilson <chris@chris-wilson.co.uk>
  24. */
  25. /*
  26. * Test case derived from the bug report by Michel Iwaniec:
  27. * http://lists.cairographics.org/archives/cairo/2008-November/015660.html
  28. */
  29. #include "cairo-test.h"
  30. static cairo_surface_t *
  31. create_source (cairo_surface_t *target, int width, int height)
  32. {
  33. cairo_surface_t *similar;
  34. cairo_t *cr;
  35. similar = cairo_image_surface_create (CAIRO_FORMAT_RGB24,
  36. width, height);
  37. cr = cairo_create (similar);
  38. cairo_surface_destroy (similar);
  39. cairo_set_source_rgb (cr, 1, 1, 1);
  40. cairo_rectangle (cr,
  41. width - 4, height - 4,
  42. 2, 2);
  43. cairo_fill (cr);
  44. cairo_set_source_rgb (cr, 1, 0, 0);
  45. cairo_rectangle (cr,
  46. width - 2, height - 4,
  47. 2, 2);
  48. cairo_fill (cr);
  49. cairo_set_source_rgb (cr, 0, 1, 0);
  50. cairo_rectangle (cr,
  51. width - 4, height - 2,
  52. 2, 2);
  53. cairo_fill (cr);
  54. cairo_set_source_rgb (cr, 0, 0, 1);
  55. cairo_rectangle (cr,
  56. width - 2, height - 2,
  57. 2, 2);
  58. cairo_fill (cr);
  59. similar = cairo_surface_reference (cairo_get_target (cr));
  60. cairo_destroy (cr);
  61. return similar;
  62. }
  63. static void
  64. draw_grid (cairo_t *cr, cairo_pattern_t *pattern, int dst_x, int dst_y)
  65. {
  66. cairo_matrix_t m;
  67. cairo_save (cr);
  68. cairo_translate (cr, dst_x, dst_y);
  69. cairo_scale (cr, 16, 16);
  70. cairo_rotate (cr, 1);
  71. cairo_matrix_init_translate (&m, 2560-4, 1280-4);
  72. cairo_pattern_set_matrix (pattern, &m);
  73. cairo_set_source (cr, pattern);
  74. cairo_rectangle (cr, 0, 0, 4, 4);
  75. cairo_fill (cr);
  76. cairo_set_source_rgb (cr, .7, .7, .7);
  77. cairo_set_line_width (cr, 1./16);
  78. cairo_move_to (cr, 0, 0);
  79. cairo_line_to (cr, 4, 0);
  80. cairo_move_to (cr, 0, 2);
  81. cairo_line_to (cr, 4, 2);
  82. cairo_move_to (cr, 0, 4);
  83. cairo_line_to (cr, 4, 4);
  84. cairo_move_to (cr, 0, 0);
  85. cairo_line_to (cr, 0, 4);
  86. cairo_move_to (cr, 2, 0);
  87. cairo_line_to (cr, 2, 4);
  88. cairo_move_to (cr, 4, 0);
  89. cairo_line_to (cr, 4, 4);
  90. cairo_stroke (cr);
  91. cairo_restore (cr);
  92. }
  93. static cairo_test_status_t
  94. draw (cairo_t *cr, int width, int height)
  95. {
  96. cairo_surface_t *source;
  97. cairo_pattern_t *pattern;
  98. cairo_paint (cr);
  99. source = create_source (cairo_get_target (cr), 2560, 1280);
  100. pattern = cairo_pattern_create_for_surface (source);
  101. cairo_surface_destroy (source);
  102. cairo_pattern_set_filter (pattern, CAIRO_FILTER_NEAREST);
  103. cairo_pattern_set_extend (pattern, CAIRO_EXTEND_NONE);
  104. draw_grid (cr, pattern, 50, 0);
  105. draw_grid (cr, pattern, 130, 0);
  106. draw_grid (cr, pattern, 210, 0);
  107. draw_grid (cr, pattern, 290, 0);
  108. draw_grid (cr, pattern, 50, 230);
  109. draw_grid (cr, pattern, 130, 230);
  110. draw_grid (cr, pattern, 210, 230);
  111. draw_grid (cr, pattern, 290, 230);
  112. cairo_pattern_destroy (pattern);
  113. return CAIRO_TEST_SUCCESS;
  114. }
  115. /* XFAIL: loss of precision converting a cairo matrix to */
  116. CAIRO_TEST (scale_offset_image,
  117. "Tests drawing surfaces under various scales and transforms",
  118. "surface, scale-offset", /* keywords */
  119. NULL, /* requirements */
  120. 320, 320,
  121. NULL, draw)