Skip to content

Instantly share code, notes, and snippets.

@csaunders
Created November 26, 2014 13:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save csaunders/96f8c402cbb52ca1ed22 to your computer and use it in GitHub Desktop.
Save csaunders/96f8c402cbb52ca1ed22 to your computer and use it in GitHub Desktop.
/*
* @csaunders
* @gregov
* @Abid
*/
package main
import (
"image"
"image/color"
"image/draw"
"image/jpeg"
"log"
"math"
"os"
)
type FauxImage struct {
image.Image
}
func loadImage(filename string) (image.Image, error) {
file, err := os.Open(filename)
if err != nil {
return FauxImage{}, err
}
return jpeg.Decode(file)
}
func subtract(c1, c2 color.Color) (r, g, b, a float64) {
c1r, c1g, c1b, c1a := c1.RGBA()
c2r, c2g, c2b, c2a := c2.RGBA()
return float64(c1r - c2r), float64(c1g - c2g), float64(c1b - c2b), float64(c1a - c2a)
}
func gradientFor(src image.Image) image.Image {
gradient := image.NewGray(src.Bounds())
draw.Draw(gradient, src.Bounds(), src, src.Bounds().Min, draw.Src)
rect := gradient.Bounds()
max := rect.Max
for x := rect.Min.X; x < rect.Max.X; x++ {
for y := rect.Min.Y; y < rect.Max.Y; y++ {
clr := gradient.At(x, y)
clr1 := clr
clr2 := clr
nextX := x + 1
nextY := y + 1
if nextX < max.X {
clr1 = gradient.At(nextX, y)
}
if nextY < max.Y {
clr2 = gradient.At(x, nextY)
}
r, g, b, a := subtract(clr1, clr)
r1, g1, b1, a1 := subtract(clr2, clr)
rf := uint8(math.Abs(r) + math.Abs(r1))
gf := uint8(math.Abs(g) + math.Abs(g1))
bf := uint8(math.Abs(b) + math.Abs(b1))
af := uint8(math.Abs(a) + math.Abs(a1))
gradient.Set(x, y, color.RGBA{R: rf, G: gf, B: bf, A: af})
}
}
return gradient
}
func main() {
img, err := loadImage("bb.jpg")
if err != nil {
log.Fatal("Oh noes!!! ", err)
}
gradient := gradientFor(img)
output, _ := os.OpenFile("other.jpg", os.O_WRONLY|os.O_CREATE, 0644)
defer output.Sync()
defer output.Close()
err = jpeg.Encode(output, gradient, nil)
if err != nil {
log.Fatal(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment