An LGPL/GPL-licensed artwork library
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

art_rect_uta.c 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /* Libart_LGPL - library of basic graphic primitives
  2. * Copyright (C) 1998 Raph Levien
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Library General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Library General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Library General Public
  15. * License along with this library; if not, write to the
  16. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  17. * Boston, MA 02111-1307, USA.
  18. */
  19. #include "config.h"
  20. #include "art_rect_uta.h"
  21. /* Functions to decompose a microtile array into a list of rectangles. */
  22. /**
  23. * art_rect_list_from_uta: Decompose uta into list of rectangles.
  24. * @uta: The source uta.
  25. * @max_width: The maximum width of the resulting rectangles.
  26. * @max_height: The maximum height of the resulting rectangles.
  27. * @p_nrects: Where to store the number of returned rectangles.
  28. *
  29. * Allocates a new list of rectangles, sets *@p_nrects to the number
  30. * in the list. This list should be freed with art_free().
  31. *
  32. * Each rectangle bounded in size by (@max_width, @max_height).
  33. * However, these bounds must be at least the size of one tile.
  34. *
  35. * This routine provides a precise implementation, i.e. the rectangles
  36. * cover exactly the same area as the uta. It is thus appropriate in
  37. * cases where the overhead per rectangle is small compared with the
  38. * cost of filling in extra pixels.
  39. *
  40. * Return value: An array containing the resulting rectangles.
  41. **/
  42. ArtIRect *
  43. art_rect_list_from_uta (ArtUta *uta, int max_width, int max_height,
  44. int *p_nrects)
  45. {
  46. ArtIRect *rects;
  47. int n_rects, n_rects_max;
  48. int x, y;
  49. int width, height;
  50. int ix;
  51. int left_ix;
  52. ArtUtaBbox *utiles;
  53. ArtUtaBbox bb;
  54. int x0, y0, x1, y1;
  55. int *glom;
  56. int glom_rect;
  57. n_rects = 0;
  58. n_rects_max = 1;
  59. rects = art_new (ArtIRect, n_rects_max);
  60. width = uta->width;
  61. height = uta->height;
  62. utiles = uta->utiles;
  63. glom = art_new (int, width * height);
  64. for (ix = 0; ix < width * height; ix++)
  65. glom[ix] = -1;
  66. ix = 0;
  67. for (y = 0; y < height; y++)
  68. for (x = 0; x < width; x++)
  69. {
  70. bb = utiles[ix];
  71. if (bb)
  72. {
  73. x0 = ((uta->x0 + x) << ART_UTILE_SHIFT) + ART_UTA_BBOX_X0(bb);
  74. y0 = ((uta->y0 + y) << ART_UTILE_SHIFT) + ART_UTA_BBOX_Y0(bb);
  75. y1 = ((uta->y0 + y) << ART_UTILE_SHIFT) + ART_UTA_BBOX_Y1(bb);
  76. left_ix = ix;
  77. /* now try to extend to the right */
  78. while (x != width - 1 &&
  79. ART_UTA_BBOX_X1(bb) == ART_UTILE_SIZE &&
  80. (((bb & 0xffffff) ^ utiles[ix + 1]) & 0xffff00ff) == 0 &&
  81. (((uta->x0 + x + 1) << ART_UTILE_SHIFT) +
  82. ART_UTA_BBOX_X1(utiles[ix + 1]) -
  83. x0) <= max_width)
  84. {
  85. bb = utiles[ix + 1];
  86. ix++;
  87. x++;
  88. }
  89. x1 = ((uta->x0 + x) << ART_UTILE_SHIFT) + ART_UTA_BBOX_X1(bb);
  90. /* if rectangle nonempty */
  91. if ((x1 ^ x0) | (y1 ^ y0))
  92. {
  93. /* try to glom onto an existing rectangle */
  94. glom_rect = glom[left_ix];
  95. if (glom_rect != -1 &&
  96. x0 == rects[glom_rect].x0 &&
  97. x1 == rects[glom_rect].x1 &&
  98. y0 == rects[glom_rect].y1 &&
  99. y1 - rects[glom_rect].y0 <= max_height)
  100. {
  101. rects[glom_rect].y1 = y1;
  102. }
  103. else
  104. {
  105. if (n_rects == n_rects_max)
  106. art_expand (rects, ArtIRect, n_rects_max);
  107. rects[n_rects].x0 = x0;
  108. rects[n_rects].y0 = y0;
  109. rects[n_rects].x1 = x1;
  110. rects[n_rects].y1 = y1;
  111. glom_rect = n_rects;
  112. n_rects++;
  113. }
  114. if (y != height - 1)
  115. glom[left_ix + width] = glom_rect;
  116. }
  117. }
  118. ix++;
  119. }
  120. art_free (glom);
  121. *p_nrects = n_rects;
  122. return rects;
  123. }