filterAPI.cpp 6.2 KB

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