The Machine Perception Toolbox

[Introduction]- [News]- [Download]- [Screenshots]- [Manual (pdf)]- [Forums]- [API Reference]- [Repository ]

 

Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

common.h

Go to the documentation of this file.
00001 /*
00002  * common.h
00003  *
00004  * Copyright (c) 2003 Machine Perception Laboratory
00005  * University of California San Diego.
00006  * Please read the disclaimer and notes about redistribution
00007  * at the end of this file.
00008  *
00009  * Authors: Josh Susskind, Ian Fasel, Bret Fortenberry
00010  */
00011 #ifndef __MPLABCOMMON_H__
00012 #define __MPLABCOMMON_H__
00013 
00014 #ifdef WIN32
00015 #include <windows.h>
00016 #else
00017 #include <iostream.h>
00018 #include <sys/time.h>
00019 #include <pthread.h>
00020 #include "errno.h"
00021 #endif
00022 #include <iostream>
00023 
00024 #ifndef BYTE
00025 #define BYTE unsigned char
00026 #endif
00027 #ifndef PIXEL_TYPE
00028 #define PIXEL_TYPE BYTE
00029 #endif
00030 
00031 #undef BOOL
00032 typedef int BOOL;
00033 
00034 #ifndef TRUE
00035 #define TRUE 1
00036 #endif
00037 
00038 #ifndef FALSE
00039 #define FALSE 0
00040 #endif
00041 
00042 #undef PIXEL_TYPE
00043 #define PIXEL_TYPE BYTE
00044 
00045 typedef struct sMUTEX {
00046         int activation;
00047 #ifdef WIN32
00048         HANDLE mutex;
00049 #else
00050         pthread_mutex_t mutex;
00051 #endif
00052 } MUTEX_TYPE;
00053 
00054 
00055 
00056 
00057 // ================================================================ 
00058 
00059 #ifdef WIN32
00060 #define MUTEX_LOCK_RETURN_TYPE DWORD
00061 #define THREAD_RETURN_TYPE void
00062 #define THREAD_TYPE HANDLE
00063 #define THREAD_COND HANDLE
00064 
00065 #define TIMETYPE DWORD
00066 #define IMAGE_FLIPPED true
00067 
00068 // ================================================================
00069 
00070 #else   //if not WIN32  
00071 
00072 class  nRGBTRIPLE{
00073 public:
00074   PIXEL_TYPE rgbtRed;
00075   PIXEL_TYPE rgbtGreen;
00076   PIXEL_TYPE rgbtBlue;
00077 
00078   // Default & Copy Constructors and = operator
00079   nRGBTRIPLE() : rgbtRed(0), rgbtGreen(0), rgbtBlue(0){};
00080   nRGBTRIPLE(PIXEL_TYPE r, PIXEL_TYPE g, PIXEL_TYPE b) :
00081     rgbtRed(r), rgbtGreen(g), rgbtBlue(b){};
00082   template<class T> 
00083         inline nRGBTRIPLE(const T &v){
00084           rgbtRed = static_cast<PIXEL_TYPE>(v);
00085           rgbtGreen = static_cast<PIXEL_TYPE>(v);
00086           rgbtBlue = static_cast<PIXEL_TYPE>(v);
00087   }
00088   nRGBTRIPLE(const nRGBTRIPLE &v){
00089         rgbtRed = v.rgbtRed;
00090         rgbtGreen = v.rgbtGreen;
00091         rgbtBlue = v.rgbtBlue;
00092   }
00093   template<class T> 
00094 //      inline nRGBTRIPLE& operator=(const T &v){
00095           inline void operator=(const T &v){
00096                 rgbtRed = static_cast<PIXEL_TYPE>(v);
00097                 rgbtGreen = static_cast<PIXEL_TYPE>(v);
00098                 rgbtBlue = static_cast<PIXEL_TYPE>(v);
00099 //              return *this;
00100   }
00101 //  inline nRGBTRIPLE& operator=(const nRGBTRIPLE &v){
00102   inline void operator=(const nRGBTRIPLE &v){
00103                 rgbtRed = v.rgbtRed;
00104                 rgbtGreen = v.rgbtGreen;
00105                 rgbtBlue = v.rgbtBlue;
00106 //              return *this;
00107   }
00108 };
00109 
00110 #define RGBTRIPLE nRGBTRIPLE
00111 
00112 typedef unsigned long       DWORD;
00113 typedef unsigned short      WORD;
00114 typedef long                                                            LONG;
00115 typedef long                                                            LONGLONG;
00116 
00117 
00118 #define MUTEX_LOCK_RETURN_TYPE int
00119 #define WAIT_OBJECT_0 0
00120 #define WAIT_TIMEOUT EBUSY
00121 #define WAIT_ABANDONED EDEADLK
00122 #define WAIT_FAILED EINVAL
00123 #define THREAD_RETURN_TYPE void *
00124 #define THREAD_TYPE pthread_t
00125 #define THREAD_COND pthread_cond_t
00126 
00127 #define TIMETYPE struct timeval
00128 #define IMAGE_FLIPPED false
00129 
00130 typedef struct tagBITMAPFILEHEADER { // bmfh 
00131     WORD    bfType; 
00132     DWORD   bfSize; 
00133     WORD    bfReserved1; 
00134     WORD    bfReserved2; 
00135     DWORD   bfOffBits; 
00136 } BITMAPFILEHEADER; 
00137 
00138 typedef struct tagBITMAPINFOHEADER{ // bmih 
00139     DWORD  biSize; 
00140     LONG   biWidth; 
00141     LONG   biHeight; 
00142     WORD   biPlanes; 
00143     WORD   biBitCount;
00144     DWORD  biCompression; 
00145     DWORD  biSizeImage; 
00146     LONG   biXPelsPerMeter; 
00147     LONG   biYPelsPerMeter; 
00148     DWORD  biClrUsed; 
00149     DWORD  biClrImportant; 
00150 } BITMAPINFOHEADER;
00151 
00152         
00153 //defined in mpisearch
00154 /*#ifndef max
00155 template <class T, class U>
00156 static inline T max(T x, U y){ return static_cast<T>(x > y ? x : y);}
00157 #endif
00158 #ifndef min
00159 template <class T, class U>
00160 static inline T min(T x, U y){ return static_cast<T>(x < y ? x : y);}
00161 #endif
00162 */
00163  
00164 #endif //not WIN32
00165 
00167 
00168 inline MUTEX_LOCK_RETURN_TYPE LockMutex(MUTEX_TYPE pMutex){
00169         if(pMutex.activation == 1) {
00170                 std::cout << "error - trying to lock previously locked mutex\n";
00171                 return 0;
00172         }
00173         pMutex.activation = 1;
00174 #ifdef WIN32
00175         return WaitForSingleObject( pMutex.mutex, INFINITE );
00176 #else
00177         return pthread_mutex_lock(&pMutex.mutex);
00178 #endif
00179 }
00180 
00182 
00183 inline MUTEX_LOCK_RETURN_TYPE TryLockMutex(MUTEX_TYPE pMutex, DWORD msec = 16){
00184         MUTEX_LOCK_RETURN_TYPE rtn;
00185 #ifdef WIN32
00186         rtn = WaitForSingleObject(pMutex.mutex, msec );
00187 #else
00188         rtn = pthread_mutex_trylock(&pMutex.mutex);
00189 #endif
00190         if (rtn == WAIT_OBJECT_0) pMutex.activation = 1;
00191         return rtn;
00192 }
00193 
00194 inline void CreateMutex (MUTEX_TYPE &pMutex) {
00195         pMutex.activation = 0;
00196 #ifdef WIN32
00197         pMutex.mutex = CreateMutex (NULL, FALSE, NULL);
00198 #else
00199         pthread_mutex_init(&pMutex.mutex,NULL);
00200 #endif
00201 }
00202 
00203 inline int ReleaseMutex(MUTEX_TYPE pMutex) {
00204         pMutex.activation = 0;
00205 #ifdef WIN32 
00206         return ReleaseMutex(pMutex.mutex);
00207 #else
00208         return (pthread_mutex_unlock(&pMutex.mutex)==0);
00209 #endif
00210 }
00211 
00212 inline void CreateMutexEvent(THREAD_COND &pCond) {
00213 #ifdef WIN32
00214         pCond = CreateEvent(NULL, TRUE, FALSE, NULL); 
00215 #else
00216         pthread_cond_init(&pCond,NULL);
00217 #endif
00218 }
00219 
00220 inline void MutexCondSignal(THREAD_COND pCond) {
00221 #ifdef WIN32
00222         PulseEvent(pCond);
00223 #else
00224         pthread_cond_signal(&pCond);
00225 #endif
00226 }
00227 
00228 inline void MutexCondWait(THREAD_COND &pCond, MUTEX_TYPE &pMutex) {
00229 #ifdef WIN32
00230         WaitForSingleObject( pCond, INFINITE );
00231 #else
00232         pthread_cond_wait(&pCond,&pMutex.mutex);
00233 #endif
00234 }
00235 
00236 
00238 
00239 
00240 #endif // __MPLABCOMMON_H__
00241 
00242 
00243 /*
00244  * 
00245  * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
00246  * 
00247  *    1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
00248  *    2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
00249  *    3. The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission.
00250  * 
00251  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00252  * 
00253  */
00254 
00255 
00256 
00257 
00258 
00259 
00260 
00261 
00262 
00263 
00264 
00265 
00266 

Generated on Mon Nov 8 17:07:32 2004 for MPT by  doxygen 1.3.9.1