imageTemplate.h 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // zlib open source license
  2. //
  3. // Copyright (c) 2018 to 2019 David Forsgren Piuva
  4. //
  5. // This software is provided 'as-is', without any express or implied
  6. // warranty. In no event will the authors be held liable for any damages
  7. // arising from the use of this software.
  8. //
  9. // Permission is granted to anyone to use this software for any purpose,
  10. // including commercial applications, and to alter it and redistribute it
  11. // freely, subject to the following restrictions:
  12. //
  13. // 1. The origin of this software must not be misrepresented; you must not
  14. // claim that you wrote the original software. If you use this software
  15. // in a product, an acknowledgment in the product documentation would be
  16. // appreciated but is not required.
  17. //
  18. // 2. Altered source versions must be plainly marked as such, and must not be
  19. // misrepresented as being the original software.
  20. //
  21. // 3. This notice may not be removed or altered from any source
  22. // distribution.
  23. #ifndef DFPSR_IMAGE_TEMPLATE
  24. #define DFPSR_IMAGE_TEMPLATE
  25. #include "imageInternal.h"
  26. #include "../../math/scalar.h"
  27. #include "../Image.h"
  28. #include <limits>
  29. namespace dsr {
  30. // TODO: Remove clamped pixel operation
  31. // Each image type must define initializeImage instead of a constructor;
  32. // These macros are used to compile instances of template functions because it's much safer than exposing header defined template classes.
  33. #define IMAGE_DEFINITION(IMAGE_TYPE,CHANNELS,COLOR_TYPE,ELEMENT_TYPE) \
  34. void IMAGE_TYPE::writePixel(IMAGE_TYPE &image, int32_t x, int32_t y, COLOR_TYPE color) { \
  35. if (x >= 0 && x < image.width && y >= 0 && y < image.height) { \
  36. *(COLOR_TYPE*)(buffer_dangerous_getUnsafeData(image.buffer) + image.startOffset + (x * sizeof(COLOR_TYPE)) + (y * image.stride)) = color; \
  37. } \
  38. } \
  39. void IMAGE_TYPE::writePixel_unsafe(IMAGE_TYPE &image, int32_t x, int32_t y, COLOR_TYPE color) { \
  40. *(COLOR_TYPE*)(buffer_dangerous_getUnsafeData(image.buffer) + image.startOffset + (x * sizeof(COLOR_TYPE)) + (y * image.stride)) = color; \
  41. } \
  42. COLOR_TYPE IMAGE_TYPE::readPixel_clamp(const IMAGE_TYPE &image, int32_t x, int32_t y) { \
  43. if (image.width > 0 && image.height > 0) { \
  44. if (x < 0) { x = 0; } \
  45. if (y < 0) { y = 0; } \
  46. if (x >= image.width) { x = image.width - 1; } \
  47. if (y >= image.height) { y = image.height - 1; } \
  48. return *(COLOR_TYPE*)(buffer_dangerous_getUnsafeData(image.buffer) + image.startOffset + (x * sizeof(COLOR_TYPE)) + (y * image.stride)); \
  49. } else { \
  50. return COLOR_TYPE(); \
  51. } \
  52. } \
  53. COLOR_TYPE IMAGE_TYPE::readPixel_unsafe(const IMAGE_TYPE &image, int32_t x, int32_t y) { \
  54. assert(x >= 0 && x < image.width && y >= 0 && y < image.height); \
  55. return *(COLOR_TYPE*)(buffer_dangerous_getUnsafeData(image.buffer) + image.startOffset + (x * sizeof(COLOR_TYPE)) + (y * image.stride)); \
  56. }
  57. }
  58. #endif