Skip to content

Commit 9b7e9ec

Browse files
committed
GIF: Basic HEX view support
1 parent c68e0fe commit 9b7e9ec

1 file changed

Lines changed: 38 additions & 0 deletions

File tree

app/src/commonMain/kotlin/HtmlGenerator.kt

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ import com.ashampoo.kim.format.bmff.box.ItemInfoEntryBox
2626
import com.ashampoo.kim.format.bmff.box.ItemInformationBox
2727
import com.ashampoo.kim.format.bmff.box.ItemLocationBox
2828
import com.ashampoo.kim.format.bmff.box.MetaBox
29+
import com.ashampoo.kim.format.gif.GifChunkType
30+
import com.ashampoo.kim.format.gif.GifImageParser
2931
import com.ashampoo.kim.format.jpeg.JpegConstants
3032
import com.ashampoo.kim.format.jpeg.JpegSegmentAnalyzer
3133
import com.ashampoo.kim.format.jxl.box.ExifBox
@@ -243,6 +245,9 @@ fun generateHexHtml(bytes: ByteArray): String {
243245
ImageFormat.JXL ->
244246
generateHtmlFromSlices(bytes, createBaseMediaFileFormatSlices(bytes))
245247

248+
ImageFormat.GIF ->
249+
generateHtmlFromSlices(bytes, createGifSlices(bytes))
250+
246251
else -> "HEX view for $format is not (yet) supported."
247252
}
248253
}
@@ -1106,6 +1111,39 @@ private fun createBaseMediaFileFormatSlices(bytes: ByteArray): List<LabeledSlice
11061111
return slices
11071112
}
11081113

1114+
private fun createGifSlices(bytes: ByteArray): List<LabeledSlice> {
1115+
1116+
val chunks = GifImageParser.readChunks(
1117+
byteReader = ByteArrayByteReader(bytes),
1118+
chunkTypeFilter = null
1119+
)
1120+
1121+
val slices = mutableListOf<LabeledSlice>()
1122+
1123+
var startPosition = 0
1124+
1125+
for (chunk in chunks) {
1126+
1127+
val endPosition = startPosition + chunk.bytes.size
1128+
1129+
slices.add(
1130+
LabeledSlice(
1131+
range = startPosition until endPosition,
1132+
label = chunk.type.name,
1133+
separatorLineType = SeparatorLineType.BOLD,
1134+
snipAfterLineCount = if (chunk.type == GifChunkType.APPLICATION_EXTENSION) 5 else 1
1135+
)
1136+
)
1137+
1138+
startPosition = endPosition
1139+
}
1140+
1141+
/* For safety sort in offset order. */
1142+
slices.sortBy { it.range.first }
1143+
1144+
return slices
1145+
}
1146+
11091147
/**
11101148
* To prevent missing parts of the document this method
11111149
* should check that nothing is missing or add it.

0 commit comments

Comments
 (0)