Die printfs, die
[jpcrr.git] / streamtools / screenshot.cpp
blobf5b9ba28445f1ed03d068a212fa30412409932ab
1 #include "newpacket.hpp"
2 #include "png-out.hpp"
3 #include <cstdio>
4 #include <cctype>
5 #include <iostream>
6 #include <iomanip>
7 #include <cstdlib>
8 #include <cstring>
9 #include <sstream>
10 #include "timeparse.hpp"
12 unsigned image_seq = 0;
13 char namebuffer[4096];
14 char prefix[4000];
16 #define MAXTIME 0xFFFFFFFFFFFFFFFFULL
18 struct packet* old_packet;
20 uint64_t* target_stamps;
21 unsigned target_count;
23 int in_range(uint64_t low, uint64_t high)
25 for(unsigned i = 0; i < target_count; i++)
26 if(target_stamps[i] >= low && target_stamps[i] < high)
27 return 1;
28 return 0;
31 void handle_packet(struct packet* p)
33 int do_it = 0;
35 if(p == NULL) {
36 if(old_packet && in_range(old_packet->rp_timestamp, MAXTIME))
37 do_it = 1;
38 } else {
39 if(p->rp_major != 0)
40 return;
41 if(old_packet && in_range(old_packet->rp_timestamp, p->rp_timestamp))
42 do_it = 1;
45 if(do_it && old_packet) {
46 try {
47 image_frame f(*old_packet);
48 std::stringstream name;
49 name << prefix << std::setfill('0') << std::setw(5) << image_seq++ << ".png";
50 std::cerr << "Saving screenshot '" << name.str() << "'." << std::endl;
51 f.save_png(name.str());
52 } catch(std::exception& e) {
53 std::cerr << "Can't save screenshot: " << e.what() << std::endl;
56 if(old_packet)
57 delete old_packet;
58 old_packet = p;
61 void packet_loop(const char* name)
63 struct packet* p;
64 read_channel rc(name);
65 while((p = rc.read()))
66 handle_packet(p);
67 handle_packet(NULL);
70 int main(int argc, char** argv)
72 const char* _prefix = "screenshot-";
73 char* _input = NULL;
75 for(int i = 1; i < argc; i++) {
76 if(!strncmp(argv[i], "--input=", 8))
77 _input = argv[i] + 8;
78 else if(!strncmp(argv[i], "--prefix=", 9))
79 _prefix= argv[i] + 9;
80 else if(!strncmp(argv[i], "-", 1)) {
81 std::cerr << "Unknown option '" << argv[i] << "'." << std::endl;
82 exit(1);
83 } else {
84 uint64_t stamp = parse_timespec(argv[i]);
85 target_stamps = (uint64_t*)realloc(target_stamps, sizeof(uint64_t) * (target_count + 1));
86 target_stamps[target_count++] = stamp;
90 if(!_input || !target_count) {
91 std::cerr << "syntax: " << argv[0] << " --input=<file> [--prefix=<prefix>] <timespec>..."
92 << std::endl;
93 exit(1);
95 strcpy(prefix, _prefix);
96 packet_loop(_input);
97 return 0;