delete m_pdib;
file: cdib.h -- cdib class header file
// cdib.h declaration for inside visual c++ cdib class
#ifndef _inside_visual_cpp_cdib
#define _inside_visual_cpp_cdib
class cdib : public cobject
{
enum alloc {noalloc, crtalloc, heapalloc};
declare_serial(cdib)
public:
lpvoid m_lpvcolortable;
hbitmap m_hbitmap;
lpbyte m_lpimage; // starting address of dib bits
lpbitmapinfoheader m_lpbmih; // buffer containing the bitmapinfoheader
private:
hglobal m_hglobal; // for external windows we need to free;
// could be allocated by this class or allocated externally
alloc m_nbmihalloc;
alloc m_nimagealloc;
dword m_dwsizeimage; // of bits -- not bitmapinfoheader or bitmapfileheader
int m_ncolortableentries;
handle m_hfile;
handle m_hmap;
lpvoid m_lpvfile;
hpalette m_hpalette;
public:
cdib();
cdib(csize size, int nbitcount); // builds bitmapinfoheader
~cdib();
int getsizeimage() {return m_dwsizeimage;}
int getsizeheader()
{return sizeof(bitmapinfoheader) + sizeof(rgbquad) * m_ncolortableentries;}
csize getdimensions();
bool attachmapfile(const char* strpathname, bool bshare = false);
bool copytomapfile(const char* strpathname);
bool attachmemory(lpvoid lpvmem, bool bmustdelete = false, hglobal hglobal = null);
bool draw(cdc* pdc, cpoint origin, csize size); // until we implemnt createdibsection
hbitmap createsection(cdc* pdc = null);
uint usepalette(cdc* pdc, bool bbackground = false);
bool makepalette();
bool setsystempalette(cdc* pdc);
bool compress(cdc* pdc, bool bcompress = true); // false means decompress
hbitmap createbitmap(cdc* pdc);
bool read(cfile* pfile);
bool readsection(cfile* pfile, cdc* pdc = null);
bool write(cfile* pfile);
void serialize(carchive& ar);
void empty();
private:
void detachmapfile();
void computepalettesize(int nbitcount);
void computemetrics();
};
#endif // _inside_visual_cpp_cdib
file: cdib.cpp -- cdib class definitions
// cdib.cpp
// new version for win32
#include "stdafx.h"
#include "cdib.h"
#ifdef _debug
#define new debug_new
#undef this_file
static char this_file[] = __file__;
#endif
implement_serial(cdib, cobject, 0);
cdib::cdib()
{
m_hfile = null;
m_hbitmap = null;
m_hpalette = null;
m_nbmihalloc = m_nimagealloc = noalloc;
empty();
}
cdib::cdib(csize size, int nbitcount)
{
m_hfile = null;
m_hbitmap = null;
m_hpalette = null;
m_nbmihalloc = m_nimagealloc = noalloc;
empty();
computepalettesize(nbitcount);
m_lpbmih = (lpbitmapinfoheader) new
char[sizeof(bitmapinfoheader) + sizeof(rgbquad) * m_ncolortableentries];
m_nbmihalloc = crtalloc;
m_lpbmih->bisize = sizeof(bitmapinfoheader);
m_lpbmih->biwidth = size.cx;
m_lpbmih->biheight = size.cy;
m_lpbmih->biplanes = 1;
m_lpbmih->bibitcount = nbitcount;
m_lpbmih->bicompression = bi_rgb;
m_lpbmih->bisizeimage = 0;
m_lpbmih->bixpelspermeter = 0;
m_lpbmih->biypelspermeter = 0;
m_lpbmih->biclrused = m_ncolortableentries;
m_lpbmih->biclrimportant = m_ncolortableentries;
computemetrics();
memset(m_lpvcolortable, 0, sizeof(rgbquad) * m_ncolortableentries);
m_lpimage = null; // no data yet
}
cdib::~cdib()
{
empty();
}
csize cdib::getdimensions()
{
if(m_lpbmih == null) return csize(0, 0);
return csize((int) m_lpbmih->biwidth, (int) m_lpbmih->biheight);
}
bool cdib::attachmapfile(const char* strpathname, bool bshare) // for reading
{
// if we open the same file twice, windows treats it as 2 separate files
// doesn't work with rare bmp files where # palette entries > biclrused
handle hfile = ::createfile(strpathname, generic_write | generic_read,
bshare ? file_share_read : 0,
null, open_existing, file_attribute_normal, null);
assert(hfile != invalid_handle_value);
dword dwfilesize = ::getfilesize(hfile, null);
handle hmap = ::createfilemapping(hfile, null, page_readwrite, 0, 0, null);
dword dwerr = ::getlasterror();
if(hmap == null) {
afxmessagebox("empty bitmap file");
return false;
}
lpvoid lpvfile = ::mapviewoffile(hmap, file_map_write, 0, 0, 0); // map whole file
assert(lpvfile != null);
if(((lpbitmapfileheader) lpvfile)->bftype != 0x4d42) {
afxmessagebox("invalid bitmap file");
detachmapfile();
return false;
}
attachmemory((lpbyte) lpvfile + sizeof(bitmapfileheader));
m_lpvfile = lpvfile;
m_hfile = hfile;
m_hmap = hmap;
return true;
}
bool cdib::copytomapfile(const char* strpathname)
{
// copies dib to a new file, releases prior pointers
// if you previously used createsection, the hbitmap will be null (and unusable)
bitmapfileheader bmfh;
bmfh.bftype = 0x4d42; // 'bm'
bmfh.bfsize = m_dwsizeimage + sizeof(bitmapinfoheader) +
sizeof(rgbquad) *