-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #536 from qdsfdhvh/master
Version 1.8.1
- Loading branch information
Showing
10 changed files
with
690 additions
and
414 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
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: 38 additions & 37 deletions
75
...loader/component/fetcher/CGImageRefExt.kt → ...m/seiko/imageloader/util/CGImageRefExt.kt
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 |
---|---|---|
@@ -1,87 +1,88 @@ | ||
package com.seiko.imageloader.component.fetcher | ||
package com.seiko.imageloader.util | ||
|
||
import cnames.structs.CGColorSpace | ||
import cnames.structs.CGImage | ||
import kotlinx.cinterop.CPointed | ||
import kotlinx.cinterop.CPointer | ||
import kotlinx.cinterop.CValue | ||
import kotlinx.cinterop.ExperimentalForeignApi | ||
import kotlinx.cinterop.readBytes | ||
import kotlinx.cinterop.addressOf | ||
import kotlinx.cinterop.usePinned | ||
import org.jetbrains.skia.ColorAlphaType | ||
import org.jetbrains.skia.ColorType | ||
import org.jetbrains.skia.Image | ||
import org.jetbrains.skia.ImageInfo | ||
import platform.CoreGraphics.CGBitmapContextCreate | ||
import platform.CoreGraphics.CGColorSpaceCreateDeviceRGB | ||
import platform.CoreGraphics.CGColorSpaceRef | ||
import platform.CoreGraphics.CGColorSpaceRelease | ||
import platform.CoreGraphics.CGContextClearRect | ||
import platform.CoreGraphics.CGContextDrawImage | ||
import platform.CoreGraphics.CGContextRef | ||
import platform.CoreGraphics.CGContextRelease | ||
import platform.CoreGraphics.CGImageAlphaInfo | ||
import platform.CoreGraphics.CGImageGetHeight | ||
import platform.CoreGraphics.CGImageGetWidth | ||
import platform.CoreGraphics.CGImageRef | ||
import platform.CoreGraphics.CGRect | ||
import platform.CoreGraphics.CGRectMake | ||
import platform.posix.free | ||
import platform.posix.malloc | ||
import platform.posix.size_t | ||
import platform.posix.uint32_t | ||
|
||
@OptIn(ExperimentalForeignApi::class) | ||
internal fun CGImageRef.toSkiaImage(): Image { | ||
fun CGImageRef.toSkiaImage(): Image { | ||
val cgImage: CPointer<CGImage> = this | ||
val width: size_t = CGImageGetWidth(cgImage) | ||
val height: size_t = CGImageGetHeight(cgImage) | ||
val space: CPointer<CGColorSpace>? = CGColorSpaceCreateDeviceRGB() | ||
val space: CGColorSpaceRef? = CGColorSpaceCreateDeviceRGB() | ||
val bitsPerComponent: ULong = 8u | ||
val bitsPerPixel: ULong = bitsPerComponent * 4u | ||
val bytesPerPixel: ULong = bitsPerPixel / bitsPerComponent | ||
val bytesPerRow: ULong = width * bytesPerPixel | ||
val bufferSize: ULong = width * height * bytesPerPixel | ||
val bitmapInfo: uint32_t = CGImageAlphaInfo.kCGImageAlphaPremultipliedLast.value | ||
val bitmapInfo: uint32_t = platform.CoreGraphics.CGImageAlphaInfo.kCGImageAlphaPremultipliedLast.value | ||
|
||
check(bufferSize != 0UL) { "image can't be 0 bytes" } | ||
check(width > 0UL) { "width should be more then 0 px ($width)" } | ||
check(height > 0UL) { "height should be more then 0 px ($height)" } | ||
|
||
val data: CPointer<out CPointed> = malloc(bufferSize) | ||
?: throw IllegalArgumentException("can't allocate memory for render bitmap of $cgImage") | ||
|
||
val rect: CValue<CGRect> = CGRectMake( | ||
x = 0.0, | ||
y = 0.0, | ||
width = width.toDouble(), | ||
height = height.toDouble(), | ||
) | ||
|
||
val ctx: CGContextRef = CGBitmapContextCreate( | ||
data = data, | ||
width = width, | ||
height = height, | ||
bitsPerComponent = bitsPerComponent, | ||
bytesPerRow = bytesPerRow, | ||
space = space, | ||
bitmapInfo = bitmapInfo, | ||
) ?: throw IllegalArgumentException("can't create bitmap context for $cgImage") | ||
val bytes = ByteArray(bufferSize.toInt()) | ||
|
||
CGContextClearRect(c = ctx, rect = rect) | ||
CGContextDrawImage(c = ctx, rect = rect, image = cgImage) | ||
bytes.usePinned { pinnedArray -> | ||
val data = pinnedArray.addressOf(0) | ||
|
||
CGContextRelease(ctx) | ||
val ctx: CGContextRef = CGBitmapContextCreate( | ||
data = data, | ||
width = width, | ||
height = height, | ||
bitsPerComponent = bitsPerComponent, | ||
bytesPerRow = bytesPerRow, | ||
space = space, | ||
bitmapInfo = bitmapInfo, | ||
) ?: throw IllegalArgumentException("can't create bitmap context for $cgImage") | ||
|
||
val bytes: ByteArray = data.readBytes(bufferSize.toInt()) | ||
CGContextClearRect(c = ctx, rect = rect) | ||
CGContextDrawImage(c = ctx, rect = rect, image = cgImage) | ||
|
||
free(data) | ||
CGContextRelease(ctx) | ||
|
||
return Image.makeRaster( | ||
imageInfo = ImageInfo( | ||
width = width.toInt(), | ||
height = height.toInt(), | ||
colorType = ColorType.RGBA_8888, | ||
alphaType = ColorAlphaType.PREMUL, | ||
), | ||
bytes = bytes, | ||
rowBytes = bytesPerRow.toInt(), | ||
) | ||
if (space != null) { | ||
CGColorSpaceRelease(space) | ||
} | ||
|
||
return Image.makeRaster( | ||
imageInfo = ImageInfo( | ||
width = width.toInt(), | ||
height = height.toInt(), | ||
colorType = ColorType.RGBA_8888, | ||
alphaType = ColorAlphaType.PREMUL, | ||
), | ||
bytes = bytes, | ||
rowBytes = bytesPerRow.toInt(), | ||
) | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
image-loader/src/iosMain/kotlin/com/seiko/imageloader/util/UIImageExt.kt
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,13 @@ | ||
package com.seiko.imageloader.util | ||
|
||
import kotlinx.cinterop.ExperimentalForeignApi | ||
import org.jetbrains.skia.Image | ||
import platform.CoreGraphics.CGImageRef | ||
import platform.UIKit.UIImage | ||
|
||
@OptIn(ExperimentalForeignApi::class) | ||
fun UIImage.toSkiaImage(): Image { | ||
val cgImage: CGImageRef = this.CGImage() | ||
?: throw IllegalArgumentException("can't read CGImage of $this") | ||
return cgImage.toSkiaImage() | ||
} |
16 changes: 16 additions & 0 deletions
16
image-loader/src/macosMain/kotlin/com/seiko/imageloader/util/NSImageExt.kt
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,16 @@ | ||
package com.seiko.imageloader.util | ||
|
||
import kotlinx.cinterop.ExperimentalForeignApi | ||
import org.jetbrains.skia.Image | ||
import platform.AppKit.NSImage | ||
import platform.CoreGraphics.CGImageRef | ||
|
||
@OptIn(ExperimentalForeignApi::class) | ||
fun NSImage.toSkiaImage(): Image { | ||
val cgImage: CGImageRef = this.CGImageForProposedRect( | ||
proposedDestRect = null, | ||
context = null, | ||
hints = null, | ||
) ?: throw IllegalArgumentException("can't read CGImage of $this") | ||
return cgImage.toSkiaImage() | ||
} |
Oops, something went wrong.