//将jpg数据显示到界面 DWORD WINAPI drawJPEG(LPVOID param) { CLiveview *dlg = (CLiveview *)param; dlg->fetching = true; while (dlg->fetching) { if (dlg->mJpegQueue.size() == 0) { continue; } IPicture *pPic = getPicFromHGlobal(dlg->mJpegQueue.front()); dlg->mJpegQueue.pop(); if (pPic == NULL) break; OLE_XSIZE_HIMETRIC hmWidth; OLE_YSIZE_HIMETRIC hmHeight; pPic->get_Width(&hmWidth); pPic->get_Height(&hmHeight); double fX, fY; CRect rect; GetClientRect(dlg->m_Liveview.m_hWnd, rect); fX = rect.Width(); fY = rect.Height(); HDC *pDC = &dlg->m_Liveview.GetDC()->m_hDC; pPic->Render(*pDC, 0, 0, (DWORD)fX, (DWORD)fY, 0, hmHeight, hmWidth, -hmHeight, NULL); pPic->Release(); } dlg->fetching = false; dlg->FreshUI(); return -1; }
//拍摄照片按钮 void CLiveview::OnBnClickedBtnpic() { Document d; //发送拍照请求 d = indevice->actTakePicture(); if (indevice->isJsonOk(d)) { //请求成功就解析照片地址 继续获取 CString pictureurl = d["result"].GetArray()[0].GetArray()[0].GetString(); ULONG size = 0; PUCHAR picture = HttpClient::getData(pictureurl, size); if (size <= 0) { return; } //将图片数据存储为文件 FILE *file = fopen("d:\\picture.jpg", "wb"); if (file) { fwrite(picture, size, 1, file); fclose(file); } //将获取的数据解码为照片并显示 HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, size); LPVOID pData = GlobalLock(hGlobal); memcpy(pData, picture, size); GlobalUnlock(hGlobal); delete[] picture; IPicture *pPic = getPicFromHGlobal(hGlobal); if (pPic == NULL) return; OLE_XSIZE_HIMETRIC hmWidth; OLE_YSIZE_HIMETRIC hmHeight; pPic->get_Width(&hmWidth); pPic->get_Height(&hmHeight); double fX, fY; CRect rect; mPic.GetWindowRect(&rect); fX = rect.Width(); fY = rect.Height(); HDC *pDC = &mPic.GetDC()->m_hDC; pPic->Render(*pDC, 0, 0, (DWORD)fX, (DWORD)fY, 0, hmHeight, hmWidth, -hmHeight, NULL); pPic->Release(); } }
bool CLotteryDlg::_loadPic(CString strFullPath, PicInfo* picInfo) { try { HANDLE hFile = CreateFile(strFullPath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (hFile == INVALID_HANDLE_VALUE) return false; DWORD dwFileSize = GetFileSize(hFile, NULL); if (-1 == dwFileSize) return false; picInfo->_hGlobal = GlobalAlloc(GMEM_MOVEABLE, dwFileSize); if (picInfo->_hGlobal == NULL) return false; void * pImageMemory = GlobalLock(picInfo->_hGlobal); DWORD dwReadedSize;//保存实际读取的文件的大小 ReadFile(hFile, pImageMemory, dwFileSize, &dwReadedSize, NULL); GlobalUnlock(picInfo->_hGlobal); CloseHandle(hFile); IStream* pIStream;//创建一个ISTream接口指针,保存图片流 IPicture* pIPicture;//创建一个IPicture接口指针,表示图片对象 if (S_OK != CreateStreamOnHGlobal(picInfo->_hGlobal, false, &pIStream))//用全局内存初始化IStream接口指针 return false; if (S_OK != OleLoadPicture(pIStream, 0, false, IID_IPicture, (LPVOID*)&(pIPicture)))//用OleLoadPicture获得IPicture接口指针 { ::GlobalFree(picInfo->_hGlobal); pIStream->Release(); return false; } //得到IPicture COM接口对象后,就可以进行获得图片信息,显示图片等操作 pIPicture->get_Width(&picInfo->_cx); pIPicture->get_Height(&picInfo->_cy); picInfo->_iStream = pIStream; picInfo->_iPic = pIPicture; picInfo->_x = picInfo->_y = 0; picInfo->_strFilePath = strFullPath; } catch (...) { return false; } return true; }
HRESULT CPicStatic::ShowJpeg(LPVOID picData,DWORD dwFileSize,int x, int y,CDC *pDC) { IStream *pStm; IPicture *pPic; BOOL bResult; HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, dwFileSize); LPVOID pvData = NULL; if (hGlobal == NULL) return false; pvData = GlobalLock(hGlobal); if ( pvData == NULL) return false; memcpy(pvData,picData,dwFileSize); GlobalUnlock(hGlobal); CreateStreamOnHGlobal(hGlobal, TRUE, &pStm); bResult=OleLoadPicture(pStm,dwFileSize,TRUE,IID_IPicture,(LPVOID*)&pPic); if(FAILED(bResult)) { // CloseHandle(hFile); return false; } OLE_XSIZE_HIMETRIC hmWidth; OLE_YSIZE_HIMETRIC hmHeight; pPic->get_Width(&hmWidth); pPic->get_Height(&hmHeight); //将图形输出到屏幕上(有点像BitBlt) bResult=pPic->Render(pDC->m_hDC,0,0,x,y,0,hmHeight,hmWidth,-hmHeight,NULL); // CloseHandle(hFile); pStm->Release(); pPic->Release(); if (SUCCEEDED(bResult)) { return S_OK; } else { return E_FAIL; } }
void CCommon::DrawFaceImageMin(CStatic *m_picBox,unsigned char *image,unsigned long int size) { CRect rect; m_picBox->GetClientRect(&rect);//获得pictrue控件所在的举行区域 CDC *pDC=m_picBox->GetDC();//获得pictrue控件的DC IPicture *pPic;//定义显示图片的接口(可显示jpg格式图片) HGLOBAL hGlobal=GlobalAlloc(GMEM_MOVEABLE,size);//分配内存空间 void *pData=GlobalLock(hGlobal);//定义图象数据指针 memcpy(pData,image,size); GlobalUnlock(hGlobal);//释放内存空间 IStream *pStream=NULL;//定义数据流指针 //创建数据流 if(CreateStreamOnHGlobal(hGlobal,TRUE,&pStream)==S_OK) { //装载图象文件 if(SUCCEEDED(OleLoadPicture(pStream,size,TRUE,IID_IPicture,(LPVOID*)&pPic))) { OLE_XSIZE_HIMETRIC hmWidth; OLE_XSIZE_HIMETRIC hmHeight; pPic->get_Width(&hmWidth);//获得图象真实宽度 pPic->get_Height(&hmHeight);//获得图象真实高度 //在控件上显示图片 pPic->Render(*pDC,4,4,rect.Width()-8,rect.Height()-8,0,hmHeight,hmWidth,-hmHeight,NULL); pPic->Release(); pStream->Release();//释放数据流 } } if(hGlobal) { GlobalFree(hGlobal); hGlobal = NULL; } m_picBox-> ReleaseDC(pDC); }
bool Bitmap::loadPicture(LPCTSTR pszFilename) { // Loads an image using the IPicture COM interface. // Supported image formats: BMP, EMF, GIF, ICO, JPG, WMF, TGA. // // Based on code from MSDN Magazine, October 2001. // http://msdn.microsoft.com/msdnmag/issues/01/10/c/default.aspx // IPicture interface doesn't support TGA files. if (_tcsstr(pszFilename, _T(".TGA")) || _tcsstr(pszFilename, _T(".tga"))) return loadTarga(pszFilename); HRESULT hr = 0; HANDLE hFile = 0; HGLOBAL hGlobal = 0; IStream *pIStream = 0; IPicture *pIPicture = 0; BYTE *pBuffer = 0; DWORD dwFileSize = 0; DWORD dwBytesRead = 0; LONG lWidth = 0; LONG lHeight = 0; if (!m_logpixelsx && !m_logpixelsy) { HDC hScreenDC = CreateCompatibleDC(GetDC(0)); if (!hScreenDC) return false; m_logpixelsx = GetDeviceCaps(hScreenDC, LOGPIXELSX); m_logpixelsy = GetDeviceCaps(hScreenDC, LOGPIXELSY); DeleteDC(hScreenDC); } hFile = CreateFile(pszFilename, FILE_READ_DATA, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); if (hFile == INVALID_HANDLE_VALUE) return false; if (!(dwFileSize = GetFileSize(hFile, 0))) { CloseHandle(hFile); return false; } if (!(hGlobal = GlobalAlloc(GMEM_MOVEABLE | GMEM_NODISCARD, dwFileSize))) { CloseHandle(hFile); return false; } if (!(pBuffer = reinterpret_cast<BYTE*>(GlobalLock(hGlobal)))) { GlobalFree(hGlobal); CloseHandle(hFile); return false; } if (!ReadFile(hFile, pBuffer, dwFileSize, &dwBytesRead, 0)) { GlobalUnlock(hGlobal); GlobalFree(hGlobal); CloseHandle(hFile); return false; } GlobalUnlock(hGlobal); CloseHandle(hFile); if (FAILED(CreateStreamOnHGlobal(hGlobal, FALSE, &pIStream))) { GlobalFree(hGlobal); return false; } if (FAILED(OleLoadPicture(pIStream, 0, FALSE, IID_IPicture, reinterpret_cast<LPVOID*>(&pIPicture)))) { pIStream->Release(); GlobalFree(hGlobal); return false; } pIStream->Release(); GlobalFree(hGlobal); pIPicture->get_Width(&lWidth); pIPicture->get_Height(&lHeight); width = MulDiv(lWidth, m_logpixelsx, HIMETRIC_INCH); height = MulDiv(lHeight, m_logpixelsy, HIMETRIC_INCH); if (!create(width, height)) { pIPicture->Release(); return false; } selectObject(); hr = pIPicture->Render(dc, 0, 0, width, height, 0, lHeight, lWidth, -lHeight, 0); deselectObject(); pIPicture->Release(); return (SUCCEEDED(hr)) ? true : false; }
// // Entry point for conversion // UT_Error IE_ImpGraphic_Win32Native::_convertGraphic(UT_ByteBuf * pBB, std::string& mimetype) { IPicture* pPicture = NULL; IStream* stream; HGLOBAL hG; HBITMAP hBitmap; OLE_HANDLE* hB; PBITMAPINFO bi; UT_ByteBuf bBufBMP; UT_Error err; /* If the system has GDI+, use it*/ if (isGDIPlusAvailable()) { m_pBB = new UT_ByteBuf(); return GDIconvertGraphic(pBB, m_pBB, mimetype); } // the code below always writes out PNG's for now; we could update it to support // native JPEG images as well, or just delete it and always use GDI+. mimetype = "image/png"; // We need to store the incoming bytebuffer in a Windows global heap size_t nBlockLen = pBB->getLength(); hG = GlobalAlloc(GPTR, nBlockLen); if (!hG) return UT_IE_NOMEMORY; CopyMemory(hG, pBB->getPointer(0), nBlockLen); // Create a stream from heap HRESULT hr = CreateStreamOnHGlobal(hG,false,&stream); if (!SUCCEEDED(hr) || !stream) { GlobalFree(hG); return UT_IE_NOMEMORY; } hr = OleLoadPicture(stream,0,false,IID_IPicture,(void**)&pPicture); stream->Release(); GlobalFree(hG); if (!SUCCEEDED(hr) || !pPicture) { return UT_IE_UNKNOWNTYPE; } pPicture->get_Handle((unsigned int*)&hB); hBitmap = (HBITMAP)CopyImage(hB,IMAGE_BITMAP,0,0,LR_COPYRETURNORG); HWND hWnd = GetDesktopWindow(); // Create a BMP file from a BITMAP bi = CreateBitmapInfoStruct(hBitmap); CreateBMPFile(hWnd, bBufBMP, bi, hBitmap, GetDC(hWnd)); LocalFree ((HLOCAL)bi); InitializePrivateClassData(); /* Read Header Data */ err = Read_BMP_Header(&bBufBMP); /* It's not a bitmap, then we have to rendered it into a device context and get a bitmap from there. Case wmf graphics */ if (err) { if (err!=UT_IE_BOGUSDOCUMENT) { pPicture->Release(); return err; } long nWidth = 0; long nHeight = 0; long nScaleToWidth= 500; long nScaleToHeight= 500; RECT rc, rect; BYTE *imagedata; HBITMAP hBit; HBITMAP hOld; BITMAPINFO bmi; HDC hWndDC = GetDC(hWnd); HDC hMemDC = CreateCompatibleDC(hWndDC); HBRUSH hBrush = (HBRUSH)GetCurrentObject(hMemDC, OBJ_BRUSH); pPicture->get_Width (&nWidth); pPicture->get_Height(&nHeight); bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); bmi.bmiHeader.biWidth = nScaleToWidth; bmi.bmiHeader.biHeight = nScaleToHeight; bmi.bmiHeader.biPlanes = 1; bmi.bmiHeader.biBitCount = 24; // as we want true-color bmi.bmiHeader.biCompression = BI_RGB; // no compression bmi.bmiHeader.biSizeImage = (((bmi.bmiHeader.biWidth * bmi.bmiHeader.biBitCount + 31) & ~31) >> 3) * bmi.bmiHeader.biHeight; bmi.bmiHeader.biXPelsPerMeter = 0; bmi.bmiHeader.biYPelsPerMeter = 0; bmi.bmiHeader.biClrImportant = 0; bmi.bmiHeader.biClrUsed = 0; // we are not using palette hBit = CreateDIBSection(hMemDC,&bmi,DIB_RGB_COLORS,(void**)&imagedata,0,0); hOld = (HBITMAP) SelectObject(hMemDC, hBit); rect.left = 0; rect.top = nScaleToHeight; rect.right = nScaleToWidth; rect.bottom = 0; FillRect(hMemDC, &rect, hBrush); pPicture->Render(hMemDC, 0,0, nScaleToWidth, nScaleToHeight, 0, nHeight, nWidth, -nHeight, &rc); hBit = (HBITMAP)SelectObject(hMemDC, hOld); bi = CreateBitmapInfoStruct(hBit); CreateBMPFile(hWnd, bBufBMP, &bmi, hBit, hMemDC); LocalFree ((HLOCAL)bi); DeleteDC(hMemDC); DeleteDC(hWndDC); DeleteObject(hBrush); DeleteObject(hBit); err = Read_BMP_Header(&bBufBMP); if (err) { pPicture->Release(); return err; } } pPicture->Release(); if ((err = Initialize_PNG())) { return err; } /* Read Palette, if no palette set Header accordingly */ if(m_iBitsPerPlane < 24) { if ((err = Convert_BMP_Palette(&bBufBMP))) return err; } else { UT_uint16 bitsPerChannel; UT_uint16 colorType; if (m_iBitsPerPlane == 24) { bitsPerChannel = 8; colorType = PNG_COLOR_TYPE_RGB; } else if (m_iBitsPerPlane == 32) { bitsPerChannel = 8; colorType = PNG_COLOR_TYPE_RGB_ALPHA; } else if (m_iBitsPerPlane == 48) { bitsPerChannel = 16; colorType = PNG_COLOR_TYPE_RGB; } else if (m_iBitsPerPlane == 64) { bitsPerChannel = 16; colorType = PNG_COLOR_TYPE_RGB_ALPHA; } else { return UT_ERROR; } png_set_IHDR ( m_pPNG, m_pPNGInfo, m_iWidth, m_iHeight, bitsPerChannel, colorType, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT ); } if ((err = Convert_BMP(&bBufBMP))) { return err; } /* Clean Up Memory Used */ FREEP(m_pPNGInfo->palette); png_destroy_write_struct(&m_pPNG, &m_pPNGInfo); return UT_OK; }
//==================================================================================== //= //= Function Name: LoadPictureIPicture //= //= Parameters : pcImage - the descriptor to load the image into //= szFilename - the path of the image file to load from //= //= Returns : TRUE if successful, FALSE otherwise //= //= Description : Loads BMP, EMF, JPG, ICO, WMF, GIF files into a texture descriptor //= by leveraging the power of Windows IPicture routines and OLE //= //==================================================================================== BOOL LoadPictureIPicture(CImage *pcImage, char *szFilename) { HDC hdcTemp; // The DC To Hold Our Bitmap HBITMAP hbmpTemp; // Holds The Bitmap Temporarily IPicture *pPicture; // IPicture Interface OLECHAR wszPath[MAX_PATH + 1]; // Full Path To Picture (WCHAR) long lWidth; // Width In Logical Units long lHeight; // Height In Logical Units long lWidthPixels; // Width In Pixels long lHeightPixels; // Height In Pixels HRESULT hr; short sType; DWORD dwAttributes; //---------------------------------------------------------------------------------------- // Convert From ASCII To Unicode and load the picture path //---------------------------------------------------------------------------------------- MultiByteToWideChar(CP_ACP, 0, szFilename, -1, wszPath, MAX_PATH); hr = OleLoadPicturePath(wszPath, 0, 0, 0, IID_IPicture, (void**)&pPicture); if(FAILED(hr)) { return FALSE; } hdcTemp = CreateCompatibleDC(GetDC(0)); if(!hdcTemp) { pPicture->Release(); return FALSE; } //---------------------------------------------------------------------------------------- // get the pixel dimensions of the image //---------------------------------------------------------------------------------------- pPicture->get_Attributes(&dwAttributes); pPicture->get_Type(&sType); pPicture->get_Width(&lWidth); lWidthPixels = MulDiv(lWidth, GetDeviceCaps(hdcTemp, LOGPIXELSX), 2540); pPicture->get_Height(&lHeight); lHeightPixels = MulDiv(lHeight, GetDeviceCaps(hdcTemp, LOGPIXELSY), 2540); //---------------------------------------------------------------------------------------- // Create A Temporary Bitmap //---------------------------------------------------------------------------------------- BITMAPINFO bi = {0}; // The Type Of Bitmap We Request unsigned int* pBits = NULL; // Pointer To The Bitmap Bits bi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); // Set Structure Size bi.bmiHeader.biBitCount = 32; // 32 Bit bi.bmiHeader.biWidth = lWidthPixels; // Power Of Two Width bi.bmiHeader.biHeight = lHeightPixels; // Make Image Top Up (Positive Y-Axis) bi.bmiHeader.biCompression = BI_RGB; // RGB Encoding bi.bmiHeader.biPlanes = 1; // 1 Bitplane hbmpTemp = CreateDIBSection(hdcTemp, &bi, DIB_RGB_COLORS, (void**)&pBits, 0, 0); if(!hbmpTemp) // Did Creation Fail? { DeleteDC(hdcTemp); // Delete The Device Context pPicture->Release(); // Decrements IPicture Reference Count return FALSE; // Return False (Failure) } SelectObject(hdcTemp, hbmpTemp); // Select Handle To Our Temp DC And Our Temp Bitmap Object //---------------------------------------------------------------------------------------- // Render The IPicture On To The Bitmap //---------------------------------------------------------------------------------------- pPicture->Render(hdcTemp, 0, 0, lWidthPixels, lHeightPixels, 0, lHeight, lWidth, -lHeight, 0); //---------------------------------------------------------------------------------------- // Copy the pixels to our texture descriptor whilst // Converting from BGR To RGB format and adding alpha of 255 //---------------------------------------------------------------------------------------- int x, y; unsigned int iAlpha; BOOL bNonZeroAlpha; CImage cImageImport; int i; CImageCopier cCopier; bNonZeroAlpha = FALSE; for (y = 0; y < lHeightPixels; y++) { for (x = 0; x < lWidthPixels; x++) { unsigned int iPixel = pBits[(x + (lHeightPixels - y - 1) * lWidthPixels)]; iAlpha = iPixel & 0xff000000; if (iAlpha != 0) { bNonZeroAlpha = TRUE; break; } } } //---------------------------------------------------------------------------------------- // setup the texture descriptor //---------------------------------------------------------------------------------------- // NOTE: does not set the unique ID correctly if (bNonZeroAlpha) { pcImage->Init(lWidthPixels, lHeightPixels, PT_uchar, IMAGE_DIFFUSE_RED, IMAGE_DIFFUSE_GREEN, IMAGE_DIFFUSE_BLUE, IMAGE_OPACITY, CHANNEL_ZERO); cImageImport.Init(lWidthPixels, lHeightPixels, pBits, PT_uchar, IMAGE_DIFFUSE_BLUE, IMAGE_DIFFUSE_GREEN, IMAGE_DIFFUSE_RED, IMAGE_OPACITY, CHANNEL_ZERO); cCopier.Init(&cImageImport, pcImage); for (i = 0; i < lHeightPixels; i++) { cImageImport.SetData(&pBits[(lHeightPixels - 1 - i) * lWidthPixels]); cCopier.Copy(0, i, 0, 0, lWidthPixels, 1); } //cCopier.Copy(0, 0, NULL); cCopier.Kill(); cImageImport.Kill(); } else { pcImage->Init(lWidthPixels, lHeightPixels, PT_uchar, IMAGE_DIFFUSE_RED, IMAGE_DIFFUSE_GREEN, IMAGE_DIFFUSE_BLUE, CHANNEL_ZERO); cImageImport.Init(lWidthPixels, lHeightPixels, pBits, PT_uchar, IMAGE_DIFFUSE_BLUE, IMAGE_DIFFUSE_GREEN, IMAGE_DIFFUSE_RED, IMAGE_IGNORED, CHANNEL_ZERO); cCopier.Init(&cImageImport, pcImage); for (i = 0; i < lHeightPixels; i++) { cImageImport.SetData(&pBits[(lHeightPixels - 1 - i) * lWidthPixels]); cCopier.Copy(0, i, 0, 0, lWidthPixels, 1); } cCopier.Kill(); cImageImport.Kill(); } DeleteObject(hbmpTemp); // Delete The Object DeleteDC(hdcTemp); // Delete The Device Context pPicture->Release(); // Decrements IPicture Reference Count return TRUE; // Return True (All Good) }
////////////////////////////////////////////////////////////////////////// //函数:BuildTexture() //参数:szPathName纹理路径,纹理ID //功能:加载纹理 ////////////////////////////////////////////////////////////////////////// bool Menu::BuildTexture(char *szPathName, GLuint &texid) { HDC hdcTemp; // The DC To Hold Our Bitmap HBITMAP hbmpTemp; // Holds The Bitmap Temporarily IPicture *pPicture; // IPicture Interface OLECHAR wszPath[MAX_PATH+1]; // Full Path To Picture (WCHAR) char szPath[MAX_PATH+1]; // Full Path To Picture long lWidth; // Width In Logical Units long lHeight; // Height In Logical Units long lWidthPixels; // Width In Pixels long lHeightPixels; // Height In Pixels GLint glMaxTexDim ; // Holds Maximum Texture Size if (strstr(szPathName, "http://")) // If PathName Contains http:// Then... { strcpy(szPath, szPathName); // Append The PathName To szPath } else // Otherwise... We Are Loading From A File { GetCurrentDirectory(MAX_PATH, szPath); // Get Our Working Directory strcat(szPath, "\\Image\\"); //strcat(szPath, "\\"); // Append "\" After The Working Directory strcat(szPath, szPathName); // Append The PathName } MultiByteToWideChar(CP_ACP, 0, szPath, -1, wszPath, MAX_PATH); // Convert From ASCII To Unicode HRESULT hr = OleLoadPicturePath(wszPath, 0, 0, 0, IID_IPicture, (void**)&pPicture); if(FAILED(hr)) // If Loading Failed return FALSE; // Return False HDC tempDC=GetDC(0); hdcTemp = CreateCompatibleDC(tempDC); // Create The Windows Compatible Device Context if(!hdcTemp) // Did Creation Fail? { pPicture->Release(); // Decrements IPicture Reference Count return FALSE; // Return False (Failure) } ReleaseDC(NULL,tempDC); glGetIntegerv(GL_MAX_TEXTURE_SIZE, &glMaxTexDim); // Get Maximum Texture Size Supported pPicture->get_Width(&lWidth); // Get IPicture Width (Convert To Pixels) lWidthPixels = MulDiv(lWidth, GetDeviceCaps(hdcTemp, LOGPIXELSX), 2540); pPicture->get_Height(&lHeight); // Get IPicture Height (Convert To Pixels) lHeightPixels = MulDiv(lHeight, GetDeviceCaps(hdcTemp, LOGPIXELSY), 2540); // Resize Image To Closest Power Of Two if (lWidthPixels <= glMaxTexDim) // Is Image Width Less Than Or Equal To Cards Limit lWidthPixels = 1 << (int)floor((log((double)lWidthPixels)/log(2.0f)) + 0.5f); else // Otherwise Set Width To "Max Power Of Two" That The Card Can Handle lWidthPixels = glMaxTexDim; if (lHeightPixels <= glMaxTexDim) // Is Image Height Greater Than Cards Limit lHeightPixels = 1 << (int)floor((log((double)lHeightPixels)/log(2.0f)) + 0.5f); else // Otherwise Set Height To "Max Power Of Two" That The Card Can Handle lHeightPixels = glMaxTexDim; // Create A Temporary Bitmap BITMAPINFO bi = {0}; // The Type Of Bitmap We Request DWORD *pBits = 0; // Pointer To The Bitmap Bits bi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); // Set Structure Size bi.bmiHeader.biBitCount = 32; // 32 Bit bi.bmiHeader.biWidth = lWidthPixels; // Power Of Two Width bi.bmiHeader.biHeight = lHeightPixels; // Make Image Top Up (Positive Y-Axis) bi.bmiHeader.biCompression = BI_RGB; // RGB Encoding bi.bmiHeader.biPlanes = 1; // 1 Bitplane // Creating A Bitmap This Way Allows Us To Specify Color Depth And Gives Us Imediate Access To The Bits hbmpTemp = CreateDIBSection(hdcTemp, &bi, DIB_RGB_COLORS, (void**)&pBits, 0, 0); if(!hbmpTemp) // Did Creation Fail? { DeleteDC(hdcTemp); // Delete The Device Context pPicture->Release(); // Decrements IPicture Reference Count return FALSE; // Return False (Failure) } HGDIOBJ old; old=SelectObject(hdcTemp, hbmpTemp); // Select Handle To Our Temp DC And Our Temp Bitmap Object // Render The IPicture On To The Bitmap pPicture->Render(hdcTemp, 0, 0, lWidthPixels, lHeightPixels, 0, lHeight, lWidth, -lHeight, 0); // Convert From BGR To RGB Format And Add An Alpha Value Of 255 for(long i = 0; i < lWidthPixels * lHeightPixels; i++) // Loop Through All Of The Pixels { BYTE* pPixel = (BYTE*)(&pBits[i]); // Grab The Current Pixel BYTE temp = pPixel[0]; // Store 1st Color In Temp Variable (Blue) pPixel[0] = pPixel[2]; // Move Red Value To Correct Position (1st) pPixel[2] = temp; // Move Temp Value To Correct Blue Position (3rd) // This Will Make Any Black Pixels, Completely Transparent (You Can Hardcode The Value If You Wish) if ((pPixel[0]==0) && (pPixel[1]==0) && (pPixel[2]==0)) // Is Pixel Completely Black pPixel[3] = 0; // Set The Alpha Value To 0 else // Otherwise pPixel[3] = 255; // Set The Alpha Value To 255 } glGenTextures(1, &texid); // Create The Texture // Typical Texture Generation Using Data From The Bitmap glBindTexture(GL_TEXTURE_2D, texid); // Bind To The Texture ID glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); // (Modify This For The Type Of Filtering You Want) glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); // (Modify This For The Type Of Filtering You Want) glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, lWidthPixels, lHeightPixels, 0, GL_RGBA, GL_UNSIGNED_BYTE, pBits); // (Modify This If You Want Mipmaps) SelectObject(hdcTemp,old); DeleteObject(hbmpTemp); // Delete The Object DeleteDC(hdcTemp); // Delete The Device Context pPicture->Release(); // Decrements IPicture Reference Count return TRUE; // Return True (All Good) }
LRESULT CVideoRecvTestDlg::OnProceVideo(WPARAM wParam, LPARAM lParam) { if(lParam == NULL) { return E_FAIL; } VideoFrameData* TmpVideo = (VideoFrameData*)lParam; switch(TmpVideo->dwVideoType) { case VIDEO_TYPE_H264_NORMAL_I: case VIDEO_TYPE_H264_NORMAL_P: { CString strMsg; strMsg.Format("DataLen = %d, ExtInfo = %s", TmpVideo->dwVideoDataLen, TmpVideo->pszVideoExInfo); HDC TempDC = ::GetDC(GetDlgItem(IDC_STATIC_VIDEO)->GetSafeHwnd()); SetBkMode(TempDC, 3); SetTextColor(TempDC, RGB(0, 128, 0)); Rectangle(TempDC, 10, 20, 700, 80); TextOut(TempDC, 20, 20, "H264CallBack FrameInfo:", 23); int ilen = strMsg.GetLength(); CString str1 = strMsg.Left(60); CString str2 = strMsg.Right(ilen - 60); TextOut(TempDC, 20, 40, str1.GetBuffer(), str1.GetLength()); TextOut(TempDC, 20, 60, str2.GetBuffer(), str2.GetLength()); //TextOut(TempDC, 20, 40, strMsg.GetBuffer(), strMsg.GetLength()); ::ReleaseDC(GetDlgItem(IDC_STATIC_VIDEO)->GetSafeHwnd(), TempDC); } break; case VIDEO_TYPE_H264_HISTORY_I: case VIDEO_TYPE_H264_HISTORY_P: { CString strMsg; strMsg.Format("DataLen = %d, ExtInfo = %s", TmpVideo->dwVideoDataLen, TmpVideo->pszVideoExInfo); HDC TempDC = ::GetDC(GetDlgItem(IDC_STATIC_VIDEO)->GetSafeHwnd()); SetBkMode(TempDC, 3); SetTextColor(TempDC, RGB(0, 128, 0)); Rectangle(TempDC, 10, 20, 700, 100); TextOut(TempDC, 20, 20, "H264CallBack FrameInfo:", 23); int ilen = strMsg.GetLength(); CString str1 = strMsg.Left(60); CString str2 = strMsg.Right(ilen - 60); TextOut(TempDC, 20, 40, str1.GetBuffer(), str1.GetLength()); TextOut(TempDC, 20, 60, str2.GetBuffer(), str2.GetLength()); //TextOut(TempDC, 20, 40, strMsg.GetBuffer(), strMsg.GetLength()); char* pszTime = strstr(TmpVideo->pszVideoExInfo, "FrameTime:"); DWORD64 dw64TimeMS = 0; if(pszTime) { sscanf(pszTime, "FrameTime:%I64u", &dw64TimeMS); CTime cFrameTime(dw64TimeMS/1000); strMsg = cFrameTime.Format("%Y.%m.%d_%H:%M:%S"); TextOut(TempDC, 400, 60, strMsg.GetBuffer(), strMsg.GetLength()); } ::ReleaseDC(GetDlgItem(IDC_STATIC_VIDEO)->GetSafeHwnd(), TempDC); if(((dw64TimeMS/1000) >= m_dwEndTime)&&m_realfinish == false) { ::SendMessage(GetSafeHwnd(), WM_MESSAGE_GETFINISH, 0, 0); } } break; case VIDEO_TYPE_JPEG_HISTORY: { IStream* pStm = NULL; CreateStreamOnHGlobal(NULL, TRUE, &pStm); IPicture* picholder; LARGE_INTEGER liTempStar = {0}; pStm->Seek(liTempStar, STREAM_SEEK_SET, NULL); ULONG iWritten = NULL; pStm->Write(TmpVideo->pVideoData, TmpVideo->dwVideoDataLen, &iWritten); pStm->Seek(liTempStar, STREAM_SEEK_SET, NULL); if(FAILED(OleLoadPicture(pStm, TmpVideo->dwVideoDataLen, TRUE, IID_IPicture, (void**)&picholder))) { pStm->Release(); return 0; } HDC TempDC; TempDC = ::GetDC(GetDlgItem(IDC_STATIC_VIDEO)->GetSafeHwnd()); OLE_XSIZE_HIMETRIC hmWidth; OLE_YSIZE_HIMETRIC hmHeight; picholder->get_Width(&hmWidth); picholder->get_Height(&hmHeight); int nWidth =MulDiv(hmWidth, GetDeviceCaps(TempDC, LOGPIXELSX), 2540); int nHeight = MulDiv(hmHeight, GetDeviceCaps(TempDC, LOGPIXELSY), 2540); picholder->Render(TempDC, 0, 0, m_ShowFrameRect.right-m_ShowFrameRect.left, m_ShowFrameRect.bottom-m_ShowFrameRect.top, 0, hmHeight, hmWidth, -hmHeight, NULL); ::ReleaseDC(GetDlgItem(IDC_STATIC_VIDEO)->GetSafeHwnd(), TempDC); picholder->Release(); pStm->Release(); CString strMsg; strMsg.Format("DataLen = %d, ExtInfo = %s", TmpVideo->dwVideoDataLen, TmpVideo->pszVideoExInfo); TempDC = ::GetDC(GetDlgItem(IDC_STATIC_VIDEO)->GetSafeHwnd()); SetBkMode(TempDC, 3); SetTextColor(TempDC, RGB(0, 128, 0)); Rectangle(TempDC, 10, 220, 700, 280); TextOut(TempDC, 20, 220, "HistoryVideoCallback JPEG FrameInfo:", 23); TextOut(TempDC, 20, 240, strMsg.GetBuffer(), strMsg.GetLength()); char* pszTime = strstr(TmpVideo->pszVideoExInfo, "FrameTime:"); DWORD64 dw64TimeMS = 0; if(pszTime) { sscanf(pszTime, "FrameTime:%I64u", &dw64TimeMS); } CTime cFrameTime(dw64TimeMS/1000); strMsg = cFrameTime.Format("%Y.%m.%d_%H:%M:%S"); TextOut(TempDC, 20, 260, strMsg.GetBuffer(), strMsg.GetLength()); ::ReleaseDC(GetDlgItem(IDC_STATIC_VIDEO)->GetSafeHwnd(), TempDC); SetWindowText(strMsg.GetBuffer()); if(((dw64TimeMS/1000) >= m_dwEndTime)&&m_realfinish == false) { ::SendMessage(GetSafeHwnd(), WM_MESSAGE_GETFINISH, 0, 0); } } break; } return S_OK; }
UINT Enconn::ConnectThread(LPVOID pParam)//接收线程 { int rcvLen = 0; int nDataLen = 0; int recvedlen =0; char pData[256] = {0}; unsigned char msgcmd=0; IPicture *pPic; IStream *pStm; HGLOBAL hMem; LPVOID lpBuf; HDC hdc; HWND hwnd; MSGHEAD head; CString finename; int pptindex=0; Enconn* pEnconn = (Enconn*) pParam; pEnconn->m_hSocket = socket(AF_INET,SOCK_STREAM,0); struct sockaddr_in client_addr; client_addr.sin_family = AF_INET; client_addr.sin_addr.s_addr = inet_addr(pEnconn->m_pAddr); client_addr.sin_port = htons(PORT); int nSize = 4000; pEnconn->PostMessage(RM_ST_CONNING,0);//给窗体发送消息 setsockopt(pEnconn->m_hSocket, SOL_SOCKET, SO_RCVTIMEO, (char *)&nSize ,sizeof(nSize)); //设置socket接收超时,4秒钟 if(connect(pEnconn->m_hSocket, (LPSOCKADDR)&client_addr, sizeof(client_addr)) != 0) { pEnconn->PostMessage(RM_ST_FAIL,NULL); closesocket(pEnconn->m_hSocket); pEnconn->m_hSocket=0; return 0; } pEnconn->Connflag=1; pEnconn->PostMessage(RM_ST_SUCCESS,0); if(pEnconn->Enctype==1)//ENC1200 { pEnconn->PackHeaderMSG((BYTE *)pData, MSG_PASSWORD, (HEAD_LEN+1+_tcslen(pEnconn->m_pPassword))); pData[HEAD_LEN]='A';//表示管理员 WideCharToMultiByte(CP_ACP,NULL,pEnconn->m_pPassword,_tcslen(pEnconn->m_pPassword),&pData[HEAD_LEN+1],8,NULL,FALSE); rcvLen=pEnconn->Send((char*)pData,HEAD_LEN+1+_tcslen(pEnconn->m_pPassword));//发送密码 } else//ENC110 { *(short*)(&pData)=htons(4+_tcslen(pEnconn->m_pPassword)); pData[2]=MSG_PASSWORD; pData[3]='A';//表示管理员 WideCharToMultiByte(CP_ACP,NULL,pEnconn->m_pPassword,_tcslen(pEnconn->m_pPassword),&pData[4],8,NULL,FALSE); rcvLen=pEnconn->Send((char*)pData,4+_tcslen(pEnconn->m_pPassword));//发送密码 } if(rcvLen==-1)goto Exit; while(pEnconn->m_hSocket>0) { int nn = GetTickCount(); if(pEnconn->Enctype==1)//ENC1200 { rcvLen=pEnconn->Recv((char*)pEnconn->m_cVideoBuffer, HEAD_LEN);//接收编码器的数据 } else//ENC110 { rcvLen=pEnconn->Recv((char*)pEnconn->m_cVideoBuffer, 2);//接收编码器的数据 } if(rcvLen == -1) { goto Exit; } if(pEnconn->Enctype==1)//ENC1200 { memcpy(&head,(pEnconn->m_cVideoBuffer),HEAD_LEN); nDataLen=ntohs(head.nLen)-HEAD_LEN; if(nDataLen < 0)goto Exit; rcvLen=pEnconn->Recv((char*)pEnconn->m_cVideoBuffer, nDataLen); msgcmd=head.nMsg; } else//ENC110 { nDataLen=htons(*((unsigned short*)(pEnconn->m_cVideoBuffer)))-2; if(nDataLen < 1)goto Exit; rcvLen=pEnconn->Recv((char*)pEnconn->m_cVideoBuffer, nDataLen); msgcmd=pEnconn->m_cVideoBuffer[0]; } if(rcvLen == -1) { goto Exit; } switch(msgcmd) { case MSG_CONNECTSUCC: (pEnconn->m_RichEdit)->SetWindowTextW(_T("链接成功")); break; case MSG_PASSWORD_ERR: pEnconn->PostMessage(RM_PASSWD_ERR,NULL); (pEnconn->m_RichEdit)->SetWindowTextW(_T("密码错误")); break; case MSG_MAXCLIENT_ERR: (pEnconn->m_RichEdit)->SetSel(-1,-1); (pEnconn->m_RichEdit)->ReplaceSel(_T("最大客户数\n"),0); break; case MSG_SCREENDATA: //(pEnconn->m_RichEdit)->SetSel(-1,-1); //(pEnconn->m_RichEdit)->ReplaceSel(_T("h264 Data\n"),0); //(pEnconn->m_RichEdit)->SetWindowTextW(_T("h264")); //printf("Recv h264 Data\n"); if(pEnconn->saveStream==1) { if(pEnconn->Enctype==1)//ENC1200 pEnconn->H264File.Write((char*)pEnconn->m_cVideoBuffer+sizeof(FRAMEHEAD),nDataLen-sizeof(FRAMEHEAD)); else pEnconn->H264File.Write((char*)pEnconn->m_cVideoBuffer+1+sizeof(DataHeader),nDataLen-1-sizeof(DataHeader)); } break; case MSG_AUDIODATA: //(pEnconn->m_RichEdit)->SetSel(-1,-1); //(pEnconn->m_RichEdit)->ReplaceSel(_T("AAC Data\n"),0); //printf("Recv AAC Data\n"); if(pEnconn->saveStream==1) { if(pEnconn->Enctype==1)//ENC1200 pEnconn->AacFile.Write((char*)pEnconn->m_cVideoBuffer+sizeof(FRAMEHEAD),nDataLen-sizeof(FRAMEHEAD)); else pEnconn->AacFile.Write((char*)pEnconn->m_cVideoBuffer+1+sizeof(DataHeader),nDataLen-1-sizeof(DataHeader)); } break; case MSG_SYSPARAMS: printf("Recv MSG_SYSPARAMS \n"); break; case MSG_TRACKAUTO: printf("Recv MSG_TRACKAUTO \n"); break; case MSG_DATAINFO: printf("Recv MSG_DATAINFO \n"); break; case MSG_LOW_BITRATE: printf("Recv LowStream ok \n"); break; case MSG_LOW_SCREENDATA: printf("Recv LowStream Data\n"); break; case MSG_PIC_DATA: //memcpy(&nDataLen, &pEnconn->m_cVideoBuffer[1],sizeof(nDataLen)); memcpy(&nDataLen, (char*)pEnconn->m_cVideoBuffer+1,sizeof(nDataLen)); printf("PPT索引图片=%d 字节\n", nDataLen); //hMem = ::GlobalAlloc(GMEM_MOVEABLE,nDataLen); //lpBuf = ::GlobalLock(hMem); rcvLen = pEnconn->Recv((char*)pEnconn->m_cVideoBuffer+4, nDataLen); finename.Format(_T("%d.jpg"),pptindex); if (!(pEnconn->JpgFile.Open(finename,CFile::modeCreate | CFile::modeWrite))) { printf("open jpegFile fail! \n"); pptindex++; break; } pEnconn->JpgFile.Write((char*)pEnconn->m_cVideoBuffer+4,nDataLen); pEnconn->JpgFile.Close(); pptindex++; hMem = ::GlobalAlloc(GMEM_MOVEABLE,nDataLen); lpBuf = ::GlobalLock(hMem); memcpy(lpBuf,(char*)pEnconn->m_cVideoBuffer+4,nDataLen); ::GlobalUnlock(hMem); ::CreateStreamOnHGlobal(hMem, TRUE, &pStm); //装入图形文件 if(SUCCEEDED(OleLoadPicture(pStm,nDataLen,TRUE,IID_IPicture,(LPVOID*)&pPic))) { OLE_XSIZE_HIMETRIC hmWidth; OLE_YSIZE_HIMETRIC hmHeight; pPic->get_Width(&hmWidth); //用接口方法获得图片的宽和高 pPic->get_Height(&hmHeight); printf("PPTJPEG=%d=%d\n",hmWidth,hmHeight); //获取桌面窗口句柄 hwnd = ::GetDesktopWindow(); //获取桌面窗口DC hdc = ::GetWindowDC(hwnd); CRect rect; CDC *pDC=CDC::FromHandle(hdc); //////显示原图大小///////////////////////////////////////////////////////////// CSize sz(hmWidth,hmHeight); pDC->HIMETRICtoDP(&sz); //转换MM_HIMETRIC模式单位为MM_TEXT像素单位 pPic->Render(hdc,80,80,sz.cx,sz.cy,0,hmHeight,hmWidth,-hmHeight,NULL); //按窗口尺寸显示/////////////////// GetClientRect(hwnd,&rect); if(pPic) pPic->Release(); ::ReleaseDC(hwnd, hdc); } pStm->Release(); printf("Recv PPT Data\n"); break; case MSG_GET_LOGOINFO: printf("Recv MSG_GET_LOGOINFO \n"); break; case MSG_PIC_DATAEX: printf("Recv MSG_PIC_DATAEX \n"); recvedlen=0; break; default: break; } } Exit: pEnconn->Connflag=0; pEnconn->PostMessage(RM_DISCONN,NULL); return 1; }
LRESULT COldVideoRecvTestDlg::OnProceVideo(WPARAM wParam, LPARAM lParam) { if(lParam == NULL) { return E_FAIL; } VIDEO_PACKET_DATA* TmpVideo = (VIDEO_PACKET_DATA*)lParam; switch(TmpVideo->dwVideoType) { case VIDEO_TYPE_H264_NORMAL_I: case VIDEO_TYPE_H264_NORMAL_P: { CString strMsg; strMsg.Format("DataLen = %d, ExtInfo = %s", TmpVideo->dwVideoDataLen, TmpVideo->pszVideoExInfo); HDC TempDC = ::GetDC(GetDlgItem(IDC_STATIC_VIDEO)->GetSafeHwnd()); SetBkMode(TempDC, 3); SetTextColor(TempDC, RGB(0, 128, 0)); Rectangle(TempDC, 10, 20, 700, 80); TextOut(TempDC, 20, 20, "H264CallBack FrameInfo:", 23); int ilen = strMsg.GetLength(); CString str1 = strMsg.Left(60); CString str2 = strMsg.Right(ilen - 60); TextOut(TempDC, 20, 40, str1.GetBuffer(), str1.GetLength()); TextOut(TempDC, 20, 60, str2.GetBuffer(), str2.GetLength()); //TextOut(TempDC, 20, 40, strMsg.GetBuffer(), strMsg.GetLength()); ::ReleaseDC(GetDlgItem(IDC_STATIC_VIDEO)->GetSafeHwnd(), TempDC); } break; case VIDEO_TYPE_H264_HISTORY_I: case VIDEO_TYPE_H264_HISTORY_P: { CString strMsg; strMsg.Format("DataLen = %d, ExtInfo = %s", TmpVideo->dwVideoDataLen, TmpVideo->pszVideoExInfo); HDC TempDC = ::GetDC(GetDlgItem(IDC_STATIC_VIDEO)->GetSafeHwnd()); SetBkMode(TempDC, 3); SetTextColor(TempDC, RGB(0, 128, 0)); Rectangle(TempDC, 10, 20, 700, 100); TextOut(TempDC, 20, 20, "H264CallBack FrameInfo:", 23); int ilen = strMsg.GetLength(); CString str1 = strMsg.Left(60); CString str2 = strMsg.Right(ilen - 60); TextOut(TempDC, 20, 40, str1.GetBuffer(), str1.GetLength()); TextOut(TempDC, 20, 60, str2.GetBuffer(), str2.GetLength()); //TextOut(TempDC, 20, 40, strMsg.GetBuffer(), strMsg.GetLength()); char* pszTime = strstr(TmpVideo->pszVideoExInfo, "FrameTime:"); DWORD64 dw64TimeMS = 0; if(pszTime) { sscanf(pszTime, "FrameTime:%I64u", &dw64TimeMS); CTime cFrameTime(dw64TimeMS/1000); strMsg = cFrameTime.Format("%Y.%m.%d_%H:%M:%S"); TextOut(TempDC, 20, 60, strMsg.GetBuffer(), strMsg.GetLength()); } ::ReleaseDC(GetDlgItem(IDC_STATIC_VIDEO)->GetSafeHwnd(), TempDC); if((dw64TimeMS/1000) >= m_dwEndTime) { ::SendMessage(GetSafeHwnd(), WM_MESSAGE_GETFINISH, 0, 0); } } break; case VIDEO_TYPE_JPEG_HISTORY: { PBYTE pJPEGPos = NULL; DWORD dwJPEGDataLen = 0; int iRedLightCount = 0; int iRedLightBufPosLen = sizeof(RECT) * 20; PBYTE pRedLightPosBuf = new BYTE[iRedLightBufPosLen]; if(HVAPIUTILS_ParseHistoryVideoFrame(TmpVideo->pVideoData, TmpVideo->dwVideoDataLen, iRedLightCount, iRedLightBufPosLen, pRedLightPosBuf, pJPEGPos, dwJPEGDataLen) != S_OK) { return E_FAIL; } if(pJPEGPos == NULL || dwJPEGDataLen <= 0) { return E_FAIL; } int iEnhanceBufLen = 0; PBYTE pEnhanceBuf = NULL; if(m_iRedLightEnhanceFlag == 1) { iEnhanceBufLen = (1024 << 10); pEnhanceBuf = new BYTE[iEnhanceBufLen]; if(pEnhanceBuf) { if(HVAPIUTILS_TrafficLightEnhance(pJPEGPos, dwJPEGDataLen, iRedLightCount, pRedLightPosBuf, pEnhanceBuf, iEnhanceBufLen, m_iBrightness, m_iHueThreshold, m_CompressRate) != S_OK) { delete[] pEnhanceBuf; pEnhanceBuf = NULL; iEnhanceBufLen = 0; } } } IStream* pStm = NULL; CreateStreamOnHGlobal(NULL, TRUE, &pStm); IPicture* picholder; LARGE_INTEGER liTempStar = {0}; pStm->Seek(liTempStar, STREAM_SEEK_SET, NULL); ULONG iWritten = NULL; if(pEnhanceBuf) pStm->Write(pEnhanceBuf, iEnhanceBufLen, &iWritten); else pStm->Write(pJPEGPos, dwJPEGDataLen, &iWritten); pStm->Seek(liTempStar, STREAM_SEEK_SET, NULL); if(pEnhanceBuf) { if(FAILED(OleLoadPicture(pStm, iEnhanceBufLen, TRUE, IID_IPicture, (void**)&picholder))) { pStm->Release(); return E_FAIL; } } else { if(FAILED(OleLoadPicture(pStm, dwJPEGDataLen, TRUE, IID_IPicture, (void**)&picholder))) { pStm->Release(); return E_FAIL; } } HDC TempDC; TempDC = ::GetDC(GetDlgItem(IDC_STATIC_VIDEO)->GetSafeHwnd()); OLE_XSIZE_HIMETRIC hmWidth; OLE_YSIZE_HIMETRIC hmHeight; picholder->get_Width(&hmWidth); picholder->get_Height(&hmHeight); int nWidth =MulDiv(hmWidth, GetDeviceCaps(TempDC, LOGPIXELSX), 2540); int nHeight = MulDiv(hmHeight, GetDeviceCaps(TempDC, LOGPIXELSY), 2540); picholder->Render(TempDC, 0, 0, m_ShowFrameRect.right-m_ShowFrameRect.left, m_ShowFrameRect.bottom-m_ShowFrameRect.top, 0, hmHeight, hmWidth, -hmHeight, NULL); ::ReleaseDC(GetDlgItem(IDC_STATIC_VIDEO)->GetSafeHwnd(), TempDC); picholder->Release(); pStm->Release(); CString strMsg; strMsg.Format("DataLen = %d, ExtInfo = %s", TmpVideo->dwVideoDataLen, TmpVideo->pszVideoExInfo); TempDC = ::GetDC(GetDlgItem(IDC_STATIC_VIDEO)->GetSafeHwnd()); SetBkMode(TempDC, 3); SetTextColor(TempDC, RGB(0, 128, 0)); Rectangle(TempDC, 10, 220, 700, 280); TextOut(TempDC, 20, 220, "HistoryVideoCallback JPEG FrameInfo:", 23); TextOut(TempDC, 20, 240, strMsg.GetBuffer(), strMsg.GetLength()); char* pszTime = strstr(TmpVideo->pszVideoExInfo, "FrameTime:"); DWORD64 dw64TimeMS = 0; if(pszTime) { sscanf(pszTime, "FrameTime:%I64u", &dw64TimeMS); } CTime cFrameTime(dw64TimeMS/1000); strMsg = cFrameTime.Format("%Y.%m.%d_%H:%M:%S"); TextOut(TempDC, 20, 260, strMsg.GetBuffer(), strMsg.GetLength()); ::ReleaseDC(GetDlgItem(IDC_STATIC_VIDEO)->GetSafeHwnd(), TempDC); SetWindowText(strMsg.GetBuffer()); if((dw64TimeMS/1000) >= m_dwEndTime) { ::SendMessage(GetSafeHwnd(), WM_MESSAGE_GETFINISH, 0, 0); } } break; } return S_OK; }
__declspec(dllexport) bool ConvertWMFtoPNGA(char *bwmfName, char *bpngName, bool eraseTransparenz) { FILE *wmfFile, *pngFile; HGLOBAL hGlobal; DWORD dwSize; IPicture *image; int width, height; long hmWidth, hmHeight; BYTE *p1, *p2; int x, y; if (!(wmfFile = fopen(wmfName,"rb"))) return false; fseek(wmfFile, 0, SEEK_END); dwSize = ftell(wmfFile); fseek(wmfFile, 0, SEEK_SET); hGlobal = GlobalAlloc(GMEM_MOVEABLE | GMEM_NODISCARD, dwSize); if (!hGlobal) { fclose(wmfFile); return false; } char *pData = reinterpret_cast<char*>(GlobalLock(hGlobal)); if (!pData) { GlobalFree(hGlobal); fclose(wmfFile); return false; } if (fread(pData,1,dwSize,wmfFile) != dwSize) { GlobalFree(hGlobal); fclose(wmfFile); return false; } GlobalUnlock(hGlobal); fclose(wmfFile); IStream *pStream = NULL; if (CreateStreamOnHGlobal(hGlobal,TRUE,&pStream) != S_OK) return false; if (OleLoadPicture(pStream,dwSize,false,IID_IPicture, reinterpret_cast<LPVOID *>(&image)) != S_OK) { pStream->Release(); return false; } pStream->Release(); GlobalFree(hGlobal); image->get_Width(&hmWidth); image->get_Height(&hmHeight); width = MulDiv(hmWidth, 96, 2540); height = MulDiv(hmHeight, 96, 2540); BITMAPINFO *bitmapInfo = (BITMAPINFO *)NULL; bitmapInfo = (BITMAPINFO *)LocalAlloc(LMEM_FIXED|LMEM_ZEROINIT, sizeof(BITMAPINFO) ); bitmapInfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER); bitmapInfo->bmiHeader.biWidth = width; bitmapInfo->bmiHeader.biHeight = height; bitmapInfo->bmiHeader.biPlanes = 1; bitmapInfo->bmiHeader.biBitCount = 32; bitmapInfo->bmiHeader.biCompression = BI_RGB; bitmapInfo->bmiHeader.biSizeImage = width * height * 4; PBYTE bitmapData1; HDC myHdc1 = CreateCompatibleDC(NULL); HBITMAP bitmap1 = CreateDIBSection(myHdc1, bitmapInfo, DIB_RGB_COLORS, (PVOID *) &bitmapData1, NULL, 0); SetStretchBltMode(myHdc1, HALFTONE); SelectObject(myHdc1, bitmap1); memset(bitmapData1, 0x00ffffff, 4 * width * height); PBYTE bitmapData2; HDC myHdc2 = CreateCompatibleDC(NULL); SetStretchBltMode(myHdc2, HALFTONE); HBITMAP bitmap2 = CreateDIBSection(myHdc2, bitmapInfo, DIB_RGB_COLORS, (PVOID *) &bitmapData2, NULL, 0); SelectObject(myHdc2, bitmap2); memset(bitmapData2, 0x00000000, 4 * width * height); image->Render(myHdc1, 0, 0, width, height, 0, hmHeight, hmWidth, -hmHeight, NULL); image->Render(myHdc2, 0, 0, width, height, 0, hmHeight, hmWidth, -hmHeight, NULL); int offTop = 0; int offLeft = 0; int offBottom = 0; int offRight = 0; if (eraseTransparenz) { for (y = 0; y < height; ++y) { p1 = bitmapData1 + (width*4*y); p2 = bitmapData2 + (width*4*y); for (x = 0; x < width; ++x) { transparent = true; if (*p1 == *p2) { if (*(p1+1) == *(p2+1)) { if (*(p1+2) == *(p2+2)) { transparent = false; break; } } } p1 += 4; p2 += 4; } if (!transparent) break; } offTop = y; for (x = 0; x < width; ++x) { p1 = bitmapData1 + (x*4); p2 = bitmapData2 + (x*4); for (y = 0; y < height; ++y) { transparent = true; if (*p1 == *p2) { if (*(p1+1) == *(p2+1)) { if (*(p1+2) == *(p2+2)) { transparent = false; break; } } } p1 += (4*width); p2 += (4*width); } if (!transparent) break; } offLeft = x; for (y = height-1; y >= 0; --y) { p1 = bitmapData1 + y * (width * 4); p2 = bitmapData2 + y * (width * 4); for (x = 0; x < width; ++x) { transparent = true; if (*p1 == *p2) { if (*(p1+1) == *(p2+1)) { if (*(p1+2) == *(p2+2)) { transparent = false; break; } } } p1 += 4; p2 += 4; } if (!transparent) break; } offBottom = height - y - 1; transparent = true; for (x = width-1; x >= 0; --x) { p1 = bitmapData1 + x*4; p2 = bitmapData2 + x*4; for (y = 0; y < height; ++y) { if (*p1 == *p2) { if (*(p1+1) == *(p2+1)) { if (*(p1+2) == *(p2+2)) { transparent = false; break; } } } p1 += width*4; p2 += width*4; } if (!transparent) break; } offRight = width - x - 1; } png_structp png_ptr; png_infop info_ptr; pngFile = fopen(pngName, "wb"); if (pngFile == NULL) return false; png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, (void *)pngName, NULL, NULL); if (png_ptr == NULL) { fclose(pngFile); return false; } info_ptr = png_create_info_struct(png_ptr); if (info_ptr == NULL) { fclose(pngFile); png_destroy_write_struct(&png_ptr, png_infopp_NULL); return false; } if (setjmp(png_jmpbuf(png_ptr))) { fclose(pngFile); png_destroy_write_struct(&png_ptr, &info_ptr); return false; } png_init_io(png_ptr, pngFile); int pngWidth = width - (offLeft + offRight); int pngHeight = height - (offTop + offBottom); png_set_IHDR(png_ptr, info_ptr, pngWidth, pngHeight, 8, PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE); int row_stride = 4 * pngWidth; info_ptr->pixel_depth = 32; info_ptr->channels = 4; info_ptr->bit_depth = 8; info_ptr->color_type = PNG_COLOR_TYPE_RGB_ALPHA; info_ptr->rowbytes = row_stride; png_write_info(png_ptr, info_ptr); BYTE *row_pointers = new BYTE[10+row_stride]; BYTE *q; for (y = 0; y < pngHeight; ++y) { q = row_pointers; p1 = bitmapData1 + (((height - 1) - (y + offBottom)) * width + offLeft) * 4; p2 = bitmapData2 + (((height - 1) - (y + offBottom)) * width + offLeft) * 4; for (x = 0; x < pngWidth; ++x) { transparent = true; if (*p1 == *p2) { if (*(p1+1) == *(p2+1)) { if (*(p1+2) == *(p2+2)) { transparent = false; } } } *q = *(p1+2); ++q; *q = *(p1+1); ++q; *q = *(p1); ++q; if (transparent) *q = 0; else *q = 255; ++q; p1 += 4; p2 += 4; } png_write_row(png_ptr, row_pointers); } delete [] row_pointers; png_write_end(png_ptr, info_ptr); png_destroy_write_struct(&png_ptr, &info_ptr); fclose(pngFile); if (bitmapInfo) LocalFree(bitmapInfo); DeleteObject(bitmap1); DeleteDC(myHdc1); DeleteObject(bitmap2); DeleteDC(myHdc2); return true; }
// load an image from file. The supported filetypes depend on the plattform bool LoadImageFromFile(const char *filename, CByteImage *pImage) { HDC hdcTemp; // The DC To Hold Our Bitmap HBITMAP hbmpTemp; // Holds The Bitmap Temporarily IPicture *pPicture; // IPicture Interface OLECHAR wszPath[MAX_PATH+1]; // Full Path To Picture (WCHAR) long lWidth; // Width In Logical Units long lHeight; // Height In Logical Units long lWidthPixels; // Width In Pixels long lHeightPixels; // Height In Pixels MultiByteToWideChar(CP_ACP, 0, filename, -1, wszPath, MAX_PATH); // Convert From ASCII To Unicode HRESULT hr = OleLoadPicturePath(wszPath, 0, 0, 0, IID_IPicture, (void**)&pPicture); if(FAILED(hr)) // If Loading Failed return false; // Return False hdcTemp = CreateCompatibleDC(GetDC(0)); // Create The Windows Compatible Device Context if(!hdcTemp) // Did Creation Fail? { pPicture->Release(); // Decrements IPicture Reference Count return false; // Return False (Failure) } pPicture->get_Width(&lWidth); // Get IPicture Width (Convert To Pixels) lWidthPixels = MulDiv(lWidth, GetDeviceCaps(hdcTemp, LOGPIXELSX), 2540); pPicture->get_Height(&lHeight); // Get IPicture Height (Convert To Pixels) lHeightPixels = MulDiv(lHeight, GetDeviceCaps(hdcTemp, LOGPIXELSY), 2540); // Create A Temporary Bitmap BITMAPINFO bi = {0}; // The Type Of Bitmap We Request DWORD *pBits = 0; // Pointer To The Bitmap Bits bi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); // Set Structure Size bi.bmiHeader.biBitCount = 32; // 32 Bit bi.bmiHeader.biWidth = lWidthPixels; // Power Of Two Width bi.bmiHeader.biHeight = lHeightPixels; // Make Image Top Up (Positive Y-Axis) bi.bmiHeader.biCompression = BI_RGB; // RGB Encoding bi.bmiHeader.biPlanes = 1; // 1 Bitplane // Creating A Bitmap This Way Allows Us To Specify Color Depth And Gives Us Imediate Access To The Bits hbmpTemp = CreateDIBSection(hdcTemp, &bi, DIB_RGB_COLORS, (void**)&pBits, 0, 0); if(!hbmpTemp) // Did Creation Fail? { DeleteDC(hdcTemp); // Delete The Device Context pPicture->Release(); // Decrements IPicture Reference Count return false; // Return False (Failure) } SelectObject(hdcTemp, hbmpTemp); // Select Handle To Our Temp DC And Our Temp Bitmap Object // Render The IPicture On To The Bitmap pPicture->Render(hdcTemp, 0, 0, lWidthPixels, lHeightPixels, 0, 0, lWidth, lHeight, 0); if (pImage->m_bOwnMemory) delete [] pImage->pixels; pImage->width = lWidthPixels; pImage->height = lHeightPixels; pImage->bytesPerPixel = 3; pImage->type = CByteImage::eRGB24; pImage->pixels = new unsigned char[pImage->bytesPerPixel * lWidthPixels*lHeightPixels]; pImage->m_bOwnMemory = true; unsigned char *output = pImage->pixels; // Convert From BGR To RGB Format And Add An Alpha Value Of 255 for(long i = 0; i < lWidthPixels * lHeightPixels; i++) // Loop Through All Of The Pixels { BYTE* pPixel = (BYTE*)(&pBits[i]); // Grab The Current Pixel output[3*i] = pPixel[2]; // Store 1st Color In Temp Variable (Blue) output[3*i + 1] = pPixel[1]; // Move Red Value To Correct Position (1st) output[3*i + 2] = pPixel[0]; // Move Temp Value To Correct Blue Position (3rd) } DeleteObject(hbmpTemp); // Delete The Object DeleteDC(hdcTemp); // Delete The Device Context pPicture->Release(); // Decrements IPicture Reference Count return true; }
bool CPlugin::PaintImage(HDC hPaintDC) { if (!m_hImageMemory) return false; // Create an IStream from the file in memory IStream* pStreamIn = NULL; HRESULT hr = ::CreateStreamOnHGlobal(m_hImageMemory, false/*fDeleteOnRelease*/, &pStreamIn); if (FAILED(hr) || !pStreamIn) return false; // Create an IPicture from the IStream; the function opens JPEG's, BMP's, GIF's, WMF's, EMF's, and ICO's!! DWORD dwSize = ::GlobalSize(m_hImageMemory); IPicture* pPicture = NULL; hr = ::OleLoadPicture(pStreamIn, dwSize, false, IID_IPicture, (void**)&pPicture); // Either way we are done with the IStream and the memory pStreamIn->Release(); if (FAILED(hr) || !pPicture) return false; // Get the screen dimensions for the HIMETRIC conversion HDC hDC = ::CreateIC("DISPLAY", NULL, NULL, NULL); long lResolutionX = GetDeviceCaps(hDC, LOGPIXELSX); long lResolutionY = GetDeviceCaps(hDC, LOGPIXELSY); ::DeleteDC(hDC); // Get width and height of the picture #define HIMETRIC_INCH 2540 long hmWidth = 0; hr = pPicture->get_Width(&hmWidth); int bmWidth = ::MulDiv(hmWidth, lResolutionX, HIMETRIC_INCH); long hmHeight = 0; hr = pPicture->get_Height(&hmHeight); int bmHeight = ::MulDiv(hmHeight, lResolutionY, HIMETRIC_INCH); RECT rect; ::GetClientRect(m_hWnd, &rect); m_HotRect.left = ((rect.right - rect.left) - bmWidth) / 2; m_HotRect.top = ((rect.bottom - rect.top) - bmHeight) / 2; m_HotRect.right = m_HotRect.left + bmWidth; m_HotRect.bottom = m_HotRect.top + bmHeight; // Render the picture into the DC hr = pPicture->Render( hPaintDC, // HDC the device context on which to render the image m_HotRect.left, // long left position of destination in HDC m_HotRect.top, // long top position of destination in HDC bmWidth, // long width of destination in HDC bmHeight, // long height of destination in HDC 0, // OLE_XPOS_HIMETRIC left position in source picture hmHeight, // OLE_YPOS_HIMETRIC top position in source picture hmWidth, // OLE_XSIZE_HIMETRIC width in source picture -hmHeight, // OLE_YSIZE_HIMETRIC height in source picture NULL // LPCRECT pointer to destination for a metafile hdc ); pPicture->Release(); return SUCCEEDED(hr); }
int _tmain(int argc, _TCHAR* argv[]) { if( argc < 2 ) { _tprintf(_T("converts a picture to a tank map\n")); _tprintf(_T("supported formats: bmp, gif, jpg, wmf, ico\n")); _tprintf(_T("using: mkmap bmpfile.bmp [mapfile]\n")); return 0; } _tprintf(_T("input file: \n %s\n"), argv[1]); IPicture *pPicture = LoadPicture(argv[1]); if( NULL == pPicture ) { _tprintf(_T("Loading FAILED!\n")); return -1; } HDC hdc = GetDC(NULL); long height, width; pPicture->get_Height(&height); pPicture->get_Width(&width); long height_ = MulDiv(height, GetDeviceCaps(hdc, LOGPIXELSY), HIMETRIC_INCH); long width_ = MulDiv(width, GetDeviceCaps(hdc, LOGPIXELSX), HIMETRIC_INCH); _tprintf(_T("width %d px\n"), width_); _tprintf(_T("height %d px\n"), height_); HDC hdc_tmp = CreateCompatibleDC(hdc); HBITMAP bmp = CreateCompatibleBitmap(hdc, width_, height_); ReleaseDC(NULL, hdc); HBITMAP old = (HBITMAP) SelectObject(hdc_tmp, bmp); pPicture->Render(hdc_tmp, 0, 0, width_, height_, 0, height, width, -height, NULL); _TCHAR outfn[MAX_PATH]; if( argc > 2 ) _tcscpy(outfn, argv[2]); else _stprintf(outfn, _T("%s.tankmap"), argv[1]); FILE *file = _tfopen(outfn, _T("wt")); if( NULL != file ) { _tprintf(_T("writing map to '%s'\n"), outfn); try { fwassert(_ftprintf(file, _T("<tank map>\n"))); fwassert(_ftprintf(file, _T("width %d\n"), width_)); fwassert(_ftprintf(file, _T("height %d\n"), height_)); _TCHAR name[MAX_OBJNAME]; int objcount = 0; for( int y = 0; y < height_; y++ ) for( int x = 0; x < width_; x++ ) { if( ObjectFromColor(name, GetPixel(hdc_tmp, x, y)) ) { fwassert(_ftprintf(file, _T("%s %d %d\n"), name, 16 + x * 32, 16 + y * 32)); objcount++; } } _tprintf(_T("%d objects written\n"), objcount); _tprintf(_T("done.\n"), objcount); } catch(const _TCHAR *msg) { fclose(file); _tprintf(msg); } } else { _tprintf(_T("can't open file '%s' for writing\n"), outfn); } SelectObject(hdc_tmp, old); DeleteObject(bmp); DeleteDC(hdc_tmp); SAFE_RELEASE(pPicture); return 0; }
CTextureLoader::sRgbResult CTextureLoader::readPixels(char *file) { sRgbResult rgbResult; if (!file) { return rgbResult; } HDC hdcTemp; HBITMAP hbmpTemp; IPicture *pPicture; OLECHAR wszPath[MAX_PATH+1]; char szPath[MAX_PATH+1]; long lWidth; long lHeight; long lWidthPixels; long lHeightPixels; GLint glMaxTexDim ; if (strstr(file, "http://")) { strcpy(szPath, file); } else { if (strstr(file,":")==NULL) { GetCurrentDirectory(MAX_PATH, szPath); strcat(szPath, "\\"); strcat(szPath, file); } else { strcpy(szPath,file); } } MultiByteToWideChar(CP_ACP, 0, szPath, -1, wszPath, MAX_PATH); HRESULT hr = OleLoadPicturePath(wszPath, 0, 0, 0, IID_IPicture, (void**)&pPicture); if(FAILED(hr)) { return rgbResult; } hdcTemp = CreateCompatibleDC(GetDC(0)); if(!hdcTemp) { pPicture->Release(); return rgbResult; } glGetIntegerv(GL_MAX_TEXTURE_SIZE, &glMaxTexDim); pPicture->get_Width(&lWidth); lWidthPixels = MulDiv(lWidth, GetDeviceCaps(hdcTemp, LOGPIXELSX), 2540); pPicture->get_Height(&lHeight); lHeightPixels= MulDiv(lHeight, GetDeviceCaps(hdcTemp, LOGPIXELSY), 2540); // Resize Image To Closest Power Of Two if (lWidthPixels <= glMaxTexDim) { lWidthPixels = 1 << (int)floor((log((double)lWidthPixels)/log(2.0f)) + 0.5f); } else { lWidthPixels = glMaxTexDim; } if (lHeightPixels <= glMaxTexDim) { lHeightPixels = 1 << (int)floor((log((double)lHeightPixels)/log(2.0f)) + 0.5f); } else { lHeightPixels = glMaxTexDim; } // Create A Temporary Bitmap BITMAPINFO bi = {0}; DWORD *pBits = 0; bi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); bi.bmiHeader.biBitCount = 32; bi.bmiHeader.biWidth = lWidthPixels; bi.bmiHeader.biHeight = lHeightPixels; bi.bmiHeader.biCompression = BI_RGB; bi.bmiHeader.biPlanes = 1; hbmpTemp = CreateDIBSection(hdcTemp, &bi, DIB_RGB_COLORS, (void**)&pBits, 0, 0); if(!hbmpTemp) { DeleteDC(hdcTemp); pPicture->Release(); return rgbResult; } SelectObject(hdcTemp, hbmpTemp); pPicture->Render(hdcTemp, 0, 0, lWidthPixels, lHeightPixels, 0, lHeight, lWidth, -lHeight, 0); rgbResult.resetSize(lWidthPixels); for(long i = 0; i < lWidthPixels * lHeightPixels; i++) { BYTE* pPixel = (BYTE*)(&pBits[i]); BYTE temp = pPixel[0]; pPixel[0] = pPixel[2]; pPixel[2] = temp; rgbResult.rgb[i]=sRgbPixel(pPixel); } return rgbResult; }
CTextureLoader::sLoaderResult CTextureLoader::buildTexture(const char *file, GLuint &texture, bool trans, GLint texture_filter, bool auto_transparent, GLubyte R, GLubyte G, GLubyte B, bool full_path) { sLoaderResult lr; if (!file) { return lr; } HDC hdcTemp; HBITMAP hbmpTemp; IPicture *pPicture; OLECHAR wszPath[MAX_PATH+1]; char szPath[MAX_PATH+1]; long lWidth; long lHeight; long lWidthPixels; long lHeightPixels; GLint glMaxTexDim ; if (strstr(file, "http://")) { strcpy(szPath, file); } else { if (strstr(file,":")==NULL) { GetCurrentDirectory(MAX_PATH, szPath); strcat(szPath, "\\"); strcat(szPath, file); } else { strcpy(szPath,file); } } MultiByteToWideChar(CP_ACP, 0, szPath, -1, wszPath, MAX_PATH); HRESULT hr = OleLoadPicturePath(wszPath, 0, 0, 0, IID_IPicture, (void**)&pPicture); if(FAILED(hr)) { return lr; } hdcTemp = CreateCompatibleDC(GetDC(0)); if(!hdcTemp) { pPicture->Release(); return lr; } glGetIntegerv(GL_MAX_TEXTURE_SIZE, &glMaxTexDim); pPicture->get_Width(&lWidth); lWidthPixels = MulDiv(lWidth, GetDeviceCaps(hdcTemp, LOGPIXELSX), 2540); pPicture->get_Height(&lHeight); lHeightPixels= MulDiv(lHeight, GetDeviceCaps(hdcTemp, LOGPIXELSY), 2540); lr.texture_dimensions.x=lWidthPixels; lr.texture_dimensions.y=lHeightPixels; // Resize Image To Closest Power Of Two if (lWidthPixels <= glMaxTexDim) { lWidthPixels = 1 << (int)floor((log((double)lWidthPixels)/log(2.0f)) + 0.5f); } else { lWidthPixels = glMaxTexDim; } if (lHeightPixels <= glMaxTexDim) { lHeightPixels = 1 << (int)floor((log((double)lHeightPixels)/log(2.0f)) + 0.5f); } else { lHeightPixels = glMaxTexDim; } // Create A Temporary Bitmap BITMAPINFO bi = {0}; DWORD *pBits = 0; bi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); bi.bmiHeader.biBitCount = 32; bi.bmiHeader.biWidth = lWidthPixels; bi.bmiHeader.biHeight = lHeightPixels; bi.bmiHeader.biCompression = BI_RGB; bi.bmiHeader.biPlanes = 1; hbmpTemp = CreateDIBSection(hdcTemp, &bi, DIB_RGB_COLORS, (void**)&pBits, 0, 0); if(!hbmpTemp) { DeleteDC(hdcTemp); pPicture->Release(); return lr; } SelectObject(hdcTemp, hbmpTemp); pPicture->Render(hdcTemp, 0, 0, lWidthPixels, lHeightPixels, 0, lHeight, lWidth, -lHeight, 0); bool first=false; GLuint r,g,b; for(long i = 0; i < lWidthPixels * lHeightPixels; i++) { BYTE* pPixel = (BYTE*)(&pBits[i]); BYTE temp = pPixel[0]; pPixel[0] = pPixel[2]; pPixel[2] = temp; if (!first) { r=pPixel[0]; g=pPixel[1]; b=pPixel[2]; first=true; } if (trans) { if (auto_transparent) { if ((pPixel[0]==r) && (pPixel[1]==g) && (pPixel[2]==b)) { pPixel[3]=0; } else { pPixel[3]=255; } } else { if ((pPixel[0]==R) && (pPixel[1]==G) && (pPixel[2]==B)) { pPixel[3]=0; } else { pPixel[3]=255; } } } else { pPixel[3]=255; } } glGenTextures(1, &texture); glBindTexture(GL_TEXTURE_2D, texture); /*glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,texture_filter); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,texture_filter); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, lWidthPixels, lHeightPixels, 0, GL_RGBA, GL_UNSIGNED_BYTE, pBits);*/ glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST); gluBuild2DMipmaps(GL_TEXTURE_2D, 4, lWidthPixels, lHeightPixels, GL_RGBA, GL_UNSIGNED_BYTE, pBits); DeleteObject(hbmpTemp); DeleteDC(hdcTemp); pPicture->Release(); lr.done=true; return lr; }