summaryrefslogtreecommitdiff
path: root/v4l2-apps/libv4l/libv4lconvert/processing/rgbprocessing.c
blob: 4e0fc3f4aec59b729df6993fc319ea0b19ef5e79 (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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
#             (C) 2008-2009 Elmar Kleijn <elmar_kleijn@hotmail.com>
#             (C) 2008-2009 Sjoerd Piepenbrink <need4weed@gmail.com>
#             (C) 2008-2009 Hans de Goede <hdegoede@redhat.com>

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include "libv4lprocessing-priv.h"

void rgb_normalize_analyse(unsigned char *buf, int width, int height,
  struct v4lprocessing_data *data)
{
  int value, max = 0, min = 255;
  unsigned char *buf_end = buf + width * height * 3;

  while (buf < buf_end) {
    value = *buf++;
    if (max < value)
      max = value;
    if (min > value)
      min = value;
  }

  data->comp1 = ((data->norm_high_bound - data->norm_low_bound) << 16) / (max - min);
  data->offset1 = min;
  data->offset2 = data->norm_low_bound;
}

void rgb_whitebalance_analyse(unsigned char *buf, int width, int height,
  struct v4lprocessing_data *data)
{
  int value, x = 0, y = 0, z = 0;
  float x_avg, y_avg, z_avg, avg_avg;
  unsigned char *buf_end = buf + width * height * 3;

  while (buf < buf_end) {
    x += *buf++;
    y += *buf++;
    z += *buf++;
  }

  x_avg = x;
  y_avg = y;
  z_avg = z;
  avg_avg = (x_avg + y_avg + z_avg) / 3;

  data->comp1 = (avg_avg / x_avg) * 65536;
  data->comp2 = (avg_avg / y_avg) * 65536;
  data->comp3 = (avg_avg / z_avg) * 65536;
}

void rgb_normalize_whitebalance_analyse(unsigned char *buf,
  int width, int height, struct v4lprocessing_data *data)
{
  int value, max = 0, min = 255;
  int n_fac, wb_max, x = 0, y = 0, z = 0;
  float x_avg, y_avg, z_avg, avg_avg;
  unsigned char *buf_end = buf + width * height * 3;

  while (buf < buf_end) {
    x += *buf;
    value = *buf++;
    if (max < value)
      max = value;
    if (min > value)
      min = value;
    y += *buf;
    value = *buf++;
    if (max < value)
      max = value;
    if (min > value)
      min = value;
    z += *buf;
    value = *buf++;
    if (max < value)
      max = value;
    if (min > value)
      min = value;
  }

  x_avg = x;
  y_avg = y;
  z_avg = z;
  avg_avg = (x_avg + y_avg + z_avg) / 3;

  n_fac = ((data->norm_high_bound - data->norm_low_bound) << 16) / (max - min);

  data->comp1 = (avg_avg / x_avg) * n_fac;
  data->comp2 = (avg_avg / y_avg) * n_fac;
  data->comp3 = (avg_avg / z_avg) * n_fac;

  data->offset1 = min;
  data->offset2 = data->norm_low_bound;
}

#define CLIP(color) (unsigned char)(((color)>0xff)?0xff:(((color)<0)?0:(color)))
#define TOP(color) (unsigned char)(((color)>0xff)?0xff:(color))

void rgb_normalize(unsigned char *buf, int width, int height,
  struct v4lprocessing_data *data)
{
  int value;
  unsigned char *buf_end = buf + width * height * 3;

  while (buf < buf_end) {
    value = ((data->comp1 * (*buf - data->offset1)) >> 16) +
      data->offset2;
    *buf++ = CLIP(value);
  }
}

void rgb_whitebalance(unsigned char *buf, int width, int height,
  struct v4lprocessing_data *data)
{
  int value;
  unsigned char *buf_end = buf + width * height * 3;

  while (buf < buf_end) {
    value = (*buf * data->comp1) >> 16;
    *buf++ = TOP(value);
    value = (*buf * data->comp2) >> 16;
    *buf++ = TOP(value);
    value = (*buf * data->comp3) >> 16;
    *buf++ = TOP(value);
  }
}

void rgb_normalize_whitebalance(unsigned char *buf, int width, int height,
  struct v4lprocessing_data *data)
{
  int i, value;
  int limit = width * height * 3;
  unsigned char *buf_end = buf + width * height * 3;

  while (buf < buf_end) {
    value = ((data->comp1 * (*buf - data->offset1)) >> 16) + data->offset2;
    *buf++ = CLIP(value);
    value = ((data->comp2 * (*buf - data->offset1)) >> 16) + data->offset2;
    *buf++ = CLIP(value);
    value = ((data->comp3 * (*buf - data->offset1)) >> 16) + data->offset2;
    *buf++ = CLIP(value);
  }
}