fixed the bit order of the image descriptor's packed field

This commit is contained in:
2021-05-25 23:31:43 +03:00
parent aa90abc3ff
commit 1aa42cde33
2 changed files with 49 additions and 20 deletions
+5 -5
View File
@@ -53,11 +53,11 @@ type Palette []RGB
/* /*
ImageDescriptorPacked { ImageDescriptorPacked {
0: LocalColorTableFlag | this flag is set (1) if the image contains a local color table 0-2: LocalColorTableEntries | 2 to the power of (this field + 1)
1: InterlaceFlag | this flag is set (1) if the image is interlaced 3-4: Reserved
2: SortFlag | this flag is set (1) if the color table is sorted by importance (frequency of occurrence). only available on 89a 5: SortFlag | this flag is set (1) if the color table is sorted by importance (frequency of occurrence). only available on 89a
3-4: Reserved 6: InterlaceFlag | this flag is set (1) if the image is interlaced
5-7: LocalColorTableEntrySize 7: LocalColorTableFlag | this flag is set (1) if the image contains a local color table
} }
*/ */
+44 -15
View File
@@ -6,6 +6,7 @@ import (
"encoding/binary" "encoding/binary"
"fmt" "fmt"
"io" "io"
"math"
"os" "os"
"strings" "strings"
) )
@@ -30,6 +31,10 @@ func (v Palette) UnmarshalBinary(data []byte) error {
return nil return nil
} }
func deinterlace() {
// TODO:
}
func main() { func main() {
inputFile := os.Args[1] inputFile := os.Args[1]
@@ -89,11 +94,13 @@ func main() {
palette.UnmarshalBinary(globalColorTable) palette.UnmarshalBinary(globalColorTable)
} }
for i := 1; readNextBytes(file) != TRAILER; i++ { counter := 1
for readNextBytes(file) != TRAILER {
nextByte := make([]byte, 1) nextByte := make([]byte, 1)
file.Read(nextByte) file.Read(nextByte)
fmt.Printf("next byte is: 0x%x\n", nextByte[0]) // fmt.Printf("next byte is: 0x%x\n", nextByte[0])
switch nextByte[0] { switch nextByte[0] {
case EXTENSION_BLOCK: case EXTENSION_BLOCK:
@@ -101,7 +108,7 @@ func main() {
nextSecondByte := make([]byte, 1) nextSecondByte := make([]byte, 1)
file.Read(nextSecondByte) file.Read(nextSecondByte)
fmt.Printf("next second byte is: 0x%x\n", nextSecondByte[0]) // fmt.Printf("next second byte is: 0x%x\n", nextSecondByte[0])
switch nextSecondByte[0] { switch nextSecondByte[0] {
case PLAINTEXT_BLOCK: case PLAINTEXT_BLOCK:
@@ -156,22 +163,43 @@ func main() {
decompressedFrameSize := uint64(int64(imageDescriptor.Height) * int64(imageDescriptor.Width)) decompressedFrameSize := uint64(int64(imageDescriptor.Height) * int64(imageDescriptor.Width))
LocalColorTableFlag := imageDescriptor.Packed & 1 LocalColorTableFlag := (imageDescriptor.Packed & 0x80) >> 7
InterlaceFlag := (imageDescriptor.Packed & 2) >> 1 InterlaceFlag := (imageDescriptor.Packed & 0x40) >> 6
SortFlag := (imageDescriptor.Packed & 4) >> 2 SortFlag := (imageDescriptor.Packed & 0x20) >> 5
LocalColorTableSize := (imageDescriptor.Packed & 0xE0) >> 5 LocalColorTableEntries := math.Pow(2, float64((imageDescriptor.Packed&7)+1))
fmt.Printf("Local Color Table Flag: %v\n", LocalColorTableFlag) // fmt.Printf("Local Color Table Flag: %v\n", LocalColorTableFlag)
fmt.Printf("Interlace Flag: %v\n", InterlaceFlag) // fmt.Printf("Interlace Flag: %v\n", InterlaceFlag)
fmt.Printf("Sort Flag: %v\n", SortFlag) // fmt.Printf("Sort Flag: %v\n", SortFlag)
fmt.Printf("Local Color Table Size: %v\n", LocalColorTableSize) // fmt.Printf("Local Color Table Entries: %v\n", LocalColorTableEntries)
fmt.Printf("Left Offset: %v\n", imageDescriptor.Left)
fmt.Printf("Top Offset: %v\n", imageDescriptor.Top)
// TODO: implement deinterlacing
// TODO: implement sort flag thingy ???
// TODO: implement local color table
// TODO: use color tables for correct colors on pixels // TODO: use color tables for correct colors on pixels
// TODO: write the image data to PNG // TODO: write the image data to PNG
// local color table comes first
var LocalColorTable Palette
if LocalColorTableFlag == 1 {
tempLocalColorTable := make([]byte, int(LocalColorTableEntries*3))
file.Read(tempLocalColorTable)
LocalColorTable = make(Palette, int(LocalColorTableEntries))
LocalColorTable.UnmarshalBinary(tempLocalColorTable)
// TODO: use local color table
}
if InterlaceFlag == 1 {
// TODO: implement deinterlacing
deinterlace()
}
if SortFlag == 1 {
// TODO: implement sort flag thingy ???
}
LZWCodeSize := make([]byte, 1) LZWCodeSize := make([]byte, 1)
file.Read(LZWCodeSize) file.Read(LZWCodeSize)
@@ -185,13 +213,14 @@ func main() {
panic(err) panic(err)
} }
outFile, err := os.OpenFile(fmt.Sprintf("./%s/%s-raw.%v", dirName, st.Name(), i), os.O_CREATE|os.O_RDWR, os.FileMode(0755)) outFile, err := os.OpenFile(fmt.Sprintf("./%s/%s-raw.%v", dirName, st.Name(), counter), os.O_CREATE|os.O_RDWR, os.FileMode(0755))
if err != nil { if err != nil {
panic(err) panic(err)
} }
outFile.Write(decompressedFrameData) outFile.Write(decompressedFrameData)
counter++
break break
} }
} }