1 ! Copyright (C) 2010 Philipp Brüschweiler.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors alien.c-types alien.data arrays combinators
4 destructors gdk.pixbuf.ffi gobject.ffi grouping images
5 images.loader io kernel locals math sequences
7 FROM: system => os linux freebsd netbsd openbsd ;
9 SPECIALIZED-ARRAY: uchar
13 os { linux freebsd netbsd openbsd } member? [
14 "png" gtk-image register-image-class
15 "tif" gtk-image register-image-class
16 "tiff" gtk-image register-image-class
17 "gif" gtk-image register-image-class
18 "jpg" gtk-image register-image-class
19 "jpeg" gtk-image register-image-class
20 "bmp" gtk-image register-image-class
21 "ico" gtk-image register-image-class
26 : image-data ( GdkPixbuf -- data )
28 [ gdk_pixbuf_get_pixels ]
29 [ gdk_pixbuf_get_width ]
30 [ gdk_pixbuf_get_height ]
31 [ gdk_pixbuf_get_rowstride ]
32 [ gdk_pixbuf_get_n_channels ]
33 [ gdk_pixbuf_get_bits_per_sample ]
35 [let :> ( pixels w h rowstride channels bps )
36 bps channels * 7 + 8 /i w * :> bytes-per-row
38 bytes-per-row rowstride =
39 [ pixels h rowstride * memory>byte-array ]
41 pixels rowstride h * uchar <c-direct-array>
42 rowstride <sliced-groups>
43 [ bytes-per-row head-slice ] map concat
47 : component-type ( GdkPixbuf -- component-type )
48 gdk_pixbuf_get_bits_per_sample {
49 { 8 [ ubyte-components ] }
50 { 16 [ ushort-components ] }
51 { 32 [ uint-components ] }
54 : GdkPixbuf>image ( GdkPixbuf -- image )
57 [ [ gdk_pixbuf_get_width ] [ gdk_pixbuf_get_height ] bi 2array >>dim ]
58 [ image-data >>bitmap ]
59 [ gdk_pixbuf_get_has_alpha RGBA RGB ? >>component-order ]
60 [ component-type >>component-type ]
62 f >>premultiplied-alpha?
67 M: gtk-image stream>image
69 stream-contents data>GInputStream &g_object_unref
70 GInputStream>GdkPixbuf &g_object_unref