Skip to content

Commit ee7cc61

Browse files
committed
Add image and byte utilities
This commit introduces toBase64() and encodeGzip() methods for ByteArray and image resizing methods for BufferedImage.
1 parent c137ec5 commit ee7cc61

File tree

6 files changed

+152
-0
lines changed

6 files changed

+152
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package org.jetbrains.kotlinx.dataframe.impl.io
2+
3+
import java.util.Base64
4+
5+
internal fun ByteArray.toBase64(): String = Base64.getEncoder().encodeToString(this)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package org.jetbrains.kotlinx.dataframe.impl.io
2+
3+
import java.io.ByteArrayOutputStream
4+
import java.util.zip.GZIPOutputStream
5+
6+
internal fun ByteArray.encodeGzip(): ByteArray {
7+
val bos = ByteArrayOutputStream()
8+
GZIPOutputStream(bos).use { it.write(this) }
9+
10+
return bos.toByteArray()
11+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package org.jetbrains.kotlinx.dataframe.impl.io
2+
3+
import java.awt.Graphics2D
4+
import java.awt.RenderingHints
5+
import java.awt.image.BufferedImage
6+
import java.awt.image.ImageObserver
7+
import java.io.ByteArrayOutputStream
8+
import javax.imageio.ImageIO
9+
import kotlin.math.max
10+
import kotlin.math.min
11+
12+
internal fun BufferedImage.resizeKeepingAspectRatio(
13+
maxSize: Int,
14+
resultImageType: Int = BufferedImage.TYPE_INT_ARGB,
15+
interpolation: Any = RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR,
16+
renderingQuality: Any = RenderingHints.VALUE_RENDER_QUALITY,
17+
antialiasing: Any = RenderingHints.VALUE_ANTIALIAS_ON,
18+
observer: ImageObserver? = null
19+
): BufferedImage {
20+
val aspectRatio = width.toDouble() / height.toDouble()
21+
val size = min(maxSize, max(width, height))
22+
23+
val (nWidth, nHeight) = if (width > height) {
24+
Pair(size, (size / aspectRatio).toInt())
25+
} else {
26+
Pair((size * aspectRatio).toInt(), size)
27+
}
28+
29+
return resize(nWidth, nHeight, resultImageType, interpolation, renderingQuality, antialiasing, observer)
30+
}
31+
32+
internal fun BufferedImage.resize(
33+
width: Int,
34+
height: Int,
35+
resultImageType: Int = BufferedImage.TYPE_INT_ARGB,
36+
interpolation: Any = RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR,
37+
renderingQuality: Any = RenderingHints.VALUE_RENDER_QUALITY,
38+
antialiasing: Any = RenderingHints.VALUE_ANTIALIAS_ON,
39+
observer: ImageObserver? = null
40+
): BufferedImage {
41+
val resized = BufferedImage(width, height, resultImageType)
42+
val g: Graphics2D = resized.createGraphics()
43+
44+
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, interpolation)
45+
g.setRenderingHint(RenderingHints.KEY_RENDERING, renderingQuality)
46+
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, antialiasing)
47+
48+
g.drawImage(this, 0, 0, width, height, observer)
49+
g.dispose()
50+
51+
return resized
52+
}
53+
54+
internal const val DEFAULT_IMG_FORMAT = "png"
55+
56+
internal fun BufferedImage.toByteArray(format: String = DEFAULT_IMG_FORMAT): ByteArray =
57+
ByteArrayOutputStream().use { bos ->
58+
ImageIO.write(this, format, bos)
59+
bos.toByteArray()
60+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package org.jetbrains.kotlinx.dataframe.impl.io
2+
3+
import java.util.Base64
4+
5+
internal fun ByteArray.toBase64(): String = Base64.getEncoder().encodeToString(this)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package org.jetbrains.kotlinx.dataframe.impl.io
2+
3+
import java.io.ByteArrayOutputStream
4+
import java.util.zip.GZIPOutputStream
5+
6+
internal fun ByteArray.encodeGzip(): ByteArray {
7+
val bos = ByteArrayOutputStream()
8+
GZIPOutputStream(bos).use { it.write(this) }
9+
10+
return bos.toByteArray()
11+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package org.jetbrains.kotlinx.dataframe.impl.io
2+
3+
import java.awt.Graphics2D
4+
import java.awt.RenderingHints
5+
import java.awt.image.BufferedImage
6+
import java.awt.image.ImageObserver
7+
import java.io.ByteArrayOutputStream
8+
import javax.imageio.ImageIO
9+
import kotlin.math.max
10+
import kotlin.math.min
11+
12+
internal fun BufferedImage.resizeKeepingAspectRatio(
13+
maxSize: Int,
14+
resultImageType: Int = BufferedImage.TYPE_INT_ARGB,
15+
interpolation: Any = RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR,
16+
renderingQuality: Any = RenderingHints.VALUE_RENDER_QUALITY,
17+
antialiasing: Any = RenderingHints.VALUE_ANTIALIAS_ON,
18+
observer: ImageObserver? = null
19+
): BufferedImage {
20+
val aspectRatio = width.toDouble() / height.toDouble()
21+
val size = min(maxSize, max(width, height))
22+
23+
val (nWidth, nHeight) = if (width > height) {
24+
Pair(size, (size / aspectRatio).toInt())
25+
} else {
26+
Pair((size * aspectRatio).toInt(), size)
27+
}
28+
29+
return resize(nWidth, nHeight, resultImageType, interpolation, renderingQuality, antialiasing, observer)
30+
}
31+
32+
internal fun BufferedImage.resize(
33+
width: Int,
34+
height: Int,
35+
resultImageType: Int = BufferedImage.TYPE_INT_ARGB,
36+
interpolation: Any = RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR,
37+
renderingQuality: Any = RenderingHints.VALUE_RENDER_QUALITY,
38+
antialiasing: Any = RenderingHints.VALUE_ANTIALIAS_ON,
39+
observer: ImageObserver? = null
40+
): BufferedImage {
41+
val resized = BufferedImage(width, height, resultImageType)
42+
val g: Graphics2D = resized.createGraphics()
43+
44+
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, interpolation)
45+
g.setRenderingHint(RenderingHints.KEY_RENDERING, renderingQuality)
46+
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, antialiasing)
47+
48+
g.drawImage(this, 0, 0, width, height, observer)
49+
g.dispose()
50+
51+
return resized
52+
}
53+
54+
internal const val DEFAULT_IMG_FORMAT = "png"
55+
56+
internal fun BufferedImage.toByteArray(format: String = DEFAULT_IMG_FORMAT): ByteArray =
57+
ByteArrayOutputStream().use { bos ->
58+
ImageIO.write(this, format, bos)
59+
bos.toByteArray()
60+
}

0 commit comments

Comments
 (0)