Created
November 17, 2011 19:56
-
-
Save joncooper/1374302 to your computer and use it in GitHub Desktop.
blog-instashred
This file contains hidden or 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
| type Image interface { | |
| image.Image | |
| Set(x, y int, c image.Color) | |
| } |
This file contains hidden or 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
| type Image interface { | |
| // ColorModel returns the Image's ColorModel. | |
| ColorModel() ColorModel | |
| // Bounds returns the domain for which At can return non-zero color. | |
| // The bounds do not necessarily contain the point (0, 0). | |
| Bounds() Rectangle | |
| // At returns the color of the pixel at (x, y). | |
| // At(Bounds().Min.X, Bounds().Min.Y) returns the upper-left pixel of the grid. | |
| // At(Bounds().Max.X-1, Bounds().Max.Y-1) returns the lower-right one. | |
| At(x, y int) Color | |
| } | |
| type ColorModel interface { | |
| Convert(c Color) Color | |
| } | |
| type Color interface { | |
| RGBA() (r, g, b, a uint32) | |
| } | |
| type Rectangle struct { | |
| Min, Max Point | |
| } | |
| type Point struct { | |
| X, Y int | |
| } |
This file contains hidden or 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
| func main() { | |
| random_image := new(RandomColorImage) | |
| output_image := image.NewRGBA(256, 256) | |
| draw.Draw(output_image, output_image.Bounds(), random_image, image.ZP, draw.Src) | |
| WritePNGFile("random_image.png", output_image) | |
| } |
This file contains hidden or 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
| type RandomColorImage struct{} | |
| func (rcm *RandomColorImage) ColorModel() image.ColorModel { | |
| return image.RGBAColorModel | |
| } | |
| func (rcm *RandomColorImage) Bounds() image.Rectangle { | |
| return image.Rectangle{image.Point{-1e9,-1e9}, image.Point{1e9,1e9}} | |
| } | |
| func (rcm *RandomColorImage) Opaque() bool { | |
| return true | |
| } | |
| func (rcm *RandomColorImage) At(x, y int) image.Color { | |
| seed := rand.Uint32() | |
| r := uint8((seed << 24) >> 24) | |
| g := uint8((seed << 16) >> 24) | |
| b := uint8((seed << 8) >> 24) | |
| a := uint8(0xFF) | |
| return image.NRGBAColor{r,g,b,a} | |
| } |
This file contains hidden or 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
| func WritePNGFile(filename string, image image.Image) { | |
| f, err := os.Create(filename) | |
| if err != nil { | |
| log.Fatal(err) | |
| } | |
| defer f.Close() | |
| err = png.Encode(f, image) | |
| if err != nil { | |
| log.Fatal(err) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment