Debian Codegraph @

tests/test-race.go

raw
1 /**
2 * Go race detector
3 *
4 * MIT License
5 *
6 * Copyright (c) 2019 struktur AG, Joachim Bauch <[email protected]>
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * SOFTWARE.
25 *
26 * Based on an example by "Hans Duedal" ([email protected]) to reproduce
27 * https://github.com/strukturag/libheif/issues/147
28 *
29 */
30 package main
31
32 import (
33 "fmt"
34 "image"
35 "io/ioutil"
36 "os"
37 "sync"
38
39 "github.com/strukturag/libheif/go/heif"
40 )
41
42 func decodeImage(data []byte) (image.Image, int, int, error) {
43 ctx, err := heif.NewContext()
44 if err != nil {
45 return nil, 0, 0, err
46 }
47 err = ctx.ReadFromMemory(data)
48 if err != nil {
49 return nil, 0, 0, err
50 }
51 imgh, err := ctx.GetPrimaryImageHandle()
52 if err != nil {
53 return nil, 0, 0, err
54 }
55
56 width := imgh.GetWidth()
57 height := imgh.GetHeight()
58
59 img, err := imgh.DecodeImage(heif.ColorspaceUndefined, heif.ChromaUndefined, nil)
60 if err != nil {
61 return nil, 0, 0, err
62 }
63
64 img, err = img.ScaleImage(width/2, height/2)
65 if err != nil {
66 return nil, 0, 0, err
67 }
68
69 goimg, err := img.GetImage()
70 if err != nil {
71 return nil, 0, 0, err
72 }
73
74 return goimg, width / 2, height / 2, nil
75 }
76
77 func main() {
78 if len(os.Args) != 2 {
79 fmt.Printf("USAGE: %s <filename>\n", os.Args[0])
80 os.Exit(1)
81 }
82
83 filename := os.Args[1]
84 imgbytes, err := ioutil.ReadFile(filename)
85 if err != nil {
86 panic(err)
87 }
88
89 count := 100
90
91 var ready sync.WaitGroup
92 var done sync.WaitGroup
93
94 fmt.Printf("Decoding in %d goroutines ...", count)
95 ready.Add(count)
96 done.Add(count)
97 for i := 0; i < count; i++ {
98 go func() {
99 defer done.Done()
100 ready.Done()
101 ready.Wait()
102 _, _, _, err := decodeImage(imgbytes)
103 if err != nil {
104 panic(err)
105 }
106 }()
107 }
108 done.Wait()
109 fmt.Println("ok")
110 }