Commits

Matthew Turk committed ccbf369

Pixelizer can now output simple greyscale images!

Comments (0)

Files changed (1)

 import (
 	// stdlib
 	"fmt"
-    //"image"
-    //"image/png"
+    "image"
+    //"image/draw"
+    //"image/color"
+    "image/png"
     "math"
+    "os"
     "time"
 
 	// local
     return
 }
 
+func (img *ImageBuffer) MapToImage(mi, ma float64) (out *image.RGBA){
+    out = image.NewRGBA(image.Rect(0, 0, img.Nx, img.Ny))
+    var v float64
+    var cv uint8
+    for pi := 0; pi < img.Ny*img.Nx ; pi++ {
+        v = img.Buffer[pi]
+        cv = uint8(255*(v - mi) / (ma - mi))
+        for ci := 0; ci < 3; ci ++ {
+            out.Pix[pi*4+ci] = cv
+        }
+        out.Pix[pi*4+3] = 255
+    }
+    return
+}
+
 func main() {
     f, err := hdf5.OpenFile("redshift0058.yt", hdf5.F_ACC_RDONLY)
     if err != nil {
     for i := range img.Buffer {
         mi = math.Min(mi, img.Buffer[i])
         ma = math.Max(ma, img.Buffer[i])
+        img.Buffer[i] = math.Log10(img.Buffer[i])
     }
     fmt.Printf("Min value in Buffer: %0.3e\n", mi)
     fmt.Printf("Max value in Buffer: %0.3e\n", ma)
+    out := img.MapToImage(math.Log10(mi), math.Log10(ma))
+    file, _ := os.Create("test.png")
+    png.Encode(file, out)
+    file.Close()
 }