void StrokeMask::Read(const std::string& filename) { /** * The format of the .stroke file is: * stroke 255 * Mask.png */ std::string extension = Helpers::GetFileExtension(filename); if(extension != "stroke") { std::stringstream ss; ss << "StrokeMask cannot read files with extension other than .stroke! Specified file had extension ." << extension << " You might want ReadFromImage instead."; throw std::runtime_error(ss.str()); } //Create an input stream for file std::ifstream fin(filename.c_str()); if(!fin ) { throw std::runtime_error("File not found!"); } std::string line; std::stringstream linestream; std::string strokeString; int strokeValue; getline(fin, line); linestream.clear(); linestream << line; linestream >> strokeString >> strokeValue; if(strokeString != "stroke") { throw std::runtime_error("Invalid .stroke file!"); } std::cout << "strokeValue: " << strokeValue << std::endl; std::string imageFileName; getline(fin, imageFileName); if(imageFileName.length() == 0) { throw std::runtime_error("Image file name was empty!"); } std::string path = Helpers::GetPath(filename); std::string fullImageFileName = path + imageFileName; ReadFromImage(fullImageFileName, strokeValue); }
void Mask::Read(const std::string& filename) { /** * The format of the .mask file is: * hole 0 * valid 255 * Mask.png * * OR * * valid 255 * hole 0 * Mask.png * * That is, the "valid VALUE" line can be either on the first or second line. * Note that the 0 and 255 here are arbitrary and can be anything. */ std::string extension = GetFileExtension(filename); if(extension != "mask") { std::stringstream ss; ss << "Cannot read any file except .mask! Specified file was ." << extension << " You might want ReadFromImage instead."; throw std::runtime_error(ss.str()); } //Create an input stream for file std::ifstream fin(filename.c_str()); if(!fin ) { throw std::runtime_error("File not found!"); } std::string line; std::stringstream linestream; int holeValue = 0; int validValue = 0; std::string type1; std::string type2; int value1 = 0; int value2 = 0; getline(fin, line); linestream.clear(); linestream << line; linestream >> type1 >> value1; if(type1 == "hole") { holeValue = value1; } else if(type1 == "valid") { validValue = value1; } else { throw std::runtime_error("Invalid .mask file!"); } getline(fin, line); linestream.clear(); linestream << line; linestream >> type2 >> value2; if(type1 == type2) { throw std::runtime_error("Invalid .mask file! Valid or hole value listed twice!"); } if(type2 == "hole") { holeValue = value2; } else if(type2 == "valid") { validValue = value2; } else { throw std::runtime_error("Invalid .mask file!"); } std::string imageFileName; // linestream >> imageFileName; getline(fin, imageFileName); std::cout << "Mask image file: " << imageFileName << std::endl; std::string path = GetPath(filename); std::string fullImageFileName = path + imageFileName; std::cout << "Full mask image file: " << fullImageFileName << std::endl; ReadFromImage(fullImageFileName, HolePixelValueWrapper<int>(holeValue), ValidPixelValueWrapper<int>(validValue)); }
void ForegroundBackgroundSegmentMask::Read(const std::string& filename) { /** * The format of the .fbmask (foreground/background mask) file is: * foreground 0 * background 255 * Mask.png * * OR * * background 255 * foreground 0 * Mask.png * * That is, the "foreground [VALUE]" line can be either on the first or second line. * Note that the 0 and 255 here are arbitrary and can be anything. */ std::string extension = Helpers::GetFileExtension(filename); if(extension != "fbmask") { std::stringstream ss; ss << "Cannot read files with extension other than .fbmask! Specified file had extension ." << extension << " You might want ReadFromImage instead."; throw std::runtime_error(ss.str()); } //Create an input stream for file std::ifstream fin(filename.c_str()); if(!fin ) { throw std::runtime_error("File not found!"); } std::string line; std::stringstream linestream; int foregroundValue = 0; int backgroundValue = 0; std::string type1; std::string type2; int value1 = 0; int value2 = 0; getline(fin, line); linestream.clear(); linestream << line; linestream >> type1 >> value1; if(type1 == "foreground") { foregroundValue = value1; } else if(type1 == "background") { backgroundValue = value1; } else { throw std::runtime_error("Invalid .fbmask file!"); } getline(fin, line); linestream.clear(); linestream << line; linestream >> type2 >> value2; if(type1 == type2) { throw std::runtime_error("Invalid .fbmask file! Foreground or background value listed twice!"); } if(type2 == "foreground") { foregroundValue = value2; } else if(type2 == "background") { backgroundValue = value2; } else { throw std::runtime_error("Invalid .fbmask file!"); } std::cout << "ForegroundBackgroundSegmentMask read from " << filename << ": " << "foregroundValue: " << foregroundValue << " backgroundValue: " << backgroundValue << std::endl; std::string imageFileName; getline(fin, imageFileName); if(imageFileName.length() == 0) { throw std::runtime_error("Image file name was empty!"); } std::string path = Helpers::GetPath(filename); std::string fullImageFileName = path + imageFileName; ReadFromImage(fullImageFileName, ForegroundPixelValueWrapper<int>(foregroundValue), BackgroundPixelValueWrapper<int>(backgroundValue)); }