filterAPI.h 4.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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_FILTER
  25. #define DFPSR_API_FILTER
  26. #include "../image/Image.h"
  27. #include <functional>
  28. namespace dsr {
  29. // Sampling modes
  30. enum class Sampler {
  31. Nearest, // Taking the nearest value to create square pixels.
  32. Linear // Taking a linear interpolation of the nearest pixels.
  33. };
  34. // Image resizing
  35. // Create a stretched version of the source image with the given dimensions and default RGBA pack order.
  36. OrderedImageRgbaU8 filter_resize(const ImageRgbaU8 &source, Sampler interpolation, int32_t newWidth, int32_t newHeight);
  37. AlignedImageU8 filter_resize(const ImageU8 &source, Sampler interpolation, int32_t newWidth, int32_t newHeight);
  38. // The nearest-neighbor resize used for up-scaling the window canvas.
  39. // The source image is scaled by pixelWidth and pixelHeight from the upper left corner.
  40. // If source is too small, transparent black pixels (0, 0, 0, 0) fills the outside.
  41. // If source is too large, partial pixels will be cropped away completely and replaced by the black border.
  42. // Letting the images have the same pack order and be aligned to 16-bytes will increase speed.
  43. void filter_blockMagnify(const ImageRgbaU8 &target, const ImageRgbaU8 &source, int pixelWidth, int pixelHeight);
  44. // Image generation and filtering
  45. // Create images from Lambda expressions when speed is not critical.
  46. // Capture images within [] and sample pixels from them using image_readPixel_border, image_readPixel_clamp and image_readPixel_tile.
  47. // Lambda expressions for generating integer images.
  48. using ImageGenRgbaU8 = std::function<ColorRgbaI32(int x, int y)>;
  49. using ImageGenI32 = std::function<int32_t(int x, int y)>; // Used for U8 and U16 images using different saturations.
  50. using ImageGenF32 = std::function<float(int x, int y)>;
  51. // In-place image generation to an existing image.
  52. // The pixel at the upper left corner gets (startX, startY) as x and y arguments to the function.
  53. void filter_mapRgbaU8(const ImageRgbaU8 target, const ImageGenRgbaU8& lambda, int startX = 0, int startY = 0);
  54. void filter_mapU8(const ImageU8 target, const ImageGenI32& lambda, int startX = 0, int startY = 0);
  55. void filter_mapU16(const ImageU16 target, const ImageGenI32& lambda, int startX = 0, int startY = 0);
  56. void filter_mapF32(const ImageF32 target, const ImageGenF32& lambda, int startX = 0, int startY = 0);
  57. // A simpler image generation that constructs the image as a result.
  58. // Example:
  59. // int width = 64;
  60. // int height = 64;
  61. // ImageRgbaU8 fadeImage = filter_generateRgbaU8(width, height, [](int x, int y)->ColorRgbaI32 {
  62. // return ColorRgbaI32(x * 4, y * 4, 0, 255);
  63. // });
  64. // ImageRgbaU8 brighterImage = filter_generateRgbaU8(width, height, [fadeImage](int x, int y)->ColorRgbaI32 {
  65. // ColorRgbaI32 source = image_readPixel_clamp(fadeImage, x, y);
  66. // return ColorRgbaI32(source.red * 2, source.green * 2, source.blue * 2, source.alpha);
  67. // });
  68. OrderedImageRgbaU8 filter_generateRgbaU8(int width, int height, const ImageGenRgbaU8& lambda, int startX = 0, int startY = 0);
  69. AlignedImageU8 filter_generateU8(int width, int height, const ImageGenI32& lambda, int startX = 0, int startY = 0);
  70. AlignedImageU16 filter_generateU16(int width, int height, const ImageGenI32& lambda, int startX = 0, int startY = 0);
  71. AlignedImageF32 filter_generateF32(int width, int height, const ImageGenF32& lambda, int startX = 0, int startY = 0);
  72. }
  73. #endif