Compare commits

..

2 Commits

Author SHA1 Message Date
Dmitry Fedotov
c3b2036713 WIP 2025-03-25 23:55:13 +03:00
Dmitry Fedotov
a95b833004 add Makefile 2025-03-25 22:18:06 +03:00
4 changed files with 34 additions and 66 deletions

View File

@@ -16,4 +16,4 @@ This will generate an image in current directory.
See **./barnsley-fern --help** for available flags.
Image on this page was generated with **./barnsley-fern-go -x 500 -y 500 -c 3 -out example/barnsley-fern.png**
Image on this page was generated with **./barnsley-fern-go -h 500 -v 500 -rainbow**

View File

@@ -2,40 +2,21 @@ package main
import (
"errors"
"fmt"
"image"
"image/color"
"image/jpeg"
"image/png"
"io"
)
var (
errEncodeImage = errors.New("не удалось создать изображение")
errUnknownFormat = errors.New("неизвестный формат изображения")
)
var erreEcnodePng = errors.New("не удалось создать png изображение")
func createBarnsleyFernImage(s *settings, w io.Writer) error {
func createBarnsleyFernPng(s *settings, w io.Writer) error {
img := newImageWithBackGround(s.X, s.Y, s.BG)
drawBarnsleyFern(img, newColorFunc(s.CM), s.Dots)
switch s.OM {
case outputjpeg:
opts := &jpeg.Options{
Quality: 100,
}
if err := jpeg.Encode(w, img, opts); err != nil {
return errors.Join(errEncodeImage, err)
}
case outputpng:
if err := png.Encode(w, img); err != nil {
return errors.Join(errEncodeImage, err)
}
default:
return fmt.Errorf("%w: %d", errUnknownFormat, s.OM)
if err := png.Encode(w, img); err != nil {
return errors.Join(erreEcnodePng, err)
}
return nil

30
main.go
View File

@@ -6,30 +6,24 @@ import (
)
func main() {
exitCode := run()
os.Exit(exitCode)
}
filename, settings := parseSettings()
func run() int {
s := parseSettings()
file, err := os.Create(s.Fname)
file, err := os.Create(filename)
if err != nil {
fmt.Println("ошибка открытия файла", err)
return 1
return
}
if err := createBarnsleyFernImage(s, file); err != nil {
defer func() {
if err := file.Close(); err != nil {
fmt.Println("ошибка закрытия файла", err)
}
}()
if err := createBarnsleyFernPng(settings, file); err != nil {
fmt.Println("ошибка создания изображения:", err)
return 2
return
}
if err := file.Close(); err != nil {
fmt.Println("ошибка закрытия файла:", err)
return 3
}
fmt.Println("done")
return 0
fmt.Println("done...")
}

View File

@@ -2,7 +2,6 @@ package main
import (
"flag"
"fmt"
"image/color"
)
@@ -19,45 +18,39 @@ type outputmode int
const (
outputpng outputmode = iota
outputjpeg
outputvideo
)
type settings struct {
X, Y int // image size
Dots int // dots to draw for single image
BG color.Color // background color
CM colormode // settings for colorFunc
OM outputmode // what to output
Fname string // filename for output
X, Y int // image size
Dots int // dots to draw for single image
BG color.Color // background color
CM colormode // settings for colorFunc
}
func parseSettings() *settings {
func parseSettings() (string, *settings) {
var (
x, y int
dots int
cmode int
output int
fname string
x, y int
dots int
cmode int
fname string
)
flag.IntVar(&x, "x", 1920, "размер картинки по горизонтали")
flag.IntVar(&y, "y", 1080, "размер картинки по вертикали")
flag.IntVar(&dots, "d", 100000, "сколько точек рисовать")
flag.IntVar(&cmode, "c", 0, "color mode: 0 - default green, 1 - random, 2 - timed, 3 - rainbow")
flag.IntVar(&output, "o", 0, fmt.Sprintf("режим вывода: %d - png, %d - jpeg", outputpng, outputjpeg))
flag.IntVar(&cmode, "color", 0, "color mode: 0 - default green, 1 - random, 2 - timed, 3 - rainbow")
flag.StringVar(&fname, "out", "barnsley-fern.png", "полный путь файла для записи изображения")
flag.Parse()
s := &settings{
X: x,
Y: y,
Dots: dots,
BG: color.White,
CM: colormode(cmode),
OM: outputmode(output),
Fname: fname,
X: x,
Y: y,
Dots: dots,
BG: color.White,
CM: colormode(cmode),
}
return s
return fname, s
}