-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencode.go
More file actions
54 lines (46 loc) · 1.55 KB
/
encode.go
File metadata and controls
54 lines (46 loc) · 1.55 KB
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
package prism
//#cgo pkg-config: --libs-only-L opencv libturbojpeg
//#cgo CFLAGS: -O3 -Wno-error=unused-function
//#cgo LDFLAGS: -lopencv_imgproc -lopencv_core -lopencv_highgui -lturbojpeg
//#include "prism.h"
import "C"
import (
"errors"
"image/color"
"image/jpeg"
"io"
"unsafe"
)
// EncodeJPEG writes the Image img to w in JPEG 4:2:0 baseline format with the
// given quality, from 1 - 100.
func EncodeJPEG(w io.Writer, img *Image, quality int) (err error) {
defer recoverWithError(&err)
if img.ColorModel() == color.Gray16Model || img.ColorModel() == color.NRGBA64Model {
// workaround for lack of bitdepth conversion in OpenCV C API
err = jpeg.Encode(w, img, &jpeg.Options{Quality: quality})
return
}
result := C.prismEncodeJPEG(img.iplImage, C.int(quality))
if result == nil {
err = errors.New("Unable to encode JPEG image")
return
}
// write bytes directly without copying to Go-land
_, err = w.Write((*[1 << 30]byte)(unsafe.Pointer(result.buffer))[:result.size:result.size])
C.prismRelease(result)
return
}
// EncodePNG writes the Image img to w in PNG format with the given
// Zlib compression level, from 0 (none) - 9
func EncodePNG(w io.Writer, img *Image, compression int) (err error) {
defer recoverWithError(&err)
result := C.prismEncodePNG(img.iplImage, C.int(compression))
if result == nil {
err = errors.New("Unable to encode PNG image")
return
}
// write bytes directly without copying to Go-land
_, err = w.Write((*[1 << 30]byte)(unsafe.Pointer(result.buffer))[:result.size:result.size])
C.prismRelease(result)
return
}