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

Add support for other color spaces #159

Merged
merged 3 commits into from
Dec 24, 2023
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
4 changes: 1 addition & 3 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@ gradle-starter = "0.67.0"
gradle-doctor = "0.9.1"
maven-junit = "5.10.1"
maven-assertj = "3.24.2"
maven-commons = "2.15.1"
maven-binarycompatiblity = "0.13.2"
maven-dokka = "1.9.10"

[libraries]
kotlin-gradle = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin" }
junit-jupiter-api = { module = "org.junit.jupiter:junit-jupiter-api", version.ref = "maven-junit" }
junit-jupiter-engine = { module = "org.junit.jupiter:junit-jupiter-engine", version.ref = "maven-junit" }
junit-jupiter-platform = { module = "org.junit.platform:junit-platform-launcher" }
assertj-core = { module = "org.assertj:assertj-core", version.ref = "maven-assertj" }
commons-io = { module = "commons-io:commons-io", version.ref = "maven-commons" }
jetbrains-dokka = { module = "org.jetbrains.dokka:dokka-gradle-plugin", version.ref = "maven-dokka" }

[plugins]
Expand Down
1 change: 1 addition & 0 deletions webp-imageio/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,6 @@ tasks.named("processResources") {
dependencies {
testImplementation(libs.assertj.core)
testRuntimeOnly(libs.junit.jupiter.engine)
testRuntimeOnly(libs.junit.jupiter.platform)
testImplementation(libs.junit.jupiter.api)
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,6 @@ public open class WebPImageWriterSpi :
return false
}
}
val colorSpace = colorModel.colorSpace
if (!colorSpace.isCS_sRGB) {
return false
}
val sampleSize = sampleModel.sampleSize
for (i in sampleSize.indices) {
if (sampleSize[i] > 8) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ internal class WebPWriter(originatingProvider: ImageWriterSpi?) : ImageWriter(or
val width = aRi.width
val height = aRi.height
val colorModel = aRi.colorModel
return if (colorModel is ComponentColorModel) {
val colorSpace = colorModel.colorSpace
return if (colorSpace.isCS_sRGB && colorModel is ComponentColorModel) {
val sampleModel = aRi.sampleModel as ComponentSampleModel
when (sampleModel.transferType) {
DataBuffer.TYPE_BYTE -> extractComponentRGBByte(
Expand All @@ -116,7 +117,7 @@ internal class WebPWriter(originatingProvider: ImageWriterSpi?) : ImageWriter(or

else -> throw IOException("Incompatible image: $aRi")
}
} else if (colorModel is DirectColorModel) {
} else if (colorSpace.isCS_sRGB && colorModel is DirectColorModel) {
val sampleModel = aRi.sampleModel as SinglePixelPackedSampleModel
val type = sampleModel.transferType
if (type == DataBuffer.TYPE_INT) {
Expand Down
21 changes: 21 additions & 0 deletions webp-imageio/src/test/kotlin/com/luciad/imageio/webp/WebPTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ 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.condition.DisabledOnJre
import org.junit.jupiter.api.condition.JRE
import org.junit.jupiter.api.io.TempDir
import java.awt.image.BufferedImage
import java.awt.image.DataBufferInt
Expand Down Expand Up @@ -83,6 +85,25 @@ class WebPTest {
assertThat(image.height).isEqualTo(301)
}

@Test
@DisabledOnJre(JRE.JAVA_9) // for some reason Java 9 can't read JPEGs with indexed colors
fun nonRgbColorSpace(@TempDir tempDir: Path) {
val inputImage1 = ImageIO.read(readResource("non_rgb_1.jpg").inputStream())
val inputImage2 = ImageIO.read(readResource("non_rgb_2.jpeg").inputStream())
val outputFile1 = tempDir.resolve("out_1.webp").toFile()
val outputFile2 = tempDir.resolve("out_2.webp").toFile()

ImageIO.write(inputImage1, "webp", outputFile1).let(::check)
ImageIO.write(inputImage2, "webp", outputFile2).let(::check)
val outputImage1 = readImage(webp = outputFile1.readBytes())
val outputImage2 = readImage(webp = outputFile2.readBytes())

assertThat(outputImage1.width).isEqualTo(500)
assertThat(outputImage1.height).isEqualTo(333)
assertThat(outputImage2.width).isEqualTo(1000)
assertThat(outputImage2.height).isEqualTo(1000)
}

@Test
fun testCompress() {
val image = BufferedImage(256, 256, BufferedImage.TYPE_INT_ARGB)
Expand Down
Binary file added webp-imageio/src/test/resources/non_rgb_1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added webp-imageio/src/test/resources/non_rgb_2.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.