@@ -20,6 +20,15 @@ public extension NSImage {
2020 return self . size. width
2121 }
2222
23+ var CGImage : CGImage {
24+ get {
25+ let imageData = self . tiffRepresentation!
26+ let source = CGImageSourceCreateWithData ( imageData as CFData , nil ) . unsafelyUnwrapped
27+ let maskRef = CGImageSourceCreateImageAtIndex ( source, Int ( 0 ) , nil )
28+ return maskRef. unsafelyUnwrapped
29+ }
30+ }
31+
2332 /// Returns a png representation of the current image.
2433 var PNGRepresentation : Data ? {
2534 if let tiff = self . tiffRepresentation, let tiffData = NSBitmapImageRep ( data: tiff) {
@@ -59,25 +68,54 @@ public extension NSImage {
5968 throw ScriptError . generalError ( message: " Unable to resize image " )
6069 }
6170
62- func areImagesSame( firstPNGData: Data , secondPNGData: Data ) -> Bool {
63- let sequence = Data ( [ 0x6C , 0x65 , 0x58 , 0x49 , 0x66 ] )
64-
65- guard let firstOffset = firstPNGData. indexOf ( data: sequence) else { return false }
66- let firstSubdata = firstPNGData. subdata ( in: firstOffset ..< firstPNGData. endIndex)
67-
68- guard let secondOffset = secondPNGData. indexOf ( data: sequence) else { return false }
69- let secondSubdata = secondPNGData. subdata ( in: secondOffset ..< secondPNGData. endIndex)
71+ // func areImagesSame(firstPNGData: Data, secondPNGData: Data) -> Bool {
72+ // let sequence = Data([0x6C, 0x65, 0x58, 0x49, 0x66])
73+ //
74+ // guard let firstOffset = firstPNGData.indexOf(data: sequence) else { return false }
75+ // let firstSubdata = firstPNGData.subdata(in: firstOffset ..< firstPNGData.endIndex)
76+ //
77+ // guard let secondOffset = secondPNGData.indexOf(data: sequence) else { return false }
78+ // let secondSubdata = secondPNGData.subdata(in: secondOffset ..< secondPNGData.endIndex)
79+ //
80+ // return firstSubdata == secondSubdata
81+ // }
82+
83+ func areImagesSame( leftImage: NSImage , rightImage: NSImage ) -> Bool {
84+ let width = Int ( leftImage. size. width)
85+ let height = Int ( leftImage. size. height)
86+ guard leftImage. size == rightImage. size else {
87+ return false
88+ }
89+ if let cfData1: CFData = leftImage. CGImage. dataProvider? . data,
90+ let l = CFDataGetBytePtr ( cfData1) ,
91+ let cfData2: CFData = rightImage. CGImage. dataProvider? . data,
92+ let r = CFDataGetBytePtr ( cfData2) {
93+ let bytesPerpixel = 4
94+ let firstPixel = 0
95+ let lastPixel = ( width * height - 1 ) * bytesPerpixel
96+ let range = stride ( from: firstPixel, through: lastPixel, by: bytesPerpixel)
97+ for pixelAddress in range {
98+ if l. advanced ( by: pixelAddress) . pointee != r. advanced ( by: pixelAddress) . pointee || //Red
99+ l. advanced ( by: pixelAddress + 1 ) . pointee != r. advanced ( by: pixelAddress + 1 ) . pointee || //Green
100+ l. advanced ( by: pixelAddress + 2 ) . pointee != r. advanced ( by: pixelAddress + 2 ) . pointee || //Blue
101+ l. advanced ( by: pixelAddress + 3 ) . pointee != r. advanced ( by: pixelAddress + 3 ) . pointee { //Alpha
102+ print ( pixelAddress)
103+
104+ return false
105+ }
106+ }
107+ }
70108
71- return firstSubdata == secondSubdata
72- }
109+ return true
110+ }
73111
74112 /// Saves the PNG representation of the current image to the HD.
75113 ///
76114 /// - parameter url: The location url to which to write the png file.
77115 func savePNGRepresentationToURL( url: URL , onlyChange: Bool = true ) throws {
78116 if let pngData = self . PNGRepresentation {
79- if let originalData = try ? Data ( contentsOf: url) {
80- if onlyChange && areImagesSame ( firstPNGData : pngData , secondPNGData : originalData ) {
117+ if let originalImage = NSImage ( contentsOf: url) {
118+ if onlyChange && areImagesSame ( leftImage : self , rightImage : originalImage ) {
81119 return
82120 }
83121 }
0 commit comments