Skip to content

Commit 7ec6ad2

Browse files
committed
copy files from kadai1/tanaka0325
1 parent 3e3ad91 commit 7ec6ad2

File tree

11 files changed

+257
-0
lines changed

11 files changed

+257
-0
lines changed

kadai2/tanaka0325/imgconv/args.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package imgconv
2+
3+
// Args is type for command line arguments.
4+
type Args []string
5+
6+
func (args Args) uniq() []string {
7+
m := map[string]bool{}
8+
u := []string{}
9+
10+
for _, v := range args {
11+
if !m[v] {
12+
m[v] = true
13+
u = append(u, v)
14+
}
15+
}
16+
17+
return u
18+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package main
2+
3+
import (
4+
"flag"
5+
"fmt"
6+
"os"
7+
8+
"github.com/gopherdojo/dojo8/kadai2/tanaka0325/imgconv"
9+
)
10+
11+
var options imgconv.Options
12+
var args imgconv.Args
13+
14+
func init() {
15+
options.From = flag.String("f", "jpg", "file extention before convert")
16+
options.To = flag.String("t", "png", "file extention after convert")
17+
options.DryRun = flag.Bool("n", false, "dry run")
18+
flag.Parse()
19+
20+
args = flag.Args()
21+
}
22+
23+
func main() {
24+
if err := imgconv.Run(options, args); err != nil {
25+
fmt.Fprintln(os.Stderr, err)
26+
os.Exit(1)
27+
}
28+
29+
os.Exit(0)
30+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package imgconv
2+
3+
import (
4+
"fmt"
5+
"image"
6+
"image/gif"
7+
"image/jpeg"
8+
"image/png"
9+
"io"
10+
"os"
11+
12+
"golang.org/x/image/bmp"
13+
"golang.org/x/image/tiff"
14+
)
15+
16+
// convImage is type included infomation to convert file format.
17+
type convImage struct {
18+
filename string
19+
fromExt string
20+
toExt string
21+
image image.Image
22+
}
23+
24+
func (i *convImage) decode() error {
25+
r, err := os.Open(i.filename + "." + i.fromExt)
26+
if err != nil {
27+
return err
28+
}
29+
defer r.Close()
30+
31+
img, err := decodeHelper(r, i.fromExt)
32+
if err != nil {
33+
return fmt.Errorf("decode error: %w", err)
34+
}
35+
36+
i.image = img
37+
return nil
38+
}
39+
40+
func decodeHelper(r io.Reader, ext string) (image.Image, error) {
41+
switch ext {
42+
case "png":
43+
return png.Decode(r)
44+
case "jpg", "jpeg":
45+
return jpeg.Decode(r)
46+
case "gif":
47+
return gif.Decode(r)
48+
case "bmp":
49+
return bmp.Decode(r)
50+
case "tiff", "tif":
51+
return tiff.Decode(r)
52+
}
53+
return nil, fmt.Errorf("%s is not allowed", ext)
54+
}
55+
56+
func (i *convImage) encode() error {
57+
w, err := os.Create(i.filename + "." + i.toExt)
58+
if err != nil {
59+
return err
60+
}
61+
defer func() error {
62+
if err := w.Close(); err != nil {
63+
return err
64+
}
65+
return nil
66+
}()
67+
68+
switch i.toExt {
69+
case "png":
70+
return png.Encode(w, i.image)
71+
case "jpg", "jpeg":
72+
return jpeg.Encode(w, i.image, nil)
73+
case "gif":
74+
return gif.Encode(w, i.image, nil)
75+
case "bmp":
76+
return gif.Encode(w, i.image, nil)
77+
case "tiff", "tif":
78+
return gif.Encode(w, i.image, nil)
79+
}
80+
81+
return fmt.Errorf("cannot encode image")
82+
}

kadai2/tanaka0325/imgconv/go.mod

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module github.com/gopherdojo/dojo8/kadai1/tanaka0325/imgconv
2+
3+
go 1.14
4+
5+
require golang.org/x/image v0.0.0-20200618115811-c13761719519

kadai2/tanaka0325/imgconv/go.mod.back

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module github.com/gopherdojo/dojo8/kadai1/tanaka0325/imgconv
2+
3+
go 1.14
4+
5+
require golang.org/x/image v0.0.0-20200618115811-c13761719519

kadai2/tanaka0325/imgconv/go.sum

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
golang.org/x/image v0.0.0-20200618115811-c13761719519 h1:1e2ufUJNM3lCHEY5jIgac/7UTjd6cgJNdatjPdFWf34=
2+
golang.org/x/image v0.0.0-20200618115811-c13761719519/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
3+
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=

kadai2/tanaka0325/imgconv/imgconv.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// Imgconv package is to convert images file format.
2+
package imgconv
3+
4+
import (
5+
"fmt"
6+
"os"
7+
"path/filepath"
8+
"strings"
9+
)
10+
11+
var allowedExts = []string{"png", "jpg", "jpeg", "gif", "bmp", "tiff", "tif"}
12+
13+
// Run is to convert image file format
14+
func Run(options Options, args Args) error {
15+
// validator
16+
if err := options.validate(allowedExts); err != nil {
17+
return err
18+
}
19+
20+
// get target image flepaths from args
21+
paths, err := getTargetFilePaths(args, *options.From)
22+
if err != nil {
23+
return err
24+
}
25+
26+
// convert
27+
imgs, err := createConvImages(paths, *options.From, *options.To)
28+
if err != nil {
29+
return err
30+
}
31+
for _, img := range imgs {
32+
if err := img.decode(); err != nil {
33+
return err
34+
}
35+
36+
if *options.DryRun {
37+
fmt.Println(img.filename+"."+img.fromExt, "=>", img.filename+"."+img.toExt)
38+
} else {
39+
if err := img.encode(); err != nil {
40+
return err
41+
}
42+
}
43+
}
44+
45+
return nil
46+
}
47+
48+
func getTargetFilePaths(args Args, from string) ([]string, error) {
49+
uns := args.uniq()
50+
paths := []string{}
51+
52+
for _, n := range uns {
53+
if err := filepath.Walk(n, func(path string, info os.FileInfo, err error) error {
54+
if filepath.Ext(path) == "."+from {
55+
paths = append(paths, path)
56+
}
57+
return nil
58+
}); err != nil {
59+
return nil, err
60+
}
61+
}
62+
63+
return paths, nil
64+
}
65+
66+
func createConvImages(paths []string, from, to string) ([]convImage, error) {
67+
images := []convImage{}
68+
for _, p := range paths {
69+
i := convImage{
70+
filename: strings.Replace(p, "."+from, "", 1),
71+
fromExt: strings.ToLower(from),
72+
toExt: strings.ToLower(to),
73+
}
74+
images = append(images, i)
75+
}
76+
77+
return images, nil
78+
}

kadai2/tanaka0325/imgconv/options.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package imgconv
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
"strings"
7+
)
8+
9+
// Args is type for command line options.
10+
type Options struct {
11+
From *string
12+
To *string
13+
DryRun *bool
14+
}
15+
16+
func (opt Options) validate(allow_list []string) error {
17+
to := strings.ToLower(*opt.To)
18+
from := strings.ToLower(*opt.From)
19+
targetExts := []string{to, from}
20+
for _, e := range targetExts {
21+
if err := include(allow_list, e); err != nil {
22+
return fmt.Errorf("%w. ext is only allowd in %s", err, allow_list)
23+
}
24+
}
25+
26+
return nil
27+
}
28+
29+
func include(list []string, w string) error {
30+
for _, e := range list {
31+
if e == w {
32+
return nil
33+
}
34+
}
35+
return errors.New(w + " is not allowed")
36+
}
10.7 KB
Loading
10.7 KB
Loading

0 commit comments

Comments
 (0)