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

課題1 waytkheming #12

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
17 changes: 17 additions & 0 deletions kadai1/waytkheming/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
.DS_Store

# 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/

16 changes: 16 additions & 0 deletions kadai1/waytkheming/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# 課題1

## 次の仕様を満たすコマンドを作って下さい

- ディレクトリを指定する
- 指定したディレクトリ以下のJPGファイルをPNGに変換(デフォルト)
- ディレクトリ以下は再帰的に処理する
- 変換前と変換後の画像形式を指定できる(オプション)

## 以下を満たすように開発してください

- mainパッケージと分離する
- 自作パッケージと標準パッケージと準標準パッケージのみ使う
- 準標準パッケージ:golang.org/x以下のパッケージ
- ユーザ定義型を作ってみる
- GoDocを生成してみる
77 changes: 77 additions & 0 deletions kadai1/waytkheming/cli/cli.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
このパッケージはImageconvパッケージを起動するためのパッケージになります。

*/

package cli

// Package cli is run Image convert function

import (
"flag"
"fmt"
"io"
"os"
"path/filepath"
"sync"

"github.com/waytkheming/godojo/dojo7/kadai1/waytkheming/imageconv"
)

var (
from, to string
wg sync.WaitGroup
)

// Exit code.
const (
ExitCodeOK = 0
)

// CLI -> cli struct
type CLI struct {
outStream, errStream io.Writer
}

// NewCLI -> Initialize CLI
func NewCLI(outStream, errStream io.Writer) *CLI {
return &CLI{outStream: outStream, errStream: errStream}
}

// Run -> run cli
func (c *CLI) Run(args []string) int {
flags := flag.NewFlagSet("convert", flag.ContinueOnError)
flags.SetOutput(c.errStream)
flags.StringVar(&from, "from", "jpg",
"input file format (support: jpg/png/gif, default: jpg)")
flags.StringVar(&from, "f", "jpg",
"input file format (support: jpg/png/gif, default: jpg)")
flags.StringVar(&to, "to", "png",
"output file format (support: jpg/png/gif, default: png)")
flags.StringVar(&to, "t", "png",
"output file format (support: jpg/png/gif, default: png)")
flags.Parse(args[1:])
path := flags.Arg(0)

converter := imageconv.NewConverter(path, from, to)
fmt.Println(converter)
err := filepath.Walk(converter.Path, converter.CrawlFile)

if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}

queue := make(chan imageconv.ImageFile)
for _, image := range converter.Images {
wg.Add(1)
go converter.GetImages(queue, &wg)
queue <- image
}

close(queue)
wg.Wait()

return ExitCodeOK

}
22 changes: 22 additions & 0 deletions kadai1/waytkheming/imageconv/image.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package imageconv

import (
"path/filepath"
"regexp"
)

//ImageFile -> Image struct
type ImageFile struct {
Path string
Name string
Format string
}

// NewImage -> Initialize ImageFile
func NewImage(path string) ImageFile {
format := filepath.Ext(path)
rep := regexp.MustCompile(format + "$")
name := filepath.Base(rep.ReplaceAllString(path, ""))

return ImageFile{Path: path, Name: name, Format: format}
}
124 changes: 124 additions & 0 deletions kadai1/waytkheming/imageconv/imageconv.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/*
このパッケージは、画像ファイルをpng,jpg,gifからpng,jpg,gifへ変換する機能を持っています。

*/
package imageconv

import (
"image"
"image/gif"
"image/jpeg"
"image/png"
"io"
"os"
"path/filepath"
"strings"
"sync"
)

// Converter -> Converter struct
type Converter struct {
Path string
Images []ImageFile
From string
To string
}

// NewConverter -> Initialize ImageConverter
func NewConverter(path string, from string, to string) Converter {
return Converter{Path: path, From: from, To: to}
}

// GetImages is queuing imageFile
func (c *Converter) GetImages(q chan ImageFile, wg *sync.WaitGroup) {
for {
image, more := <-q
if more {
_ = c.Convert(image)
} else {
wg.Done()
return
}
}
}

//Convert -> Convert Image FIl
func (c *Converter) Convert(i ImageFile) error {
file, err := os.Open(i.Path)
if err != nil {
return err
}
defer file.Close()

image, err := c.decode(file)
if err != nil {
return err
}
outFile, err := os.Create(strings.TrimSuffix(c.Path, c.From) + c.To)
if err != nil {
return err
}
defer outFile.Close()

err = c.encode(outFile, image)

if err != nil {
return err
}

return nil
}

// CrawlFile -> found image file and append Converter.Files
func (c *Converter) CrawlFile(path string, info os.FileInfo, err error) error {
if checkExtension(filepath.Ext(path)) == ("." + c.From) {
if !info.IsDir() {
c.Images = append(c.Images, NewImage(path))
}
}
return nil
}

func checkExtension(path string) string {
if path == ".jpeg" {
return ".jpg"
}
return path
}

func (c *Converter) decode(file io.Reader) (image.Image, error) {
var (
img image.Image
err error
)
switch c.From {
case "jpeg", "jpg", "JPG", "JPEG":
img, err = jpeg.Decode(file)
case "gif", "GIF":
img, err = gif.Decode(file)
case "png", "PNG":
img, err = png.Decode(file)
}
if err != nil {
return nil, err
}

return img, nil
}

func (c *Converter) encode(file io.Writer, img image.Image) error {
var err error
switch c.To {
case "jpeg", "jpg", "JPG", "JPEG":
err = jpeg.Encode(file, img, nil)
case "gif", "GIF":
err = gif.Encode(file, img, nil)
case "png", "PNG":
err = png.Encode(file, img)
}
if err != nil {
return err
}

return nil
}
14 changes: 14 additions & 0 deletions kadai1/waytkheming/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package main

import (
"fmt"
"os"

"github.com/waytkheming/godojo/dojo7/kadai1/waytkheming/cli"
)

func main() {
fmt.Println("Start running CLI...")
cli := cli.NewCLI(os.Stdout, os.Stderr)
os.Exit(cli.Run(os.Args))
}
Binary file added kadai1/waytkheming/screenshots/godoc-cli-pkg.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added kadai1/waytkheming/testdata/earthmap1k.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added kadai1/waytkheming/testdata/earthmap1k.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added kadai1/waytkheming/testdata/earthmap1k.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.