Created
January 13, 2015 12:16
-
-
Save mshr-h/984021ccf0a158dcc734 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"image" | |
"image/png" | |
"log" | |
"os" | |
) | |
func main() { | |
var inFile *os.File | |
var outFile *os.File | |
var img image.Image | |
var err error | |
if len(os.Args) < 2 { | |
log.Fatal() | |
} | |
for i := 1; i < len(os.Args); i++ { | |
filename := os.Args[i] | |
if inFile, err = os.Open(filename); err != nil { | |
log.Fatal(err) | |
} | |
if img, err = png.Decode(inFile); err != nil { | |
log.Fatal(err) | |
} | |
defer inFile.Close() | |
out_filename := filename[0:len(filename)-4] + "_t" + ".png" | |
if outFile, err = os.Create(out_filename); err != nil { | |
log.Fatal(err) | |
} | |
var matrix = [3][3]float64{ | |
{2, 0, 0}, // {a, b, t_x} | |
{0, 0.5, 0}, // {c, d, t_y} | |
{0, 0, 1}, // {0, 0, 1} | |
} | |
img = affine_map(&img, &matrix) | |
if err = png.Encode(outFile, img); err != nil { | |
fmt.Println() | |
return | |
} | |
} | |
} | |
func affine_map(in *image.Image, matrix *[3][3]float64) image.Image { | |
rect := (*in).Bounds() | |
out := image.NewRGBA(rect) | |
in_p := image.Pt(0, 0) | |
out_p := image.Pt(0, 0) | |
for w := -rect.Max.X / 2; w <= rect.Max.X/2; w++ { | |
for h := -rect.Max.Y / 2; h < rect.Max.Y/2; h++ { | |
out_p.X = w + rect.Max.X/2 | |
out_p.Y = h + rect.Max.Y/2 | |
in_p.X = int(float64(w)/matrix[0][0]-matrix[0][2]) + rect.Max.X/2 | |
in_p.Y = int(float64(h)/matrix[1][1]-matrix[1][2]) + rect.Max.Y/2 | |
if in_p.In(rect) { | |
out.Set(out_p.X, out_p.Y, (*in).At(in_p.X, in_p.Y)) | |
} | |
} | |
} | |
return out | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment