Skip to content

Commit

Permalink
Merge pull request #112 from usefulness/fix_initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
mateuszkwiecinski authored Jul 2, 2023
2 parents 98c618d + 43ed90d commit 45948e9
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ internal object WebPWrapper {

private var NATIVE_LIBRARY_LOADED = false

init {
loadNativeLibrary()
}

@JvmStatic
@Synchronized
fun loadNativeLibrary() {
Expand Down
20 changes: 20 additions & 0 deletions webp-imageio/src/test/kotlin/com/luciad/imageio/webp/SimpleTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.luciad.imageio.webp

import com.luciad.imageio.webp.utils.readImage
import com.luciad.imageio.webp.utils.readResource
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test

class SimpleTest {

/**
* For some reason this reproduces the issue from https://github.com/usefulness/webp-imageio/issues/111
*/
@Test
fun readLossy() {
val image = readImage(readResource("lossy.webp"))

assertThat(image.width).isEqualTo(1024)
assertThat(image.height).isEqualTo(752)
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package com.luciad.imageio.webp

import com.luciad.imageio.webp.utils.getResourceStream
import com.luciad.imageio.webp.utils.readImage
import com.luciad.imageio.webp.utils.readResource
import com.luciad.imageio.webp.utils.requireWebpImageReader
import com.luciad.imageio.webp.utils.requireWebpImageWriter
import com.luciad.imageio.webp.utils.writeWebpImage
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.io.TempDir
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.luciad.imageio.webp.utils

import java.io.ByteArrayOutputStream

private object ResourcesLoader

internal fun readResource(resource: String) = getResourceStream(resource).use { stream ->
ByteArrayOutputStream().use { out ->
stream.copyTo(out)
out.toByteArray()
}
}

internal fun getResourceStream(resource: String) = checkNotNull(ResourcesLoader::class.java.classLoader.getResourceAsStream(resource)) {
"Could not load resource $resource"
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.luciad.imageio.webp
package com.luciad.imageio.webp.utils

import com.luciad.imageio.webp.WebPImageReaderSpi
import com.luciad.imageio.webp.WebPImageWriterSpi
import com.luciad.imageio.webp.WebPWriteParam
import java.awt.image.BufferedImage
import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream
import java.io.File
import javax.imageio.IIOImage
import javax.imageio.ImageIO
Expand All @@ -16,20 +18,7 @@ import javax.imageio.stream.MemoryCacheImageInputStream
internal fun Iterator<ImageReader>.requireWebpImageReader() = asSequence().single { it.originatingProvider is WebPImageReaderSpi }
internal fun Iterator<ImageWriter>.requireWebpImageWriter() = asSequence().single { it.originatingProvider is WebPImageWriterSpi }

private object ResourcesLoader

internal fun readResource(resource: String) = getResourceStream(resource).use { stream ->
ByteArrayOutputStream().use { out ->
stream.copyTo(out)
out.toByteArray()
}
}

internal fun getResourceStream(resource: String) = checkNotNull(ResourcesLoader::class.java.classLoader.getResourceAsStream(resource)) {
"Could not load resource $resource"
}

internal fun readImage(webp: ByteArray, param: ImageReadParam? = null) = checkNotNull(getImageReader(webp))
internal fun readImage(webp: ByteArray, param: ImageReadParam? = null) = getImageReader(webp)
.apply { input = MemoryCacheImageInputStream(ByteArrayInputStream(webp)) }
.read(0, param)

Expand Down

0 comments on commit 45948e9

Please sign in to comment.