root/experimental/NT-based/NTSpecific/Load n Save BMPs.hpp

/* [<][>][^][v][top][bottom][index][help] */

INCLUDED FROM


DEFINITIONS

This source file includes following definitions.
  1. Info
  2. Info
  3. Header
  4. Header
  5. dc
  6. ColSize
  7. InitHeader
  8. InitHeader
  9. CalcSize

   1 /******************************************************************************
   2 ** $Header: svn+ssh://jmmcg@svn.code.sf.net/p/libjmmcg/code/trunk/libjmmcg/experimental/NT-based/NTSpecific/Load%20n%20Save%20BMPs.hpp 2055 2017-05-13 19:35:47Z jmmcg $
   3 ** 
   4 ** Copyright © 2002 by J.M.McGuiness, coder@hussar.me.uk
   5 **
   6 ** This library is free software; you can redistribute it and/or
   7 ** modify it under the terms of the GNU Lesser General Public
   8 ** License as published by the Free Software Foundation; either
   9 ** version 2.1 of the License, or (at your option) any later version.
  10 **
  11 ** This library is distributed in the hope that it will be useful,
  12 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  13 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14 ** Lesser General Public License for more details.
  15 **
  16 ** You should have received a copy of the GNU Lesser General Public
  17 ** License along with this library; if not, write to the Free Software
  18 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19 */
  20 // DESCRIPTION:
  21 // This bit of code was copied from an article called:
  22 // "Bitmap & Palette - Drawing a bitmap from a BMP file" by
  23 // Zafir Anjum (05/97). I found it at <a href="http://www.dsp.net/zafir/bitmap/draw_bmp.html"/>
  24 //
  25 // LoadBMP              - Loads a BMP file and creates a logical palette for it.
  26 // Returns              - TRUE for success
  27 // BMPFile              - Full path of the BMP file
  28 // hDIB                 - Pointer to a HGLOBAL variable to hold the loaded bitmap
  29 //                        Memory is allocated by this function but should be 
  30 //                        released by the caller.
  31 // Pal                  - Will hold the logical palette
  32 //
  33 
  34 #pragma once
  35 
  36 #include "../../../core/ttypes.hpp"
  37 
  38 #include <boost/shared_ptr.hpp>
  39 
  40 namespace jmmcg { namespace NTUtils {
  41 
  42         class BitMapInfoWrapper {
  43         public:
  44                 explicit inline __stdcall BitMapInfoWrapper(const unsigned long size =0);
  45                 inline __stdcall BitMapInfoWrapper(const unsigned int width,const unsigned int height,const unsigned short colour_depth);
  46                 inline __stdcall BitMapInfoWrapper(const HBITMAP hbitmap,const unsigned short colour_depth);
  47                 inline __stdcall BitMapInfoWrapper(BitMapInfoWrapper const &bmi) noexcept(true)
  48                 : buff(bmi.buff) {
  49                 }
  50                 inline __stdcall ~BitMapInfoWrapper(void) noexcept(true) {
  51                 }
  52 
  53                 inline BitMapInfoWrapper & __fastcall operator=(BitMapInfoWrapper &bmi) noexcept(true) {
  54                         buff=bmi.buff;
  55                         return *this;
  56                 }
  57 
  58                 inline const BITMAPINFO * __fastcall Info(void) const noexcept(true) {
  59                         return reinterpret_cast<BITMAPINFO *>(buff.get());
  60                 }
  61                 inline BITMAPINFO * __fastcall Info(void) noexcept(true) {
  62                         return reinterpret_cast<BITMAPINFO *>(buff.get());
  63                 }
  64                 inline const BITMAPINFOHEADER * __fastcall Header(void) const noexcept(true) {
  65                         return reinterpret_cast<BITMAPINFOHEADER *>(buff.get());
  66                 }
  67                 inline BITMAPINFOHEADER * __fastcall Header(void) noexcept(true) {
  68                         return reinterpret_cast<BITMAPINFOHEADER *>(buff.get());
  69                 }
  70 
  71                 static inline DWORD __fastcall ColSize(const unsigned short colour_depth) noexcept(true);
  72 
  73         private:
  74                 class DCWrapper {
  75                 public:
  76                         explicit inline __stdcall DCWrapper(void) noexcept(true)
  77                         : dc_(CreateCompatibleDC(NULL)) {
  78                         }
  79                         inline __stdcall ~DCWrapper(void) noexcept(true) {
  80                                 DeleteDC(dc_);
  81                         }
  82 
  83                         inline HDC __fastcall dc(void) noexcept(true) {
  84                                 return dc_;
  85                         }
  86 
  87                 private:
  88                         HDC dc_;
  89 
  90                         inline __stdcall DCWrapper(const DCWrapper &) noexcept(true);
  91                         inline DCWrapper & __fastcall operator=(const DCWrapper &) noexcept(true);
  92                 };
  93 
  94                 boost::shared_ptr<char> buff;
  95 
  96                 inline void __fastcall InitHeader(void) noexcept(true);
  97                 inline void __fastcall InitHeader(const unsigned int width,const unsigned int height,const unsigned short colour_depth) noexcept(true);
  98 
  99                 static inline DWORD __fastcall CalcSize(const unsigned int width,const unsigned int height,const unsigned short colour_depth) noexcept(true);
 100         };
 101 
 102         AFX_EXT_CLASS bool __fastcall LoadBMP(const jmmcg::tstring &BMPFile,BitMapInfoWrapper &hDIB,std::auto_ptr<CPalette> &);
 103         AFX_EXT_CLASS bool __fastcall LoadBMP(CFile &,BitMapInfoWrapper &hDIB,std::auto_ptr<CPalette> &);
 104         AFX_EXT_CLASS BitMapInfoWrapper __fastcall CreateBitmapInfoStruct(const CBitmap &);
 105         AFX_EXT_CLASS bool __fastcall CreateBMPFile(const jmmcg::tstring &,const CBitmap &,const CDC &);
 106         AFX_EXT_CLASS bool __fastcall CreateBMPFile(CFile &,const CBitmap &,const CDC &);
 107 
 108         inline __stdcall
 109         BitMapInfoWrapper::BitMapInfoWrapper(const unsigned long size)
 110         : buff(new char[sizeof(BITMAPINFOHEADER)+size]) {
 111                 InitHeader();
 112         }
 113 
 114         inline __stdcall
 115         BitMapInfoWrapper::BitMapInfoWrapper(const unsigned int width,const unsigned int height,const unsigned short colour_depth)
 116         : buff(new char[CalcSize(width,height,colour_depth)]) {
 117                 InitHeader(width,height,colour_depth);
 118         }
 119 
 120         inline __stdcall
 121         BitMapInfoWrapper::BitMapInfoWrapper(const HBITMAP hbitmap,const unsigned short colour_depth) {
 122                 BITMAP bitmap;
 123                 GetObject(hbitmap,sizeof(BITMAP),&bitmap);
 124                 buff=std::auto_ptr<char>(new char[CalcSize(bitmap.bmWidth,bitmap.bmHeight,colour_depth)]);
 125                 InitHeader(bitmap.bmWidth,bitmap.bmHeight,colour_depth);
 126                 // Get the bits from the bitmap and stuff them after the LPBI
 127                 BYTE * const lpBits=reinterpret_cast<BYTE * const>(reinterpret_cast<BITMAPINFO *>(buff.get())->bmiColors)+ColSize(colour_depth);
 128                 DCWrapper temp_dc;
 129                 GetDIBits(temp_dc.dc(),hbitmap,0,bitmap.bmHeight,lpBits,reinterpret_cast<BITMAPINFO *>(buff.get()),DIB_RGB_COLORS);
 130                 // Fix this if GetDIBits messed it up....
 131                 reinterpret_cast<BITMAPINFOHEADER *>(buff.get())->biClrUsed=ColSize(colour_depth)/sizeof(RGBQUAD);
 132         }
 133 
 134         inline DWORD __fastcall
 135         BitMapInfoWrapper::ColSize(const unsigned short colour_depth) {
 136                 return sizeof(RGBQUAD)*((colour_depth<= 8) ? 1<<colour_depth : 0);
 137         }
 138 
 139         inline void __fastcall
 140         BitMapInfoWrapper::InitHeader(void) {
 141                 assert(buff.get());
 142                 reinterpret_cast<BITMAPINFOHEADER *>(buff.get())->biSize=sizeof(BITMAPINFOHEADER);
 143                 /* If the bitmap is not compressed, set the BI_RGB flag. */
 144                 reinterpret_cast<BITMAPINFOHEADER *>(buff.get())->biCompression=BI_RGB;
 145                 reinterpret_cast<BITMAPINFOHEADER *>(buff.get())->biXPelsPerMeter=reinterpret_cast<BITMAPINFOHEADER *>(buff.get())->biYPelsPerMeter=0;
 146                 /* 
 147                 * Set biClrImportant to 0, indicating that all of the 
 148                 * device colors are important.
 149                 */  
 150                 reinterpret_cast<BITMAPINFOHEADER *>(buff.get())->biClrImportant=0;
 151         }
 152 
 153         inline void __fastcall
 154         BitMapInfoWrapper::InitHeader(const unsigned int width,const unsigned int height,const unsigned short colour_depth) {
 155                 InitHeader();
 156                 reinterpret_cast<BITMAPINFOHEADER *>(buff.get())->biWidth=width;
 157                 reinterpret_cast<BITMAPINFOHEADER *>(buff.get())->biHeight=height;
 158                 reinterpret_cast<BITMAPINFOHEADER *>(buff.get())->biPlanes=1;
 159                 reinterpret_cast<BITMAPINFOHEADER *>(buff.get())->biBitCount=colour_depth;
 160                 reinterpret_cast<BITMAPINFOHEADER *>(buff.get())->biSizeImage=CalcSize(width,height,colour_depth)-sizeof(BITMAPINFOHEADER)-ColSize(colour_depth);
 161                 reinterpret_cast<BITMAPINFOHEADER *>(buff.get())->biClrUsed=ColSize(colour_depth)/sizeof(RGBQUAD);
 162         }
 163 
 164         inline DWORD __fastcall
 165         BitMapInfoWrapper::CalcSize(const unsigned int width,const unsigned int height,const unsigned short colour_depth) {
 166                 //
 167                 // DWORD align the width of the DIB
 168                 // Figure out the size of the colour table
 169                 // Calculate the size of the DIB
 170                 //
 171                 const UINT LineLen = (width*colour_depth+31)/32 * 4;
 172                 return sizeof(BITMAPINFOHEADER)
 173                         +ColSize(colour_depth)
 174                         +static_cast<DWORD>(LineLen)*static_cast<DWORD>(height);
 175         }
 176 
 177 } }

/* [<][>][^][v][top][bottom][index][help] */