-
Notifications
You must be signed in to change notification settings - Fork 0
/
evil-concurrente.go
69 lines (63 loc) · 1.31 KB
/
evil-concurrente.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
package main
import (
"fmt"
"image"
"image/color"
"image/jpeg"
"log"
"os"
"path/filepath"
"strings"
"sync"
"time"
)
func check(err error) {
if err != nil {
panic(err)
}
}
func main() {
imgPath := "lena.jpg"
f, err := os.Open(imgPath)
go check(err)
defer f.Close()
img, _, err := image.Decode(f)
size := img.Bounds().Size()
wImg := image.NewRGBA(image.Rect(0, 0, size.X, size.Y))
wg := new(sync.WaitGroup)
start := time.Now()
for y := 0; y < size.Y; y++ {
wg.Add(1)
y := y
go func() {
for x := 0; x < size.X; x++ {
pixel := img.At(x, y)
originalColor := color.RGBAModel.Convert(pixel).(color.RGBA)
// Offset colors a little, adjust it to your taste
r := uint8(originalColor.R)
g := uint8(originalColor.G)
b := uint8(originalColor.B)
// average Alpha16 A: 65535
//a := color.Alpha16{A: 45535}
wImg.Set(x, y, color.CMYK{
C: r,
M: g,
Y: b,
K: 0,
})
}
defer wg.Done()
}()
}
wg.Wait()
elapsed := time.Since(start)
log.Printf("RGB to Gray took %s", elapsed)
ext := filepath.Ext(imgPath)
name := strings.TrimSuffix(filepath.Base(imgPath), ext)
newImagePath := fmt.Sprintf("%s/%s_evil%s", filepath.Dir(imgPath), name, ext)
fg, err := os.Create(newImagePath)
defer fg.Close()
go check(err)
err = jpeg.Encode(fg, wImg, nil)
check(err)
}