Compare commits

..

3 Commits

Author SHA1 Message Date
Dmitry Fedotov
3ab6d94944 tidy up 2025-04-27 12:23:28 +03:00
fe94e38589 add jpeg generation, refactor 2025-03-30 16:49:08 +03:00
Dmitry Fedotov
878e3d7c05 add Makefile 2025-03-25 22:18:31 +03:00
4 changed files with 66 additions and 34 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 -h 500 -v 500 -rainbow**
Image on this page was generated with **./barnsley-fern-go -x 500 -y 500 -c 3 -out example/barnsley-fern.png**

View File

@@ -2,21 +2,40 @@ package main
import (
"errors"
"fmt"
"image"
"image/color"
"image/jpeg"
"image/png"
"io"
)
var erreEcnodePng = errors.New("не удалось создать png изображение")
var (
errEncodeImage = errors.New("не удалось создать изображение")
errUnknownFormat = errors.New("неизвестный формат изображения")
)
func createBarnsleyFernPng(s *settings, w io.Writer) error {
func createBarnsleyFernImage(s *settings, w io.Writer) error {
img := newImageWithBackGround(s.X, s.Y, s.BG)
drawBarnsleyFern(img, newColorFunc(s.CM), s.Dots)
if err := png.Encode(w, img); err != nil {
return errors.Join(erreEcnodePng, err)
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)
}
return nil

30
main.go
View File

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

View File

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