2 * Image Library -- Main header file
4 * (c) 2006 Pavel Charvat <pchar@ucw.cz>
6 * This software may be freely distributed and used according to the terms
7 * of the GNU Lesser General Public License.
10 #ifndef _IMAGES_IMAGES_H
11 #define _IMAGES_IMAGES_H
15 #ifdef CONFIG_UCW_CLEAN_ABI
16 #define image_channels_format_to_name ucw_image_channels_format_to_name
17 #define image_clear ucw_image_clear
18 #define image_clone ucw_image_clone
19 #define image_context_cleanup ucw_image_context_cleanup
20 #define image_context_init ucw_image_context_init
21 #define image_context_msg ucw_image_context_msg
22 #define image_context_msg_default ucw_image_context_msg_default
23 #define image_context_msg_silent ucw_image_context_msg_silent
24 #define image_context_vmsg ucw_image_context_vmsg
25 #define image_destroy ucw_image_destroy
26 #define image_dimensions_fit_to_box ucw_image_dimensions_fit_to_box
27 #define image_extension_to_format ucw_image_extension_to_format
28 #define image_file_name_to_format ucw_image_file_name_to_format
29 #define image_format_to_extension ucw_image_format_to_extension
30 #define image_init_matrix ucw_image_init_matrix
31 #define image_init_subimage ucw_image_init_subimage
32 #define image_io_cleanup ucw_image_io_cleanup
33 #define image_io_init ucw_image_io_init
34 #define image_io_read ucw_image_io_read
35 #define image_io_read_data ucw_image_io_read_data
36 #define image_io_read_header ucw_image_io_read_header
37 #define image_io_reset ucw_image_io_reset
38 #define image_io_write ucw_image_io_write
39 #define image_max_bytes ucw_image_max_bytes
40 #define image_max_dim ucw_image_max_dim
41 #define image_name_to_channels_format ucw_image_name_to_channels_format
42 #define image_new ucw_image_new
43 #define image_scale ucw_image_scale
51 * - contexts with error/message handling
52 * - imagelib is thread-safe as long as threads work in different contexts */
54 struct image_context {
55 byte *msg; /* last message */
56 uint msg_code; /* last message code (see images/error.h for details) */
57 bb_t msg_buf; /* message buffer */
58 void (*msg_callback)(struct image_context *ctx); /* called for each message (in msg_{str,code}) */
59 uint tracing_level; /* tracing level (zero to disable) */
62 /* initialization/cleanup */
63 void image_context_init(struct image_context *ctx);
64 void image_context_cleanup(struct image_context *ctx);
66 /* message handling, see images/error.h for useful macros */
67 void image_context_msg(struct image_context *ctx, uint code, char *msg, ...);
68 void image_context_vmsg(struct image_context *ctx, uint code, char *msg, va_list args);
70 /* default callback, displays messages with standard libucw's log() routine */
71 void image_context_msg_default(struct image_context *ctx);
74 void image_context_msg_silent(struct image_context *ctx);
78 * - basic manipulation with images
79 * - image structure is not directly connected to a single context
80 * but manipulation routines are (user must synchronize the access himself)! */
82 extern uint image_max_dim; /* ImageLib.ImageMaxDim */
83 extern uint image_max_bytes; /* ImageLib.ImageMaxBytes */
85 /* SSE aligning size, see IMAGE_SSE_ALIGNED */
86 #define IMAGE_SSE_ALIGN_SIZE 16
89 IMAGE_COLOR_SPACE = 0xf, /* mask for enum color_space */
90 IMAGE_ALPHA = 0x10, /* alpha channel */
91 IMAGE_PIXELS_ALIGNED = 0x20, /* align pixel size to the nearest power of two */
92 IMAGE_SSE_ALIGNED = 0x40, /* align scanlines to multiples of 16 bytes (both start and size) */
93 IMAGE_NEED_DESTROY = 0x80, /* image is allocated with xmalloc */
94 IMAGE_GAPS_PROTECTED = 0x100, /* cannot access gaps between rows */
95 IMAGE_CHANNELS_FORMAT = IMAGE_COLOR_SPACE | IMAGE_ALPHA,
96 IMAGE_PIXEL_FORMAT = IMAGE_CHANNELS_FORMAT | IMAGE_PIXELS_ALIGNED,
97 IMAGE_ALIGNED = IMAGE_PIXELS_ALIGNED | IMAGE_SSE_ALIGNED,
98 IMAGE_NEW_FLAGS = IMAGE_PIXEL_FORMAT | IMAGE_SSE_ALIGNED,
99 IMAGE_INTERNAL_FLAGS = IMAGE_NEED_DESTROY | IMAGE_GAPS_PROTECTED,
102 #define IMAGE_MAX_CHANNELS 4
103 #define IMAGE_CHANNELS_FORMAT_MAX_SIZE 128
104 byte *image_channels_format_to_name(uint format, byte *buf);
105 uint image_name_to_channels_format(byte *name);
108 byte c[IMAGE_MAX_CHANNELS];
113 byte *pixels; /* aligned top left pixel, there are at least sizeof(uint)
114 unused bytes after the buffer (possible optimizations) */
115 uint cols; /* number of columns */
116 uint rows; /* number of rows */
117 uint channels; /* number of color channels including the alpha channel */
118 uint pixel_size; /* size of pixel in bytes (1, 2, 3 or 4) */
119 uint row_size; /* scanline size in bytes */
120 uint row_pixels_size; /* scanline size in bytes excluding rows gaps */
121 uint image_size; /* rows * row_size */
122 uint flags; /* enum image_flag */
125 struct image *image_new(struct image_context *ctx, uint cols, uint rows, uint flags, struct mempool *pool);
126 struct image *image_clone(struct image_context *ctx, struct image *src, uint flags, struct mempool *pool);
127 void image_destroy(struct image *img);
128 void image_clear(struct image_context *ctx, struct image *img);
129 struct image *image_init_matrix(struct image_context *ctx, struct image *img, byte *pixels, uint cols, uint rows, uint row_size, uint flags);
130 struct image *image_init_subimage(struct image_context *ctx, struct image *img, struct image *src, uint left, uint top, uint cols, uint rows);
132 static inline int image_dimensions_valid(uint cols, uint rows)
134 return cols && rows && cols <= image_max_dim && rows <= image_max_dim;
138 int image_scale(struct image_context *ctx, struct image *dest, struct image *src);
139 void image_dimensions_fit_to_box(uint *cols, uint *rows, uint max_cols, uint max_rows, uint upsample);
144 IMAGE_FORMAT_UNDEFINED,
152 /* R - read_header input */
153 /* H - read_header output */
154 /* I - read_data input */
155 /* O - read_data output */
156 /* W - write input */
158 struct image *image; /* [ OW] - image data */
159 enum image_format format; /* [R W] - file format (IMAGE_FORMAT_x) */
160 struct fastbuf *fastbuf; /* [R W] - source/destination stream */
161 struct mempool *pool; /* [ I ] - parameter to image_new */
162 uint cols; /* [ HI ] - number of columns, parameter to image_new */
163 uint rows; /* [ HI ] - number of rows, parameter to image_new */
164 uint flags; /* [ HI ] - see enum image_io_flags */
165 uint jpeg_quality; /* [ W] - JPEG compression quality (1..100) */
166 uint number_of_colors; /* [ H ] - number of image colors */
167 struct color background_color; /* [ HI ] - background color, zero if undefined */
168 uint exif_size; /* [ H W] - EXIF size in bytes (zero if not present) */
169 byte *exif_data; /* [ H W] - EXIF data */
172 struct image_context *context;
173 struct mempool *internal_pool;
175 void (*read_cancel)(struct image_io *io);
178 enum image_io_flags {
179 IMAGE_IO_IMAGE_FLAGS = 0xffff, /* [ HI ] - mask of parameters to image new, read_header fills IMAGE_CHANNELS_FORMAT */
180 IMAGE_IO_NEED_DESTROY = 0x10000, /* [ O ] - enables automatic call of image_destroy */
181 IMAGE_IO_HAS_PALETTE = 0x20000, /* [ H ] - true for image with indexed colors */
182 IMAGE_IO_USE_BACKGROUND = 0x40000, /* [ I ] - merge transparent pixels with background_color */
183 IMAGE_IO_WANT_EXIF = 0x80000, /* [R ] - read EXIF data if present */
186 int image_io_init(struct image_context *ctx, struct image_io *io);
187 void image_io_cleanup(struct image_io *io);
188 void image_io_reset(struct image_io *io);
190 int image_io_read_header(struct image_io *io);
191 struct image *image_io_read_data(struct image_io *io, int ref);
192 struct image *image_io_read(struct image_io *io, int ref);
194 int image_io_write(struct image_io *io);
196 byte *image_format_to_extension(enum image_format format);
197 enum image_format image_extension_to_format(byte *extension);
198 enum image_format image_file_name_to_format(byte *file_name);