generated from gopherdojo/template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
675b8a5
commit 3783728
Showing
3 changed files
with
83 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |