-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathwriter.go
153 lines (141 loc) · 3.15 KB
/
writer.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// Copyright 2012 Harry de Boer. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package pnm
import (
"errors"
"fmt"
"image"
"image/color"
"io"
)
const (
PBM int = 0
PGM int = 1
PPM int = 2
)
// packByte packs 8 pixels of bit depth 1 into a byte.
//
// The bits are packed with the first value as the most significant bit.
// If there are less than 8 values in bit, the remaining bits are 0.
// If there are more than 8 values in bit, these are ignored.
func packByte(bit []uint8) (b byte) {
n := len(bit)
if n > 8 {
n = 8
}
for i := n - 1; i >= 0; i-- {
b = b >> 1
if bit[i] == 0 {
b += 128
}
}
return b
}
func encodePBM(w io.Writer, m image.Image) error {
b := m.Bounds()
// write header
_, err := fmt.Fprintf(w, "P4\n%d %d\n", b.Dx(), b.Dy())
if err != nil {
return err
}
cm := make(color.Palette, 2)
cm[0] = color.Gray{255}
cm[1] = color.Gray{0}
// write raster
byteCount := b.Dx() / 8
if b.Dx()%8 != 0 {
byteCount += 1
}
row := make([]uint8, b.Dx())
packedRow := make([]byte, byteCount)
for y := b.Min.Y; y < b.Max.Y; y++ {
// Read row and convert to black/white.
for x := b.Min.X; x < b.Max.X; x++ {
c := cm.Convert(m.At(x, y)).(color.Gray)
row[x-b.Min.X] = c.Y
}
// Pack values into and write
i := 0
x := 0
for x < b.Dx() {
n := b.Dx() - x
if n > 8 {
n = 8
}
packedRow[i] = packByte(row[x : x+n])
x += n
i++
}
if _, err := w.Write(packedRow); err != nil {
return err
}
}
return nil
}
func encodePGM(w io.Writer, m image.Image, maxvalue int) error {
b := m.Bounds()
// write header
_, err := fmt.Fprintf(w, "P5\n%d %d\n%d\n", b.Dx(), b.Dy(), maxvalue)
if err != nil {
return err
}
// write raster
cm := color.GrayModel
row := make([]uint8, b.Dx())
for y := b.Min.Y; y < b.Max.Y; y++ {
for x := b.Min.X; x < b.Max.X; x++ {
c := cm.Convert(m.At(x, y)).(color.Gray)
row[x-b.Min.X] = c.Y
}
if _, err := w.Write(row); err != nil {
return err
}
}
return nil
}
func encodePPM(w io.Writer, m image.Image, maxvalue int) error {
b := m.Bounds()
// write header
_, err := fmt.Fprintf(w, "P6\n%d %d\n%d\n", b.Dx(), b.Dy(), maxvalue)
if err != nil {
return err
}
// write raster
cm := color.RGBAModel
row := make([]uint8, b.Dx()*3)
for y := b.Min.Y; y < b.Max.Y; y++ {
i := 0
for x := b.Min.X; x < b.Max.X; x++ {
c := cm.Convert(m.At(x, y)).(color.RGBA)
row[i] = c.R
row[i+1] = c.G
row[i+2] = c.B
i += 3
}
if _, err := w.Write(row); err != nil {
return err
}
}
return nil
}
// Encode writes an image.Image m to io.Writer w in PNM format.
//
// The specific format is determined by pnmType, this can be one of:
// - pnm.PBM (black/white)
// - pnm.PGM (grayscale)
// - pnm.PPM (RGB)
// The image m is converted if necessary.
// Note that PGM/PPM always use 8 bits per channel at the moment and that
// maxvalue is always 255.
func Encode(w io.Writer, m image.Image, pnmType int) error {
switch pnmType {
case PBM:
return encodePBM(w, m)
case PGM:
return encodePGM(w, m, 255)
case PPM:
return encodePPM(w, m, 255)
}
return errors.New("Invalid PNM type specified.")
}