1 ! (c)2010 Joe Groff bsd license
2 USING: accessors alien alien.c-types alien.data alien.enums alien.strings
3 assocs byte-arrays classes.struct destructors grouping images images.loader
4 io kernel locals math mime.types namespaces sequences specialized-arrays
5 windows.com windows.gdiplus windows.streams windows.types ;
6 FROM: system => os windows? ;
7 IN: images.loader.gdiplus
9 SPECIALIZED-ARRAY: ImageCodecInfo
14 { "png" "tif" "tiff" "gif" "jpg" "jpeg" "bmp" "ico" }
15 [ gdi+-image register-image-class ] each
20 : <GpRect> ( x y w h -- rect )
21 GpRect <struct-boa> ; inline
23 : stream>gdi+-bitmap ( stream -- bitmap )
24 stream>IStream &com-release
25 { void* } [ GdipCreateBitmapFromStream check-gdi+-status ]
26 with-out-parameters &GdipFree ;
28 : gdi+-bitmap-width ( bitmap -- w )
29 { UINT } [ GdipGetImageWidth check-gdi+-status ]
32 : gdi+-bitmap-height ( bitmap -- h )
33 { UINT } [ GdipGetImageHeight check-gdi+-status ]
36 : gdi+-lock-bitmap ( bitmap rect mode format -- data )
37 { BitmapData } [ GdipBitmapLockBits check-gdi+-status ]
40 :: gdi+-bitmap>data ( bitmap -- w h pixels )
41 bitmap [ gdi+-bitmap-width ] [ gdi+-bitmap-height ] bi :> ( w h )
42 bitmap 0 0 w h <GpRect> ImageLockModeRead enum>number
43 PixelFormat32bppARGB gdi+-lock-bitmap :> bitmap-data
44 bitmap-data [ Scan0>> ] [ Stride>> ] [ Height>> * ] tri
45 memory>byte-array :> pixels
46 bitmap bitmap-data GdipBitmapUnlockBits check-gdi+-status
49 :: data>image ( w h pixels -- image )
53 BGRA >>component-order
54 ubyte-components >>component-type
57 ! Only one pixel format supported, but I can't find images in the
58 ! wild, loaded using gdi+, in which the format is different.
59 ERROR: unsupported-pixel-format component-order ;
61 : check-pixel-format ( image -- )
62 component-order>> dup BGRA = [ drop ] [ unsupported-pixel-format ] if ;
64 : image>gdi+-bitmap ( image -- bitmap )
65 dup check-pixel-format
66 [ dim>> first2 ] [ rowstride PixelFormat32bppARGB ] [ bitmap>> ] tri
68 GdipCreateBitmapFromScan0 check-gdi+-status
69 ] with-out-parameters &GdipFree ;
71 : image-encoders-size ( -- num size )
73 GdipGetImageEncodersSize check-gdi+-status
74 ] with-out-parameters ;
76 : image-encoders ( -- codec-infos )
77 image-encoders-size dup <byte-array> 3dup
78 GdipGetImageEncoders check-gdi+-status
79 nip swap ImageCodecInfo <c-direct-array> ;
81 : extension>mime-type ( extension -- mime-type )
82 ! Crashes if you let this mime through on my machine.
83 dup mime-types at dup "image/bmp" = [ unknown-image-extension ] when nip ;
85 : mime-type>clsid ( mime-type -- clsid )
86 image-encoders [ MimeType>> alien>native-string = ] with find nip Clsid>> ;
89 start-gdi+ &stop-gdi+ drop ;
91 : write-image-to-stream ( image stream extension -- )
93 [ stream>IStream &com-release ]
94 [ extension>mime-type mime-type>clsid ] tri*
95 f GdipSaveImageToStream check-gdi+-status ;
99 M: gdi+-image stream>image*
105 M: gdi+-image image>stream ( image extension class -- )
106 drop startup-gdi+ output-stream get swap write-image-to-stream ;