-
Notifications
You must be signed in to change notification settings - Fork 0
/
PPM.go
413 lines (372 loc) · 10.8 KB
/
PPM.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
package Netpbm
import (
"errors"
"fmt"
"math"
"os"
"strconv"
"strings"
)
type PPM struct {
data [][]Pixel
width, height int
magicNumber string
max uint8
}
type Pixel struct {
R, G, B uint8
}
// ReadPPM reads a PPM image from a file and returns a struct that represents the image.
func ReadPPM(filename string) (*PPM, error) {
var err error
var magicNumber string = ""
var width int
var height int
var maxval int
var counter int
var headersize int
var splitfile []string
file, err := os.ReadFile(filename)
if err != nil {
}
if strings.Contains(string(file), "\r") {
splitfile = strings.SplitN(string(file), "\r\n", -1)
} else {
splitfile = strings.SplitN(string(file), "\n", -1)
}
// Range each line, to insert informations into the struct
for i, _ := range splitfile {
if strings.Contains(splitfile[i], "P3") {
magicNumber = "P3"
} else if strings.Contains(splitfile[i], "P6") {
magicNumber = "P6"
}
if strings.HasPrefix(splitfile[i], "#") && maxval != 0 {
headersize = counter
}
splitl := strings.SplitN(splitfile[i], " ", -1)
if width == 0 && height == 0 && len(splitl) >= 2 {
width, err = strconv.Atoi(splitl[0])
height, err = strconv.Atoi(splitl[1])
headersize = counter
}
if maxval == 0 && width != 0 {
maxval, err = strconv.Atoi(splitfile[i])
headersize = counter
}
counter++
}
data := make([][]Pixel, height)
var splitdata []string
if counter > headersize {
// Split the data each time there is a space to insert it into the data
for i := 0; i < height; i++ {
splitdata = strings.SplitN(splitfile[headersize+1+i], " ", -1)
data[i] = make([]Pixel, width)
for j := 0; j < width*3; j += 3 {
r, _ := strconv.Atoi(splitdata[j])
// If are used to verify that the value is within the limit of the file
if r > maxval {
r = maxval
}
g, _ := strconv.Atoi(splitdata[j+1])
if g > maxval {
g = maxval
}
b, _ := strconv.Atoi(splitdata[j+2])
if b > maxval {
b = maxval
}
data[i][j/3] = Pixel{R: uint8(r), G: uint8(g), B: uint8(b)}
}
}
}
return &PPM{data: data, width: width, height: height, magicNumber: magicNumber, max: uint8(maxval)}, err
}
// Size returns the width and height of the image.
func (ppm *PPM) Size() (int, int) {
return ppm.width, ppm.height
}
// At returns the value of the pixel at (x, y).
func (ppm *PPM) At(x, y int) Pixel {
return ppm.data[y][x]
}
// Set sets the value of the pixel at (x, y).
func (ppm *PPM) Set(x, y int, value Pixel) {
ppm.data[x][y] = value
}
// Save saves the PPM image to a file and returns an error if there was a problem.
func (ppm *PPM) Save(filename string) error {
file, err := os.Create(filename)
if err != nil {
file.Close()
return err
}
// Write magic number to the save
_, err = fmt.Fprintln(file, ppm.magicNumber)
if err != nil {
file.Close()
return err
}
// Write width and height to the save
_, err = fmt.Fprintln(file, ppm.width, ppm.height)
if err != nil {
file.Close()
return err
}
// Write max value to the save
_, err = fmt.Fprintln(file, ppm.max)
if err != nil {
file.Close()
return err
}
// Print the data into the file
for y := 0; y < ppm.height; y++ {
for x := 0; x < ppm.width; x++ {
if ppm.data[y][x].R > ppm.max || ppm.data[y][x].G > ppm.max || ppm.data[y][x].B > ppm.max {
errors.New("data value is too high")
} else {
fmt.Fprint(file, ppm.data[y][x].R, ppm.data[y][x].G, ppm.data[y][x].B, " ")
}
}
fmt.Fprintln(file)
}
return err
}
// Invert inverts the colors of the PPM image.
func (ppm *PPM) Invert() {
for i := 0; i < ppm.height; i++ {
for j := 0; j < ppm.width; j++ {
r := ppm.max - ppm.data[i][j].R
g := ppm.max - ppm.data[i][j].G
b := ppm.max - ppm.data[i][j].B
ppm.data[i][j] = Pixel{R: r, G: g, B: b}
}
}
}
// Flip flips the PPM image horizontally.
func (ppm *PPM) Flip() {
for i := 0; i < len(ppm.data); i++ {
for j := 0; j < len(ppm.data[i])/2; j++ {
startdata := ppm.data[i][j]
ppm.data[i][j] = ppm.data[i][len(ppm.data[i])-1-j]
ppm.data[i][len(ppm.data[i])-1-j] = startdata
}
}
}
// Flop flops the PPM image vertically.
func (ppm *PPM) Flop() {
for i := 0; i < len(ppm.data)/2; i++ {
startdata := ppm.data[i]
ppm.data[i] = ppm.data[len(ppm.data)-1-i]
ppm.data[len(ppm.data)-1-i] = startdata
}
}
// SetMagicNumber sets the magic number of the PPM image.
func (ppm *PPM) SetMagicNumber(magicNumber string) {
ppm.magicNumber = magicNumber
}
// SetMaxValue sets the max value of the PPM image.
func (ppm *PPM) SetMaxValue(maxValue uint8) {
oldMax := ppm.max
ppm.max = maxValue
var r, g, b uint8
for i := 0; i < ppm.height; i++ {
for j := 0; j < ppm.width; j++ {
r = uint8(float64(ppm.data[i][j].R) * float64(ppm.max) / float64(oldMax))
g = uint8(float64(ppm.data[i][j].G) * float64(ppm.max) / float64(oldMax))
b = uint8(float64(ppm.data[i][j].B) * float64(ppm.max) / float64(oldMax))
ppm.data[i][j] = Pixel{R: r, G: g, B: b}
}
}
}
// Rotate90CW rotates the PPM image 90° clockwise.
func (ppm *PPM) Rotate90CW() {
newData := make([][]Pixel, ppm.width)
for i := 0; i < ppm.height; i++ {
newData[i] = make([]Pixel, ppm.height)
for j := 0; j < ppm.width; j++ {
newData[i][j] = ppm.data[j][i]
}
}
for i := 0; i < ppm.height; i++ {
for j := 0; j < ppm.width/2; j++ {
prevdata := newData[i][j]
newData[i][j] = newData[i][ppm.height-j-1]
newData[i][ppm.height-j-1] = prevdata
}
}
ppm.data = newData
}
// ToPGM converts the PPM image to PGM.
func (ppm *PPM) ToPGM() *PGM {
var magicnb string
data := make([][]uint8, ppm.height)
// Change the magic number to the correct one
if ppm.magicNumber == "P3" {
magicnb = "P2"
} else if ppm.magicNumber == "P6" {
magicnb = "P5"
}
// Change the data to PGM format
for i := 0; i < ppm.height; i++ {
data[i] = make([]uint8, ppm.width)
for j := 0; j < ppm.width; j++ {
data[i][j] = uint8((int(ppm.data[i][j].R) + int(ppm.data[i][j].G) + int(ppm.data[i][j].B)) / 3)
}
}
return &PGM{magicNumber: magicnb, data: data, width: ppm.width, height: ppm.height, max: ppm.max}
}
// ToPBM converts the PPM image to PBM.
func (ppm *PPM) ToPBM() *PBM {
var magicnb string
data := make([][]bool, ppm.height)
// Change the magic number to the correct one
if ppm.magicNumber == "P3" {
magicnb = "P1"
} else if ppm.magicNumber == "P6" {
magicnb = "P4"
}
// Change the data to PBM format
for i := 0; i < ppm.height; i++ {
data[i] = make([]bool, ppm.width)
for j := 0; j < ppm.width; j++ {
data[i][j] = uint8((int(ppm.data[i][j].R)+int(ppm.data[i][j].G)+int(ppm.data[i][j].B))/3) < ppm.max/2
}
}
return &PBM{magicNumber: magicnb, width: ppm.width, height: ppm.height, data: data}
}
type Point struct {
X, Y int
}
// DrawLine draws a line between two points in the PPM image using the specified color.
func (ppm *PPM) DrawLine(p1, p2 Point, color Pixel) {
// Calculate the differences in X and Y coordinates
dx := float64(p2.X - p1.X)
dy := float64(p2.Y - p1.Y)
// Determine the number of steps based on the maximum difference in coordinates
steps := int(math.Max(math.Abs(dx), math.Abs(dy)))
// Calculate the incremental changes in X and Y coordinates
xIncrement := dx / float64(steps)
yIncrement := dy / float64(steps)
// Initialize the starting coordinates
x, y := float64(p1.X), float64(p1.Y)
// Draw the line by setting the specified color at each point along the line
for i := 0; i <= steps; i++ {
ppm.drawdata(int(x), int(y), color)
x += xIncrement
y += yIncrement
}
}
// DrawRectangle draws a rectangle.
func (ppm *PPM) DrawRectangle(p1 Point, width, height int, color Pixel) {
// Verify if the height of the rectangle go beyond the size of the canva
if p1.Y+height < ppm.height {
for y := p1.Y; y <= p1.Y+height; y++ {
// Verify if the width of the rectangle goes beyond the size of the canva
if p1.X+width < ppm.width {
for x := p1.X; x <= p1.X+width; x++ {
if x == p1.X || x == p1.X+width {
ppm.data[y][x] = color
} else if y == p1.Y || y == p1.Y+height {
ppm.data[y][x] = color
}
}
} else if p1.X+width >= ppm.width {
for x := p1.X; x < ppm.width; x++ {
if y == p1.Y || y == p1.Y+height {
ppm.data[y][x] = color
} else if x == p1.X || x == p1.X+width {
ppm.data[y][x] = color
}
}
}
}
} else if p1.Y+height >= ppm.height {
for y := p1.Y; y < ppm.height; y++ {
if p1.X+width < ppm.width {
for x := p1.X; x <= p1.X+width; x++ {
if x == p1.X || x == p1.X+width {
ppm.data[y][x] = color
} else if y == p1.Y || y == p1.Y+height {
ppm.data[y][x] = color
}
}
} else if p1.X+width >= ppm.width {
for x := p1.X; x < ppm.width; x++ {
if y == p1.Y || y == p1.Y+height {
ppm.data[y][x] = color
} else if x == p1.X || x == p1.X+width {
ppm.data[y][x] = color
}
}
}
}
}
}
// DrawFilledRectangle draws a filled rectangle.
func (ppm *PPM) DrawFilledRectangle(p1 Point, width, height int, color Pixel) {
// If the height of the rectangle is within the limit of the file
if p1.Y+height < ppm.height {
for y := p1.Y; y < p1.Y+height; y++ {
// If the width of the rectangle is within the data of the file
if p1.X+width < ppm.width {
for x := p1.X; x <= p1.X+width; x++ {
ppm.data[y][x] = color
}
// If the width of the rectangle is within the data of the file
} else if p1.X+width > ppm.width {
for x := p1.X; x < ppm.width; x++ {
ppm.data[y][x] = color
}
}
}
// If the height of the rectangle goes beyond the limit of the file
} else if p1.Y+height > ppm.height {
for y := p1.Y; y < ppm.height; y++ {
// If the width of the rectangle is within the data of the file
if p1.X+width < ppm.width {
for x := p1.X; x <= p1.X+width; x++ {
ppm.data[y][x] = color
}
// If the width of the rectangle is within the data of the file
} else if p1.X+width > ppm.width {
for x := p1.X; x < ppm.width; x++ {
ppm.data[y][x] = color
}
}
}
}
}
// DrawCircle draws a circle.
func (ppm *PPM) DrawCircle(center Point, radius int, color Pixel) {
// ...
}
// DrawFilledCircle draws a filled circle.
func (ppm *PPM) DrawFilledCircle(center Point, radius int, color Pixel) {
// ...
}
// DrawTriangle draws a triangle.
func (ppm *PPM) DrawTriangle(p1, p2, p3 Point, color Pixel) {
ppm.DrawLine(p1, p2, color)
ppm.DrawLine(p2, p3, color)
ppm.DrawLine(p3, p1, color)
}
// DrawFilledTriangle draws a filled triangle.
func (ppm *PPM) DrawFilledTriangle(p1, p2, p3 Point, color Pixel) {
// ...
}
// DrawPolygon draws a polygon.
func (ppm *PPM) DrawPolygon(points []Point, color Pixel) {
// ...
}
// DrawFilledPolygon draws a filled polygon.
func (ppm *PPM) DrawFilledPolygon(points []Point, color Pixel) {
// ...
}
// drawdata draw color only if they are within range
func (ppm *PPM) drawdata(x, y int, value Pixel) {
if x >= 0 && x < ppm.width && y >= 0 && y < ppm.height {
ppm.data[y][x] = value
}
}