Advertisement
Guest User

Disch

a guest
Jan 23rd, 2010
713
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.34 KB | None | 0 0
  1. class Bitmap32
  2. {
  3. public:
  4.     // typedef a pixel type.  Since this is 32-bit, we'll want a 32bit data type
  5.     //   on Windows, we can use DWORD
  6.     typedef DWORD       pixel_t;
  7.    
  8. private:
  9.     // data members:
  10.     HBITMAP   bmp;      // the actual bitmap
  11.     HDC       dc;       // the host DC
  12.     HBITMAP   bmpold;   // the host's original bitmap (for cleanup purposes)
  13.    
  14.     pixel_t*  pix;      // pointer to the pixel data
  15.    
  16.     int       pitch;    // pitch of the image (in pixel_t's, not in bytes!
  17.                         //    so technically it's not the pitch)
  18.                        
  19.     int       width;    // width of the bitmap
  20.     int       height;   // height of the bitmap
  21.    
  22. public:
  23.     //========================================
  24.     //  Constructors
  25.     Bitmap32()        // default ctor -- no image
  26.     {
  27.         Zero();
  28.     }
  29.    
  30.     Bitmap32(int wd,int ht)     // creates a wd*ht bitmap
  31.     {
  32.         Zero();
  33.         Create(wd,ht);
  34.     }
  35.    
  36.     //========================================
  37.     //  Destructor
  38.     ~Bitmap32()
  39.     {
  40.         Destroy();
  41.     }
  42.    
  43.     //========================================
  44.     //  Resizing the bitmap
  45.     //   note that doing this will destroy the contents of the bitmap
  46.     void SetSize(int wd,int ht)
  47.     {
  48.         Destroy();
  49.         Create(wd,ht);
  50.     }
  51.    
  52.     //========================================
  53.     //  Getting information about the bitmap
  54.     inline int GetWidth() const         { return width;     }
  55.     inline int GetHeight() const        { return height;    }
  56.     inline int GetPitch() const         { return pitch;     }
  57.     inline bool IsOk() const            { return pix != 0;  }   // returns true if
  58.                                                                 //  the bitmap is ok for drawing
  59.  
  60.     //========================================
  61.     //  Getting the DC for drawing
  62.     //      (I don't know if const correctness works here -- not sure how
  63.     //  HDC is defined)
  64.     inline const HDC GetDC() const      { return dc;        }
  65.     inline HDC GetDC()                  { return dc;        }
  66.    
  67.     //========================================
  68.     //  Pixel level access - getting setting individual pixels
  69.     //    Note there's no bounds checking here for speed purposes
  70.     //  so be sure to check that yourself
  71.     inline pixel_t operator () (int x,int y) const  { return pix[ (y*pitch) + x ];  }
  72.     inline pixel_t& operator () (int x,int y)       { return pix[ (y*pitch) + x ];  }
  73.    
  74.     //=========================================
  75.     //  Getting the pixel buffer
  76.     //    Alternative to the above operator () approach.  Potentially faster for
  77.     //  drawing large groups of pixels since you don't have to do (y*pitch) for every
  78.     //  access.  Although easier to misuse and therefore less safe.  Pick your poison.
  79.     inline const pixel_t* GetPixels() const { return pix; }
  80.     inline pixel_t* GetPixels()             { return pix; }
  81.    
  82.  
  83. private:
  84.     //=========================================
  85.     //=========================================
  86.     //  The "inner workings"
  87.    
  88.     void Zero()
  89.     {
  90.         bmp =       0;
  91.         dc =        0;
  92.         bmpold =    0;
  93.         pix =       0;
  94.         pitch =     0;
  95.         width =     0;
  96.         height =    0;
  97.     }
  98.    
  99.     //=========================================
  100.     void Destroy()
  101.     {
  102.         if(!IsOk())
  103.             return;     // nothing to destroy
  104.        
  105.         SelectObject(dc,bmpold);    // put the old bmp in the dc
  106.         DeleteDC(dc);               // then clean up the dc
  107.         DeleteObject(bmp);          // and the dibsection
  108.     }
  109.    
  110.     //=========================================
  111.     void Create(int wd,int ht)
  112.     {
  113.         // make sure the dims are valid
  114.         if((wd <= 0) || (ht <= 0))
  115.         {
  116.             // invalid dims
  117.             Zero();
  118.             return;
  119.         }
  120.                
  121.         // Step 1:  Prep a BITMAPINFO structure
  122.         BITMAPINFO bi;
  123.         memset(&bi,0,sizeof(BITMAPINFO));       // zero memory for good measure
  124.        
  125.         bi.bmiHeader.biSize =       sizeof(BITMAPINFOHEADER);
  126.         bi.bmiHeader.biWidth =      wd;
  127.         bi.bmiHeader.biHeight =     -ht;  // windows bitmaps are upside down unless the height is negative
  128.         bi.bmiHeader.biPlanes =     1;
  129.         bi.bmiHeader.biBitCount =   32;
  130.        
  131.         // Step 2:  Create the dib section
  132.         bmp = CreateDIBSection(0,&bi,DIB_RGB_COLORS,(void**)(&pix),0,0);
  133.         if(!bmp)        // failure is unlikely... but just in case
  134.         {
  135.             Zero();     // maybe throwing an exception would be a better option
  136.             return;     //  than silent failure.  But I'm lazy
  137.         }
  138.        
  139.         // Step 3:  Create the DC
  140.         dc = CreateCompatibleDC(0);
  141.        
  142.         // Step 4:  put the dib section in the dc
  143.         bmpold = (HBITMAP)SelectObject(dc,bmp);
  144.        
  145.         // Step 5:  record other tidbits
  146.         width = wd;
  147.         height = ht;
  148.         pitch = width;      // this just happens to work out on 32-bit bitmaps
  149.        
  150.         // all done
  151.     }
  152.    
  153.     //==================================
  154.     //  copy ctor and assignment operator
  155.     //   private with no bodies to prevent unwanted copying
  156.     Bitmap32(const Bitmap32&);
  157.     Bitmap32& operator = (const Bitmap32&);
  158. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement