Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor and CI #2

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Created by .ignore support plugin (hsz.mobi)
### Go template
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out*

# Dependency directories (remove the comment below to include it)
\vendor/
\tools/vendor/

\.idea/

\dist/

\.vscode/
\bin/

.DS_Store
32 changes: 32 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
linters:
disable-all: true
enable:
- errcheck
- gofmt
- golint
- goimports

linters-settings:
errcheck:
check-type-assertions: true
check-blank: true
gofmt:
simplify: true
golint:
min-confidence: 0
goimports:
local-prefixes: github.com/eleby/

run:
issues-exit-code: 0
tests: true
skip-dirs:
- vendor/
skip-files:
- \.pb\.go$

issues:
exclude-use-default: false
exclude:
# for "public interface + private struct implementation" cases only!
- exported func * returns unexported type *, which can be annoying to use
73 changes: 73 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
release:
github:
owner: eleby
name: pixelizer

env:
- GO111MODULE=on
- GOPROXY=https://goproxy.io
before:
hooks:
- go mod tidy -v

builds:
- id: cli
binary: pixelizer
goos:
- linux
- darwin
- windows
goarch:
- 386
- amd64
- arm
- arm64
mod_timestamp: '{{ .CommitTimestamp }}'
env:
- CGO_ENABLED=0
main: ./cmd/pixelizer
flags:
- -trimpath
ldflags:
- -s -w -X main.version={{ .Version }} -X main.commit={{ .FullCommit }} -X main.date={{ .Date }} -X main.goversion={{ .Env.GOVERSION }}

archives:
- id: cli
builds:
- cli
format: tar.gz
wrap_in_directory: true
format_overrides:
- goos: windows
format: zip
replacements:
amd64: 64-bit
386: 32-bit
darwin: macOS
linux: Tux
name_template: "{{ .Binary }}-{{ .Version }}-{{ .Os }}-{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}"
files:
- LICENSE
- README.md
checksum:
name_template: "{{ .ProjectName }}-{{ .Version }}-checksums.txt"

snapshot:
name_template: SNAPSHOT-{{ .Commit }}

changelog:
sort: asc
filters:
exclude:
- "^docs:"
- "^test:"
- "^dev:"
- "README"
- "^readme"
- "^scripts"
- "scripts:"
- Merge pull request
- Merge branch
- Rebase on master
- "^Makefile"
- "goreleaser"
6 changes: 3 additions & 3 deletions main/console.go → cmd/pixelizer/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (
)

const (
NONE = iota
DEBUG = iota
INFO = iota
NONE = iota
DEBUG
INFO
)

var LogLevel = NONE
Expand Down
55 changes: 41 additions & 14 deletions main/image.go → cmd/pixelizer/image.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"fmt"
"image"
"image/color"
"image/color/palette"
Expand All @@ -9,17 +10,19 @@ import (
"image/png"
"os"
"strconv"

log "github.com/sirupsen/logrus"
)

const GifDelayEachFrame = 20

