MMBitmap_c.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #include "MMBitmap.h"
  2. #include <assert.h>
  3. #include <string.h>
  4. //MMBitmapRef createMMBitmap()
  5. MMBitmapRef createMMBitmap(
  6. uint8_t *buffer,
  7. size_t width,
  8. size_t height,
  9. size_t bytewidth,
  10. uint8_t bitsPerPixel,
  11. uint8_t bytesPerPixel
  12. ){
  13. MMBitmapRef bitmap = malloc(sizeof(MMBitmap));
  14. if (bitmap == NULL) return NULL;
  15. bitmap->imageBuffer = buffer;
  16. bitmap->width = width;
  17. bitmap->height = height;
  18. bitmap->bytewidth = bytewidth;
  19. bitmap->bitsPerPixel = bitsPerPixel;
  20. bitmap->bytesPerPixel = bytesPerPixel;
  21. return bitmap;
  22. }
  23. void destroyMMBitmap(MMBitmapRef bitmap)
  24. {
  25. assert(bitmap != NULL);
  26. if (bitmap->imageBuffer != NULL) {
  27. free(bitmap->imageBuffer);
  28. bitmap->imageBuffer = NULL;
  29. }
  30. free(bitmap);
  31. }
  32. void destroyMMBitmapBuffer(char * bitmapBuffer, void * hint)
  33. {
  34. if (bitmapBuffer != NULL)
  35. {
  36. free(bitmapBuffer);
  37. }
  38. }
  39. MMBitmapRef copyMMBitmap(MMBitmapRef bitmap)
  40. {
  41. uint8_t *copiedBuf = NULL;
  42. assert(bitmap != NULL);
  43. if (bitmap->imageBuffer != NULL) {
  44. const size_t bufsize = bitmap->height * bitmap->bytewidth;
  45. copiedBuf = malloc(bufsize);
  46. if (copiedBuf == NULL) return NULL;
  47. memcpy(copiedBuf, bitmap->imageBuffer, bufsize);
  48. }
  49. return createMMBitmap(copiedBuf,
  50. bitmap->width,
  51. bitmap->height,
  52. bitmap->bytewidth,
  53. bitmap->bitsPerPixel,
  54. bitmap->bytesPerPixel);
  55. }
  56. MMBitmapRef copyMMBitmapFromPortion(MMBitmapRef source, MMRect rect)
  57. {
  58. assert(source != NULL);
  59. if (source->imageBuffer == NULL || !MMBitmapRectInBounds(source, rect)) {
  60. return NULL;
  61. } else {
  62. uint8_t *copiedBuf = NULL;
  63. const size_t bufsize = rect.size.height * source->bytewidth;
  64. const size_t offset = (source->bytewidth * rect.origin.y) +
  65. (rect.origin.x * source->bytesPerPixel);
  66. /* Don't go over the bounds, programmer! */
  67. assert((bufsize + offset) <= (source->bytewidth * source->height));
  68. copiedBuf = malloc(bufsize);
  69. if (copiedBuf == NULL) return NULL;
  70. memcpy(copiedBuf, source->imageBuffer + offset, bufsize);
  71. return createMMBitmap(copiedBuf,
  72. rect.size.width,
  73. rect.size.height,
  74. source->bytewidth,
  75. source->bitsPerPixel,
  76. source->bytesPerPixel);
  77. }
  78. }