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
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 30 additions & 15 deletions ColorKit/ColorKit/DominantColors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,28 @@ extension UIImage {
throw ImageColorError.cgImageFailure
}

let cfData = cgImage.dataProvider!.data
guard let data = CFDataGetBytePtr(cfData) else {
throw ImageColorError.cgImageDataFailure
}
// ------
// Step 2: Convert the image to RGBA8 data using CIImage
// ------

let ciImage = CIImage(cgImage: cgImage)
let imageWidth = Int(ciImage.extent.width)
let imageHeight = Int(ciImage.extent.height)

let ciContext = CIContext()
let rowBytes = 4 * imageWidth
let dataSize = rowBytes * imageHeight
var data = [UInt8](repeating: 0, count: dataSize)
ciContext.render(
ciImage,
toBitmap: &data,
rowBytes: rowBytes,
bounds: ciImage.extent,
format: .RGBA8,
colorSpace: ciImage.colorSpace)

// ------
// Step 2: Add each pixel to a NSCountedSet. This will give us a count for each color.
// Step 3: Add each pixel to a NSCountedSet. This will give us a count for each color.
// ------

let colorsCountedSet = NSCountedSet(capacity: Int(targetSize.area))
Expand All @@ -151,9 +166,9 @@ extension UIImage {
let B: UInt8
}

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

for xCoordonate in 0 ..< imageWidth {
let index = (imageWidth * yCoordonate + xCoordonate) * 4

// Let's make sure there is enough alpha.
guard data[index + 3] > 150 else { continue }
Expand All @@ -164,7 +179,7 @@ extension UIImage {
}

// ------
// Step 3: Remove colors that are barely present on the image.
// Step 4: Remove colors that are barely present on the image.
// ------

let minCountThreshold = Int(targetSize.area * (0.01 / 100.0))
Expand All @@ -182,22 +197,22 @@ extension UIImage {
}

// ------
// Step 4: Sort the remaning colors by frequency.
// Step 5: Sort the remaning colors by frequency.
// ------

let sortedColorsFrequencies = filteredColorsCountMap.sorted { (lhs, rhs) -> Bool in
return lhs.frequency > rhs.frequency
}

// ------
// Step 5: Only keep the most frequent colors.
// Step 6: Only keep the most frequent colors.
// ------

let maxNumberOfColors = 500
let colorFrequencies = sortedColorsFrequencies.prefix(maxNumberOfColors)

// ------
// Step 6: Combine similar colors together.
// Step 7: Combine similar colors together.
// ------

/// The main dominant colors on the picture.
Expand Down Expand Up @@ -226,23 +241,23 @@ extension UIImage {
}

// ------
// Step 7: Again, limit the number of colors we keep, this time drastically.
// Step 8: Again, limit the number of colors we keep, this time drastically.
// ------

// We only keep the first few dominant colors.
let dominantColorsMaxCount = 8
dominantColors = Array(dominantColors.prefix(dominantColorsMaxCount))

// ------
// Step 8: Sort again on frequencies because the order may have changed because we combined colors.
// Step 9: Sort again on frequencies because the order may have changed because we combined colors.
// ------

dominantColors = dominantColors.sorted(by: { (lhs, rhs) -> Bool in
return lhs.frequency > rhs.frequency
})

// ------
// Step 9: Calculate the frequency of colors as a percentage.
// Step 10: Calculate the frequency of colors as a percentage.
// ------

/// The total count of colors
Expand Down