drawAPI.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. 
  2. // zlib open source license
  3. //
  4. // Copyright (c) 2017 to 2025 David Forsgren Piuva
  5. //
  6. // This software is provided 'as-is', without any express or implied
  7. // warranty. In no event will the authors be held liable for any damages
  8. // arising from the use of this software.
  9. //
  10. // Permission is granted to anyone to use this software for any purpose,
  11. // including commercial applications, and to alter it and redistribute it
  12. // freely, subject to the following restrictions:
  13. //
  14. // 1. The origin of this software must not be misrepresented; you must not
  15. // claim that you wrote the original software. If you use this software
  16. // in a product, an acknowledgment in the product documentation would be
  17. // appreciated but is not required.
  18. //
  19. // 2. Altered source versions must be plainly marked as such, and must not be
  20. // misrepresented as being the original software.
  21. //
  22. // 3. This notice may not be removed or altered from any source
  23. // distribution.
  24. #ifndef DFPSR_API_DRAW
  25. #define DFPSR_API_DRAW
  26. #include "../implementation/image/Image.h"
  27. namespace dsr {
  28. // Instead of having lots of arguments for source and target regions, this library uses a system of sub-images, so that any non-aligned drawing method can have source and target regions modified by cropping the image arguments as views.
  29. // To limit drawing to a rectangular target region:
  30. // * Create a sub-image using image_getSubImage.
  31. // 0-----------------------------------X
  32. // | Parent-image |
  33. // | |
  34. // | ------------------- |
  35. // | | IRect | |
  36. // | | | |
  37. // | | | |
  38. // | | | |
  39. // | ------------------- |
  40. // | |
  41. // Y-----------------------------------*
  42. // * Translate coordinates by subtracting the region's upper left corner.
  43. // 0-----------------------------------X
  44. // | Parent-image |
  45. // | |
  46. // | 0-------------------X |
  47. // | | Sub-image | |
  48. // | | | |
  49. // | | | |
  50. // | | | |
  51. // | Y-------------------* |
  52. // | |
  53. // Y-----------------------------------*
  54. // * Draw to the new sub-image in the new local coordinate system.
  55. // 0-------------------X
  56. // | Sub-image / | |
  57. // | /| |__|
  58. // | ________/ | | |
  59. // | / | |
  60. // Y-------------------*
  61. // Drawing shapes
  62. // TODO: Create wrappers taking left, top, width, height to reduce clutter from the IRect constructor.
  63. void draw_rectangle(const ImageU8& image, const IRect& bound, int color);
  64. void draw_rectangle(const ImageU16& image, const IRect& bound, int color);
  65. void draw_rectangle(const ImageF32& image, const IRect& bound, float color);
  66. void draw_rectangle(const ImageRgbaU8& image, const IRect& bound, const ColorRgbaI32& color);
  67. // Draw using a color that has been packed in advance with the same pack order using the image_saturateAndPack function.
  68. // This saves time on saturation and packing when drawing many rectangles of the same color.
  69. void draw_rectangle(const ImageRgbaU8& image, const IRect& bound, uint32_t packedColor);
  70. // TODO: Also take two IVector2D as inlined wrapper functions.
  71. void draw_line(const ImageU8& image, int32_t x1, int32_t y1, int32_t x2, int32_t y2, int color);
  72. void draw_line(const ImageU16& image, int32_t x1, int32_t y1, int32_t x2, int32_t y2, int color);
  73. void draw_line(const ImageF32& image, int32_t x1, int32_t y1, int32_t x2, int32_t y2, float color);
  74. void draw_line(const ImageRgbaU8& image, int32_t x1, int32_t y1, int32_t x2, int32_t y2, const ColorRgbaI32& color);
  75. // Draw using a color that has been packed in advance with the same pack order using the image_saturateAndPack function.
  76. // This saves time on saturation and packing when drawing many lines of the same color.
  77. void draw_line(const ImageRgbaU8& image, int32_t x1, int32_t y1, int32_t x2, int32_t y2, uint32_t packedColor);
  78. // Drawing images
  79. // Draw an image to another image
  80. // All image types can draw to their own format
  81. // All image types can draw to RgbaU8
  82. // All monochrome types can draw to each other
  83. // The source and target images can be sub-images from the same atlas but only if the sub-regions are not overlapping
  84. void draw_copy(const ImageRgbaU8& target, const ImageRgbaU8& source, int32_t left = 0, int32_t top = 0);
  85. void draw_copy(const ImageU8& target, const ImageU8& source, int32_t left = 0, int32_t top = 0);
  86. void draw_copy(const ImageU16& target, const ImageU16& source, int32_t left = 0, int32_t top = 0);
  87. void draw_copy(const ImageF32& target, const ImageF32& source, int32_t left = 0, int32_t top = 0);
  88. void draw_copy(const ImageRgbaU8& target, const ImageU8& source, int32_t left = 0, int32_t top = 0);
  89. void draw_copy(const ImageRgbaU8& target, const ImageU16& source, int32_t left = 0, int32_t top = 0);
  90. void draw_copy(const ImageRgbaU8& target, const ImageF32& source, int32_t left = 0, int32_t top = 0);
  91. void draw_copy(const ImageU8& target, const ImageF32& source, int32_t left = 0, int32_t top = 0);
  92. void draw_copy(const ImageU8& target, const ImageU16& source, int32_t left = 0, int32_t top = 0);
  93. void draw_copy(const ImageU16& target, const ImageU8& source, int32_t left = 0, int32_t top = 0);
  94. void draw_copy(const ImageU16& target, const ImageF32& source, int32_t left = 0, int32_t top = 0);
  95. void draw_copy(const ImageF32& target, const ImageU8& source, int32_t left = 0, int32_t top = 0);
  96. void draw_copy(const ImageF32& target, const ImageU16& source, int32_t left = 0, int32_t top = 0);
  97. // Draw one RGBA image to another using alpha filtering
  98. // Target alpha does no affect RGB blending, in case that it contains padding for opaque targets
  99. // If you really want to draw to a transparent layer, this method should not be used
  100. void draw_alphaFilter(const ImageRgbaU8& target, const ImageRgbaU8& source, int32_t left = 0, int32_t top = 0);
  101. // Draw one RGBA image to another using the alpha channel as height
  102. // sourceAlphaOffset is added to non-zero heights from source alpha
  103. // Writes each source pixel who's alpha value is greater than the target's
  104. // Zero alpha can be used as a mask, because no source value can be below zero in unsigned color formats
  105. void draw_maxAlpha(const ImageRgbaU8& target, const ImageRgbaU8& source, int32_t left = 0, int32_t top = 0, int32_t sourceAlphaOffset = 0);
  106. // Draw between multiple images using a height buffer
  107. // Each source pixel is drawn where the source height's pixel exceeds the target height's pixel
  108. // Including the source height pixel, so that the drawn object occludes the following objects below it
  109. // Can be used for isometric top-down and side-scroller games with heavy graphical effects
  110. // A usually contains color pixels
  111. // B usually contains surface normals for light effects
  112. // 16-bit integer depth buffers:
  113. // Fully deterministic overlaps
  114. // Source height zero is treated as invisible even if sourceHeightOffset adds to the height
  115. // It's recommended to let the target height buffer use 32768 as height zero to allow placing things on negative locations
  116. void draw_higher(
  117. ImageU16& targetHeight, const ImageU16& sourceHeight,
  118. int32_t left = 0, int32_t top = 0, int32_t sourceHeightOffset = 0
  119. );
  120. void draw_higher(const ImageU16& targetHeight, const ImageU16& sourceHeight,
  121. ImageRgbaU8& targetA, const ImageRgbaU8& sourceA,
  122. int32_t left = 0, int32_t top = 0, int32_t sourceHeightOffset = 0
  123. );
  124. void draw_higher(const ImageU16& targetHeight, const ImageU16& sourceHeight,
  125. ImageRgbaU8& targetA, const ImageRgbaU8& sourceA,
  126. ImageRgbaU8& targetB, const ImageRgbaU8& sourceB,
  127. int32_t left = 0, int32_t top = 0, int32_t sourceHeightOffset = 0
  128. );
  129. // 32-bit floating-point depth buffers
  130. // Source height negative infinity is used for invisible pixels
  131. // Negative infinity is expressed using -std::numeric_limits<float>::infinity() from limits.h
  132. // Same pixel size as in ImageRgbaU8 to make aligned reading easier when used together with colors
  133. // Floats allow doing light calculations directly without having to perform expensive conversions from integers
  134. void draw_higher(
  135. ImageF32& targetHeight, const ImageF32& sourceHeight,
  136. int32_t left = 0, int32_t top = 0, float sourceHeightOffset = 0
  137. );
  138. void draw_higher(const ImageF32& targetHeight, const ImageF32& sourceHeight,
  139. ImageRgbaU8& targetA, const ImageRgbaU8& sourceA,
  140. int32_t left = 0, int32_t top = 0, float sourceHeightOffset = 0
  141. );
  142. void draw_higher(const ImageF32& targetHeight, const ImageF32& sourceHeight,
  143. ImageRgbaU8& targetA, const ImageRgbaU8& sourceA,
  144. ImageRgbaU8& targetB, const ImageRgbaU8& sourceB,
  145. int32_t left = 0, int32_t top = 0, float sourceHeightOffset = 0
  146. );
  147. // TODO: Inlined wrappers using IVector2D.
  148. // Draw one RGBA image to another using alpha clipping
  149. // Source is solid where alpha is greater than threshold, which can be used for animations
  150. void draw_alphaClip(const ImageRgbaU8& target, const ImageRgbaU8& source, int32_t left = 0, int32_t top = 0, int32_t threshold = 127);
  151. // Draw a uniform color using a grayscale silhouette as the alpha channel
  152. void draw_silhouette(const ImageRgbaU8& target, const ImageU8& silhouette, const ColorRgbaI32& color, int32_t left = 0, int32_t top = 0);
  153. }
  154. #endif