Skip to content

Commit 1018cdb

Browse files
committed
Fix GIF encoding issue.
1 parent a0df31e commit 1018cdb

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

tiny-engine/src/jvmMain/kotlin/com/squareup/gifencoder/FastGifEncoder.kt

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,10 @@ class FastGifEncoder(
6767
!(options.left + width > screenWidth || options.top + height > screenHeight),
6868
) { "Image does not fit in screen." }
6969

70-
val paddedColorCount = colorTable.paddedSize()
70+
var paddedColorCount = 2
71+
while (paddedColorCount < rgbPalette.size) {
72+
paddedColorCount *= 2
73+
}
7174
GraphicsControlExtensionBlock.write(
7275
outputStream,
7376
options.disposalMethod,
@@ -80,7 +83,18 @@ class FastGifEncoder(
8083
outputStream, options.left, options.top, width,
8184
height, true, false, false, getColorTableSizeField(paddedColorCount),
8285
)
83-
colorTable.write(outputStream)
86+
// Write color table directly from rgbPalette to preserve duplicate RGB entries
87+
for (i in 0 until rgbPalette.size) {
88+
val rgb = rgbPalette.getRGAasInt(i)
89+
outputStream.write(rgb shr 16 and 0xFF)
90+
outputStream.write(rgb shr 8 and 0xFF)
91+
outputStream.write(rgb and 0xFF)
92+
}
93+
for (i in rgbPalette.size until paddedColorCount) {
94+
outputStream.write(0)
95+
outputStream.write(0)
96+
outputStream.write(0)
97+
}
8498

8599
// Convert ByteArray palette indices to IntArray for LZW encoder
86100
val colorIndices = IntArray(indexedData.size) { indexedData[it].toInt() and 0xFF }

0 commit comments

Comments
 (0)