Skip to content

Commit 13e74b1

Browse files
committed
Adding new CheckPixel method to validate a specific pixel color
1 parent 85d6ac6 commit 13e74b1

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

include/Frame.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,9 @@ namespace openshot
249249
/// Get pixel data (for only a single scan-line)
250250
const unsigned char* GetPixels(int row);
251251

252+
/// Check a specific pixel color value (returns True/False)
253+
bool CheckPixel(int row, int col, int red, int green, int blue, int alpha);
254+
252255
/// Get height of image
253256
int GetHeight();
254257

src/Frame.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,28 @@ const unsigned char* Frame::GetPixels(int row)
480480
return image->scanLine(row);
481481
}
482482

483+
// 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) {
485+
int col_pos = col * 4; // Find column array position
486+
if (!image || row < 0 || row >= (height - 1) ||
487+
col_pos < 0 || col_pos >= (width - 1) ) {
488+
// invalid row / col
489+
return false;
490+
}
491+
// Check pixel color
492+
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) {
497+
// Pixel color matches successfully
498+
return true;
499+
} else {
500+
// Pixel color does not match
501+
return false;
502+
}
503+
}
504+
483505
// Set Pixel Aspect Ratio
484506
void Frame::SetPixelRatio(int num, int den)
485507
{

tests/FFmpegReader_Tests.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ TEST(FFmpegReader_Check_Video_File)
100100
CHECK_EQUAL(0, (int)pixels[pixel_index + 2]);
101101
CHECK_EQUAL(255, (int)pixels[pixel_index + 3]);
102102

103+
// 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));
106+
103107
// Get frame 1
104108
f = r.GetFrame(2);
105109

@@ -113,6 +117,10 @@ TEST(FFmpegReader_Check_Video_File)
113117
CHECK_EQUAL(188, (int)pixels[pixel_index + 2]);
114118
CHECK_EQUAL(255, (int)pixels[pixel_index + 3]);
115119

120+
// 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));
123+
116124
// Close reader
117125
r.Close();
118126
}

0 commit comments

Comments
 (0)