summaryrefslogtreecommitdiff
path: root/libhistogram/color_sampling_tool.cpp
blob: ca4da65abb6559a9127f1316c0011c176b594b98 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <chrono>
#include <iostream>
#include <fstream>
#include <signal.h>
#include <thread>
#include <unistd.h>

#include "histogram_collector.h"

void sigint_handler(int) {
}

void show_usage(char* progname) {
    std::cout << "Usage: ./" + std::string(progname) + " {options} \n"
              << "Sample the V (as in HSV) channel of the pixels that were displayed onscreen.\n\n"
              << "\tOptions:\n"
              << "\t-h      display this help message\n"
              << "\t-o      write output to specified filename\n"
              << "\t-t NUM  Collect results over NUM seconds, and then exit\n"
              << "\t-m NUM  Only store the last NUM frames of statistics\n";
}

int main(int argc, char** argv) {
    struct sigaction sigHandler;
    sigHandler.sa_handler = sigint_handler;
    sigemptyset(&sigHandler.sa_mask);
    sigHandler.sa_flags = 0;
    sigaction(SIGINT, &sigHandler, NULL);

    int c;
    char * output_filename = NULL;
    int timeout = -1;
    while ((c = getopt(argc, argv, "o:t:h")) != -1) {
        switch (c) {
            case 'o': output_filename = optarg; break;
            case 't': timeout = strtol(optarg, NULL, 10); break;
            default:
            case 'h': show_usage(argv[0]); return EXIT_SUCCESS;
        }
    }

    histogram::HistogramCollector histogram;
    histogram.start();

    bool cancelled_during_wait = false;
    if (timeout > 0) {
        std::cout << "Sampling for " << timeout << " seconds.\n";
        struct timespec request, remaining;
        request.tv_sec = timeout;
        request.tv_nsec = 0;
        cancelled_during_wait = (nanosleep(&request, &remaining) != 0);
    } else {
        std::cout << "Sampling until Ctrl-C is pressed\n";
        sigsuspend(&sigHandler.sa_mask);
    }

    std::cout << "Sampling results:\n";

    histogram.stop();

    if (cancelled_during_wait) {
        std::cout << "Timed histogram collection cancelled via signal\n";
        return EXIT_SUCCESS;
    }

    if (output_filename) {
        std::cout << "\nWriting statistics to: " << output_filename << '\n';
        std::ofstream output_file;
        output_file.open(output_filename);
        if (!output_file.is_open()) {
            std::cerr << "Error, could not open given file: " << output_filename << "\n";
            return EXIT_FAILURE;
        }
        output_file << histogram.Dump();
        output_file.close();
    } else {
        std::cout << histogram.Dump() << '\n';
    }

    return EXIT_SUCCESS;
}