MMBitmap.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #pragma once
  2. #ifndef MMBITMAP_H
  3. #define MMBITMAP_H
  4. #include "types.h"
  5. #include "rgb.h"
  6. #include <assert.h>
  7. // #include <stdint.h>
  8. #if defined(_MSC_VER)
  9. #include "ms_stdint.h"
  10. #else
  11. #include <stdint.h>
  12. #endif
  13. #ifdef __cplusplus
  14. extern "C"
  15. {
  16. #endif
  17. struct _MMBitmap {
  18. uint8_t *imageBuffer; /* Pixels stored in Quad I format; i.e., origin is in
  19. * top left. Length should be height * bytewidth. */
  20. size_t width; /* Never 0, unless image is NULL. */
  21. size_t height; /* Never 0, unless image is NULL. */
  22. size_t bytewidth; /* The aligned width (width + padding). */
  23. uint8_t bitsPerPixel; /* Should be either 24 or 32. */
  24. uint8_t bytesPerPixel; /* For convenience; should be bitsPerPixel / 8. */
  25. };
  26. typedef struct _MMBitmap MMBitmap;
  27. typedef MMBitmap *MMBitmapRef;
  28. // MMBitmapRef bitmap;
  29. /* Creates new MMBitmap with the given values.
  30. * Follows the Create Rule (caller is responsible for destroy()'ing object). */
  31. MMBitmapRef createMMBitmap(uint8_t *buffer, size_t width, size_t height,
  32. size_t bytewidth, uint8_t bitsPerPixel,
  33. uint8_t bytesPerPixel);
  34. /* Releases memory occupied by MMBitmap. */
  35. void destroyMMBitmap(MMBitmapRef bitmap);
  36. /* Releases memory occupied by MMBitmap. Acts via CallBack method*/
  37. void destroyMMBitmapBuffer(char * bitmapBuffer, void * hint);
  38. /* Returns copy of MMBitmap, to be destroy()'d by caller. */
  39. MMBitmapRef copyMMBitmap(MMBitmapRef bitmap);
  40. /* Returns copy of one MMBitmap juxtaposed in another (to be destroy()'d
  41. * by the caller.), or NULL on error. */
  42. MMBitmapRef copyMMBitmapFromPortion(MMBitmapRef source, MMRect rect);
  43. #define MMBitmapPointInBounds(image, p) ((p).x < (image)->width && \
  44. (p).y < (image)->height)
  45. #define MMBitmapRectInBounds(image, r) \
  46. (((r).origin.x + (r).size.width <= (image)->width) && \
  47. ((r).origin.y + (r).size.height <= (image)->height))
  48. #define MMBitmapGetBounds(image) MMRectMake(0, 0, image->width, image->height)
  49. /* Get pointer to pixel of MMBitmapRef. No bounds checking is performed (check
  50. * yourself before calling this with MMBitmapPointInBounds(). */
  51. #define MMRGBColorRefAtPoint(image, x, y) \
  52. (MMRGBColor *)(assert(MMBitmapPointInBounds(image, MMPointMake(x, y))), \
  53. ((image)->imageBuffer) + (((image)->bytewidth * (y)) \
  54. + ((x) * (image)->bytesPerPixel)))
  55. /* Dereference pixel of MMBitmapRef. Again, no bounds checking is performed. */
  56. #define MMRGBColorAtPoint(image, x, y) *MMRGBColorRefAtPoint(image, x, y)
  57. /* Hex/integer value of color at point. */
  58. #define MMRGBHexAtPoint(image, x, y) \
  59. hexFromMMRGB(MMRGBColorAtPoint(image, x, y))
  60. /* Increment either point.x or point.y depending on the position of point.x.
  61. * That is, if x + 1 is >= width, increment y and start x at the beginning.
  62. * Otherwise, increment x.
  63. *
  64. * This is used as a convenience macro to scan rows when calling functions such
  65. * as findColorInRectAt() and findBitmapInBitmapAt(). */
  66. #define ITER_NEXT_POINT(pixel, width, start_x) \
  67. do { \
  68. if (++(pixel).x >= (width)) { \
  69. (pixel).x = start_x; \
  70. ++(point).y; \
  71. } \
  72. } while (0);
  73. #ifdef __cplusplus
  74. }
  75. #endif
  76. #endif /* MMBITMAP_H */