-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjhenc.go
73 lines (62 loc) · 1.79 KB
/
jhenc.go
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package jhenc
import (
"encoding/json"
"errors"
"fmt"
"image"
"image/color"
"io"
"strings"
)
type JSONResponse struct {
Rows [][]string
}
func JsonEncode(w io.Writer, img image.Image) error {
var err error
// Check image size
imgwidth, imgheight := int64(img.Bounds().Dx()), int64(img.Bounds().Dy())
if imgwidth <= 0 || imgheight <= 0 || imgwidth >= 1<<32 || imgheight >= 1<<32 {
return errors.New(fmt.Sprintf("Unacceptable image size %d x %d.\n", imgwidth, imgheight))
}
min := img.Bounds().Min
max := img.Bounds().Max
rows := [][]string{}
for y := min.Y; y <= max.Y; y++ {
row := []string{}
for x := min.X; x <= max.X; x++ {
c := color.RGBAModel.Convert(img.At(x, y)).(color.RGBA)
row = append(row, fmt.Sprintf("rgb(%d, %d, %d)", c.R, c.G, c.B))
}
rows = append(rows, row)
}
response, err := json.Marshal(JSONResponse{Rows: rows})
if err != nil {
return err
}
w.Write(response)
return err
}
func HtmlEncode(w io.Writer, img image.Image) error {
var err error
// Check image size
imgwidth, imgheight := int64(img.Bounds().Dx()), int64(img.Bounds().Dy())
if imgwidth <= 0 || imgheight <= 0 || imgwidth >= 1<<32 || imgheight >= 1<<32 {
return errors.New(fmt.Sprintf("Unacceptable image size %d x %d.\n", imgwidth, imgheight))
}
min := img.Bounds().Min
max := img.Bounds().Max
out := "<table cellspacing='0' cellpadding='0'>\n<thead></thead>\n<tbody>\n"
lines := []string{}
for y := min.Y; y <= max.Y; y++ {
line := "<tr>"
for x := min.X; x <= max.X; x++ {
c := color.RGBAModel.Convert(img.At(x, y)).(color.RGBA)
line += fmt.Sprintf("<td style='background-color:rgb(%d, %d, %d);'></td>\n", c.R, c.G, c.B)
}
line += "</tr>\n"
lines = append(lines, line)
}
out += strings.Join(lines, "\n")
w.Write([]byte(out + "</tbody>\n</table>\n"))
return err
}