|
@@ -7,6 +7,7 @@
|
|
|
|
|
|
import Foundation
|
|
|
import AppKit
|
|
|
+import CoreImage
|
|
|
|
|
|
class ImageProcess : NSObject {
|
|
|
|
|
@@ -28,18 +29,34 @@ class ImageProcess : NSObject {
|
|
|
let checkImage = cImage!
|
|
|
|
|
|
let resultImageRep = NSBitmapImageRep.init(cgImage: resultImage.cgImage(forProposedRect: nil, context: nil, hints: nil)!)
|
|
|
- let checkImageRep = NSBitmapImageRep.init(cgImage: checkImage.cgImage(forProposedRect: nil, context: nil, hints: nil)!)
|
|
|
+ var checkImageRep = NSBitmapImageRep.init(cgImage: checkImage.cgImage(forProposedRect: nil, context: nil, hints: nil)!)
|
|
|
|
|
|
let rWidth = resultImageRep.pixelsWide
|
|
|
let rHeight = resultImageRep.pixelsHigh
|
|
|
// let rBitPerPixel = resultImageRep.bitsPerPixel / 8
|
|
|
// let rBytePerRow = resultImageRep.bytesPerRow
|
|
|
|
|
|
- let cWidth = checkImageRep.pixelsWide
|
|
|
- let cHeight = checkImageRep.pixelsHigh
|
|
|
+ var cWidth = checkImageRep.pixelsWide
|
|
|
+ var cHeight = checkImageRep.pixelsHigh
|
|
|
// let cBitPerPixel = checkImageRep.bitsPerPixel / 8
|
|
|
// let cBytePerRow = checkImageRep.bytesPerRow
|
|
|
|
|
|
+ // 图像为等比例、且长宽比相同
|
|
|
+ if rHeight*cHeight > 0 &&
|
|
|
+ fabs(Double(rWidth-cWidth)) > 1 &&
|
|
|
+ fabs(Double(rWidth)/Double(rHeight) - Double(cWidth)/Double(cHeight)) < 0.001 {
|
|
|
+ var ciImage = CIImage(cgImage: checkImage.cgImage(forProposedRect: nil, context: nil, hints: nil)!)
|
|
|
+ ciImage = ciImage.transformed(by: CGAffineTransformMakeScale(Double(rWidth)/Double(cWidth), Double(rHeight)/Double(cHeight)))
|
|
|
+ let context = CIContext()
|
|
|
+ let cgImage = context.createCGImage(ciImage, from: ciImage.extent)
|
|
|
+ if cgImage != nil {
|
|
|
+ checkImageRep = NSBitmapImageRep(cgImage: cgImage!);
|
|
|
+
|
|
|
+ cWidth = checkImageRep.pixelsWide
|
|
|
+ cHeight = checkImageRep.pixelsHigh
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
let maxWidth = min(rWidth, cWidth) - 1
|
|
|
let maxHeight = min(rHeight, cHeight) - 1
|
|
|
|
|
@@ -203,25 +220,41 @@ class ImageProcess : NSObject {
|
|
|
}
|
|
|
|
|
|
let resultImage = rImage!
|
|
|
- let checkImage = cImage!
|
|
|
+ var checkImage = cImage!
|
|
|
|
|
|
let resultImageRep = NSBitmapImageRep.init(cgImage: resultImage.cgImage(forProposedRect: nil, context: nil, hints: nil)!)
|
|
|
- let checkImageRep = NSBitmapImageRep.init(cgImage: checkImage.cgImage(forProposedRect: nil, context: nil, hints: nil)!)
|
|
|
+ var checkImageRep = NSBitmapImageRep.init(cgImage: checkImage.cgImage(forProposedRect: nil, context: nil, hints: nil)!)
|
|
|
|
|
|
let rWidth = resultImageRep.pixelsWide
|
|
|
let rHeight = resultImageRep.pixelsHigh
|
|
|
// let rBitPerPixel = resultImageRep.bitsPerPixel / 8
|
|
|
// let rBytePerRow = resultImageRep.bytesPerRow
|
|
|
|
|
|
- let cWidth = checkImageRep.pixelsWide
|
|
|
- let cHeight = checkImageRep.pixelsHigh
|
|
|
+ var cWidth = checkImageRep.pixelsWide
|
|
|
+ var cHeight = checkImageRep.pixelsHigh
|
|
|
// let cBitPerPixel = checkImageRep.bitsPerPixel / 8
|
|
|
// let cBytePerRow = checkImageRep.bytesPerRow
|
|
|
|
|
|
+ // 图像为等比例、且长宽比相同
|
|
|
+ if rHeight*cHeight > 0 &&
|
|
|
+ fabs(Double(rWidth-cWidth)) > 1 &&
|
|
|
+ fabs(Double(rWidth)/Double(rHeight) - Double(cWidth)/Double(cHeight)) < 0.001 {
|
|
|
+ var ciImage = CIImage(cgImage: checkImage.cgImage(forProposedRect: nil, context: nil, hints: nil)!)
|
|
|
+ ciImage = ciImage.transformed(by: CGAffineTransformMakeScale(Double(rWidth)/Double(cWidth), Double(rHeight)/Double(cHeight)))
|
|
|
+ let context = CIContext()
|
|
|
+ let cgImage = context.createCGImage(ciImage, from: ciImage.extent)
|
|
|
+ if cgImage != nil {
|
|
|
+ checkImageRep = NSBitmapImageRep(cgImage: cgImage!);
|
|
|
+
|
|
|
+ cWidth = checkImageRep.pixelsWide
|
|
|
+ cHeight = checkImageRep.pixelsHigh
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
let maxWidth = min(rWidth, cWidth) - 1
|
|
|
let maxHeight = min(rHeight, cHeight) - 1
|
|
|
|
|
|
- if (maxWidth == 0) {
|
|
|
+ if (maxWidth == 0 || maxHeight == 0) {
|
|
|
complention(-1)
|
|
|
return
|
|
|
}
|
|
@@ -346,9 +379,11 @@ class ImageProcess : NSObject {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- NSLog(String("过滤点数目\(bgCount)"))
|
|
|
-
|
|
|
- complention(outDegree)
|
|
|
+ DispatchQueue.global().async {
|
|
|
+ NSLog(String("过滤点数目\(bgCount)"))
|
|
|
+
|
|
|
+ complention(outDegree)
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|