Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/add support swift package manager #11

Merged
merged 6 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions .github/workflows/build-and-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ on:
jobs:
publish:
name: Build and Test
runs-on: macos-13
runs-on: macos-latest

steps:
- name: Checkout
Expand All @@ -25,11 +25,8 @@ jobs:
- name: Build
shell: bash
run: |
mkdir -p DerivedData/compilation-database
curl -L -O https://sonarcloud.io/static/cpp/build-wrapper-macosx-x86.zip
unzip -o build-wrapper-macosx-x86.zip
build-wrapper-macosx-x86/build-wrapper-macosx-x86 --out-dir DerivedData/compilation-database xcodebuild clean test -project WCPhotoManipulator.xcodeproj -scheme WCPhotoManipulator -sdk iphonesimulator -destination 'platform=iOS Simulator,name=iPhone 15 Pro' -enableCodeCoverage YES -configuration Debug | xcpretty -c
./xccov-to-sonarqube-generic.sh ~/Library/Developer/Xcode/DerivedData/*/Logs/Test/*.xcresult/ > sonarqube-generic-coverage.xml
xcodebuild clean test -scheme WCPhotoManipulator -sdk iphonesimulator -destination 'platform=iOS Simulator,name=iPhone 15' -enableCodeCoverage YES -configuration Debug -derivedDataPath Build/ | xcpretty -c
./xccov-to-sonarqube-generic.sh Build/Logs/Test/*.xcresult/ > sonarqube-generic-coverage.xml

- name: Setup sonarqube
uses: warchant/setup-sonar-scanner@v8
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Mac OS X
.DS_Store

# Xcode
#
# gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// swift-tools-version: 5.4
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "WCPhotoManipulator",
platforms: [.iOS(.v14)],
products: [
.library(
name: "WCPhotoManipulator",
targets: ["WCPhotoManipulator"]),
],
targets: [
.target(
name: "WCPhotoManipulator"),
.testTarget(
name: "WCPhotoManipulatorTests",
dependencies: ["WCPhotoManipulator"],
resources: [.copy("Resources/background.jpg"),
.copy("Resources/overlay.png")]
),
.testTarget(
name: "WCPhotoManipulatorObjCTests",
dependencies: ["WCPhotoManipulator"],
resources: [.copy("Resources/background.jpg"),
.copy("Resources/overlay.png")]
),
],
swiftLanguageVersions: [.v5]
)
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
//

#import <XCTest/XCTest.h>
#import "WCPhotoManipulator-Swift.h"
#import "Helpers/UIImage+Testing.h"
@import WCPhotoManipulator;

@interface BitmapUtilsObjCTests : XCTestCase

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
//

#import <XCTest/XCTest.h>
#import "UIImage+Testing.h"
#import "NSData+Testing.h"
#import "WCPhotoManipulator-Swift.h"
#import "Helpers/UIImage+Testing.h"
#import "Helpers/NSData+Testing.h"
@import WCPhotoManipulator;

@interface FileUtilsObjCTests : XCTestCase

Expand Down Expand Up @@ -103,7 +103,7 @@ - (void)testImageToData_WhenOuptutPNG_ShouldReturnPng {
/// imageFromUrl
///////////////////////////
- (void)testImageFromUrl_WhenLocalFile_ShouldHaveData {
url = [[NSBundle bundleForClass:[self class]] URLForResource:@"overlay" withExtension:@"png"];
url = [NSURL fileURLWithPath:[UIImage urlImageNamedTest:@"overlay.png"]];

image = [FileUtils imageFromUrl:url];
XCTAssertNotNil(image);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
//

#import <UIKit/UIKit.h>
#import "WCPhotoManipulator-Swift.h"
@import WCPhotoManipulator;

NS_ASSUME_NONNULL_BEGIN

@interface UIImage (Testing)

+(UIImage*)imageNamedTest:(NSString *)name;
+(NSString*)urlImageNamedTest:(NSString *)name;
-(UIColor*)colorAt:(CGPoint)location;

@end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,17 @@ @implementation BundleLocator
@implementation UIImage (Testing)

+(UIImage *)imageNamedTest:(NSString *)name {
NSBundle *bundle = [NSBundle bundleForClass:[BundleLocator class]];
NSString *path = [bundle pathForResource:name.stringByDeletingPathExtension ofType:name.pathExtension];
return [UIImage imageWithContentsOfFile:path];
return [UIImage imageWithContentsOfFile:[self urlImageNamedTest:name]];
}

+(NSString *)urlImageNamedTest:(NSString *)name {
return [[self bundle] pathForResource:name.stringByDeletingPathExtension ofType:name.pathExtension];
}

+(NSBundle *)bundle {
NSURL *url = [[[NSBundle bundleForClass:[BundleLocator class]] resourceURL] URLByAppendingPathComponent:@"WCPhotoManipulator_WCPhotoManipulatorObjCTests.bundle"];

return [NSBundle bundleWithURL:url];
}

-(BOOL)isAlphaFirst {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//

#import <XCTest/XCTest.h>
#import "WCPhotoManipulator-Swift.h"
@import WCPhotoManipulator;

@interface MimeUtilsObjCTests : XCTestCase

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
//

#import <XCTest/XCTest.h>
#import "WCPhotoManipulator-Swift.h"
#import "Helpers/UIImage+Testing.h"
@import WCPhotoManipulator;

@interface UIImage_PhotoManipulatorObjCTests : XCTestCase

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,22 +65,22 @@ class FileUtilsSwiftTests: XCTestCase {
data = FileUtils.imageToData(image, mimeType: MimeUtils.JPEG, quality: 100)

XCTAssertNotNil(data)
XCTAssertEqual((data as NSData).mimeType(), MimeUtils.JPEG)
XCTAssertEqual(data.mimeType(), MimeUtils.JPEG)
}

func testImageToData_WhenOuptutPNG_ShouldReturnPng() throws {
image = UIImage.init(namedTest: "overlay.png")
data = FileUtils.imageToData(image, mimeType: MimeUtils.PNG, quality: 100)

XCTAssertNotNil(data)
XCTAssertEqual((data as NSData).mimeType(), MimeUtils.PNG)
XCTAssertEqual(data.mimeType(), MimeUtils.PNG)
}

////////////////////////////
/// imageFromUrl
///////////////////////////
func testImageFromUrl_WhenLocalFile_ShouldHaveData() throws {
url = Bundle(for: type(of: self)).url(forResource: "overlay", withExtension: "png")
url = URL(fileURLWithPath: UIImage.urlImageNamedTest("overlay.png") ?? "")

image = FileUtils.imageFromUrl(url)
XCTAssertNotNil(image)
Expand Down
64 changes: 64 additions & 0 deletions Tests/WCPhotoManipulatorTests/Helpers/CGBitmapInfo+Testing.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
//
// File.swift
//
//
// Created by Woraphot Chokratanasombat on 21/8/2567 BE.
//

import UIKit

extension CGBitmapInfo {
enum ComponentLayout {
case bgra
case abgr
case argb
case rgba
case bgr
case rgb

var count: Int {
switch self {
case .bgr, .rgb: return 3
default: return 4
}
}
}
/***


var isAlphaFirst: Bool {
let alphaInfo = cgImage?.alphaInfo
return alphaInfo == .first || alphaInfo == .premultipliedFirst || alphaInfo == .noneSkipFirst
}

var isAlphaLast: Bool {
let alphaInfo = cgImage?.alphaInfo
return alphaInfo == .last || alphaInfo == .premultipliedLast || alphaInfo == .noneSkipLast
}

var isLittleEndian: Bool {
let byteInfo = cgImage?.bitmapInfo
return byteInfo == .byteOrder16Little || byteInfo == .byteOrder32Little
}
*/
var componentLayout: ComponentLayout? {
guard let alphaInfo = CGImageAlphaInfo(rawValue: rawValue & Self.alphaInfoMask.rawValue) else { return nil }
let isLittleEndian = contains(.byteOrder32Little)

if alphaInfo == .none {
return isLittleEndian ? .bgr : .rgb
}
let alphaIsFirst = alphaInfo == .premultipliedFirst || alphaInfo == .first || alphaInfo == .noneSkipFirst

if isLittleEndian {
return alphaIsFirst ? .bgra : .abgr
} else {
return alphaIsFirst ? .argb : .rgba
}
}

