commit:013209d6d952aec43ee146929f776720b17b88b7
author:Chip Black
committer:Chip Black
date:Mon Nov 23 00:59:45 2015 -0600
parents:dc3eb2ecec7dd7f5aab732dbcc68c5947664bb4a
Add transparency, the secret 17th color
diff --git a/bs/codec_manager.go b/bs/codec_manager.go
line changes: +7/-6
index 00b9b6e..553e653
--- a/bs/codec_manager.go
+++ b/bs/codec_manager.go
@@ -5,15 +5,16 @@ import (
     "bytex64.net/code/bitsmash/ac"
 )
 
-func (self *BitSmash) codecManagerInit(c ac.CodecManager, paletteMap uint16) {
-    var colorBiases [16]uint32
-    for i := 0; i < 16; i++ {
-        colorBiases[i] = uint32(paletteMap & 1)
-        paletteMap >>= 1
+func (self *BitSmash) codecManagerInit(c ac.CodecManager) {
+    pm := self.paletteMap
+    var colorBiases [17]uint32
+    for i := 0; i < 17; i++ {
+        colorBiases[i] = uint32(pm & 1)
+        pm >>= 1
     }
 
     c.AddModel(packet.CONTEXT_PACKET_TYPE,     ac.NewModelOrder0(4))
-    c.AddModel(packet.CONTEXT_COLOR,           ac.NewModelOrder0PreBias(16, colorBiases[:]))
+    c.AddModel(packet.CONTEXT_COLOR,           ac.NewModelOrder0PreBias(17, colorBiases[:]))
     c.AddModel(packet.CONTEXT_PATTERN_NUMBER,  ac.NewModelOrder0PreBias(16, packet.ContextBias(packet.CONTEXT_PATTERN_NUMBER)))
     c.AddModel(packet.CONTEXT_PATTERN_REPEAT,  ac.NewModelOrder0PreBias(8,  packet.ContextBias(packet.CONTEXT_PATTERN_REPEAT)))
     c.AddModel(packet.CONTEXT_RLE_REPEAT,      ac.NewModelOrder0(16))

diff --git a/bs/image.go b/bs/image.go
line changes: +1/-1
index 699b236..a200421
--- a/bs/image.go
+++ b/bs/image.go
@@ -46,7 +46,7 @@ func loadImage(filename string, opts *EncodeOpts) image.PalettedImage {
     // If no palette has been specified, automatically select one
     if (opts.Palette == -1) {
         opts.Palette = findPalette(img)
-        log.Printf("Automatically selected palette %d\n", opts.Palette);
+        log.Printf("Automatically selected palette %d\n", opts.Palette)
     }
 
     // Convert to the selected palette

diff --git a/bs/io.go b/bs/io.go
line changes: +5/-5
index 516126a..2538e66
--- a/bs/io.go
+++ b/bs/io.go
@@ -41,18 +41,18 @@ func (self *BitSmash) ReadFrom(file io.Reader) error {
     }
     self.size = sizes[int(header[0]) & 0x7]
     self.palette = (int(header[0]) >> 3) & 0xF
-    self.transparent = ((header[0] >> 7) & 0x1) == 1
 
     n_packets := int(header[1]) + (int(header[2] & 0x3) << 8) + 1
     self.packetList = make([]packet.Packet, n_packets)
 
-    self.paletteMap = uint16(header[3]) + (uint16(header[4]) << 8);
+    // The transparent bit is the 17th bit of the palette map
+    self.paletteMap = uint32(header[3]) + (uint32(header[4]) << 8) + (uint32(header[0] & 0x80) << 9);
 
     decoder, err := ac.NewDecoder(file)
     if err != nil {
         return err
     }
-    self.codecManagerInit(decoder, self.paletteMap)
+    self.codecManagerInit(decoder)
 
     for i := 0; i < n_packets; i++ {
         var pkt packet.Packet // Packet! PACKET! PACKET!!!
@@ -81,7 +81,7 @@ func (self *BitSmash) WriteTo(file io.Writer) error {
 
     var header [5]byte
     transparent := 0
-    if (self.transparent) {
+    if (self.paletteMap & 0x10000 > 0) {
         transparent = 1
     }
     header[0] = byte(sizeIndex(self.size) + (self.palette << 3) + (transparent << 7))
@@ -96,7 +96,7 @@ func (self *BitSmash) WriteTo(file io.Writer) error {
     if err != nil {
         return err
     }
-    self.codecManagerInit(encoder, self.paletteMap)
+    self.codecManagerInit(encoder)
 
     for i, p := range(self.packetList) {
         encoder.Encode(packet.CONTEXT_PACKET_TYPE, uint32(self.packetType(i)))

diff --git a/bs/new.go b/bs/new.go
line changes: +1/-4
index 3261116..0eeed40
--- a/bs/new.go
+++ b/bs/new.go
@@ -17,8 +17,7 @@ type EncodeOpts struct {
 type BitSmash struct {
     size int
     palette int
-    paletteMap uint16
-    transparent bool
+    paletteMap uint32
     packetList []packet.Packet
 }
 
@@ -27,7 +26,6 @@ func NewFromPixels(pixels []uint8, size int, opts *EncodeOpts) BitSmash {
         size,
         opts.Palette,
         createPaletteMap(pixels),
-        false,
         make([]packet.Packet, 0, 1),
     }
 
@@ -54,7 +52,6 @@ func NewFromFile(filename string) BitSmash {
         0,
         0,
         0,
-        false,
         nil,
     }
 

diff --git a/bs/palette.go b/bs/palette.go
line changes: +2/-2
index be6dbb0..90e65ed
--- a/bs/palette.go
+++ b/bs/palette.go
@@ -34,8 +34,8 @@ func findPalette(img image.Image) int {
     return selected
 }
 
-func createPaletteMap(pixels []uint8) uint16 {
-    m := uint16(0);
+func createPaletteMap(pixels []uint8) uint32 {
+    m := uint32(0);
 
     for _, v := range(pixels) {
         m |= 1 << v;

diff --git a/bs/util.go b/bs/util.go
line changes: +6/-1
index 1b707a2..ac263e9
--- a/bs/util.go
+++ b/bs/util.go
@@ -53,7 +53,12 @@ func (self *BitSmash) Dump() {
     self.unpackRepeat()
     size := self.Size()
     fmt.Printf("%d pixels, %dx%d\n", self.Length(), size.X, size.Y)
-    fmt.Printf("Palette %d, palette map %b\n", self.palette, self.paletteMap)
+    fmt.Printf("Palette %d, palette map %b %04b %04b %04b %04b\n", self.palette,
+                                                                   self.paletteMap & 0x10000 >> 16,
+                                                                   self.paletteMap & 0xF000 >> 12,
+                                                                   self.paletteMap & 0xF00 >> 8,
+                                                                   self.paletteMap & 0xF0 >> 4,
+                                                                   self.paletteMap & 0xF)
     fmt.Printf("%d packets:\n", len(self.packetList))
 
     c := 0

diff --git a/palette/generate.pl b/palette/generate.pl
line changes: +1/-0
index d15ea89..89fbeb3
--- a/palette/generate.pl
+++ b/palette/generate.pl
@@ -110,6 +110,7 @@ sub print_palette_golang {
     for my $c (@{$palette{$p}}) {
         print_color_golang($c);
     }
+    say "    color.RGBA{0, 0, 0, 0},";
     say "},";
 }
 

diff --git a/palette/palettes.go b/palette/palettes.go
line changes: +10/-0
index 5ff08b7..351505b
--- a/palette/palettes.go
+++ b/palette/palettes.go
@@ -21,6 +21,7 @@ var Palettes []color.Palette = []color.Palette {
         RGB8{0xFF, 0x55, 0xFF},
         RGB8{0xFF, 0xFF, 0x55},
         RGB8{0xFF, 0xFF, 0xFF},
+        color.RGBA{0, 0, 0, 0},
     },
     // 1 - Gray/R/G/B
     color.Palette{
@@ -40,6 +41,7 @@ var Palettes []color.Palette = []color.Palette {
         RGB8{0x00, 0x46, 0xAA},
         RGB8{0x00, 0x61, 0xE7},
         RGB8{0x54, 0x83, 0xFE},
+        color.RGBA{0, 0, 0, 0},
     },
     // 2 - Red/Blue/Yellow
     color.Palette{
@@ -59,6 +61,7 @@ var Palettes []color.Palette = []color.Palette {
         RGB8{0xA1, 0xA1, 0x00},
         RGB8{0xD4, 0xD5, 0x00},
         RGB8{0xFF, 0xFE, 0xFE},
+        color.RGBA{0, 0, 0, 0},
     },
     // 3 - Purple/Orange/Green
     color.Palette{
@@ -78,6 +81,7 @@ var Palettes []color.Palette = []color.Palette {
         RGB8{0x00, 0x60, 0x00},
         RGB8{0x00, 0x7C, 0x00},
         RGB8{0x00, 0x99, 0x00},
+        color.RGBA{0, 0, 0, 0},
     },
     // 4 - Red/Green
     color.Palette{
@@ -97,6 +101,7 @@ var Palettes []color.Palette = []color.Palette {
         RGB8{0x00, 0x6E, 0x00},
         RGB8{0x00, 0x7C, 0x00},
         RGB8{0x00, 0x8A, 0x00},
+        color.RGBA{0, 0, 0, 0},
     },
     // 5 - Orange/Blue
     color.Palette{
@@ -116,6 +121,7 @@ var Palettes []color.Palette = []color.Palette {
         RGB8{0x00, 0x56, 0xCE},
         RGB8{0x00, 0x61, 0xE7},
         RGB8{0x0D, 0x6D, 0xFE},
+        color.RGBA{0, 0, 0, 0},
     },
     // 6 - Yellow/Purple
     color.Palette{
@@ -135,6 +141,7 @@ var Palettes []color.Palette = []color.Palette {
         RGB8{0xAC, 0x00, 0x9C},
         RGB8{0xC1, 0x00, 0xAF},
         RGB8{0xD7, 0x00, 0xC2},
+        color.RGBA{0, 0, 0, 0},
     },
     // 7 - Red
     color.Palette{
@@ -154,6 +161,7 @@ var Palettes []color.Palette = []color.Palette {
         RGB8{0xD1, 0x00, 0x00},
         RGB8{0xE0, 0x00, 0x00},
         RGB8{0xEF, 0x00, 0x00},
+        color.RGBA{0, 0, 0, 0},
     },
     // 8 - Green
     color.Palette{
@@ -173,6 +181,7 @@ var Palettes []color.Palette = []color.Palette {
         RGB8{0x01, 0xFE, 0x00},
         RGB8{0xB5, 0xFE, 0xB5},
         RGB8{0xFF, 0xFE, 0xFE},
+        color.RGBA{0, 0, 0, 0},
     },
     // 9 - Blue
     color.Palette{
@@ -192,5 +201,6 @@ var Palettes []color.Palette = []color.Palette {
         RGB8{0x00, 0x5F, 0xE1},
         RGB8{0x00, 0x66, 0xF1},
         RGB8{0x0D, 0x6D, 0xFE},
+        color.RGBA{0, 0, 0, 0},
     },
 }