region.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /* Definitions for partial image regions.
  2. *
  3. * J.Cupitt, 8/4/93
  4. *
  5. * 2/3/11
  6. * - move to GObject
  7. */
  8. /*
  9. This file is part of VIPS.
  10. VIPS is free software; you can redistribute it and/or modify
  11. it under the terms of the GNU Lesser General Public License as published by
  12. the Free Software Foundation; either version 2 of the License, or
  13. (at your option) any later version.
  14. This program is distributed in the hope that it will be useful,
  15. but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. GNU Lesser General Public License for more details.
  18. You should have received a copy of the GNU Lesser General Public License
  19. along with this program; if not, write to the Free Software
  20. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  21. 02110-1301 USA
  22. */
  23. /*
  24. These files are distributed with VIPS - http://www.vips.ecs.soton.ac.uk
  25. */
  26. #ifndef VIPS_REGION_H
  27. #define VIPS_REGION_H
  28. #ifdef __cplusplus
  29. extern "C" {
  30. #endif /*__cplusplus*/
  31. #define VIPS_TYPE_REGION (vips_region_get_type())
  32. #define VIPS_REGION( obj ) \
  33. (G_TYPE_CHECK_INSTANCE_CAST( (obj), \
  34. VIPS_TYPE_REGION, VipsRegion ))
  35. #define VIPS_REGION_CLASS( klass ) \
  36. (G_TYPE_CHECK_CLASS_CAST( (klass), \
  37. VIPS_TYPE_REGION, VipsRegionClass))
  38. #define VIPS_IS_REGION( obj ) \
  39. (G_TYPE_CHECK_INSTANCE_TYPE( (obj), VIPS_TYPE_REGION ))
  40. #define VIPS_IS_REGION_CLASS( klass ) \
  41. (G_TYPE_CHECK_CLASS_TYPE( (klass), VIPS_TYPE_REGION ))
  42. #define VIPS_REGION_GET_CLASS( obj ) \
  43. (G_TYPE_INSTANCE_GET_CLASS( (obj), \
  44. VIPS_TYPE_REGION, VipsRegionClass ))
  45. /**
  46. * VipsRegionShrink:
  47. * @VIPS_REGION_SHRINK_MEAN: use the average
  48. * @VIPS_REGION_SHRINK_MEDIAN: use the median
  49. * @VIPS_REGION_SHRINK_MODE: use the mode
  50. *
  51. * How to calculate the output pixels when shrinking a 2x2 region.
  52. */
  53. typedef enum {
  54. VIPS_REGION_SHRINK_MEAN,
  55. VIPS_REGION_SHRINK_MEDIAN,
  56. VIPS_REGION_SHRINK_MODE,
  57. VIPS_REGION_SHRINK_LAST
  58. } VipsRegionShrink;
  59. /* Sub-area of image.
  60. */
  61. typedef struct _VipsRegion {
  62. VipsObject parent_object;
  63. /*< public >*/
  64. /* Users may read these two fields.
  65. */
  66. VipsImage *im; /* Link back to parent image */
  67. VipsRect valid; /* Area of parent we can see */
  68. /* The rest of VipsRegion is private.
  69. */
  70. /*< private >*/
  71. RegionType type; /* What kind of attachment */
  72. VipsPel *data; /* Off here to get data */
  73. int bpl; /* Bytes-per-line for data */
  74. void *seq; /* Sequence we are using to fill region */
  75. /* The thread that made this region. Used to assert() test that
  76. * regions are not being shared between threads.
  77. */
  78. GThread *thread;
  79. /* Ref to the window we use for this region, if any.
  80. */
  81. VipsWindow *window;
  82. /* Ref to the buffer we use for this region, if any.
  83. */
  84. VipsBuffer *buffer;
  85. /* The image this region is on has changed and caches need to be
  86. * dropped.
  87. */
  88. gboolean invalid;
  89. } VipsRegion;
  90. typedef struct _VipsRegionClass {
  91. VipsObjectClass parent_class;
  92. } VipsRegionClass;
  93. /* Don't put spaces around void here, it breaks gtk-doc.
  94. */
  95. GType vips_region_get_type(void);
  96. VipsRegion *vips_region_new( VipsImage *image );
  97. int vips_region_buffer( VipsRegion *reg, const VipsRect *r );
  98. int vips_region_image( VipsRegion *reg, const VipsRect *r );
  99. int vips_region_region( VipsRegion *reg, VipsRegion *dest,
  100. const VipsRect *r, int x, int y );
  101. int vips_region_equalsregion( VipsRegion *reg1, VipsRegion *reg2 );
  102. int vips_region_position( VipsRegion *reg, int x, int y );
  103. void vips_region_paint( VipsRegion *reg, const VipsRect *r, int value );
  104. void vips_region_paint_pel( VipsRegion *reg,
  105. const VipsRect *r, const VipsPel *ink );
  106. void vips_region_black( VipsRegion *reg );
  107. void vips_region_copy( VipsRegion *reg, VipsRegion *dest,
  108. const VipsRect *r, int x, int y );
  109. int vips_region_shrink_method( VipsRegion *from, VipsRegion *to,
  110. const VipsRect *target, VipsRegionShrink method );
  111. int vips_region_shrink( VipsRegion *from, VipsRegion *to,
  112. const VipsRect *target );
  113. int vips_region_prepare( VipsRegion *reg, const VipsRect *r );
  114. int vips_region_prepare_to( VipsRegion *reg,
  115. VipsRegion *dest, const VipsRect *r, int x, int y );
  116. VipsPel *vips_region_fetch( VipsRegion *region,
  117. int left, int top, int width, int height, size_t *len );
  118. int vips_region_width( VipsRegion *region );
  119. int vips_region_height( VipsRegion *region );
  120. void vips_region_invalidate( VipsRegion *reg );
  121. /* Use this to count pixels passing through key points. Handy for spotting bad
  122. * overcomputation.
  123. */
  124. #ifdef DEBUG_LEAK
  125. #define VIPS_COUNT_PIXELS( R, N ) vips__region_count_pixels( R, N )
  126. #else /*!DEBUG_LEAK*/
  127. #define VIPS_COUNT_PIXELS( R, N )
  128. #endif /*DEBUG_LEAK*/
  129. /* Macros on VipsRegion.
  130. * VIPS_REGION_LSKIP() add to move down line
  131. * VIPS_REGION_N_ELEMENTS() number of elements across region
  132. * VIPS_REGION_SIZEOF_LINE() sizeof width of region
  133. * VIPS_REGION_ADDR() address of pixel in region
  134. */
  135. #define VIPS_REGION_LSKIP( R ) \
  136. ((size_t)((R)->bpl))
  137. #define VIPS_REGION_N_ELEMENTS( R ) \
  138. ((size_t)((R)->valid.width * (R)->im->Bands))
  139. #define VIPS_REGION_SIZEOF_LINE( R ) \
  140. ((size_t)((R)->valid.width * VIPS_IMAGE_SIZEOF_PEL( (R)->im) ))
  141. /* If DEBUG is defined, add bounds checking.
  142. */
  143. #ifdef DEBUG
  144. #define VIPS_REGION_ADDR( R, X, Y ) \
  145. ( (vips_rect_includespoint( &(R)->valid, (X), (Y) ))? \
  146. ((R)->data + ((Y) - (R)->valid.top) * VIPS_REGION_LSKIP(R) + \
  147. ((X) - (R)->valid.left) * VIPS_IMAGE_SIZEOF_PEL((R)->im)): \
  148. (fprintf( stderr, \
  149. "VIPS_REGION_ADDR: point out of bounds, " \
  150. "file \"%s\", line %d\n" \
  151. "(point x=%d, y=%d\n" \
  152. " should have been within VipsRect left=%d, top=%d, " \
  153. "width=%d, height=%d)\n", \
  154. __FILE__, __LINE__, \
  155. (X), (Y), \
  156. (R)->valid.left, \
  157. (R)->valid.top, \
  158. (R)->valid.width, \
  159. (R)->valid.height ), abort(), (VipsPel *) NULL) \
  160. )
  161. #else /*DEBUG*/
  162. #define VIPS_REGION_ADDR( R, X, Y ) \
  163. ((R)->data + \
  164. ((Y)-(R)->valid.top) * VIPS_REGION_LSKIP( R ) + \
  165. ((X)-(R)->valid.left) * VIPS_IMAGE_SIZEOF_PEL( (R)->im ))
  166. #endif /*DEBUG*/
  167. #define VIPS_REGION_ADDR_TOPLEFT( R ) ((R)->data)
  168. #ifdef __cplusplus
  169. }
  170. #endif /*__cplusplus*/
  171. #endif /*VIPS_REGION_H*/