filterAPI.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. #define DFPSR_INTERNAL_ACCESS
  24. #include <cassert>
  25. #include "imageAPI.h"
  26. #include "filterAPI.h"
  27. #include "../image/draw.h"
  28. #include "../image/PackOrder.h"
  29. #include "../image/internal/imageTemplate.h"
  30. #include "../image/internal/imageInternal.h"
  31. using namespace dsr;
  32. // -------------------------------- Image generation and filtering --------------------------------
  33. static void mapRgbaU8(ImageRgbaU8Impl& target, const ImageGenRgbaU8& lambda, int startX, int startY) {
  34. const int targetWidth = target.width;
  35. const int targetHeight = target.height;
  36. const int targetStride = target.stride;
  37. SafePointer<Color4xU8> targetRow = imageInternal::getSafeData<Color4xU8>(target);
  38. for (int y = startY; y < targetHeight + startY; y++) {
  39. SafePointer<Color4xU8> targetPixel = targetRow;
  40. for (int x = startX; x < targetWidth + startX; x++) {
  41. *targetPixel = target.packRgba(lambda(x, y).saturate());
  42. targetPixel += 1;
  43. }
  44. targetRow.increaseBytes(targetStride);
  45. }
  46. }
  47. void dsr::filter_mapRgbaU8(ImageRgbaU8 target, const ImageGenRgbaU8& lambda, int startX, int startY) {
  48. if (target.get() != nullptr) {
  49. mapRgbaU8(*target, lambda, startX, startY);
  50. }
  51. }
  52. OrderedImageRgbaU8 dsr::filter_generateRgbaU8(int width, int height, const ImageGenRgbaU8& lambda, int startX, int startY) {
  53. OrderedImageRgbaU8 result = image_create_RgbaU8(width, height);
  54. filter_mapRgbaU8(result, lambda, startX, startY);
  55. return result;
  56. }
  57. template <typename IMAGE_TYPE, typename PIXEL_TYPE, int MIN_VALUE, int MAX_VALUE>
  58. static void mapMonochrome(IMAGE_TYPE& target, const ImageGenI32& lambda, int startX, int startY) {
  59. const int targetWidth = target.width;
  60. const int targetHeight = target.height;
  61. const int targetStride = target.stride;
  62. SafePointer<PIXEL_TYPE> targetRow = imageInternal::getSafeData<PIXEL_TYPE>(target);
  63. for (int y = startY; y < targetHeight + startY; y++) {
  64. SafePointer<PIXEL_TYPE> targetPixel = targetRow;
  65. for (int x = startX; x < targetWidth + startX; x++) {
  66. int output = lambda(x, y);
  67. if (output < MIN_VALUE) { output = MIN_VALUE; }
  68. if (output > MAX_VALUE) { output = MAX_VALUE; }
  69. *targetPixel = output;
  70. targetPixel += 1;
  71. }
  72. targetRow.increaseBytes(targetStride);
  73. }
  74. }
  75. void dsr::filter_mapU8(ImageU8 target, const ImageGenI32& lambda, int startX, int startY) {
  76. if (target.get() != nullptr) {
  77. mapMonochrome<ImageU8Impl, uint8_t, 0, 255>(*target, lambda, startX, startY);
  78. }
  79. }
  80. AlignedImageU8 dsr::filter_generateU8(int width, int height, const ImageGenI32& lambda, int startX, int startY) {
  81. AlignedImageU8 result = image_create_U8(width, height);
  82. filter_mapU8(result, lambda, startX, startY);
  83. return result;
  84. }
  85. void dsr::filter_mapU16(ImageU16 target, const ImageGenI32& lambda, int startX, int startY) {
  86. if (target.get() != nullptr) {
  87. mapMonochrome<ImageU16Impl, uint16_t, 0, 65535>(*target, lambda, startX, startY);
  88. }
  89. }
  90. AlignedImageU16 dsr::filter_generateU16(int width, int height, const ImageGenI32& lambda, int startX, int startY) {
  91. AlignedImageU16 result = image_create_U16(width, height);
  92. filter_mapU16(result, lambda, startX, startY);
  93. return result;
  94. }
  95. static void mapF32(ImageF32Impl& target, const ImageGenF32& lambda, int startX, int startY) {
  96. const int targetWidth = target.width;
  97. const int targetHeight = target.height;
  98. const int targetStride = target.stride;
  99. SafePointer<float> targetRow = imageInternal::getSafeData<float>(target);
  100. for (int y = startY; y < targetHeight + startY; y++) {
  101. SafePointer<float> targetPixel = targetRow;
  102. for (int x = startX; x < targetWidth + startX; x++) {
  103. *targetPixel = lambda(x, y);
  104. targetPixel += 1;
  105. }
  106. targetRow.increaseBytes(targetStride);
  107. }
  108. }
  109. void dsr::filter_mapF32(ImageF32 target, const ImageGenF32& lambda, int startX, int startY) {
  110. if (target.get() != nullptr) {
  111. mapF32(*target, lambda, startX, startY);
  112. }
  113. }
  114. AlignedImageF32 dsr::filter_generateF32(int width, int height, const ImageGenF32& lambda, int startX, int startY) {
  115. AlignedImageF32 result = image_create_F32(width, height);
  116. filter_mapF32(result, lambda, startX, startY);
  117. return result;
  118. }
  119. // -------------------------------- Resize --------------------------------
  120. static OrderedImageRgbaU8 resizeToRef(const ImageRgbaU8Impl& source, Sampler interpolation, int32_t newWidth, int32_t newHeight) {
  121. OrderedImageRgbaU8 resultImage = image_create_RgbaU8(newWidth, newHeight);
  122. imageImpl_resizeToTarget(*resultImage, source, interpolation == Sampler::Linear);
  123. return resultImage;
  124. }
  125. OrderedImageRgbaU8 dsr::filter_resize(ImageRgbaU8 source, Sampler interpolation, int32_t newWidth, int32_t newHeight) {
  126. if (source.get() != nullptr) {
  127. return resizeToRef(*source, interpolation, newWidth, newHeight);
  128. } else {
  129. return OrderedImageRgbaU8(); // Null gives null
  130. }
  131. }
  132. void dsr::filter_blockMagnify(ImageRgbaU8 target, const ImageRgbaU8& source, int pixelWidth, int pixelHeight) {
  133. if (target.get() != nullptr && source.get() != nullptr) {
  134. imageImpl_blockMagnify(*target, *source, pixelWidth, pixelHeight);
  135. }
  136. }