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.

118 lines
3.5 KiB

  1. /* Copyright (C) 2001-2007 Peter Selinger.
  2. * This file is part of Potrace. It is free software and it is covered
  3. * by the GNU General Public License. See the file COPYING for details. */
  4. #ifndef BITMAP_H
  5. #define BITMAP_H
  6. #ifdef HAVE_CONFIG_H
  7. #include "config.h"
  8. #endif
  9. #include <string.h>
  10. #include <stdlib.h>
  11. /* The bitmap type is defined in potracelib.h */
  12. #include "potracelib.h"
  13. /* The present file defines some convenient macros and static inline
  14. * functions for accessing bitmaps. Since they only produce inline
  15. * code, they can be conveniently shared by the library and frontends,
  16. * if desired */
  17. /* ---------------------------------------------------------------------- */
  18. /* some measurements */
  19. #define BM_WORDSIZE ( (int) sizeof(potrace_word) )
  20. #define BM_WORDBITS (8 * BM_WORDSIZE)
  21. #define BM_HIBIT ( ( (potrace_word) 1 ) << (BM_WORDBITS - 1) )
  22. #define BM_ALLBITS (~(potrace_word) 0)
  23. /* macros for accessing pixel at index (x,y). U* macros omit the
  24. * bounds check. */
  25. #define bm_scanline( bm, y ) ( (bm)->map + (y) * (bm)->dy )
  26. #define bm_index( bm, x, y ) (&bm_scanline( bm, y )[(x) / BM_WORDBITS])
  27. #define bm_mask( x ) ( BM_HIBIT >> ( (x) & (BM_WORDBITS - 1) ) )
  28. #define bm_range( x, a ) ( (int) (x) >= 0 && (int) (x) < (a) )
  29. #define bm_safe( bm, x, y ) ( bm_range( x, (bm)->w ) && bm_range( y, (bm)->h ) )
  30. #define BM_UGET( bm, x, y ) ( ( *bm_index( bm, x, y ) & bm_mask( x ) ) != 0 )
  31. #define BM_USET( bm, x, y ) ( *bm_index( bm, x, y ) |= bm_mask( x ) )
  32. #define BM_UCLR( bm, x, y ) ( *bm_index( bm, x, y ) &= ~bm_mask( x ) )
  33. #define BM_UINV( bm, x, y ) ( *bm_index( bm, x, y ) ^= bm_mask( x ) )
  34. #define BM_UPUT( bm, x, y, b ) ( (b) ? BM_USET( bm, x, y ) : BM_UCLR( bm, x, y ) )
  35. #define BM_GET( bm, x, y ) (bm_safe( bm, x, y ) ? BM_UGET( bm, x, y ) : 0)
  36. #define BM_SET( bm, x, y ) (bm_safe( bm, x, y ) ? BM_USET( bm, x, y ) : 0)
  37. #define BM_CLR( bm, x, y ) (bm_safe( bm, x, y ) ? BM_UCLR( bm, x, y ) : 0)
  38. #define BM_INV( bm, x, y ) (bm_safe( bm, x, y ) ? BM_UINV( bm, x, y ) : 0)
  39. #define BM_PUT( bm, x, y, b ) (bm_safe( bm, x, y ) ? BM_UPUT( bm, x, y, b ) : 0)
  40. /* free the given bitmap. Leaves errno untouched. */
  41. static inline void bm_free( potrace_bitmap_t* bm )
  42. {
  43. if( bm )
  44. {
  45. free( bm->map );
  46. }
  47. free( bm );
  48. }
  49. /* return new un-initialized bitmap. NULL with errno on error */
  50. static inline potrace_bitmap_t* bm_new( int w, int h )
  51. {
  52. potrace_bitmap_t* bm;
  53. int dy = (w + BM_WORDBITS - 1) / BM_WORDBITS;
  54. bm = (potrace_bitmap_t*) malloc( sizeof(potrace_bitmap_t) );
  55. if( !bm )
  56. {
  57. return NULL;
  58. }
  59. bm->w = w;
  60. bm->h = h;
  61. bm->dy = dy;
  62. bm->map = (potrace_word*) malloc( dy * h * BM_WORDSIZE );
  63. if( !bm->map )
  64. {
  65. free( bm );
  66. return NULL;
  67. }
  68. return bm;
  69. }
  70. /* clear the given bitmap. Set all bits to c. */
  71. static inline void bm_clear( potrace_bitmap_t* bm, int c )
  72. {
  73. memset( bm->map, c ? -1 : 0, bm->dy * bm->h * BM_WORDSIZE );
  74. }
  75. /* duplicate the given bitmap. Return NULL on error with errno set. */
  76. static inline potrace_bitmap_t* bm_dup( const potrace_bitmap_t* bm )
  77. {
  78. potrace_bitmap_t* bm1 = bm_new( bm->w, bm->h );
  79. if( !bm1 )
  80. {
  81. return NULL;
  82. }
  83. memcpy( bm1->map, bm->map, bm->dy * bm->h * BM_WORDSIZE );
  84. return bm1;
  85. }
  86. /* invert the given bitmap. */
  87. static inline void bm_invert( potrace_bitmap_t* bm )
  88. {
  89. int i;
  90. for( i = 0; i < bm->dy * bm->h; i++ )
  91. {
  92. bm->map[i] ^= BM_ALLBITS;
  93. }
  94. }
  95. #endif /* BITMAP_H */