]> mj.ucw.cz Git - libucw.git/blob - images/images.h
ee68a52b35842c43cb8a6854eca0075154d4ff41
[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 void image_thread_err_format(struct image_thread *thread, uns code, char *msg, ...);
47
48 /* basic image manupulation */
49
50 enum color_space {
51   COLOR_SPACE_UNKNOWN,
52   COLOR_SPACE_GRAYSCALE,
53   COLOR_SPACE_RGB,
54   COLOR_SPACE_MAX
55 };
56
57 enum image_flag {
58   IMAGE_COLOR_SPACE = 0x7,      /* mask for enum color_space */
59   IMAGE_ALPHA = 0x8,            /* alpha channel */
60   IMAGE_PIXELS_ALIGNED = 0x10,  /* align pixel size to the nearest power of two  */
61   IMAGE_SSE_ALIGNED = 0x20,     /* align scanlines to multiples of 16 bytes (both start and size) */
62   IMAGE_CHANNELS_FORMAT = IMAGE_COLOR_SPACE | IMAGE_ALPHA,
63   IMAGE_PIXEL_FORMAT = IMAGE_CHANNELS_FORMAT | IMAGE_PIXELS_ALIGNED,
64   IMAGE_ALIGNED = IMAGE_PIXELS_ALIGNED | IMAGE_SSE_ALIGNED,
65 };
66
67 struct image {
68   byte *pixels;                 /* left top pixel, there are at least sizeof(uns)
69                                    unsed bytes after the buffer (possible optimizations) */
70   u32 cols;                     /* number of columns */
71   u32 rows;                     /* number of rows */
72   u32 pixel_size;               /* size of pixel (1, 2, 3 or 4) */
73   u32 row_size;                 /* scanline size in bytes */
74   u32 image_size;               /* size of pixels buffer (rows * rows_size) */
75   u32 flags;                    /* enum image_flag */
76 };
77
78 struct image *image_new(struct image_thread *it, uns cols, uns rows, uns flags, struct mempool *pool);
79 struct image *image_clone(struct image_thread *it, struct image *src, uns flags, struct mempool *pool);
80 void image_destroy(struct image_thread *it, struct image *img); /* only with NULL mempool */
81 void image_clear(struct image_thread *it, struct image *img);
82
83 byte *color_space_to_name(enum color_space cs);
84 byte *image_channels_format_to_name(uns format);
85 uns image_name_to_channels_format(byte *name);
86
87 /* scale.c */
88
89 int image_scale(struct image_thread *thread, struct image *dest, struct image *src);
90 void image_dimensions_fit_to_box(u32 *cols, u32 *rows, u32 max_cols, u32 max_rows, uns upsample);
91
92 /* image-io.c */
93
94 enum image_format {
95   IMAGE_FORMAT_UNKNOWN,
96   IMAGE_FORMAT_JPEG,
97   IMAGE_FORMAT_PNG,
98   IMAGE_FORMAT_GIF,
99   IMAGE_FORMAT_MAX
100 };
101
102 struct image_io {
103   struct image *image;
104   struct fastbuf *fastbuf;
105   enum image_format format;
106   struct mempool *pool;
107   u32 cols;
108   u32 rows;
109   u32 flags;
110   /* internals */
111   struct image_thread *thread;
112   struct mempool *internal_pool;
113   int image_destroy;
114   void *read_data;
115   void (*read_cancel)(struct image_io *io);
116   union {
117     struct {
118     } jpeg;
119     struct {
120     } png;
121     struct {
122     } gif;
123   };
124 };
125
126 void image_io_init(struct image_thread *it, struct image_io *io);
127 void image_io_cleanup(struct image_io *io);
128 void image_io_reset(struct image_io *io);
129
130 int image_io_read_header(struct image_io *io);
131 struct image *image_io_read_data(struct image_io *io, int ref);
132 struct image *image_io_read(struct image_io *io, int ref);
133
134 int image_io_write(struct image_io *io);
135
136 byte *image_format_to_extension(enum image_format format);
137 enum image_format image_extension_to_format(byte *extension);
138 enum image_format image_file_name_to_format(byte *file_name);
139
140 /* internals */
141
142 #ifdef CONFIG_LIBJPEG
143 int libjpeg_read_header(struct image_io *io);
144 int libjpeg_read_data(struct image_io *io);
145 int libjpeg_write(struct image_io *io);
146 #endif
147
148 #ifdef CONFIG_LIBPNG
149 int libpng_read_header(struct image_io *io);
150 int libpng_read_data(struct image_io *io);
151 int libpng_write(struct image_io *io);
152 #endif
153
154 #ifdef CONFIG_LIBUNGIF
155 int libungif_read_header(struct image_io *io);
156 int libungif_read_data(struct image_io *io);
157 #endif
158
159 #ifdef CONFIG_LIBMAGICK
160 int libmagick_read_header(struct image_io *io);
161 int libmagick_read_data(struct image_io *io);
162 int libmagick_write(struct image_io *io);
163 #endif
164
165 #endif