BRL-CAD
io.h
Go to the documentation of this file.
1/* I O . H
2 * BRL-CAD
3 *
4 * Copyright (c) 2011-2023 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 "common.h"
32#include <stddef.h> /* for size_t */
33#include "bu/mime.h"
34#include "icv/defines.h"
35
36__BEGIN_DECLS
37
38/** @{ */
39/** @file icv/io.h */
40
41/**
42 * This function allocates memory for an image and returns the
43 * resultant image.
44 *
45 * @param width Width of the image to be created
46 * @param height Height of the image to be created
47 * @param color_space Color space of the image (RGB, grayscale)
48 * @return Image structure with allocated space and zeroed data array
49 */
50ICV_EXPORT extern icv_image_t *icv_create(size_t width, size_t height, ICV_COLOR_SPACE color_space);
51
52/**
53 * This function zeroes all the data entries of an image
54 * @param bif Image Structure
55 */
56ICV_EXPORT extern icv_image_t *icv_zero(icv_image_t *bif);
57
58/**
59 * This function frees the allocated memory for a ICV Structure and
60 * data.
61 */
62ICV_EXPORT extern int icv_destroy(icv_image_t *bif);
63
64/**
65 * Function to calculate (or make an educated guess) about the
66 * dimensions of an image, when the image doesn't supply such
67 * information.
68 *
69 * Standard image sizes may be hinted using the label parameter. Many
70 * standard print and display sizes (e.g., "A4" and "SVGA") are
71 * recognized and used in concert with the dpi and data_size
72 * parameters.
73 *
74 * @param[in] label String hinting at a size (pass NULL if not using)
75 * @param[in] dpi Dots per inch of image (pass 0 if not using)
76 * @param[in] data_size Number of uncompressed image bytes (necessary if deducing an unspecified image size)
77 * @param[in] type Image type (necessary if deducing an unspecified image size)
78 *
79 * @param[out] widthp Pointer to variable that will hold image width
80 * @param[out] heightp Pointer to variable that will hold image height
81 *
82 * @return
83 * Returns 1 if an image size was identified, zero otherwise.
84 *
85 */
86ICV_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);
87
88/**
89 * Load a file into an ICV struct. For most formats, this will be
90 * called with format=ICV_IMAGE_AUTO.
91 *
92 * The data is packed in icv_image struct in double format with varied
93 * channels as per the specification of image to be loaded.
94 *
95 * To read stream from stdin pass NULL pointer for filename.
96 *
97 * In case of bw and pix image if size is unknown pass 0 for width and
98 * height. This will read the image till EOF is reached. The image
99 * size of the output image will be : height = 1; width = size; where
100 * size = total bytes read
101 *
102 * @param filename File to read
103 * @param format Probable format of the file, typically
104 * ICV_IMAGE_AUTO
105 * @param width Width when passed as parameter from calling
106 * program.
107 * @param height Height when passed as parameter from calling
108 * program.
109 * @return A newly allocated struct holding the loaded image info.
110 */
111ICV_EXPORT extern icv_image_t *icv_read(const char *filename, bu_mime_image_t format, size_t width, size_t height);
112
113/**
114 * Saves Image to a file or streams to stdout in respective format
115 *
116 * To stream it to stdout pass NULL pointer for filename.
117 *
118 * @param bif Image structure of file.
119 * @param filename Filename of the file to be written.
120 * @param format Specific format of the file to be written.
121 * @return on success 0, on failure -1 with log messages.
122 */
123ICV_EXPORT extern int icv_write(icv_image_t *bif, const char*filename, bu_mime_image_t format);
124
125/**
126 * Write an image line to the data of ICV struct. Can handle unsigned
127 * char buffers.
128 *
129 * Note : This function requires memory allocation for ICV_UCHAR_DATA,
130 * which in turn acquires BU_SEM_SYSCALL semaphore.
131 *
132 * @param bif ICV struct where data is to be written
133 * @param y Index of the line at which data is to be written. 0 for
134 * the first line
135 * @param data Line Data to be written
136 * @param type Type of data, e.g., uint8 data specify ICV_DATA_UCHAR or 1
137 * @return on success 0, on failure -1
138 */
139ICV_EXPORT int icv_writeline(icv_image_t *bif, size_t y, void *data, ICV_DATA type);
140
141/**
142 * Writes a pixel to the specified coordinates in the data of ICV
143 * struct.
144 *
145 * @param bif ICV struct where data is to be written
146 * @param x x-dir coordinate of the pixel
147 * @param y y-dir coordinate of the pixel. (0,0) coordinate is taken
148 * as bottom left
149 * @param data Data to be written
150 * @return on success 0, on failure -1
151 */
152ICV_EXPORT int icv_writepixel(icv_image_t *bif, size_t x, size_t y, double *data);
153
154/**
155 * Converts double data of icv_image to unsigned char data.
156 * This function also does gamma correction using the gamma_corr
157 * parameter of the image structure.
158 *
159 * Gamma correction prevents bad color aliasing.
160 *
161 * @param bif ICV struct where data is to be read from
162 * @return array of unsigned char converted data, or NULL on failure
163 */
164ICV_EXPORT unsigned char *icv_data2uchar(const icv_image_t *bif);
165
166/**
167 * Converts unsigned char array to double array.
168 * This function returns array of double data.
169 *
170 * Used to convert data from pix, bw, ppm type images for icv_image
171 * struct.
172 *
173 * This does not free the char data.
174 *
175 * @param data pointer to the array to be converted.
176 * @param size Size of the array.
177 * @return double array.
178 *
179 */
180ICV_EXPORT double *icv_uchar2double(unsigned char *data, size_t size);
181
182
183/** @} */
184
185__END_DECLS
186
187#endif /* ICV_IO_H */
188
189/*
190 * Local Variables:
191 * tab-width: 8
192 * mode: C
193 * indent-tabs-mode: t
194 * c-file-style: "stroustrup"
195 * End:
196 * ex: shiftwidth=4 tabstop=8
197 */
Header file for the BRL-CAD common definitions.
void float float * y
Definition: tig.h:73
void float float int int int int float * size
Definition: tig.h:132
void float * x
Definition: tig.h:72
ICV_COLOR_SPACE
Definition: defines.h:50
ICV_DATA
Definition: defines.h:56
double * icv_uchar2double(unsigned char *data, size_t size)
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)
unsigned char * icv_data2uchar(const icv_image_t *bif)
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)
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)