開発環境
- macOS Mojave - Apple (OS)
- Emacs (Text Editor)
- Windows 10 Pro (OS)
- Visual Studio Code (Text Editor)
- Go (プログラミング言語)
プログラミング言語Go (ADDISON-WESLEY PROFESSIONAL COMPUTING SERIES) (Alan A.A. Donovan(著)、Brian W. Kernighan(著)、柴田 芳樹(翻訳)、丸善出版)の第3章(基本データ型)、3.3(複素数)、練習問題3.6の解答を求めてみる。
コード
package main
import (
"fmt"
"image"
"image/color"
"image/png"
"os"
)
func main() {
const (
width, height = 15, 15
)
img := image.NewRGBA(image.Rect(0, 0, width, height))
for py := 0; py < height; py++ {
for px := 0; px < width; px++ {
var c color.Color
if py < height/3 {
if px < width/3 {
c = color.RGBA{255, 0, 0, 255}
} else if px < 2*width/3 {
c = color.RGBA{0, 255, 0, 255}
} else {
c = color.RGBA{0, 0, 255, 255}
}
} else if py < 2*height/3 {
if px < width/3 {
c = color.RGBA{0, 255, 0, 255}
} else if px < 2*width/3 {
c = color.RGBA{0, 0, 255, 255}
} else {
c = color.RGBA{255, 0, 0, 255}
}
} else {
if px < width/3 {
c = color.RGBA{0, 0, 255, 255}
} else if px < 2*width/3 {
c = color.RGBA{255, 0, 0, 255}
} else {
c = color.RGBA{0, 255, 0, 255}
}
}
img.Set(px, py, c)
}
}
file, err := os.Create("sample6.png")
if err != nil {
fmt.Fprint(os.Stderr, err)
os.Exit(1)
}
defer file.Close()
err = png.Encode(file, img)
if err != nil {
fmt.Fprint(os.Stderr, err)
os.Exit(1)
}
file1, err := os.Open("sample6.png")
if err != nil {
fmt.Fprint(os.Stderr, err)
os.Exit(1)
}
defer file1.Close()
img1, err := png.Decode(file1)
if err != nil {
fmt.Fprint(os.Stderr, err)
os.Exit(1)
}
rect1 := img1.Bounds()
width1 := rect1.Max.X - rect1.Min.X
height1 := rect1.Max.Y - rect1.Min.Y
fmt.Println(rect1, width1, height1)
imgSuperSampling := image.NewRGBA(image.Rect(0, 0, 2*width1, 2*height1))
for py := 0; py < 2*height1; py++ {
for px := 0; px < 2*width1; px++ {
cx := px / 2
cy := py / 2
c1 := img1.At(cx-1, cy-1)
c2 := img1.At(cx, cy-1)
c3 := img1.At(cx+1, cy-1)
c4 := img1.At(cx-1, cy)
c5 := img1.At(cx, cy)
c6 := img1.At(cx+1, cy)
c7 := img1.At(cx-1, cy+1)
c8 := img1.At(cx, cy+1)
c9 := img1.At(cx+1, cy+1)
r1, g1, b1, a1 := c1.RGBA()
r2, g2, b2, a2 := c2.RGBA()
r3, g3, b3, a3 := c3.RGBA()
r4, g4, b4, a4 := c4.RGBA()
r5, g5, b5, a5 := c5.RGBA()
r6, g6, b6, a6 := c6.RGBA()
r7, g7, b7, a7 := c7.RGBA()
r8, g8, b8, a8 := c8.RGBA()
r9, g9, b9, a9 := c9.RGBA()
r := uint8((r1 + r2 + r3 + r4 + r5 + r6 + r7 + r8 + r9) / 9.0)
g := uint8((g1 + g2 + g3 + g4 + g5 + g6 + g7 + g8 + g9) / 9.0)
b := uint8((b1 + b2 + b3 + b4 + b5 + b6 + b7 + b8 + b9) / 9.0)
a := uint8((a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8 + a9) / 9.0)
imgSuperSampling.Set(px, py, color.RGBA{r, g, b, a})
}
}
fileSuperSampling, err := os.Create("sample6_supersampling.png")
if err != nil {
fmt.Fprint(os.Stderr, err)
os.Exit(1)
}
defer fileSuperSampling.Close()
png.Encode(fileSuperSampling, imgSuperSampling)
}
入出力結果(cmd(コマンドプロンプト)、Terminal)
C:\Users\...> go run sample6.go (0,0)-(15,15) 15 15 C:\Users\...>
sample6.png
sample6_supersampling.png
ギザギザの影響は多少減った気がするけど、supersamplingってこういうことでいいのかはまだはっきりとは理解できてなかったり。実験用の元の画像が適してない可能性も。
0 コメント:
コメントを投稿