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

kadai1 ramenjuniti #11

Open
wants to merge 2 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
14 changes: 14 additions & 0 deletions kadai1/ramenjuniti/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
BIN := iconv

.PHONY: test
test:
go test -cover -v ./...

.PHONY: build
build: test
go build -o $(BIN)

.PHONY: clean
clean:
rm $(BIN)
go clean
31 changes: 31 additions & 0 deletions kadai1/ramenjuniti/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# iconv

## Description

課題1(画像変換コマンドを作ろう)を実装したもの

以下のパッケージで構成されています

- main
- target
- imgconv

## Usage

```
iconv -in {変換前の形式} -out {変換したい形式} {ディレクトリ}
```

jpg, png, gif, tif, bmp に対応しています

## Build

```bash
make build
```

## Test

```bash
make test
```
5 changes: 5 additions & 0 deletions kadai1/ramenjuniti/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/gopherdojo/dojo5/kadai1/ramenjuniti

go 1.12

require golang.org/x/image v0.0.0-20190523035834-f03afa92d3ff
3 changes: 3 additions & 0 deletions kadai1/ramenjuniti/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
golang.org/x/image v0.0.0-20190523035834-f03afa92d3ff h1:+2zgJKVDVAz/BWSsuniCmU1kLCjL88Z8/kv39xCI9NQ=
golang.org/x/image v0.0.0-20190523035834-f03afa92d3ff/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
69 changes: 69 additions & 0 deletions kadai1/ramenjuniti/imgconv/imgconv.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package imgconv

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

"golang.org/x/image/bmp"
"golang.org/x/image/tiff"
)

// ImgData デコードされた画像データが入る
type ImgData struct {
Path string
Data image.Image
}

// Decode pathの画像をデコードし、ImgDataに入れる
func Decode(path string) (*ImgData, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()

img, _, err := image.Decode(file)
if err != nil {
return nil, err
}

return &ImgData{Path: path, Data: img}, nil
}

// Encode iのDataをout形式のファイルにエンコードし、ファイル作成
// jpg, png, gif, tif, bmpに対応
func (i *ImgData) Encode(out string) error {
file, err := os.Create(rmExt(i.Path) + "." + out)
if err != nil {
return err
}

switch out {
case "png":
err = png.Encode(file, i.Data)
case "jpg", "jpeg":
err = jpeg.Encode(file, i.Data, nil)
case "gif":
err = gif.Encode(file, i.Data, nil)
case "tif", "tiff":
err = tiff.Encode(file, i.Data, nil)
case "bmp":
err = bmp.Encode(file, i.Data)
}

if err != nil {
return err
}
defer file.Close()

return nil
}

// rmExt pathの拡張子を除いた文字列を返す
func rmExt(path string) string {
return path[:len(path)-len(filepath.Ext(path))]
}
64 changes: 64 additions & 0 deletions kadai1/ramenjuniti/imgconv/imgconv_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package imgconv

import (
"os"
"testing"
)

var tests = []struct {
name string
path string
out string
outFile string
}{
{
name: "jpg to png",
path: "../testdata/imgconv/sample.jpg",
out: "png",
outFile: "../testdata/imgconv/sample.png",
},
{
name: "jpg to gif",
path: "../testdata/imgconv/sample.jpg",
out: "gif",
outFile: "../testdata/imgconv/sample.gif",
},
{
name: "jpg to tif",
path: "../testdata/imgconv/sample.jpg",
out: "tif",
outFile: "../testdata/imgconv/sample.tif",
},
{
name: "jpg to bmp",
path: "../testdata/imgconv/sample.jpg",
out: "bmp",
outFile: "../testdata/imgconv/sample.bmp",
},
}

func TestConvert(t *testing.T) {
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
img, err := Decode(test.path)
if err != nil {
t.Fatalf("unexpected error %v", err)
}

err = img.Encode(test.out)
if err != nil {
t.Fatalf("unexpected error %v", err)
}

_, err = os.Stat(test.outFile)
if err != nil {
t.Fatalf("did not create %v", test.outFile)
}

err = os.Remove(test.outFile)
if err != nil {
t.Fatalf("cannot remove %v", test.outFile)
}
})
}
}
71 changes: 71 additions & 0 deletions kadai1/ramenjuniti/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package main

