Exemplo n.º 1
0
// 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-trubo, not libjpeg.
func DecodeIntoRGBA(r io.Reader, options *DecoderOptions) (dest *image.RGBA, err error) {
	// Recover panic
	defer func() {
		if r := recover(); r != nil {
			log.Println(r)
			if _, ok := r.(error); !ok {
				err = fmt.Errorf("JPEG error: %v", r)
			}
		}
	}()

	dinfo := C.new_decompress()
	defer C.destroy_decompress(dinfo)

	srcManager := makeSourceManager(r, dinfo)
	defer releaseSourceManager(srcManager)

	C.jpeg_read_header(dinfo, C.TRUE)
	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 := C.getJCS_EXT_RGBA()
	if colorSpace == C.JCS_UNKNOWN {
		return nil, errors.New("JCS_EXT_RGBA is not supported (probably built without libjpeg-trubo)")
	}

	dinfo.out_color_space = colorSpace
	readScanLines(dinfo, dest.Pix, dest.Stride)
	return
}
Exemplo n.º 2
0
// SupportRGBA returns whether RGBA decoding is supported.
func SupportRGBA() bool {
	if C.getJCS_EXT_RGBA() == C.JCS_UNKNOWN {
		return false
	}
	return true
}