]> mj.ucw.cz Git - libucw.git/blob - images/images.h
- removed testing code from io-libungif
[libucw.git] / images / images.h
1 #ifndef _IMAGES_IMAGES_H
2 #define _IMAGES_IMAGES_H
3
4 #include "lib/mempool.h"
5
6 /* image.c */
7
8 /* error handling */
9
10 enum image_error {
11   IMAGE_ERR_OK = 0,
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,
19   IMAGE_ERR_MAX
20 };
21
22 struct image_thread {
23   byte *err_msg;
24   enum image_error err_code;
25   struct mempool *pool;
26 };
27
28 void image_thread_init(struct image_thread *thread);
29 void image_thread_cleanup(struct image_thread *thread);
30
31 static inline void
32 image_thread_flush(struct image_thread *thread)
33 {
34   thread->err_code = 0;
35   thread->err_msg = NULL;
36   mp_flush(thread->pool);
37 }
38
39 static inline void
40 image_thread_err(struct image_thread *thread, uns code, char *msg)
41 {
42   thread->err_code = code;
43   thread->err_msg = (byte *)msg;
44 }
45
46 static inline void
47 image_thread_err_dup(struct image_thread *thread, uns code, char *msg)
48 {
49   thread->err_code = code;
50   thread->err_msg = mp_strdup(thread->pool, msg);
51 }
52
53 void image_thread_err_format(struct image_thread *thread, uns code, char *msg, ...);
54
55 /* basic image manupulation */
56
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)))
59
60 enum color_space {
61   COLOR_SPACE_UNKNOWN,
62   COLOR_SPACE_GRAYSCALE,
63   COLOR_SPACE_RGB,
64   COLOR_SPACE_MAX
65 };
66
67 enum image_flag {
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_NEED_DESTROY = 0x40,    /* image is allocated with xmalloc */
73   IMAGE_CHANNELS_FORMAT = IMAGE_COLOR_SPACE | IMAGE_ALPHA,
74   IMAGE_PIXEL_FORMAT = IMAGE_CHANNELS_FORMAT | IMAGE_PIXELS_ALIGNED,
75   IMAGE_ALIGNED = IMAGE_PIXELS_ALIGNED | IMAGE_SSE_ALIGNED,
76 };
77
78 struct image {
79   byte *pixels;                 /* left top pixel, there are at least sizeof(uns)
80                                    unsed bytes after the buffer (possible optimizations) */
81   u32 cols;                     /* number of columns */
82   u32 rows;                     /* number of rows */
83   u32 pixel_size;               /* size of pixel (1, 2, 3 or 4) */
84   u32 row_size;                 /* scanline size in bytes */
85   u32 image_size;               /* rows * rows_size */
86   u32 flags;                    /* enum image_flag */
87 };
88
89 struct image *image_new(struct image_thread *it, uns cols, uns rows, uns flags, struct mempool *pool);
90 struct image *image_clone(struct image_thread *it, struct image *src, uns flags, struct mempool *pool);
91 void image_destroy(struct image *img); /* only with NULL mempool */
92 void image_clear(struct image_thread *it, struct image *img);
93 int image_init_matrix(struct image_thread *it, struct image *img, byte *pixels, uns cols, uns rows, uns row_size, uns flags);
94 int image_init_subimage(struct image_thread *it, struct image *img, struct image *src, uns left, uns top, uns cols, uns rows);
95
96 static inline int
97 image_dimensions_valid(uns cols, uns rows)
98 {
99   return cols && rows && cols <= IMAGE_MAX_SIZE && rows <= IMAGE_MAX_SIZE;
100 }
101
102 byte *color_space_to_name(enum color_space cs);
103 byte *image_channels_format_to_name(uns format);
104 uns image_name_to_channels_format(byte *name);
105
106 struct color {
107   byte c[3];
108   byte color_space;
109 } PACKED;
110
111 /* scale.c */
112
113 int image_scale(struct image_thread *thread, struct image *dest, struct image *src);
114 void image_dimensions_fit_to_box(u32 *cols, u32 *rows, u32 max_cols, u32 max_rows, uns upsample);
115
116 /* alpha.c */
117
118 int image_apply_background(struct image_thread *thread, struct image *dest, struct image *src, struct color *background);
119
120 /* image-io.c */
121
122 enum image_format {
123   IMAGE_FORMAT_UNDEFINED,
124   IMAGE_FORMAT_JPEG,
125   IMAGE_FORMAT_PNG,
126   IMAGE_FORMAT_GIF,
127   IMAGE_FORMAT_MAX
128 };
129
130 struct image_io {
131                                         /*  R - read_header input */
132                                         /*   H - read_header output */
133                                         /*    I - read_data input */
134                                         /*     O - read_data output */
135                                         /*      W - write input */
136
137   struct image *image;                  /* [   OW] - image data */
138   enum image_format format;             /* [R   W] - file format (IMAGE_FORMAT_x) */
139   struct fastbuf *fastbuf;              /* [R   W] - source/destination stream */
140   struct mempool *pool;                 /* [  I  ] - parameter to image_new */
141   u32 cols;                             /* [ HI  ] - number of columns, parameter to image_new */
142   u32 rows;                             /* [ HI  ] - number of rows, parameter to image_new */
143   u32 flags;                            /* [ HI  ] - see enum image_io_flags */
144   u32 jpeg_quality;                     /* [    W] - JPEG compression quality (1..100) */
145   u32 number_of_colors;                 /* [ H   ] - number of image colors */
146   struct color background_color;        /* [ HI  ] - background color, zero if undefined */
147
148   /* internals */
149   struct image_thread *thread;
150   struct mempool *internal_pool;
151   int image_destroy;
152   void *read_data;
153   void (*read_cancel)(struct image_io *io);
154 };
155
156 enum image_io_flags {
157   IMAGE_IO_IMAGE_FLAGS = 0xffff,        /* [ HI  ] - mask of parameters to image new, read_header fills IMAGE_CHANNELS_FORMAT */
158   IMAGE_IO_HAS_PALETTE = 0x10000,       /* [ H   ] - true for image with indexed colors */
159   IMAGE_IO_USE_BACKGROUND = 0x20000,    /* [  I  ] - merge transparent pixels with background_color */
160 };
161
162 void image_io_init(struct image_thread *it, struct image_io *io);
163 void image_io_cleanup(struct image_io *io);
164 void image_io_reset(struct image_io *io);
165
166 int image_io_read_header(struct image_io *io);
167 struct image *image_io_read_data(struct image_io *io, int ref);
168 struct image *image_io_read(struct image_io *io, int ref);
169
170 int image_io_write(struct image_io *io);
171
172 byte *image_format_to_extension(enum image_format format);
173 enum image_format image_extension_to_format(byte *extension);
174 enum image_format image_file_name_to_format(byte *file_name);
175
176 #endif