commit:dc3eb2ecec7dd7f5aab732dbcc68c5947664bb4a
author:Chip Black
committer:Chip Black
date:Sun Nov 22 23:26:25 2015 -0600
parents:ba3337f40ebfd836f3dba55a909a03f47a324a6f
Implement decode to PNG
diff --git a/bs/image.go b/bs/image.go
line changes: +15/-0
index f519f70..699b236
--- a/bs/image.go
+++ b/bs/image.go
@@ -61,3 +61,18 @@ func getPixelList(img image.PalettedImage) []uint8 {
 
     return p.Pix
 }
+
+func (self *BitSmash) toImage() image.PalettedImage {
+    self.unpackRepeat()
+    size := self.Size()
+    img := image.NewPaletted(image.Rectangle{image.Pt(0, 0), size}, palette.Palettes[self.palette])
+    c := 0
+
+    for _, p := range(self.packetList) {
+        pixels := p.Pixels()
+        copy(img.Pix[c:], pixels)
+        c += len(pixels)
+    }
+
+    return img
+}

diff --git a/bs/io.go b/bs/io.go
line changes: +11/-0
index 2dd539e..516126a
--- a/bs/io.go
+++ b/bs/io.go
@@ -1,10 +1,17 @@
 package bs
 
+// I! O! Let's go!
+// I! O! Let's go!
+// I! O! Let's go!
+// I! O! Let's go!
+//   - with apologies to The Ramones
+
 import (
     "io"
     "log"
     "fmt"
     "errors"
+    "image/png"
 
     "bytex64.net/code/bitsmash/packet"
     "bytex64.net/code/bitsmash/ac"
@@ -100,3 +107,7 @@ func (self *BitSmash) WriteTo(file io.Writer) error {
     return nil
 }
 
+func (self *BitSmash) WriteImage(file io.Writer) error {
+    image := self.toImage()
+    return png.Encode(file, image)
+}

diff --git a/main.go b/main.go
line changes: +9/-5
index a63a6bf..6df608f
--- a/main.go
+++ b/main.go
@@ -48,12 +48,16 @@ func main() {
     if args.dump {
         smash.Dump()
     } else {
-        var buf bytes.Buffer
-        smash.WriteTo(&buf)
-        if args.base64 {
-            fmt.Println(":_", base64.RawStdEncoding.EncodeToString(buf.Bytes()))
+        if (args.decode) {
+            smash.WriteImage(os.Stdout)
         } else {
-            buf.WriteTo(os.Stdout)
+            var buf bytes.Buffer
+            smash.WriteTo(&buf)
+            if args.base64 {
+                fmt.Println(":_", base64.RawStdEncoding.EncodeToString(buf.Bytes()))
+            } else {
+                buf.WriteTo(os.Stdout)
+            }
         }
     }
 }

diff --git a/packet/repeat.go b/packet/repeat.go
line changes: +1/-1
index de3080b..258cb0c
--- a/packet/repeat.go
+++ b/packet/repeat.go
@@ -17,7 +17,7 @@ func (self *RepeatPacket) Length() int {
     for _, p := range(self.packetSlice) {
         c += p.Length()
     }
-    return 0
+    return c
 }
 
 func (*RepeatPacket) ByteLength() int {