void CGUI::Xeromyces_ReadObject(XMBElement Element, CXeromyces* pFile, IGUIObject* pParent, std::vector<std::pair<CStr, CStr> >& NameSubst, boost::unordered_set<VfsPath>& Paths, u32 nesting_depth) { ENSURE(pParent); XMBAttributeList attributes = Element.GetAttributes(); CStr type(attributes.GetNamedItem(pFile->GetAttributeID("type"))); if (type.empty()) type = "empty"; // Construct object from specified type // henceforth, we need to do a rollback before aborting. // i.e. releasing this object IGUIObject* object = ConstructObject(type); if (!object) { LOGERROR("GUI: Unrecognized object type \"%s\"", type.c_str()); return; } // Cache some IDs for element attribute names, to avoid string comparisons #define ELMT(x) int elmt_##x = pFile->GetElementID(#x) #define ATTR(x) int attr_##x = pFile->GetAttributeID(#x) ELMT(object); ELMT(action); ELMT(repeat); ELMT(translatableAttribute); ELMT(translate); ELMT(attribute); ELMT(keep); ELMT(include); ATTR(style); ATTR(type); ATTR(name); ATTR(hotkey); ATTR(z); ATTR(on); ATTR(file); ATTR(directory); ATTR(id); ATTR(context); // // Read Style and set defaults // // If the setting "style" is set, try loading that setting. // // Always load default (if it's available) first! // CStr argStyle(attributes.GetNamedItem(attr_style)); if (m_Styles.count("default") == 1) object->LoadStyle(*this, "default"); if (!argStyle.empty()) { if (m_Styles.count(argStyle) == 0) LOGERROR("GUI: Trying to use style '%s' that doesn't exist.", argStyle.c_str()); else object->LoadStyle(*this, argStyle); } bool NameSet = false; bool ManuallySetZ = false; CStrW inclusionPath; CStr hotkeyTag; for (XMBAttribute attr : attributes) { // If value is "null", then it is equivalent as never being entered if (CStr(attr.Value) == "null") continue; // Ignore "type" and "style", we've already checked it if (attr.Name == attr_type || attr.Name == attr_style) continue; if (attr.Name == attr_name) { CStr name(attr.Value); for (const std::pair<CStr, CStr>& sub : NameSubst) name.Replace(sub.first, sub.second); object->SetName(name); NameSet = true; continue; } if (attr.Name == attr_hotkey) hotkeyTag = attr.Value; if (attr.Name == attr_z) ManuallySetZ = true; if (object->SetSetting(pFile->GetAttributeString(attr.Name), attr.Value.FromUTF8(), true) != PSRETURN_OK) LOGERROR("GUI: (object: %s) Can't set \"%s\" to \"%s\"", object->GetPresentableName(), pFile->GetAttributeString(attr.Name), attr.Value); } // Check if name isn't set, generate an internal name in that case. if (!NameSet) { object->SetName("__internal(" + CStr::FromInt(m_InternalNameNumber) + ")"); ++m_InternalNameNumber; } if (!hotkeyTag.empty()) m_HotkeyObjects[hotkeyTag].push_back(object); CStrW caption(Element.GetText().FromUTF8()); if (!caption.empty()) object->SetSetting("caption", caption, true); for (XMBElement child : Element.GetChildNodes()) { // Check what name the elements got int element_name = child.GetNodeName(); if (element_name == elmt_object) { // Call this function on the child Xeromyces_ReadObject(child, pFile, object, NameSubst, Paths, nesting_depth); } else if (element_name == elmt_action) { // Scripted <action> element // Check for a 'file' parameter CStrW filename(child.GetAttributes().GetNamedItem(attr_file).FromUTF8()); CStr code; // If there is a file, open it and use it as the code if (!filename.empty()) { Paths.insert(filename); CVFSFile scriptfile; if (scriptfile.Load(g_VFS, filename) != PSRETURN_OK) { LOGERROR("Error opening GUI script action file '%s'", utf8_from_wstring(filename)); throw PSERROR_GUI_JSOpenFailed(); } code = scriptfile.DecodeUTF8(); // assume it's UTF-8 } XMBElementList grandchildren = child.GetChildNodes(); if (!grandchildren.empty()) // The <action> element contains <keep> and <translate> tags. for (XMBElement grandchild : grandchildren) { if (grandchild.GetNodeName() == elmt_translate) code += g_L10n.Translate(grandchild.GetText()); else if (grandchild.GetNodeName() == elmt_keep) code += grandchild.GetText(); } else // It’s pure JavaScript code. // Read the inline code (concatenating to the file code, if both are specified) code += CStr(child.GetText()); CStr action = CStr(child.GetAttributes().GetNamedItem(attr_on)); // We need to set the GUI this object belongs to because RegisterScriptHandler requires an associated GUI. object->SetGUI(this); object->RegisterScriptHandler(action.LowerCase(), code, this); } else if (element_name == elmt_repeat) { Xeromyces_ReadRepeat(child, pFile, object, NameSubst, Paths, nesting_depth); } else if (element_name == elmt_translatableAttribute) { // This is an element in the form “<translatableAttribute id="attributeName">attributeValue</translatableAttribute>”. CStr attributeName(child.GetAttributes().GetNamedItem(attr_id)); // Read the attribute name. if (attributeName.empty()) { LOGERROR("GUI: ‘translatableAttribute’ XML element with empty ‘id’ XML attribute found. (object: %s)", object->GetPresentableName().c_str()); continue; } CStr value(child.GetText()); if (value.empty()) continue; CStr context(child.GetAttributes().GetNamedItem(attr_context)); // Read the context if any. if (!context.empty()) { CStr translatedValue(g_L10n.TranslateWithContext(context, value)); object->SetSetting(attributeName, translatedValue.FromUTF8(), true); } else { CStr translatedValue(g_L10n.Translate(value)); object->SetSetting(attributeName, translatedValue.FromUTF8(), true); } } else if (element_name == elmt_attribute) { // This is an element in the form “<attribute id="attributeName"><keep>Don’t translate this part // </keep><translate>but translate this one.</translate></attribute>”. CStr attributeName(child.GetAttributes().GetNamedItem(attr_id)); // Read the attribute name. if (attributeName.empty()) { LOGERROR("GUI: ‘attribute’ XML element with empty ‘id’ XML attribute found. (object: %s)", object->GetPresentableName().c_str()); continue; } CStr translatedValue; for (XMBElement grandchild : child.GetChildNodes()) { if (grandchild.GetNodeName() == elmt_translate) translatedValue += g_L10n.Translate(grandchild.GetText()); else if (grandchild.GetNodeName() == elmt_keep) translatedValue += grandchild.GetText(); } object->SetSetting(attributeName, translatedValue.FromUTF8(), true); } else if (element_name == elmt_include) { CStrW filename(child.GetAttributes().GetNamedItem(attr_file).FromUTF8()); CStrW directory(child.GetAttributes().GetNamedItem(attr_directory).FromUTF8()); if (!filename.empty()) { if (!directory.empty()) LOGWARNING("GUI: Include element found with file name (%s) and directory name (%s). Only the file will be processed.", utf8_from_wstring(filename), utf8_from_wstring(directory)); Paths.insert(filename); CXeromyces XeroIncluded; if (XeroIncluded.Load(g_VFS, filename, "gui") != PSRETURN_OK) { LOGERROR("GUI: Error reading included XML: '%s'", utf8_from_wstring(filename)); continue; } XMBElement node = XeroIncluded.GetRoot(); if (node.GetNodeName() != XeroIncluded.GetElementID("object")) { LOGERROR("GUI: Error reading included XML: '%s', root element must have be of type 'object'.", utf8_from_wstring(filename)); continue; } if (nesting_depth+1 >= MAX_OBJECT_DEPTH) { LOGERROR("GUI: Too many nested GUI includes. Probably caused by a recursive include attribute. Abort rendering '%s'.", utf8_from_wstring(filename)); continue; } Xeromyces_ReadObject(node, &XeroIncluded, object, NameSubst, Paths, nesting_depth+1); } else if (!directory.empty()) { if (nesting_depth+1 >= MAX_OBJECT_DEPTH) { LOGERROR("GUI: Too many nested GUI includes. Probably caused by a recursive include attribute. Abort rendering '%s'.", utf8_from_wstring(directory)); continue; } VfsPaths pathnames; vfs::GetPathnames(g_VFS, directory, L"*.xml", pathnames); for (const VfsPath& path : pathnames) { // as opposed to loading scripts, don't care if it's loaded before // one might use the same parts of the GUI in different situations Paths.insert(path); CXeromyces XeroIncluded; if (XeroIncluded.Load(g_VFS, path, "gui") != PSRETURN_OK) { LOGERROR("GUI: Error reading included XML: '%s'", path.string8()); continue; } XMBElement node = XeroIncluded.GetRoot(); if (node.GetNodeName() != XeroIncluded.GetElementID("object")) { LOGERROR("GUI: Error reading included XML: '%s', root element must have be of type 'object'.", path.string8()); continue; } Xeromyces_ReadObject(node, &XeroIncluded, object, NameSubst, Paths, nesting_depth+1); } } else LOGERROR("GUI: 'include' XML element must have valid 'file' or 'directory' attribute found. (object %s)", object->GetPresentableName().c_str()); } else { // Try making the object read the tag. if (!object->HandleAdditionalChildren(child, pFile)) LOGERROR("GUI: (object: %s) Reading unknown children for its type", object->GetPresentableName().c_str()); } } if (!ManuallySetZ) { // Set it automatically to 10 plus its parents bool absolute; GUI<bool>::GetSetting(object, "absolute", absolute); if (absolute) // If the object is absolute, we'll have to get the parent's Z buffered, // and add to that! GUI<float>::SetSetting(object, "z", pParent->GetBufferedZ() + 10.f, true); else // If the object is relative, then we'll just store Z as "10" GUI<float>::SetSetting(object, "z", 10.f, true); } try { if (pParent == m_BaseObject) AddObject(object); else pParent->AddChild(object); } catch (PSERROR_GUI& e) { LOGERROR("GUI error: %s", e.what()); } }
CStr::CStr(CStr const &s) { strcpy(this.sData,s.get()); }
void GrabCutMF::Demo(CStr &wkDir, float w1, float w2, float w3, float alpha, float beta, float gama, float mu) { CStr imgDir = wkDir + "Imgs/", salDir = wkDir + "Sal4N/", iluDir = wkDir + "Ilu4N/"; vecS namesNE; int imgNum = CmFile::GetNamesNE(imgDir + "*.jpg", namesNE); CmFile::MkDir(salDir); CmFile::MkDir(iluDir); printf("w1 = %g, w2 = %g, w3 = %g, alpha = %g, beta = %g, gama = %g, mu = %g\n", w1, w2, w3, alpha, beta, gama, mu); // Number of labels //const int M = 2; CmTimer tm("Time"), tmIni("TimeIni"), tmRef("TimeRef"); double maxWeight = 2; // 2: 0.958119, 1: 0.953818, tm.Start(); #pragma omp parallel for for (int i = 0; i < imgNum; i++){ printf("Processing %d/%d: %s%s.jpg%20s\r\n", i, imgNum, _S(imgDir), _S(namesNE[i]), ""); CmFile::Copy(imgDir + namesNE[i] + ".jpg", salDir + namesNE[i] + ".jpg"); //CmFile::Copy(imgDir + namesNE[i] + ".png", salDir + namesNE[i] + "_GT.png"); Mat _imMat3u = imread(imgDir + namesNE[i] + ".jpg"), imMat3f, imMat3u, gt1u; Mat _gt1u = imread(imgDir + namesNE[i] + ".png", CV_LOAD_IMAGE_GRAYSCALE); if(_gt1u.rows == 0 && _gt1u.cols == 0) { cout<<"Error: unable to open "<<(imgDir + namesNE[i] + ".png")<<endl; continue; } blur(_gt1u, _gt1u, Size(3,3)); Mat _res1u = Mat::zeros(_imMat3u.size(), CV_8U); Rect wkRect = CmCv::GetMaskRange(_gt1u, 30, 200); _imMat3u(wkRect).copyTo(imMat3u); _gt1u(wkRect).copyTo(gt1u); imMat3u.convertTo(imMat3f, CV_32FC3, 1/255.0); Rect rect = CmCv::GetMaskRange(gt1u, 5, 128); Mat edge1u; // Use an edge map to expand the background mask in flat (no edge) region CmCv::CannySimpleRGB(imMat3u, edge1u, 120, 1200, 5); dilate(edge1u, edge1u, Mat(), Point(-1, -1), 3); Mat borderMask1u(imMat3u.size(), CV_8U), tmpMask; memset(borderMask1u.data, 255, borderMask1u.step.p[0] * borderMask1u.rows); borderMask1u(rect) = Scalar(0); getGrabMask(edge1u, borderMask1u); //* The Mean field based GrabCut //tmIni.Start(); GrabCutMF cutMF(imMat3f, imMat3u, salDir + namesNE[i], w1, w2, w3, alpha, beta, gama, mu); //Mat borderMask1u = CmCv::getGrabMask(imMat3u, rect), tmpMask; imwrite(salDir + namesNE[i] + "_BM.png", borderMask1u); imwrite(salDir + namesNE[i] + ".jpg", imMat3u); //imwrite(salDir + namesNE[i] + "_GT.png", gt1u); cutMF.initialize(rect, borderMask1u, (float)maxWeight, true); //cutMF.setGrabReg(rect, CmCv::getGrabMask(imMat3u, rect)); //tmIni.Stop(); //tmRef.Start(); cutMF.refine(); //tmRef.Stop(); Mat res1u = cutMF.drawResult(), invRes1u; res1u.copyTo(_res1u(wkRect)); imwrite(salDir + namesNE[i] + "_GCMF1.png", _res1u); //if (sum(res1u).val[0] < EPS){ // printf("%s.jpg don't contains a salient object\n", _S(namesNE[i])); // continue; //} dilate(res1u(rect), tmpMask, Mat(), Point(-1, -1), 10); bitwise_not(tmpMask, borderMask1u(rect)); getGrabMask(edge1u, borderMask1u); //blur(res1u, invRes1u, Size(3, 3)); // //PointSeti hullPnts; //convexHullOfMask(invRes1u, hullPnts); //fillConvexPoly(invRes1u, hullPnts, 255); //bitwise_not(invRes1u, invRes1u); //bitwise_or(invRes1u, borderMask1u, borderMask1u); imwrite(salDir + namesNE[i] + "_MB2.png", borderMask1u); //double w = maxWeight - (maxWeight-1)*sum(res1u).val[0]/(borderMask1u.rows*borderMask1u.cols*255 - sum(borderMask1u).val[0]); cutMF.initialize(rect, borderMask1u, 2); cutMF.refine(); //printf("weight = %g\n", w); //imshow("Result", res1u); //imshow("Possible", borderMask1u); //imshow("Image", imMat3f); //waitKey(0); res1u = cutMF.drawResult(); Rect rectRes = CmCv::GetMaskRange(res1u, 5, 128); if (rectRes.width * 1.1 < rect.width || rectRes.height * 1.1 < rect.height){ // Too short result printf("%s.jpg contains a small object\n", _S(namesNE[i])); memset(borderMask1u.data, 255, borderMask1u.step.p[0] * borderMask1u.rows); borderMask1u(rect) = Scalar(0); cutMF.initialize(rect, borderMask1u, 2); cutMF.refine(); res1u = cutMF.drawResult(); imwrite(salDir + namesNE[i] + "_MB2.png", borderMask1u); CmFile::Copy2Dir(salDir + namesNE[i] + "*.*", iluDir); imwrite(iluDir + namesNE[i] + "_GCMF.png", _res1u); } res1u.copyTo(_res1u(wkRect)); imwrite(salDir + namesNE[i] + "_GCMF.png", _res1u); } tm.Stop(); double avgTime = tm.TimeInSeconds()/imgNum; printf("Speed: %gs, %gfps\t\t\n", avgTime, 1/avgTime); //tmIni.Report(); //tmRef.Report(); //CmEvaluation::EvalueMask(imgDir + "*.png", salDir, ".png", "_GC.png"); char* pDes[] = { "GCMF1", "GCMF"}; //, "CudaG4", "Onecut", "GC", "CudaH", vecS des = charPointers2StrVec (pDes); CStr rootDir = CmFile::GetFatherFolder(wkDir), dbName = CmFile::GetNameNE(wkDir.substr(0, wkDir.size() - 1)); CmEvaluation::EvalueMask(imgDir + "*.png", salDir, des, wkDir.substr(0, wkDir.size() - 1) + "Res.m", 0.3, false, "", dbName); }
template <> void XMLWriter_File::ElementAttribute<CStr>(const char* name, const CStr& value, bool newelement) { ElementAttribute(name, value.c_str(), newelement); }
void CGUI::Xeromyces_ReadSprite(XMBElement Element, CXeromyces* pFile) { // Sprite object we're adding CGUISprite* Sprite = new CGUISprite; // and what will be its reference name CStr name; // // Read Attributes // // Get name, we know it exists because of DTD requirements name = Element.GetAttributes().GetNamedItem( pFile->GetAttributeID("name") ); if (m_Sprites.find(name) != m_Sprites.end()) LOGWARNING(L"GUI sprite name '%hs' used more than once; first definition will be discarded", name.c_str()); // // Read Children (the images) // SGUIImageEffects* effects = NULL; // Iterate children XMBElementList children = Element.GetChildNodes(); for (int i=0; i<children.Count; ++i) { // Get node XMBElement child = children.Item(i); CStr ElementName (pFile->GetElementString(child.GetNodeName())); if (ElementName == "image") { Xeromyces_ReadImage(child, pFile, *Sprite); } else if (ElementName == "effect") { if (effects) { LOGERROR(L"GUI <sprite> must not have more than one <effect>"); } else { effects = new SGUIImageEffects; Xeromyces_ReadEffects(child, pFile, *effects); } } else { debug_warn(L"Invalid data - DTD shouldn't allow this"); } } // Apply the effects to every image (unless the image overrides it with // different effects) if (effects) for (std::vector<SGUIImage*>::iterator it = Sprite->m_Images.begin(); it != Sprite->m_Images.end(); ++it) if (! (*it)->m_Effects) (*it)->m_Effects = new SGUIImageEffects(*effects); // do a copy just so it can be deleted correctly later delete effects; // // Add Sprite // m_Sprites[name] = Sprite; }
void CGUI::Xeromyces_ReadObject(XMBElement Element, CXeromyces* pFile, IGUIObject *pParent, const std::vector<std::pair<CStr, CStr> >& NameSubst, boost::unordered_set<VfsPath>& Paths) { ENSURE(pParent); int i; // Our object we are going to create IGUIObject *object = NULL; XMBAttributeList attributes = Element.GetAttributes(); // Well first of all we need to determine the type CStr type (attributes.GetNamedItem(pFile->GetAttributeID("type"))); if (type.empty()) type = "empty"; // Construct object from specified type // henceforth, we need to do a rollback before aborting. // i.e. releasing this object object = ConstructObject(type); if (!object) { // Report error that object was unsuccessfully loaded LOGERROR(L"GUI: Unrecognized object type \"%hs\"", type.c_str()); return; } // Cache some IDs for element attribute names, to avoid string comparisons #define ELMT(x) int elmt_##x = pFile->GetElementID(#x) #define ATTR(x) int attr_##x = pFile->GetAttributeID(#x) ELMT(object); ELMT(action); ELMT(repeat); ATTR(style); ATTR(type); ATTR(name); ATTR(hotkey); ATTR(z); ATTR(on); ATTR(file); // // Read Style and set defaults // // If the setting "style" is set, try loading that setting. // // Always load default (if it's available) first! // CStr argStyle (attributes.GetNamedItem(attr_style)); if (m_Styles.count("default") == 1) object->LoadStyle(*this, "default"); if (! argStyle.empty()) { // additional check if (m_Styles.count(argStyle) == 0) { LOGERROR(L"GUI: Trying to use style '%hs' that doesn't exist.", argStyle.c_str()); } else object->LoadStyle(*this, argStyle); } // // Read Attributes // bool NameSet = false; bool ManuallySetZ = false; // if z has been manually set, this turn true CStr hotkeyTag; // Now we can iterate all attributes and store for (i=0; i<attributes.Count; ++i) { XMBAttribute attr = attributes.Item(i); // If value is "null", then it is equivalent as never being entered if (CStr(attr.Value) == "null") continue; // Ignore "type" and "style", we've already checked it if (attr.Name == attr_type || attr.Name == attr_style) continue; // Also the name needs some special attention if (attr.Name == attr_name) { CStr name (attr.Value); // Apply the requested substitutions for (size_t j = 0; j < NameSubst.size(); ++j) name.Replace(NameSubst[j].first, NameSubst[j].second); object->SetName(name); NameSet = true; continue; } // Wire up the hotkey tag, if it has one if (attr.Name == attr_hotkey) hotkeyTag = attr.Value; if (attr.Name == attr_z) ManuallySetZ = true; // Try setting the value if (object->SetSetting(pFile->GetAttributeString(attr.Name), attr.Value.FromUTF8(), true) != PSRETURN_OK) { LOGERROR(L"GUI: (object: %hs) Can't set \"%hs\" to \"%ls\"", object->GetPresentableName().c_str(), pFile->GetAttributeString(attr.Name).c_str(), attr.Value.FromUTF8().c_str()); // This is not a fatal error } } // Check if name isn't set, generate an internal name in that case. if (!NameSet) { object->SetName("__internal(" + CStr::FromInt(m_InternalNameNumber) + ")"); ++m_InternalNameNumber; } // Attempt to register the hotkey tag, if one was provided if (! hotkeyTag.empty()) m_HotkeyObjects[hotkeyTag].push_back(object); CStrW caption (Element.GetText().FromUTF8()); if (! caption.empty()) { // Set the setting caption to this object->SetSetting("caption", caption, true); // There is no harm if the object didn't have a "caption" } // // Read Children // // Iterate children XMBElementList children = Element.GetChildNodes(); for (i=0; i<children.Count; ++i) { // Get node XMBElement child = children.Item(i); // Check what name the elements got int element_name = child.GetNodeName(); if (element_name == elmt_object) { // Call this function on the child Xeromyces_ReadObject(child, pFile, object, NameSubst, Paths); } else if (element_name == elmt_action) { // Scripted <action> element // Check for a 'file' parameter CStrW filename (child.GetAttributes().GetNamedItem(attr_file).FromUTF8()); CStr code; // If there is a file, open it and use it as the code if (! filename.empty()) { Paths.insert(filename); CVFSFile scriptfile; if (scriptfile.Load(g_VFS, filename) != PSRETURN_OK) { LOGERROR(L"Error opening GUI script action file '%ls'", filename.c_str()); throw PSERROR_GUI_JSOpenFailed(); } code = scriptfile.DecodeUTF8(); // assume it's UTF-8 } // Read the inline code (concatenating to the file code, if both are specified) code += CStr(child.GetText()); CStr action = CStr(child.GetAttributes().GetNamedItem(attr_on)); // We need to set the GUI this object belongs to because RegisterScriptHandler requires an associated GUI. object->SetGUI(this); object->RegisterScriptHandler(action.LowerCase(), code, this); } else if (element_name == elmt_repeat) { Xeromyces_ReadRepeat(child, pFile, object, Paths); } else { // Try making the object read the tag. if (!object->HandleAdditionalChildren(child, pFile)) { LOGERROR(L"GUI: (object: %hs) Reading unknown children for its type", object->GetPresentableName().c_str()); } } } // // Check if Z wasn't manually set // if (!ManuallySetZ) { // Set it automatically to 10 plus its parents bool absolute; GUI<bool>::GetSetting(object, "absolute", absolute); // If the object is absolute, we'll have to get the parent's Z buffered, // and add to that! if (absolute) { GUI<float>::SetSetting(object, "z", pParent->GetBufferedZ() + 10.f, true); } else // If the object is relative, then we'll just store Z as "10" { GUI<float>::SetSetting(object, "z", 10.f, true); } } // // Input Child // try { if (pParent == m_BaseObject) AddObject(object); else pParent->AddChild(object); } catch (PSERROR_GUI& e) { LOGERROR(L"GUI error: %hs", e.what()); } }
void CFilePacker::PackString(const CStr& str) { const size_t length = str.length(); PackSize(length); PackRaw(str.c_str(), length); }