import (
"flag"
"log"

"github.com/gopherdojo/dojo5/kadai1/ramenjuniti/imgconv"
"github.com/gopherdojo/dojo5/kadai1/ramenjuniti/target"
)

func main() {
in := flag.String("in", "jpg", "file type before conversion")
out := flag.String("out", "png", "file type to convert")
formats := []string{"jpg", "png", "gif", "tif", "bmp"}

flag.Parse()
args := flag.Args()

if len(args) == 0 {
log.Fatal("directory not specified")
}

if *in == "jpeg" {
*in = "jpg"
}
if *in == "tiff" {
*in = "tif"
}
if *out == "jpeg" {
*out = "jpg"
}
if *out == "tiff" {
*out = "tif"
}

if !canConv(*in, formats) {
log.Fatalf("cannot convert %v file", *in)
}
if !canConv(*out, formats) {
log.Fatalf("cannot convert %v file", *out)
}

for _, arg := range args {
targets, err := target.Get(arg, *in)
if err != nil {
log.Fatal(err)
}

for _, t := range targets {
img, err := imgconv.Decode(t)
if err != nil {
log.Fatal(err)
}

err = img.Encode(*out)
if err != nil {
log.Fatal(err)
}
}
}
}

// canConv tがconvに含まれるかどうかをboolで返す
func canConv(f string, formats []string) bool {
for _, v := range formats {
if f == v {
return true
}
}
return false
}
25 changes: 25 additions & 0 deletions kadai1/ramenjuniti/target/target.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package target

import (
"os"
"path/filepath"
)

// Get root下のin形式のファイルのパスが入ったsliceとerrを返す
func Get(root, in string) ([]string, error) {
targets := []string{}

err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if filepath.Ext(path) == "."+in {
targets = append(targets, path)
}

return nil
})

if err != nil {
return targets, err
}

return targets, nil
}
84 changes: 84 additions & 0 deletions kadai1/ramenjuniti/target/target_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package target

import (
"reflect"
"testing"
)

var tests = []struct {
name string
root string
in string
targets []string
}{
{
name: "get jpg files",
root: "../testdata/target",
in: "jpg",
targets: []string{
"../testdata/target/jpg/sample1.jpg",
"../testdata/target/jpg/sample2.jpg",
"../testdata/target/jpg/sample3.jpg",
},
},
{
name: "get png files",
root: "../testdata/target",
in: "png",
targets: []string{
"../testdata/target/png/sample1.png",
"../testdata/target/png/sample2.png",
"../testdata/target/png/sample3.png",
},
},
{
name: "get gif files",
root: "../testdata/target",
in: "gif",
targets: []string{
"../testdata/target/gif/sample1.gif",
"../testdata/target/gif/sample2.gif",
"../testdata/target/gif/sample3.gif",
},
},
{
name: "get tif files",
root: "../testdata/target",
in: "tif",
targets: []string{
"../testdata/target/tif/sample1.tif",
"../testdata/target/tif/sample2.tif",
"../testdata/target/tif/sample3.tif",
},
},
{
name: "get bmp files",
root: "../testdata/target",
in: "bmp",
targets: []string{
"../testdata/target/bmp/sample1.bmp",
"../testdata/target/bmp/sample2.bmp",
"../testdata/target/bmp/sample3.bmp",
},
},
{
name: "no file",
root: "../testdata/target",
in: "hoge",
targets: []string{},
},
}

func TestGet(t *testing.T) {
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
got, err := Get(test.root, test.in)
if err != nil {
t.Fatalf("unexpected error %v", err)
}
if !reflect.DeepEqual(got, test.targets) {
t.Fatalf("got targets: %v, want targets: %v", got, test.targets)
}
})
}
}
Binary file added kadai1/ramenjuniti/testdata/imgconv/sample.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 not shown.
Binary file not shown.
Binary file not shown.
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.
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.
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.
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.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
Binary file not shown.