void LoadRGBAImage(TEXTURE_RGBA& tex, string Filename, string MkFilename) { unsigned char col[3]; unsigned int ind = 0; TEXTURE_RGBA& bitmap = tex; //返回位图 bitmap.buffer = nullptr; bitmap.sizeX = bitmap.sizeY = 0; std::ifstream bmpfile(Filename, std::ios::binary | std::ios::in); //位图文件(二进制) std::ifstream maskfile(MkFilename, std::ios::binary | std::ios::in); //遮罩位图文件(二进制) if (!bmpfile.is_open()) { printf("[console][Warning] Cannot load %s\n", Filename.c_str()); return; } BITMAPFILEHEADER bfh; //各种关于文件的参数 BITMAPINFOHEADER bih; //各种关于位图的参数 //开始读取 maskfile.read((char*)&bfh, sizeof(BITMAPFILEHEADER)); //这两个是占位mask文件的 maskfile.read((char*)&bih, sizeof(BITMAPINFOHEADER)); //到了后面mask可以直接从颜色部分开始读取 bmpfile.read((char*)&bfh, sizeof(BITMAPFILEHEADER)); //真正的info以这个bmp文件为准 bmpfile.read((char*)&bih, sizeof(BITMAPINFOHEADER)); //它将覆盖之前从mask文件读出来的info数据 bitmap.sizeX = bih.biWidth; bitmap.sizeY = bih.biHeight; bitmap.buffer = unique_ptr<ubyte[]>(new unsigned char[bitmap.sizeX * bitmap.sizeY * 4]); bool noMaskFile = MkFilename == ""; for (unsigned int i = 0; i < bitmap.sizeX * bitmap.sizeY; i++) { //把BGR格式转换为RGB格式 bmpfile.read((char*)col, 3); bitmap.buffer[ind++] = col[2]; //R bitmap.buffer[ind++] = col[1]; //G bitmap.buffer[ind++] = col[0]; //B if (noMaskFile) { bitmap.buffer[ind++] = 255; } else { //将遮罩图的红色通道反相作为Alpha通道 maskfile.read((char*)col, 3); bitmap.buffer[ind++] = 255 - col[2]; //A } } bmpfile.close(); maskfile.close(); }
void kinectGuiApp::clearMask() { kinect.clearMask(); ofFile maskfile(maskFilename); if (maskfile.exists()) maskfile.remove(); }