Source

haskell-win32 / Graphics / Win32 / GDI / Bitmap.hsc

Full commit
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
-----------------------------------------------------------------------------
-- |
-- Module      :  Graphics.Win32.GDI.Bitmap
-- Copyright   :  (c) Alastair Reid, 1997-2003
-- License     :  BSD-style (see the file libraries/base/LICENSE)
--
-- Maintainer  :  Esa Ilari Vuokko <ei@vuokko.info>
-- Stability   :  provisional
-- Portability :  portable
--
-- A collection of FFI declarations for interfacing with Win32.
--
-----------------------------------------------------------------------------

module Graphics.Win32.GDI.Bitmap(
	RasterOp3,
	RasterOp4,
	sRCCOPY,
	sRCPAINT,
	sRCAND,
	sRCINVERT,
	sRCERASE,
	nOTSRCCOPY,
	nOTSRCERASE,
	mERGECOPY,
	mERGEPAINT,
	pATCOPY,
	pATPAINT,
	pATINVERT,
	dSTINVERT,
	bLACKNESS,
	wHITENESS,

	mAKEROP4,

	BITMAP,
	LPBITMAP,
	setBITMAP,
	deleteBitmap,
	createCompatibleBitmap,
	createBitmap,
	createBitmapIndirect,
	createDIBPatternBrushPt,
	getBitmapDimensionEx,
	setBitmapDimensionEx,
	getBitmapInfo,

	BitmapCompression,
	bI_RGB,
	bI_RLE8,
	bI_RLE4,
	bI_BITFIELDS,

	ColorFormat,
	dIB_PAL_COLORS,
	dIB_RGB_COLORS,

	LPBITMAPINFO,
	BITMAPINFOHEADER,
	LPBITMAPINFOHEADER,
	getBITMAPINFOHEADER_,

	BITMAPFILEHEADER,
	LPBITMAPFILEHEADER,
	getBITMAPFILEHEADER,

	sizeofBITMAP,
	sizeofBITMAPINFO,
	sizeofBITMAPINFOHEADER,
	sizeofBITMAPFILEHEADER,
	sizeofLPBITMAPFILEHEADER,

	createBMPFile,
	cBM_INIT,
	getDIBits,
	setDIBits,
	createDIBitmap

        ) where

import System.Win32.Types
import Graphics.Win32.GDI.Types

import Control.Monad (liftM)
import Foreign
import Foreign.C

#include "Win32Aux.h"
#include <windows.h>

----------------------------------------------------------------
-- Resources
----------------------------------------------------------------

