...
Source file
src/image/png/paeth.go
1
2
3
4
5 package png
6
7
8 const intSize = 32 << (^uint(0) >> 63)
9
10 func abs(x int) int {
11
12 m := x >> (intSize - 1)
13
14
15
16
17
18
19 return (x ^ m) - m
20 }
21
22
23 func paeth(a, b, c uint8) uint8 {
24
25
26
27
28
29
30
31 pc := int(c)
32 pa := int(b) - pc
33 pb := int(a) - pc
34 pc = abs(pa + pb)
35 pa = abs(pa)
36 pb = abs(pb)
37 if pa <= pb && pa <= pc {
38 return a
39 } else if pb <= pc {
40 return b
41 }
42 return c
43 }
44
45
46
47 func filterPaeth(cdat, pdat []byte, bytesPerPixel int) {
48 var a, b, c, pa, pb, pc int
49 for i := 0; i < bytesPerPixel; i++ {
50 a, c = 0, 0
51 for j := i; j < len(cdat); j += bytesPerPixel {
52 b = int(pdat[j])
53 pa = b - c
54 pb = a - c
55 pc = abs(pa + pb)
56 pa = abs(pa)
57 pb = abs(pb)
58 if pa <= pb && pa <= pc {
59
60 } else if pb <= pc {
61 a = b
62 } else {
63 a = c
64 }
65 a += int(cdat[j])
66 a &= 0xff
67 cdat[j] = uint8(a)
68 c = b
69 }
70 }
71 }
72
View as plain text