Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix pixel color calculation for different data formats #28

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

mishpajz
Copy link

@mishpajz mishpajz commented Sep 19, 2023

Change the way pixel data is obtained from image in dominantColorFrequencies.

Previously, raw image data was retrieved from CGImage and assumed to be in a RGBA8 format (meaning each pixel has 4 bytes, with R, G, B and A values in this order). However, this is not guaranteed, and if the image was in a different format, the algorithm returned wrong values.

This PR adds conversion from CGImage to CIImage, and uses CIContext to render the image to a bitmap buffer. This ensures that the image data is always in RGBA8 format.

for yCoordonate in 0 ..< cgImage.height {
for xCoordonate in 0 ..< cgImage.width {
let index = (cgImage.width * yCoordonate + xCoordonate) * 4
for yCoordonate in 0 ..< imageHeight {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yCoordonate should be replaced with yCoordinate, xCoordonate should be replaced with xCoordinate

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants