File: histogram_utils.cpp

package info (click to toggle)
satdump 1.2.2%2Bgb79af48-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 81,648 kB
  • sloc: cpp: 276,768; ansic: 164,598; lisp: 1,219; sh: 283; xml: 106; makefile: 7
file content (93 lines) | stat: -rw-r--r-- 2,823 bytes parent folder | download | duplicates (2)
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
#include "histogram_utils.h"
#include <limits>

#include "logger.h"

namespace image
{
    namespace histogram
    {
        std::vector<int> get_histogram(std::vector<int> values, int len)
        {
            std::vector<int> final_hist(len, 0);

            // Compute histogram
            for (int px = 0; px < (int)values.size(); px++)
                final_hist[values[px]] += 1;

            return final_hist;
        }

        std::vector<int> equalize_histogram(std::vector<int> hist)
        {
            std::vector<int> equ_hist(hist.size());
            equ_hist[0] = hist[0];
            for (int i = 1; i < (int)hist.size(); i++)
                equ_hist[i] = hist[i] + equ_hist[i - 1];
            return equ_hist;
        }

        int try_find_val(std::vector<int> &array, int val)
        {
            int pos = -1;
            for (int i = 0; i < (int)array.size(); i++)
            {
                if (array[i] == val)
                {
                    pos = i;
                    return pos;
                }
            }
            return pos;
        }

        /*int try_find_val_around(std::vector<int> &array, int cent_pos, int val)
        {
            int array_size = array.size();
            for (int i = 0; i < array_size; i++)
            {
                int pos_min = cent_pos - i;
                int pos_max = cent_pos + i;

                // if (pos_min >= 0 && pos_min < array_size)
                //     if (array[pos_min] == val)
                //         return pos_min;

                if (pos_max >= 0 && pos_max < array_size)
                    if (array[pos_max] == val)
                        return pos_max;
            }
            return try_find_val(array, val);
        }*/

        int find_target_value_hist(std::vector<int> &array, int cent_pos, int val)
        {
            if (val == std::numeric_limits<int>::max())
                return -1;
            if (val < 0)
                return -1;

            int pos = try_find_val(array, val);
            if (pos == -1)
            {
                pos = find_target_value_hist(array, cent_pos, val + 1);
                if (pos == -1)
                    pos = find_target_value_hist(array, cent_pos, val - 1);
            }
            return pos;
        }

        std::vector<int> make_hist_match_table(std::vector<int> input_hist, std::vector<int> target_hist, int /*maxdiff*/)
        {
            std::vector<int> table(target_hist.size());
            for (int i = 0; i < (int)target_hist.size(); i++)
            {
                table[i] = find_target_value_hist(target_hist, i, input_hist[i]);
                if (i != 0 && table[i] == 0)
                    table[i] = i;
            }
            table[0] = 0; // To be sure
            return table;
        }
    }
}