-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
141 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
Sources/ZPLBuilder/Utils/ZPLImageEncoder/ZPLCGImageReader.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// | ||
// ZPLCGImageReader.swift | ||
// | ||
// | ||
// Created by chen on 2024/5/4. | ||
// | ||
|
||
#if canImport(CoreGraphics) | ||
import Foundation | ||
import CoreGraphics | ||
import OSLog | ||
|
||
public struct ZPLCGImageReader: ZPLImageReader { | ||
private let imageData: CFData | ||
private let imagePtr: UnsafePointer<UInt8> | ||
private let bytesPerPixel: Int | ||
private let bytesPerRow: Int | ||
|
||
public var width: Int | ||
public var height: Int | ||
|
||
public init?(cgImage: CGImage, targetSize: CGSize?) { | ||
guard cgImage.width > 0, cgImage.height > 0 else { | ||
return nil | ||
} | ||
|
||
self.width = { | ||
if let targetWidth = targetSize?.width { | ||
return Int(targetWidth) | ||
} else { | ||
return cgImage.width | ||
} | ||
}() | ||
self.height = { | ||
if let targetHeight = targetSize?.height { | ||
return Int(targetHeight) | ||
} else { | ||
return cgImage.height | ||
} | ||
}() | ||
|
||
guard | ||
self.width > 0, self.height > 0, | ||
let cgImage = width == cgImage.width && height == cgImage.height | ||
? cgImage | ||
: cgImage.resized(width: width, height: height), | ||
let imageData = cgImage.dataProvider?.data, | ||
let imagePtr = CFDataGetBytePtr(imageData) | ||
else { | ||
return nil | ||
} | ||
|
||
self.imageData = imageData | ||
self.imagePtr = imagePtr | ||
self.bytesPerPixel = cgImage.bitsPerPixel / 8 | ||
self.bytesPerRow = cgImage.bytesPerRow | ||
} | ||
|
||
private func offset(x: Int, y: Int) -> Int { | ||
y * bytesPerRow + x * bytesPerPixel | ||
} | ||
|
||
public func getRed(x: Int, y: Int) -> UInt8 { | ||
imagePtr[offset(x: x, y: y)] | ||
} | ||
|
||
public func getGreen(x: Int, y: Int) -> UInt8 { | ||
imagePtr[offset(x: x, y: y) + 1] | ||
} | ||
|
||
public func getBlue(x: Int, y: Int) -> UInt8 { | ||
imagePtr[offset(x: x, y: y) + 2] | ||
} | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// | ||
// File.swift | ||
// | ||
// | ||
// Created by chen on 2024/5/4. | ||
// | ||
|
||
import Foundation | ||
|
||
public struct ZPLImage { | ||
public var bytesPerRow: Int | ||
public var totalBytes: Int | ||
public var data: String | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
Sources/ZPLBuilder/Utils/ZPLImageEncoder/ZPLImageReader.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// | ||
// ZPLImageReader.swift | ||
// | ||
// | ||
// Created by chen on 2024/5/4. | ||
// | ||
|
||
import Foundation | ||
|
||
public protocol ZPLImageReader { | ||
var width: Int { get } | ||
var height: Int { get } | ||
|
||
func getRed(x: Int, y: Int) -> UInt8 | ||
func getGreen(x: Int, y: Int) -> UInt8 | ||
func getBlue(x: Int, y: Int) -> UInt8 | ||
} |