-- Yoiks - name clash
-- %dis bitmap x = ptr ({LPTSTR} x)
--
-- type Bitmap = LPCTSTR
--
-- intToBitmap :: Int -> Bitmap
-- intToBitmap i = makeIntResource (toWord i)
--
-- %fun LoadBitmap :: MbHINSTANCE -> Bitmap -> IO HBITMAP
-- %fail { res1 == 0 } { ErrorString("LoadBitmap") }
--
-- %const Bitmap
-- % [ OBM_CLOSE        = { MAKEINTRESOURCE(OBM_CLOSE)       }
-- % , OBM_UPARROW      = { MAKEINTRESOURCE(OBM_UPARROW)     }
-- % , OBM_DNARROW      = { MAKEINTRESOURCE(OBM_DNARROW)     }
-- % , OBM_RGARROW      = { MAKEINTRESOURCE(OBM_RGARROW)     }
-- % , OBM_LFARROW      = { MAKEINTRESOURCE(OBM_LFARROW)     }
-- % , OBM_REDUCE       = { MAKEINTRESOURCE(OBM_REDUCE)      }
-- % , OBM_ZOOM         = { MAKEINTRESOURCE(OBM_ZOOM)        }
-- % , OBM_RESTORE      = { MAKEINTRESOURCE(OBM_RESTORE)     }
-- % , OBM_REDUCED      = { MAKEINTRESOURCE(OBM_REDUCED)     }
-- % , OBM_ZOOMD        = { MAKEINTRESOURCE(OBM_ZOOMD)       }
-- % , OBM_RESTORED     = { MAKEINTRESOURCE(OBM_RESTORED)    }
-- % , OBM_UPARROWD     = { MAKEINTRESOURCE(OBM_UPARROWD)    }
-- % , OBM_DNARROWD     = { MAKEINTRESOURCE(OBM_DNARROWD)    }
-- % , OBM_RGARROWD     = { MAKEINTRESOURCE(OBM_RGARROWD)    }
-- % , OBM_LFARROWD     = { MAKEINTRESOURCE(OBM_LFARROWD)    }
-- % , OBM_MNARROW      = { MAKEINTRESOURCE(OBM_MNARROW)     }
-- % , OBM_COMBO        = { MAKEINTRESOURCE(OBM_COMBO)       }
-- % , OBM_UPARROWI     = { MAKEINTRESOURCE(OBM_UPARROWI)    }
-- % , OBM_DNARROWI     = { MAKEINTRESOURCE(OBM_DNARROWI)    }
-- % , OBM_RGARROWI     = { MAKEINTRESOURCE(OBM_RGARROWI)    }
-- % , OBM_LFARROWI     = { MAKEINTRESOURCE(OBM_LFARROWI)    }
-- % , OBM_OLD_CLOSE    = { MAKEINTRESOURCE(OBM_OLD_CLOSE)   }
-- % , OBM_SIZE         = { MAKEINTRESOURCE(OBM_SIZE)        }
-- % , OBM_OLD_UPARROW  = { MAKEINTRESOURCE(OBM_OLD_UPARROW) }
-- % , OBM_OLD_DNARROW  = { MAKEINTRESOURCE(OBM_OLD_DNARROW) }
-- % , OBM_OLD_RGARROW  = { MAKEINTRESOURCE(OBM_OLD_RGARROW) }
-- % , OBM_OLD_LFARROW  = { MAKEINTRESOURCE(OBM_OLD_LFARROW) }
-- % , OBM_BTSIZE       = { MAKEINTRESOURCE(OBM_BTSIZE)      }
-- % , OBM_CHECK        = { MAKEINTRESOURCE(OBM_CHECK)       }
-- % , OBM_CHECKBOXES   = { MAKEINTRESOURCE(OBM_CHECKBOXES)  }
-- % , OBM_BTNCORNERS   = { MAKEINTRESOURCE(OBM_BTNCORNERS)  }
-- % , OBM_OLD_REDUCE   = { MAKEINTRESOURCE(OBM_OLD_REDUCE)  }
-- % , OBM_OLD_ZOOM     = { MAKEINTRESOURCE(OBM_OLD_ZOOM)    }
-- % , OBM_OLD_RESTORE  = { MAKEINTRESOURCE(OBM_OLD_RESTORE) }
-- % ]

----------------------------------------------------------------
-- Raster Ops
----------------------------------------------------------------

#{enum RasterOp3,
 , sRCCOPY      = SRCCOPY
 , sRCPAINT     = SRCPAINT
 , sRCAND       = SRCAND
 , sRCINVERT    = SRCINVERT
 , sRCERASE     = SRCERASE
 , nOTSRCCOPY   = NOTSRCCOPY
 , nOTSRCERASE  = NOTSRCERASE
 , mERGECOPY    = MERGECOPY
 , mERGEPAINT   = MERGEPAINT
 , pATCOPY      = PATCOPY
 , pATPAINT     = PATPAINT
 , pATINVERT    = PATINVERT
 , dSTINVERT    = DSTINVERT
 , bLACKNESS    = BLACKNESS
 , wHITENESS    = WHITENESS
 }

----------------------------------------------------------------
-- BITMAP
----------------------------------------------------------------

type BITMAP =
  ( INT     -- bmType
  , INT     -- bmWidth
  , INT     -- bmHeight
  , INT     -- bmWidthBytes
  , WORD    -- bmPlanes
  , WORD    -- bmBitsPixel
  , LPVOID  -- bmBits
  )

