Skip to content

Commit

Permalink
Merge pull request #30 from marcprux/master
Browse files Browse the repository at this point in the history
Android Support
  • Loading branch information
tayloraswift authored Jan 5, 2025
2 parents b8deca7 + ec98009 commit ebddc1a
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 3 deletions.
20 changes: 20 additions & 0 deletions .github/workflows/android.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: Android

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
android:
runs-on: ubuntu-24.04
name: Android

steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Build for Android
uses: skiptools/swift-android-action@v2
with:
run-tests: false
15 changes: 15 additions & 0 deletions Sources/JPEG/decode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1768,6 +1768,20 @@ extension JPEG.Data.Spectral:RandomAccessCollection
///
/// - Returns:
/// The plane.
#if os(Android) // workaround for an Android toolchain crash in `yield`
public
subscript(p:Int) -> Plane
{
get
{
self.planes[p]
}
set
{
self.planes[p] = newValue
}
}
#else
public
subscript(p:Int) -> Plane
{
Expand All @@ -1780,6 +1794,7 @@ extension JPEG.Data.Spectral:RandomAccessCollection
yield &self.planes[p]
}
}
#endif
/// Returns the index of the plane storing the color channel represented
/// by the given component key, or `nil` if the component key is a
/// non-recognized component.
Expand Down
22 changes: 19 additions & 3 deletions Sources/JPEGSystem/os.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ import JPEG
import Darwin
#elseif os(Linux)
import Glibc
#elseif os(Android)
import Android
#else
#warning("unsupported or untested platform (please open an issue at https://github.com/tayloraswift/swift-jpeg/issues)")
#endif

#if os(macOS) || os(Linux)
#if os(macOS) || os(Linux) || os(Android)

/// A namespace for platform-dependent functionality.
///
Expand All @@ -25,7 +27,11 @@ enum System
public
enum File
{
#if os(Android)
typealias Descriptor = OpaquePointer
#else
typealias Descriptor = UnsafeMutablePointer<FILE>
#endif

/// A type for reading data from files on disk.
public
Expand Down Expand Up @@ -102,7 +108,12 @@ extension System.File.Source
{
(buffer:inout UnsafeMutableBufferPointer<UInt8>, count:inout Int) in

count = fread(buffer.baseAddress, MemoryLayout<UInt8>.stride,
#if os(Android)
let baseAddress = buffer.baseAddress!
#else
let baseAddress = buffer.baseAddress
#endif
count = fread(baseAddress, MemoryLayout<UInt8>.stride,
capacity, self.descriptor)
}

Expand Down Expand Up @@ -210,7 +221,12 @@ extension System.File.Destination
{
let count:Int = buffer.withUnsafeBufferPointer
{
fwrite($0.baseAddress, MemoryLayout<UInt8>.stride,
#if os(Android)
let baseAddress = $0.baseAddress!
#else
let baseAddress = $0.baseAddress
#endif
return fwrite(baseAddress, MemoryLayout<UInt8>.stride,
$0.count, self.descriptor)
}

Expand Down

0 comments on commit ebddc1a

Please sign in to comment.