|
220 | 220 | guard |
221 | 221 | let thresholdOutputImage = try? deltaOutputImage.applyingThreshold(deltaThreshold), |
222 | 222 | let averagePixel = thresholdOutputImage.applyingAreaAverage().renderSingleValue(in: context) |
223 | | - else { |
| 223 | + else { |
224 | 224 | return "Newly-taken snapshot's data could not be processed." |
225 | 225 | } |
226 | 226 | actualPixelPrecision = 1 - averagePixel |
|
259 | 259 | } |
260 | 260 | } |
261 | 261 | } |
262 | | - let failingPixelPercent = Float(failingPixelCount) / Float(deltaOutputImage.extent.width * deltaOutputImage.extent.height) |
| 262 | + let failingPixelPercent = |
| 263 | + Float(failingPixelCount) |
| 264 | + / Float(deltaOutputImage.extent.width * deltaOutputImage.extent.height) |
263 | 265 | actualPixelPrecision = 1 - failingPixelPercent |
264 | 266 | } |
265 | 267 |
|
|
268 | 270 | // DeltaE is in a 0-100 scale, so we need to divide by 100 to transform it to a percentage. |
269 | 271 | let minimumPerceptualPrecision = 1 - min(maximumDeltaE / 100, 1) |
270 | 272 | return """ |
271 | | - The percentage of pixels that match \(actualPixelPrecision) is less than required \(pixelPrecision) |
272 | | - The lowest perceptual color precision \(minimumPerceptualPrecision) is less than required \(perceptualPrecision) |
273 | | - """ |
| 273 | + The percentage of pixels that match \(actualPixelPrecision) is less than required \(pixelPrecision) |
| 274 | + The lowest perceptual color precision \(minimumPerceptualPrecision) is less than required \(perceptualPrecision) |
| 275 | + """ |
274 | 276 | } |
275 | 277 |
|
276 | 278 | extension CIImage { |
|
295 | 297 | } |
296 | 298 |
|
297 | 299 | func renderSingleValue(in context: CIContext) -> Float? { |
298 | | - guard let buffer = render(in: context) else { return nil } |
299 | | - defer { buffer.free() } |
300 | | - return buffer.data.load(fromByteOffset: 0, as: Float.self) |
| 300 | + guard let buffer = render(in: context) else { return nil } |
| 301 | + defer { buffer.free() } |
| 302 | + return buffer.data.load(fromByteOffset: 0, as: Float.self) |
301 | 303 | } |
302 | 304 |
|
303 | 305 | func render(in context: CIContext, format: CIFormat = CIFormat.Rh) -> vImage_Buffer? { |
304 | 306 | // Some hardware configurations (virtualized CPU renderers) do not support 32-bit float output formats, |
305 | 307 | // so use a compatible 16-bit float format and convert the output value to 32-bit floats. |
306 | | - guard var buffer16 = try? vImage_Buffer(width: Int(extent.width), height: Int(extent.height), bitsPerPixel: 16) else { return nil } |
| 308 | + guard |
| 309 | + var buffer16 = try? vImage_Buffer( |
| 310 | + width: Int(extent.width), height: Int(extent.height), bitsPerPixel: 16) |
| 311 | + else { return nil } |
307 | 312 | defer { buffer16.free() } |
308 | 313 | context.render( |
309 | 314 | self, |
|
314 | 319 | colorSpace: nil |
315 | 320 | ) |
316 | 321 | guard |
317 | | - var buffer32 = try? vImage_Buffer(width: Int(buffer16.width), height: Int(buffer16.height), bitsPerPixel: 32), |
| 322 | + var buffer32 = try? vImage_Buffer( |
| 323 | + width: Int(buffer16.width), height: Int(buffer16.height), bitsPerPixel: 32), |
318 | 324 | vImageConvert_Planar16FtoPlanarF(&buffer16, &buffer32, 0) == kvImageNoError |
319 | 325 | else { return nil } |
320 | 326 | return buffer32 |
|
0 commit comments