dflplot 0.01

dsimcha dsimcha at yahoo.com
Sat Jul 10 06:43:59 PDT 2010


== Quote from Michal Minich (michal.minich at gmail.com)'s article
> On Sat, 10 Jul 2010 04:40:04 +0000, dsimcha wrote:
> > 2. There is currently no "proper" way to save a plot. This is because
> > DFL's Bitmap object doesn't provide a way to obtain the underlying
> > pixels yet, and core.stdc.windows doesn't seem to provide the necessary
> > stuff to do it manually via the Windows API.
> When using classical windows api, I'm almost entirely sure that only way
> to have accessible bitmap pixels, while still being able to draw them to
> window is using dib section.
> HANDLE cDC;
> HBITMAP hBitmap;
> BITMAPINFO bmi;
> bmi.bmiHeader.biSize = BITMAPINFOHEADER.sizeof;
> bmi.bmiHeader.biWidth = w; // must  be rounded to 4
> bmi.bmiHeader.biHeight = -h; // must  be rounded to 4
> bmi.bmiHeader.biPlanes = 1;
> bmi.bmiHeader.biBitCount = 24;
> bmi.bmiHeader.biCompression = BI_RGB;
> VOID* ppvBits;
> HANDLE cDC = CreateCompatibleDC (hDC);
> hBitmap = CreateDIBSection (cDC, &bmi, DIB_RGB_COLORS, &ppvBits, null, 0);
> if (hBitmap == NULL || ppvBits == NULL)
>     throw new Exception ("Failed to Create Device Independed Bitmap
> (DIB)");
> HGDIOBJ selectObj = SelectObject (cDC, hBitmap);
> if (selectObj == NULL)
>     throw new Exception ("Failed to SelectObject the DIB");
> ColorBgr* pixRgb = cast (ColorBgr*) ppvBits; // ppvBits are initialized
> bitmap data
> //struct ColorBgr { ubyte blue; ubyte green; ubyte red; }
> // to display
> BITMAPINFO bi;
> bi.bmiHeader.biSize = BITMAPINFO.sizeof;
> bi.bmiHeader.biWidth = image.size.width;
> bi.bmiHeader.biHeight = 0 - image.size.height;
> bi.bmiHeader.biPlanes = 1;
> bi.bmiHeader.biBitCount = 24;
> SetDIBitsToDevice (hDC,
>       0, 0,
>       image.size.width, image.size.height,
>       0, image.size.height,
>       image.size.height, image.size.height,
>       &image.pixels[0], &bi, DIB_RGB_COLORS);

Right.  DFL can give access to the underlying handle for its Bitmap objects (which
are basically wrappers around the awful Win32 API).  I have never used the
Win32API directly before and tried to paste together some code from MSDN to use
GetDIBits.  It didn't work because not all the necessary stuff is declared in
core.stdc.windows, so I gave up for now and decided to hope either someone submits
a patch to me, or the DFL maintainers wrap GetDIBits in their Bitmap object.

In general, I want to eventually make this lib cross-platform (when gtkD matures,
maybe) so I don't want to rely too directly on native OS APIs anyhow.


More information about the Digitalmars-d-announce mailing list