PixelFormat.cxx
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00031
00032
00033
00034 #include <openvideo/PixelFormat.h>
00035
00036 #ifdef OV_IS_WINDOWS
00037 # include <windows.h>
00038 #endif
00039
00040 #include <GL/gl.h>
00041 #include <map>
00042
00043
00044 #ifndef GL_BGR
00045 # define GL_BGR 0x80E0
00046 #endif
00047
00048 #ifndef GL_BGRA
00049 # define GL_BGRA 0x80E1
00050 #endif
00051
00052
00053 namespace openvideo
00054 {
00055
00056
00057 typedef std::map<std::string, PIXEL_FORMAT> StringFormatMap;
00058 typedef std::pair<std::string, PIXEL_FORMAT> StringFormatPair;
00059
00060
00061 const char* formatNames[] = {
00062 "R8G8B8",
00063 "B8G8R8",
00064 "R8G8B8X8",
00065 "B8G8R8X8",
00066 "R5G6B5",
00067 "L8",
00068 "UNKNOWN"
00069 };
00070
00071 static StringFormatMap stringFormatMap;
00072
00073
00074 static void
00075 fillStringFormatMap()
00076 {
00077 stringFormatMap.clear();
00078
00079 for(int i=0; i<FORMAT_UNKNOWN; i++)
00080 stringFormatMap.insert( StringFormatPair(formatNames[i], static_cast<PIXEL_FORMAT>(i)) );
00081 }
00082
00083
00084 PIXEL_FORMAT
00085 PixelFormat::StringToFormat(const std::string& formatName)
00086 {
00087 if(stringFormatMap.empty())
00088 fillStringFormatMap();
00089
00090 StringFormatMap::iterator it = stringFormatMap.find(formatName);
00091
00092 if(it == stringFormatMap.end())
00093 return FORMAT_UNKNOWN;
00094
00095 return it->second;
00096 }
00097
00098
00099 std::string
00100 PixelFormat::FormatToString(PIXEL_FORMAT format)
00101 {
00102 if(format>=0 && format<FORMAT_UNKNOWN)
00103 return formatNames[format];
00104
00105 return formatNames[FORMAT_UNKNOWN];
00106 }
00107
00108
00109 int
00110 PixelFormat::getBitsPerPixel(PIXEL_FORMAT format)
00111 {
00112 switch(format)
00113 {
00114 case FORMAT_R8G8B8X8:
00115 case FORMAT_B8G8R8X8:
00116 return 32;
00117
00118 case FORMAT_R8G8B8:
00119 case FORMAT_B8G8R8:
00120 return 24;
00121
00122 case FORMAT_R5G6B5:
00123 return 16;
00124
00125 case FORMAT_L8:
00126 return 8;
00127
00128 default:
00129 return 0;
00130 }
00131 }
00132
00133
00134 PIXEL_FORMAT
00135 PixelFormat::fromOGL(int format)
00136 {
00137 switch(format)
00138 {
00139 case GL_RGB:
00140 return FORMAT_R8G8B8;
00141 case GL_BGR:
00142 return FORMAT_B8G8R8;
00143 case GL_RGBA:
00144 return FORMAT_R8G8B8X8;
00145 case GL_BGRA:
00146 return FORMAT_B8G8R8X8;
00147 case GL_LUMINANCE:
00148 return FORMAT_L8;
00149 default:
00150 return FORMAT_UNKNOWN;
00151 }
00152 }
00153
00154
00155 bool
00156 PixelFormat::toOGL(PIXEL_FORMAT format, unsigned int& oglFormat, int& oglInternalFormat)
00157 {
00158 switch(format)
00159 {
00160 case FORMAT_R8G8B8:
00161 oglFormat=GL_RGB;
00162 oglInternalFormat=3;
00163 return true;
00164
00165 case FORMAT_B8G8R8:
00166 oglFormat=GL_BGR_EXT;
00167 oglInternalFormat=3;
00168 return true;
00169
00170 case FORMAT_R8G8B8X8:
00171 oglFormat=GL_RGBA;
00172 oglInternalFormat=4;
00173 return true;
00174
00175 case FORMAT_B8G8R8X8:
00176 oglFormat=GL_BGRA_EXT;
00177 oglInternalFormat=4;
00178 return true;
00179
00180 case FORMAT_L8:
00181 oglFormat=GL_LUMINANCE;
00182 oglInternalFormat=GL_LUMINANCE8;
00183 return true;
00184 }
00185
00186 return false;
00187 }
00188
00189
00190 }