Coin Logo Coin3D is Free Software,
published under the BSD 3-clause license.
https://coin3d.github.io
https://www.kongsberg.com/en/kogt/
simage12.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) Kongsberg Oil & Gas Technologies
3  *
4  * Permission to use, copy, modify, and distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16 
17 #include <assert.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 
21 #ifdef HAVE_CONFIG_H
22 #include <config.h>
23 #endif /* HAVE_CONFIG_H */
24 
25 #include <simage.h>
26 #include <simage_private.h>
27 #include <string.h>
28 
29 s_image *
30 s_image_create(int w, int h, int components,
31  unsigned char * prealloc /* | NULL */ )
32 {
33  s_image * image = (s_image*) malloc(sizeof(s_image));
34  image->width = w;
35  image->height = h;
36  image->components = components;
37  image->order = SIMAGE_ORDER_RGB;
38  image->didalloc = 0;
39  image->data = prealloc;
40  if (image->data == NULL) {
41  image->didalloc = 1;
42  image->data = (unsigned char *) malloc((size_t)w*h*components);
43  }
44 
45  /* needed for simage 1.6 */
46  image->opendata = NULL;
47  image->oktoreadall = 1;
48  image->openfilename = NULL;
49  memset(&image->openfuncs, 0, sizeof(struct simage_open_funcs));
50 
51  /* return image struct */
52  return (s_image*) image;
53 }
54 
55 void
57 {
58  if (image) {
59  if (image->didalloc) free((void*)image->data);
60 
61  if (image->opendata) {
62  image->openfuncs.close_func(image->opendata);
63  }
64  if (image->openfilename) {
65  free((void*) image->openfilename);
66  }
67  free((void*)image);
68  }
69 }
70 
71 int
73 {
74  if (image) return image->width;
75  return 0;
76 }
77 
78 int
80 {
81  if (image) return image->height;
82  return 0;
83 }
84 
85 int
87 {
88  if (image) return image->components;
89  return 0;
90 }
91 
92 
93 int
95 {
96  int oldorder = image->order;
97  image->order = order;
98  return oldorder;
99 }
100 
101 int
103 {
104  return image->order;
105 }
106 
107 unsigned char *
109 {
110  if (image) {
111  if (image->opendata && image->data == NULL) {
112  int i;
113  image->data = (unsigned char *) malloc((size_t)image->width*image->height*image->components);
114  image->didalloc = 1;
115  for (i = 0; i < image->height; i++) {
116  (void) s_image_read_line(image, i,
117  image->data+image->width*image->components);
118  }
119  }
120  return image->data;
121  }
122  return NULL;
123 }
124 
125 void
126 s_image_set(s_image * image, int w, int h, int components,
127  unsigned char * data, int copydata)
128 {
129  if (image->width == w && image->height == h && image->components == components) {
130  if (copydata) {
131  if (!image->didalloc) {
132  /* we shouldn't overwrite preallocated data */
133  image->data = (unsigned char*) malloc((size_t)w*h*components);
134  image->didalloc = 1;
135  }
136  memcpy(image->data, data, (size_t)w*h*components);
137  }
138  else {
139  if (image->didalloc) free((void*) image->data);
140  image->data = data;
141  image->didalloc = 0;
142  }
143  }
144  else {
145  if (image->didalloc) free((void*) image->data);
146  image->width = w;
147  image->height = h;
148  image->components = components;
149  if (copydata) {
150  image->data = (unsigned char *) malloc((size_t)w*h*components);
151  image->didalloc = 1;
152  memcpy(image->data, data, (size_t)w*h*components);
153  }
154  else {
155  image->data = data;
156  image->didalloc = 0;
157  }
158  }
159  image->order = SIMAGE_ORDER_RGB;
160 }
161 
162 s_image *
163 s_image_load(const char * filename, s_image * prealloc /* | NULL */)
164 {
165  unsigned char * data;
166  int w,h,nc;
167 
168  data = simage_read_image(filename, &w, &h, &nc);
169  if (data == NULL) return NULL;
170  if (prealloc == NULL ||
171  prealloc->width != w ||
172  prealloc->height != h ||
173  prealloc->components != nc) {
174  prealloc = s_image_create(w, h, nc, data);
175  prealloc->didalloc = 1; /* we did alloc this data */
176  }
177  else {
178  /* copy into preallocated buffer */
179  memcpy(prealloc->data, data, (size_t)w*h*nc);
180 
181  /* we don't need this copy any more */
182  simage_free_image(data);
183  }
184  prealloc->order = SIMAGE_ORDER_RGB;
185  prealloc->openfilename = (char*) malloc(strlen(filename) + 1);
186  strcpy(prealloc->openfilename, filename);
187  return prealloc;
188 }
189 
190 int
191 s_image_save(const char * filename, s_image * image,
192  s_params * params /* | NULL */)
193 {
194  char * ext = NULL;
195  if (params != NULL) {
197  "file type", &ext,
198  NULL);
199  }
200  if (ext == NULL) {
201  ext = (char*) strrchr(filename, '.');
202  if (ext == NULL) return 0;
203 
204  /* skip period */
205  ext++;
206  }
207 
208  return simage_save_image(filename,
209  s_image_data(image),
210  image->width,
211  image->height,
212  image->components,
213  ext);
214 }
int s_image_set_component_order(s_image *image, int order)
Definition: simage12.c:94
int s_params_get(s_params *params,...)
Definition: params.c:251
char * filename
Definition: stream.c:38
s_image * s_image_create(int w, int h, int components, unsigned char *prealloc)
Definition: simage12.c:30
void s_image_destroy(s_image *image)
Definition: simage12.c:56
unsigned char * data
void simage_free_image(unsigned char *imagedata)
Definition: simage.c:406
int s_image_get_component_order(s_image *image)
Definition: simage12.c:102
int s_image_components(s_image *image)
Definition: simage12.c:86
void s_image_set(s_image *image, int w, int h, int components, unsigned char *data, int copydata)
Definition: simage12.c:126
struct simage_open_funcs openfuncs
int s_image_width(s_image *image)
Definition: simage12.c:72
Windows specific information.
int s_image_read_line(s_image *image, int line, unsigned char *buf)
Definition: simage.c:453
void(* close_func)(void *opendata)
unsigned char * s_image_data(s_image *image)
Definition: simage12.c:108
s_image * s_image_load(const char *filename, s_image *prealloc)
Definition: simage12.c:163
int simage_save_image(const char *filename, const unsigned char *bytes, int width, int height, int numcomponents, const char *filenameextension)
Definition: simage_write.c:433
s_params * params
Definition: stream.c:48
int s_image_save(const char *filename, s_image *image, s_params *params)
Definition: simage12.c:191
int s_image_height(s_image *image)
Definition: simage12.c:79
unsigned char * simage_read_image(const char *filename, int *width, int *height, int *numComponents)
Definition: simage.c:292