-
-
Notifications
You must be signed in to change notification settings - Fork 98
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
Showing
4 changed files
with
195 additions
and
61 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 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,37 @@ | ||
package canvas | ||
|
||
import ( | ||
"bytes" | ||
"image" | ||
"image/jpeg" | ||
"image/png" | ||
"io" | ||
) | ||
|
||
// Image allows the renderer to optimize specific cases | ||
type Image struct { | ||
image.Image | ||
Bytes []byte | ||
Mimetype string // image/png or image/jpeg for instance | ||
} | ||
|
||
// NewJPEGImage parses a reader to later give access to the JPEG raw bytes. | ||
func NewJPEGImage(r io.Reader) (Image, error) { | ||
return newImage("image/jpeg", jpeg.Decode, r) | ||
} | ||
|
||
// NewPNGImage parses a reader to later give access to the PNG raw bytes | ||
func NewPNGImage(r io.Reader) (Image, error) { | ||
return newImage("image/png", png.Decode, r) | ||
} | ||
|
||
func newImage(mimetype string, decode func(io.Reader) (image.Image, error), r io.Reader) (Image, error) { | ||
var buffer bytes.Buffer | ||
r = io.TeeReader(r, &buffer) | ||
img, err := decode(r) | ||
return Image{ | ||
Image: img, | ||
Bytes: buffer.Bytes(), | ||
Mimetype: mimetype, | ||
}, err | ||
} |
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 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