Debian Codegraph @

encode.go

raw symbols SCIP
1 package golibjpegturbo
2
3 /*
4 #include <stddef.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <jpeglib.h>
8 typedef unsigned char *PUCHAR;
9
10 void error_panic(j_common_ptr cinfo);
11
12 typedef struct {
13 unsigned char *buf;
14 unsigned long buf_size;
15 } mem_helper;
16
17 mem_helper *alloc_mem_helper();
18 */
19 import "C"
20
21 import (
22 "fmt"
23 "image"
24 "io"
25 "unsafe"
26 )
27
28 // DefaultQuality is the default quality encoding parameter.
29 const DefaultQuality = 75
30
31 // Options are the encoding parameters.
32 // Quality ranges from 1 to 100 inclusive, higher is better.
33 type Options struct {
34 Quality int
35 }
36
37 // Encode writes the Image m to w in JPEG 4:2:0 baseline format with the given
38 // options. Default parameters are used if a nil *Options is passed.
39 func Encode(w io.Writer, m image.Image, o *Options) (err error) {
40 defer func() {
41 if r := recover(); r != nil {
42 var ok bool
43 err, ok = r.(error)
44 if !ok {
45 err = fmt.Errorf("JPEG error: %v", r)
46 }
47 }
48 }()
49
50 b := m.Bounds()
51 dx := b.Dx()
52 dy := b.Dy()
53 if dx <= 0 || dy <= 0 {
54 return fmt.Errorf("image with invalid size, dx: %d, dy: %d (both must be > 0)", dx, dy)
55 }
56
57 quality := 75
58 if o != nil {
59 quality = o.Quality
60 }
61
62 cinfoSize := C.size_t(unsafe.Sizeof(C.struct_jpeg_compress_struct{}))
63
64 cinfo := (*C.struct_jpeg_compress_struct)(C.malloc(cinfoSize))
65 defer C.free(unsafe.Pointer(cinfo))
66
67 cinfoErrSize := C.size_t(unsafe.Sizeof(C.struct_jpeg_error_mgr{}))
68 cinfo.err = (*C.struct_jpeg_error_mgr)(C.malloc(cinfoErrSize))
69 defer C.free(unsafe.Pointer(cinfo.err))
70
71 C.jpeg_std_error(cinfo.err)
72 cinfo.err.error_exit = (*[0]byte)(C.error_panic)
73
74 memHelper := C.alloc_mem_helper()
75 C.jpeg_CreateCompress(cinfo, C.JPEG_LIB_VERSION, cinfoSize)
76 C.jpeg_mem_dest(cinfo, &memHelper.buf, &memHelper.buf_size)
77
78 nBytes := dx * 3 // for a line, 3 bytes per pixel
79 cinfo.image_width = C.JDIMENSION(dx)
80 cinfo.image_height = C.JDIMENSION(dy)
81
82 gray, isGray := m.(*image.Gray)
83 rgba, isRgba := m.(*image.RGBA)
84
85 cinfo.input_components = 3
86 cinfo.in_color_space = C.JCS_RGB
87
88 if isGray {
89 nBytes = dx
90 cinfo.input_components = 1
91 cinfo.in_color_space = C.JCS_GRAYSCALE
92 }
93 // Note: for more speed could try to go directly to JCS_EXT_RGBA but not
94 // sure if libjpeg matches Go and treats JCS_EXT_RGBA as alpha-premultipled
95 if isRgba {
96 nBytes = dx * 3
97 cinfo.input_components = 3
98 cinfo.in_color_space = C.JCS_RGB
99 }
100
101 C.jpeg_set_defaults(cinfo)
102 C.jpeg_set_quality(cinfo, C.int(quality), C.TRUE)
103 C.jpeg_start_compress(cinfo, C.TRUE)
104
105 bufBytes := C.malloc(C.size_t(nBytes))
106 rowPtr := C.JSAMPROW(bufBytes)
107 buf := sliceFromCBytes(bufBytes, nBytes)
108
109 if isGray {
110 for y := 0; y < dy; y++ {
111 off := y * gray.Stride
112 copy(buf[:], gray.Pix[off:off+nBytes])
113 C.jpeg_write_scanlines(cinfo, &rowPtr, 1)
114 }
115 } else if isRgba {
116 for y := 0; y < dy; y++ {
117 off := y * rgba.Stride
118 p := rgba.Pix[off:]
119 dstOff := 0
120 srcOff := 0
121 for x := 0; x < dx; x++ {
122 buf[dstOff] = p[srcOff]
123 dstOff++
124 srcOff++
125 buf[dstOff] = p[srcOff]
126 dstOff++
127 srcOff++
128 buf[dstOff] = p[srcOff]
129 dstOff++
130 srcOff += 2
131 }
132
133 C.jpeg_write_scanlines(cinfo, &rowPtr, 1)
134 }
135 } else {
136 for y := 0; y < dy; y++ {
137 off := 0
138 for x := 0; x < dx; x++ {
139 r, g, b, _ := m.At(x, y).RGBA()
140 buf[off] = byte(r >> 8)
141 off++
142 buf[off] = byte(g >> 8)
143 off++
144 buf[off] = byte(b >> 8)
145 off++
146 }
147
148 C.jpeg_write_scanlines(cinfo, &rowPtr, 1)
149 }
150 }
151
152 C.jpeg_finish_compress(cinfo)
153 C.jpeg_destroy_compress(cinfo)
154
155 outBs := C.GoBytes(unsafe.Pointer(memHelper.buf), C.int(memHelper.buf_size))
156 w.Write(outBs)
157 C.free(unsafe.Pointer(memHelper.buf))
158 C.free(bufBytes)
159 C.free(unsafe.Pointer(memHelper))
160 return nil
161 }