Record of some of the computer tech I deal with so that it's documented at least somewhere.

Tuesday 20 April 2010

Image Processing with Go

The usual story of aphid meets programming language is to see what it's like at image processing.

file.go from the Go Tutorial

package main

import (
"fmt"
"image"
png "image/png"
"./file"
"os"
)

var errn = 0
func abort(e os.Error) {
errn++
if e != nil {
fmt.Printf("ERR %s\n", e)
os.Exit(errn)
}
}

func main() {
fd, err := file.Open("in.png", 0, 0)
abort(err)
var img image.Image
img, err = png.Decode(fd)
fd.Close()
abort(err)
w, h := img.Width(), img.Height()
opng := image.NewNRGBA(w, h)
var c image.Color
for y := 0; y < h; y++ {
for x := 0; x < w; x++ {
c = img.At(x, y)
opng.Set(x, y, c)
}
}

fd, err = file.Open("create.png", os.O_RDWR | os.O_CREATE, 0644)
abort(err)
err = png.Encode(fd, opng)
abort(err)
fd.Close()
}


% cd /dev/shm; cp $home/1920x1440.png in.png; time /tmp/8.out
real 0m9.810s
user 0m12.425s
sys 0m0.072s


around 1s per 2000 pixels

No comments: