diff --git a/kadai2/tanaka0325/imgconv/cmd/imgconv/main.go b/kadai2/tanaka0325/imgconv/cmd/imgconv/main.go index f3ffe0e..ebf85fb 100644 --- a/kadai2/tanaka0325/imgconv/cmd/imgconv/main.go +++ b/kadai2/tanaka0325/imgconv/cmd/imgconv/main.go @@ -41,8 +41,8 @@ func main() { for _, path := range paths { param := imgconv.ConvertParam{ Path: path, - BeforeImage: imgconv.NewCnvImage(*options.From), - AfterImage: imgconv.NewCnvImage(*options.To), + BeforeImage: imgconv.NewImage(*options.From), + AfterImage: imgconv.NewImage(*options.To), FromExt: *options.From, ToExt: *options.To, } diff --git a/kadai2/tanaka0325/imgconv/cnv_image.go b/kadai2/tanaka0325/imgconv/cnv_image.go deleted file mode 100644 index 91c0183..0000000 --- a/kadai2/tanaka0325/imgconv/cnv_image.go +++ /dev/null @@ -1,81 +0,0 @@ -package imgconv - -import ( - "image" - "image/gif" - "image/jpeg" - "image/png" - "io" - - "golang.org/x/image/bmp" - "golang.org/x/image/tiff" -) - -type Decoder interface { - Decode(io.Reader) (image.Image, error) -} - -type Encoder interface { - Encode(io.Writer, image.Image) error -} - -type DecodeEncoder interface { - Decoder - Encoder -} - -type CnvImage struct{} - -// CnvImagePng is type for png format. -type CnvImagePNG CnvImage - -func (ip CnvImagePNG) Decode(r io.Reader) (image.Image, error) { return png.Decode(r) } - -func (ip CnvImagePNG) Encode(w io.Writer, i image.Image) error { return png.Encode(w, i) } - -// CnvImageJPEG is type for jpeg format. -type CnvImageJPEG CnvImage - -func (ip CnvImageJPEG) Decode(r io.Reader) (image.Image, error) { return jpeg.Decode(r) } - -func (ip CnvImageJPEG) Encode(w io.Writer, i image.Image) error { return jpeg.Encode(w, i, nil) } - -// CnvImageGIF is type for gif format. -type CnvImageGIF CnvImage - -func (ip CnvImageGIF) Decode(r io.Reader) (image.Image, error) { return gif.Decode(r) } - -func (ip CnvImageGIF) Encode(w io.Writer, i image.Image) error { - return gif.Encode(w, i, &gif.Options{NumColors: 256}) -} - -// CnvImageBMP is type for bmp format. -type CnvImageBMP CnvImage - -func (ip CnvImageBMP) Decode(r io.Reader) (image.Image, error) { return bmp.Decode(r) } - -func (ip CnvImageBMP) Encode(w io.Writer, i image.Image) error { return bmp.Encode(w, i) } - -// CnvImageTIFF is type for tiff format. -type CnvImageTIFF CnvImage - -func (ip CnvImageTIFF) Decode(r io.Reader) (image.Image, error) { return tiff.Decode(r) } - -func (ip CnvImageTIFF) Encode(w io.Writer, i image.Image) error { return tiff.Encode(w, i, nil) } - -func NewCnvImage(ext string) DecodeEncoder { - switch ext { - case "png": - return &CnvImagePNG{} - case "jpg", "jpeg": - return &CnvImageJPEG{} - case "gif": - return &CnvImageGIF{} - case "bmp": - return &CnvImageBMP{} - case "tiff", "tif": - return &CnvImageTIFF{} - } - - return nil -} diff --git a/kadai2/tanaka0325/imgconv/image.go b/kadai2/tanaka0325/imgconv/image.go new file mode 100644 index 0000000..cdca775 --- /dev/null +++ b/kadai2/tanaka0325/imgconv/image.go @@ -0,0 +1,81 @@ +package imgconv + +import ( + "image" + "image/gif" + "image/jpeg" + "image/png" + "io" + + "golang.org/x/image/bmp" + "golang.org/x/image/tiff" +) + +type Decoder interface { + Decode(io.Reader) (image.Image, error) +} + +type Encoder interface { + Encode(io.Writer, image.Image) error +} + +type DecodeEncoder interface { + Decoder + Encoder +} + +type Image struct{} + +// ImagePng is type for png format. +type ImagePNG Image + +func (ip ImagePNG) Decode(r io.Reader) (image.Image, error) { return png.Decode(r) } + +func (ip ImagePNG) Encode(w io.Writer, i image.Image) error { return png.Encode(w, i) } + +// ImageJPEG is type for jpeg format. +type ImageJPEG Image + +func (ip ImageJPEG) Decode(r io.Reader) (image.Image, error) { return jpeg.Decode(r) } + +func (ip ImageJPEG) Encode(w io.Writer, i image.Image) error { return jpeg.Encode(w, i, nil) } + +// ImageGIF is type for gif format. +type ImageGIF Image + +func (ip ImageGIF) Decode(r io.Reader) (image.Image, error) { return gif.Decode(r) } + +func (ip ImageGIF) Encode(w io.Writer, i image.Image) error { + return gif.Encode(w, i, &gif.Options{NumColors: 256}) +} + +// ImageBMP is type for bmp format. +type ImageBMP Image + +func (ip ImageBMP) Decode(r io.Reader) (image.Image, error) { return bmp.Decode(r) } + +func (ip ImageBMP) Encode(w io.Writer, i image.Image) error { return bmp.Encode(w, i) } + +// ImageTIFF is type for tiff format. +type ImageTIFF Image + +func (ip ImageTIFF) Decode(r io.Reader) (image.Image, error) { return tiff.Decode(r) } + +func (ip ImageTIFF) Encode(w io.Writer, i image.Image) error { return tiff.Encode(w, i, nil) } + +func NewImage(ext string) DecodeEncoder { + switch ext { + case "png": + return &ImagePNG{} + case "jpg", "jpeg": + return &ImageJPEG{} + case "gif": + return &ImageGIF{} + case "bmp": + return &ImageBMP{} + case "tiff", "tif": + return &ImageTIFF{} + } + + return nil +}