1 #ifndef _IMAGES_IMAGES_H
2 #define _IMAGES_IMAGES_H
4 #include "lib/mempool.h"
12 IMAGE_ERR_UNSPECIFIED,
13 IMAGE_ERR_NOT_IMPLEMENTED,
14 IMAGE_ERR_INVALID_DIMENSIONS,
15 IMAGE_ERR_INVALID_FILE_FORMAT,
16 IMAGE_ERR_INVALID_PIXEL_FORMAT,
17 IMAGE_ERR_READ_FAILED,
18 IMAGE_ERR_WRITE_FAILED,
24 enum image_error err_code;
28 void image_thread_init(struct image_thread *thread);
29 void image_thread_cleanup(struct image_thread *thread);
32 image_thread_flush(struct image_thread *thread)
35 thread->err_msg = NULL;
36 mp_flush(thread->pool);
40 image_thread_err(struct image_thread *thread, uns code, char *msg)
42 thread->err_code = code;
43 thread->err_msg = (byte *)msg;
47 image_thread_err_dup(struct image_thread *thread, uns code, char *msg)
49 thread->err_code = code;
50 thread->err_msg = mp_strdup(thread->pool, msg);
53 void image_thread_err_format(struct image_thread *thread, uns code, char *msg, ...);
55 /* basic image manupulation */
57 #define IMAGE_MAX_SIZE 0xffffU /* maximum number of cols/rows, must be <(1<<16) */
58 #define IMAGE_SSE_ALIGN_SIZE (MAX(16, sizeof(uns)))
62 COLOR_SPACE_GRAYSCALE,
68 IMAGE_COLOR_SPACE = 0x7, /* mask for enum color_space */
69 IMAGE_ALPHA = 0x8, /* alpha channel */
70 IMAGE_PIXELS_ALIGNED = 0x10, /* align pixel size to the nearest power of two */
71 IMAGE_SSE_ALIGNED = 0x20, /* align scanlines to multiples of 16 bytes (both start and size) */
72 IMAGE_CHANNELS_FORMAT = IMAGE_COLOR_SPACE | IMAGE_ALPHA,
73 IMAGE_PIXEL_FORMAT = IMAGE_CHANNELS_FORMAT | IMAGE_PIXELS_ALIGNED,
74 IMAGE_ALIGNED = IMAGE_PIXELS_ALIGNED | IMAGE_SSE_ALIGNED,
78 byte *pixels; /* left top pixel, there are at least sizeof(uns)
79 unsed bytes after the buffer (possible optimizations) */
80 u32 cols; /* number of columns */
81 u32 rows; /* number of rows */
82 u32 pixel_size; /* size of pixel (1, 2, 3 or 4) */
83 u32 row_size; /* scanline size in bytes */
84 u32 image_size; /* size of pixels buffer (rows * rows_size) */
85 u32 flags; /* enum image_flag */
88 struct image *image_new(struct image_thread *it, uns cols, uns rows, uns flags, struct mempool *pool);
89 struct image *image_clone(struct image_thread *it, struct image *src, uns flags, struct mempool *pool);
90 void image_destroy(struct image *img); /* only with NULL mempool */
91 void image_clear(struct image_thread *it, struct image *img);
93 byte *color_space_to_name(enum color_space cs);
94 byte *image_channels_format_to_name(uns format);
95 uns image_name_to_channels_format(byte *name);
104 int image_scale(struct image_thread *thread, struct image *dest, struct image *src);
105 void image_dimensions_fit_to_box(u32 *cols, u32 *rows, u32 max_cols, u32 max_rows, uns upsample);
109 int image_apply_background(struct image_thread *thread, struct image *dest, struct image *src, struct color *background);
114 IMAGE_FORMAT_UNDEFINED,
122 /* R - read_header input */
123 /* H - read_header output */
124 /* I - read_data input */
125 /* O - read_data output */
126 /* W - write input */
128 struct image *image; /* [ OW] - image data */
129 enum image_format format; /* [R W] - file format (IMAGE_FORMAT_x) */
130 struct fastbuf *fastbuf; /* [R W] - source/destination stream */
131 struct mempool *pool; /* [ I ] - parameter to image_new */
132 u32 cols; /* [ HI ] - number of columns, parameter to image_new */
133 u32 rows; /* [ HI ] - number of rows, parameter to image_new */
134 u32 flags; /* [ HI ] - see enum image_io_flags */
135 u32 jpeg_quality; /* [ W] - JPEG compression quality (1..100) */
136 u32 number_of_colors; /* [ H ] - number of image colors */
137 struct color background_color; /* [ HI ] - background color, zero if undefined */
140 struct image_thread *thread;
141 struct mempool *internal_pool;
144 void (*read_cancel)(struct image_io *io);
147 enum image_io_flags {
148 IMAGE_IO_IMAGE_FLAGS = 0xffff, /* [ HI ] - mask of parameters to image new, read_header fills IMAGE_CHANNELS_FORMAT */
149 IMAGE_IO_HAS_PALETTE = 0x10000, /* [ H ] - true for image with indexed colors */
150 IMAGE_IO_HAS_BACKGROUND = 0x20000, /* [ H ] - picture contains background info */
151 IMAGE_IO_USE_BACKGROUND = 0x40000, /* [ I ] - merge transparent pixels with background_color */
154 void image_io_init(struct image_thread *it, struct image_io *io);
155 void image_io_cleanup(struct image_io *io);
156 void image_io_reset(struct image_io *io);
158 int image_io_read_header(struct image_io *io);
159 struct image *image_io_read_data(struct image_io *io, int ref);
160 struct image *image_io_read(struct image_io *io, int ref);
162 int image_io_write(struct image_io *io);
164 byte *image_format_to_extension(enum image_format format);
165 enum image_format image_extension_to_format(byte *extension);
166 enum image_format image_file_name_to_format(byte *file_name);