Skip to content
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/gashiura/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, build with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

.DS_Store
11 changes: 11 additions & 0 deletions kadai1/gashiura/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
version: '3'
services:
app:
container_name: golang
image: golang:latest
volumes:
- ./src:/go/src
ports:
- "8080:8080"
- "6060:6060"
tty: true
Binary file added kadai1/gashiura/src/images/racket.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/gashiura/src/images/racket.jpeg
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/gashiura/src/images/racket.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
85 changes: 85 additions & 0 deletions kadai1/gashiura/src/imgconv/imgconv.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// Package imgconv is a lib to convert image formats.
package imgconv

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

type Converter struct {
// image extension before conversion
SrcExt string
// image extension after conversion
DstExt string
}

const (
extJpeg = "jpeg"
extPng = "png"
extGif = "gif"
)

/*
Convert converts the image format file specified as the conversion source under the specified directory
into the image format of the conversion destination
*/
func (conv Converter) Convert(rootDir string) error {
err := filepath.Walk(rootDir, func(path string, info os.FileInfo, err error) error {
if filepath.Ext(path) == "." + conv.SrcExt {
file, err := os.Open(path)
if err != nil { return err }
defer file.Close()

img, err := conv.decodeImage(file)
if err != nil { return err }

outPath := strings.Replace(path, conv.SrcExt, conv.DstExt, -1)

out, err := os.Create(outPath)
if err != nil { return err }
defer out.Close()

err = conv.encodeImage(out, img)
if err != nil { return err }
}
return nil
})

return err
}

func (conv Converter) decodeImage(file io.Reader) (img image.Image, err error) {
switch conv.SrcExt {
case extJpeg:
img, err = jpeg.Decode(file)
case extPng:
img, err = png.Decode(file)
case extGif:
img, err = gif.Decode(file)
default:
img, err = image.Image(nil), errors.New("extension is incorrect.")
}
return
}

func (conv Converter) encodeImage(file io.Writer, image image.Image) (err error) {
switch conv.DstExt {
case extJpeg:
opt := &jpeg.Options{Quality: 100}
err = jpeg.Encode(file, image, opt)
case extPng:
err = png.Encode(file, image)
case extGif:
err = gif.Encode(file, image, nil)
default:
err = errors.New("extension is incorrect.")
}
return
}
39 changes: 39 additions & 0 deletions kadai1/gashiura/src/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package main

import (
"flag"
"fmt"

"imgconv"
"option"
)

var (
s = flag.String("s", "jpeg", "source image format. support:[jpeg, png, gif]")
d = flag.String("d", "png", "destination image format. support:[jpeg, png, gif]")
)

func main() {
flag.Parse()
args := flag.Args()
if len(args) != 1 {
fmt.Println("set the directory path in the argument.")
return
}

err := option.Valid(*s, *d)
if err != nil {
fmt.Println(err)
return
}

conv := imgconv.Converter{SrcExt: *s, DstExt: *d}
err = conv.Convert(args[0])

if err != nil {
fmt.Println(err)
return
}

fmt.Println("complete!")
}
14 changes: 14 additions & 0 deletions kadai1/gashiura/src/option/option.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package option

import (
"errors"
"regexp"
)

func Valid(s, d string) error {
r := regexp.MustCompile("^(jpeg|png|gif)$")
if !r.MatchString(s) || !r.MatchString(d) {
return errors.New("please set \"jpeg\" or \"png\" or \"gif\" as an option.")
Copy link

Choose a reason for hiding this comment

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

ご提案: バッククオートで囲むとエスケープなしで書けます!

return errors.New(`please set "jpeg" or "png" or "gif" as an option.`)

}
return nil
}