bool CxImageIG::decodeSubLayer (CxFile& file, IGLibrary::IGLayer *pSubLayerOwner, IGSECTIONHEADER_LAYER *pLayerSection) { CxImage cxSubLayer; if (!file.Seek (pLayerSection->commonHeader.nFirstByteOffset, SEEK_SET)) throw IGEXCEPTION (CxImageIGException, "decodeSubLayer", "file.Seek failed"); // read sub-layer pixels BYTE *pBufImg = new BYTE [pLayerSection->commonHeader.nSizeBuf]; file.Read (pBufImg, pLayerSection->commonHeader.nSizeBuf, 1); if (!cxSubLayer.Decode (pBufImg, pLayerSection->commonHeader.nSizeBuf, CXIMAGEIG_LAYERFORMAT)) throw IGEXCEPTION (CxImageIGException, "decodeSubLayer", "cxSubLayer.Decode failed"); delete [] pBufImg; // read sub-layer alpha cxSubLayer.AlphaCreate (255); file.Read (cxSubLayer.pAlpha, cxSubLayer.GetWidth() * cxSubLayer.GetHeight(), 1); // decode sub-layer into layer BYTE *pLayerBits = NULL; BYTE *pSubLayerBits = NULL; for (int i = 0; i < pLayerSection->ptSize.y; i++) { pSubLayerBits = cxSubLayer.GetBits (i); pLayerBits = pSubLayerOwner->GetBits (pLayerSection->ptOffset.y + i) + 3 * pLayerSection->ptOffset.x; ::memcpy (pLayerBits, pSubLayerBits, (pLayerSection->ptSize.x) * 3); } BYTE *pLayerAlpha = NULL; BYTE *pSubLayerAlpha = NULL; for (int i = 0; i < (pLayerSection->ptSize.y); i++) { pLayerAlpha = pSubLayerOwner->AlphaGetPointer (pLayerSection->ptOffset.x, pLayerSection->ptOffset.y + i); pSubLayerAlpha = cxSubLayer.AlphaGetPointer (0, i); ::memcpy (pLayerAlpha, pSubLayerAlpha, (pLayerSection->ptSize.x)); } return true; }
__declspec(dllexport) bool LoadImage(const char *file, unsigned int maxwidth, unsigned int maxheight, ImageInfo *info) { if (!file || !info) return false; if (IsDir(file)) return false; // load the image DWORD dwImageType = GetImageType(file); CxImage *image = new CxImage(dwImageType); if (!image) return false; int actualwidth = maxwidth; int actualheight = maxheight; try { if (!image->Load(file, dwImageType, actualwidth, actualheight) || !image->IsValid()) { #if !defined(_LINUX) && !defined(__APPLE__) int nErr = GetLastError(); #else int nErr = errno; #endif printf("PICTURE::LoadImage: Unable to open image: %s Error:%s (%d)\n", file, image->GetLastError(),nErr); delete image; return false; } } catch (...) { printf("PICTURE::LoadImage: Unable to open image: %s\n", file); delete image; return false; } // ok, now resample the image down if necessary if (ResampleKeepAspect(*image, maxwidth, maxheight) < 0) { printf("PICTURE::LoadImage: Unable to resample picture: %s\n", file); delete image; return false; } // make sure our image is 24bit minimum image->IncreaseBpp(24); // fill in our struct info->width = image->GetWidth(); info->height = image->GetHeight(); info->originalwidth = actualwidth; info->originalheight = actualheight; memcpy(&info->exifInfo, image->GetExifInfo(), sizeof(EXIFINFO)); // create our texture info->context = image; info->texture = image->GetBits(); info->alpha = image->AlphaGetBits(); return (info->texture != NULL); };
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 CxImageIG::DecodeLayer (CxFile &file, int nLayerIdx, int nLayerPos, RECT *p_rcSubLayer, bool bUndo, int nSubLayerId) { // read current header IGHEADER igHeader; if (!decodeHeader (&file, &igHeader)) throw IGEXCEPTION (CxImageIGException, "DecodeLayer", "decodeHeader failed"); if (nLayerIdx < 0) throw IGEXCEPTION (CxImageIGException, "DecodeLayer", "nLayerIdx failed"); if ((nLayerPos < 0) || (nLayerPos >= info.nNumLayers)) throw IGEXCEPTION (CxImageIGException, "DecodeLayer", "nLayerPos failed"); IGSECTIONHEADER_LAYER *pLayerSections = new IGSECTIONHEADER_LAYER [CXIMAGEIG_MAX_NBLAYERS]; IGSECTIONHEADER_SELECTION *pSelectionSections = new IGSECTIONHEADER_SELECTION [CXIMAGEIG_MAX_NBSELECTIONS]; if (!decodeSections (&file, &igHeader, &pLayerSections[0], &pSelectionSections[0])) throw IGEXCEPTION (CxImageIGException, "DecodeLayer", "decodeSections failed"); IGLibrary::IGLayer *pLayer = GetLayer (nLayerPos); if (!pLayer) throw IGEXCEPTION (CxImageIGException, "DecodeLayer", "GetLayer failed"); CxImage cxSubLayer; CxImage *pDecodingLayer = p_rcSubLayer ? &cxSubLayer : pLayer; bool bIsSubLayerOwner = bUndo && p_rcSubLayer; // no need to find sub-layer owner in do mode int nLayerSectionIndex = findLayerSectionIndex (&igHeader, pLayerSections, nLayerIdx, bIsSubLayerOwner); if ((nLayerSectionIndex < 0) || (nLayerSectionIndex >= igHeader.nNbLayers)) throw IGEXCEPTION (CxImageIGException, "DecodeLayer", "findLayerSectionIndex failed"); // in undo mode, if a sub-layer owner is found, decode it if (bUndo){ if (bIsSubLayerOwner) { // decode sub-layer owner if (!DecodeLayer (file, pLayerSections [nLayerSectionIndex].commonHeader.nId, nLayerPos)) throw IGEXCEPTION (CxImageIGException, "DecodeLayer", "DecodeLayer failed"); // decode sub-layers int nSubLayerSectionIndex = findLayerSectionIndex (&igHeader, pLayerSections, nLayerIdx, bIsSubLayerOwner); if ((nSubLayerSectionIndex < 0) || (nSubLayerSectionIndex >= igHeader.nNbLayers)) throw IGEXCEPTION (CxImageIGException, "DecodeLayer", "findLayerSectionIndex failed"); RECT rcSubLayer; rcSubLayer.left = pLayerSections[nSubLayerSectionIndex].ptOffset.x; rcSubLayer.right = pLayerSections[nSubLayerSectionIndex].ptOffset.x + pLayerSections[nSubLayerSectionIndex].ptSize.x - 1; rcSubLayer.top = pLayerSections[nSubLayerSectionIndex].ptOffset.y; rcSubLayer.bottom = pLayerSections[nSubLayerSectionIndex].ptOffset.y + pLayerSections[nSubLayerSectionIndex].ptSize.y - 1; return DecodeLayer (file, pLayerSections [nLayerSectionIndex].commonHeader.nId, nLayerPos, &rcSubLayer, true, nLayerIdx); } } if (!file.Seek (pLayerSections [nLayerSectionIndex].commonHeader.nFirstByteOffset, SEEK_SET)) throw IGEXCEPTION (CxImageIGException, "DecodeLayer", "file.Seek failed"); // read layer pixels BYTE *pBufImg = new BYTE [pLayerSections [nLayerSectionIndex].commonHeader.nSizeBuf]; file.Read (pBufImg, pLayerSections [nLayerSectionIndex].commonHeader.nSizeBuf, 1); if (!pDecodingLayer->Decode (pBufImg, pLayerSections [nLayerSectionIndex].commonHeader.nSizeBuf, CXIMAGEIG_LAYERFORMAT)) throw IGEXCEPTION (CxImageIGException, "DecodeLayer", "pDecodingLayer->Decode failed"); delete [] pBufImg; // read layer alpha pDecodingLayer->AlphaCreate (255); file.Read (pDecodingLayer->pAlpha, pDecodingLayer->GetWidth() * pDecodingLayer->GetHeight(), 1); int nSubLayerWidth = 0; int nSubLayerHeight = 0; if (p_rcSubLayer) { // decode sub-layer nSubLayerWidth = p_rcSubLayer->right - p_rcSubLayer->left + 1; nSubLayerHeight = p_rcSubLayer->bottom - p_rcSubLayer->top + 1; BYTE *pLayerBits = NULL; BYTE *pSubLayerBits = NULL; for (int i = 0; i < nSubLayerHeight; i++) { // if undo, then sub-layer == original layer pSubLayerBits = bUndo ? pDecodingLayer->GetBits (p_rcSubLayer->top + i) + 3 * p_rcSubLayer->left : pDecodingLayer->GetBits (i); pLayerBits = pLayer->GetBits (p_rcSubLayer->top + i) + 3 * p_rcSubLayer->left; ::memcpy (pLayerBits, pSubLayerBits, nSubLayerWidth * 3); } BYTE *pLayerAlpha = NULL; BYTE *pSubLayerAlpha = NULL; for (int i = 0; i < nSubLayerHeight; i++) { pLayerAlpha = pLayer->AlphaGetPointer (p_rcSubLayer->left, p_rcSubLayer->top + i); pSubLayerAlpha = bUndo ? pDecodingLayer->AlphaGetPointer (p_rcSubLayer->left, p_rcSubLayer->top + i) : pDecodingLayer->AlphaGetPointer (0, i); ::memcpy (pLayerAlpha, pSubLayerAlpha, nSubLayerWidth); } if (bUndo) { // original layer is now decoded. decode sub-layers bIsSubLayerOwner = false; int nMaxSubLayerSectionIndex = findLayerSectionIndex (&igHeader, pLayerSections, nSubLayerId, bIsSubLayerOwner); // we always have bIsSubLayerOwner = false, its used for the reference only if (nMaxSubLayerSectionIndex < 0) throw IGEXCEPTION (CxImageIGException, "DecodeLayer", "findLayerSectionIndex failed"); if (nMaxSubLayerSectionIndex <= igHeader.nNbLayers) { for (int i = 0; i < pLayerSections [nLayerSectionIndex].nSubLayers; i++) { if (pLayerSections [nLayerSectionIndex].pnSubLayers [i] <= nMaxSubLayerSectionIndex) { if (!decodeSubLayer (file, pLayer, &pLayerSections [pLayerSections [nLayerSectionIndex].pnSubLayers [i]])) throw IGEXCEPTION (CxImageIGException, "DecodeLayer", "decodeSubLayer failed"); } } } } } else { pLayer->info.xOffset = pLayerSections [nLayerSectionIndex].ptOffset.x; pLayer->info.yOffset = pLayerSections [nLayerSectionIndex].ptOffset.y; } pLayer->SetId (nLayerIdx); pLayer->UpdateImageParameters(); delete [] pLayerSections; delete [] pSelectionSections; return true; }
bool CxImageIG::EncodeLayer (const wchar_t *pcwFilePath, int nLayerIdx, int nLayerPos, RECT *p_rcSubLayer, int nSubLayerOwnerId) { CxIOFile file; if (!file.Open (pcwFilePath, L"r+b")) throw IGEXCEPTION (CxImageIGException, "EncodeLayer", "file.Open failed"); // read current header IGHEADER igHeader; if (!decodeHeader (&file, &igHeader)) throw IGEXCEPTION (CxImageIGException, "EncodeLayer", "decodeHeader failed"); IGSECTIONHEADER_LAYER *pLayerSections = new IGSECTIONHEADER_LAYER[CXIMAGEIG_MAX_NBLAYERS]; IGSECTIONHEADER_SELECTION *pSelectionSections = new IGSECTIONHEADER_SELECTION[CXIMAGEIG_MAX_NBSELECTIONS]; if (!decodeSections (&file, &igHeader, &pLayerSections[0], &pSelectionSections[0])) throw IGEXCEPTION (CxImageIGException, "EncodeLayer", "decodeSections failed"); IGLibrary::IGLayer *pLayer = GetLayer (nLayerPos); if (!pLayer) throw IGEXCEPTION (CxImageIGException, "EncodeLayer", "GetLayer failed"); _ASSERTE ((int)pLayer->GetId() == nLayerIdx && "CxImageIG::EncodeLayer FAILED"); bool bIsSubLayerOwner = false; int nLayerSectionIndex = findLayerSectionIndex (&igHeader, pLayerSections, nLayerIdx, bIsSubLayerOwner); if (nLayerSectionIndex < 0) throw IGEXCEPTION (CxImageIGException, "EncodeLayer", "findLayerSectionIndex failed"); if ((nLayerSectionIndex > igHeader.nNbLayers) || (nLayerSectionIndex >= CXIMAGEIG_MAX_NBLAYERS)) throw IGEXCEPTION (CxImageIGException, "EncodeLayer", "findLayerSectionIndex failed"); CxImage cxSubLayer; CxImage *pEncodingLayer = pLayer; int nSubLayerWidth = 0; int nSubLayerHeight = 0; if (p_rcSubLayer) { // Encode sub-layer nSubLayerWidth = p_rcSubLayer->right - p_rcSubLayer->left + 1; nSubLayerHeight = p_rcSubLayer->bottom - p_rcSubLayer->top + 1; cxSubLayer.Create (nSubLayerWidth, nSubLayerHeight, 24); cxSubLayer.AlphaCreate (255); BYTE *pLayerBits = NULL; BYTE *pSubLayerBits = NULL; for (int i = 0; i < nSubLayerHeight; i++) { pSubLayerBits = cxSubLayer.GetBits (i); pLayerBits = pLayer->GetBits (p_rcSubLayer->top + i) + 3 * p_rcSubLayer->left; ::memcpy (pSubLayerBits, pLayerBits, nSubLayerWidth * 3); } BYTE *pLayerAlpha = NULL; BYTE *pSubLayerAlpha = NULL; for (int i = 0; i < nSubLayerHeight; i++) { pLayerAlpha = pLayer->AlphaGetPointer (p_rcSubLayer->left, p_rcSubLayer->top + i); pSubLayerAlpha = cxSubLayer.AlphaGetPointer (0, i); ::memcpy (pSubLayerAlpha, pLayerAlpha, nSubLayerWidth); } pEncodingLayer = &cxSubLayer; int nSubLayerSectionIndex = findLayerSectionIndex (&igHeader, pLayerSections, nSubLayerOwnerId, bIsSubLayerOwner); if (nSubLayerSectionIndex < 0) throw IGEXCEPTION (CxImageIGException, "EncodeLayer", "findLayerSectionIndex failed"); pLayerSections [nSubLayerSectionIndex].pnSubLayers [pLayerSections [nSubLayerSectionIndex].nSubLayers++] = nLayerSectionIndex; } ProgressSetRange (pLayer->GetHeight(), 0); ProgressSetMessage (L"Encoding layer..."); // fill layer offset and size pLayerSections [nLayerSectionIndex].nSubLayers = 0; pLayerSections [nLayerSectionIndex].ptOffset.x = p_rcSubLayer ? p_rcSubLayer->left : pLayer->info.xOffset; pLayerSections [nLayerSectionIndex].ptOffset.y = p_rcSubLayer ? p_rcSubLayer->top : pLayer->info.yOffset; pLayerSections [nLayerSectionIndex].ptSize.x = p_rcSubLayer ? p_rcSubLayer->right - p_rcSubLayer->left + 1 : pLayer->GetWidth(); pLayerSections [nLayerSectionIndex].ptSize.y = p_rcSubLayer ? p_rcSubLayer->bottom - p_rcSubLayer->top + 1 : pLayer->GetHeight(); if (nLayerSectionIndex == igHeader.nNbLayers) pLayerSections [nLayerSectionIndex].commonHeader.nSectionId = (BYTE)igHeader.nNbSections; pLayerSections [nLayerSectionIndex].commonHeader.nId = nLayerIdx; pLayerSections [nLayerSectionIndex].commonHeader.eSectionType = IGSECTION_LAYER; // set byte offset if (igHeader.nNbSections == 0) pLayerSections [nLayerSectionIndex].commonHeader.nFirstByteOffset = sizeof (IGHEADER) + sizeof (IGSECTIONHEADER_LAYER) * CXIMAGEIG_MAX_NBLAYERS + sizeof (IGSECTIONHEADER_SELECTION) * CXIMAGEIG_MAX_NBSELECTIONS; else pLayerSections [nLayerSectionIndex].commonHeader.nFirstByteOffset = findSectionFirstByteOffset (&igHeader, pLayerSections, pSelectionSections, pLayerSections [nLayerSectionIndex].commonHeader.nSectionId); igHeader.nNbLayers = nLayerSectionIndex + 1; if (igHeader.nNbSelections == 0) { igHeader.nNbSections = pLayerSections [nLayerSectionIndex].commonHeader.nSectionId + 1; igHeader.nNbSelections = igHeader.nNbSections - igHeader.nNbLayers; } else { if (pSelectionSections [igHeader.nNbSelections - 1].commonHeader.nSectionId == pLayerSections [nLayerSectionIndex].commonHeader.nSectionId + 1) igHeader.nNbSections = igHeader.nNbSelections + igHeader.nNbLayers; else { igHeader.nNbSections = pLayerSections [nLayerSectionIndex].commonHeader.nSectionId + 1; igHeader.nNbSelections = igHeader.nNbSections - igHeader.nNbLayers; } } // write layer pixels if (!file.Seek (pLayerSections [nLayerSectionIndex].commonHeader.nFirstByteOffset, SEEK_SET)) throw IGEXCEPTION (CxImageIGException, "EncodeLayer", "file.Seek failed"); // set max JPEG quality float fCurQuality = pEncodingLayer->GetJpegQualityF(); pEncodingLayer->SetJpegQualityF (100.0f); // JPEG encoding BYTE *pBuf = new BYTE [CXIMAGEIG_INIT_LAYERSIZE]; CxMemFile memFile (pBuf, CXIMAGEIG_INIT_LAYERSIZE); if (!pEncodingLayer->Encode (&memFile, CXIMAGEIG_LAYERFORMAT)) throw IGEXCEPTION (CxImageIGException, "EncodeLayer", "pEncodingLayer->Encode failed"); BYTE *pBufImg = memFile.GetBuffer(); long nSizeBufImg = memFile.Tell(); // reset JPEG quality pEncodingLayer->SetJpegQualityF (fCurQuality); file.Write (pBufImg, nSizeBufImg, 1); delete [] pBufImg; pLayerSections [nLayerSectionIndex].commonHeader.nSizeBuf = nSizeBufImg; // write layer alpha if (!pEncodingLayer->pAlpha) pEncodingLayer->AlphaCreate(255); int nNbPixels = pEncodingLayer->GetWidth() * pEncodingLayer->GetHeight(); file.Write (pEncodingLayer->pAlpha, nNbPixels, 1); pLayerSections [nLayerSectionIndex].commonHeader.nEndByteOffset = pLayerSections [nLayerSectionIndex].commonHeader.nFirstByteOffset + nSizeBufImg + nNbPixels; // write new headers if (!encodeHeader (&file, &igHeader)) throw IGEXCEPTION (CxImageIGException, "EncodeLayer", "encodeHeader failed"); if (!encodeSections (&file, &igHeader, pLayerSections, pSelectionSections)) throw IGEXCEPTION (CxImageIGException, "EncodeLayer", "encodeSections failed"); delete [] pLayerSections; delete [] pSelectionSections; return true; }
////////////////////////////////////////////////////////////////////////////// // CMainFrame message handlers void CMainFrame::OnEditPaste() { CDemoDoc *NewDoc=(CDemoDoc*)((CDemoApp*)AfxGetApp())->demoTemplate->OpenDocumentFile(NULL); if (NewDoc) { if (OpenClipboard()) { HANDLE hData=NULL; if (hData = GetClipboardData(((CDemoApp*)AfxGetApp())->GetCF())){ //custom CxImage object CxImage *newima = new CxImage(); DWORD dwSize = GlobalSize(hData); if (dwSize) { BYTE *lpVoid = (BYTE *)GlobalLock(hData); newima->UnDump(lpVoid); GlobalUnlock(lpVoid); } NewDoc->image = newima; } else if (hData = GetClipboardData(CF_DIB)){ // check if bitmap CxImage *newima = new CxImage(); newima->CreateFromHANDLE(hData); NewDoc->image = newima; } else { #if CXIMAGE_SUPPORT_WMF if (hData = GetClipboardData(CF_ENHMETAFILE)) //check if metafile { HENHMETAFILE hMeta = (HENHMETAFILE)hData; ENHMETAHEADER emh; GetEnhMetaFileHeader(hMeta, sizeof(emh), &emh); int cx,cy; cx = (int)((emh.rclBounds.right - emh.rclBounds.left)/2.54); cy = (int)((emh.rclBounds.bottom - emh.rclBounds.top)/2.54); HDC hDC0 = ::GetDC(0); // screen dc HBITMAP hBitmap = CreateCompatibleBitmap(hDC0, cx, cy); HDC hDC = CreateCompatibleDC(hDC0); // memory dc compatible with screen ::ReleaseDC(0, hDC0); // don't need anymore. get rid of it. if (hDC && hBitmap){ RECT rc = {0,0,cx,cy}; int bpp = ::GetDeviceCaps(hDC, BITSPIXEL); HBITMAP hBitmapOld = (HBITMAP)SelectObject(hDC, hBitmap); // paint the background DWORD dwBack = RGB(255, 255, 255); //GetSysColor(COLOR_WINDOW); DWORD OldColor = SetBkColor(hDC, dwBack); ExtTextOut(hDC, 0, 0, ETO_OPAQUE, &rc, NULL, 0, NULL); SetBkColor(hDC, OldColor); // Play the Metafile into Memory DC BOOL bRet = PlayEnhMetaFile(hDC, hMeta, &rc); SelectObject(hDC, hBitmapOld); CxImage *newima = new CxImage(); if(bRet && newima->Create(cx, cy, bpp, CXIMAGE_FORMAT_WMF)){ bRet = GetDIBits(hDC, hBitmap, 0, (UINT)cy, newima->GetBits(), (LPBITMAPINFO)newima->GetDIB(), DIB_RGB_COLORS); NewDoc->image = newima; } else { delete newima; } } if (hBitmap) DeleteObject(hBitmap); if (hDC) DeleteDC(hDC); } #endif } } CloseClipboard(); CString s; s.Format(_T("Clipboard Image %d"),((CDemoApp*)AfxGetApp())->m_nDocCount++); NewDoc->SetTitle(s); NewDoc->UpdateAllViews(0,WM_USER_NEWIMAGE); NewDoc->UpdateStatusBar(); } }
__declspec(dllexport) bool LoadImageFromMemory(const BYTE *buffer, unsigned int size, const char *mime, unsigned int maxwidth, unsigned int maxheight, ImageInfo *info) { if (!buffer || !size || !mime || !info) return false; // load the image DWORD dwImageType = CXIMAGE_FORMAT_UNKNOWN; if (strlen(mime)) dwImageType = GetImageType(mime); if (dwImageType == CXIMAGE_FORMAT_UNKNOWN) dwImageType = DetectFileType(buffer, size); if (dwImageType == CXIMAGE_FORMAT_UNKNOWN) { printf("PICTURE::LoadImageFromMemory: Unable to determine image type."); return false; } CxImage *image = new CxImage(dwImageType); if (!image) return false; int actualwidth = maxwidth; int actualheight = maxheight; try { bool success = image->Decode((BYTE*)buffer, size, dwImageType, actualwidth, actualheight); if (!success && dwImageType != CXIMAGE_FORMAT_UNKNOWN) { // try to decode with unknown imagetype success = image->Decode((BYTE*)buffer, size, CXIMAGE_FORMAT_UNKNOWN); } if (!success || !image->IsValid()) { printf("PICTURE::LoadImageFromMemory: Unable to decode image. Error:%s\n", image->GetLastError()); delete image; return false; } } catch (...) { printf("PICTURE::LoadImageFromMemory: Unable to decode image."); delete image; return false; } // ok, now resample the image down if necessary if (ResampleKeepAspect(*image, maxwidth, maxheight) < 0) { printf("PICTURE::LoadImage: Unable to resample picture\n"); delete image; return false; } // make sure our image is 24bit minimum image->IncreaseBpp(24); // fill in our struct info->width = image->GetWidth(); info->height = image->GetHeight(); info->originalwidth = actualwidth; info->originalheight = actualheight; memcpy(&info->exifInfo, image->GetExifInfo(), sizeof(EXIFINFO)); // create our texture info->context = image; info->texture = image->GetBits(); info->alpha = image->AlphaGetBits(); return (info->texture != NULL); };
// return number of faces and score void detectFaces(const CxImage& img, std::list <Face>& lFaces, int nRot) { if (cascade_el == NULL || cascade_er == NULL || cascade_f == NULL || cascade_m == NULL || cascade_n == NULL) init(); CxImage imgRotated (img); imgRotated.Rotate180(); IplImage *pIplImage = cvCreateImageHeader (cvSize (img.GetWidth(), img.GetHeight()), 8, img.GetBpp() / 8); pIplImage->imageDataOrigin = (char*)imgRotated.GetDIB(); pIplImage->imageData = (char*)imgRotated.GetBits(); CvMemStorage *storage; /* setup memory storage, needed by the object detector */ storage = cvCreateMemStorage(0); assert(cascade_f && cascade_el && cascade_er && cascade_m && cascade_n && storage); /* haar detection for faces */ CvSeq *pFaces = cvHaarDetectObjects( pIplImage, cascade_f, storage, 1.2, 2, CV_HAAR_DO_CANNY_PRUNING, cvSize( 40, 40 ) ); if (pFaces->total == 0) return; CvSeq seqFaces (*pFaces); list <CvRect> lrFaces; for(int idxFace = 0; idxFace < seqFaces.total; idxFace++ ) { CvRect *r = (CvRect*)cvGetSeqElem(&seqFaces, idxFace); if (r) lrFaces.push_back (*r); } /* haar detection for eyes */ list<pair <RECT, RECT>> lpairEyes; detect_eyes (storage, pIplImage, lpairEyes); /* haar detection for mouths */ list<RECT> lMouths; detect_mouths (storage, pIplImage, lMouths); /* haar detection for noses */ list<RECT> lNoses; detect_noses (storage, pIplImage, lNoses); for(list <CvRect>::iterator itFace = lrFaces.begin(); itFace != lrFaces.end(); ++itFace) { CvRect r (*itFace); CvRect rFace (*itFace); rFace.x = img.GetWidth() - r.x - r.width; rFace.y = img.GetHeight() - r.y - r.height; RECT rcFace; rcFace.left = img.GetWidth() - r.x - r.width - 10; rcFace.right = img.GetWidth() - r.x + 10; rcFace.top = img.GetHeight() - r.y - r.height - 10; rcFace.bottom = img.GetHeight() - r.y + 10; /* detect current eyes */ pair <RECT, RECT> pairEyes; match_eyes_with_face (lpairEyes, rFace, pairEyes); /* detect current mouth */ RECT rcMouth; match_mouths_with_face (lMouths, rFace, pairEyes, rcMouth); /* detect current nose */ RECT rcNose; match_noses_with_face (lNoses, rFace, pairEyes, rcMouth, rcNose); lFaces.push_back (Face (rcFace, pairEyes, rcMouth, rcNose, nRot)); } Face::adjustRectFaces(lFaces); cvReleaseMemStorage (&storage); cvReleaseImageHeader (&pIplImage); }