// ///** //* Read a multi-page image //* //* This function merely calls the actual implementation above and returns itself. //* //* @param[in] filename File to load //* @param[in] mats Reference to C++ vector<Mat> object to hold the images //* @param[in] flags Flags you wish to set. //* //*/ //bool imreadmulti(const String& filename, std::vector<Mat>& mats, int flags) //{ // return imreadmulti_(filename, flags, mats); //} // static bool imwrite_( const String& filename, const Mat& image, const std::vector<int>& params, bool flipv ) { Mat temp; const Mat* pimage = ℑ CV_Assert( image.channels() == 1 || image.channels() == 3 || image.channels() == 4 ); ImageEncoder encoder = findEncoder( filename ); if( !encoder ) CV_Error( CV_StsError, "could not find a writer for the specified extension" ); if( !encoder->isFormatSupported(image.depth()) ) { CV_Assert( encoder->isFormatSupported(CV_8U) ); image.convertTo( temp, CV_8U ); pimage = &temp; } if( flipv ) { flip(*pimage, temp, 0); pimage = &temp; } encoder->setDestination( filename ); bool code = encoder->write( *pimage, params ); // CV_Assert( code ); return code; }
bool imencode( const string& ext, InputArray _image, vector<uchar>& buf, const vector<int>& params ) { Mat image = _image.getMat(); int channels = image.channels(); CV_Assert( channels == 1 || channels == 3 || channels == 4 ); ImageEncoder encoder = findEncoder( ext ); if( encoder.empty() ) CV_Error( CV_StsError, "could not find encoder for the specified extension" ); if( !encoder->isFormatSupported(image.depth()) ) { CV_Assert( encoder->isFormatSupported(CV_8U) ); Mat temp; image.convertTo(temp, CV_8U); image = temp; } bool code; if( encoder->setDestination(buf) ) { code = encoder->write(image, params); encoder->throwOnEror(); CV_Assert( code ); } else { string filename = tempfile(); code = encoder->setDestination(filename); CV_Assert( code ); code = encoder->write(image, params); encoder->throwOnEror(); CV_Assert( code ); FILE* f = fopen( filename.c_str(), "rb" ); CV_Assert(f != 0); fseek( f, 0, SEEK_END ); long pos = ftell(f); buf.resize((size_t)pos); fseek( f, 0, SEEK_SET ); buf.resize(fread( &buf[0], 1, buf.size(), f )); fclose(f); remove(filename.c_str()); } return code; }
bool imencode( const String& ext, const Mat& image, Vector<uchar>& buf, const Vector<int>& params ) { Mat temp; const Mat* pimage = ℑ int channels = image.channels(); CV_Assert( channels == 1 || channels == 3 || channels == 4 ); ImageEncoder encoder = findEncoder( ext ); if( !encoder.obj ) CV_Error( CV_StsError, "could not find encoder for the specified extension" ); if( !encoder->isFormatSupported(image.depth()) ) { CV_Assert( encoder->isFormatSupported(CV_8U) ); image.convertTo(temp, CV_8U); pimage = &temp; } bool code; if( encoder->setDestination(buf) ) { code = encoder->write(image, params); CV_Assert( code ); } else { char fnamebuf[L_tmpnam]; const char* filename = tmpnam(fnamebuf); code = encoder->setDestination(filename); CV_Assert( code ); code = encoder->write(image, params); CV_Assert( code ); FILE* f = fopen( filename, "rb" ); CV_Assert(f != 0); fseek( f, 0, SEEK_END ); long pos = ftell(f); buf.resize((size_t)pos); fseek( f, 0, SEEK_SET ); buf.resize(fread( &buf[0], 1, buf.size(), f )); fclose(f); unlink(filename); } return code; }
int main(int argc, char const* argv[]) { string address; string port; int threads = 1; fs::path db_path; namespace po = boost::program_options; po::options_description desc("Allowed options"); desc.add_options() ("help,h", "produce help message.") ("address", po::value(&address)->default_value("0.0.0.0"), "") ("port", po::value(&port)->default_value("18888"), "") ("db", po::value(&db_path), "") ; po::positional_options_description p; p.add("db", 1); po::variables_map vm; po::store(po::command_line_parser(argc, argv). options(desc).positional(p).run(), vm); po::notify(vm); if (vm.count("help") || db_path.empty()) { cout << "Usage:" << endl; cout << "\tserver ... <db>" << endl; cout << desc; cout << endl; return 0; } // Create a multiplexer for handling requests served::multiplexer mux; picpac::IndexedFileReader db(db_path); default_random_engine rng; magic_t cookie = magic_open(MAGIC_MIME_TYPE); CHECK(cookie); magic_load(cookie, NULL); // GET /hello mux.handle("/l") .get(no_throw([&db, &cookie](served::response &res, const served::request &req) { rfc3986::Form query(req.url().query()); rfc3986::Form trans; // transfer all applicable image parameters to trans // so we can later use that for image display #define PICPAC_CONFIG_UPDATE(C,P) \ { auto it = query.find(#P); if (it != query.end()) trans.insert(*it);} PICPAC_CONFIG_UPDATE_ALL(0); #undef PICPAC_CONFIG_UPDATE string ext = trans.encode(true); int count = query.get<int>("count", 20); string anno = query.get<string>("anno", ""); res << "<html><body><table><tr><th>Image</th></tr>"; for (unsigned i = 0; i < count; ++i) { int id = rand() % db.size(); res << "<tr><td><img src=\"/image?id=" << lexical_cast<string>(id) << ext << "\"></img></td></tr>"; } res << "</table></body></html>"; })); mux.handle("/file") .get(no_throw([&db, &cookie](served::response &res, const served::request &req) { rfc3986::Form query(req.url().query()); int id = lexical_cast<int>(query["id"]); Record rec; db.read(id, &rec); string buf = rec.field_string(0); char const *mime = magic_buffer(cookie, &buf[0], buf.size()); res.set_header("Content-Type", mime); res.set_body(buf); })); mux.handle("/image") .get(no_throw([&db, &rng](served::response &res, const served::request &req) { rfc3986::Form query(req.url().query()); PICPAC_CONFIG conf; conf.anno_color3 = 255; conf.anno_copy = true; conf.anno_thickness = 2; conf.pert_color1 = 20; conf.pert_color2 = 20; conf.pert_color3 = 20; conf.pert_angle = 20; conf.pert_min_scale = 0.8; conf.pert_max_scale = 1.2; #define PICPAC_CONFIG_UPDATE(C,P) C.P = query.get<decltype(C.P)>(#P, C.P) PICPAC_CONFIG_UPDATE_ALL(conf); #undef PICPAC_CONFIG_UPDATE ImageLoader loader(conf); ImageLoader::PerturbVector pv; int id = query.get<int>("id", rng() % db.size()); ImageLoader::Value v; loader.sample(rng, &pv); loader.load([&db, id](Record *r){db.read(id, r);}, pv, &v); ImageEncoder encoder; string buf; cv::Mat image = v.image; if (conf.annotate.size()) { image = v.annotation; } encoder.encode(image, &buf); res.set_header("Content-Type", "image/jpeg"); res.set_body(buf); })); mux.use_after(served::plugin::access_log); LOG(INFO) << "listening at " << address << ':' << port; served::net::server server(address, port, mux); LOG(INFO) << "running server with " << threads << " threads."; server.run(threads); magic_close(cookie); return 0; }