diff --git a/kadai2/hiroygo/.gitignore b/kadai2/hiroygo/.gitignore new file mode 100644 index 0000000..39a03ac --- /dev/null +++ b/kadai2/hiroygo/.gitignore @@ -0,0 +1,354 @@ +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. +## +## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore + +# User-specific files +*.rsuser +*.suo +*.user +*.userosscache +*.sln.docstates + +# User-specific files (MonoDevelop/Xamarin Studio) +*.userprefs + +# Mono auto generated files +mono_crash.* + +# Build results +[Dd]ebug/ +[Dd]ebugPublic/ +[Rr]elease/ +[Rr]eleases/ +x64/ +x86/ +[Aa][Rr][Mm]/ +[Aa][Rr][Mm]64/ +bld/ +[Bb]in/ +[Oo]bj/ +[Ll]og/ +[Ll]ogs/ + +# Visual Studio 2015/2017 cache/options directory +.vs/ +# Uncomment if you have tasks that create the project's static files in wwwroot +#wwwroot/ + +# Visual Studio 2017 auto generated files +Generated\ Files/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +# NUnit +*.VisualState.xml +TestResult.xml +nunit-*.xml + +# Build Results of an ATL Project +[Dd]ebugPS/ +[Rr]eleasePS/ +dlldata.c + +# Benchmark Results +BenchmarkDotNet.Artifacts/ + +# .NET Core +project.lock.json +project.fragment.lock.json +artifacts/ + +# StyleCop +StyleCopReport.xml + +# Files built by Visual Studio +*_i.c +*_p.c +*_h.h +*.ilk +*.meta +*.obj +*.iobj +*.pch +*.pdb +*.ipdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*_wpftmp.csproj +*.log +*.vspscc +*.vssscc +.builds +*.pidb +*.svclog +*.scc + +# Chutzpah Test files +_Chutzpah* + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opendb +*.opensdf +*.sdf +*.cachefile +*.VC.db +*.VC.VC.opendb + +# Visual Studio profiler +*.psess +*.vsp +*.vspx +*.sap + +# Visual Studio Trace Files +*.e2e + +# TFS 2012 Local Workspace +$tf/ + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper +*.DotSettings.user + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# AxoCover is a Code Coverage Tool +.axoCover/* +!.axoCover/settings.json + +# Visual Studio code coverage results +*.coverage +*.coveragexml + +# NCrunch +_NCrunch_* +.*crunch*.local.xml +nCrunchTemp_* + +# MightyMoose +*.mm.* +AutoTest.Net/ + +# Web workbench (sass) +.sass-cache/ + +# Installshield output folder +[Ee]xpress/ + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish/ + +# Publish Web Output +*.[Pp]ublish.xml +*.azurePubxml +# Note: Comment the next line if you want to checkin your web deploy settings, +# but database connection strings (with potential passwords) will be unencrypted +*.pubxml +*.publishproj + +# Microsoft Azure Web App publish settings. Comment the next line if you want to +# checkin your Azure Web App publish settings, but sensitive information contained +# in these scripts will be unencrypted +PublishScripts/ + +# NuGet Packages +*.nupkg +# NuGet Symbol Packages +*.snupkg +# The packages folder can be ignored because of Package Restore +**/[Pp]ackages/* +# except build/, which is used as an MSBuild target. +!**/[Pp]ackages/build/ +# Uncomment if necessary however generally it will be regenerated when needed +#!**/[Pp]ackages/repositories.config +# NuGet v3's project.json files produces more ignorable files +*.nuget.props +*.nuget.targets + +# Microsoft Azure Build Output +csx/ +*.build.csdef + +# Microsoft Azure Emulator +ecf/ +rcf/ + +# Windows Store app package directories and files +AppPackages/ +BundleArtifacts/ +Package.StoreAssociation.xml +_pkginfo.txt +*.appx +*.appxbundle +*.appxupload + +# Visual Studio cache files +# files ending in .cache can be ignored +*.[Cc]ache +# but keep track of directories ending in .cache +!?*.[Cc]ache/ + +# Others +ClientBin/ +~$* +*~ +*.dbmdl +*.dbproj.schemaview +*.jfm +*.pfx +*.publishsettings +orleans.codegen.cs + +# Including strong name files can present a security risk +# (https://github.com/github/gitignore/pull/2483#issue-259490424) +#*.snk + +# Since there are multiple workflows, uncomment next line to ignore bower_components +# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) +#bower_components/ + +# RIA/Silverlight projects +Generated_Code/ + +# Backup & report files from converting an old project file +# to a newer Visual Studio version. Backup files are not needed, +# because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm +ServiceFabricBackup/ +*.rptproj.bak + +# SQL Server files +*.mdf +*.ldf +*.ndf + +# Business Intelligence projects +*.rdl.data +*.bim.layout +*.bim_*.settings +*.rptproj.rsuser +*- [Bb]ackup.rdl +*- [Bb]ackup ([0-9]).rdl +*- [Bb]ackup ([0-9][0-9]).rdl + +# Microsoft Fakes +FakesAssemblies/ + +# GhostDoc plugin setting file +*.GhostDoc.xml + +# Node.js Tools for Visual Studio +.ntvs_analysis.dat +node_modules/ + +# Visual Studio 6 build log +*.plg + +# Visual Studio 6 workspace options file +*.opt + +# Visual Studio 6 auto-generated workspace file (contains which files were open etc.) +*.vbw + +# Visual Studio LightSwitch build output +**/*.HTMLClient/GeneratedArtifacts +**/*.DesktopClient/GeneratedArtifacts +**/*.DesktopClient/ModelManifest.xml +**/*.Server/GeneratedArtifacts +**/*.Server/ModelManifest.xml +_Pvt_Extensions + +# Paket dependency manager +.paket/paket.exe +paket-files/ + +# FAKE - F# Make +.fake/ + +# CodeRush personal settings +.cr/personal + +# Python Tools for Visual Studio (PTVS) +__pycache__/ +*.pyc + +# Cake - Uncomment if you are using it +# tools/** +# !tools/packages.config + +# Tabs Studio +*.tss + +# Telerik's JustMock configuration file +*.jmconfig + +# BizTalk build output +*.btp.cs +*.btm.cs +*.odx.cs +*.xsd.cs + +# OpenCover UI analysis results +OpenCover/ + +# Azure Stream Analytics local run output +ASALocalRun/ + +# MSBuild Binary and Structured Log +*.binlog + +# NVidia Nsight GPU debugger configuration file +*.nvuser + +# MFractors (Xamarin productivity tool) working folder +.mfractor/ + +# Local History for Visual Studio +.localhistory/ + +# BeatPulse healthcheck temp database +healthchecksdb + +# Backup folder for Package Reference Convert tool in Visual Studio 2017 +MigrationBackup/ + +# Ionide (cross platform F# VS Code tools) working folder +.ionide/ + +# add +*.o +.vscode/ \ No newline at end of file diff --git a/kadai2/hiroygo/README.md b/kadai2/hiroygo/README.md new file mode 100644 index 0000000..aff6fc8 --- /dev/null +++ b/kadai2/hiroygo/README.md @@ -0,0 +1,27 @@ +# 課題2 +## 【TRY】io.Readerとio.Writer +### io.Readerとio.Writerについて調べてみよう +#### 標準パッケージでどのように使われているか +* データの入出力処理を抽象化するために `io.Reader`, `io.Writer` は使われている +* `*os.File`, `*bytes.Buffer`, `net.Conn` などが `io.Reader`, `io.Writer` を実装している +* 利用例. `func json.NewEncoder(w io.Writer) *Encoder` + * この関数は json エンコーダを返す + * `Encoder` の `Encode` を実行すると、内部で `w.Write` を実行し、エンコード結果を `w` に出力する + +#### io.Readerとio.Writerがあることで、どういう利点があるのか具体例を挙げて考えてみる +* 上記利用例のように `func json.NewEncoder(w io.Writer) *Encoder` の `w` には `*os.File`, `*bytes.Buffer` など様々なものを入れることができる +* `w` は `io.Writer` を実装さえしていればよく、`Encoder` が `w` の内部実装まで考える必要がない +* 新しく型を追加してもそれが、`io.Writer` を実装していれば `Encode` を実行できる。`Encoder` 側の修正は必要ない + +## 【TRY】テストを書いてみよう +### 1回目の課題のテストを作ってみて下さい +* テストのしやすさを考えてリファクタリングしてみる +* テストのカバレッジを取ってみる +* テーブル駆動テストを行う +* テストヘルパーを作ってみる + +### テストのカバレッジ +``` +$ go test -coverprofile=profile github.com/gopherdojo/dojo8/kadai2/hiroygo/imgconv +ok github.com/gopherdojo/dojo8/kadai2/hiroygo/imgconv 0.497s coverage: 88.1% of statements +``` diff --git a/kadai2/hiroygo/go.mod b/kadai2/hiroygo/go.mod new file mode 100644 index 0000000..3a7046d --- /dev/null +++ b/kadai2/hiroygo/go.mod @@ -0,0 +1,5 @@ +module github.com/gopherdojo/dojo8/kadai2/hiroygo + +go 1.14 + +require golang.org/x/image v0.0.0-20200618115811-c13761719519 diff --git a/kadai2/hiroygo/go.sum b/kadai2/hiroygo/go.sum new file mode 100644 index 0000000..83da957 --- /dev/null +++ b/kadai2/hiroygo/go.sum @@ -0,0 +1,4 @@ +github.com/gopherdojo/dojo8 v0.0.0-20200703052727-6a79d18126bf h1:lpYevjFQMxI5VNBc3WXV6Z5pDDrdppdDKwmeBoyt5BE= +golang.org/x/image v0.0.0-20200618115811-c13761719519 h1:1e2ufUJNM3lCHEY5jIgac/7UTjd6cgJNdatjPdFWf34= +golang.org/x/image v0.0.0-20200618115811-c13761719519/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= diff --git a/kadai2/hiroygo/imgconv/imgconv.go b/kadai2/hiroygo/imgconv/imgconv.go new file mode 100644 index 0000000..a03b241 --- /dev/null +++ b/kadai2/hiroygo/imgconv/imgconv.go @@ -0,0 +1,161 @@ +package imgconv + +import ( + "errors" + "fmt" + "image" + "image/gif" + "image/jpeg" + "image/png" + "os" + "path/filepath" + "strings" + + "golang.org/x/image/bmp" + "golang.org/x/image/tiff" +) + +const ( + // Unknown 不明な画像 + Unknown ImageType = "unknown" + // Jpeg Jpeg 画像 + Jpeg ImageType = "jpg" + // Png Png 画像 + Png ImageType = "png" + // Tiff Tiff 画像 + Tiff ImageType = "tiff" + // Bmp Bmp 画像 + Bmp ImageType = "bmp" + // Gif Gif 画像 + Gif ImageType = "gif" +) + +// ImageType 画像種別 +type ImageType string + +func (t ImageType) String() string { + return string(t) +} + +// ToImageType 文字列を ImageType に変換する +// e.g. "jpg" => Jpeg +func ToImageType(s string) ImageType { + switch s { + case "jpeg", "jpg": + return Jpeg + case "png": + return Png + case "tiff": + return Tiff + case "bmp": + return Bmp + case "gif": + return Gif + default: + return Unknown + } +} + +func imageTypeFromFileName(name string) ImageType { + extWithoutDot := strings.TrimLeft(filepath.Ext(name), ".") + return ToImageType(strings.ToLower(extWithoutDot)) +} + +// ImageFilePathesRecursive ディレクトリ内の指定された画像種別のファイルパスを再帰的に返す +func ImageFilePathesRecursive(dir string, target ImageType) ([]string, error) { + var pathes []string + err := filepath.Walk(dir, func(visitPath string, f os.FileInfo, err error) error { + if err != nil { + return fmt.Errorf("WalkFunc error, %w", err) + } + + if !f.Mode().IsRegular() { + return nil + } + + t := imageTypeFromFileName(f.Name()) + if t != target { + return nil + } + + pathes = append(pathes, visitPath) + return nil + }) + if err != nil { + return nil, fmt.Errorf("Walk error, %w", err) + } + + return pathes, nil +} + +// LoadImage 画像を読み込み、その Image を返す +func LoadImage(path string) (image.Image, error) { + f, err := os.Open(path) + if err != nil { + return nil, fmt.Errorf("Open error, %w", err) + } + defer f.Close() + + m, _, err := image.Decode(f) + if err != nil { + return nil, fmt.Errorf("Decode error, %w", err) + } + + return m, nil +} + +// ReplaceExt 画像ファイルパスの拡張子を置換した結果を返す +func ReplaceExt(path string, replace ImageType) string { + newName := filepath.Base(path[:len(path)-len(filepath.Ext(path))]) + newName += "." + replace.String() + return filepath.Join(filepath.Dir(path), newName) +} + +// SaveImage Image を指定された画像形式で保存する +func SaveImage(m image.Image, out ImageType, savePath string) (rerr error) { + if m == nil { + return errors.New("Image が nil です") + } + + var encoder func(*os.File, image.Image) error + switch out { + case Jpeg: + encoder = func(f *os.File, m image.Image) error { + return jpeg.Encode(f, m, nil) + } + case Png: + encoder = func(f *os.File, m image.Image) error { + return png.Encode(f, m) + } + case Tiff: + encoder = func(f *os.File, m image.Image) error { + return tiff.Encode(f, m, nil) + } + case Bmp: + encoder = func(f *os.File, m image.Image) error { + return bmp.Encode(f, m) + } + case Gif: + encoder = func(f *os.File, m image.Image) error { + return gif.Encode(f, m, nil) + } + default: + return fmt.Errorf("画像種別 %s は不明です", out.String()) + } + + f, err := os.Create(savePath) + if err != nil { + return fmt.Errorf("Create error, %w", err) + } + defer func() { + if err := f.Close(); err != nil { + rerr = fmt.Errorf("Close error, %w", err) + } + }() + + if err := encoder(f, m); err != nil { + return fmt.Errorf("Encode error, %w", err) + } + + return +} diff --git a/kadai2/hiroygo/imgconv/imgconv_test.go b/kadai2/hiroygo/imgconv/imgconv_test.go new file mode 100644 index 0000000..7f0f12e --- /dev/null +++ b/kadai2/hiroygo/imgconv/imgconv_test.go @@ -0,0 +1,202 @@ +package imgconv_test + +import ( + "io/ioutil" + "os" + "path/filepath" + "reflect" + "testing" + + "github.com/gopherdojo/dojo8/kadai2/hiroygo/imgconv" +) + +var testdataDir string = filepath.Join("..", "testdata") +var testdataSubDir string = filepath.Join(testdataDir, "sub") +var testJpgFile string = filepath.Join(testdataDir, "go.jpg") +var testJpegFile string = filepath.Join(testdataDir, "go.jpeg") +var testSubJpgFile string = filepath.Join(testdataSubDir, "go.jpg") +var testPngFile string = filepath.Join(testdataDir, "go.png") +var testTiffFile string = filepath.Join(testdataDir, "go.tiff") +var testBmpFile string = filepath.Join(testdataDir, "go.bmp") +var testGifFile string = filepath.Join(testdataDir, "go.gif") + +func TestToImageType(t *testing.T) { + cases := []struct { + name string + input string + expected imgconv.ImageType + }{ + {name: "jpg", input: "jpg", expected: imgconv.Jpeg}, + {name: "jpeg", input: "jpeg", expected: imgconv.Jpeg}, + {name: "png", input: "png", expected: imgconv.Png}, + {name: "tiff", input: "tiff", expected: imgconv.Tiff}, + {name: "bmp", input: "bmp", expected: imgconv.Bmp}, + {name: "gif", input: "gif", expected: imgconv.Gif}, + {name: "txt", input: "txt", expected: imgconv.Unknown}, + } + + for _, c := range cases { + c := c + t.Run(c.name, func(t *testing.T) { + t.Parallel() + if actual := imgconv.ToImageType(c.input); actual != c.expected { + t.Errorf("want ToImageType(%s) = %v, got %v", c.input, c.expected, actual) + } + }) + } +} + +func TestImageFilePathesRecursive(t *testing.T) { + cases := []struct { + name string + dir string + imgType imgconv.ImageType + expectedPathes []string + }{ + {name: "jpg_files", dir: testdataDir, imgType: imgconv.Jpeg, expectedPathes: []string{testJpegFile, testJpgFile, testSubJpgFile}}, + {name: "png_files", dir: testdataDir, imgType: imgconv.Png, expectedPathes: []string{testPngFile}}, + {name: "tiff_files", dir: testdataDir, imgType: imgconv.Tiff, expectedPathes: []string{testTiffFile}}, + {name: "bmp_files", dir: testdataDir, imgType: imgconv.Bmp, expectedPathes: []string{testBmpFile}}, + {name: "gif_files", dir: testdataDir, imgType: imgconv.Gif, expectedPathes: []string{testGifFile}}, + } + + for _, c := range cases { + c := c + t.Run(c.name, func(t *testing.T) { + t.Parallel() + actual, err := imgconv.ImageFilePathesRecursive(c.dir, c.imgType) + if err != nil { + t.Fatalf("ImageFilePathesRecursive(%s, %v) error %v", c.dir, c.imgType, err) + } + + // NOTE:expectedPathes と actual の中身の順序まで一致しないと成功にならない + // e.g. expectedPathes:{"test1.jpg", "test2.jpg"}, actual:{"test1.jpg", "test2.jpg"} => OK + // e.g. expectedPathes:{"test1.jpg", "test2.jpg"}, actual:{"test2.jpg", "test1.jpg"} => NG + if !reflect.DeepEqual(actual, c.expectedPathes) { + t.Errorf("want ImageFilePathesRecursive(%s, %v) = %v, got %v", c.dir, c.imgType, c.expectedPathes, actual) + } + }) + } +} + +func TestLoadImage(t *testing.T) { + cases := []struct { + name string + path string + wantErr bool + }{ + {name: "jpg", path: testJpgFile, wantErr: false}, + {name: "png", path: testPngFile, wantErr: false}, + {name: "tiff", path: testTiffFile, wantErr: false}, + {name: "bmp", path: testBmpFile, wantErr: false}, + {name: "gif", path: testGifFile, wantErr: false}, + {name: "directory", path: testdataDir, wantErr: true}, + } + + for _, c := range cases { + c := c + t.Run(c.name, func(t *testing.T) { + t.Parallel() + _, err := imgconv.LoadImage(c.path) + if !c.wantErr && err != nil { + t.Errorf("LoadImage(%s) error %v", c.path, err) + } + }) + } +} + +func TestReplaceExt(t *testing.T) { + cases := []struct { + name string + path string + outType imgconv.ImageType + expected string + }{ + {name: "without_ext", path: "test", outType: imgconv.Jpeg, expected: "test.jpg"}, + {name: "ends_dot", path: "test.", outType: imgconv.Jpeg, expected: "test.jpg"}, + {name: "begin_dot", path: ".test.", outType: imgconv.Jpeg, expected: ".test.jpg"}, + {name: "only_dot", path: ".", outType: imgconv.Jpeg, expected: "..jpg"}, + {name: "empty", path: "", outType: imgconv.Jpeg, expected: "..jpg"}, + {name: "has_parent_dir", path: testJpgFile, outType: imgconv.Png, expected: testPngFile}, + {name: "jpg", path: testPngFile, outType: imgconv.Jpeg, expected: testJpgFile}, + {name: "png", path: testJpgFile, outType: imgconv.Png, expected: testPngFile}, + {name: "tiff", path: testJpgFile, outType: imgconv.Tiff, expected: testTiffFile}, + {name: "bmp", path: testJpgFile, outType: imgconv.Bmp, expected: testBmpFile}, + {name: "gif", path: testJpgFile, outType: imgconv.Gif, expected: testGifFile}, + } + + for _, c := range cases { + c := c + t.Run(c.name, func(t *testing.T) { + t.Parallel() + actual := imgconv.ReplaceExt(c.path, c.outType) + if actual != c.expected { + t.Errorf("want ReplaceExt(%s, %v) = %s, got %s", c.path, c.outType, c.expected, actual) + } + }) + } +} + +func tempDir(t *testing.T) string { + t.Helper() + dir, err := ioutil.TempDir("", "") + if err != nil { + t.Fatalf("TempDir error %v", err) + } + + t.Cleanup(func() { + if err := os.RemoveAll(dir); err != nil { + t.Fatalf("RemoveAll error %v", err) + } + }) + + return dir +} + +func TestSaveImage(t *testing.T) { + tmpDir := tempDir(t) + saveFileBase := filepath.Join(tmpDir, "test") + + type saveParam struct { + name string + saveType imgconv.ImageType + savePath string + wantErr bool + } + saveParams := []saveParam{ + {name: "jpg", saveType: imgconv.Jpeg, savePath: imgconv.ReplaceExt(saveFileBase, imgconv.Jpeg), wantErr: false}, + {name: "png", saveType: imgconv.Png, savePath: imgconv.ReplaceExt(saveFileBase, imgconv.Png), wantErr: false}, + {name: "tiff", saveType: imgconv.Tiff, savePath: imgconv.ReplaceExt(saveFileBase, imgconv.Tiff), wantErr: false}, + {name: "bmp", saveType: imgconv.Bmp, savePath: imgconv.ReplaceExt(saveFileBase, imgconv.Bmp), wantErr: false}, + {name: "gif", saveType: imgconv.Gif, savePath: imgconv.ReplaceExt(saveFileBase, imgconv.Gif), wantErr: false}, + {name: "unknown", saveType: imgconv.Unknown, savePath: imgconv.ReplaceExt(saveFileBase, imgconv.Unknown), wantErr: true}, + } + + cases := []struct { + name string + srcPath string + saveParams []saveParam + }{ + {name: "jpg_to", srcPath: testJpgFile, saveParams: saveParams}, + {name: "png_to", srcPath: testPngFile, saveParams: saveParams}, + {name: "tiff_to", srcPath: testTiffFile, saveParams: saveParams}, + {name: "bmp_to", srcPath: testBmpFile, saveParams: saveParams}, + {name: "gif_to", srcPath: testGifFile, saveParams: saveParams}, + } + + for _, c := range cases { + for _, innerCase := range c.saveParams { + t.Run(c.name+"_"+innerCase.name, func(t *testing.T) { + m, err := imgconv.LoadImage(c.srcPath) + if err != nil { + t.Fatalf("LoadImage(%s) error %v", c.srcPath, err) + } + + err = imgconv.SaveImage(m, innerCase.saveType, innerCase.savePath) + if !innerCase.wantErr && err != nil { + t.Errorf("SaveImage(image.Image, %v, %s) error %v", innerCase.saveType, innerCase.savePath, err) + } + }) + } + } +} diff --git a/kadai2/hiroygo/main.go b/kadai2/hiroygo/main.go new file mode 100644 index 0000000..ef567da --- /dev/null +++ b/kadai2/hiroygo/main.go @@ -0,0 +1,56 @@ +package main + +import ( + "flag" + "fmt" + "os" + + "github.com/gopherdojo/dojo8/kadai2/hiroygo/imgconv" +) + +func parseArgs() (dir string, in, out imgconv.ImageType, err error) { + d := flag.String("d", "./", "変換の対象となる画像が格納されたディレクトリパス") + i := flag.String("i", "jpg", "変換の対象となる画像種別") + o := flag.String("o", "png", "変換後の画像種別") + flag.Parse() + + in = imgconv.ToImageType(*i) + out = imgconv.ToImageType(*o) + if in == imgconv.Unknown || out == imgconv.Unknown { + in = imgconv.Unknown + out = imgconv.Unknown + err = fmt.Errorf("設定された画像種別が不正です") + return + } + + dir = *d + return +} + +func main() { + dir, inType, outType, err := parseArgs() + if err != nil { + fmt.Fprintln(os.Stderr, err) + return + } + + pathes, err := imgconv.ImageFilePathesRecursive(dir, inType) + if err != nil { + fmt.Fprintln(os.Stderr, err) + return + } + + for _, path := range pathes { + m, err := imgconv.LoadImage(path) + if err != nil { + fmt.Fprintf(os.Stderr, "%s の読み込みに失敗しました。%v\n", path, err) + continue + } + + savePath := imgconv.ReplaceExt(path, outType) + if err := imgconv.SaveImage(m, outType, savePath); err != nil { + fmt.Fprintf(os.Stderr, "%s の保存に失敗しました。%v\n", savePath, err) + continue + } + } +} diff --git a/kadai2/hiroygo/testdata/go.bmp b/kadai2/hiroygo/testdata/go.bmp new file mode 100644 index 0000000..0516deb Binary files /dev/null and b/kadai2/hiroygo/testdata/go.bmp differ diff --git a/kadai2/hiroygo/testdata/go.gif b/kadai2/hiroygo/testdata/go.gif new file mode 100644 index 0000000..31350e1 Binary files /dev/null and b/kadai2/hiroygo/testdata/go.gif differ diff --git a/kadai2/hiroygo/testdata/go.jpeg b/kadai2/hiroygo/testdata/go.jpeg new file mode 100644 index 0000000..46ed1e0 Binary files /dev/null and b/kadai2/hiroygo/testdata/go.jpeg differ diff --git a/kadai2/hiroygo/testdata/go.jpg b/kadai2/hiroygo/testdata/go.jpg new file mode 100644 index 0000000..46ed1e0 Binary files /dev/null and b/kadai2/hiroygo/testdata/go.jpg differ diff --git a/kadai2/hiroygo/testdata/go.png b/kadai2/hiroygo/testdata/go.png new file mode 100644 index 0000000..f3737d8 Binary files /dev/null and b/kadai2/hiroygo/testdata/go.png differ diff --git a/kadai2/hiroygo/testdata/go.tiff b/kadai2/hiroygo/testdata/go.tiff new file mode 100644 index 0000000..d2b3b67 Binary files /dev/null and b/kadai2/hiroygo/testdata/go.tiff differ diff --git a/kadai2/hiroygo/testdata/sub/go.jpg b/kadai2/hiroygo/testdata/sub/go.jpg new file mode 100644 index 0000000..46ed1e0 Binary files /dev/null and b/kadai2/hiroygo/testdata/sub/go.jpg differ