コード例 #1
0
ファイル: decompress.go プロジェクト: ieee0824/go-libjpeg
// 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) {
	// 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 = rgb.NewImage(image.Rect(0, 0, int(dinfo.output_width), int(dinfo.output_height)))

	dinfo.out_color_space = C.JCS_RGB
	readScanLines(dinfo, dest.Pix, dest.Stride)
	return
}
コード例 #2
0
ファイル: decompress.go プロジェクト: ieee0824/go-libjpeg
// 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
	readScanLines(dinfo, dest.Pix, dest.Stride)
	return
}
コード例 #3
0
ファイル: rgb_test.go プロジェクト: edvakf/go-libjpeg
func TestImageInterface(t *testing.T) {
	rect := image.Rect(0, 0, 100, 100)
	img := rgb.NewImage(rect)

	if got := img.ColorModel(); got != rgb.ColorModel {
		t.Errorf("ColorModel() should return rgb.ColorModel, got: %v", got)
	}

	if got := img.Bounds(); got != rect {
		t.Errorf("Bounds() should return %v, got: %v", rect, got)
	}

	black := color.RGBA{0x00, 0x00, 0x00, 0xFF}
	if got := img.At(0, 0); got != black {
		t.Errorf("At(0, 0) should return %v, got: %v", black, got)
	}

	blank := color.RGBA{}
	if got := img.At(-1, -1); got != blank {
		t.Errorf("At(0, 0) should return %v, got: %v", blank, got)
	}
}