aboutsummaryrefslogtreecommitdiffstats
path: root/gamma_corr.c
diff options
context:
space:
mode:
authorPatrick Roth <roth@stettbacher.ch>2019-10-04 11:51:48 +0200
committerPatrick Roth <roth@stettbacher.ch>2019-10-04 11:51:48 +0200
commita0f501fa5650d0b6062cc8f26b34bce11137643d (patch)
tree8e31c5ac3409d4ce48887d88d4530b88a02c2660 /gamma_corr.c
downloado3000-color-pipe-a0f501fa5650d0b6062cc8f26b34bce11137643d.tar.gz
o3000-color-pipe-a0f501fa5650d0b6062cc8f26b34bce11137643d.zip
initial commit
import from github
Diffstat (limited to 'gamma_corr.c')
-rw-r--r--gamma_corr.c113
1 files changed, 113 insertions, 0 deletions
diff --git a/gamma_corr.c b/gamma_corr.c
new file mode 100644
index 0000000..c0e5cd4
--- /dev/null
+++ b/gamma_corr.c
@@ -0,0 +1,113 @@
+/**
+* @file gamma_corr.c
+* @brief gamma correction algorithm
+* @author Patrick Roth - roth@stettbacher.ch
+* @version 1.0
+* @date 2015-09-08
+* @copyright 2012-2016 Stettbacher Signal Processing AG
+*
+* @remarks
+*
+* <PRE>
+* This library 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 library 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 library; if not, write to the Free Software
+* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+* </PRE>
+*
+*/
+
+#include <stdio.h>
+#include <string.h>
+#include <math.h>
+
+#include "color_pipe_private.h"
+
+
+/**
+ * Apply gamma correction to given image type: RGB or monochrome with 8 bit per color channel
+ *
+ * @param img_rgb On return: gamma corrected image
+ * @param img_in image to be gamma corrected
+ * @param height image height in number of pixels
+ * @param width image width in number of pixels
+ * @param max_val maximum pixel value
+ * @param gamma gamma coefficient
+ * @param is_color 1 if it's a color image, 0 if it's a monochrome image
+ * @return 0 on success otherwise -1
+ */
+static void gamma_corr8(uint8_t *img_rgb, const uint8_t *img_in, const int height, const int width, const int *gamma_table, const int is_color)
+#include "alg_gamma.h"
+
+
+/**
+ * Apply gamma correction to given image type: RGB or monochrome with 16 bit per color channel
+ *
+ * @param img_rgb On return: gamma corrected image
+ * @param img_in image to be gamma corrected
+ * @param height image height in number of pixels
+ * @param width image width in number of pixels
+ * @param max_val maximum pixel value
+ * @param gamma gamma coefficient
+ * @param is_color 1 if it's a color image, 0 if it's a monochrome image
+ * @return 0 on success otherwise -1
+ */
+static void gamma_corr16(uint16_t *img_rgb, const uint16_t *img_in, const int height, const int width, const int *gamma_table, const int is_color)
+#include "alg_gamma.h"
+
+
+/**
+ * Apply gamma correction to given image type.
+ *
+ * @param gamma_data required data for gamma correction
+ * @return 0 on success otherwise -1
+ */
+int gamma_corr(struct gamma_data_t *gamma_data) {
+
+ void *img_gamma, *img_in;
+ int *gamma_table;
+ int i, is_color, bit_channel, width, height, max_pix, gamma_table_bitdepth;
+ float gamma, gamma_table_init;
+
+
+ // put variables on stack
+ is_color = gamma_data->is_color;
+ img_gamma = gamma_data->img_gamma;
+ img_in = gamma_data->img_in;
+ bit_channel = gamma_data->bit_channel;
+ width = gamma_data->width;
+ height = gamma_data->height;
+ gamma = gamma_data->gamma;
+ max_pix = (1<<bit_channel)-1;
+ gamma_table_bitdepth = gamma_data->gamma_table_bitdepth;
+ gamma_table_init = gamma_data->gamma_table_init;
+ gamma_table = gamma_data->gamma_table;
+
+ /*
+ * Do check whether lookup table is initialzed with correct gamma value
+ */
+ if(gamma_table_bitdepth != bit_channel || gamma != gamma_table_init) {
+ for(i = 0; i <= max_pix; i++) {
+ gamma_table[i] = roundf(max_pix*pow(i/((float)max_pix), 1.0/gamma));
+ }
+ gamma_data->gamma_table_bitdepth = bit_channel;
+ gamma_data->gamma_table_init = gamma;
+ }
+
+ if(bit_channel <= 8) {
+ gamma_corr8(img_gamma, img_in, height, width, gamma_table, is_color);
+ }
+ else if(bit_channel <= 16) {
+ gamma_corr16(img_gamma, img_in, height, width, gamma_table, is_color);
+ }
+ return 0;
+} \ No newline at end of file