2010-07-16 17:26:48 -04:00
|
|
|
! Copyright (C) 2010 Philipp Brüschweiler.
|
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2016-11-30 12:21:42 -05:00
|
|
|
USING: accessors alien.c-types alien.data alien.syntax arrays
|
|
|
|
assocs combinators gdk.pixbuf.ffi glib.ffi gobject.ffi grouping
|
|
|
|
images images.loader io kernel locals math sequences
|
2016-11-30 05:57:14 -05:00
|
|
|
specialized-arrays system unicode ;
|
2011-11-02 12:38:03 -04:00
|
|
|
IN: images.loader.gtk
|
2010-07-16 17:26:48 -04:00
|
|
|
SPECIALIZED-ARRAY: uchar
|
|
|
|
|
|
|
|
SINGLETON: gtk-image
|
2011-09-24 22:19:34 -04:00
|
|
|
|
2016-11-30 12:21:42 -05:00
|
|
|
os linux? [
|
|
|
|
{ "png" "tif" "tiff" "gif" "jpg" "jpeg" "bmp" "ico" }
|
|
|
|
[ gtk-image register-image-class ] each
|
|
|
|
] when
|
2010-07-16 17:26:48 -04:00
|
|
|
|
|
|
|
<PRIVATE
|
|
|
|
|
|
|
|
: image-data ( GdkPixbuf -- data )
|
2010-07-17 05:47:31 -04:00
|
|
|
{
|
|
|
|
[ gdk_pixbuf_get_pixels ]
|
|
|
|
[ gdk_pixbuf_get_width ]
|
|
|
|
[ gdk_pixbuf_get_height ]
|
|
|
|
[ gdk_pixbuf_get_rowstride ]
|
|
|
|
[ gdk_pixbuf_get_n_channels ]
|
|
|
|
[ gdk_pixbuf_get_bits_per_sample ]
|
|
|
|
} cleave
|
|
|
|
[let :> ( pixels w h rowstride channels bps )
|
2010-07-16 17:26:48 -04:00
|
|
|
bps channels * 7 + 8 /i w * :> bytes-per-row
|
2010-07-17 05:47:31 -04:00
|
|
|
|
|
|
|
bytes-per-row rowstride =
|
|
|
|
[ pixels h rowstride * memory>byte-array ]
|
|
|
|
[
|
2011-09-25 14:49:27 -04:00
|
|
|
pixels rowstride h * uchar <c-direct-array>
|
2013-03-23 15:08:18 -04:00
|
|
|
rowstride <groups>
|
2010-07-17 05:47:31 -04:00
|
|
|
[ bytes-per-row head-slice ] map concat
|
|
|
|
] if
|
2010-07-16 17:26:48 -04:00
|
|
|
] ;
|
|
|
|
|
2014-03-07 17:42:51 -05:00
|
|
|
CONSTANT: bits>components {
|
|
|
|
{ 8 ubyte-components }
|
|
|
|
{ 16 ushort-components }
|
|
|
|
{ 32 uint-components }
|
|
|
|
}
|
|
|
|
|
2010-07-16 17:26:48 -04:00
|
|
|
: component-type ( GdkPixbuf -- component-type )
|
2014-03-07 17:42:51 -05:00
|
|
|
gdk_pixbuf_get_bits_per_sample bits>components at ;
|
2010-07-16 17:26:48 -04:00
|
|
|
|
|
|
|
: GdkPixbuf>image ( GdkPixbuf -- image )
|
|
|
|
[ image new ] dip
|
|
|
|
{
|
|
|
|
[ [ gdk_pixbuf_get_width ] [ gdk_pixbuf_get_height ] bi 2array >>dim ]
|
|
|
|
[ image-data >>bitmap ]
|
|
|
|
[ gdk_pixbuf_get_has_alpha RGBA RGB ? >>component-order ]
|
|
|
|
[ component-type >>component-type ]
|
|
|
|
} cleave
|
|
|
|
f >>premultiplied-alpha?
|
|
|
|
f >>upside-down? ;
|
|
|
|
|
2014-03-07 17:42:51 -05:00
|
|
|
: image>GdkPixbuf ( image -- GdkPixbuf )
|
|
|
|
{
|
|
|
|
[ bitmap>> ]
|
|
|
|
[ drop GDK_COLORSPACE_RGB ]
|
|
|
|
[ has-alpha? ]
|
2014-10-10 05:14:57 -04:00
|
|
|
[ component-type>> bytes-per-component 8 * ]
|
2014-03-07 17:42:51 -05:00
|
|
|
[ dim>> first2 ]
|
|
|
|
[ rowstride ]
|
|
|
|
} cleave f f gdk_pixbuf_new_from_data ;
|
|
|
|
|
|
|
|
: GdkPixbuf>byte-array ( GdkPixbuf type -- byte-array )
|
|
|
|
{ void* int } [
|
|
|
|
rot f f
|
|
|
|
{ { pointer: GError initial: f } } [
|
|
|
|
gdk_pixbuf_save_to_bufferv drop
|
|
|
|
] with-out-parameters
|
|
|
|
] with-out-parameters rot handle-GError memory>byte-array ;
|
|
|
|
|
|
|
|
! The type parameter is almost always the same as the file extension,
|
|
|
|
! except for in the jpg -> jpeg and tif -> tiff cases.
|
|
|
|
: extension>pixbuf-type ( extension -- type )
|
|
|
|
>lower { { "jpg" "jpeg" } { "tif" "tiff" } } ?at drop ;
|
|
|
|
|
|
|
|
: write-image ( image extension -- )
|
|
|
|
[ image>GdkPixbuf &g_object_unref ] [ extension>pixbuf-type ] bi*
|
|
|
|
GdkPixbuf>byte-array write ;
|
|
|
|
|
2010-07-16 17:26:48 -04:00
|
|
|
PRIVATE>
|
|
|
|
|
2012-08-25 14:44:40 -04:00
|
|
|
M: gtk-image stream>image*
|
|
|
|
drop
|
|
|
|
stream-contents data>GInputStream &g_object_unref
|
|
|
|
GInputStream>GdkPixbuf &g_object_unref
|
|
|
|
GdkPixbuf>image ;
|
2014-03-07 17:42:51 -05:00
|
|
|
|
|
|
|
M: gtk-image image>stream
|
|
|
|
drop write-image ;
|