peekBITMAP :: Ptr BITMAP -> IO BITMAP
peekBITMAP p = do
  ty     <- #{peek BITMAP,bmType} p
  width  <- #{peek BITMAP,bmWidth} p
  height <- #{peek BITMAP,bmHeight} p
  wbytes <- #{peek BITMAP,bmWidthBytes} p
  planes <- #{peek BITMAP,bmPlanes} p
  pixel  <- #{peek BITMAP,bmBitsPixel} p
  bits   <- #{peek BITMAP,bmBits} p
  return (ty, width, height, wbytes, planes, pixel, bits)

pokeBITMAP :: Ptr BITMAP -> BITMAP -> IO ()
pokeBITMAP p (ty, width, height, wbytes, planes, pixel, bits) = do
  #{poke BITMAP,bmType}       p ty
  #{poke BITMAP,bmWidth}      p width
  #{poke BITMAP,bmHeight}     p height
  #{poke BITMAP,bmWidthBytes} p wbytes
  #{poke BITMAP,bmPlanes}     p planes
  #{poke BITMAP,bmBitsPixel}  p pixel
  #{poke BITMAP,bmBits}       p bits

type LPBITMAP = Ptr BITMAP

setBITMAP :: LPBITMAP -> BITMAP -> IO ()
setBITMAP = pokeBITMAP

----------------------------------------------------------------
-- Misc
----------------------------------------------------------------

deleteBitmap :: HBITMAP -> IO ()
deleteBitmap bitmap =
  failIfFalse_ "DeleteBitmap" $ c_DeleteBitmap bitmap
foreign import stdcall unsafe "windows.h DeleteObject"
  c_DeleteBitmap :: HBITMAP -> IO Bool

createBitmap :: INT -> INT -> UINT -> UINT -> Maybe LPVOID -> IO HBITMAP
createBitmap w h planes bits mb_color_data =
  failIfNull "CreateBitmap" $
    c_CreateBitmap w h planes bits (maybePtr mb_color_data)
foreign import stdcall unsafe "windows.h CreateBitmap"
  c_CreateBitmap :: INT -> INT -> UINT -> UINT -> LPVOID -> IO HBITMAP

createBitmapIndirect :: LPBITMAP -> IO HBITMAP
createBitmapIndirect p_bm =
  failIfNull "CreateBitmapIndirect" $ c_CreateBitmapIndirect p_bm
foreign import stdcall unsafe "windows.h CreateBitmapIndirect"
  c_CreateBitmapIndirect :: LPBITMAP -> IO HBITMAP

createCompatibleBitmap :: HDC -> Int32 -> Int32 -> IO HBITMAP
createCompatibleBitmap dc w h =
  failIfNull "CreateCompatibleBitmap" $ c_CreateCompatibleBitmap dc w h
foreign import stdcall unsafe "windows.h CreateCompatibleBitmap"
  c_CreateCompatibleBitmap :: HDC -> Int32 -> Int32 -> IO HBITMAP

createDIBPatternBrushPt :: LPVOID -> ColorFormat -> IO HBRUSH
createDIBPatternBrushPt bm usage =
  failIfNull "CreateDIBPatternBrushPt" $ c_CreateDIBPatternBrushPt bm usage
foreign import stdcall unsafe "windows.h CreateDIBPatternBrushPt"
  c_CreateDIBPatternBrushPt :: LPVOID -> ColorFormat -> IO HBRUSH

----------------------------------------------------------------
-- Querying
----------------------------------------------------------------

getBitmapDimensionEx :: HBITMAP -> IO SIZE
getBitmapDimensionEx bm =
  allocaSIZE $ \ p_size -> do
  failIfFalse_ "GetBitmapDimensionEx" $ c_GetBitmapDimensionEx bm p_size
  peekSIZE p_size
foreign import stdcall unsafe "windows.h GetBitmapDimensionEx"
  c_GetBitmapDimensionEx :: HBITMAP -> Ptr SIZE -> IO Bool

setBitmapDimensionEx :: HBITMAP -> SIZE -> IO SIZE
setBitmapDimensionEx bm (cx,cy) =
  allocaSIZE $ \ p_size -> do
  failIfFalse_ "SetBitmapDimensionEx" $ do
    c_SetBitmapDimensionEx bm cx cy p_size
  peekSIZE p_size
