Skip to content

Commit

Permalink
CGImage.resizedImage was broken for grayscale images
Browse files Browse the repository at this point in the history
bitsPerComponent and pixel were hardcoded to 8/32, and colorSpace was empty. Works fine for RGB and ARGB but not for e.g. grayscale. Instead, we now pull this info from the CGImage we're passing anyway.

This fixes the test `testResizeGrayscaleImage` and therefore fixes #30.
  • Loading branch information
Sören Kuklau committed Nov 30, 2022
1 parent 8d3b3c7 commit e506ac4
Showing 1 changed file with 17 additions and 15 deletions.
32 changes: 17 additions & 15 deletions CoreTootin/Extensions/CGImage+Additions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
// GNU General Public License for more details.
//

import Foundation
import Accelerate
import Foundation

public extension CGImage
{
Expand All @@ -29,27 +29,28 @@ public extension CGImage

func resizedImage(newHeight: CGFloat, scale: CGFloat = 1.0) -> CGImage?
{
let newWidth = (newHeight/CGFloat(height)) * CGFloat(width)
return resizedImage(newSize: CGSize(width: newWidth, height: newHeight), scale: scale)
let newWidth = (newHeight / CGFloat(height)) * CGFloat(width)
return self.resizedImage(newSize: CGSize(width: newWidth, height: newHeight), scale: scale)
}

func resizedImage(newSize: CGSize, scale: CGFloat = 1.0) -> CGImage?
{
var format = vImage_CGImageFormat(bitsPerComponent: 8,
bitsPerPixel: 32,
colorSpace: nil,
bitmapInfo: bitmapInfo,
version: 0, decode: nil, renderingIntent: .defaultIntent)
var format = vImage_CGImageFormat(bitsPerComponent: UInt32(self.bitsPerComponent),
bitsPerPixel: UInt32(self.bitsPerPixel),
colorSpace: Unmanaged.passUnretained(self.colorSpace!),
bitmapInfo: self.bitmapInfo,
version: 0, decode: nil, renderingIntent: .defaultIntent)

var sourceBuffer = vImage_Buffer()
defer
{
sourceBuffer.data.deallocate()
sourceBuffer.data?.deallocate()
}

var error: Int = kvImageNoError
error = vImageBuffer_InitWithCGImage(&sourceBuffer, &format, nil, self, numericCast(kvImageNoFlags))
guard error == kvImageNoError else
guard error == kvImageNoError
else
{
return nil
}
Expand All @@ -65,9 +66,9 @@ public extension CGImage
}

var destBuffer = vImage_Buffer(data: destData,
height: vImagePixelCount(newHeight),
width: vImagePixelCount(newWidth),
rowBytes: destBytesPerRow)
height: vImagePixelCount(newHeight),
width: vImagePixelCount(newWidth),
rowBytes: destBytesPerRow)

if colorSpace?.model == .rgb
{
Expand All @@ -82,9 +83,10 @@ public extension CGImage

error = kvImageNoError
let resizedImageReference = vImageCreateCGImageFromBuffer(&destBuffer, &format, nil,
nil, numericCast(kvImageNoFlags), &error)
nil, numericCast(kvImageNoFlags), &error)

guard error == kvImageNoError, let resizedImage = resizedImageReference?.takeRetainedValue() else
guard error == kvImageNoError, let resizedImage = resizedImageReference?.takeRetainedValue()
else
{
return nil
}
Expand Down

0 comments on commit e506ac4

Please sign in to comment.