コード例 #1
0
ファイル: image.go プロジェクト: jimmyfrasche/cairo
//FromImage copies an image into a surface.
//
//The created image surface will have the same size as img,
//the optimal stride for img's width, and FormatARGB32.
//
//Originally cairo_image_surface_create_for_data and
//cairo_format_stride_for_width.
func FromImage(img image.Image) (ImageSurface, error) {
	f := FormatARGB32.c()
	b := img.Bounds()
	w, h := b.Dx(), b.Dy()
	s := int(C.cairo_format_stride_for_width(f, C.int(w)))

	n := s * h
	data := (*C.uchar)(C.calloc(C.size_t(uintptr(n)), 1))
	pseudoslice := (*[1 << 30]C.uchar)(unsafe.Pointer(data))[:n:n]

	i := 0
	for y := b.Min.Y; y < b.Max.Y; y++ {
		for x := b.Min.X; x < b.Max.X; x++ {
			r, g, b, a := img.At(x, y).RGBA()
			pseudoslice[i+oA] = C.uchar(a)
			pseudoslice[i+oR] = C.uchar(r)
			pseudoslice[i+oG] = C.uchar(g)
			pseudoslice[i+oB] = C.uchar(b)
			i += 4
		}
		i += 4 * (s/4 - w)
	}

	is := C.cairo_image_surface_create_for_data(data, f, C.int(w), C.int(h), C.int(s))
	C.cairo_surface_set_user_data(is, imgKey, unsafe.Pointer(data), free)

	return newImg(is, FormatARGB32, w, h, s)
}
コード例 #2
0
ファイル: cairo.go プロジェクト: mantyr/go-cairo
func (self Format) StrideForWidth(width int) int {
	return int(C.cairo_format_stride_for_width(C.cairo_format_t(self), C.int(width)))
}