func launchPixellisator(f *os.File, min int, count int, increase int) {
func launchPixellisator(f *os.File, min int, count int, increase int) error {
logIfVerbose(DEBUG, "launchPixellisator[ %v / %v / %v / %v ]", f.Name(), min, count, increase)
img, _, errImageDecode := image.Decode(f)
logIfExists(errImageDecode)
eleby marked this conversation as resolved.
Show resolved Hide resolved
if errImageDecode != nil {
return
img, _, err := image.Decode(f)
if err != nil {
return fmt.Errorf("image decode: %w", err)
}

b := img.Bounds()
minX := b.Min.X
minY := b.Min.Y
Expand All @@ -35,35 +38,52 @@ func launchPixellisator(f *os.File, min int, count int, increase int) {
if count > 1 {
name = "result" + strconv.Itoa(i+1) + ".png"
}
out, errOutput := os.Create(FileDirectory + name)
logIfExists(errOutput)
errOutputEncode := png.Encode(out, newImg)
logIfExists(errOutputEncode)
out, err := os.Create(FileDirectory + name)
if err != nil {
log.Error(err)
}

if err := png.Encode(out, newImg); err != nil {
log.Error(err)
}

if hasParam("gif") && !hasParam("pixel") {
addToGif(&gifImg, newImg)
}

if hasParam("print") {
printInTerminal(newImg, min+(i*increase), i)
}

if i+1 == count && hasParam("gif") && !hasParam("pixel") {
if hasParam("reverse") {
reverseGif(&gifImg)
}

if hasParam("full") {
addReverseToGif(&gifImg)
}
gifOutput, errGif := os.Create(FileDirectory + "results.gif")
logIfExists(errGif)
errGifEncode := gif.EncodeAll(gifOutput, &gifImg)
logIfExists(errGifEncode)

gifOutput, err := os.Create(FileDirectory + "results.gif")
if err != nil {
log.Error(err)
}

if err := gif.EncodeAll(gifOutput, &gifImg); err != nil {
log.Error(err)
}
}
}

return nil
}

func repixellise(img image.Image, pointMin image.Point, pointMax image.Point, pixelization int) image.Image {
logIfVerbose(DEBUG, "repixellise[ %v / %v / %v ]", pointMin, pointMax, pixelization)

i := 0
j := 0

var result *image.RGBA

if hasParam("pixel") {
Expand All @@ -72,6 +92,7 @@ func repixellise(img image.Image, pointMin image.Point, pointMax image.Point, pi
} else {
result = image.NewRGBA(image.Rectangle{Min: pointMin, Max: pointMax})
}

for y := pointMin.Y; y < pointMax.Y; y += pixelization {
for x := pointMin.X; x < pointMax.X; x += pixelization {
colorRGBA := avg(img, x, y, pixelization)
Expand All @@ -85,11 +106,13 @@ func repixellise(img image.Image, pointMin image.Point, pointMax image.Point, pi
i = 0
j++
}

return result
}

func avg(img image.Image, x int, y int, count int) color.RGBA {
logIfVerbose(INFO, "avg[ { %v ; %v } / %v ]", x, y, count)

total := count * count

r := 0
Expand Down Expand Up @@ -140,6 +163,7 @@ func addToGif(gifImg *gif.GIF, img image.Image) {

func addReverseToGif(gifImg *gif.GIF) {
logIfVerbose(DEBUG, "addReverseToGif")

for i := len(gifImg.Image) - 2; i > 0; i-- {
gifImg.Image = append(gifImg.Image, gifImg.Image[i])
gifImg.Delay = append(gifImg.Delay, GifDelayEachFrame)
Expand All @@ -148,9 +172,12 @@ func addReverseToGif(gifImg *gif.GIF) {

func reverseGif(gifImg *gif.GIF) {
logIfVerbose(DEBUG, "reverseGif")
var images []*image.Paletted

var images = make([]*image.Paletted, 0, len(gifImg.Image))
eleby marked this conversation as resolved.
Show resolved Hide resolved

for i := len(gifImg.Image) - 1; i >= 0; i-- {
images = append(images, gifImg.Image[i])
}

gifImg.Image = images
}
47 changes: 35 additions & 12 deletions main/main.go → cmd/pixelizer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,57 +4,80 @@ import (
_ "image/jpeg"
_ "image/png"
"os"

log "github.com/sirupsen/logrus"
)

var WorkingDirectory string
var FileDirectory string
var SaveDir string
var AppName = "pixelizer"

func main() {
printVersion()

if len(os.Args) < 2 || os.Args[1] == "man" || os.Args[1] == "help" {
printManual()
return
}

initLogLevel()

mainParam := os.Args[1]

if mainParam == "clear" {
switch mainParam {
case "clear":
setSaveDir()
readAndGetParams()
setPreviousWorkingDirectory()
addWorkingDirToArgs()
setFileDirectory()
clearResults()
} else if mainParam == "redo" {
case "redo":
setSaveDir()
readAndGetParams()
setPreviousWorkingDirectory()
addWorkingDirToArgs()
setFileDirectory()

img := os.Args[1]
min := param(2)
count := param(3)
increase := param(4)
f, errOpenImg := os.Open(FileDirectory + getNameOfFile(img))
logIfExists(errOpenImg)
if errOpenImg != nil {

f, err := os.Open(FileDirectory + getNameOfFile(img))
if err != nil {
log.Error(err)
return
}
launchPixellisator(f, min, count, increase)
} else {

if err := launchPixellisator(f, min, count, increase); err != nil {
eleby marked this conversation as resolved.
Show resolved Hide resolved
log.Error(err)
return
}

default:
setDirVariables()

os.Args = append(os.Args, WorkingDirectory)
f, errOpenImage := os.Open(mainParam)
logIfExists(errOpenImage)
if errOpenImage != nil {

f, err := os.Open(mainParam)
if err != nil {
log.Error(err)

return
}

defer f.Close()

min := param(2)
count := param(3)
increase := param(4)
launchPixellisator(f, min, count, increase)

if err := launchPixellisator(f, min, count, increase); err != nil {
log.Error(err)
return
}

save()
}
}
Loading