22 #include <fvutils/writers/png.h> 23 #include <utils/system/argparser.h> 24 #include <utils/misc/string_conversions.h> 26 #include <mongo/client/dbclient.h> 27 #include <mongo/client/gridfs.h> 30 using namespace mongo;
33 #ifdef HAVE_MONGODB_VERSION_H 35 # define QUERY MONGO_QUERY 40 print_usage(
const char *progname)
42 printf(
"Usage: %s [-h] [-o dir] [-f] [-d database] -c collection items...\n" 43 " -h Show this help message\n" 44 " -o dir Output directory where to create PNG files\n" 45 " -f Use original filenames form database\n" 46 " -d database Database to query for images\n" 47 " -c collection Collection to query for images\n" 49 "Items are either timestamps (ms precision) or timestamp ranges in\n" 52 "Example: %s -d fflog -c openni_image_rgb 0..1355421345807\n" 53 "\n", progname, progname);
57 main(
int argc,
char **argv)
60 if (argp.has_arg(
"h")) {
65 const std::vector<const char *> &items = argp.items();
67 std::string output_dir =
"tmp/";
68 std::string database =
"fflog";
69 std::string collection;
70 std::string query_coll;
71 bool filename_indexed = ! argp.has_arg(
"f");
73 std::vector<std::pair<long long, long long> > times;
75 if (argp.has_arg(
"o")) {
76 output_dir = argp.arg(
"o");
77 if (output_dir[output_dir.length() - 1] !=
'/') {
81 if (argp.has_arg(
"d")) {
82 database = argp.arg(
"d");
84 if (argp.has_arg(
"c")) {
85 collection = argp.arg(
"c");
88 printf(
"No collection given\n");
92 query_coll = database +
"." + collection;
95 times.push_back(std::make_pair(0L, std::numeric_limits<long long>::max()));
97 for (
unsigned int i = 0; i < items.size(); ++i) {
98 std::string item = items[i];
99 std::string::size_type dotpos = item.find(
"..");
100 if (dotpos == std::string::npos) {
102 long int ts = argp.parse_item_int(i);
103 times.push_back(std::make_pair(ts, ts));
106 std::string first_ts, second_ts;
107 first_ts = item.substr(0, dotpos);
108 second_ts = item.substr(dotpos + 2);
109 times.push_back(std::make_pair(StringConversions::to_long(first_ts),
110 StringConversions::to_long(second_ts)));
115 unsigned int image_n = 0;
117 DBClientConnection *mongodb_client =
118 new DBClientConnection(
true);
120 mongodb_client->connect(
"localhost", errmsg);
122 GridFS *gridfs =
new GridFS(*mongodb_client,
"fflog");
125 for (
unsigned int i = 0; i < times.size(); ++i) {
128 if (times[i].first == times[i].second) {
129 printf(
"Querying for timestamp %lli\n", times[i].first);
130 q = QUERY(
"timestamp" << times[i].first).sort(
"timestamp", 1);
132 printf(
"Querying for range %lli..%lli\n", times[i].first, times[i].second);
133 q = QUERY(
"timestamp" 134 << mongo::GTE << times[i].first
135 << mongo::LTE << times[i].second)
136 .sort(
"timestamp", 1);
139 #if __cplusplus >= 201103L 140 std::unique_ptr<mongo::DBClientCursor> cursor =
141 mongodb_client->query(query_coll, q);
143 std::auto_ptr<mongo::DBClientCursor> cursor =
144 mongodb_client->query(query_coll, q);
147 while (cursor->more()) {
148 BSONObj doc = cursor->next();
150 BSONObj imgdoc = doc.getObjectField(
"image");
151 if (imgdoc[
"colorspace"].String() ==
"RGB") {
152 std::string filename = imgdoc.getFieldDotted(
"data.filename").String();
153 long filesize = imgdoc.getFieldDotted(
"data.length").numberLong();
154 std::string image_id = imgdoc[
"image_id"].String();
156 std::string out_filename;
158 if (filename_indexed) {
159 if (asprintf(&fntmp,
"%s%s-%08d.png", output_dir.c_str(),
160 image_id.c_str(), image_n++) != -1)
162 out_filename = fntmp;
166 if (asprintf(&fntmp,
"%s%s.png", output_dir.c_str(),
167 filename.c_str()) != -1)
169 out_filename = fntmp;
175 printf(
"Restoring RGB image %s (%s)\n", filename.c_str(), out_filename.c_str());
177 GridFile file = gridfs->findFile(filename);
178 if (! file.exists()) {
179 printf(
"File %s does not exist\n", filename.c_str());
183 unsigned int width = imgdoc[
"width"].Int();
184 unsigned int height = imgdoc[
"height"].Int();
186 if (colorspace_buffer_size(RGB, width, height) != (
size_t)filesize) {
187 printf(
"Buffer size mismatch (DB %li vs. exp. %zu)\n",
188 filesize, colorspace_buffer_size(RGB, width, height));
192 unsigned char *buffer = malloc_buffer(RGB, width, height);
194 unsigned char *tmp = buffer;
195 for (
int c = 0; c < file.getNumChunks(); ++c) {
196 mongo::GridFSChunk chunk = file.getChunk(c);
198 const char *chunk_data = chunk.data(len);
199 memcpy(tmp, chunk_data, len);
203 PNGWriter writer(out_filename.c_str(), width, height);
213 delete mongodb_client;
Fawkes library namespace.
Parse command line arguments.
virtual void set_buffer(colorspace_t cspace, unsigned char *buffer)
Set image buffer.