Skip to content

Commit 401ea8e

Browse files
committed
Do the same with to_blackwhite. Change order of parameters using dummy variable for safety.
1 parent 31b4de6 commit 401ea8e

10 files changed

+113
-33
lines changed

SerialPrograms/Source/CommonTools/Images/ImageFilter.cpp

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "Kernels/ImageFilters/Kernels_ImageFilter_Basic.h"
99
#include "Kernels/ImageFilters/RGB32_Range/Kernels_ImageFilter_RGB32_Range.h"
1010
#include "Kernels/ImageFilters/RGB32_EuclideanDistance/Kernels_ImageFilter_RGB32_Euclidean.h"
11+
#include "Kernels/ImageFilters/RGB32_Brightness/Kernels_ImageFilter_RGB32_Brightness.h"
1112
#include "CommonFramework/ImageTypes/ImageViewRGB32.h"
1213
#include "CommonFramework/ImageTypes/ImageRGB32.h"
1314
#include "ImageFilter.h"
@@ -114,7 +115,9 @@ ImageRGB32 to_blackwhite_rgb32_range(
114115
ImageRGB32 ret(image.width(), image.height());
115116
Kernels::to_blackwhite_rgb32_range(
116117
image.data(), image.bytes_per_row(), image.width(), image.height(),
117-
ret.data(), ret.bytes_per_row(), mins, maxs, in_range_black
118+
ret.data(), ret.bytes_per_row(),
119+
in_range_black,
120+
mins, maxs, nullptr
118121
);
119122
return ret;
120123
}
@@ -126,7 +129,9 @@ ImageRGB32 to_blackwhite_rgb32_range(
126129
ImageRGB32 ret(image.width(), image.height());
127130
pixels_in_range = Kernels::to_blackwhite_rgb32_range(
128131
image.data(), image.bytes_per_row(), image.width(), image.height(),
129-
ret.data(), ret.bytes_per_row(), mins, maxs, in_range_black
132+
ret.data(), ret.bytes_per_row(),
133+
in_range_black,
134+
mins, maxs, nullptr
130135
);
131136
return ret;
132137
}
@@ -154,6 +159,25 @@ std::vector<std::pair<ImageRGB32, size_t>> to_blackwhite_rgb32_range(
154159
return ret;
155160
}
156161

