[clean-list] Two questions (Types and graphics)

Martin Wierich martinw@cs.kun.nl
Wed, 10 Jan 2001 11:44:18 +0100


Hi Jerzy,

Jerzy Karczmarczuk wrote:
...
> Even simpler: how to read a pixel value from a bitmap?
...
> I got several pieces of data, for example the size, say, 284*281
> of my  BMP picture. And the {#Char} BitmapContent as well. But it
> is not directly usable.

The bitmapContents field contains the bitmaps whole pixel information. It is
stored in Microsofts (?) device independent format which you have to look up.
(If  I remember it right: the {#Char} is a sequence of 3 byte chunks where every
chunk contains one pixel's RGB information. They are stored row wise and every
row has to begin on a word aligned position.)

So much about reading a pixel value from a bitmap. It is your choice if you want
to work directly with this bitmapContents or with some data structure that you
derived from this information (e.g. a two dimensional array).

...
> Better, I think that functional languages are simply
> ideal to do some serious work on texture generation.
> Mapping,
> Warping,
> Morphing, ...  -- all this is <<par excellence>> functional.

So you also need to know how to store pixels in a bitmap. At first you somehow
have to generate a {#Char} bitmapContents. (This is your problem.)

Then take a look at the OSBitmap type:

:: OSBitmap
 = { originalSize  :: !(!Int,!Int) // The size of the bitmap
   , reSize         :: !(!Int,!Int) // to store values passed to resizeBitmap
   , bitmapContents :: !{#Char} // (device independent) bitmap information
   , bitmapHandle   :: !Int // The handle to the screen bitmap (for screen)
   }

To create such a record you need to create a bitmapHandle. You can do it the
same way it is done by the function osReadBitmap:

 # (hdc, tb) = winCreateScreenHDC OSNewToolbox
 # (hbmp,tb) = winCreateBitmap w data hdc tb
 # tb        = winDestroyScreenHDC (hdc,tb)
 = (if (tb==OSDummyToolbox) True True
   , { originalSize=(w,h)
     , reSize=(w,h)
     , bitmapContents=data
     , bitmapHandle=hbmp
     }
   )

// w:width of bitmap
// h:height of bitmap
// data: the {#Char} bitmapContents that you have created yourself

Background information: Windows can transform the device independent bitmap
information into device dependent bitmap information whose handle is the
"bitmapHandle". When this once has been done rendering of that bitmap via that
handle is faster. For this transformation a handle to a "device context" is
necessary. (A device context for a screen is different from one for e.g. a
printer.)

If you dive into the C implementations (as proposed by John) you can find out
what these "win..." functions do: winCreateScreedHDC creates a device context,
winCreateBitmap applies this transformation and delivers the handle.
winDestroyScreenHDC deallocates the device context again (Take care that tb is
strictly evaluated, otherwise the deallocation will not happen)

Now you should be equipped for reading and generating bitmaps with Clean on
Windows, or did I leave any question open?

groetjes,
  Martin Wierich
  (send us your fancy mapping warping morphing graphics stuff)