foreign import stdcall unsafe "windows.h SetBitmapDimensionEx"
  c_SetBitmapDimensionEx :: HBITMAP -> LONG -> LONG -> Ptr SIZE -> IO Bool

getBitmapInfo :: HBITMAP -> IO BITMAP
getBitmapInfo bm =
  allocaBytes (fromIntegral sizeofBITMAP) $ \ p_bm -> do
  failIfFalse_ "GetBitmapInfo" $ c_GetBitmapInfo bm sizeofBITMAP p_bm
  peekBITMAP p_bm
foreign import stdcall unsafe "windows.h GetObjectW"
  c_GetBitmapInfo :: HBITMAP -> DWORD -> LPBITMAP -> IO Bool

----------------------------------------------------------------
--
----------------------------------------------------------------

type BitmapCompression = DWORD

#{enum BitmapCompression,
 , bI_RGB       = BI_RGB
 , bI_RLE8      = BI_RLE8
 , bI_RLE4      = BI_RLE4
 , bI_BITFIELDS = BI_BITFIELDS
 }

type ColorFormat = DWORD

#{enum ColorFormat,
 , dIB_PAL_COLORS = DIB_PAL_COLORS
 , dIB_RGB_COLORS = DIB_RGB_COLORS
 }

----------------------------------------------------------------
-- BITMAPINFO
----------------------------------------------------------------

type LPBITMAPINFO = Ptr ()

----------------------------------------------------------------
-- BITMAPINFOHEADER
----------------------------------------------------------------

type BITMAPINFOHEADER =
 ( DWORD              -- biSize      -- sizeof(BITMAPINFOHEADER)
 , LONG               -- biWidth
 , LONG               -- biHeight
 , WORD               -- biPlanes
 , WORD               -- biBitCount  -- 1, 4, 8, 16, 24 or 32
 , BitmapCompression  -- biCompression
 , DWORD              -- biSizeImage
 , LONG               -- biXPelsPerMeter
 , LONG               -- biYPelsPerMeter
 , Maybe DWORD        -- biClrUsed
 , Maybe DWORD        -- biClrImportant
 )

peekBITMAPINFOHEADER :: Ptr BITMAPINFOHEADER -> IO BITMAPINFOHEADER
peekBITMAPINFOHEADER p = do
  size     <- #{peek BITMAPINFOHEADER,biSize} p
  width    <- #{peek BITMAPINFOHEADER,biWidth} p
  height   <- #{peek BITMAPINFOHEADER,biHeight} p
  planes   <- #{peek BITMAPINFOHEADER,biPlanes} p
  nbits    <- #{peek BITMAPINFOHEADER,biBitCount} p
  comp     <- #{peek BITMAPINFOHEADER,biCompression} p
  imsize   <- #{peek BITMAPINFOHEADER,biSizeImage} p
  xDensity <- #{peek BITMAPINFOHEADER,biXPelsPerMeter} p
  yDensity <- #{peek BITMAPINFOHEADER,biYPelsPerMeter} p
  clrUsed  <- liftM numToMaybe $ #{peek BITMAPINFOHEADER,biClrUsed} p
  clrImp   <- liftM numToMaybe $ #{peek BITMAPINFOHEADER,biClrImportant} p
  return (size, width, height, planes, nbits, comp, imsize,
          xDensity, yDensity, clrUsed, clrImp)

type LPBITMAPINFOHEADER   = Ptr BITMAPINFOHEADER

getBITMAPINFOHEADER_ :: LPBITMAPINFOHEADER -> IO BITMAPINFOHEADER
getBITMAPINFOHEADER_ = peekBITMAPINFOHEADER

----------------------------------------------------------------
-- BITMAPFILEHEADER
----------------------------------------------------------------

type BITMAPFILEHEADER =
 ( WORD   -- bfType      -- "BM" == 0x4d42
 , DWORD  -- bfSize      -- number of bytes in file
 , WORD   -- bfReserved1 -- == 0
 , WORD   -- bfReserved2 -- == 0
 , DWORD  -- bfOffBits   -- == (char*) bits - (char*) filehdr
 )

