Пример #1
0
func decodeCMYK(cinfo *C.struct_jpeg_decompress_struct) image.Image {
	buf := make([]byte, int(cinfo.output_width*4))
	scanLine := &buf[0]
	scanLines := C.JSAMPARRAY(unsafe.Pointer(&scanLine))

	img := image.NewRGBA(image.Rect(0, 0, int(cinfo.output_width), int(cinfo.output_height)))

	for cinfo.output_scanline < cinfo.output_height {
		y := int(cinfo.output_scanline)

		C.jpeg_read_scanlines(cinfo, scanLines, 1)

		for x := 0; x < int(cinfo.output_width); x += 1 {
			off := y * img.Stride

			c := buf[x*4+0]
			m := buf[x*4+1]
			y := buf[x*4+2]
			k := buf[x*4+3]

			r := uint8(255. * (1. - float64(c)) * (1. - float64(k)))
			g := uint8(255. * (1. - float64(m)) * (1. - float64(k)))
			b := uint8(255. * (1. - float64(y)) * (1. - float64(k)))

			img.Pix[off+4*x+0] = r
			img.Pix[off+4*x+1] = g
			img.Pix[off+4*x+2] = b
			img.Pix[off+4*x+3] = 255
		}
	}

	return img
}
Пример #2
0
func decodeGrayscale(cinfo *C.struct_jpeg_decompress_struct) image.Image {
	buf := make([]byte, int(cinfo.output_width))
	scanLine := &buf[0]
	scanLines := C.JSAMPARRAY(unsafe.Pointer(&scanLine))

	img := image.NewGray(image.Rect(0, 0, int(cinfo.output_width), int(cinfo.output_height)))

	for cinfo.output_scanline < cinfo.output_height {
		y := int(cinfo.output_scanline)

		C.jpeg_read_scanlines(cinfo, scanLines, 1)

		for x := 0; x < int(cinfo.output_width); x += 1 {
			off := y * img.Stride
			img.Pix[off+x] = buf[x]
		}
	}

	return img
}
Пример #3
0
func decodeRGB(cinfo *C.struct_jpeg_decompress_struct) image.Image {
	buf := make([]byte, int(cinfo.output_width*3))
	scanLine := &buf[0]
	scanLines := C.JSAMPARRAY(unsafe.Pointer(&scanLine))

	img := image.NewRGBA(image.Rect(0, 0, int(cinfo.output_width), int(cinfo.output_height)))

	for cinfo.output_scanline < cinfo.output_height {
		y := int(cinfo.output_scanline)

		C.jpeg_read_scanlines(cinfo, scanLines, 1)

		for x := 0; x < int(cinfo.output_width); x += 1 {
			off := y * img.Stride

			img.Pix[off+4*x+0] = buf[x*3+0]
			img.Pix[off+4*x+1] = buf[x*3+1]
			img.Pix[off+4*x+2] = buf[x*3+2]
			img.Pix[off+4*x+3] = 255
		}
	}

	return img
}