Source code upload
[framework/connectivity/libgphoto2.git] / libgphoto2 / gamma.c
1 /** \file gamma.c
2  *
3  * \author Copyright 2001 Lutz Müller <lutz@users.sf.net>
4  *
5  * \par License
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * \par
12  * This library is distributed in the hope that it will be useful, 
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of 
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details. 
16  *
17  * \par
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23
24 #include "config.h"
25 #include "gamma.h"
26
27 #include <math.h>
28
29 #include <gphoto2/gphoto2-result.h>
30
31 static int
32 gp_gamma_correct_triple (unsigned char *table_red,
33                          unsigned char *table_green,
34                          unsigned char *table_blue,
35                          unsigned char *data, unsigned int size)
36 {
37         unsigned int x;
38
39         for (x = 0; x < (size * 3); x += 3) {
40                 data[x + 0] = table_red  [data[x + 0]];
41                 data[x + 1] = table_green[data[x + 1]];
42                 data[x + 2] = table_blue [data[x + 2]];
43         }
44
45         return (GP_OK);
46 }
47
48 /**
49  * \brief Gamma correction
50  *
51  * Corrects size pixels within the table with a given Gamma
52  * correction table.
53  * 
54  * \param table the gamma correction table as generated by gp_gamma_fill_table()
55  * \param data the data do process, both input and output
56  * \param size in number of pixels (RGB byte pairs)
57  *
58  * \returns a gphoto error code
59  */
60 int
61 gp_gamma_correct_single (unsigned char *table, unsigned char *data,
62                          unsigned int size)
63 {
64         return (gp_gamma_correct_triple (table, table, table, data, size));
65 }
66
67 /**
68  * \brief Initialize a Gamma conversion table
69  * 
70  * Initializes the gamma conversion table for later use by gp_gamma_correct_single().
71  * Requires a 256 byte array as table.
72  * \param table a 256 byte array of unsigned char
73  * \param g gamma correction value
74  *
75  * \returns a gphoto error code
76  */
77 int
78 gp_gamma_fill_table (unsigned char *table, double g)
79 {
80         unsigned int x;
81
82         for (x = 0; x < 256; x++)
83                 table[x] = 255 * pow ((double) x/255., g);
84
85         return (GP_OK);
86 }