peekBITMAPFILEHEADER :: Ptr BITMAPFILEHEADER -> IO BITMAPFILEHEADER
peekBITMAPFILEHEADER p = do
  ty     <- #{peek BITMAPFILEHEADER,bfType} p
  size   <- #{peek BITMAPFILEHEADER,bfSize} p
  res1   <- #{peek BITMAPFILEHEADER,bfReserved1} p
  res2   <- #{peek BITMAPFILEHEADER,bfReserved2} p
  offset <- #{peek BITMAPFILEHEADER,bfOffBits} p
  return (ty, size, res1, res2, offset)

type LPBITMAPFILEHEADER = Ptr BITMAPFILEHEADER

getBITMAPFILEHEADER :: LPBITMAPFILEHEADER -> IO BITMAPFILEHEADER
getBITMAPFILEHEADER = peekBITMAPFILEHEADER

sizeofBITMAP             :: Word32
sizeofBITMAP             = #{size BITMAP}
sizeofBITMAPINFO         :: Word32
sizeofBITMAPINFO         = #{size BITMAPINFO}
sizeofBITMAPINFOHEADER   :: Word32
sizeofBITMAPINFOHEADER   = #{size BITMAPINFOHEADER}
sizeofBITMAPFILEHEADER   :: Word32
sizeofBITMAPFILEHEADER   = #{size BITMAPFILEHEADER}
sizeofLPBITMAPFILEHEADER :: Word32
sizeofLPBITMAPFILEHEADER = #{size BITMAPFILEHEADER}

----------------------------------------------------------------
-- CreateBMPFile
----------------------------------------------------------------

-- A (large) helper function - courtesy of Microsoft

createBMPFile :: String -> HBITMAP -> HDC -> IO ()
createBMPFile name bm dc =
  withCWString name $ \ c_name ->
  c_CreateBMPFile c_name bm dc
foreign import ccall unsafe "dumpBMP.h CreateBMPFile"
  c_CreateBMPFile :: LPCTSTR -> HBITMAP -> HDC -> IO ()

{-# CFILES cbits/dumpBMP.c #-}

----------------------------------------------------------------
-- Device Independent Bitmaps
----------------------------------------------------------------

#{enum DWORD,
 , cBM_INIT = CBM_INIT
 }

getDIBits :: HDC -> HBITMAP -> INT -> INT -> Maybe LPVOID -> LPBITMAPINFO -> ColorFormat -> IO INT
getDIBits dc bm start nlines mb_bits info usage =
  failIfZero "GetDIBits" $
    c_GetDIBits dc bm start nlines (maybePtr mb_bits) info usage
foreign import stdcall unsafe "windows.h GetDIBits"
  c_GetDIBits :: HDC -> HBITMAP -> INT -> INT -> LPVOID -> LPBITMAPINFO -> ColorFormat -> IO INT

setDIBits :: HDC -> HBITMAP -> INT -> INT -> LPVOID -> LPBITMAPINFO -> ColorFormat -> IO INT
setDIBits dc bm start nlines bits info use =
  failIfZero "SetDIBits" $ c_SetDIBits dc bm start nlines bits info use
foreign import stdcall unsafe "windows.h SetDIBits"
  c_SetDIBits :: HDC -> HBITMAP -> INT -> INT -> LPVOID -> LPBITMAPINFO -> ColorFormat -> IO INT

createDIBitmap :: HDC -> LPBITMAPINFOHEADER -> DWORD -> LPVOID -> LPBITMAPINFO -> ColorFormat -> IO HBITMAP
createDIBitmap dc hdr option init_val info usage =
  failIfNull "CreateDIBitmap" $
    c_CreateDIBitmap dc hdr option init_val info usage
foreign import stdcall unsafe "windows.h CreateDIBitmap"
  c_CreateDIBitmap :: HDC -> LPBITMAPINFOHEADER -> DWORD -> LPVOID -> LPBITMAPINFO -> ColorFormat -> IO HBITMAP

----------------------------------------------------------------
-- End
----------------------------------------------------------------