var chromaIsPremultipliedByAlpha: Bool {
let alphaInfo = CGImageAlphaInfo(rawValue: rawValue & Self.alphaInfoMask.rawValue)
return alphaInfo == .premultipliedFirst || alphaInfo == .premultipliedLast
}
}
24 changes: 24 additions & 0 deletions Tests/WCPhotoManipulatorTests/Helpers/Data+Testing.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// File.swift
//
//
// Created by Woraphot Chokratanasombat on 21/8/2567 BE.
//

import Foundation

extension Data {
public func mimeType() -> String? {
var c: UInt8 = 0;

copyBytes(to: &c, count: 1)

switch (c) {
case 0xFF: return "image/jpeg"
case 0x89: return "image/png"
case 0x47: return "image/gif"
case 0x49, 0x4D: return "image/tiff"
default: return nil
}
}
}
65 changes: 65 additions & 0 deletions Tests/WCPhotoManipulatorTests/Helpers/UIImage+Testing.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
//
// File.swift
//
//
// Created by Woraphot Chokratanasombat on 21/8/2567 BE.
//

import UIKit

extension UIImage {
public convenience init?(namedTest name: String) {
self.init(contentsOfFile: Self.urlImageNamedTest(name) ?? "")
}

static func urlImageNamedTest(_ name: String) -> String? {
guard let name = name as NSString? else {
return nil
}
return bundle()?.path(forResource: name.deletingPathExtension, ofType: name.pathExtension)
}

static func bundle() -> Bundle? {
guard let url = Bundle(for: BundleLocator.self).resourceURL?.appendingPathComponent("WCPhotoManipulator_WCPhotoManipulatorTests.bundle") else {
return nil
}
return Bundle(url: url)
}

func color(at point: CGPoint) -> UIColor? {
guard
let cgImage = cgImage,
let space = cgImage.colorSpace,
let pixelData = cgImage.dataProvider?.data,
let layout = cgImage.bitmapInfo.componentLayout
else {
return nil
}
let data: UnsafePointer<UInt8> = CFDataGetBytePtr(pixelData)

let comp = CGFloat(layout.count)
let isHDR = CGColorSpaceUsesITUR_2100TF(space)
let hdr = CGFloat(isHDR ? 2 : 1)
let pixelInfo = Int((size.width * point.y * scale + point.x * scale) * comp * hdr)
let i = Array(0 ... Int(comp - 1)).map {
CGFloat(data[pixelInfo + $0 * Int(hdr)]) / CGFloat(255)
}

switch layout {
case .bgra:
return UIColor(red: i[2], green: i[1], blue: i[0], alpha: i[3])
case .abgr:
return UIColor(red: i[3], green: i[2], blue: i[1], alpha: i[0])
case .argb:
return UIColor(red: i[1], green: i[2], blue: i[3], alpha: i[0])
case .rgba:
return UIColor(red: i[0], green: i[1], blue: i[2], alpha: i[3])
case .bgr:
return UIColor(red: i[2], green: i[1], blue: i[0], alpha: 1)
case .rgb:
return UIColor(red: i[0], green: i[1], blue: i[2], alpha: 1)
}
}
}

