BOOL CSonicImage::Load(HGLOBAL hGlobal, DWORD dwSize) { if(m_gif.LoadGif(hGlobal, dwSize) == 1) { if(PrepareMemDC(m_gif.GetWidth(), m_gif.GetHeight()) == FALSE) { m_gif.Clear(); return FALSE; } m_gif.Draw(m_Dib.GetSafeHdc()); } else { BYTE * pData = (BYTE *)GlobalLock(hGlobal); CxImage img; img.Decode(pData, dwSize, 0); GlobalUnlock(hGlobal); if(PrepareMemDC(img.GetWidth(), img.GetHeight()) == FALSE) { img.Clear(); return FALSE; } if(!img.AlphaIsValid()) { img.Draw(m_Dib.GetSafeHdc()); CSSE::DoOr(0xff000000, m_Dib.GetBits(), m_Dib.GetSize()); } else { if(img.GetBpp() != 24) { img.Clear(); return FALSE; } BYTE * pSrc = img.GetBits(); BYTE * pAlpha = img.AlphaGetBits(); BYTE * pMyBits = m_Dib.GetBits(); int nLineTail = m_nWidth % 4; for(int i = 0; i < m_nHeight; i++) { for(int j = 0; j < m_nWidth; j++) { *pMyBits++ = *pSrc++; *pMyBits++ = *pSrc++; *pMyBits++ = *pSrc++; *pMyBits++ = *pAlpha++; } pSrc += nLineTail; } EnableAlphaChannel(); } img.Clear(); } return TRUE; }
bool SaveThumb(CxImage &image, const char *file, const char *thumb, int maxWidth, int maxHeight, bool bNeedToConvert = true, bool autoRotate = true) { // ok, now resample the image down if necessary int ret = ResampleKeepAspectArea(image, maxWidth * maxHeight); if (ret < 0) return false; if (ret) bNeedToConvert = true; // if we don't have a png but have a < 24 bit image, then convert to 24bits if ( image.GetNumColors()) { if (!image.IncreaseBpp(24) || !image.IsValid()) { printf("PICTURE::SaveThumb: Unable to convert to 24bpp: Error:%s\n", image.GetLastError()); return false; } bNeedToConvert = true; } if ( autoRotate && image.GetExifInfo()->Orientation > 1) { image.RotateExif(image.GetExifInfo()->Orientation); bNeedToConvert = true; } #ifndef _LINUX ::DeleteFile(thumb); #else unlink(thumb); #endif // only resave the image if we have to (quality of the JPG saver isn't too hot!) if (bNeedToConvert) { // May as well have decent quality thumbs image.SetJpegQuality(90); if (!image.Save(thumb, image.AlphaIsValid() ? CXIMAGE_FORMAT_PNG : CXIMAGE_FORMAT_JPG)) { printf("PICTURE::SaveThumb: Unable to save image: %s Error:%s\n", thumb, image.GetLastError()); ::DeleteFile(thumb); return false; } } else { // Don't need to convert the file - copy it instead if (!CopyFile(file, thumb)) { printf("PICTURE::SaveThumb: Unable to copy file %s\n", file); ::DeleteFile(thumb); return false; } } return true; }
////////////////////////////////////////////////////////////////////////////// // CDemoView message handlers ////////////////////////////////////////////////////////////////////////////// void CDemoView::OnMouseMove(UINT nFlags, CPoint point) { CDemoDoc* pDoc = GetDocument(); CxImage* ima = pDoc->GetImage(); if (!ima) return; // We'll get the RGB values at the point the user selects long x = point.x; long y = point.y; GetImageXY(pDoc, ima, &x,&y); TCHAR s[80]; if (ima->IsInside(x,y)) { long yflip = ima->GetHeight() - y - 1; _stprintf(s,_T("x: %d y: %d idx: %d"), x, y, ima->GetPixelIndex(x,yflip)); RGBQUAD rgb=ima->GetPixelColor(x,yflip); if (ima->AlphaIsValid()) rgb.rgbReserved=ima->AlphaGet(x,yflip); else rgb.rgbReserved=ima->GetPaletteColor(ima->GetPixelIndex(x,yflip)).rgbReserved; _stprintf(&s[_tcslen(s)],_T(" RGBA: (%d, %d, %d, %d)"), rgb.rgbRed, rgb.rgbGreen, rgb.rgbBlue, rgb.rgbReserved); //Enable these lines if you want draw over the image //if ((nFlags & MK_LBUTTON)==MK_LBUTTON){ // ima->SetPixelColor(x,yflip,RGB(rand()/(RAND_MAX/256),rand()/(RAND_MAX/256),rand()/(RAND_MAX/256))); // Invalidate(0); //} #ifdef VATI_EXTENSIONS if (nFlags & MK_RBUTTON && !(nFlags & MK_LBUTTON)) { switch (pDoc->m_tool){ case 1: // selection if ( nFlags & MK_CONTROL ) // CTRL+right button: move selection { for (int i=0; i<pDoc->m_NumSel; i++) { pDoc->m_Sel[i].x += (long)(x-m_oldPnt.x); pDoc->m_Sel[i].y += (long)(y-m_oldPnt.y); } m_oldPnt.x = x; m_oldPnt.y = y; //redraw selection CWnd* pFrame=GetParentFrame(); RECT rClient; pFrame->GetClientRect(&rClient); pFrame->RedrawWindow( &rClient, NULL, RDW_INVALIDATE | RDW_UPDATENOW); DrawSelection(); } else if ( !(nFlags & MK_CONTROL) && pDoc->m_isRectSel && pDoc->m_NumSel==5 ) { // calculate angle difference looking from rectangle center double d_angle = atan2( (y - m_orgPnt.y), (x - m_orgPnt.x) ) - atan2( (m_oldPnt.y - m_orgPnt.y), (m_oldPnt.x - m_orgPnt.x) ); m_oldPnt.x = x; m_oldPnt.y = y; Dpoint2d p; // rotate corner points around center point for(int i=0;i<5;i++) { p.x = m_dpnt[i].x - m_orgPnt.x; p.y = m_dpnt[i].y - m_orgPnt.y; m_dpnt[i].x = p.x*cos(d_angle) - p.y*sin(d_angle) + m_orgPnt.x; m_dpnt[i].y = p.x*sin(d_angle) + p.y*cos(d_angle) + m_orgPnt.y; // write back to selection pDoc->m_Sel[i].x = (long)m_dpnt[i].x ; pDoc->m_Sel[i].y = (long)m_dpnt[i].y ; } // redraw the rectangle CWnd* pFrame=GetParentFrame(); RECT rClient; pFrame->GetClientRect(&rClient); pFrame->RedrawWindow( &rClient, NULL, RDW_INVALIDATE | RDW_UPDATENOW); DrawSelection(); } break; } // end switch } // end if MK_RBUTTON #endif if (nFlags & MK_LBUTTON) switch (pDoc->m_tool){ case 0: // move { SetCursor(LoadCursor(0,IDC_SIZEALL)); CSize sz(GetTotalSize()); CWnd* pFrame=GetParentFrame(); RECT rClient; pFrame->GetClientRect(&rClient); if (sz.cx>rClient.right) SetScrollPos(SB_HORZ,m_RefScroll.x - point.x + m_RefPoint.x); else SetScrollPos(SB_HORZ,0); if (sz.cy>rClient.bottom) SetScrollPos(SB_VERT,m_RefScroll.y - point.y + m_RefPoint.y); else SetScrollPos(SB_VERT,0); Invalidate(0); break; } case 1: //selection SetCursor(LoadCursor(0,IDC_CROSS)); #if CXIMAGE_DEMO_SELECT #ifdef VATI_EXTENSIONS if ( nFlags & MK_SHIFT ) { // rectangle selection pDoc->m_isRectSel = 1; // in rectangle mode, selection array has 0,1 or 5 points if (!pDoc->m_NumSel) { pDoc->m_Sel[0].x = x; pDoc->m_Sel[0].y = y; pDoc->m_NumSel = 1; } else // already has at least one corner { pDoc->m_Sel[1].x = x; pDoc->m_Sel[1].y = pDoc->m_Sel[0].y; pDoc->m_Sel[2].x = x; pDoc->m_Sel[2].y = y; pDoc->m_Sel[3].x = pDoc->m_Sel[0].x; pDoc->m_Sel[3].y = y; //close the rectangle: pDoc->m_Sel[4].x = pDoc->m_Sel[0].x; pDoc->m_Sel[4].y = pDoc->m_Sel[0].y; pDoc->m_NumSel = 5; } // delete old rectangle from display: CWnd* pFrame=GetParentFrame(); RECT rClient; pFrame->GetClientRect(&rClient); pFrame->RedrawWindow( &rClient, NULL, RDW_INVALIDATE | RDW_UPDATENOW); // draw the new rectangle DrawSelection(); } else #endif { //freehand selection (original) float zoom=pDoc->GetZoomFactor(); CPoint pos(GetScrollPosition()); int i=pDoc->m_NumSel; pDoc->m_Sel[i].x = (long)((point.x + pos.x)/zoom); pDoc->m_Sel[i].y = (long)((point.y + pos.y)/zoom); if (i<(MAX_SEL_POINTS-2)) pDoc->m_NumSel++; DrawSelection(); } #endif } //end switch } else _tcscpy(s,_T(" ")); CStatusBar& statusBar = ((CMainFrame *)(AfxGetApp()->m_pMainWnd))->GetStatusBar(); statusBar.SetPaneText(0, s); CScrollView::OnMouseMove(nFlags, point); }