Skip to content

Commit 02b5ed6

Browse files
Merge pull request #159 from kaden-sharpin/master
Add support for other color spaces
2 parents e16df3a + 2d73571 commit 02b5ed6

File tree

7 files changed

+26
-9
lines changed

7 files changed

+26
-9
lines changed

gradle/libs.versions.toml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,14 @@ gradle-starter = "0.67.0"
33
gradle-doctor = "0.9.1"
44
maven-junit = "5.10.1"
55
maven-assertj = "3.24.2"
6-
maven-commons = "2.15.1"
76
maven-binarycompatiblity = "0.13.2"
87
maven-dokka = "1.9.10"
98

109
[libraries]
11-
kotlin-gradle = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin" }
1210
junit-jupiter-api = { module = "org.junit.jupiter:junit-jupiter-api", version.ref = "maven-junit" }
1311
junit-jupiter-engine = { module = "org.junit.jupiter:junit-jupiter-engine", version.ref = "maven-junit" }
12+
junit-jupiter-platform = { module = "org.junit.platform:junit-platform-launcher" }
1413
assertj-core = { module = "org.assertj:assertj-core", version.ref = "maven-assertj" }
15-
commons-io = { module = "commons-io:commons-io", version.ref = "maven-commons" }
1614
jetbrains-dokka = { module = "org.jetbrains.dokka:dokka-gradle-plugin", version.ref = "maven-dokka" }
1715

1816
[plugins]

webp-imageio/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,6 @@ tasks.named("processResources") {
5858
dependencies {
5959
testImplementation(libs.assertj.core)
6060
testRuntimeOnly(libs.junit.jupiter.engine)
61+
testRuntimeOnly(libs.junit.jupiter.platform)
6162
testImplementation(libs.junit.jupiter.api)
6263
}

webp-imageio/src/main/kotlin/com/luciad/imageio/webp/WebPImageWriterSpi.kt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,6 @@ public open class WebPImageWriterSpi :
6767
return false
6868
}
6969
}
70-
val colorSpace = colorModel.colorSpace
71-
if (!colorSpace.isCS_sRGB) {
72-
return false
73-
}
7470
val sampleSize = sampleModel.sampleSize
7571
for (i in sampleSize.indices) {
7672
if (sampleSize[i] > 8) {

webp-imageio/src/main/kotlin/com/luciad/imageio/webp/internal/WebPWriter.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ internal class WebPWriter(originatingProvider: ImageWriterSpi?) : ImageWriter(or
9797
val width = aRi.width
9898
val height = aRi.height
9999
val colorModel = aRi.colorModel
100-
return if (colorModel is ComponentColorModel) {
100+
val colorSpace = colorModel.colorSpace
101+
return if (colorSpace.isCS_sRGB && colorModel is ComponentColorModel) {
101102
val sampleModel = aRi.sampleModel as ComponentSampleModel
102103
when (sampleModel.transferType) {
103104
DataBuffer.TYPE_BYTE -> extractComponentRGBByte(
@@ -116,7 +117,7 @@ internal class WebPWriter(originatingProvider: ImageWriterSpi?) : ImageWriter(or
116117

117118
else -> throw IOException("Incompatible image: $aRi")
118119
}
119-
} else if (colorModel is DirectColorModel) {
120+
} else if (colorSpace.isCS_sRGB && colorModel is DirectColorModel) {
120121
val sampleModel = aRi.sampleModel as SinglePixelPackedSampleModel
121122
val type = sampleModel.transferType
122123
if (type == DataBuffer.TYPE_INT) {

webp-imageio/src/test/kotlin/com/luciad/imageio/webp/WebPTest.kt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import com.luciad.imageio.webp.utils.requireWebpImageWriter
88
import com.luciad.imageio.webp.utils.writeWebpImage
99
import org.assertj.core.api.Assertions.assertThat
1010
import org.junit.jupiter.api.Test
11+
import org.junit.jupiter.api.condition.DisabledOnJre
12+
import org.junit.jupiter.api.condition.JRE
1113
import org.junit.jupiter.api.io.TempDir
1214
import java.awt.image.BufferedImage
1315
import java.awt.image.DataBufferInt
@@ -83,6 +85,25 @@ class WebPTest {
8385
assertThat(image.height).isEqualTo(301)
8486
}
8587

88+
@Test
89+
@DisabledOnJre(JRE.JAVA_9) // for some reason Java 9 can't read JPEGs with indexed colors
90+
fun nonRgbColorSpace(@TempDir tempDir: Path) {
91+
val inputImage1 = ImageIO.read(readResource("non_rgb_1.jpg").inputStream())
92+
val inputImage2 = ImageIO.read(readResource("non_rgb_2.jpeg").inputStream())
93+
val outputFile1 = tempDir.resolve("out_1.webp").toFile()
94+
val outputFile2 = tempDir.resolve("out_2.webp").toFile()
95+
96+
ImageIO.write(inputImage1, "webp", outputFile1).let(::check)
97+
ImageIO.write(inputImage2, "webp", outputFile2).let(::check)
98+
val outputImage1 = readImage(webp = outputFile1.readBytes())
99+
val outputImage2 = readImage(webp = outputFile2.readBytes())
100+
101+
assertThat(outputImage1.width).isEqualTo(500)
102+
assertThat(outputImage1.height).isEqualTo(333)
103+
assertThat(outputImage2.width).isEqualTo(1000)
104+
assertThat(outputImage2.height).isEqualTo(1000)
105+
}
106+
86107
@Test
87108
fun testCompress() {
88109
val image = BufferedImage(256, 256, BufferedImage.TYPE_INT_ARGB)
714 KB
Loading
56.6 KB
Loading

0 commit comments

Comments
 (0)