class BundleLocator {}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

import XCTest
import WCPhotoManipulator

final class TextStyleTests: XCTestCase {
override func setUpWithError() throws {
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

import XCTest
import WCPhotoManipulator

class UIImage_PhotoManipulatorSwiftTests: XCTestCase {
var image: UIImage! = nil
Expand All @@ -23,14 +24,14 @@ class UIImage_PhotoManipulatorSwiftTests: XCTestCase {
func testHasAlpha_WhenNoData_ShouldReturnFalse() throws {
image = UIImage.init()
XCTAssertNotNil(image)
XCTAssertTrue(image.responds(to: Selector(("hasAlpha"))))
XCTAssertTrue(image.responds(to: #selector(UIImage.hasAlpha as (UIImage) -> () -> Bool)))
XCTAssertFalse(image.hasAlpha())
}

func testHasAlpha_ShouldReturnCorrectly() throws {
image = UIImage.init(namedTest: "overlay.png")
XCTAssertNotNil(image)
XCTAssertTrue(image.responds(to: Selector(("hasAlpha"))))
XCTAssertTrue(image.responds(to: #selector(UIImage.hasAlpha as (UIImage) -> () -> Bool)))
XCTAssertTrue(image.hasAlpha())


Expand Down
2 changes: 1 addition & 1 deletion WCPhotoManipulator.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ Pod::Spec.new do |s|

s.platform = :ios, "9.0"
s.source = { :git => "https://github.com/guhungry/ios-photo-manipulator.git", :tag => "v#{s.version}" }
s.source_files = "WCPhotoManipulator/**/*.{swift}"
s.source_files = "Sources/WCPhotoManipulator/**/*.{swift}"
s.swift_version = '5.0'
end
Loading