Skip to content

Kadai1 marltake #14

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

Closed
wants to merge 2 commits into from
Closed
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
14 changes: 14 additions & 0 deletions kadai1/marltake/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# 課題1回答
## 画像変換コマンド仕様
* 指定されたディレクトリ以下の画像ファイルを再帰的に検索して処理する
* デフォルトはJPGファイルをPNGファイルに変換
* オプションで変更前後の形式を指定可能
* 前後のフォーマットが同じオプションは指定不可
* 返還後のファイル名が存在する場合は、処理をskip
* -i 問い合わせる、-f 強制上書きとかできるかな
## 実装条件
* mainパッケージと分離する
* 自作パッケージと標準パッケージと準標準パッケージのみ使う
* 準標準パッケージ:golang.org/x以下のパッケージ
* ユーザ定義型を作ってみる
* GoDocを生成してみる
73 changes: 73 additions & 0 deletions kadai1/marltake/convert/convert.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package convert

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

func ConfigConvert(src string, dest string) func(string, os.FileInfo, error) error {
srcExt := "." + src
lenSrcExt := len(srcExt)
destExt := "." + dest
// TODO declare decode for src and encode for dest here
return func(path string, info os.FileInfo, err error) error {
println(path)
if strings.ToLower(filepath.Ext(path)) == srcExt {
destPath := path[:len(path)-lenSrcExt] + destExt
// TODO error handling
if _, err := os.Stat(destPath); os.IsNotExist(err) {
file, _ := os.Open(path)
defer file.Close()
var img image.Image
switch src {
case "jpg":
img, _ = jpeg.Decode(file)
case "png":
img, _ = png.Decode(file)
case "gif":
img, _ = gif.Decode(file)
}
destfile, _ := os.Create(destPath)
defer destfile.Close()
switch dest {
case "jpg":
jpeg.Encode(destfile, img, nil)
case "png":
png.Encode(destfile, img)
case "gif":
gif.Encode(destfile, img, nil)
}
} else {
println("skip not to over write.", path)
}
}
return nil
}
}

func ParseTarget(target string) (src string, dest string, ok bool) {
targets := strings.Split(target, ",")
allowedExt := map[string]bool{
"jpg": true,
"png": true,
"gif": true,
}
if len(targets) != 2 {
return "", "", false
}
if targets[0] == "" {
targets[0] = "Jpg"
}
if targets[1] == "" {
targets[1] = "png"
}
if targets[0] != targets[1] && allowedExt[targets[0]] && allowedExt[targets[1]] {
return targets[0], targets[1], true
}
return "", "", false
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

名前付きリターンを使うなら、src, dest, ok に代入して return で返すべき

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

import (
"flag"
"fmt"
"log"
"path/filepath"

"./convert"
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

相対 import は不可になる。

)

func main() {
var target = flag.String("t", "jpg,png", "source and destination picture type")
flag.Parse()
src, dest, ok := convert.ParseTarget(*target)
if !ok {
log.Fatal(fmt.Errorf("Invalid target option %s", *target))
}
err := filepath.Walk(flag.Arg(0), convert.ConfigConvert(src, dest))
if err != nil {
log.Fatal(err)
}
}