filterAPI.h 4.1 KB

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