raster-source.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * Copyright © 2011 Intel Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person
  5. * obtaining a copy of this software and associated documentation
  6. * files (the "Software"), to deal in the Software without
  7. * restriction, including without limitation the rights to use, copy,
  8. * modify, merge, publish, distribute, sublicense, and/or sell copies
  9. * of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be
  13. * included in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  19. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  20. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. * SOFTWARE.
  23. *
  24. * Author: Chris Wilson <chris@chris-wilson.co.uk>
  25. */
  26. #include "cairo-test.h"
  27. #include <stdio.h>
  28. #include <errno.h>
  29. /* Basic test to exercise the new mime-surface callback. */
  30. #define WIDTH 200
  31. #define HEIGHT 80
  32. /* Lazy way of determining PNG dimensions... */
  33. static void
  34. png_dimensions (const char *filename,
  35. cairo_content_t *content, int *width, int *height)
  36. {
  37. cairo_surface_t *surface;
  38. surface = cairo_image_surface_create_from_png (filename);
  39. *content = cairo_surface_get_content (surface);
  40. *width = cairo_image_surface_get_width (surface);
  41. *height = cairo_image_surface_get_height (surface);
  42. cairo_surface_destroy (surface);
  43. }
  44. static cairo_surface_t *
  45. png_acquire (cairo_pattern_t *pattern, void *closure,
  46. cairo_surface_t *target,
  47. const cairo_rectangle_int_t *extents)
  48. {
  49. return cairo_image_surface_create_from_png (closure);
  50. }
  51. static cairo_surface_t *
  52. red_acquire (cairo_pattern_t *pattern, void *closure,
  53. cairo_surface_t *target,
  54. const cairo_rectangle_int_t *extents)
  55. {
  56. cairo_surface_t *image;
  57. cairo_t *cr;
  58. image = cairo_surface_create_similar_image (target,
  59. CAIRO_FORMAT_RGB24,
  60. extents->width,
  61. extents->height);
  62. cairo_surface_set_device_offset (image, extents->x, extents->y);
  63. cr = cairo_create (image);
  64. cairo_set_source_rgb (cr, 1, 0, 0);
  65. cairo_paint (cr);
  66. cairo_destroy (cr);
  67. return image;
  68. }
  69. static void
  70. release (cairo_pattern_t *pattern, void *closure, cairo_surface_t *image)
  71. {
  72. cairo_surface_destroy (image);
  73. }
  74. static cairo_test_status_t
  75. draw (cairo_t *cr, int width, int height)
  76. {
  77. const char *png_filename = "png.png";
  78. cairo_pattern_t *png, *red;
  79. cairo_content_t content;
  80. int png_width, png_height;
  81. int i, j;
  82. png_dimensions (png_filename, &content, &png_width, &png_height);
  83. png = cairo_pattern_create_raster_source ((void*)png_filename,
  84. content, png_width, png_height);
  85. cairo_raster_source_pattern_set_acquire (png, png_acquire, release);
  86. red = cairo_pattern_create_raster_source (NULL,
  87. CAIRO_CONTENT_COLOR, WIDTH, HEIGHT);
  88. cairo_raster_source_pattern_set_acquire (red, red_acquire, release);
  89. cairo_set_source_rgb (cr, 0, 0, 1);
  90. cairo_paint (cr);
  91. cairo_translate (cr, 0, (HEIGHT-png_height)/2);
  92. for (i = 0; i < 4; i++) {
  93. for (j = 0; j < 4; j++) {
  94. cairo_pattern_t *source;
  95. if ((i ^ j) & 1)
  96. source = red;
  97. else
  98. source = png;
  99. cairo_set_source (cr, source);
  100. cairo_rectangle (cr, i * WIDTH/4, j * png_height/4, WIDTH/4, png_height/4);
  101. cairo_fill (cr);
  102. }
  103. }
  104. cairo_pattern_destroy (red);
  105. cairo_pattern_destroy (png);
  106. return CAIRO_TEST_SUCCESS;
  107. }
  108. CAIRO_TEST (raster_source,
  109. "Check that the mime-surface embedding works",
  110. "api", /* keywords */
  111. NULL, /* requirements */
  112. WIDTH, HEIGHT,
  113. NULL, draw)