1 ! Copyright (C) 2010 Joe Groff.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors alien alien.c-types alien.data alien.enums alien.strings
4 assocs byte-arrays classes.struct destructors grouping images images.loader
5 io kernel libc locals math mime.types namespaces sequences specialized-arrays
6 system windows.com windows.gdiplus windows.streams windows.types ;
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 ! Copy the rect to stack space because the gc might move it
38 ! because calling GdipBitmapLockBits triggers callbacks to Factor.
39 { BitmapData GpRect } [
40 :> ( stack-data stack-rect )
41 stack-rect rect binary-object memcpy
42 bitmap stack-rect mode format stack-data GdipBitmapLockBits
44 ] with-out-parameters drop ;
46 :: gdi+-bitmap>data ( bitmap -- w h pixels )
47 bitmap [ gdi+-bitmap-width ] [ gdi+-bitmap-height ] bi :> ( w h )
48 bitmap 0 0 w h <GpRect> ImageLockModeRead enum>number
49 PixelFormat32bppARGB gdi+-lock-bitmap :> bitmap-data
50 bitmap-data [ Scan0>> ] [ Stride>> ] [ Height>> * ] tri
51 memory>byte-array :> pixels
52 bitmap bitmap-data GdipBitmapUnlockBits check-gdi+-status
55 :: data>image ( w h pixels -- image )
59 BGRA >>component-order
60 ubyte-components >>component-type
63 ! Loaded images usually have the format BGRA, text rendered BGRX.
64 ERROR: unsupported-pixel-format component-order ;
66 : check-pixel-format ( component-order -- )
67 dup { BGRX BGRA } member? [ drop ] [ unsupported-pixel-format ] if ;
69 : image>gdi+-bitmap ( image -- bitmap )
70 dup component-order>> check-pixel-format
71 [ dim>> first2 ] [ rowstride PixelFormat32bppARGB ] [ bitmap>> ] tri
73 GdipCreateBitmapFromScan0 check-gdi+-status
74 ] with-out-parameters &GdipFree ;
76 : image-encoders-size ( -- num size )
78 GdipGetImageEncodersSize check-gdi+-status
79 ] with-out-parameters ;
81 : image-encoders ( -- codec-infos )
82 image-encoders-size dup <byte-array> 3dup
83 GdipGetImageEncoders check-gdi+-status
84 nip swap ImageCodecInfo <c-direct-array> ;
86 : extension>mime-type ( extension -- mime-type )
87 ! Crashes if you let this mime through on my machine.
88 dup mime-types at dup "image/bmp" = [ unknown-image-extension ] when nip ;
90 : mime-type>clsid ( mime-type -- clsid )
91 image-encoders [ MimeType>> alien>native-string = ] with find nip Clsid>> ;
94 start-gdi+ &stop-gdi+ drop ;
96 : write-image-to-stream ( image stream extension -- )
98 [ stream>IStream &com-release ]
99 [ extension>mime-type mime-type>clsid ] tri*
100 f GdipSaveImageToStream check-gdi+-status ;
104 M: gdi+-image stream>image*
110 M: gdi+-image image>stream ( image extension class -- )
111 drop startup-gdi+ output-stream get swap write-image-to-stream ;