forked from pixiv/go-libjpeg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
decompress.go
643 lines (548 loc) · 17.2 KB
/
decompress.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
package jpeg
/*
#include <stdio.h>
#include <stdlib.h>
#include "jpeglib.h"
#include "jerror.h"
#include "jpeg.h"
void error_panic(j_common_ptr dinfo);
extern void custom_emit_message(j_common_ptr cinfo, int msg_level);
static struct jpeg_decompress_struct *new_decompress(void) {
struct jpeg_decompress_struct *dinfo = (struct jpeg_decompress_struct *)calloc(sizeof(struct jpeg_decompress_struct), 1);
if (!dinfo) {
return NULL;
}
struct my_error_mgr *jerr = (struct my_error_mgr *)calloc(sizeof(struct my_error_mgr), 1);
if (!jerr) {
free(dinfo);
return NULL;
}
dinfo->err = jpeg_std_error(&jerr->pub);
jerr->pub.error_exit = (void *)error_longjmp;
if (setjmp(jerr->jmpbuf) != 0) {
free(jerr);
free(dinfo);
return NULL;
}
jpeg_create_decompress(dinfo);
return dinfo;
}
static void forward_format_output(j_common_ptr cinfo, char *buffer) {
struct jpeg_error_mgr *err = cinfo->err;
(*err->format_message) (cinfo, buffer);
}
static int start_decompress(j_decompress_ptr dinfo)
{
// handle error
struct my_error_mgr *err = (struct my_error_mgr *)dinfo->err;
if (setjmp(err->jmpbuf) != 0) {
return err->pub.msg_code;
}
jpeg_start_decompress(dinfo);
return 0;
}
static int finish_decompress(j_decompress_ptr dinfo)
{
// handle error
struct my_error_mgr *err = (struct my_error_mgr *)dinfo->err;
if (setjmp(err->jmpbuf) != 0) {
return err->pub.msg_code;
}
jpeg_finish_decompress(dinfo);
return 0;
}
static void destroy_decompress(struct jpeg_decompress_struct *dinfo) {
free(dinfo->err);
jpeg_destroy_decompress(dinfo);
free(dinfo);
}
static int read_header(struct jpeg_decompress_struct *dinfo, int req_img)
{
// handle error
struct my_error_mgr *err = (struct my_error_mgr *)dinfo->err;
if (setjmp(err->jmpbuf) != 0) {
return err->pub.msg_code;
}
jpeg_read_header(dinfo, req_img);
return 0;
}
static JDIMENSION read_scanlines(j_decompress_ptr dinfo, unsigned char *buf, int stride, int height, int *msg_code) {
// handle error
struct my_error_mgr *err = (struct my_error_mgr *)dinfo->err;
if (setjmp(err->jmpbuf) != 0) {
*msg_code = err->pub.msg_code;
return 0;
}
JSAMPROW *rows = alloca(sizeof(JSAMPROW) * height);
int i;
for (i = 0; i < height; i++) {
rows[i] = &buf[i * stride];
}
*msg_code = 0;
return jpeg_read_scanlines(dinfo, rows, height);
}
static int DCT_v_scaled_size(j_decompress_ptr dinfo, int component) {
#if JPEG_LIB_VERSION >= 70
return dinfo->comp_info[component].DCT_v_scaled_size;
#else
return dinfo->comp_info[component].DCT_scaled_size;
#endif
}
static JDIMENSION read_mcu_gray(struct jpeg_decompress_struct *dinfo, JSAMPROW pix, int stride, int imcu_rows, int *msg_code) {
// handle error
struct my_error_mgr *err = (struct my_error_mgr *)dinfo->err;
if (setjmp(err->jmpbuf) != 0) {
*msg_code = err->pub.msg_code;
return 0;
}
JSAMPROW *rows = alloca(sizeof(JSAMPROW) * imcu_rows);
int h = 0;
for (h = 0; h < imcu_rows; h++) {
rows[h] = &pix[stride * h];
}
// Get the data
*msg_code = 0;
return jpeg_read_raw_data(dinfo, &rows, imcu_rows);
}
static JDIMENSION read_mcu_ycbcr(struct jpeg_decompress_struct *dinfo, JSAMPROW y_row, JSAMPROW cb_row, JSAMPROW cr_row, int y_stride, int c_stride, int imcu_rows, int *msg_code) {
// handle error
struct my_error_mgr *err = (struct my_error_mgr *)dinfo->err;
if (setjmp(err->jmpbuf) != 0) {
*msg_code = err->pub.msg_code;
return 0;
}
// Allocate JSAMPIMAGE to hold pointers to one iMCU worth of image data
// this is a safe overestimate; we use the return value from
// jpeg_read_raw_data to figure out what is the actual iMCU row count.
JSAMPROW *y_rows = alloca(sizeof(JSAMPROW) * imcu_rows);
JSAMPROW *cb_rows = alloca(sizeof(JSAMPROW) * imcu_rows);
JSAMPROW *cr_rows = alloca(sizeof(JSAMPROW) * imcu_rows);
JSAMPARRAY image[] = {y_rows, cb_rows, cr_rows};
int x = 0;
// First fill in the pointers into the plane data buffers
int h = 0;
for (h = 0; h < imcu_rows; h++) {
y_rows[h] = &y_row[y_stride * h];
cb_rows[h] = &cb_row[c_stride * h];
cr_rows[h] = &cr_row[c_stride * h];
}
// Get the data
*msg_code = 0;
return jpeg_read_raw_data(dinfo, image, imcu_rows);
}
*/
import "C"
import (
"errors"
"fmt"
"image"
"image/color"
"io"
"os"
"unsafe"
"github.com/pixiv/go-libjpeg/rgb"
)
//export custom_emit_message
func custom_emit_message(cinfo C.j_common_ptr, msgLevel C.int) {
// msg_level is -1 for warnings,
// 0 and up for trace messages.
err := cinfo.err
if msgLevel < 0 {
if err.num_warnings == 0 || err.trace_level >= 3 {
err.num_warnings++
if warningOutput != nil {
buf := make([]C.char, 256)
C.forward_format_output(cinfo, &buf[0])
(warningOutput).Write([]byte(C.GoString(&buf[0])))
}
}
return
}
if err.trace_level >= msgLevel {
buf := make([]C.char, 256)
C.forward_format_output(cinfo, &buf[0])
(warningOutput).Write([]byte(C.GoString(&buf[0])))
}
}
var warningOutput io.Writer = os.Stdout
func SetWarningOutput(w io.Writer) {
warningOutput = w
}
func newDecompress(r io.Reader) *C.struct_jpeg_decompress_struct {
dinfo := C.new_decompress()
if dinfo == nil {
return nil
}
makeSourceManager(r, dinfo)
return dinfo
}
func destroyDecompress(dinfo *C.struct_jpeg_decompress_struct) {
if dinfo == nil {
return
}
sourceManager := getSourceManager(dinfo)
if sourceManager != nil {
releaseSourceManager(sourceManager)
}
C.destroy_decompress(dinfo)
}
func readHeader(dinfo *C.struct_jpeg_decompress_struct) error {
if C.read_header(dinfo, C.TRUE) != 0 {
return errors.New(jpegErrorMessage(unsafe.Pointer(dinfo)))
}
return nil
}
func startDecompress(dinfo *C.struct_jpeg_decompress_struct) error {
if C.start_decompress(dinfo) != 0 {
return errors.New(jpegErrorMessage(unsafe.Pointer(dinfo)))
}
return nil
}
func finishDecompress(dinfo *C.struct_jpeg_decompress_struct) error {
if C.finish_decompress(dinfo) != 0 {
return errors.New(jpegErrorMessage(unsafe.Pointer(dinfo)))
}
return nil
}
func readScanlines(dinfo *C.struct_jpeg_decompress_struct, row *C.uchar, stride, height C.int) (lines C.JDIMENSION, err error) {
code := C.int(0)
lines = C.read_scanlines(dinfo, row, stride, height, &code)
if code != 0 {
err = errors.New(jpegErrorMessage(unsafe.Pointer(dinfo)))
} else if lines == 0 {
err = errors.New("unexpected EOF")
}
return
}
func readMCUGray(dinfo *C.struct_jpeg_decompress_struct, pix C.JSAMPROW, stride, iMCURows int) (line C.JDIMENSION, err error) {
code := C.int(0)
line = C.read_mcu_gray(dinfo, pix, C.int(stride), C.int(iMCURows), &code)
if code != 0 {
err = errors.New(jpegErrorMessage(unsafe.Pointer(dinfo)))
}
return
}
func readMCUYCbCr(dinfo *C.struct_jpeg_decompress_struct, y, cb, cr C.JSAMPROW, yStride, cStride int, iMCURows int) (line C.JDIMENSION, err error) {
code := C.int(0)
line = C.read_mcu_ycbcr(dinfo, y, cb, cr, C.int(yStride), C.int(cStride), C.int(iMCURows), &code)
if code != 0 {
err = errors.New(jpegErrorMessage(unsafe.Pointer(dinfo)))
}
return
}
// DecoderOptions specifies JPEG decoding parameters.
type DecoderOptions struct {
ScaleTarget image.Rectangle // ScaleTarget is the target size to scale image.
DCTMethod DCTMethod // DCTMethod is DCT Algorithm method.
DisableFancyUpsampling bool // If true, disable fancy upsampling
DisableBlockSmoothing bool // If true, disable block smoothing
}
type ErrMgr struct {
NumWarnings int
}
// SupportRGBA returns whether RGBA decoding is supported.
func SupportRGBA() bool {
if getJCS_EXT_RGBA() == C.JCS_UNKNOWN {
return false
}
return true
}
// Decode reads a JPEG data stream from r and returns decoded image as an image.Image.
// Output image has YCbCr colors or 8bit Grayscale.
func Decode(r io.Reader, options *DecoderOptions) (dest image.Image, errMgr ErrMgr, err error) {
dinfo := newDecompress(r)
if dinfo == nil {
return nil, ErrMgr{}, errors.New("allocation failed")
}
defer destroyDecompress(dinfo)
dinfo.err.emit_message = (*[0]byte)(C.custom_emit_message)
err = readHeader(dinfo)
if err != nil {
return nil, ErrMgr{}, err
}
setupDecoderOptions(dinfo, options)
switch dinfo.num_components {
case 1:
if dinfo.jpeg_color_space != C.JCS_GRAYSCALE {
return nil, ErrMgr{}, errors.New("unsupported colorspace")
}
dest, err = decodeGray(dinfo)
case 3:
switch dinfo.jpeg_color_space {
case C.JCS_YCbCr:
dest, err = decodeYCbCr(dinfo)
case C.JCS_RGB:
fmt.Println("decoding rgb")
dest, err = decodeRGB(dinfo)
default:
return nil, ErrMgr{}, errors.New("unsupported colorspace")
}
default:
return nil, ErrMgr{}, fmt.Errorf("unsupported number of components: %d", dinfo.num_components)
}
// fmt.Println("dinfo.err.num_warnings", dinfo.err.num_warnings)
errMgr = ErrMgr{
NumWarnings: int(dinfo.err.num_warnings),
}
return
}
func decodeGray(dinfo *C.struct_jpeg_decompress_struct) (dest *image.Gray, err error) {
// output dawnsampled raw data before starting decompress
dinfo.raw_data_out = C.TRUE
err = startDecompress(dinfo)
if err != nil {
return nil, err
}
defer func() {
ferr := finishDecompress(dinfo)
if ferr != nil && err == nil {
err = ferr
}
}()
compInfo := (*[1]C.jpeg_component_info)(unsafe.Pointer(dinfo.comp_info))
dest = NewGrayAligned(image.Rect(0, 0, int(compInfo[0].downsampled_width), int(compInfo[0].downsampled_height)))
iMCURows := int(C.DCT_v_scaled_size(dinfo, C.int(0)) * compInfo[0].v_samp_factor)
for dinfo.output_scanline < dinfo.output_height {
_, err = readMCUGray(dinfo, C.JSAMPROW(unsafe.Pointer(&dest.Pix[dest.Stride*int(dinfo.output_scanline)])), dest.Stride, iMCURows)
if err != nil {
return
}
}
return
}
func decodeYCbCr(dinfo *C.struct_jpeg_decompress_struct) (dest *image.YCbCr, err error) {
// output dawnsampled raw data before starting decompress
dinfo.raw_data_out = C.TRUE
err = startDecompress(dinfo)
if err != nil {
return nil, err
}
compInfo := (*[3]C.jpeg_component_info)(unsafe.Pointer(dinfo.comp_info))
dwY := compInfo[Y].downsampled_width
dhY := compInfo[Y].downsampled_height
dwC := compInfo[Cb].downsampled_width
dhC := compInfo[Cb].downsampled_height
//fmt.Printf("%d %d %d %d\n", dwY, dhY, dwC, dhC)
if dwC != compInfo[Cr].downsampled_width || dhC != compInfo[Cr].downsampled_height {
return nil, errors.New("Unsupported color subsampling (Cb and Cr differ)")
}
// Since the decisions about which DCT size and subsampling mode
// to use, if any, are complex, instead just check the calculated
// output plane sizes and infer the subsampling mode from that.
var subsampleRatio image.YCbCrSubsampleRatio
cVDiv := 1
switch {
case dwY == dwC && dhY == dhC:
subsampleRatio = image.YCbCrSubsampleRatio444
case dwY == dwC && (dhY+1)/2 == dhC:
subsampleRatio = image.YCbCrSubsampleRatio440
cVDiv = 2
case (dwY+1)/2 == dwC && dhY == dhC:
subsampleRatio = image.YCbCrSubsampleRatio422
case (dwY+1)/2 == dwC && (dhY+1)/2 == dhC:
subsampleRatio = image.YCbCrSubsampleRatio420
cVDiv = 2
default:
return nil, errors.New("Unsupported color subsampling")
}
// Allocate distination iamge
dest = NewYCbCrAligned(image.Rect(0, 0, int(dinfo.output_width), int(dinfo.output_height)), subsampleRatio)
var iMCURows int
for i := 0; i < int(dinfo.num_components); i++ {
compRows := int(C.DCT_v_scaled_size(dinfo, C.int(i)) * compInfo[i].v_samp_factor)
if compRows > iMCURows {
iMCURows = compRows
}
}
yStride, cStride := dest.YStride, dest.CStride
for dinfo.output_scanline < dinfo.output_height {
y := C.JSAMPROW(unsafe.Pointer(&dest.Y[yStride*int(dinfo.output_scanline)]))
cb := C.JSAMPROW(unsafe.Pointer(&dest.Cb[cStride*int(dinfo.output_scanline)/cVDiv]))
cr := C.JSAMPROW(unsafe.Pointer(&dest.Cr[cStride*int(dinfo.output_scanline)/cVDiv]))
_, err = readMCUYCbCr(dinfo, y, cb, cr, yStride, cStride, iMCURows)
if err != nil {
return
}
}
return
}
func readRGBScanlines(dinfo *C.struct_jpeg_decompress_struct, pix []uint8, stride int) (err error) {
err = startDecompress(dinfo)
if err != nil {
return
}
defer func() {
ferr := finishDecompress(dinfo)
if ferr != nil && err == nil {
err = ferr
}
}()
for dinfo.output_scanline < dinfo.output_height {
pbuf := (*C.uchar)(unsafe.Pointer(&pix[stride*int(dinfo.output_scanline)]))
_, err = readScanlines(dinfo, pbuf, C.int(stride), dinfo.rec_outbuf_height)
if err != nil {
return
}
}
return
}
// TODO: supports decoding into image.RGBA instead of rgb.Image.
func decodeRGB(dinfo *C.struct_jpeg_decompress_struct) (dest *rgb.Image, err error) {
C.jpeg_calc_output_dimensions(dinfo)
dest = rgb.NewImage(image.Rect(0, 0, int(dinfo.output_width), int(dinfo.output_height)))
dinfo.out_color_space = C.JCS_RGB
err = readRGBScanlines(dinfo, dest.Pix, dest.Stride)
return
}
// DecodeIntoRGB reads a JPEG data stream from r and returns decoded image as an rgb.Image with RGB colors.
func DecodeIntoRGB(r io.Reader, options *DecoderOptions) (dest *rgb.Image, err error) {
dinfo := newDecompress(r)
if dinfo == nil {
return nil, errors.New("allocation failed")
}
defer destroyDecompress(dinfo)
err = readHeader(dinfo)
if err != nil {
return nil, err
}
setupDecoderOptions(dinfo, options)
return decodeRGB(dinfo)
}
// DecodeIntoRGBA reads a JPEG data stream from r and returns decoded image as an image.RGBA with RGBA colors.
// This function only works with libjpeg-turbo, not libjpeg.
func DecodeIntoRGBA(r io.Reader, options *DecoderOptions) (dest *image.RGBA, err error) {
dinfo := newDecompress(r)
if dinfo == nil {
return nil, errors.New("allocation failed")
}
defer destroyDecompress(dinfo)
// Recover panic
defer func() {
if r := recover(); r != nil {
if _, ok := r.(error); !ok {
err = fmt.Errorf("JPEG error: %v", r)
}
}
}()
err = readHeader(dinfo)
if err != nil {
return nil, err
}
setupDecoderOptions(dinfo, options)
C.jpeg_calc_output_dimensions(dinfo)
dest = image.NewRGBA(image.Rect(0, 0, int(dinfo.output_width), int(dinfo.output_height)))
colorSpace := getJCS_EXT_RGBA()
if colorSpace == C.JCS_UNKNOWN {
return nil, errors.New("JCS_EXT_RGBA is not supported (probably built without libjpeg-turbo)")
}
dinfo.out_color_space = colorSpace
err = readRGBScanlines(dinfo, dest.Pix, dest.Stride)
return
}
// DecodeConfig returns the color model and dimensions of a JPEG image without decoding the entire image.
func DecodeConfig(r io.Reader) (config image.Config, err error) {
dinfo := newDecompress(r)
if dinfo == nil {
err = errors.New("allocation failed")
return
}
defer destroyDecompress(dinfo)
// Recover panic
defer func() {
if r := recover(); r != nil {
if _, ok := r.(error); !ok {
err = fmt.Errorf("JPEG error: %v", r)
}
}
}()
err = readHeader(dinfo)
if err != nil {
return
}
config = image.Config{
ColorModel: color.YCbCrModel,
Width: int(dinfo.image_width),
Height: int(dinfo.image_height),
}
return
}
func setupDecoderOptions(dinfo *C.struct_jpeg_decompress_struct, opt *DecoderOptions) {
tw, th := opt.ScaleTarget.Dx(), opt.ScaleTarget.Dy()
if tw > 0 && th > 0 {
var scaleFactor int
for scaleFactor = 1; scaleFactor <= 8; scaleFactor++ {
if ((scaleFactor*int(dinfo.image_width)+7)/8) >= tw &&
((scaleFactor*int(dinfo.image_height)+7)/8) >= th {
break
}
}
if scaleFactor < 8 {
dinfo.scale_num = C.uint(scaleFactor)
dinfo.scale_denom = 8
}
}
dinfo.dct_method = C.J_DCT_METHOD(opt.DCTMethod)
if opt.DisableFancyUpsampling {
dinfo.do_fancy_upsampling = C.FALSE
} else {
dinfo.do_fancy_upsampling = C.TRUE
}
if opt.DisableBlockSmoothing {
dinfo.do_block_smoothing = C.FALSE
} else {
dinfo.do_block_smoothing = C.TRUE
}
}
const alignSize int = C.ALIGN_SIZE
// NewYCbCrAligned Allocates YCbCr image with padding.
// Because LibJPEG needs extra padding to decoding buffer, This func add an
// extra alignSize (16) padding to cover overflow from any such modes.
func NewYCbCrAligned(r image.Rectangle, subsampleRatio image.YCbCrSubsampleRatio) *image.YCbCr {
w, h, cw, ch := r.Dx(), r.Dy(), 0, 0
switch subsampleRatio {
case image.YCbCrSubsampleRatio422:
cw = (r.Max.X+1)/2 - r.Min.X/2
ch = h
case image.YCbCrSubsampleRatio420:
cw = (r.Max.X+1)/2 - r.Min.X/2
ch = (r.Max.Y+1)/2 - r.Min.Y/2
case image.YCbCrSubsampleRatio440:
cw = w
ch = (r.Max.Y+1)/2 - r.Min.Y/2
default:
cw = w
ch = h
}
// TODO: check the padding size to minimize memory allocation.
yStride := pad(w, alignSize) + alignSize
cStride := pad(cw, alignSize) + alignSize
yHeight := pad(h, alignSize) + alignSize
cHeight := pad(ch, alignSize) + alignSize
b := make([]byte, yStride*yHeight+2*cStride*cHeight)
return &image.YCbCr{
Y: b[:yStride*yHeight],
Cb: b[yStride*yHeight+0*cStride*cHeight : yStride*yHeight+1*cStride*cHeight],
Cr: b[yStride*yHeight+1*cStride*cHeight : yStride*yHeight+2*cStride*cHeight],
SubsampleRatio: subsampleRatio,
YStride: yStride,
CStride: cStride,
Rect: r,
}
}
func pad(a int, b int) int {
return (a + (b - 1)) & (^(b - 1))
}
// NewGrayAligned Allocates Grey image with padding.
// This func add an extra padding to cover overflow from decoding image.
func NewGrayAligned(r image.Rectangle) *image.Gray {
w, h := r.Dx(), r.Dy()
// TODO: check the padding size to minimize memory allocation.
stride := pad(w, alignSize) + alignSize
ph := pad(h, alignSize) + alignSize
pix := make([]uint8, stride*ph)
return &image.Gray{
Pix: pix,
Stride: stride,
Rect: r,
}
}