00001
00011 #include "standard.h"
00012 #include "PortablePicture.h"
00013 #include "GLBitmapBuffer.h"
00014
00015 GLBitmapBuffer::GLBitmapBuffer()
00016 {
00017 hdc = CreateCompatibleDC (NULL);
00018 assert(hdc);
00019
00020 hBitmap = NULL;
00021 hBitmapOld = NULL;
00022 data = NULL;
00023 }
00024
00025
00026 void GLBitmapBuffer::select()
00027 {
00028 m_hrcOld = wglGetCurrentContext();
00029 hdcOld = wglGetCurrentDC();
00030
00031 if (!wglMakeCurrent (hdc, m_hrc))
00032 throw Error ("GLBitmapBuffer::Select(): wglMakeCurrent() failure.");
00033 }
00034
00035 void GLBitmapBuffer::unselect()
00036 {
00037 wglMakeCurrent (hdcOld, m_hrcOld);
00038 }
00039
00040 void GLBitmapBuffer::create(unsigned int w, unsigned int h)
00041 {
00042
00043
00044
00045 width = w;
00046 height = h;
00047
00048
00049 BITMAPV4HEADER BIH ;
00050 int iSize = sizeof(BITMAPV4HEADER) ;
00051 memset(&BIH, 0, iSize);
00052
00053
00054 BIH.bV4Size = iSize;
00055 BIH.bV4Width = width;
00056 BIH.bV4Height = height;
00057 BIH.bV4Planes = 1;
00058 BIH.bV4BitCount = 24;
00059
00060 #if 0
00061 BIH.bV4BitCount = 32;
00062 BIH.bV4V4Compression = BI_BITFIELDS;
00063 BIH.bV4RedMask = 0x00FF0000;
00064 BIH.bV4GreenMask = 0x0000FF00;
00065 BIH.bV4BlueMask = 0x000000FF;
00066 BIH.bV4AlphaMask = 0xFF000000;
00067 #endif
00068
00069
00070 hBitmap = CreateDIBSection(
00071 hdc,
00072 (BITMAPINFO*)&BIH,
00073 DIB_RGB_COLORS,
00074 (void**)&data,
00075 NULL,
00076 0);
00077
00078 assert (hBitmap);
00079 assert (data);
00080
00081 hBitmapOld = (HBITMAP)::SelectObject(
00082 hdc,
00083 hBitmap);
00084
00085
00086
00087
00088 DWORD dwFlags = PFD_DRAW_TO_BITMAP | PFD_SUPPORT_OPENGL;
00089
00090
00091
00092
00093 PIXELFORMATDESCRIPTOR pfd ;
00094 memset(&pfd,0, sizeof(PIXELFORMATDESCRIPTOR)) ;
00095 pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
00096 pfd.nVersion = 1 ;
00097 pfd.dwFlags = dwFlags ;
00098 pfd.iPixelType = PFD_TYPE_RGBA ;
00099 pfd.cColorBits = (BYTE)24 ;
00100 pfd.cDepthBits = 32 ;
00101 pfd.iLayerType = PFD_MAIN_PLANE ;
00102 #if 0
00103 pfd.cAlphaBits = 1;
00104 #endif
00105
00106
00107 int nPixelFormat = ChoosePixelFormat (hdc, &pfd);
00108
00109 if (!nPixelFormat)
00110 throw Error("GLBitmapBuffer::Create(): ChoosePixelFormat failure.");
00111
00112 if (!SetPixelFormat (hdc, nPixelFormat, &pfd))
00113 throw Error("GLBitmapBuffer::Create(): SetPixelFormat failure.");
00114
00115 m_hrc = wglCreateContext (hdc);
00116
00117 if (!m_hrc)
00118 throw Error ("GLBitmapBuffer::Create(): wglCreateContext failure.");
00119 }
00120
00121 RgbByte *GLBitmapBuffer::operator[] (int index)
00122 {
00123 return (RgbByte*)&data[index * width * 3];
00124 }
00125
00126 GLBitmapBuffer::~GLBitmapBuffer()
00127 {
00128 if (hBitmapOld)
00129 SelectObject (hdc, hBitmapOld);
00130
00131 if (hBitmap)
00132 DeleteObject (hBitmap);
00133
00134 if (hdc)
00135 DeleteDC (hdc);
00136
00137
00138
00139
00140 if (m_hrc == wglGetCurrentContext())
00141 wglMakeCurrent(NULL,NULL) ;
00142
00143 wglDeleteContext(m_hrc) ;
00144 m_hrc = NULL ;
00145 }
00146
00147 void GLBitmapBuffer::saveAs (char *filename)
00148 {
00149 PortablePixMap p;
00150 p.setDimensions (getWidth(), getHeight());
00151 memcpy (p[0], data, getWidth()*getHeight()*3);
00152
00153 RGBTRIPLE *pic = (RGBTRIPLE*)p[0];
00154 for (unsigned int i = 0; i < getWidth()*getHeight(); i++)
00155 {
00156 std::swap (pic[i].rgbtBlue, pic[i].rgbtRed);
00157 }
00158
00159 p.saveAs(filename);
00160 }