libeblearntools
/home/rex/ebltrunk/tools/libeblearntools/include/camera_windows.hpp
00001 /***************************************************************************
00002  *   Copyright (C) 2010 by Pierre Sermanet and Marc Howard *
00003  *   pierre.sermanet@gmail.com   *
00004  *   All rights reserved.
00005  *
00006  * Redistribution and use in source and binary forms, with or without
00007  * modification, are permitted provided that the following conditions are met:
00008  *     * Redistributions of source code must retain the above copyright
00009  *       notice, this list of conditions and the following disclaimer.
00010  *     * Redistributions in binary form must reproduce the above copyright
00011  *       notice, this list of conditions and the following disclaimer in the
00012  *       documentation and/or other materials provided with the distribution.
00013  *     * Redistribution under a license not approved by the Open Source 
00014  *       Initiative (http://www.opensource.org) must display the 
00015  *       following acknowledgement in all advertising material:
00016  *        This product includes software developed at the Courant
00017  *        Institute of Mathematical Sciences (http://cims.nyu.edu).
00018  *     * The names of the authors may not be used to endorse or promote products
00019  *       derived from this software without specific prior written permission.
00020  *
00021  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED 
00022  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00023  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00024  * DISCLAIMED. IN NO EVENT SHALL ThE AUTHORS BE LIABLE FOR ANY
00025  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00026  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00027  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00028  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00029  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00030  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00031  ***************************************************************************/
00032 
00033 // ************************************************************************
00034 //      class Camera_windows
00035 //      Camera functionality provided by separate CVFWCapture class.
00036 //  Author:     Marc Howard
00037 //      Email:  marc.e.howard AT gmail DOT com
00038 //      Last Modified: Juuly 2010
00039 //      Borrowed some code from Audrey J. W. Mbogho  walegwa AT yahoo DOT com
00040 //      Last Modified: January 2005
00041 //      Environment: Visual Studio 2008, Windows 7 64 Bit, 
00042 //        Logitech Quickcam 4000 Pro.
00043 // ************************************************************************
00044 
00045 #ifndef CAMERA_WINDOWS_HPP_
00046 #define CAMERA_WINDOWS_HPP_
00047 
00048 #include <fstream>
00049 
00050 using namespace std;
00051 
00052 namespace ebl {
00053 
00054   template <typename Tdata>
00055   camera_windows<Tdata>::camera_windows(int height_, int width_) {
00056     : camera<Tdata>(height_, width_) {
00057 #ifdef __WINDOWS__
00058     bmpData = '\0';             
00059     pbmi = NULL;        
00060     BitmapSize = 0;
00061     cap.Initialize();
00062 #else
00063     eblerror("cannot use camera_windows when not under Windows");
00064 #endif
00065   }
00066 
00067   template <typename Tdata>
00068   camera_windows<Tdata>::~camera_windows() {
00069 #ifdef __WINDOWS__
00070     cap.Destroy();      // Done using VFW object
00071 #endif
00072   }
00073 
00074   template <typename Tdata>
00075   idx<Tdata> camera_windows<Tdata>::grab() {
00076     BITMAPINFOHEADER bmih;      // Contained in BITMAPINFO structure
00077     pbmi = NULL;  // CaptureDIB will automatically allocate this if set to NULL
00078     // Capture an image from the capture device.
00079     if (cap.CaptureDIB(&pbmi, 0, &BitmapSize)) {
00080         // Obtain args for SetDIBitsToDevice
00081         bmih = pbmi->bmiHeader;
00082         int height = -1, width = -1;
00083         width = bmih.biWidth;
00084         height = bmih.biHeight;
00085         cout << "camera is returning " << height << "x" << width << " frames."
00086              << endl;
00087         height = (bmih.biHeight>0) ? bmih.biHeight : -bmih.biHeight;
00088         bmpData = (char *)pbmi;
00089         bmpData += cap.CalcBitmapInfoSize(bmih);
00090       }
00091     frame_id_++;
00092     return bmpData;
00093   }
00094 
00095 } /* namespace ebl */
00096 
00097 #endif CAMERA_WINDOWS_HPP_
00098