Skip to content

Commit 0ffe15a

Browse files
committed
init
0 parents  commit 0ffe15a

7 files changed

+162
-0
lines changed

go.mod

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module github.com/GoEntity/godotify
2+
3+
go 1.20
4+
5+
require github.com/disintegration/imaging v1.6.2
6+
7+
require golang.org/x/image v0.8.0 // indirect

go.sum

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
github.com/disintegration/imaging v1.6.2 h1:w1LecBlG2Lnp8B3jk5zSuNqd7b4DXhcjwek1ei82L+c=
2+
github.com/disintegration/imaging v1.6.2/go.mod h1:44/5580QXChDfwIclfc/PCwrr44amcmDAg8hxG0Ewe4=
3+
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
4+
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
5+
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
6+
golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8 h1:hVwzHzIUGRjiF7EcUjqNxk3NCfkPxbDKRdnNE1Rpg0U=
7+
golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
8+
golang.org/x/image v0.8.0 h1:agUcRXV/+w6L9ryntYYsF2x9fQTMd4T8fiiYXAVW6Jg=
9+
golang.org/x/image v0.8.0/go.mod h1:PwLxp3opCYg4WR2WO9P0L6ESnsD6bLTWcw8zanLMVFM=
10+
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
11+
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
12+
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
13+
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
14+
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
15+
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
16+
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
17+
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
18+
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
19+
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
20+
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
21+
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
22+
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
23+
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
24+
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
25+
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
26+
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
27+
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
28+
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
29+
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
30+
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
31+
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
32+
golang.org/x/text v0.10.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
33+
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
34+
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
35+
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
36+
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
37+
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

pkg/godotify.go

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package dotify
2+
3+
import (
4+
"image"
5+
"image/jpeg"
6+
"image/png"
7+
"os"
8+
"path/filepath"
9+
"strings"
10+
11+
"github.com/disintegration/imaging"
12+
)
13+
14+
type DottyConfig struct {
15+
Intensity float64
16+
}
17+
18+
func GoDotify(inputFile string, outputFile string, config DottyConfig) error {
19+
20+
//openfile
21+
infile, err := os.Open(inputFile)
22+
if err != nil {
23+
return err
24+
}
25+
defer infile.Close()
26+
27+
//decode
28+
src, err := imaging.Decode(infile)
29+
if err != nil {
30+
return err
31+
}
32+
33+
//to make it easier for humans (... reverse intensity val 0 - 1)
34+
intensity := 1.1 - config.Intensity
35+
if intensity < 0 {
36+
intensity = 0
37+
} else if intensity > 1 {
38+
intensity = 1
39+
}
40+
41+
//make img smaller
42+
smaller := imaging.Resize(src, int(float64(src.Bounds().Dx())*0.1*intensity), 0, imaging.Lanczos)
43+
44+
//make img bigger again
45+
godotified := imaging.Resize(smaller, src.Bounds().Dx(), src.Bounds().Dy(), imaging.NearestNeighbor)
46+
47+
//openfile (output)
48+
outfile, err := os.Create(outputFile)
49+
if err != nil {
50+
return err
51+
}
52+
defer outfile.Close()
53+
54+
//encode and write output file
55+
ext := strings.ToLower(filepath.Ext(outputFile))
56+
if ext == ".png" {
57+
err = png.Encode(outfile, godotified)
58+
} else if ext == ".jpg" || ext == ".jpeg" {
59+
err = jpeg.Encode(outfile, godotified, nil)
60+
} else {
61+
return image.ErrFormat
62+
}
63+
if err != nil {
64+
return err
65+
}
66+
67+
return nil
68+
}

test/godotify_test.go

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package godotify_test
2+
3+
import (
4+
"os"
5+
"testing"
6+
7+
godotify "github.com/GoEntity/godotify/pkg"
8+
)
9+
10+
func TestGoDotify(t *testing.T) {
11+
testCases := []struct {
12+
name string
13+
inputFile string
14+
outputFile string
15+
intensity float64
16+
expectedErr bool
17+
}{
18+
{
19+
name: "Test valid PNG",
20+
inputFile: "test_valid.png",
21+
outputFile: "test_valid_output.png",
22+
intensity: 0.5,
23+
},
24+
{
25+
name: "Test invalid file",
26+
inputFile: "test_invalid.txt",
27+
outputFile: "test_invalid_output.png",
28+
intensity: 0.5,
29+
expectedErr: true,
30+
},
31+
}
32+
33+
for _, tc := range testCases {
34+
t.Run(tc.name, func(t *testing.T) {
35+
err := godotify.GoDotify(tc.inputFile, tc.outputFile, godotify.DottyConfig{Intensity: tc.intensity})
36+
37+
if tc.expectedErr && err == nil {
38+
t.Errorf("error expected but got none")
39+
} else if !tc.expectedErr && err != nil {
40+
t.Errorf("didnt expect an error but got one: %v", err)
41+
}
42+
43+
if !tc.expectedErr {
44+
if _, err := os.Stat(tc.outputFile); os.IsNotExist(err) {
45+
t.Errorf("didnt make output file")
46+
}
47+
}
48+
})
49+
}
50+
}

test/test_invalid.png

9.1 KB
Loading

test/test_valid.png

9.1 KB
Loading

test/test_valid_output.png

7.48 KB
Loading

0 commit comments

Comments
 (0)