]> gitweb.factorcode.org Git - factor.git/blob - basis/images/loader/gdiplus/gdiplus.factor
core, basis, extra: Remove DOS line endings from files.
[factor.git] / basis / images / loader / gdiplus / gdiplus.factor
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
8
9 SPECIALIZED-ARRAY: ImageCodecInfo
10
11 SINGLETON: gdi+-image
12
13 os windows? [
14     { "png" "tif" "tiff" "gif" "jpg" "jpeg" "bmp" "ico" }
15     [ gdi+-image register-image-class ] each
16 ] when
17
18 <PRIVATE
19
20 : <GpRect> ( x y w h -- rect )
21     GpRect <struct-boa> ; inline
22
23 : stream>gdi+-bitmap ( stream -- bitmap )
24     stream>IStream &com-release
25     { void* } [ GdipCreateBitmapFromStream check-gdi+-status ]
26     with-out-parameters &GdipFree ;
27
28 : gdi+-bitmap-width ( bitmap -- w )
29     { UINT } [ GdipGetImageWidth check-gdi+-status ]
30     with-out-parameters ;
31
32 : gdi+-bitmap-height ( bitmap -- h )
33     { UINT } [ GdipGetImageHeight check-gdi+-status ]
34     with-out-parameters ;
35
36 : gdi+-lock-bitmap ( bitmap rect mode format -- data )
37     { BitmapData } [ GdipBitmapLockBits check-gdi+-status ]
38     with-out-parameters ;
39
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
47     w h pixels ;
48
49 :: data>image ( w h pixels -- image )
50     image new
51         { w h } >>dim
52         pixels >>bitmap
53         BGRA >>component-order
54         ubyte-components >>component-type
55         f >>upside-down? ;
56
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 ;
60
61 : check-pixel-format ( image -- )
62     component-order>> dup BGRA = [ drop ] [ unsupported-pixel-format ] if ;
63
64 : image>gdi+-bitmap ( image -- bitmap )
65     dup check-pixel-format
66     [ dim>> first2 ] [ rowstride PixelFormat32bppARGB ] [ bitmap>> ] tri
67     { void* } [
68         GdipCreateBitmapFromScan0 check-gdi+-status
69     ] with-out-parameters &GdipFree ;
70
71 : image-encoders-size ( -- num size )
72     { UINT UINT } [
73         GdipGetImageEncodersSize check-gdi+-status
74     ] with-out-parameters ;
75
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> ;
80
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 ;
84
85 : mime-type>clsid ( mime-type -- clsid )
86     image-encoders [ MimeType>> alien>native-string = ] with find nip Clsid>> ;
87
88 : startup-gdi+ ( -- )
89     start-gdi+ &stop-gdi+ drop ;
90
91 : write-image-to-stream ( image stream extension -- )
92     [ image>gdi+-bitmap ]
93     [ stream>IStream &com-release ]
94     [ extension>mime-type mime-type>clsid ] tri*
95     f GdipSaveImageToStream check-gdi+-status ;
96
97 PRIVATE>
98
99 M: gdi+-image stream>image*
100     drop startup-gdi+
101     stream>gdi+-bitmap
102     gdi+-bitmap>data
103     data>image ;
104
105 M: gdi+-image image>stream ( image extension class -- )
106     drop startup-gdi+ output-stream get swap write-image-to-stream ;