Skip to content

Commit 2bea5da

Browse files
shoumikhinpytorchbot
authored andcommitted
Fix image processing. (#6153)
Summary: Pull Request resolved: #6153 overriding_review_checks_triggers_an_audit_and_retroactive_review Oncall Short Name: executorch Differential Revision: D64218669 fbshipit-source-id: 63e5aea82d65515977fa3c411e0190683e2cb061 (cherry picked from commit 4204e50)
1 parent e0fcdd4 commit 2bea5da

File tree

1 file changed

+17
-44
lines changed

1 file changed

+17
-44
lines changed

examples/demo-apps/apple_ios/LLaMA/LLaMA/Application/ContentView.swift

Lines changed: 17 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -19,66 +19,39 @@ class RunnerHolder: ObservableObject {
1919
extension UIImage {
2020
func resized(to newSize: CGSize) -> UIImage {
2121
let format = UIGraphicsImageRendererFormat.default()
22-
let renderer = UIGraphicsImageRenderer(size: newSize, format: format)
23-
let image = renderer.image { _ in
24-
draw(in: CGRect(origin: .zero, size: newSize))
22+
format.scale = 1
23+
return UIGraphicsImageRenderer(size: newSize, format: format).image {
24+
_ in draw(in: CGRect(origin: .zero, size: newSize))
2525
}
2626
return image
2727
}
2828

2929
func toRGBArray() -> [UInt8]? {
30-
guard let cgImage = self.cgImage else {
31-
NSLog("Failed to get CGImage from UIImage")
32-
return nil
33-
}
30+
guard let cgImage = self.cgImage else { return nil }
3431

35-
let width = cgImage.width
36-
let height = cgImage.height
37-
let colorSpace = CGColorSpaceCreateDeviceRGB()
38-
let bytesPerPixel = 4
39-
let bytesPerRow = bytesPerPixel * width
40-
let bitsPerComponent = 8
41-
let bitmapInfo = CGImageAlphaInfo.premultipliedLast.rawValue
32+
let width = Int(cgImage.width), height = Int(cgImage.height)
33+
let totalPixels = width * height, bytesPerPixel = 4, bytesPerRow = bytesPerPixel * width
34+
var rgbValues = [UInt8](repeating: 0, count: totalPixels * 3)
35+
var pixelData = [UInt8](repeating: 0, count: width * height * bytesPerPixel)
4236

4337
guard let context = CGContext(
44-
data: nil,
45-
width: width,
46-
height: height,
47-
bitsPerComponent: bitsPerComponent,
48-
bytesPerRow: bytesPerRow,
49-
space: colorSpace,
50-
bitmapInfo: bitmapInfo
51-
) else {
52-
NSLog("Failed to create CGContext")
53-
return nil
54-
}
38+
data: &pixelData, width: width, height: height, bitsPerComponent: 8,
39+
bytesPerRow: bytesPerRow, space: CGColorSpaceCreateDeviceRGB(),
40+
bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue | CGBitmapInfo.byteOrder32Big.rawValue
41+
) else { return nil }
5542

5643
context.draw(cgImage, in: CGRect(x: 0, y: 0, width: width, height: height))
5744

58-
guard let pixelBuffer = context.data else {
59-
NSLog("Failed to get pixel data from CGContext")
60-
return nil
61-
}
62-
63-
let pixelData = pixelBuffer.bindMemory(to: UInt8.self, capacity: width * height * bytesPerPixel)
64-
65-
var rgbArray = [UInt8](repeating: 0, count: width * height * 3)
66-
6745
for y in 0..<height {
6846
for x in 0..<width {
6947
let pixelIndex = (y * width + x) * bytesPerPixel
70-
let r = UInt8(pixelData[pixelIndex])
71-
let g = UInt8(pixelData[pixelIndex + 1])
72-
let b = UInt8(pixelData[pixelIndex + 2])
73-
74-
let rgbIndex = (y * width + x)
75-
rgbArray[rgbIndex] = r
76-
rgbArray[rgbIndex + height * width] = g
77-
rgbArray[rgbIndex + 2 * height * width] = b
48+
let rgbIndex = y * width + x
49+
rgbValues[rgbIndex] = pixelData[pixelIndex]
50+
rgbValues[rgbIndex + totalPixels] = pixelData[pixelIndex + 1]
51+
rgbValues[rgbIndex + totalPixels * 2] = pixelData[pixelIndex + 2]
7852
}
7953
}
80-
81-
return rgbArray
54+
return rgbValues
8255
}
8356
}
8457

0 commit comments

Comments
 (0)