BRL-CAD
dpix.c
Go to the documentation of this file.
1 /* D P I X . 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 dpix.c
21  *
22  * this contains read/write routines for dpix format.
23  *
24  */
25 
26 #include "common.h"
27 
28 #include <sys/stat.h> /* for file mode info in WRMODE */
29 
30 #include "bio.h"
31 #include "bu/log.h"
32 #include "icv.h"
33 #include "vmath.h"
34 
35 #define WRMODE S_IRUSR|S_IRGRP|S_IROTH
36 
37 /*
38  * This function normalizes the data array of the input image.
39  * This performs the normalization when the input image has data
40  * entries less than 0.0 or greater than 1.0 .
41  */
44 {
45  double *data;
46  double max, min;
47  double m, b;
48  size_t size;
49  unsigned long int i;
50 
51  if (bif == NULL) {
52  bu_log("icv_normalize : trying to normalize a NULL bif\n");
53  return bif;
54  }
55 
56  data = bif->data;
57 
58  /* Number of data elements. */
59  size = bif->height*bif->width*bif->channels;
60 
61  min = INFINITY;
62  max = -INFINITY;
63 
64  for (i = 0; i<size; i++) {
65  V_MIN(min, *data);
66  V_MAX(max, *data);
67  data++;
68  }
69  /* strict Condition for avoiding normalization */
70  if (max <= 1.0 || min >= 0.0)
71  return bif;
72 
73  data = bif->data;
74  m = 1/(max-min);
75  b = -min/(max-min);
76 
77  for (i =0; i<size; i++) {
78  *data = m*(*data) + b;
79  data++;
80  }
81 
82  return bif;
83 }
84 
85 
87 dpix_read(const char *filename, int width, int height)
88 {
89  icv_image_t *bif;
90  int fd;
91  size_t size;
92  ssize_t ret;
93 
94  if (width == 0 || height == 0) {
95  bu_log("dpix_read : Using default size.\n");
96  height = 512;
97  width = 512;
98  }
99 
100  if (filename == NULL)
101  fd = fileno(stdin);
102  else if ((fd = open(filename, O_RDONLY|O_BINARY, WRMODE)) <0) {
103  bu_log("dpix_read : Cannot open file %s for reading\n, ", filename);
104  return NULL;
105  }
106 
107  bif = icv_create(width, height, ICV_COLOR_SPACE_RGB);
108 
109  /* Size in Bytes for reading. */
110  size = width*height*3*sizeof(bif->data[0]);
111 
112  /* read dpix data */
113  ret = read(fd, bif->data, size);
114 
115  if (ret != (ssize_t)size) {
116  bu_log("dpix_read : Error while reading\n");
117  icv_destroy(bif);
118  return NULL;
119  }
120 
121  icv_normalize(bif);
122 
123  return bif;
124 }
125 
126 
127 int
128 dpix_write(icv_image_t *bif, const char *filename)
129 {
130  int fd;
131  size_t size;
132  ssize_t ret;
133 
134  if (bif->color_space == ICV_COLOR_SPACE_GRAY) {
135  icv_gray2rgb(bif);
136  } else if (bif->color_space != ICV_COLOR_SPACE_RGB) {
137  bu_log("pix_write : Color Space conflict");
138  return -1;
139  }
140 
141  if (filename==NULL)
142  fd = fileno(stdout);
143  else if ((fd = open(filename, O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, WRMODE)) < 0) {
144  bu_log("dpix_write: Cannot open file for saving\n");
145  return -1;
146  }
147 
148  /* size in bytes */
149  size = bif->width*bif->height*3*sizeof(bif->data[0]);
150 
151  /* write dpix data */
152  ret = write(fd, bif->data, size);
153  close(fd);
154 
155  if (ret != (ssize_t)size) {
156  bu_log("dpix_write : Short Write");
157  return -1;
158  }
159  return 0;
160 }
161 
162 
163 /*
164  * Local Variables:
165  * tab-width: 8
166  * mode: C
167  * indent-tabs-mode: t
168  * c-file-style: "stroustrup"
169  * End:
170  * ex: shiftwidth=4 tabstop=8
171  */
ptrdiff_t ssize_t
Definition: common.h:119
double * data
Definition: icv.h:89
char filename[MAXLENGTH]
Definition: human.c:105
void bu_log(const char *,...) _BU_ATTR_PRINTF12
Definition: log.c:176
int width
Definition: icv.h:91
int icv_destroy(icv_image_t *bif)
Definition: fileformat.c:321
Header file for the BRL-CAD common definitions.
int channels
Definition: icv.h:91
Definition: icv.h:86
int height
Definition: icv.h:91
ustring width
#define HIDDEN
Definition: common.h:86
COMPLEX data[64]
Definition: fftest.c:34
int icv_gray2rgb(icv_image_t *img)
Definition: color_space.c:37
ICV_COLOR_SPACE color_space
Definition: icv.h:88
HIDDEN icv_image_t * icv_normalize(icv_image_t *bif)
Definition: dpix.c:43
icv_image_t * icv_create(int width, int height, ICV_COLOR_SPACE color_space)
Definition: fileformat.c:276
#define WRMODE
Definition: dpix.c:35
int dpix_write(icv_image_t *bif, const char *filename)
Definition: dpix.c:128
icv_image_t * dpix_read(const char *filename, int width, int height)
Definition: dpix.c:87