BRL-CAD
Loading...
Searching...
No Matches
io.h
Go to the documentation of this file.
1/* I O . H
2 * BRL-CAD
3 *
4 * Copyright (c) 2011-2026 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/** @addtogroup icv_io
21 *
22 * @brief
23 * Functions provided by the LIBICV image processing library for reading
24 * and writing of images.
25 *
26 */
27
28#ifndef ICV_IO_H
29#define ICV_IO_H
30
31#include <stddef.h> /* for size_t */
32
33#include "common.h"
34#include "vmath.h"
35#include "bu/mime.h"
36#include "icv/defines.h"
37
39
40/** @{ */
41/** @file icv/io.h */
42
43/**
44 * This function allocates memory for an image and returns the
45 * resultant image.
46 *
47 * @param width Width of the image to be created
48 * @param height Height of the image to be created
49 * @param color_space Color space of the image (RGB, grayscale)
50 * @return Image structure with allocated space and zeroed data array, or
51 * NULL if the arguments are invalid or allocation fails.
52 */
53ICV_EXPORT extern icv_image_t *icv_create(size_t width, size_t height, ICV_COLOR_SPACE color_space);
54
55/**
56 * This function zeroes all the data entries of an image
57 * @param bif Image Structure
58 */
60
61/**
62 * This function frees the allocated memory for a ICV Structure and
63 * data.
64 */
66
67/**
68 * Function to calculate (or make an educated guess) about the
69 * dimensions of an image, when the image doesn't supply such
70 * information.
71 *
72 * Standard image sizes may be hinted using the label parameter. Many
73 * standard print and display sizes (e.g., "A4" and "SVGA") are
74 * recognized and used in concert with the dpi and data_size
75 * parameters.
76 *
77 * @param[in] label String hinting at a size (pass NULL if not using)
78 * @param[in] dpi Dots per inch of image (pass 0 if not using)
79 * @param[in] data_size Number of uncompressed image bytes (necessary if deducing an unspecified image size)
80 * @param[in] type Image type (necessary if deducing an unspecified image size)
81 *
82 * @param[out] widthp Pointer to variable that will hold image width
83 * @param[out] heightp Pointer to variable that will hold image height
84 *
85 * @return
86 * Returns 1 if an image size was identified, zero otherwise.
87 *
88 */
89ICV_EXPORT extern int icv_image_size(const char *label, size_t dpi, size_t data_size, bu_mime_image_t type, size_t *widthp, size_t *heightp);
90
91/**
92 * Load a file into an ICV struct. For most formats, this will be
93 * called with format=ICV_IMAGE_AUTO.
94 *
95 * The data is packed in icv_image struct in double format with varied
96 * channels as per the specification of image to be loaded.
97 *
98 * To read stream from stdin pass NULL pointer for filename.
99 *
100 * In case of bw and pix image if size is unknown pass 0 for width and
101 * height. This will read the image till EOF is reached. The image
102 * size of the output image will be : height = 1; width = size; where
103 * size = total bytes read
104 *
105 * @param filename File to read
106 * @param format Probable format of the file, typically
107 * ICV_IMAGE_AUTO
108 * @param width Width when passed as parameter from calling
109 * program.
110 * @param height Height when passed as parameter from calling
111 * program.
112 * @return A newly allocated struct holding the loaded image info.
113 */
114ICV_EXPORT extern icv_image_t *icv_read(const char *filename, bu_mime_image_t format, size_t width, size_t height);
115
116/**
117 * Load an image from a memory buffer.
118 *
119 * @param buffer Pointer to the memory buffer.
120 * @param size Size of the memory buffer in bytes.
121 * @param format Probable format of the image.
122 * @param width Width when passed as parameter from calling program.
123 * @param height Height when passed as parameter from calling program.
124 * @return A newly allocated struct holding the loaded image info, or NULL on failure.
125 */
126ICV_EXPORT extern icv_image_t *icv_read_mem(const unsigned char *buffer, size_t size, bu_mime_image_t format, size_t width, size_t height);
127
128/**
129 * Saves Image to a file or streams to stdout in respective format
130 *
131 * To stream it to stdout pass NULL pointer for filename.
132 * Writers do not modify the input image. If a format requires a different
133 * channel layout, conversion is performed on a temporary image; alpha is
134 * dropped only for formats that cannot represent it.
135 *
136 * @param bif Image structure of file.
137 * @param filename Filename of the file to be written.
138 * @param format Specific format of the file to be written.
139 * @return on success 0, on failure -1 with log messages.
140 */
141ICV_EXPORT extern int icv_write(icv_image_t *bif, const char*filename, bu_mime_image_t format);
142
143/**
144 * Saves Image to a dynamically allocated memory buffer in the respective format.
145 * The input image is not modified. If a format requires a different channel
146 * layout, conversion is performed on a temporary image; alpha is dropped only
147 * for formats that cannot represent it.
148 *
149 * @param bif Image structure.
150 * @param buffer Pointer to an unsigned char pointer to hold the allocated memory.
151 * @param size Pointer to a size_t to hold the size of the written buffer.
152 * @param format Specific format of the file to be written.
153 * @return on success 0, on failure -1 with log messages.
154 *
155 * Note: The caller is responsible for freeing *buffer using bu_free().
156 */
157ICV_EXPORT extern int icv_write_mem(icv_image_t *bif, unsigned char **buffer, size_t *size, bu_mime_image_t format);
158
159/**
160 * Write an image line to the data of ICV struct. Can handle unsigned
161 * char buffers.
162 *
163 * Note : This function requires memory allocation for ICV_UCHAR_DATA,
164 * which in turn acquires BU_SEM_SYSCALL semaphore.
165 *
166 * @param bif ICV struct where data is to be written
167 * @param y Index of the line at which data is to be written. 0 for
168 * the first line
169 * @param data Line Data to be written
170 * @param type Type of data, e.g., uint8 data specify ICV_DATA_UCHAR or 1
171 * @return on success 0, on failure -1
172 */
173ICV_EXPORT int icv_writeline(icv_image_t *bif, size_t y, void *data, ICV_DATA type);
174
175/**
176 * Writes a pixel to the specified coordinates in the data of ICV
177 * struct.
178 *
179 * @param bif ICV struct where data is to be written
180 * @param x x-dir coordinate of the pixel
181 * @param y y-dir coordinate of the pixel. (0,0) coordinate is taken
182 * as bottom left
183 * @param data Data to be written
184 * @return on success 0, on failure -1
185 */
186ICV_EXPORT int icv_writepixel(icv_image_t *bif, size_t x, size_t y, double *data);
187
188/**
189 * Converts double data of icv_image to unsigned char data.
190 * This function also does gamma correction using the gamma_corr
191 * parameter of the image structure.
192 *
193 * Gamma correction prevents bad color aliasing.
194 *
195 * @param bif ICV struct where data is to be read from
196 * @return array of unsigned char converted data, or NULL on failure
197 */
199
200/**
201 * Converts unsigned char array to double array.
202 * This function returns array of double data.
203 *
204 * Used to convert data from pix, bw, ppm type images for icv_image
205 * struct.
206 *
207 * This does not free the char data.
208 *
209 * @param data pointer to the array to be converted.
210 * @param size Size of the array.
211 * @return double array.
212 *
213 */
214ICV_EXPORT double *icv_uchar2double(unsigned char *data, size_t size);
215
216
217/**
218 * Options that can be passed to icv_ascii_art to modify its output.
219 */
225#define ICV_ASCII_ART_PARAMS_DEFAULT {0, 0, 1.0}
226
227/**
228 * Converts the image to an ASCII art text string. This function returns a
229 * char array - it is the caller's responsibility to free it.
230 *
231 * Note that this function does not attempt to correct for the aspect ratio
232 * problem (character printing in terminals often will result in the image
233 * being "stretched" in height.) If the use case calls for that, the
234 * application should use icv_resize to adjust the image before calling
235 * icv_ascii_art. (See the ascii libicv test code for an example.)
236 *
237 * @param i ICV image data.
238 * @param p ASCII text generation parameters.
239 * @return char array.
240 *
241 */
243
244/**
245 * Allocate and zero-initialise a new icv_render_info struct.
246 * Caller is responsible for filling in the fields and eventually
247 * calling icv_render_info_destroy().
248 */
250
251/**
252 * Release all memory owned by an icv_render_info, including the struct itself.
253 *
254 * @return 0 on success.
255 */
257
258/**
259 * Attach render metadata to an image. Any existing render_info on the
260 * image is first freed. Passing NULL clears the metadata. On success,
261 * ownership of info transfers to img.
262 *
263 * @return 0 on success, -1 if img is NULL.
264 */
266
267/**
268 * Return the render metadata attached to an image, or NULL if none.
269 */
271
272/** @} */
273
275
276#endif /* ICV_IO_H */
277
278/*
279 * Local Variables:
280 * tab-width: 8
281 * mode: C
282 * indent-tabs-mode: t
283 * c-file-style: "stroustrup"
284 * End:
285 * ex: shiftwidth=4 tabstop=8
286 */
Definition dvec.h:74
Header file for the BRL-CAD common definitions.
ICV_COLOR_SPACE
Definition defines.h:50
ICV_DATA
Definition defines.h:56
double * icv_uchar2double(unsigned char *data, size_t size)
char * icv_ascii_art(icv_image_t *i, struct icv_ascii_art_params *p)
int icv_image_size(const char *label, size_t dpi, size_t data_size, bu_mime_image_t type, size_t *widthp, size_t *heightp)
int icv_destroy(icv_image_t *bif)
icv_image_t * icv_read(const char *filename, bu_mime_image_t format, size_t width, size_t height)
int icv_writeline(icv_image_t *bif, size_t y, void *data, ICV_DATA type)
struct icv_render_info * icv_image_get_render_info(const icv_image_t *img)
int icv_render_info_destroy(struct icv_render_info *info)
int icv_write_mem(icv_image_t *bif, unsigned char **buffer, size_t *size, bu_mime_image_t format)
struct icv_render_info * icv_render_info_create(void)
unsigned char * icv_data2uchar(const icv_image_t *bif)
icv_image_t * icv_read_mem(const unsigned char *buffer, size_t size, bu_mime_image_t format, size_t width, size_t height)
int icv_writepixel(icv_image_t *bif, size_t x, size_t y, double *data)
int icv_write(icv_image_t *bif, const char *filename, bu_mime_image_t format)
int icv_image_set_render_info(icv_image_t *img, struct icv_render_info *info)
icv_image_t * icv_zero(icv_image_t *bif)
icv_image_t * icv_create(size_t width, size_t height, ICV_COLOR_SPACE color_space)
fastf_t brightness_multiplier
Definition io.h:223
fundamental vector, matrix, quaternion math macros