drawAPI.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. // zlib open source license
  2. //
  3. // Copyright (c) 2017 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_API_DRAW
  24. #define DFPSR_API_DRAW
  25. #include "types.h"
  26. #include <functional>
  27. namespace dsr {
  28. // ------------------------ Below is untested! ------------------------ //
  29. // Drawing shapes
  30. void draw_rectangle(ImageU8& image, const IRect& bound, int color);
  31. void draw_rectangle(ImageF32& image, const IRect& bound, float color);
  32. void draw_rectangle(ImageRgbaU8& image, const IRect& bound, const ColorRgbaI32& color);
  33. void draw_line(ImageU8& image, int32_t x1, int32_t y1, int32_t x2, int32_t y2, int color);
  34. void draw_line(ImageF32& image, int32_t x1, int32_t y1, int32_t x2, int32_t y2, float color);
  35. void draw_line(ImageRgbaU8& image, int32_t x1, int32_t y1, int32_t x2, int32_t y2, const ColorRgbaI32& color);
  36. // Drawing images
  37. // Draw an image to another image
  38. // All image types can draw to their own format
  39. // All image types can draw to RgbaU8
  40. // All monochrome types can draw to each other
  41. // The source and target images can be sub-images from the same atlas but only if the sub-regions are not overlapping
  42. void draw_copy(ImageRgbaU8& target, const ImageRgbaU8& source, int32_t left = 0, int32_t top = 0);
  43. void draw_copy(ImageU8& target, const ImageU8& source, int32_t left = 0, int32_t top = 0);
  44. void draw_copy(ImageU16& target, const ImageU16& source, int32_t left = 0, int32_t top = 0);
  45. void draw_copy(ImageF32& target, const ImageF32& source, int32_t left = 0, int32_t top = 0);
  46. void draw_copy(ImageRgbaU8& target, const ImageU8& source, int32_t left = 0, int32_t top = 0);
  47. void draw_copy(ImageRgbaU8& target, const ImageU16& source, int32_t left = 0, int32_t top = 0);
  48. void draw_copy(ImageRgbaU8& target, const ImageF32& source, int32_t left = 0, int32_t top = 0);
  49. void draw_copy(ImageU8& target, const ImageF32& source, int32_t left = 0, int32_t top = 0);
  50. void draw_copy(ImageU8& target, const ImageU16& source, int32_t left = 0, int32_t top = 0);
  51. void draw_copy(ImageU16& target, const ImageU8& source, int32_t left = 0, int32_t top = 0);
  52. void draw_copy(ImageU16& target, const ImageF32& source, int32_t left = 0, int32_t top = 0);
  53. void draw_copy(ImageF32& target, const ImageU8& source, int32_t left = 0, int32_t top = 0);
  54. void draw_copy(ImageF32& target, const ImageU16& source, int32_t left = 0, int32_t top = 0);
  55. // Draw one RGBA image to another using alpha filtering
  56. // Target alpha does no affect RGB blending, in case that it contains padding for opaque targets
  57. // If you really want to draw to a transparent layer, this method should not be used
  58. void draw_alphaFilter(ImageRgbaU8& target, const ImageRgbaU8& source, int32_t left = 0, int32_t top = 0);
  59. // Draw one RGBA image to another using the alpha channel as height
  60. // sourceAlphaOffset is added to non-zero heights from source alpha
  61. // Writes each source pixel who's alpha value is greater than the target's
  62. // Zero alpha can be used as a mask, because no source value can be below zero in unsigned color formats
  63. void draw_maxAlpha(ImageRgbaU8& target, const ImageRgbaU8& source, int32_t left = 0, int32_t top = 0, int32_t sourceAlphaOffset = 0);
  64. // Draw between multiple images using a height buffer
  65. // Each source pixel is drawn where the source height's pixel exceeds the target height's pixel
  66. // Including the source height pixel, so that the drawn object occludes the following objects below it
  67. // Can be used for isometric top-down and side-scroller games with heavy graphical effects
  68. // A usually contains color pixels
  69. // B usually contains surface normals for light effects
  70. // 16-bit integer depth buffers:
  71. // Fully deterministic overlaps
  72. // Source height zero is treated as invisible even if sourceHeightOffset adds to the height
  73. // It's recommended to let the target height buffer use 32768 as height zero to allow placing things on negative locations
  74. void draw_higher(
  75. ImageU16& targetHeight, const ImageU16& sourceHeight,
  76. int32_t left = 0, int32_t top = 0, int32_t sourceHeightOffset = 0
  77. );
  78. void draw_higher(ImageU16& targetHeight, const ImageU16& sourceHeight,
  79. ImageRgbaU8& targetA, const ImageRgbaU8& sourceA,
  80. int32_t left = 0, int32_t top = 0, int32_t sourceHeightOffset = 0
  81. );
  82. void draw_higher(ImageU16& targetHeight, const ImageU16& sourceHeight,
  83. ImageRgbaU8& targetA, const ImageRgbaU8& sourceA,
  84. ImageRgbaU8& targetB, const ImageRgbaU8& sourceB,
  85. int32_t left = 0, int32_t top = 0, int32_t sourceHeightOffset = 0
  86. );
  87. // 32-bit floating-point depth buffers
  88. // Source height negative infinity is used for invisible pixels
  89. // Negative infinity is expressed using -std::numeric_limits<float>::infinity() from limits.h
  90. // Same pixel size as in ImageRgbaU8 to make aligned reading easier when used together with colors
  91. // Floats allow doing light calculations directly without having to perform expensive conversions from integers
  92. void draw_higher(
  93. ImageF32& targetHeight, const ImageF32& sourceHeight,
  94. int32_t left = 0, int32_t top = 0, float sourceHeightOffset = 0
  95. );
  96. void draw_higher(ImageF32& targetHeight, const ImageF32& sourceHeight,
  97. ImageRgbaU8& targetA, const ImageRgbaU8& sourceA,
  98. int32_t left = 0, int32_t top = 0, float sourceHeightOffset = 0
  99. );
  100. void draw_higher(ImageF32& targetHeight, const ImageF32& sourceHeight,
  101. ImageRgbaU8& targetA, const ImageRgbaU8& sourceA,
  102. ImageRgbaU8& targetB, const ImageRgbaU8& sourceB,
  103. int32_t left = 0, int32_t top = 0, float sourceHeightOffset = 0
  104. );
  105. // Draw one RGBA image to another using alpha clipping
  106. // Source is solid where alpha is greater than threshold, which can be used for animations
  107. void draw_alphaClip(ImageRgbaU8& target, const ImageRgbaU8& source, int32_t left = 0, int32_t top = 0, int32_t threshold = 127);
  108. // Draw a uniform color using a grayscale silhouette as the alpha channel
  109. void draw_silhouette(ImageRgbaU8& target, const ImageU8& silhouette, const ColorRgbaI32& color, int32_t left = 0, int32_t top = 0);
  110. // TODO: Make a separate filter API
  111. // Image resizing
  112. // The interpolate argument
  113. // Bi-linear interoplation is used when true
  114. // Nearest neighbor sampling is used when false
  115. // Create a stretched version of the source image with the given dimensions and default RGBA pack order
  116. OrderedImageRgbaU8 filter_resize(const ImageRgbaU8& image, Sampler interpolation, int32_t newWidth, int32_t newHeight);
  117. // Resize with borders of pixels that aren't stretched
  118. // Using a larger border than half the size will be clamped, so that the center keeps at least 2x2 pixels
  119. OrderedImageRgbaU8 filter_resize3x3(const ImageRgbaU8& image, Sampler interpolation, int newWidth, int newHeight, int leftBorder, int topBorder, int rightBorder, int bottomBorder);
  120. // The source image is scaled by pixelWidth and pixelHeight from the upper left corner
  121. // If source is too small, transparent black pixels (0, 0, 0, 0) fills the outside
  122. // If source is too large, partial pixels will be cropped away completely and replaced by the black border
  123. // Letting the images have the same pack order and be aligned to 16-bytes will increase speed
  124. void filter_blockMagnify(ImageRgbaU8& target, const ImageRgbaU8& source, int pixelWidth, int pixelHeight);
  125. // Image generation and filtering
  126. // Create new images from Lambda expressions
  127. // Useful for pre-generating images for reuse, reference implementations and fast prototyping
  128. // Lambda expressions for generating integer images
  129. using ImageGenRgbaU8 = std::function<ColorRgbaI32(int, int)>;
  130. using ImageGenF32 = std::function<float(int, int)>;
  131. // In-place image generation to an existing image
  132. // The pixel at the upper left corner gets (startX, startY) as x and y arguments to the function
  133. void filter_mapRgbaU8(ImageRgbaU8& target, const ImageGenRgbaU8& lambda, int startX = 0, int startY = 0);
  134. void filter_mapF32(ImageF32& target, const ImageGenF32& lambda, int startX = 0, int startY = 0);
  135. // A simpler image generation that constructs the image as a result
  136. // Example:
  137. // int width = 64;
  138. // int height = 64;
  139. // ImageRgbaU8 fadeImage = filter_generateRgbaU8(width, height, [](int x, int y)->ColorRgbaI32 {
  140. // return ColorRgbaI32(x * 4, y * 4, 0, 255);
  141. // });
  142. // ImageRgbaU8 brighterImage = filter_generateRgbaU8(width, height, [fadeImage](int x, int y)->ColorRgbaI32 {
  143. // ColorRgbaI32 source = image_readPixel_clamp(fadeImage, x, y);
  144. // return ColorRgbaI32(source.red * 2, source.green * 2, source.blue * 2, source.alpha);
  145. // });
  146. ImageRgbaU8 filter_generateRgbaU8(int width, int height, const ImageGenRgbaU8& lambda, int startX = 0, int startY = 0);
  147. ImageF32 filter_generateF32(int width, int height, const ImageGenF32& lambda, int startX = 0, int startY = 0);
  148. }
  149. #endif