-1

Edited

Dear Everyone.

My code seems that making you confused.

My question is, is it best to compare at regular intervals to check that the entire screen is black?

I wonder there is another algorithm to check black-screen or there is not?

Another questions, what will be the accuracy of my idea(granularity is 10)?

SUPER Thanks!!

===========================================================================

I am a C language programmer.

I want check black screen algorithm

There is a screen.

The screen consists of a total of 1000 pixels. If the pixel is black, the pixel has a value of zero (0).

The probability that a pixel is black is unknown. This is because I don't know what screen will appear on the screen.

I want to check only a few pixels to confirm if the entire screen is black or not

I think I can get a rough idea of whether the entire screen is black or not. Choose one out of ten to see if it is black or not. Repeat the above.

char pixel[1000];

for (idx = 0; idx < 1000; idx++) {
  if (idx % 10 ==0) {
    if (pixel[idx] == black)
      black_count++;
  }
} 

if (black_count > 0)
  print("This screen black maybe");

I think there is no other way.

If you have any other comments, please answer them.

Thanks.


Dear pritaeas

You say that Possible duplicate of Best way to compare two int arrays of the same length?

But This question is in a different area.

I use only one array.

You are wrong.

Thanks.

  • Possible duplicate of [Best way to compare two int arrays of the same length?](https://stackoverflow.com/questions/9120065/best-way-to-compare-two-int-arrays-of-the-same-length) – pritaeas Jul 04 '18 at 07:58
  • 1
    If the entire screen is black shouldn't `black_count` be equal to `100`. And why can't you just check every pixel? – Ajay Brahmakshatriya Jul 04 '18 at 08:05
  • If you can't afford to check all the pixels and you have no other information regarding the possible entries, then randomization is your best bet. As it is, your algorithm will miss some patterns (and is wrong, as @AjayBrahmakshatriya pointed out). Moreover, you can improve it by exiting the loop as soon as a non-black pixel is found. – Patrice Gahide Jul 04 '18 at 08:14
  • Do not loop 1000 times and then only do something every 10th step. Loop 100 times and multiply counter by 10 to get the index. – Yunnosch Jul 04 '18 at 08:24
  • How many false positives or false negatives are you willing to tolerate. If the answer is "none" you cannot skip any pixel. – Yunnosch Jul 04 '18 at 08:26
  • If you are only checking for non-zero, you could probably use a wider (say 64 bit integer) datatype and check 8 pixels in one operation of same time duration... – Mark Setchell Jul 04 '18 at 09:16

1 Answers1

0

You can view the char[1000] array as a uint_fast8_t[1000 / sizeof(uint_fast8_t)] array because if one of the char values is nonzero, any bigger data type will also be nonzero.

If checking regular intervals works for you, you could try something like this:

#include <stdint.h>
#include <stdbool.h>

char pixel[1000];

int granularity = 10; // Lower: more pixels checked
uint_fast8_t *start = (uint_fast8_t*) pixel;
uint_fast8_t *end = start + (1000 / sizeof(uint_fast8_t) );

bool black = true;

for(; start < end; start += granularity) {
    if(*start) {
        black = false;
        break;
    }
}
tstenner
  • 10,080
  • 10
  • 57
  • 92