//旋转图片 VOID CImageEditorControl::SetRoundImage(BYTE cbRoundType) { //状态效验 ASSERT(m_ImageSource.IsNull()==false); if (m_ImageSource.IsNull()==true) return; //构造位置 CSize SizeSource(m_ImageSource.GetWidth(),m_ImageSource.GetHeight()); CSize SizeResult(m_ImageSource.GetHeight(),m_ImageSource.GetWidth()); //创建位图 CImage ImageSource; CImage ImageResult; ImageSource.Create(m_ImageSource.GetWidth(),m_ImageSource.GetHeight(),32); ImageResult.Create(m_ImageSource.GetHeight(),m_ImageSource.GetWidth(),32); //绘画位图 CDC * pDCResult=CDC::FromHandle(ImageResult.GetDC()); CDC * pDCSource=CDC::FromHandle(ImageSource.GetDC()); m_ImageSource.BitBlt(pDCSource->m_hDC,0,0,m_ImageSource.GetWidth(),m_ImageSource.GetHeight(),0,0); //获取属性 INT nPitchResult=ImageResult.GetPitch(); INT nPitchSource=ImageSource.GetPitch(); //获取数据 LPBYTE cbBitResult=(LPBYTE)ImageResult.GetBits(); LPBYTE cbBitSource=(LPBYTE)ImageSource.GetBits(); //创建区域 for (INT nYSourcePos=0;nYSourcePos<SizeSource.cy;nYSourcePos++) { for (INT nXSourcePos=0;nXSourcePos<SizeSource.cx;nXSourcePos++) { //目标位置 INT nXResultPos=(cbRoundType==ROUND_TYPE_LEFT)?nYSourcePos:(SizeSource.cy-nYSourcePos-1); INT nYResultPos=(cbRoundType==ROUND_TYPE_LEFT)?(SizeSource.cx-nXSourcePos-1):nXSourcePos; //设置颜色 DWORD dwSource=nYSourcePos*nPitchSource+nXSourcePos*4L; DWORD dwResult=nYResultPos*nPitchResult+nXResultPos*4L; *(COLORREF *)(cbBitResult+dwResult)=*(COLORREF *)(cbBitSource+dwSource); } } //释放对象 ImageSource.ReleaseDC(); ImageResult.ReleaseDC(); //更新图片 if (LoadEditImage(ImageResult)==false) { ASSERT(FALSE); return; } return; }
//绘画头像 VOID CFaceItemControl::DrawFaceNormal(CDC * pDC, INT nXPos, INT nYPos, INT nWidth, INT nHeight, DWORD dwCustomFace[FACE_CX*FACE_CY]) { //创建位图 CImage ImageCustomFace; ImageCustomFace.Create(FACE_CX,FACE_CY,32); //获取数据 INT nImagePitch=ImageCustomFace.GetPitch(); LPBYTE cbBitCustomFace=(LPBYTE)ImageCustomFace.GetBits(); //创建区域 for (INT nYImagePos=0;nYImagePos<FACE_CY;nYImagePos++) { for (INT nXImagePos=0;nXImagePos<FACE_CX;nXImagePos++) { //设置颜色 DWORD dwImageTarget=nYImagePos*nImagePitch+nXImagePos*4L; *(COLORREF *)(cbBitCustomFace+dwImageTarget)=dwCustomFace[nYImagePos*FACE_CX+nXImagePos]; } } //绘画界面 ImageCustomFace.Draw(pDC->m_hDC,nXPos,nYPos,nWidth,nHeight); return; }
bool CImageEx::SetGray() { CImage *pImage = &m_ImageClone; if ( pImage == NULL && pImage->IsNull() ) { pImage = this; } int nWidth = pImage->GetWidth(); int nHeight = pImage->GetHeight(); BYTE* pArray = (BYTE*)pImage->GetBits(); int nPitch = pImage->GetPitch(); int nBitCount = pImage->GetBPP() / 8; for (int i = 0; i < nHeight; i++) { for (int j = 0; j < nWidth; j++) { int grayVal = (BYTE)(((*(pArray + nPitch * i + j * nBitCount) * 306) + (*(pArray + nPitch * i + j * nBitCount + 1) * 601) + (*(pArray + nPitch * i + j * nBitCount + 2) * 117) + 512 ) >> 10); // 计算灰度值 *(pArray + nPitch * i + j * nBitCount) = grayVal; // 赋灰度值 *(pArray + nPitch * i + j * nBitCount + 1) = grayVal; *(pArray + nPitch * i + j * nBitCount + 2) = grayVal; } } return true; }
//绘画头像 VOID CFaceItemControl::DrawFaceOffLine(CDC * pDC, INT nXPos, INT nYPos, INT nWidth, INT nHeight, DWORD dwCustomFace[FACE_CX*FACE_CY]) { //创建位图 CImage ImageCustomFace; ImageCustomFace.Create(FACE_CX,FACE_CY,32); //获取数据 INT nImagePitch=ImageCustomFace.GetPitch(); LPBYTE cbBitCustomFace=(LPBYTE)ImageCustomFace.GetBits(); //创建区域 for (INT nYImagePos=0;nYImagePos<FACE_CY;nYImagePos++) { for (INT nXImagePos=0;nXImagePos<FACE_CX;nXImagePos++) { //设置颜色 DWORD dwImageTarget=nYImagePos*nImagePitch+nXImagePos*4L; COLORREF crImageTarget=dwCustomFace[nYImagePos*FACE_CX+nXImagePos]; //提取颜色 BYTE cbColorR=GetRValue(crImageTarget); BYTE cbColorG=GetGValue(crImageTarget); BYTE cbColorB=GetBValue(crImageTarget); BYTE cbColorGray=(BYTE)(cbColorR*0.30+cbColorG*0.59+cbColorB*0.11); //设置颜色 *(COLORREF *)(cbBitCustomFace+dwImageTarget)=RGB(cbColorGray,cbColorGray,cbColorGray); } } //绘画界面 ImageCustomFace.Draw(pDC->m_hDC,nXPos,nYPos,nWidth,nHeight); return; }
double img_get_pxl(int img_no, int i, int j, int channel) { // Reading input texture sample CImage* pImage = saved_images[img_no]; unsigned char *pData = (unsigned char*)pImage->GetBits(); int pitch = pImage->GetPitch(); int byte_pp = pImage->GetBPP() / 8; //fout << "Initial sizeof: " << sizeof(pData) << std::endl; if (pitch < 0) { //fout << "NEGATIVE; pitch = " << pitch << std::endl; pData += pitch * (image_shapes[img_no].second - 1); //pitch = -pitch; } /*for (int i = 0; i < pImage->GetHeight(); i++) // Image lines { for (int j = 0; j < pImage->GetWidth(); j++) // Pixels in line { unsigned char b = pCurrentLine[j * 4]; unsigned char g = pCurrentLine[j * 4 + 1]; unsigned char r = pCurrentLine[j * 4 + 2]; unsigned char alpha = pCurrentLine[j * 4 + 3]; } pCurrentLine += pitch; }*/ //fout << i << ' ' << pitch << ' ' << j << ' ' << byte_pp << ' ' << channel << std::endl << i * pitch + j * byte_pp + channel << // ' ' << sizeof(pData) << std::endl; unsigned char *pxl_addr = (unsigned char *)pImage->GetPixelAddress(i, j); // j, i? double pxl = double(*(pxl_addr + channel)); return pxl; }
void Csmoothorsharp::FilterImg(int* temp,CImage imga){ int nW=imga.GetWidth()-1; int nH=imga.GetHeight()-1; byte *pRealData; pRealData=(byte*)imga.GetBits(); int pit=imga.GetPitch(); int bitCount=imga.GetBPP()/8; for (int i=1;i<nH;i++) { for (int j=1;j<nW;j++) { int Rval=0,Gval=0,Bval=0,indx=0; for ( int row=-1;row<=1;row++) { for (int col=-1;col<=1;col++) { Bval+=((int)(int)(*(pRealData+pit*(i+row)+(j+col)*bitCount))) *temp[indx]; Gval=((int)(int)(*(pRealData+pit*(i+row)+(j+col)*bitCount+1)))*temp[indx]; Rval=((int)(int)(*(pRealData+pit*(i+row)+(j+col)*bitCount+2)))*temp[indx]; indx++; } } //template work is over,You need get the center point pixel. Rval=(int)(Rval*cval); if (Rval>255) Rval=255; else if(Rval<0) Rval=0; Gval=(int)(Gval*cval); if (Gval>255) Gval=255; else if(Gval<0) Gval=0; Bval=(int)(Bval*cval); if (Bval>255) Bval=255; else if (Bval<0) Bval=0; //ok,write the new pixel into the pic. *(pRealData+pit*(i-1)+(j-1)*bitCount)=Bval; *(pRealData+pit*(i-1)+(j-1)*bitCount+1)=Gval; *(pRealData+pit*(i-1)+(j-1)*bitCount+2)=Rval; } } //CALL the OnDraw in MFC }
//确定函数 VOID CDlgCustomFace::OnOK() { //连接判断 bool bConnect=false; //系统模式 if (m_cbMode==MM_SYSTEM) { CGlobalUserInfo * pGlobalUserInfo=CGlobalUserInfo::GetInstance(); bConnect=(pGlobalUserInfo->GetGlobalUserData()->wFaceID!=m_wFaceID); } //自定模式 if (m_cbMode==MM_CUSTOM) { //创建缓冲 CImage ImageCustomFace; ImageCustomFace.Create(FACE_CX,FACE_CY,32); //创建 DC CImageDC BufferDC(ImageCustomFace); m_FaceItemCustomWnd.DrawEditImage(CDC::FromHandle(BufferDC),0,0,FACE_CX,FACE_CY); //获取数据 INT nImagePitch=ImageCustomFace.GetPitch(); LPBYTE cbBitCustomFace=(LPBYTE)ImageCustomFace.GetBits(); //创建区域 for (INT nYImagePos=0;nYImagePos<FACE_CY;nYImagePos++) { for (INT nXImagePos=0;nXImagePos<FACE_CX;nXImagePos++) { //设置颜色 DWORD dwImageTarget=nYImagePos*nImagePitch+nXImagePos*4L; m_CustomFaceInfo.dwCustomFace[nYImagePos*FACE_CX+nXImagePos]=*(COLORREF *)(cbBitCustomFace+dwImageTarget); } } //设置变量 bConnect=true; m_CustomFaceInfo.dwDataSize=sizeof(m_CustomFaceInfo); } //激活任务 if (bConnect==true) { //控件控制 EnableControl(false); //激活任务 m_MissionManager.AvtiveMissionItem(this,false); return; } __super::OnOK(); }
UINT InverseThread(LPVOID pParam) { LANGID id = PIGetThreadUILanguage(); SetThreadUILanguage(id); CString str; str.LoadString(ID_IMAGE_INVERSE); PIProgressInit(PI_PROGRESS_DLG, str); // PIProgressInit(PI_PROGRESS_BAR, str); CImage* pImage = (CImage*)pParam; int nWidth = pImage->GetWidth(); int nHeight = pImage->GetHeight(); BYTE* pImageData = (BYTE*)pImage->GetBits(); int nPitch = pImage->GetPitch(); int nBitCount = pImage->GetBPP() / 8; for (int j=0; j<nHeight; j++) { for (int i=0; i<nWidth; i++) { int nPixelIndex = j * nPitch + i * nBitCount; BYTE* pPixel = pImageData + j * nPitch + i * nBitCount; *(pPixel) = 255 - *(pPixel); *(pPixel + 1) = 255 - *(pPixel + 1); *(pPixel + 2) = 255 - *(pPixel + 2); } LRESULT lResult = PIProgressPercent(j * 100 / nHeight); if (!lResult) { // quit thread return 1; } } PIProgressDone(); // refresh view PIGetActiveView()->Invalidate(FALSE); return 0; }
void CTraffic_Camera_Image::MatToCImage(Mat &mat, CImage &cImage) { //create new CImage int width = mat.cols; int height = mat.rows; int channels = mat.channels(); //cImage.ReleaseDC(); //cImage.ReleaseGDIPlus(); cImage.Destroy(); //clear cImage.Create(width, height, //positive: left-bottom-up or negative: left-top-down 8*channels ); //numbers of bits per pixel //copy values uchar* ps; uchar* pimg = (uchar*)cImage.GetBits(); //A pointer to the bitmap buffer //The pitch is the distance, in bytes. represent the beginning of // one bitmap line and the beginning of the next bitmap line int step = cImage.GetPitch(); for (int i = 0; i < height; ++i) { ps = (mat.ptr<uchar>(i)); for ( int j = 0; j < width; ++j ) { if ( channels == 1 ) //gray { *(pimg + i*step + j) = ps[j]; } else if ( channels == 3 ) //color { for (int k = 0 ; k < 3; ++k ) { *(pimg + i*step + j*3 + k ) = ps[j*3 + k]; } } } } }
void CTracer::SaveImageToFile(std::string fileName) { CImage image; int width = m_camera.m_resolution.x; int height = m_camera.m_resolution.y; image.Create(width, height, 24); int pitch = image.GetPitch(); unsigned char* imageBuffer = (unsigned char*)image.GetBits(); if (pitch < 0) { imageBuffer += pitch * (height - 1); pitch = -pitch; } int i, j; int imageDisplacement = 0; int textureDisplacement = 0; for (i = 0; i < height; i++) { for (j = 0; j < width; j++) { glm::vec3 color = m_camera.m_pixels[textureDisplacement + j]; // clamp(val, minVal, maxVal) = min(max(x, minVal), maxVal) // shows that val is in [minVal, maxVal] imageBuffer[imageDisplacement + j * 3] = unsigned(glm::clamp(color.b, 0.0f, 1.0f) * 255.0f); imageBuffer[imageDisplacement + j * 3 + 1] = unsigned(glm::clamp(color.g, 0.0f, 1.0f) * 255.0f); imageBuffer[imageDisplacement + j * 3 + 2] = unsigned(glm::clamp(color.r, 0.0f, 1.0f) * 255.0f); } imageDisplacement += pitch; textureDisplacement += width; } image.Save(fileName.c_str()); image.Destroy(); }
void TestCommand::Func2() { CImage saveimg; saveimg.Create(img_->GetWidth()/2,img_->GetHeight()/2,8); int steps=m_min(img_->GetWidth()/2,img_->GetHeight()/2); BYTE *psrc=(BYTE *)img_->GetImage()->GetBits(); int nrowsrc=img_->GetImage()->GetPitch(); BYTE *pdes=(BYTE *)saveimg.GetBits(); int nrowdes=saveimg.GetPitch(); for(int xi=0;xi<steps;xi++) for(int yi=0;yi<steps;yi++) { pdes[yi*nrowdes+xi]=(psrc[(yi*2)*nrowsrc+(xi*2)*3]+psrc[(yi*2)*nrowsrc+(xi*2)*3+1]+psrc[(yi*2)*nrowsrc+(xi*2)*3+2] +psrc[(yi*2)*nrowsrc+(2*xi+1)*3]+psrc[(yi*2)*nrowsrc+(2*xi+1)*3+1]+psrc[(yi*2)*nrowsrc+(2*xi+1)*3+2] +psrc[(yi*2+1)*nrowsrc+(2*xi)*3]+psrc[(yi*2+1)*nrowsrc+(2*xi)*3+1]+psrc[(yi*2+1)*nrowsrc+(2*xi)*3+2] +psrc[(yi*2+1)*nrowsrc+(2*xi+1)*3]+psrc[(yi*2+1)*nrowsrc+(2*xi+1)*3]+psrc[(yi*2+1)*nrowsrc+(2*xi+1)*3])/12; } saveimg.SetColorTable(0,255,ImageYH::GetColorTable()); saveimg.Save("d:\\testImg\\lena256.bmp"); }
// // Copies the content of a byte buffer to a MFC image with respect to the image's alignment // // Parameters: // [in] pInbuffer The byte buffer as received from the cam // [in] ePixelFormat The pixel format of the frame // [out] OutImage The filled MFC image // void CAsynchronousGrabDlg::CopyToImage( VmbUchar_t *pInBuffer, VmbPixelFormat_t ePixelFormat, CImage &OutImage ) { const int nHeight = m_ApiController.GetHeight(); const int nWidth = m_ApiController.GetWidth(); const int nStride = OutImage.GetPitch(); const int nBitsPerPixel = OutImage.GetBPP(); VmbError_t Result; if( ( nWidth*nBitsPerPixel ) /8 != nStride ) { Log( _TEXT( "Vimba only supports stride that is equal to width." ), VmbErrorWrongType ); return; } VmbImage SourceImage,DestinationImage; SourceImage.Size = sizeof( SourceImage ); DestinationImage.Size = sizeof( DestinationImage ); SourceImage.Data = pInBuffer; DestinationImage.Data = OutImage.GetBits(); Result = VmbSetImageInfoFromPixelFormat( ePixelFormat, nWidth, nHeight, &SourceImage ); if( VmbErrorSuccess != Result ) { Log( _TEXT( "Error setting source image info." ), static_cast<VmbErrorType>( Result ) ); return; } static const std::string DisplayFormat( "BGR24" ); Result = VmbSetImageInfoFromString( DisplayFormat.c_str(),DisplayFormat.size(), nWidth,nHeight, &DestinationImage ); if( VmbErrorSuccess != Result ) { Log( _TEXT( "Error setting destination image info." ),static_cast<VmbErrorType>( Result ) ); return; } Result = VmbImageTransform( &SourceImage, &DestinationImage,NULL,0 ); if( VmbErrorSuccess != Result ) { Log( _TEXT( "Error transforming image." ), static_cast<VmbErrorType>( Result ) ); } }
//根据文件路径获取单张图片 Texture2D* TextureLoader::CreateTexture2D(const char* fileName) { GLuint texture; glGenTextures(1, &texture); glBindTexture( GL_TEXTURE_2D, texture); CImage *img = new CImage; // 新建CImage对象 char *complete_path = FileTool::GetInstance()->GetCompletePath(fileName); if (complete_path == NULL) { SAFDelete(img); return NULL; } wchar_t *wchat_file_name = ATC2W(complete_path); img->Load(wchat_file_name); // 读入图像文件 SAFDelete(wchat_file_name); SAFDelete(complete_path); int width = img->GetWidth(); // 获取图像宽度 int height = img->GetHeight(); // 获取图像高度 int n = img->GetBPP() / 8; int test = img->GetPitch();//图片是正的还是反的 GLubyte *image = new GLubyte[width * height * n]; // 用于保存图像数据的数组 // 将图像数据读入image数组 if(test<0) for(int j = height-1,k=0; j >=0; j--,k++){ for(int i = width-1,z=width-1; i >=0; i--,z--){ int index = (k * width + z) * n; RGBQUAD rgb = GetPixelColor(img, i, j); image[index] = rgb.rgbRed; image[index+1] = rgb.rgbGreen; image[index+2] = rgb.rgbBlue; if(n==4) image[index+3] = rgb.rgbReserved; } } else for(int j = 0; j < height; j++){ for(int i = 0; i < width; i++){ int index = (j * width + i) * n; RGBQUAD rgb = GetPixelColor(img, i, j); image[index] = rgb.rgbRed; image[index+1] = rgb.rgbGreen; image[index+2] = rgb.rgbBlue; if(n==4) image[index+3] = rgb.rgbReserved; } } delete img; // CImage对象已无用,数据已读入image数组 // 根据image中的数据在纹理内存中创建纹理 glPixelStorei(GL_UNPACK_ALIGNMENT, 1); // 每行图像数据紧密排列 if(n==4) glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image); else glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image); glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT ); glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT ); glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); delete image; // 纹理数据已在显卡纹理内存中了,主机内存中的image已无用 Texture2D *tex = new Texture2D(); tex->texture_name_ = fileName; tex->height_ = height; tex->width_ = width; tex->tex_res_ = texture; return tex; }
void CanonCamera::UpdateView() { if(!liveView) return; EdsError err = EDS_ERR_OK; EdsStreamRef stream = NULL; EdsEvfImageRef evfImage = NULL; // Create memory stream. err = EdsCreateMemoryStream(0, &stream); if(err != EDS_ERR_OK) { printf("Error in EdsCreateMemoryStream: 0x%X\n", err); } err = EdsCreateEvfImageRef(stream, &evfImage); if(err != EDS_ERR_OK) { printf("Error in EdsCreateEvfImageRef: 0x%X\n", err); } bool wait = true; int numTries = 0; while(wait && numTries++ < 20) { err = EdsDownloadEvfImage(camera, evfImage); if(err != EDS_ERR_OK && err != EDS_ERR_OBJECT_NOTREADY) { printf("Error in EdsDownloadEvfImage: 0x%X\n", err); } if(err == EDS_ERR_OBJECT_NOTREADY) { Sleep(250); } else { wait = false; } } if(numTries > 20) { printf("ERROR: camera is taking too long for EdsDownloadEvfImage\n"); } unsigned char* pByteImage = NULL; // Get image (JPEG) pointer. err = EdsGetPointer(stream, (EdsVoid**)&pByteImage ); if(err != EDS_ERR_OK) { printf("Error in EdsGetPointer Histogram: 0x%X\n", err); } EdsUInt32 size; err = EdsGetLength(stream, &size); if(err != EDS_ERR_OK) { printf("Error in EdsGetLength Histogram: 0x%X\n", err); } EdsImageRef image = NULL; EdsImageInfo imageInfo; err = EdsCreateImageRef(stream, &image); if(err != EDS_ERR_OK) { printf("Error in EdsCreateImageRef: 0x%X\n", err); } err = EdsGetImageInfo(image, kEdsImageSrc_FullView, &imageInfo); if(err != EDS_ERR_OK) { printf("Error in EdsGetImageInfo: 0x%X\n", err); } if(imageInfo.componentDepth != 8) { printf("Error imageInfo.componentDepth != 8\n"); } liveImage = cvCreateImage(cvSize(imageInfo.width, imageInfo.height), IPL_DEPTH_8U, imageInfo.numOfComponents); EdsUInt32 DataSize = 0; CImage cImage; HRESULT hr; CComPtr<IStream> iStream = NULL; HGLOBAL hMem = GlobalAlloc(GHND, size); LPVOID pBuff = GlobalLock(hMem); memcpy(pBuff, pByteImage, size); GlobalUnlock(hMem); hr = CreateStreamOnHGlobal(hMem, TRUE, &iStream); // Get the bitmap image from the stream if ((hr = cImage.Load(iStream)) == S_OK) { int pitch = cImage.GetPitch(); int height = cImage.GetHeight(); BYTE* pBits = (BYTE*)cImage.GetBits(); if (pitch < 0) pBits += (pitch *(height -1)); memcpy(liveImage->imageData, pBits, abs(pitch) * height); } cImage.~CImage(); GlobalFree(hMem); cvFlip(liveImage, NULL, 0); // Release stream if(stream != NULL) { err = EdsRelease(stream); if(err != EDS_ERR_OK) { printf("Error in EdsRelease: 0x%X\n", err); } stream = NULL; } if(evfImage != NULL) { err = EdsRelease(evfImage); if(err != EDS_ERR_OK) { printf("Error in EdsRelease: 0x%X\n", err); } evfImage = NULL; } EdsRelease(image); cvShowImage(windowName.c_str(), liveImage); cvReleaseImage(&liveImage); }
//混合绘画 bool CSkinImage::BlendDrawImage(CDC * pDestDC, INT xDest, INT yDest, INT cxDest, INT cyDest, INT xSrc, INT ySrc, COLORREF crTransColor, BYTE cbAlphaDepth) { //无效区域 CRect rcDirty; pDestDC->GetClipBox(&rcDirty); //绘画判断 if (IsNull()==true) return false; //位置调整 tagImageRender ImageRender; GetDrawImageArea(xDest,yDest,cxDest,cyDest,xSrc,ySrc,rcDirty,ImageRender); //创建位图 CImage ImageResult; CImage ImageSource; ImageResult.Create(ImageRender.cxRender,ImageRender.cyRender,32); ImageSource.Create(ImageRender.cxRender,ImageRender.cyRender,32); //绘画位图 CDC * pDCImage=CDC::FromHandle(GetDC()); CDC * pDCResult=CDC::FromHandle(ImageResult.GetDC()); CDC * pDCSource=CDC::FromHandle(ImageSource.GetDC()); pDCSource->BitBlt(0,0,ImageRender.cxRender,ImageRender.cyRender,pDCImage,ImageRender.nXImage,ImageRender.nYImage,SRCCOPY); pDCResult->BitBlt(0,0,ImageRender.cxRender,ImageRender.cyRender,pDestDC,ImageRender.nXScreen,ImageRender.nYScreen,SRCCOPY); //获取属性 float fAlpha=(float)(cbAlphaDepth/255.0); INT nPitchResult=ImageResult.GetPitch(); INT nPitchSource=ImageSource.GetPitch(); //获取数据 LPBYTE cbBitResult=(LPBYTE)ImageResult.GetBits(); LPBYTE cbBitSource=(LPBYTE)ImageSource.GetBits(); //创建区域 for (INT nYPos=0;nYPos<ImageRender.cyRender;nYPos++) { for (INT nXPos=0;nXPos<ImageRender.cxRender;nXPos++) { //获取颜色 COLORREF * pcrResult=(COLORREF *)(cbBitResult+nYPos*nPitchResult+nXPos*4); COLORREF * pcrSource=(COLORREF *)(cbBitSource+nYPos*nPitchSource+nXPos*4); //混合处理 if (*pcrSource!=crTransColor) { //结果颜色 BYTE cbResultR=GetRValue(*pcrResult); BYTE cbResultG=GetGValue(*pcrResult); BYTE cbResultB=GetBValue(*pcrResult); //原图颜色 BYTE cbSourceR=GetRValue(*pcrSource); BYTE cbSourceG=GetGValue(*pcrSource); BYTE cbSourceB=GetBValue(*pcrSource); //颜色混合 cbResultR=(BYTE)(cbSourceR*fAlpha+cbResultR*(1.0-fAlpha)); cbResultG=(BYTE)(cbSourceG*fAlpha+cbResultG*(1.0-fAlpha)); cbResultB=(BYTE)(cbSourceB*fAlpha+cbResultB*(1.0-fAlpha)); //颜色混合 *pcrResult=RGB(cbResultR,cbResultG,cbResultB); } } } //绘画界面 ImageResult.BitBlt(pDestDC->m_hDC,ImageRender.nXScreen,ImageRender.nYScreen); //释放对象 ReleaseDC(); ImageSource.ReleaseDC(); ImageResult.ReleaseDC(); return true; }
void CStorageDoc::OnFileImport () { CFile fImport; CFileDialog dlgOpen (true); // #### TODO: Place here initial directory name taken from configuration structures dlgOpen.m_ofn.lpstrInitialDir = "."; dlgOpen.m_ofn.lpstrFilter = "Kontron Elektronik Images (*.img)""\0""*.IMG""\0" "All supported signle image formats""\0""*.JPG;*.JPEG;*.GIF;*.PNG;*.BMP;*.DIB""\0" "JPEG Bitmap (*.jpg, *.jpeg)""\0""*.JPG;*.JPEG""\0" "CumpuServe GIF (*.gif)""\0""*.GIF""\0" "Prtable Network Graphics (*.png)""\0""*.PNG""\0" "MS Windows Bitmap (*.bmp, *.dib)""\0""*.BMP;*.DIB""\0"; dlgOpen.m_ofn.nFilterIndex = 1; // dlgOpen.SetWindowText (theApp.LoadString (IDS_IMPORTSEQ_DLGCAPTION)); if (dlgOpen.DoModal () != IDOK) return; // #### TODO: Add support for video formats // #### TODO: Add support for multi-page tif format // #### TODO: Create import dialog // #### TODO: Add progress control on the status bar uvar32_64 selected; svar32_64 nImages; uvar32_64 dwSize = m_nDimX * m_nDimY; CString strTitle; BYTE *pbPixels = (BYTE *)aimMemoryCommit (dwSize, "CStorageDoc::OnDocAddImportseq", "pbPixels"); if ((selected = Frames[aimActive].GetActiveImageNo()) == -1) selected = Images.GetCount () - 1; theApp.StatusState (IDI_INDICATOR_IMPORT); if (dlgOpen.m_ofn.nFilterIndex == 1) { if (!fImport.Open (dlgOpen.GetPathName (), CFile::modeRead | CFile::typeBinary | CFile::shareDenyWrite)) { MessageBox (theApp.m_pMainWnd->m_hWnd, theApp.LoadString (IDS_IMPORTSEQ_OPENERR), theApp.LoadString (IDS_IMPORTSEQ_DLGCAPTION), MB_OK | MB_ICONERROR); return; } BYTE *pbHeader = new BYTE[0x80]; fImport.Read ( pbHeader, 0x80 ); if (((DWORD*)(pbHeader + 2))[0] != 0xB06D1247) { MessageBox (theApp.m_pMainWnd->m_hWnd, theApp.LoadString (IDS_IMPORTSEQ_TYPEERR), theApp.LoadString (IDS_IMPORTSEQ_DLGCAPTION), MB_OK | MB_ICONERROR); return; } if (((WORD*)pbHeader)[5] != 0x4321) { MessageBox (theApp.m_pMainWnd->m_hWnd, theApp.LoadString (IDS_IMPORTSEQ_TYPEERR), theApp.LoadString (IDS_IMPORTSEQ_DLGCAPTION), MB_OK | MB_ICONERROR); return; } if (((WORD*)pbHeader)[3] != m_nDimX || ((WORD*)pbHeader)[4] != m_nDimY || m_nBPP != 8) { MessageBox (theApp.m_pMainWnd->m_hWnd, theApp.LoadString (IDS_IMPORTSEQ_SIZEERR), theApp.LoadString (IDS_IMPORTSEQ_DLGCAPTION), MB_OK | MB_ICONERROR); return; } nImages = ((WORD*)pbHeader)[6]; theApp.StatusState (IDI_INDICATOR_IMPORT); while (--nImages >= 0) { strTitle.Format ("%s #%03d", dlgOpen.GetFileTitle (), ((WORD*)pbHeader)[6] - nImages); CPicture *pImg = new CPicture (this, strTitle, ""); fImport.Read (pbPixels, dwSize); pImg->Channels(aimAll).SetBits (pbPixels, dwSize); Images.Insert (selected++, *pImg); } nImages = ((WORD*)pbHeader)[6]; fImport.Close (); delete [] pbHeader; aimMemoryRelease (pbPixels, "CStorage::OnDocAddImportseq", "pbPixels"); } else if (dlgOpen.m_ofn.nFilterIndex >= 2) { CImage image; image.Load (dlgOpen.GetPathName ()); if (image.GetBPP () != m_nBPP || image.GetHeight () != m_nDimY || image.GetWidth () != m_nDimX) { MessageBox (theApp.m_pMainWnd->m_hWnd, theApp.LoadString (IDS_IMPORTSEQ_SIZEERR), theApp.LoadString (IDS_IMPORTSEQ_DLGCAPTION), MB_OK | MB_ICONERROR); return; } CPicture *pImg = new CPicture (this, dlgOpen.GetFileTitle (), ""); ubyte *pBits; if (image.IsDIBSection () && image.GetPitch () < 0) pBits = (ubyte*)image.GetBits () - (m_nDimY - 1) * m_nDimX; else pBits = (ubyte*)image.GetBits (); pImg->Channels(aimAll).SetBits (pBits, m_nDimX * m_nDimY * m_nBPP / 8); Images.Insert (selected++, *pImg); } theApp.StatusState (0); }
void CiratefiApp::ShowMatOnPicture(Mat& image, CDialog* dlg, int pID) { CRect PictureRect; CStatic* PictureControl=(CStatic*)dlg->GetDlgItem(pID); PictureControl->GetClientRect(&PictureRect); CDC *pDc = PictureControl->GetWindowDC(); SetStretchBltMode(pDc->m_hDC,STRETCH_HALFTONE); Mat resizeImage = image.clone(); if(resizeImage.rows>PictureRect.Height() || resizeImage.cols>PictureRect.Width()) { double resizeRatio = min((double)PictureRect.Width()/(double)image.cols, (double)PictureRect.Height()/(double)image.rows); resize(image, resizeImage, Size(), resizeRatio, resizeRatio); } CImage outputImage; int width = resizeImage.cols; int height = resizeImage.rows; int channels = resizeImage.channels(); outputImage.Destroy(); //clear outputImage.Create(width, height, 8*channels); if(channels==1) { RGBQUAD* ColorTable; int MaxColors=outputImage.GetMaxColorTableEntries(); ColorTable = new RGBQUAD[MaxColors]; outputImage.GetColorTable(0, MaxColors, ColorTable); for (int i = 0; i < MaxColors; i++) { ColorTable[i].rgbBlue = (BYTE)i; ColorTable[i].rgbGreen = (BYTE)i; ColorTable[i].rgbRed = (BYTE)i; } outputImage.SetColorTable(0, MaxColors, ColorTable); delete []ColorTable; } uchar* ps; uchar* pimg = (uchar*)outputImage.GetBits(); //A pointer to the bitmap buffer //The pitch is the distance, in bytes. represent the beginning of // one bitmap line and the beginning of the next bitmap line int step = outputImage.GetPitch(); for (int i = 0; i < height; ++i) { ps = (resizeImage.ptr<uchar>(i)); for ( int j = 0; j < width; j++ ) { if ( channels == 1 ) //gray { *(pimg + i*step + j) = ps[j]; } else if ( channels == 3 ) //color { for (int k = 0 ; k < 3; k++ ) { *(pimg + i*step + j*3 + k ) = ps[j*3 + k]; } } } } outputImage.Draw(pDc->m_hDC, CRect(CPoint(PictureRect.TopLeft().x+(PictureRect.Width()-width)/2,PictureRect.TopLeft().y+(PictureRect.Height()-height)/2), CSize(width,height))); dlg->ReleaseDC(pDc); }