-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser_example_test.go
More file actions
47 lines (39 loc) · 935 Bytes
/
parser_example_test.go
File metadata and controls
47 lines (39 loc) · 935 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package parser_test
import (
"bytes"
"fmt"
"image"
"image/jpeg"
"image/png"
"io/ioutil"
"os"
"github.com/textmodes/parser"
)
type testImage struct {
Bytes []byte
}
// Image decodes Bytes to Image.
func (i testImage) Image() (image.Image, error) {
return png.Decode(bytes.NewBuffer(i.Bytes))
}
// RenderJPEG parses p as Image and returns an encoded JPEG to standard out.
func RenderJPEG(p parser.Parser) (err error) {
if imager, ok := p.(parser.Image); ok {
var im image.Image
if im, err = imager.Image(); err != nil {
return
}
return jpeg.Encode(os.Stdout, im, nil)
}
return fmt.Errorf("parser: %v does not implement parser.Image", p)
}
// ExampleImage renders a PNG to stdout as JPEG.
func ExampleImage() {
b, err := ioutil.ReadFile(os.Args[1])
if err != nil {
panic(fmt.Sprintf("error reading %s: %v", os.Args[1], err))
}
if err = RenderJPEG(testImage{Bytes: b}); err != nil {
panic(err)
}
}