162+
163+
164+
#if 0
165+
ImageRGB32 to_blackwhite_rgb32_brightness(
166+
const ImageViewRGB32& image,
167+
bool in_range_black,
168+
uint32_t min_brightness, uint32_t max_brightness
169+
){
170+
ImageRGB32 ret(image.width(), image.height());
171+
Kernels::to_blackwhite_rgb32_brightness(
172+
image.data(), image.bytes_per_row(), image.width(), image.height(),
173+
ret.data(), ret.bytes_per_row(), mins, maxs, in_range_black
174+
);
175+
return ret;
176+
}
177+
#endif
178+
179+
180+
157181
ImageRGB32 filter_green(
158182
const ImageViewRGB32& image,
159183
Color replace_with,

SerialPrograms/Source/CommonTools/Images/ImageFilter.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,18 @@ std::vector<std::pair<ImageRGB32, size_t>> to_blackwhite_rgb32_range(
119119
);
120120

121121

122+
// Similar to "to_blackwhite_rgb32_range()". But instead of checking if each of
123+
// the RGB components are within a range, we check if the sum of them
124+
// (the brightness) is in range.
125+
ImageRGB32 to_blackwhite_rgb32_brightness(
126+
const ImageViewRGB32& image,
127+
bool in_range_black,
128+
uint32_t min_brightness, uint32_t max_brightness
129+
);
130+
131+
132+
133+
122134
// keep all pixels where green is the dominant RGB value. otherwise, replace the pixel with `replace_with`
123135
// `rgb_gap` is the amount that green has to exceed the red or blue value, in order to keep the pixel.
124136
ImageRGB32 filter_green(

SerialPrograms/Source/Kernels/ImageFilters/RGB32_Range/Kernels_ImageFilter_RGB32_Range.cpp

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -167,54 +167,85 @@ void filter_rgb32_range(
167167
size_t to_blackwhite_rgb32_range_Default(
168168
const uint32_t* in, size_t in_bytes_per_row, size_t width, size_t height,
169169
uint32_t* out, size_t out_bytes_per_row,
170-
uint32_t mins, uint32_t maxs, bool in_range_black
170+
bool in_range_black,
171+
uint32_t mins, uint32_t maxs, void*
171172
);
172173
size_t to_blackwhite_rgb32_range_x64_SSE42(
173174
const uint32_t* in, size_t in_bytes_per_row, size_t width, size_t height,
174175
uint32_t* out, size_t out_bytes_per_row,
175-
uint32_t mins, uint32_t maxs, bool in_range_black
176+
bool in_range_black,
177+
uint32_t mins, uint32_t maxs, void*
176178
);
177179
size_t to_blackwhite_rgb32_range_x64_AVX2(
178180
const uint32_t* in, size_t in_bytes_per_row, size_t width, size_t height,
179181
uint32_t* out, size_t out_bytes_per_row,
180-
uint32_t mins, uint32_t maxs, bool in_range_black
182+
bool in_range_black,
183+
uint32_t mins, uint32_t maxs, void*
181184
);
182185
size_t to_blackwhite_rgb32_range_x64_AVX512(
183186
const uint32_t* in, size_t in_bytes_per_row, size_t width, size_t height,
184187
uint32_t* out, size_t out_bytes_per_row,
185-
uint32_t mins, uint32_t maxs, bool in_range_black
188+
bool in_range_black,
189+
uint32_t mins, uint32_t maxs, void*
186190
);
187191
size_t to_blackwhite_rgb32_range_arm64_NEON(
188192
const uint32_t* in, size_t in_bytes_per_row, size_t width, size_t height,
189193
uint32_t* out, size_t out_bytes_per_row,
190-
uint32_t mins, uint32_t maxs, bool in_range_black
194+
bool in_range_black,
195+
uint32_t mins, uint32_t maxs, void*
191196
);
192197
size_t to_blackwhite_rgb32_range(
193198
const uint32_t* in, size_t in_bytes_per_row, size_t width, size_t height,
194199
uint32_t* out, size_t out_bytes_per_row,
195-
uint32_t mins, uint32_t maxs, bool in_range_black
200+
bool in_range_black,
201+
uint32_t mins, uint32_t maxs, void*
196202
){
197203
#ifdef PA_AutoDispatch_x64_17_Skylake
198204
if (CPU_CAPABILITY_CURRENT.OK_17_Skylake){
199-
return to_blackwhite_rgb32_range_x64_AVX512(in, in_bytes_per_row, width, height, out, out_bytes_per_row, mins, maxs, in_range_black);
205+
return to_blackwhite_rgb32_range_x64_AVX512(
206+
in, in_bytes_per_row, width, height,
207+
out, out_bytes_per_row,
208+
in_range_black,
209+
mins, maxs, nullptr
210+
);
200211
}
201212
#endif
202213
#ifdef PA_AutoDispatch_x64_13_Haswell
203214
if (CPU_CAPABILITY_CURRENT.OK_13_Haswell){
204-
return to_blackwhite_rgb32_range_x64_AVX2(in, in_bytes_per_row, width, height, out, out_bytes_per_row, mins, maxs, in_range_black);
215+
return to_blackwhite_rgb32_range_x64_AVX2(
216+
in, in_bytes_per_row, width, height,
217+
out, out_bytes_per_row,
218+
in_range_black,
219+
mins, maxs, nullptr
220+
);
205221
}
206222
#endif
207223
#ifdef PA_AutoDispatch_x64_08_Nehalem
208224
if (CPU_CAPABILITY_CURRENT.OK_08_Nehalem){
209-
return to_blackwhite_rgb32_range_x64_SSE42(in, in_bytes_per_row, width, height, out, out_bytes_per_row, mins, maxs, in_range_black);
225+
return to_blackwhite_rgb32_range_x64_SSE42(
226+
in, in_bytes_per_row, width, height,
227+
out, out_bytes_per_row,
228+
in_range_black,
229+
mins, maxs, nullptr
230+
);
210231
}
211232
#endif
212233
#ifdef PA_AutoDispatch_arm64_20_M1
213234
if (CPU_CAPABILITY_CURRENT.OK_M1){
214-
return to_blackwhite_rgb32_range_arm64_NEON(in, in_bytes_per_row, width, height, out, out_bytes_per_row, mins, maxs, in_range_black);
235+
return to_blackwhite_rgb32_range_arm64_NEON(
236+
in, in_bytes_per_row, width, height,
237+
out, out_bytes_per_row,
238+
in_range_black,
239+
mins, maxs, nullptr
240+
);
215241
}
216242
#endif
217-
return to_blackwhite_rgb32_range_Default(in, in_bytes_per_row, width, height, out, out_bytes_per_row, mins, maxs, in_range_black);
243+
return to_blackwhite_rgb32_range_Default(
244+
in, in_bytes_per_row, width, height,
245+
out, out_bytes_per_row,
246+
in_range_black,
247+
mins, maxs, nullptr
248+
);
218249
}
219250

220251

SerialPrograms/Source/Kernels/ImageFilters/RGB32_Range/Kernels_ImageFilter_RGB32_Range.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,10 @@ void filter_rgb32_range(
6666

6767

6868
size_t to_blackwhite_rgb32_range(
69-
const uint32_t* image, size_t bytes_per_row, size_t width, size_t height,
70-
uint32_t* out0, size_t bytes_per_row0, uint32_t mins0, uint32_t maxs0, bool in_range_black0
69+
const uint32_t* in, size_t in_bytes_per_row, size_t width, size_t height,
70+
uint32_t* out, size_t out_bytes_per_row,
71+
bool in_range_black,
72+
uint32_t mins, uint32_t maxs, void*
7173
);
7274

7375

SerialPrograms/Source/Kernels/ImageFilters/RGB32_Range/Kernels_ImageFilter_RGB32_Range_ARM64_NEON.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ class ToBlackWhite_RgbRange_arm64_NEON{
119119
using Mask = size_t;
120120

121121
public:
122-
ToBlackWhite_RgbRange_arm64_NEON(uint32_t mins, uint32_t maxs, bool in_range_black)
122+
ToBlackWhite_RgbRange_arm64_NEON(bool in_range_black, uint32_t mins, uint32_t maxs)
123123
: m_mins_u8(vreinterpretq_u8_u32(vdupq_n_u32(mins)))
124124
, m_maxs_u8(vreinterpretq_u8_u32(vdupq_n_u32(maxs)))
125125
, m_zeros_u8(vreinterpretq_u32_u8(vdupq_n_u8(0)))
@@ -177,9 +177,10 @@ class ToBlackWhite_RgbRange_arm64_NEON{
177177
size_t to_blackwhite_rgb32_range_arm64_NEON(
178178
const uint32_t* in, size_t in_bytes_per_row, size_t width, size_t height,
179179
uint32_t* out, size_t out_bytes_per_row,
180-
uint32_t mins, uint32_t maxs, bool in_range_black
180+
bool in_range_black,
181+
uint32_t mins, uint32_t maxs, void*
181182
){
182-
ToBlackWhite_RgbRange_arm64_NEON filter(mins, maxs, in_range_black);
183+
ToBlackWhite_RgbRange_arm64_NEON filter(in_range_black, mins, maxs);
183184
filter_per_pixel(in, in_bytes_per_row, width, height, filter, out, out_bytes_per_row);
184185
return filter.count();
185186
}

SerialPrograms/Source/Kernels/ImageFilters/RGB32_Range/Kernels_ImageFilter_RGB32_Range_Default.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ class ToBlackWhite_RgbRange_Default{
120120
using Mask = size_t;
121121

122122
public:
123-
ToBlackWhite_RgbRange_Default(uint32_t mins, uint32_t maxs, bool in_range_black)
123+
ToBlackWhite_RgbRange_Default(bool in_range_black, uint32_t mins, uint32_t maxs)
124124
: m_in_range_black(in_range_black ? 1 : 0)
125125
, m_minB(mins & 0x000000ff)
126126
, m_maxB(maxs & 0x000000ff)
@@ -187,9 +187,10 @@ class ToBlackWhite_RgbRange_Default{
187187
size_t to_blackwhite_rgb32_range_Default(
188188
const uint32_t* image, size_t in_bytes_per_row, size_t width, size_t height,
189189
uint32_t* out, size_t out_bytes_per_row,
190-
uint32_t mins, uint32_t maxs, bool in_range_black
190+
bool in_range_black,
191+
uint32_t mins, uint32_t maxs, void*
191192
){
192-
ToBlackWhite_RgbRange_Default filter(mins, maxs, in_range_black);
193+
ToBlackWhite_RgbRange_Default filter(in_range_black, mins, maxs);
193194
filter_per_pixel(image, in_bytes_per_row, width, height, filter, out, out_bytes_per_row);
194195
return filter.count();
195196
}

SerialPrograms/Source/Kernels/ImageFilters/RGB32_Range/Kernels_ImageFilter_RGB32_Range_x64_AVX2.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ class ToBlackWhite_RgbRange_x64_AVX2{
107107
using Mask = PartialWordAccess32_x64_AVX2;
108108

109109
public:
110-
ToBlackWhite_RgbRange_x64_AVX2(uint32_t mins, uint32_t maxs, bool in_range_black)
110+
ToBlackWhite_RgbRange_x64_AVX2(bool in_range_black, uint32_t mins, uint32_t maxs)
111111
: m_in_range_black(in_range_black ? _mm256_set1_epi32(-1) : _mm256_setzero_si256())
112112
, m_mins(_mm256_set1_epi32(mins ^ 0x80808080))
113113
, m_maxs(_mm256_set1_epi32(maxs ^ 0x80808080))
@@ -154,9 +154,10 @@ class ToBlackWhite_RgbRange_x64_AVX2{
154154
size_t to_blackwhite_rgb32_range_x64_AVX2(
155155
const uint32_t* in, size_t in_bytes_per_row, size_t width, size_t height,
156156
uint32_t* out, size_t out_bytes_per_row,
157-
uint32_t mins, uint32_t maxs, bool in_range_black
157+
bool in_range_black,
158+
uint32_t mins, uint32_t maxs, void*
158159
){
159-
ToBlackWhite_RgbRange_x64_AVX2 filter(mins, maxs, in_range_black);
160+
ToBlackWhite_RgbRange_x64_AVX2 filter(in_range_black, mins, maxs);
160161
filter_per_pixel(in, in_bytes_per_row, width, height, filter, out, out_bytes_per_row);
161162
return filter.count();
162163
}

SerialPrograms/Source/Kernels/ImageFilters/RGB32_Range/Kernels_ImageFilter_RGB32_Range_x64_AVX512.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ class ToBlackWhite_RgbRange_x64_AVX512{
123123
using Mask = PartialWordMask;
124124

125125
public:
126-
ToBlackWhite_RgbRange_x64_AVX512(uint32_t mins, uint32_t maxs, bool in_range_black)
126+
ToBlackWhite_RgbRange_x64_AVX512(bool in_range_black, uint32_t mins, uint32_t maxs)
127127
: m_in_range_black(in_range_black ? 0xffff : 0)
128128
, m_mins(_mm512_set1_epi32(mins))
129129
, m_maxs(_mm512_set1_epi32(maxs))
@@ -176,9 +176,10 @@ class ToBlackWhite_RgbRange_x64_AVX512{
176176
size_t to_blackwhite_rgb32_range_x64_AVX512(
177177
const uint32_t* in, size_t in_bytes_per_row, size_t width, size_t height,
178178
uint32_t* out, size_t out_bytes_per_row,
179-
uint32_t mins, uint32_t maxs, bool in_range_black
179+
bool in_range_black,
180+
uint32_t mins, uint32_t maxs, void*
180181
){
181-
ToBlackWhite_RgbRange_x64_AVX512 filter(mins, maxs, in_range_black);
182+
ToBlackWhite_RgbRange_x64_AVX512 filter(in_range_black, mins, maxs);
182183
filter_per_pixel(in, in_bytes_per_row, width, height, filter, out, out_bytes_per_row);
183184
return filter.count();
184185
}

SerialPrograms/Source/Kernels/ImageFilters/RGB32_Range/Kernels_ImageFilter_RGB32_Range_x64_SSE42.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ class ToBlackWhite_RgbRange_x64_SSE42{
128128
using Mask = PartialWordMask;
129129

130130
public:
131-
ToBlackWhite_RgbRange_x64_SSE42(uint32_t mins, uint32_t maxs, bool in_range_black)
131+
ToBlackWhite_RgbRange_x64_SSE42(bool in_range_black, uint32_t mins, uint32_t maxs)
132132
: m_in_range_black(in_range_black ? _mm_set1_epi32(-1) : _mm_setzero_si128())
133133
, m_mins(_mm_set1_epi32(mins ^ 0x80808080))
134134
, m_maxs(_mm_set1_epi32(maxs ^ 0x80808080))
@@ -180,9 +180,10 @@ class ToBlackWhite_RgbRange_x64_SSE42{
180180
size_t to_blackwhite_rgb32_range_x64_SSE42(
181181
const uint32_t* in, size_t in_bytes_per_row, size_t width, size_t height,
182182
uint32_t* out, size_t out_bytes_per_row,
183-
uint32_t mins, uint32_t maxs, bool in_range_black
183+
bool in_range_black,
184+
uint32_t mins, uint32_t maxs, void*
184185
){
185-
ToBlackWhite_RgbRange_x64_SSE42 filter(mins, maxs, in_range_black);
186+
ToBlackWhite_RgbRange_x64_SSE42 filter(in_range_black, mins, maxs);
186187
filter_per_pixel(in, in_bytes_per_row, width, height, filter, out, out_bytes_per_row);
187188
return filter.count();
188189
}

SerialPrograms/Source/Tests/Kernels_Tests.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,9 @@ int test_kernels_ToBlackWhiteRGB32Range(const ImageViewRGB32& image){
373373
// auto new_image = filter_rgb32_range(image, mins, maxs, COLOR_WHITE, replace_color_within_range);
374374
pixels_in_range = Kernels::to_blackwhite_rgb32_range(
375375
image.data(), image.bytes_per_row(), image.width(), image.height(),
376-
image_out.data(), image_out.bytes_per_row(), mins, maxs, in_range_black
376+
image_out.data(), image_out.bytes_per_row(),
377+
in_range_black,
378+
mins, maxs, nullptr
377379
);
378380
auto time_end = current_time();
379381
auto ns = std::chrono::duration_cast<std::chrono::nanoseconds>(time_end - time_start).count();
@@ -382,7 +384,9 @@ int test_kernels_ToBlackWhiteRGB32Range(const ImageViewRGB32& image){
382384

383385
size_t pixels_in_range_2 = Kernels::to_blackwhite_rgb32_range(
384386
image.data(), image.bytes_per_row(), image.width(), image.height(),
385-
image_out_2.data(), image_out_2.bytes_per_row(), mins, maxs, !in_range_black
387+
image_out_2.data(), image_out_2.bytes_per_row(),
388+
!in_range_black,
389+
mins, maxs, nullptr
386390
);
387391

388392
TEST_RESULT_EQUAL(pixels_in_range, pixels_in_range_2);
@@ -439,7 +443,9 @@ int test_kernels_ToBlackWhiteRGB32Range(const ImageViewRGB32& image){
439443
for(size_t i = 0; i < num_iters; i++){
440444
Kernels::to_blackwhite_rgb32_range(
441445
image.data(), image.bytes_per_row(), image.width(), image.height(),
442-
image_out.data(), image_out.bytes_per_row(), mins, maxs, in_range_black
446+
image_out.data(), image_out.bytes_per_row(),
447+
in_range_black,
448+
mins, maxs, nullptr
443449
);
444450
}
445451
time_end = current_time();

0 commit comments

Comments
 (0)