bool GOrgueBitmapCache::loadFile(wxImage& img, wxString filename) { GOrgueFilename name; name.Assign(filename, m_organfile); std::unique_ptr<GOrgueFile> file = name.Open(); if (!file->Open()) return false; unsigned length = file->GetSize(); char* data = (char*)malloc(length); if (!data) throw GOrgueOutOfMemory(); if (file->Read(data, length) != length) { free(data); return false; } file->Close(); wxMemoryInputStream is(data, length); bool result = img.LoadFile(is, wxBITMAP_TYPE_ANY, -1); free(data); return result; }
bool BM2CMP_FRAME::OpenProjectFiles( const std::vector<wxString>& aFileSet, int aCtl ) { // Prj().MaybeLoadProjectSettings(); m_BitmapFileName = aFileSet[0]; if( !m_Pict_Image.LoadFile( m_BitmapFileName ) ) { // LoadFile has its own UI, no need for further failure notification here return false; } m_Pict_Bitmap = wxBitmap( m_Pict_Image ); int h = m_Pict_Bitmap.GetHeight(); int w = m_Pict_Bitmap.GetWidth(); // Determine image resolution in DPI (does not existing in all formats). // the resolution can be given in bit per inches or bit per cm in file m_imageDPI.x = m_Pict_Image.GetOptionInt( wxIMAGE_OPTION_RESOLUTIONX ); m_imageDPI.y = m_Pict_Image.GetOptionInt( wxIMAGE_OPTION_RESOLUTIONY ); if( m_imageDPI.x > 1 && m_imageDPI.y > 1 ) { if( m_Pict_Image.GetOptionInt( wxIMAGE_OPTION_RESOLUTIONUNIT ) == wxIMAGE_RESOLUTION_CM ) { // When the initial resolution is given in bits per cm, // experience shows adding 1.27 to the resolution converted in dpi // before convert to int value reduce the conversion error // but it is not perfect m_imageDPI.x = m_imageDPI.x * 2.54 + 1.27; m_imageDPI.y = m_imageDPI.y * 2.54 + 1.27; } } else // fallback to the default value m_imageDPI.x = m_imageDPI.y = DEFAULT_DPI; // Display image info: // We are using ChangeValue here to avoid generating a wxEVT_TEXT event. m_DPIValueX->ChangeValue( wxString::Format( wxT( "%d" ), m_imageDPI.x ) ); m_DPIValueY->ChangeValue( wxString::Format( wxT( "%d" ), m_imageDPI.y ) ); updateImageInfo(); m_InitialPicturePanel->SetVirtualSize( w, h ); m_GreyscalePicturePanel->SetVirtualSize( w, h ); m_BNPicturePanel->SetVirtualSize( w, h ); m_Greyscale_Image.Destroy(); m_Greyscale_Image = m_Pict_Image.ConvertToGreyscale( ); if( m_rbOptions->GetSelection() > 0 ) NegateGreyscaleImage( ); m_Greyscale_Bitmap = wxBitmap( m_Greyscale_Image ); m_NB_Image = m_Greyscale_Image; Binarize( (double) m_sliderThreshold->GetValue()/m_sliderThreshold->GetMax() ); return true; }
void AffineTransformTestCase::setUp() { #if wxUSE_DC_TRANSFORM_MATRIX m_imgOrig.LoadFile("horse.jpg"); CPPUNIT_ASSERT( m_imgOrig.IsOk() ); m_bmpOrig = wxBitmap(m_imgOrig); #endif // wxUSE_DC_TRANSFORM_MATRIX }
bool hoxUtil::LoadBoardImage( const wxString& sImage, wxImage& image, hoxBoardImageInfo& imageInfo ) { const wxString sPrefixPath = hoxUtil::GetPath(hoxRT_BOARD); const wxString sIniFile = sPrefixPath + sImage.BeforeFirst('.') + ".ini"; hoxUtil::LoadBoardInfo( sIniFile, imageInfo ); const wxString imageFile = sPrefixPath + sImage; if ( ! image.LoadFile(imageFile, wxBITMAP_TYPE_PNG) ) { wxLogWarning("%s: Failed to load board-image from [%s].", __FUNCTION__, imageFile.c_str()); return false; } return true; }
wxString MyFrame::LoadUserImage(wxImage& image) { wxString filename; #if wxUSE_FILEDLG filename = wxLoadFileSelector(wxT("image"), wxEmptyString); if ( !filename.empty() ) { if ( !image.LoadFile(filename) ) { wxLogError(wxT("Couldn't load image from '%s'."), filename.c_str()); return wxEmptyString; } } #endif // wxUSE_FILEDLG return filename; }
hoxResult hoxUtil::LoadPieceImage( const wxString& sPath, hoxPieceType type, hoxColor color, wxImage& image) { const wxString sPrefixPath = hoxUtil::GetPath(hoxRT_PIECE) + "/" + sPath; const wxChar cColor = (color == hoxCOLOR_RED ? 'r' : 'b'); const wxString imageName = _piece_type_to_image_name( type ); const wxString sFullPath = wxString::Format("%s/%c%s", sPrefixPath.c_str(), cColor, imageName.c_str()); if ( ! image.LoadFile(sFullPath, wxBITMAP_TYPE_PNG) ) { wxLogError("%s: Failed to load piece from [%s].", __FUNCTION__, sFullPath.c_str()); return hoxRC_ERR; } return hoxRC_OK; }
//all shapes are loaded from same image file to reduce file I/O and caching //thiss also allows animated images to be self-contained void PianoRenderCache::Piano_load_shapes(RenderBuffer &buffer, const wxString& filename) { debug_function(10); //Debug debug("load_shapes('%s')", (const char*)filename.c_str()); debug(1, "load shapes file '%s'", (const char*)filename.c_str()); //reload shapes even if file name hasn't changed; color map might be different now // if (!CachedShapeFilename.CmpNoCase(filename)) { debug_more(2, ", no change"); return; } //no change if (!wxFileExists(filename)) return; Piano_flush_shapes(); //invalidate cached data if (!Shapes.LoadFile(filename, wxBITMAP_TYPE_ANY, 0) || !Shapes.IsOk()) { //wxMessageBox("Error loading image file: "+NewPictureName); Shapes.Clear(); return; } if (buffer.GetColorCount() < 2) return; //use colors from shapes file if no user-selected colors // int imgwidth=image.GetWidth(); // int imght =image.GetHeight(); // std::hash_map<WXCOLORREF, int> palcounts; //TODO: use wxImage.GetData for better performance? //TODO: use multiple images within same file? for (int y = Shapes.GetHeight() - 1; y >= 0; --y) //bottom->top for (int x = 0; x < Shapes.GetWidth(); ++x) //left->right if (!Shapes.IsTransparent(x, y)) { xlColor color, mapped; color.Set(Shapes.GetRed(x, y), Shapes.GetGreen(x, y), Shapes.GetBlue(x, y)); if (ColorMap.find(color.GetRGB()) != ColorMap.end()) continue; //already saw this color buffer.palette.GetColor(ColorMap.size() % buffer.GetColorCount(), mapped); //assign user-selected colors to shape palette sequentially, loop if run out of colors debug(10, "shape color[%d] 0x%x => user-selected color [%d] 0x%x", ColorMap.size(), color.GetRGB(), ColorMap.size() % GetColorCount(), mapped.GetRGB()); ColorMap[color.GetRGB()] = mapped; //.GetRGB(); // ShapePalette.push_back(c.GetRGB()); //keep a list of unique colors in order of occurrence from origin L-R, B-T } debug(2, "w %d, h %d, #colors %d", Shapes.GetWidth(), Shapes.GetHeight(), ColorMap.size()); CachedShapeFilename = filename; //don't load same file again }
bool PreviewGeneratorThread::createPreview(wxImage &localPreviewImage, PreviewInfo &previewInfo) { bool fileReadOK = false; { wxImage image2; // Temporarily switch off logging errors for component wx // This is to prevent telling the user in a pop-up window that loading a // malformed JPEG for the preview did not work wxLogLevel prevLevel = wxLog::GetLogLevel(); #if wxCHECK_VERSION(2, 9, 0) wxLog::SetComponentLevel(wxT("wx"), wxLOG_FatalError); #else wxLog::SetLogLevel(wxLOG_FatalError); #endif // Load JPEG image fileReadOK = localPreviewImage.LoadFile(previewInfo.filename1_, wxBITMAP_TYPE_JPEG); // TODO: Allow all types ... ? if(!previewInfo.filename2_.IsEmpty()) { fileReadOK = image2.LoadFile(previewInfo.filename2_, wxBITMAP_TYPE_JPEG); // TODO: Allow all types ... ? } #if wxCHECK_VERSION(2, 9, 0) wxLog::SetComponentLevel(wxT("wx"), prevLevel); #else wxLog::SetLogLevel(prevLevel); #endif if(previewInfo.type_ == PreviewInfo::PITKeypoints && previewInfo.keypointType_ != PreviewInfo::PIKPTNoKeypoints && !previewInfo.kpFilename1_.IsEmpty()) { // Load features from file Regard3DFeatures::FeatsR3D features; std::string kpfilename(previewInfo.kpFilename1_.mb_str()); if(openMVG::features::loadFeatsFromFile(kpfilename, features)) { // Convert to OpenCV keypoints std::vector< cv::KeyPoint > vec_keypoints; vec_keypoints.reserve(features.size()); for(size_t i = 0; i < features.size(); i++) { const Regard3DFeatures::FeatureR3D &ft = features[i]; vec_keypoints.push_back( cv::KeyPoint( ft.x(), ft.y(), ft.scale() * 2.0f, ft.orientation() ) ); } // Convert image to OpenCV cv::Mat img_cv; OpenCVHelper::convertWxImageToCVMat(localPreviewImage, img_cv); // Add keypoints to image cv::Mat img_outcv; int flags = cv::DrawMatchesFlags::DEFAULT; if(previewInfo.keypointType_ == PreviewInfo::PIKPTRichKeypoints) flags = cv::DrawMatchesFlags::DRAW_RICH_KEYPOINTS; cv::drawKeypoints(img_cv, vec_keypoints, img_outcv, cv::Scalar::all(-1), flags); //cv::imwrite("img_outcv.png", img_outcv); // Copy back to wxImage OpenCVHelper::convertCVMatToWxImage(img_outcv, localPreviewImage); } } if(previewInfo.type_ == PreviewInfo::PITMatches) { if(previewInfo.keypointType_ != PreviewInfo::PIKPTNoKeypoints && !previewInfo.matchesFilename_.IsEmpty()) { // Load features from file std::vector< cv::KeyPoint > vec_keypoints1, vec_keypoints2; Regard3DFeatures::FeatsR3D features; std::string kpfilename(previewInfo.kpFilename1_.mb_str()); if(openMVG::features::loadFeatsFromFile(kpfilename, features)) { // Convert to OpenCV keypoints vec_keypoints1.reserve(features.size()); for(size_t i = 0; i < features.size(); i++) { const Regard3DFeatures::FeatureR3D &ft = features[i]; vec_keypoints1.push_back( cv::KeyPoint( ft.x(), ft.y(), ft.scale() * 2.0f, ft.orientation() ) ); } } features.clear(); kpfilename = previewInfo.kpFilename2_.mb_str(); if(openMVG::features::loadFeatsFromFile(kpfilename, features)) { // Convert to OpenCV keypoints vec_keypoints2.reserve(features.size()); for(size_t i = 0; i < features.size(); i++) { const Regard3DFeatures::FeatureR3D &ft = features[i]; vec_keypoints2.push_back( cv::KeyPoint( ft.x(), ft.y(), ft.scale() * 2.0f, ft.orientation() ) ); } } // Load matches from file std::string sMatchFile(previewInfo.matchesFilename_.mb_str()); openMVG::matching::PairWiseMatches map_Matches; openMVG::matching::PairedIndMatchImport(sMatchFile, map_Matches); std::vector< cv::DMatch > matches1to2; if(previewInfo.enableTrackFilter_) { // Calculate tracks from matches openMVG::tracks::TracksBuilder tracksBuilder; openMVG::tracks::STLMAPTracks map_tracks; tracksBuilder.Build(map_Matches); tracksBuilder.Filter(); //-- Build tracks with STL compliant type : tracksBuilder.ExportToSTL(map_tracks); openMVG::tracks::STLMAPTracks map_tracksCommon; std::set<size_t> set_imageIndex; set_imageIndex.insert(previewInfo.index1_); set_imageIndex.insert(previewInfo.index2_); openMVG::tracks::TracksUtilsMap::GetTracksInImages(set_imageIndex, map_tracks, map_tracksCommon); openMVG::tracks::STLMAPTracks::const_iterator iterT = map_tracksCommon.begin(); while(iterT != map_tracksCommon.end()) { openMVG::tracks::submapTrack::const_iterator iter = iterT->second.begin(); size_t kpIndex1 = iter->second; iter++; size_t kpIndex2 = iter->second; matches1to2.push_back(cv::DMatch(kpIndex1, kpIndex2, 1.0f)); iterT++; } } else { openMVG::matching::PairWiseMatches::const_iterator iter = map_Matches.begin(); while(iter != map_Matches.end()) { size_t imgIndex1 = iter->first.first; size_t imgIndex2 = iter->first.second; if((imgIndex1 == previewInfo.index1_ && imgIndex2 == previewInfo.index2_) || (imgIndex1 == previewInfo.index2_ && imgIndex2 == previewInfo.index1_)) { const std::vector<openMVG::matching::IndMatch> &vec_FilteredMatches = iter->second; for(size_t i = 0; i < vec_FilteredMatches.size(); i++) { const openMVG::matching::IndMatch &match = vec_FilteredMatches[i]; size_t kpIndex1 = match._i; size_t kpIndex2 = match._j; matches1to2.push_back(cv::DMatch(kpIndex1, kpIndex2, 1.0f)); } } iter++; } } // Convert images to OpenCV cv::Mat img1, img2; OpenCVHelper::convertWxImageToCVMat(localPreviewImage, img1); OpenCVHelper::convertWxImageToCVMat(image2, img2); cv::Mat outimg; int flags = cv::DrawMatchesFlags::DEFAULT; if(previewInfo.keypointType_ == PreviewInfo::PIKPTRichKeypoints) flags = cv::DrawMatchesFlags::DRAW_RICH_KEYPOINTS; if(!previewInfo.showSingleKeypoints_) flags |= cv::DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS; cv::drawMatches(img1, vec_keypoints1, img2, vec_keypoints2, matches1to2, outimg, cv::Scalar::all(-1), cv::Scalar::all(-1), std::vector<char>(), flags); // Copy back to wxImage OpenCVHelper::convertCVMatToWxImage(outimg, localPreviewImage); } else { // Create an image with both images next to each other wxImage tmpImg; int w = localPreviewImage.GetWidth() + image2.GetWidth(); int h = std::max( localPreviewImage.GetHeight(), image2.GetHeight()); tmpImg.Create(w, h); tmpImg.Paste(localPreviewImage, 0, 0); tmpImg.Paste(image2, localPreviewImage.GetWidth(), 0); localPreviewImage = tmpImg; } } } return fileReadOK; }