BRL-CAD
encoding.c
Go to the documentation of this file.
1 /* E N C O D I N G . C
2  * BRL-CAD
3  *
4  * Copyright (c) 2013-2014 United States Government as represented by
5  * the U.S. Army Research Laboratory.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public License
9  * version 2.1 as published by the Free Software Foundation.
10  *
11  * This library is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this file; see the file named COPYING for more
18  * information.
19  */
20 /** @file encoding.c
21  *
22  * Contains encoding conversion supports for various file formats.
23  *
24  */
25 
26 #include "icv.h"
27 #include "vmath.h"
28 #include "bu/magic.h"
29 #include "bu/log.h"
30 #include "bu/malloc.h"
31 #include "bn.h"
32 
33 /**
34  * converts unsigned char array to double array.
35  * This function returns array of double data.
36  *
37  * Used to convert data from pix, bw, ppm type images for icv_image
38  * struct.
39  *
40  * This does not free the char data.
41  *
42  * @param data pointer to the array to be converted.
43  * @param size Size of the array.
44  * @return double array.
45  *
46  */
47 double *
48 uchar2double(unsigned char *data, size_t size)
49 {
50  double *double_data, *double_p;
51  unsigned char *char_p;
52 
53  if (size == 0)
54  return NULL;
55 
56  char_p = data;
57  double_p = double_data = (double *) bu_malloc(size*sizeof(double), "uchar2data : double data");
58 
59  while (size--) {
60  *double_p = ICV_CONV_8BIT(*char_p);
61  double_p++;
62  char_p++;
63  }
64 
65  return double_data;
66 }
67 
68 
69 /**
70  * Converts double data of icv_image to unsigned char data.
71  * This function also does gamma correction using the gamma_corr
72  * parameter of the image structure.
73  *
74  * This is mainly used for saving pix, bw and ppm type images.
75  * Gamma correction prevents bad color aliasing.
76  *
77  */
78 unsigned char *
80 {
81  long int size;
82  unsigned char *uchar_data, *char_p;
83  double *double_p;
84 
85  ICV_IMAGE_VAL_PTR(bif);
86 
87  size = bif->height*bif->width*bif->channels;
88  char_p = uchar_data = (unsigned char *) bu_malloc((size_t)size, "data2uchar : unsigned char data");
89 
90  double_p = bif->data;
91 
92  if (ZERO(bif->gamma_corr)) {
93  while (size--) {
94  long longval = lrint((*double_p)*255.0);
95 
96  if (longval > 255)
97  *char_p = 255;
98  else if (longval < 0)
99  *char_p = 0;
100  else
101  *char_p = (unsigned char)longval;
102 
103  char_p++;
104  double_p++;
105  }
106 
107  } else {
108  float *rand_p;
109  double ex = 1.0/bif->gamma_corr;
110  bn_rand_init(rand_p, 0);
111 
112  while (size--) {
113  *char_p = floor(pow(*double_p, ex)*255.0 + (double) bn_rand0to1(rand_p) + 0.5);
114  char_p++;
115  double_p++;
116  }
117  }
118 
119  return uchar_data;
120 }
121 
122 
123 /*
124  * Local Variables:
125  * tab-width: 8
126  * mode: C
127  * indent-tabs-mode: t
128  * c-file-style: "stroustrup"
129  * End:
130  * ex: shiftwidth=4 tabstop=8
131  */
double * data
Definition: icv.h:89
int width
Definition: icv.h:91
double * uchar2double(unsigned char *data, size_t size)
Definition: encoding.c:48
int channels
Definition: icv.h:91
Definition: icv.h:86
int height
Definition: icv.h:91
void * bu_malloc(size_t siz, const char *str)
Definition: malloc.c:314
COMPLEX data[64]
Definition: fftest.c:34
#define ICV_CONV_8BIT(data)
Definition: icv.h:135
#define ICV_IMAGE_VAL_PTR(_i)
Definition: icv.h:128
#define ZERO(val)
Definition: units.c:38
#define bn_rand0to1(_q)
Definition: rand.h:123
#define bn_rand_init(_p, _seed)
Definition: rand.h:110
float gamma_corr
Definition: icv.h:90
unsigned char * data2uchar(const icv_image_t *bif)
Definition: encoding.c:79