Skip to content

Commit 30e546f

Browse files
authored
Merge pull request #243 from OpenShot/jonoomph-range-checkpixel
Add new parameter to CheckPixel, for checking "close to" pixel colors
2 parents 2be5e5e + 722d672 commit 30e546f

File tree

3 files changed

+10
-10
lines changed

3 files changed

+10
-10
lines changed

include/Frame.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ namespace openshot
250250
const unsigned char* GetPixels(int row);
251251

252252
/// Check a specific pixel color value (returns True/False)
253-
bool CheckPixel(int row, int col, int red, int green, int blue, int alpha);
253+
bool CheckPixel(int row, int col, int red, int green, int blue, int alpha, int threshold);
254254

255255
/// Get height of image
256256
int GetHeight();

src/Frame.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,7 @@ const unsigned char* Frame::GetPixels(int row)
481481
}
482482

483483
// Check a specific pixel color value (returns True/False)
484-
bool Frame::CheckPixel(int row, int col, int red, int green, int blue, int alpha) {
484+
bool Frame::CheckPixel(int row, int col, int red, int green, int blue, int alpha, int threshold) {
485485
int col_pos = col * 4; // Find column array position
486486
if (!image || row < 0 || row >= (height - 1) ||
487487
col_pos < 0 || col_pos >= (width - 1) ) {
@@ -490,10 +490,10 @@ bool Frame::CheckPixel(int row, int col, int red, int green, int blue, int alpha
490490
}
491491
// Check pixel color
492492
const unsigned char* pixels = GetPixels(row);
493-
if (pixels[col_pos + 0] == red &&
494-
pixels[col_pos + 1] == green &&
495-
pixels[col_pos + 2] == blue &&
496-
pixels[col_pos + 3] == alpha) {
493+
if (pixels[col_pos + 0] >= (red - threshold) && pixels[col_pos + 0] <= (red + threshold) &&
494+
pixels[col_pos + 1] >= (green - threshold) && pixels[col_pos + 1] <= (green + threshold) &&
495+
pixels[col_pos + 2] >= (blue - threshold) && pixels[col_pos + 2] <= (blue + threshold) &&
496+
pixels[col_pos + 3] >= (alpha - threshold) && pixels[col_pos + 3] <= (alpha + threshold)) {
497497
// Pixel color matches successfully
498498
return true;
499499
} else {

tests/FFmpegReader_Tests.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,8 @@ TEST(FFmpegReader_Check_Video_File)
101101
CHECK_EQUAL(255, (int)pixels[pixel_index + 3]);
102102

103103
// Check pixel function
104-
CHECK_EQUAL(true, f->CheckPixel(10, 112, 21, 191, 0, 255));
105-
CHECK_EQUAL(false, f->CheckPixel(10, 112, 0, 0, 0, 0));
104+
CHECK_EQUAL(true, f->CheckPixel(10, 112, 21, 191, 0, 255, 5));
105+
CHECK_EQUAL(false, f->CheckPixel(10, 112, 0, 0, 0, 0, 5));
106106

107107
// Get frame 1
108108
f = r.GetFrame(2);
@@ -118,8 +118,8 @@ TEST(FFmpegReader_Check_Video_File)
118118
CHECK_EQUAL(255, (int)pixels[pixel_index + 3]);
119119

120120
// Check pixel function
121-
CHECK_EQUAL(true, f->CheckPixel(10, 112, 0, 96, 188, 255));
122-
CHECK_EQUAL(false, f->CheckPixel(10, 112, 0, 0, 0, 0));
121+
CHECK_EQUAL(true, f->CheckPixel(10, 112, 0, 96, 188, 255, 5));
122+
CHECK_EQUAL(false, f->CheckPixel(10, 112, 0, 0, 0, 0, 5));
123123

124124
// Close reader
125125
r.Close();

0 commit comments

Comments
 (0)