X-Git-Url: http://mj.ucw.cz/gitweb/?a=blobdiff_plain;f=images%2Fimages.h;h=fbe7e072421d4490aa91323d97fde8c125bdcb8a;hb=0db6e10eac28f38bfc3b325b13ad95107c58ce1e;hp=402ee28e90b688cd82441eff692a790f7b611692;hpb=b89a8831b2a64f9995eb7ade9d0a9e06da784950;p=libucw.git diff --git a/images/images.h b/images/images.h index 402ee28e..fbe7e072 100644 --- a/images/images.h +++ b/images/images.h @@ -1,121 +1,142 @@ +/* + * Image Library -- Main header file + * + * (c) 2006 Pavel Charvat + * + * This software may be freely distributed and used according to the terms + * of the GNU Lesser General Public License. + */ + #ifndef _IMAGES_IMAGES_H #define _IMAGES_IMAGES_H -#include "lib/mempool.h" +#include + +#ifdef CONFIG_UCW_CLEAN_ABI +#define image_channels_format_to_name ucw_image_channels_format_to_name +#define image_clear ucw_image_clear +#define image_clone ucw_image_clone +#define image_context_cleanup ucw_image_context_cleanup +#define image_context_init ucw_image_context_init +#define image_context_msg ucw_image_context_msg +#define image_context_msg_default ucw_image_context_msg_default +#define image_context_msg_silent ucw_image_context_msg_silent +#define image_context_vmsg ucw_image_context_vmsg +#define image_destroy ucw_image_destroy +#define image_dimensions_fit_to_box ucw_image_dimensions_fit_to_box +#define image_extension_to_format ucw_image_extension_to_format +#define image_file_name_to_format ucw_image_file_name_to_format +#define image_format_to_extension ucw_image_format_to_extension +#define image_init_matrix ucw_image_init_matrix +#define image_init_subimage ucw_image_init_subimage +#define image_io_cleanup ucw_image_io_cleanup +#define image_io_init ucw_image_io_init +#define image_io_read ucw_image_io_read +#define image_io_read_data ucw_image_io_read_data +#define image_io_read_header ucw_image_io_read_header +#define image_io_reset ucw_image_io_reset +#define image_io_write ucw_image_io_write +#define image_max_bytes ucw_image_max_bytes +#define image_max_dim ucw_image_max_dim +#define image_name_to_channels_format ucw_image_name_to_channels_format +#define image_new ucw_image_new +#define image_scale ucw_image_scale +#endif -/* image.c */ +struct mempool; +struct fastbuf; -/* error handling */ -enum image_error { - IMAGE_ERR_OK = 0, - IMAGE_ERR_UNSPECIFIED, - IMAGE_ERR_NOT_IMPLEMENTED, - IMAGE_ERR_INVALID_DIMENSIONS, - IMAGE_ERR_INVALID_FILE_FORMAT, - IMAGE_ERR_INVALID_PIXEL_FORMAT, - IMAGE_ERR_READ_FAILED, - IMAGE_ERR_WRITE_FAILED, - IMAGE_ERR_MAX -}; +/* context.c + * - contexts with error/message handling + * - imagelib is thread-safe as long as threads work in different contexts */ -struct image_thread { - byte *err_msg; - enum image_error err_code; - struct mempool *pool; +struct image_context { + byte *msg; /* last message */ + uns msg_code; /* last message code (see images/error.h for details) */ + bb_t msg_buf; /* message buffer */ + void (*msg_callback)(struct image_context *ctx); /* called for each message (in msg_{str,code}) */ + uns tracing_level; /* tracing level (zero to disable) */ }; -void image_thread_init(struct image_thread *thread); -void image_thread_cleanup(struct image_thread *thread); +/* initialization/cleanup */ +void image_context_init(struct image_context *ctx); +void image_context_cleanup(struct image_context *ctx); -static inline void -image_thread_flush(struct image_thread *thread) -{ - thread->err_code = 0; - thread->err_msg = NULL; - mp_flush(thread->pool); -} +/* message handling, see images/error.h for useful macros */ +void image_context_msg(struct image_context *ctx, uns code, char *msg, ...); +void image_context_vmsg(struct image_context *ctx, uns code, char *msg, va_list args); -static inline void -image_thread_err(struct image_thread *thread, uns code, char *msg) -{ - thread->err_code = code; - thread->err_msg = (byte *)msg; -} +/* default callback, displays messages with standard libucw's log() routine */ +void image_context_msg_default(struct image_context *ctx); -static inline void -image_thread_err_dup(struct image_thread *thread, uns code, char *msg) -{ - thread->err_code = code; - thread->err_msg = mp_strdup(thread->pool, msg); -} +/* empty callback */ +void image_context_msg_silent(struct image_context *ctx); -void image_thread_err_format(struct image_thread *thread, uns code, char *msg, ...); -/* basic image manupulation */ +/* image.c + * - basic manipulation with images + * - image structure is not directly connected to a single context + * but manipulation routines are (user must synchronize the access himself)! */ -#define IMAGE_MAX_SIZE 0xffffU /* maximum number of cols/rows, must be <(1<<16) */ -#define IMAGE_SSE_ALIGN_SIZE (MAX(16, sizeof(uns))) +extern uns image_max_dim; /* ImageLib.ImageMaxDim */ +extern uns image_max_bytes; /* ImageLib.ImageMaxBytes */ -enum color_space { - COLOR_SPACE_UNKNOWN, - COLOR_SPACE_GRAYSCALE, - COLOR_SPACE_RGB, - COLOR_SPACE_MAX -}; +/* SSE aligning size, see IMAGE_SSE_ALIGNED */ +#define IMAGE_SSE_ALIGN_SIZE 16 enum image_flag { - IMAGE_COLOR_SPACE = 0x7, /* mask for enum color_space */ - IMAGE_ALPHA = 0x8, /* alpha channel */ - IMAGE_PIXELS_ALIGNED = 0x10, /* align pixel size to the nearest power of two */ - IMAGE_SSE_ALIGNED = 0x20, /* align scanlines to multiples of 16 bytes (both start and size) */ - IMAGE_NEED_DESTROY = 0x40, /* image is allocated with xmalloc */ + IMAGE_COLOR_SPACE = 0xf, /* mask for enum color_space */ + IMAGE_ALPHA = 0x10, /* alpha channel */ + IMAGE_PIXELS_ALIGNED = 0x20, /* align pixel size to the nearest power of two */ + IMAGE_SSE_ALIGNED = 0x40, /* align scanlines to multiples of 16 bytes (both start and size) */ + IMAGE_NEED_DESTROY = 0x80, /* image is allocated with xmalloc */ + IMAGE_GAPS_PROTECTED = 0x100, /* cannot access gaps between rows */ IMAGE_CHANNELS_FORMAT = IMAGE_COLOR_SPACE | IMAGE_ALPHA, IMAGE_PIXEL_FORMAT = IMAGE_CHANNELS_FORMAT | IMAGE_PIXELS_ALIGNED, IMAGE_ALIGNED = IMAGE_PIXELS_ALIGNED | IMAGE_SSE_ALIGNED, + IMAGE_NEW_FLAGS = IMAGE_PIXEL_FORMAT | IMAGE_SSE_ALIGNED, + IMAGE_INTERNAL_FLAGS = IMAGE_NEED_DESTROY | IMAGE_GAPS_PROTECTED, }; -struct image { - byte *pixels; /* left top pixel, there are at least sizeof(uns) - unsed bytes after the buffer (possible optimizations) */ - u32 cols; /* number of columns */ - u32 rows; /* number of rows */ - u32 pixel_size; /* size of pixel (1, 2, 3 or 4) */ - u32 row_size; /* scanline size in bytes */ - u32 image_size; /* rows * rows_size */ - u32 flags; /* enum image_flag */ -}; - -struct image *image_new(struct image_thread *it, uns cols, uns rows, uns flags, struct mempool *pool); -struct image *image_clone(struct image_thread *it, struct image *src, uns flags, struct mempool *pool); -void image_destroy(struct image *img); /* only with NULL mempool */ -void image_clear(struct image_thread *it, struct image *img); -int image_init_matrix(struct image_thread *it, struct image *img, byte *pixels, uns cols, uns rows, uns row_size, uns flags); -int image_init_subimage(struct image_thread *it, struct image *img, struct image *src, uns left, uns top, uns cols, uns rows); - -static inline int -image_dimensions_valid(uns cols, uns rows) -{ - return cols && rows && cols <= IMAGE_MAX_SIZE && rows <= IMAGE_MAX_SIZE; -} - -byte *color_space_to_name(enum color_space cs); -byte *image_channels_format_to_name(uns format); +#define IMAGE_MAX_CHANNELS 4 +#define IMAGE_CHANNELS_FORMAT_MAX_SIZE 128 +byte *image_channels_format_to_name(uns format, byte *buf); uns image_name_to_channels_format(byte *name); struct color { - byte c[3]; + byte c[IMAGE_MAX_CHANNELS]; byte color_space; -} PACKED; +}; -/* scale.c */ +struct image { + byte *pixels; /* aligned top left pixel, there are at least sizeof(uns) + unused bytes after the buffer (possible optimizations) */ + uns cols; /* number of columns */ + uns rows; /* number of rows */ + uns channels; /* number of color channels including the alpha channel */ + uns pixel_size; /* size of pixel in bytes (1, 2, 3 or 4) */ + uns row_size; /* scanline size in bytes */ + uns row_pixels_size; /* scanline size in bytes excluding rows gaps */ + uns image_size; /* rows * row_size */ + uns flags; /* enum image_flag */ +}; -int image_scale(struct image_thread *thread, struct image *dest, struct image *src); -void image_dimensions_fit_to_box(u32 *cols, u32 *rows, u32 max_cols, u32 max_rows, uns upsample); +struct image *image_new(struct image_context *ctx, uns cols, uns rows, uns flags, struct mempool *pool); +struct image *image_clone(struct image_context *ctx, struct image *src, uns flags, struct mempool *pool); +void image_destroy(struct image *img); +void image_clear(struct image_context *ctx, struct image *img); +struct image *image_init_matrix(struct image_context *ctx, struct image *img, byte *pixels, uns cols, uns rows, uns row_size, uns flags); +struct image *image_init_subimage(struct image_context *ctx, struct image *img, struct image *src, uns left, uns top, uns cols, uns rows); -/* alpha.c */ +static inline int image_dimensions_valid(uns cols, uns rows) +{ + return cols && rows && cols <= image_max_dim && rows <= image_max_dim; +} +/* scale.c */ -int image_apply_background(struct image_thread *thread, struct image *dest, struct image *src, struct color *background); +int image_scale(struct image_context *ctx, struct image *dest, struct image *src); +void image_dimensions_fit_to_box(uns *cols, uns *rows, uns max_cols, uns max_rows, uns upsample); /* image-io.c */ @@ -128,25 +149,27 @@ enum image_format { }; struct image_io { - /* R - read_header input */ - /* H - read_header output */ - /* I - read_data input */ - /* O - read_data output */ - /* W - write input */ + /* R - read_header input */ + /* H - read_header output */ + /* I - read_data input */ + /* O - read_data output */ + /* W - write input */ struct image *image; /* [ OW] - image data */ enum image_format format; /* [R W] - file format (IMAGE_FORMAT_x) */ - struct fastbuf *fastbuf; /* [R W] - source/destination stream */ + struct fastbuf *fastbuf; /* [R W] - source/destination stream */ struct mempool *pool; /* [ I ] - parameter to image_new */ - u32 cols; /* [ HI ] - number of columns, parameter to image_new */ - u32 rows; /* [ HI ] - number of rows, parameter to image_new */ - u32 flags; /* [ HI ] - see enum image_io_flags */ - u32 jpeg_quality; /* [ W] - JPEG compression quality (1..100) */ - u32 number_of_colors; /* [ H ] - number of image colors */ + uns cols; /* [ HI ] - number of columns, parameter to image_new */ + uns rows; /* [ HI ] - number of rows, parameter to image_new */ + uns flags; /* [ HI ] - see enum image_io_flags */ + uns jpeg_quality; /* [ W] - JPEG compression quality (1..100) */ + uns number_of_colors; /* [ H ] - number of image colors */ struct color background_color; /* [ HI ] - background color, zero if undefined */ + uns exif_size; /* [ H W] - EXIF size in bytes (zero if not present) */ + byte *exif_data; /* [ H W] - EXIF data */ /* internals */ - struct image_thread *thread; + struct image_context *context; struct mempool *internal_pool; void *read_data; void (*read_cancel)(struct image_io *io); @@ -155,11 +178,12 @@ struct image_io { enum image_io_flags { IMAGE_IO_IMAGE_FLAGS = 0xffff, /* [ HI ] - mask of parameters to image new, read_header fills IMAGE_CHANNELS_FORMAT */ IMAGE_IO_NEED_DESTROY = 0x10000, /* [ O ] - enables automatic call of image_destroy */ - IMAGE_IO_HAS_PALETTE = 0x10000, /* [ H ] - true for image with indexed colors */ + IMAGE_IO_HAS_PALETTE = 0x20000, /* [ H ] - true for image with indexed colors */ IMAGE_IO_USE_BACKGROUND = 0x40000, /* [ I ] - merge transparent pixels with background_color */ + IMAGE_IO_WANT_EXIF = 0x80000, /* [R ] - read EXIF data if present */ }; -void image_io_init(struct image_thread *it, struct image_io *io); +int image_io_init(struct image_context *ctx, struct image_io *io); void image_io_cleanup(struct image_io *io); void image_io_reset(struct image_io *io);