smasherprog / screen_capture_lite

cross platform screen/window capturing library
MIT License
638 stars 156 forks source link

Mac OS example code gave incorrect color order #117

Closed EMCP closed 2 years ago

EMCP commented 2 years ago

I've been working with the coding example (really helps and well done!). the one issue was, on Mac OS the colors were flipped..

I fixed it by negating the logic at this line from https://github.com/smasherprog/screen_capture_lite/blob/master/Example_CPP/Screen_Capture_Example.cpp#L27

 if (img.isContiguous) {
        memcpy(imgdist, imgsrc, dst_size);
    } else { 
        for (auto h = 0; h < Height(img); h++) {
            auto startimgsrc = imgsrc;
            for (auto w = 0; w < Width(img); w++) {
                *imgdist++ = imgsrc->R;
                *imgdist++ = imgsrc->G;
                *imgdist++ = imgsrc->B;
                *imgdist++ = 0; // alpha should be zero
                imgsrc++;
            }
            imgsrc = SL::Screen_Capture::GotoNextRow(img, startimgsrc);
        }
    }

to

 if (!img.isContiguous) {
        memcpy(imgdist, imgsrc, dst_size);
    } else { 
        for (auto h = 0; h < Height(img); h++) {
            auto startimgsrc = imgsrc;
            for (auto w = 0; w < Width(img); w++) {
                *imgdist++ = imgsrc->R;
                *imgdist++ = imgsrc->G;
                *imgdist++ = imgsrc->B;
                *imgdist++ = 0; // alpha should be zero
                imgsrc++;
            }
            imgsrc = SL::Screen_Capture::GotoNextRow(img, startimgsrc);
        }
    }

I am unsure why it is but... the colors are now perfect.. just wanted to share

smasherprog commented 2 years ago

Are you sure its not in some code to display the image? For example, Did you try the example and save the file to disk using the tje_encode_to_file ? Is the color not correct in this case as well?

EMCP commented 2 years ago

I am quite noob to C++, so much so I didn't really dare modify the example otherwise.. especially in the encoder function..

I just took it as-is to start, and saw the solution now from the fix above.... if you want me a do a PR or something to that effect.. so you can test it just lmk.. I didn't want to do that at first and add weird switch or if statements just for OSX though

smasherprog commented 2 years ago

Can you try using the example code and uncommenting the save to file code path. Then open the images up and see if the colors are correct or incorrect? this line here https://github.com/smasherprog/screen_capture_lite/blob/3badb36ec9a7246033898a7de453321e67fb3c3f/Example_CPP/Screen_Capture_Example.cpp#L184

EMCP commented 2 years ago

here's what I uncommented

https://github.com/smasherprog/screen_capture_lite/blob/3badb36ec9a7246033898a7de453321e67fb3c3f/Example_CPP/Screen_Capture_Example.cpp#L178-L186

Attached are the Dock, VS Code.. both of which show reversed Blues and Reds I believe

Screen Shot 2021-11-21 at 12 00 04 Screen Shot 2021-11-21 at 11 59 54

This code was taken straight from master this time, whereas in previous builds I was using a latest tagged version .. just fyi. all exhibit same behavior

smasherprog commented 2 years ago

Hmm yeah ill revisit and check that out. In the mean time, the format is here https://github.com/smasherprog/screen_capture_lite/blob/3badb36ec9a7246033898a7de453321e67fb3c3f/src_cpp/ios/NSFrameProcessor.mm#L20

smasherprog commented 2 years ago

If anyone reads this and uses a mac, can you comment if you are experiencing this or post if colors work as expected.

EMCP commented 2 years ago

If it matters this is seen on a 2018 Mac Mini (Intel), with latest Monterey OS

smasherprog commented 2 years ago

So, thanks for posting on this. I finally dusted off my mac mini and was able to see that this is happening. I also am adding some permission checking code, and will fix this on mac!

smasherprog commented 2 years ago

It appears this was a bug across all platforms, so i updated the example. Thanks for finding this

smasherprog commented 2 years ago

Check out the new code. Should be fixed now in master

EMCP commented 2 years ago

Glad to help where I can, thanks 👍