VFWImage.cpp

Go to the documentation of this file.
00001 // ************************************************************************
00002 //      class CVFWImage
00003 //      Can capture bitmap from camera, display and save it.
00004 //      Camera functionality provided by separate CVFWCapture class.
00005 //  Author:     Audrey J. W. Mbogho
00006 //      Email:  walegwa AT yahoo DOT com
00007 //      Last Modified: January 2005
00008 //      Previously, I tried to give credit here to all the
00009 //      other people from whom I "borrowed" code. But that list
00010 //      has grown to be too long for that. Apologies to all!
00011 //      Environment: Visual C++, Windows 98, Logitech Webcam 4000 Pro.
00012 // ************************************************************************
00013 
00014 #include <fstream.h>
00015 #include "VFWImage.h"
00016 #include "VFWCapture.h"
00017 #include "wx/wx.h"
00018 
00019 // Constructor
00020 // Initializes object data
00021 CVFWImage::CVFWImage()
00022 {
00023         Width = 0;
00024         Height = 0;             
00025         bmpData = '\0';         
00026         pbmi = NULL;    
00027         BitmapSize = 0;
00028         
00029 }
00030 
00031 CVFWImage::~CVFWImage()
00032 {
00033 }
00034 
00035 
00036 wxImage CVFWImage::getWxImage() {
00037     m_wximage.Create(Width, Height);
00038 
00039 
00040         int rows=Height;
00041         int cols=Width;
00042         char *dibdata=bmpData;
00043 
00044     unsigned char *wximagedata = m_wximage.GetData();
00045         
00046         
00047         int r, colpos,rowpos, rowsize = 3*Width;        
00048         for(r=rows-1;r>=0;r--)
00049         {
00050                 rowpos=r*rowsize;
00051                 for(colpos=0;colpos<cols*3;) 
00052                 {                       
00053                 *wximagedata++=dibdata[rowpos+2+colpos]; //BGR in BMP
00054                         *wximagedata++=dibdata[rowpos+1+colpos]; //BGR in BMP
00055                         *wximagedata++=dibdata[rowpos+0+colpos]; //BGR in BMP
00056                         colpos +=3;
00057                 }
00058         }       
00059         return m_wximage;
00060         
00061 }
00062 
00063 
00064 wxImage CVIP2WX(Image *cvipImage) {
00065     
00066 
00067 
00068 
00069         int rows=240; //240; //Height;
00070         int cols=320; //320; //Width;
00071 
00072                 wxImage wx=wxImage(cols, rows,true);
00073         //char *dibdata=bmpData;
00074 
00075     unsigned char *wximagedata = wx.GetData();
00076         
00077         char **image_red;
00078         char **image_blue;
00079         char **image_green;
00080 
00081         image_red = (char **)getData_Image(cvipImage,0);
00082         image_blue = (char **)getData_Image(cvipImage,1);
00083         image_green = (char **)getData_Image(cvipImage,2);
00084         
00085         int r,c, colpos,rowpos ; // , rowsize = 3*Width;        
00086         for(r=0;r<rows;r++)
00087         {
00088                 //rowpos=r*rowsize;
00089                 //for(colpos=0;colpos<cols*3;) 
00090                 for(c=0;c<cols;c++)
00091                 {                       
00092                 *wximagedata++=image_red[r][c]; //RGB in wximagedata
00093                         *wximagedata++=image_blue[r][c]; 
00094                         *wximagedata++=image_green[r][c]; 
00095                         //colpos +=3;
00096                 }
00097         }       
00098         return wx;
00099         
00100 }
00101 
00102 
00103 Image *CVFWImage::getCVIPImage() {
00104    
00105 
00106         int rows=Height;
00107         int cols=Width;
00108         char *data=bmpData;
00109 
00110     Image *cvipImage = NULL;
00111     //LPVOID result;
00112         int r, c, b;
00113         char **image_red;
00114         char **image_blue;
00115         char **image_green;
00116         
00117  
00118         cvipImage = new_Image(PPM,RGB,3,rows,cols,CVIP_BYTE,REAL);
00119 
00120 
00121         image_red = (char **)getData_Image(cvipImage,0);
00122         image_blue = (char **)getData_Image(cvipImage,1);
00123         image_green = (char **)getData_Image(cvipImage,2);
00124                 
00125         for(r=rows-1;r>=0;r--)
00126         {
00127                 for(c=0;c<cols;c++) 
00128                 {
00129                         image_green[r][c]=data[0]; //BGR in BMP
00130                         image_blue[r][c]=data[1]; //BGR in BMP
00131                         image_red[r][c]=data[2]; //BGR in BMP
00132                         data +=3;
00133                 }
00134         }       
00135         return cvipImage;
00136         
00137 }
00138 
00139 
00140 // Captures a DIB captured from video camera into a CVFWImage object
00141 int CVFWImage::Capture()
00142 {
00143         BITMAPINFOHEADER bmih;  // Contained in BITMAPINFO structure
00144 
00145         CVFWCapture cap;
00146         cap.Initialize();       // Initialize first found VFW device
00147 
00148         pbmi = NULL;    // CaptureDIB will automatically allocate this if set to NULL
00149 
00150         // Capture an image from the capture device.
00151         if (cap.CaptureDIB(&pbmi, 0, &BitmapSize))
00152         {
00153                 // Obtain args for SetDIBitsToDevice
00154                 bmih = pbmi->bmiHeader;
00155                 Width = bmih.biWidth;
00156                 Height = bmih.biHeight;
00157                 Height = (bmih.biHeight>0) ? bmih.biHeight : -bmih.biHeight; // absolute value
00158                 bmpData = (char *)pbmi;
00159 
00160                 bmpData += cap.CalcBitmapInfoSize(bmih);
00161         }
00162         cap.Destroy();  // Done using VFW object
00163         return 0;
00164 }
00165 
00166 
00167 // CVFWImage::GDIPaint (hdc,x,y);
00168 // Paints bitmap to a Windows DC
00169 int CVFWImage::GDIPaint (HDC hdc,int x=0,int y=0)
00170 {
00171         int ret = 0;
00172         // Paint the image to the device.
00173         ret = SetDIBitsToDevice (hdc,x,y,Width,Height,0,0,
00174                 0,Height,(LPVOID)bmpData,pbmi,0);
00175         return ret;
00176 }
00177 
00178 
00179 // SaveBMP()
00180 // Saves current bitmap to file
00181 int CVFWImage::SaveToFile(LPCSTR fileName)
00182 {
00183         BITMAPFILEHEADER bfh;
00184 
00185         if (!pbmi)
00186                 return 1;                       // bitmap is empty
00187 
00188         ofstream bmpFile(fileName, ios::out | ios::binary);
00189         if (bmpFile.is_open())
00190         {
00191                 bfh.bfType = 0x4d42;    // 0x42 = "B" 0x4d = "M"
00192                 bfh.bfSize = (DWORD) BitmapSize + sizeof(BITMAPFILEHEADER);
00193                 bfh.bfOffBits = (DWORD)   sizeof(BITMAPFILEHEADER) +
00194                                    sizeof(BITMAPINFOHEADER) +
00195                                     pbmi->bmiHeader.biClrUsed * 
00196                 sizeof (RGBQUAD);
00197                 bfh.bfReserved1 = 0;
00198                 bfh.bfReserved2 = 0;
00199 
00200                 bmpFile.write(reinterpret_cast<const char *>(&bfh),sizeof(bfh));
00201                 bmpFile.write(reinterpret_cast<const char *>(pbmi),BitmapSize);
00202                 bmpFile.close();
00203         }
00204         return 0;
00205 }

Generated on Wed Nov 29 01:27:42 2006 by  doxygen 1.4.6