BRL-CAD
pix.c
Go to the documentation of this file.
1 /* 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 pix.c
21  *
22  * Contains routines related to pix format.
23  *
24  */
25 #include "icv.h"
26 #include "bu/malloc.h"
27 #include "bu/log.h"
28 
29 /* defined in encoding.c */
30 extern double *uchar2double(unsigned char *data, long int size);
31 extern unsigned char *data2uchar(const icv_image_t *bif);
32 
33 int
34 pix_write(icv_image_t *bif, const char *filename)
35 {
36  unsigned char *data;
37  FILE* fp;
38  size_t ret, size;
39 
40  if (bif->color_space == ICV_COLOR_SPACE_GRAY) {
41  icv_gray2rgb(bif);
42  } else if (bif->color_space != ICV_COLOR_SPACE_RGB) {
43  bu_log("pix_write : Color Space conflict");
44  return -1;
45  }
46 
47  if (filename==NULL) {
48  fp = stdout;
49  } else if ((fp = fopen(filename, "wb")) == NULL) {
50  bu_log("pix_write: Cannot open file for saving\n");
51  return -1;
52  }
53 
54  data = data2uchar(bif);
55  size = (size_t) bif->width*bif->height*3;
56  ret = fwrite(data, 1, size, fp);
57  fclose(fp);
58  if (ret != size) {
59  bu_log("pix_write : Short Write");
60  return -1;
61  }
62  return 0;
63 }
64 
65 
67 pix_read(const char* filename, int width, int height)
68 {
69  FILE* fp;
70  unsigned char *data = 0;
71  icv_image_t *bif;
72  size_t size,ret;
73  size_t buffsize=1024*3;
74 
75  if (filename == NULL) {
76  fp = stdin;
77  } else if ((fp = fopen(filename, "rb")) == NULL) {
78  bu_log("pix_read: Cannot open file for reading\n");
79  return NULL;
80  }
81  BU_ALLOC(bif, struct icv_image);
82  ICV_IMAGE_INIT(bif);
83  /* buffer pixel wise */
84  if (width == 0 || height == 0) {
85  int status = 0;
86  size = 0;
87  data = (unsigned char *)bu_malloc(buffsize, "pix_read : unsigned char data");
88 
89  /* FIXME: this is a simple but VERY slow way to read data.
90  * Better to read in big chunks, but then one has to handle
91  * partial-reads better. Below seems to ignore a read error.
92  */
93  while ((status = fread(&data[size], 1, 3, fp))==3) {
94  size+=3;
95  if (size==buffsize) {
96  buffsize+=1024*3;
97  data = (unsigned char *)bu_realloc(data, buffsize, "pix_read : increase size to accommodate data");
98  }
99  }
100  if (size<buffsize) {
101  data = (unsigned char *)bu_realloc(data, size, "pix_read : decrease size in overbuffered");
102  }
103  bif->height = 1;
104  bif->width = (int) size/3;
105  } else { /* buffer frame wise */
106  size = (size_t) height*width*3;
107  data = (unsigned char *)bu_malloc(size, "pix_read : unsigned char data");
108  ret = fread(data, 1, size, fp);
109  if (ret!=0 && ferror(fp)) {
110  bu_log("pix_read: Error Occurred while Reading\n");
111  bu_free(data, "icv_image data");
112  bu_free(bif, "icv_structure");
113  fclose(fp);
114  return NULL;
115  }
116  bif->height = height;
117  bif->width = width;
118  }
119  if (size)
120  bif->data = uchar2double(data, size);
121  else {
122  /* zero sized image */
123  bu_free(bif, "icv container");
124  bu_free(data, "unsigned char data");
125  fclose(fp);
126  return NULL;
127  }
128  bif->data = uchar2double(data, size);
129  bu_free(data, "pix_read : unsigned char data");
130  bif->magic = ICV_IMAGE_MAGIC;
131  bif->channels = 3;
133  fclose(fp);
134  return bif;
135 }
136 
137 /*
138  * Local Variables:
139  * tab-width: 8
140  * mode: C
141  * indent-tabs-mode: t
142  * c-file-style: "stroustrup"
143  * End:
144  * ex: shiftwidth=4 tabstop=8
145  */
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
#define ICV_IMAGE_INIT(_i)
Definition: icv.h:107
int width
Definition: icv.h:91
uint32_t magic
Definition: icv.h:87
int channels
Definition: icv.h:91
Definition: icv.h:86
int height
Definition: icv.h:91
ustring width
void * bu_malloc(size_t siz, const char *str)
Definition: malloc.c:314
COMPLEX data[64]
Definition: fftest.c:34
#define BU_ALLOC(_ptr, _type)
Definition: malloc.h:223
int icv_gray2rgb(icv_image_t *img)
Definition: color_space.c:37
double * uchar2double(unsigned char *data, long int size)
void * bu_realloc(void *ptr, size_t siz, const char *str)
#define ICV_IMAGE_MAGIC
Definition: magic.h:216
ICV_COLOR_SPACE color_space
Definition: icv.h:88
void bu_free(void *ptr, const char *str)
Definition: malloc.c:328
icv_image_t * pix_read(const char *filename, int width, int height)
Definition: pix.c:67
int pix_write(icv_image_t *bif, const char *filename)
Definition: pix.c:34
unsigned char * data2uchar(const icv_image_t *bif)
Definition: encoding.c:79