diff --git a/include/wx/msw/dib.h b/include/wx/msw/dib.h index dd68491a02..9cee80d22f 100644 --- a/include/wx/msw/dib.h +++ b/include/wx/msw/dib.h @@ -1,27 +1,159 @@ ///////////////////////////////////////////////////////////////////////////// // Name: wx/msw/dib.h -// Purpose: Routines for loading and saving DIBs -// Author: Various +// Purpose: wxDIB class representing Win32 device independent bitmaps +// Author: Vadim Zeitlin // Modified by: -// Created: 01/02/97 +// Created: 03.03.03 (replaces the old file with the same name) // RCS-ID: $Id$ -// Copyright: (c) Julian Smart +// Copyright: (c) 1997-2003 wxWindows team // Licence: wxWindows licence ///////////////////////////////////////////////////////////////////////////// -#ifndef _WX_DIB_H_ -#define _WX_DIB_H_ +#ifndef _WX_MSW_DIB_H_ +#define _WX_MSW_DIB_H_ class WXDLLEXPORT wxBitmap; class WXDLLEXPORT wxPalette; -// WARNING: these functions are private to wxWindows and shouldn't be used -// by the user code, they risk to disappear in the next versions! +#include "wx/msw/private.h" + +// ---------------------------------------------------------------------------- +// wxDIB: represents a DIB section +// ---------------------------------------------------------------------------- + +class WXDLLEXPORT wxDIB +{ +public: + // ctors and such + // -------------- + + // create an uninitialized DIB with the given width, height and depth (only + // 24 and 32 bpp DIBs are currently supported) + // + // after using this ctor, GetData() and GetHandle() may be used if IsOk() + // returns true + wxDIB(int width, int height, int depth) + { Init(); (void)Create(width, height, depth); } + + // same as ctor but with return value + bool Create(int width, int height, int depth); + + // dtor is not virtual, this class is not meant to be used polymorphically + ~wxDIB() + { + if ( m_handle && !::DeleteObject(m_handle) ) + { + wxLogLastError(wxT("DeleteObject(hDIB)")); + } + } + + + // operations + // ---------- + + // get the handle from the DIB and reset it, i.e. this object won't destroy + // the DIB after this (but the caller should do it) + HBITMAP Detach() { HBITMAP hbmp = m_handle; m_handle = 0; return hbmp; } + + + // accessors + // --------- + + // return true if DIB was successfully created, false otherwise + bool IsOk() const { return m_handle != 0; } + + // get the bitmap size + wxSize GetSize() const { DoGetObject(); return wxSize(m_width, m_height); } + int GetWidth() const { DoGetObject(); return m_width; } + int GetHeight() const { DoGetObject(); return m_height; } + + // get the number of bits per pixel, or depth + int GetDepth() const { DoGetObject(); return m_depth; } + + // get the DIB handle + HBITMAP GetHandle() const { return m_handle; } + + // get raw pointer to bitmap bits, you should know what you do if you + // decide to use it + void *GetData() const { DoGetObject(); return m_data; } + + + // wxImage conversion + // ------------------ + +#if wxUSE_IMAGE + // create a DIB from the given image, the DIB will be either 24 or 32 (if + // the image has alpha channel) bpp + wxDIB(const wxImage& image) { Init(); (void)Create(image); } + + // same as the above ctor but with the return code + bool Create(const wxImage& image); + + // create wxImage having the same data as this DIB + wxImage ConvertToImage() const; +#endif // wxUSE_IMAGE + + + // helper functions + // ---------------- + + // return the size of one line in a DIB with given width and depth: the + // point here is that as the scan lines need to be DWORD aligned so we may + // need to add some padding + static unsigned long GetLineSize(int width, int depth) + { + return ((width*depth + 31) & ~31) >> 3; + } + +private: + // common part of all ctors + void Init() + { + m_handle = 0; + + m_data = NULL; + + m_width = + m_height = + m_depth = 0; + } + + // the DIB section handle, 0 if invalid + HBITMAP m_handle; + + // NB: we could store only m_handle and not any of the other fields as + // we may always retrieve them from it using ::GetObject(), but we + // decide to still store them for efficiency concerns -- however if we + // don't have them from the very beginning (e.g. DIB constructed from a + // bitmap), we only retrieve them when necessary and so these fields + // should *never* be accessed directly, even from inside wxDIB code + + // function which must be called before accessing any members and which + // gets their values from m_handle, if not done yet + void DoGetObject() const; + + // pointer to DIB bits, may be NULL + void *m_data; + + // size and depth of the image + int m_width, + m_height, + m_depth; + + + // DIBs can't be copied + wxDIB(const wxDIB&); + wxDIB& operator=(const wxDIB&); +}; + // ---------------------------------------------------------------------------- // Functions for working with DIBs // ---------------------------------------------------------------------------- +// WARNING: these functions are private to wxWindows and shouldn't be used +// by the user code, they risk to disappear in the next versions! + // VZ: we have 3 different sets of functions: from bitmap.cpp (wxCreateDIB and // wxFreeDIB), from dib.cpp and from dataobj.cpp - surely there is some // redundancy between them? (FIXME) @@ -52,5 +184,5 @@ HANDLE wxReadDIB2(LPTSTR lpFileName); LPSTR wxFindDIBBits (LPSTR lpbi); HPALETTE wxMakeDIBPalette(LPBITMAPINFOHEADER lpInfo); -#endif // _WX_DIB_H_ +#endif // _WX_MSW_DIB_H_ diff --git a/src/msw/dib.cpp b/src/msw/dib.cpp index 2eac920130..632e70c552 100644 --- a/src/msw/dib.cpp +++ b/src/msw/dib.cpp @@ -1,3 +1,190 @@ +/////////////////////////////////////////////////////////////////////////////// +// Name: src/msw/dib.cpp +// Purpose: implements wxDIB class +// Author: Vadim Zeitlin +// Modified by: +// Created: 03.03.03 (replaces the old file with the same name) +// RCS-ID: $Id$ +// Copyright: (c) 2003 Vadim Zeitlin +// License: wxWindows license +/////////////////////////////////////////////////////////////////////////////// + +// ============================================================================ +// declarations +// ============================================================================ + +// ---------------------------------------------------------------------------- +// headers +// ---------------------------------------------------------------------------- + +// For compilers that support precompilation, includes "wx.h". +#include "wx/wxprec.h" + +#ifdef __BORLANDC__ + #pragma hdrstop +#endif + +#ifndef WX_PRECOMP + #include "wx/string.h" + #include "wx/log.h" +#endif //WX_PRECOMP + +#include "wx/image.h" + +#include "wx/msw/dib.h" + +// ============================================================================ +// implementation +// ============================================================================ + +// ---------------------------------------------------------------------------- +// wxDIB creation +// ---------------------------------------------------------------------------- + +bool wxDIB::Create(int width, int height, int depth) +{ + // we don't handle the palette yet + wxASSERT_MSG( depth == 24 || depth == 32, + _T("unsupported image depth in wxDIB::Create()") ); + + static const int infosize = sizeof(BITMAPINFOHEADER); + + BITMAPINFO *info = (BITMAPINFO *)malloc(infosize); + wxCHECK_MSG( info, NULL, _T("malloc(BITMAPINFO) failed") ); + + memset(info, 0, infosize); + + info->bmiHeader.biSize = infosize; + info->bmiHeader.biWidth = width; + info->bmiHeader.biHeight = -height; + info->bmiHeader.biPlanes = 1; + info->bmiHeader.biBitCount = depth; + info->bmiHeader.biCompression = BI_RGB; + info->bmiHeader.biSizeImage = GetLineSize(width, depth)*height; + + // No need to report an error here. If it fails, we just won't use a + // file mapping and CreateDIBSection will just allocate memory for us. + m_handle = ::CreateDIBSection + ( + 0, // hdc (unused with DIB_RGB_COLORS) + info, // bitmap description + DIB_RGB_COLORS, // use RGB, not palette + &m_data, // [out] DIB bits + NULL, // don't use file mapping + 0 // file mapping offset (not used here) + ); + + free(info); + + if ( !m_handle ) + { + wxLogLastError(wxT("CreateDIBSection")); + + return false; + } + + m_width = width; + m_height = height; + m_depth = depth; + + return true; +} + +// ---------------------------------------------------------------------------- +// wxDIB accessors +// ---------------------------------------------------------------------------- + +void wxDIB::DoGetObject() const +{ + // only do something if we have a valid DIB but we don't [yet] have valid + // data + if ( m_handle && !m_data ) + { + // although all the info we need is in BITMAP and so we don't really + // need DIBSECTION we still ask for it as modifying the bit values only + // works for the real DIBs and not for the bitmaps and it's better to + // check for this now rather than trying to find out why it doesn't + // work later + DIBSECTION ds; + if ( !::GetObject(m_handle, sizeof(ds), &ds) ) + { + wxLogLastError(_T("GetObject(hDIB)")); + + return; + } + + wxDIB *self = wxConstCast(this, wxDIB); + + self->m_width = ds.dsBm.bmWidth; + self->m_height = ds.dsBm.bmHeight; + self->m_depth = ds.dsBm.bmBitsPixel; + self->m_data = ds.dsBm.bmBits; + } +} + +// ---------------------------------------------------------------------------- +// wxImage support +// ---------------------------------------------------------------------------- + +#if wxUSE_IMAGE + +bool wxDIB::Create(const wxImage& image) +{ + wxCHECK_MSG( image.Ok(), false, _T("invalid wxImage in wxDIB ctor") ); + + const int h = image.GetHeight(); + const int w = image.GetWidth(); + + // if we have alpha channel, we need to create a 32bpp RGBA DIB, otherwise + // a 24bpp RGB is sufficient + const bool hasAlpha = image.HasAlpha(); + const int bpp = hasAlpha ? 32 : 24; + + if ( !Create(w, h, bpp) ) + return false; + + // DIBs are stored in bottom to top order so we need to copy bits line by + // line and starting from the end + const int srcBytesPerLine = w * 3; + const int dstBytesPerLine = GetLineSize(w, bpp); + const unsigned char *src = image.GetData() + ((h - 1) * srcBytesPerLine); + const unsigned char *alpha = hasAlpha ? image.GetAlpha() + (h - 1)*w : NULL; + unsigned char *dstLineStart = (unsigned char *)m_data; + for ( int y = 0; y < h; y++ ) + { + // copy one DIB line + unsigned char *dst = dstLineStart; + for ( int x = 0; x < w; x++ ) + { + // also, the order of RGB is inversed for DIBs + *dst++ = src[2]; + *dst++ = src[1]; + *dst++ = src[0]; + + src += 3; + + if ( alpha ) + *dst++ = *alpha++; + } + + // pass to the previous line in the image + src -= 2*srcBytesPerLine; + if ( alpha ) + alpha -= 2*w; + + // and to the next one in the DIB + dstLineStart += dstBytesPerLine; + } + + return true; +} + +#endif // wxUSE_IMAGE + +// ============================================================================ +// old DIB code, to be integrated in wxDIB class +// ============================================================================ + /******************************************************************************* * * * MODULE : DIB.CC *