Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/src/include/filter.h
blob: be27bd569434cd2cad4b2267542afb61a9831e03 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#ifndef IMAGE_H
#define IMAGE_H
#include "fconfig.h"
#ifdef __cplusplus
extern "C" {
#endif

    typedef unsigned char pixel_t;
    typedef unsigned char rgb_t[4];	/*4 is better than 3 - makes multiplying easier */
    struct truec {
	int rshift, gshift, bshift;	/*the shift ammounts */
	int rprec, gprec, bprec;	/*precisity - 0=8bit, 1=7bit, -1=9bit etc... */
	unsigned int rmask, gmask, bmask;	/*masks */
	unsigned int mask1, mask2, allmask;	/*Mask1 and mask2 are distinc color masks
						   allmask are mask for all colors */
	int byteexact;		/*When every colors is at one byte */
	int missingbyte;	/*for 32bit truecolor and exact byte places one byte is
				   unused... */
    };
    union paletteinfo {
	struct truec truec;
    };
    struct palette {
	int start;
	int end;
	int maxentries;
	int version;
	int type;
	unsigned int *pixels;
	int npreallocated;
	rgb_t *rgb;
	int flags;
	int (*alloccolor) (struct palette * pal, int init, int r, int g,
			   int b);
	void (*setpalette) (struct palette * pal, int start, int end,
			    rgb_t * rgb);
	void (*allocfinished) (struct palette * pal);
	void (*cyclecolors) (struct palette * pal, int direction);
	int size;		/*number of allocated color entries */
	void *data;		/*userdata */
	/*Preallocated palette cells */
	int ncells;
	unsigned int *index;
	CONST rgb_t *prergb;
	union paletteinfo info;
    };
    struct image {
	float pixelwidth, pixelheight;
	pixel_t **oldlines;
	pixel_t **currlines;
	void (*flip) (struct image * img);
	int width, height, nimages;
	int bytesperpixel;
	int currimage;
	int flags;
	int scanline;
	int version;
	struct palette *palette;
	void *data;		/*userdata */
    };
#define interpol1(i1,i2,n,mask) ((((i1)&(mask))*(n)+((i2)&(mask))*(256-(n)))&((mask)<<8))
#define interpol(i1,i2,n,mr,mg,mb) ((interpol1(i1,i2,n,mr)+interpol1(i1,i2,n,mg)+interpol1(i1,i2,n,mb))>>8)
#define intergray(i1,i2,n) (((i1)*n+(i2)*(256-(n)))>>8)
    /* 
     * J.B. Langston 3/13/2008
     *
     * The Mac OS X driver requires a 32-bit rgb mask where the most significant
     * byte is on (e.g., 0xffffff00). This exposed a bug in the interpol macro
     * that resulted in distorted colors for the smooth coloring modes.
     * If the interpol macro is applied to such a mask, it causes an overflow
     * of the 32-bit int, and the left-most color byte is lost.
     *
     * I added shiftinterpol macro to handle such masks. It shifts everything 1 
     * byte to the right, performs the calculation, and then shifts everything 
     * back 1 byte to the left when it is done.
     *
     * I also created the safeinterpol macro which detects if the most
     * signficant byte in the mask is on, and uses the shiftinterpol macro if
     * so, or the orignal interpol macro if not.
     *
     * I then modified the interpoltype macro to use the safeinterpol macro
     * instead of the interpol macro directly.
     */
#define shiftinterpol(i1,i2,n,mr,mg,mb) (interpol((i1)>>8,(i2)>>8,n,(mr)>>8,(mg)>>8,(mb)>>8)<<8)
#define safeinterpol(i1,i2,n,mr,mg,mb) ((((mr)|(mg)|(mb))&0xff000000)?shiftinterpol(i1,i2,n,mr,mg,mb):interpol(i1,i2,n,mr,mg,mb))
#define interpoltype(palette,i1,i2,n) ((palette).type==GRAYSCALE || (palette).type == LARGEITER?intergray(i1,i2,n):safeinterpol(i1,i2,n,(palette).info.truec.rmask,(palette).info.truec.gmask,(palette).info.truec.bmask))
/*palette flags */
#define UNKNOWNENTRIES 1
#define DONOTCHANGE 2
#define FINISHLATER 4
#define UNFINISHED 8
/*image flags */
#define FREELINES 1
#define FREEDATA 2
#define AAIMAGE 4
#define PROTECTBUFFERS 8
/*palette types supported by most of engine*/
#define C256 1
#define GRAYSCALE 2
#define TRUECOLOR16 4
#define TRUECOLOR24 8
#define TRUECOLOR 16
/*special mage types used internaly by XaoS */
#define LARGEITER 32
#define SMALLITER 64

/*palette types handled by the dithering filter*/
#define LBITMAP 256
#define MBITMAP 512
#define LIBITMAP 1024
#define MIBITMAP 2048
#define FIXEDCOLOR 4096

#define ALLMASK (C256|TRUECOLOR16|TRUECOLOR24|LARGEITER|GRAYSCALE)
#define BITMAPS (LBITMAP|MBITMAP|LIBITMAP|MIBITMAP)
#define MASK1BPP (SMALLITER|C256|GRAYSCALE)
#define MASK2BPP (TRUECOLOR16|LARGEITER)
#define MASK3BPP (TRUECOLOR24)
#define MASK4BPP (TRUECOLOR)

/*flags for requirements */
#define IMAGEDATA 1
#define TOUCHIMAGE 2
#define NEWIMAGE 4
/*flags for initdata */
#define DATALOST 1
/*flags for doit */
#define INTERRUPTIBLE 1
#define PALETTEONLY 2
/*return flags */
#define INEXACT 1
#define CHANGED 2
#define ANIMATION 4
#define UNCOMPLETTE (1<<29)
/*flags for filters */
#define ALLOCEDIMAGE 1		/*used by inherimage mechanizm */
#define SHAREDDATA 2

#define PALGORITHMS 3
#ifdef _plan9_
#undef pixel32_t
#undef pixel8_t
#undef pixel16_t
#define pixel32_t unsigned int
#define pixel16_t unsigned short
#define pixel8_t unsigned char
#undef ppixel8_t
#undef ppixel16_t
#undef ppixel24_t
#undef ppixel32_t
#define ppixel8_t pixel8_t *
#define ppixel16_t pixel16_t *
#define ppixel24_t unsigned char *
#define ppixel32_t pixel32_t *
#else
#include <pixel_t.h>		/*avoid problems with plan9-it ignores #if
				   So code must be separated into another file */
#endif
#define imgetpixel(image,x,y) ((image)->bytesperpixel==1?(image)->currlines[y][x]:((image)->bytesperpixel==4?((pixel32_t*)(image)->currlines[y])[x]:(image)->bytesperpixel==3?(((pixel16_t *)(image)->currlines[y])[x]+((image)->currlines[y][3*(x)+2]<<16)):(((pixel16_t*)(image)->currlines[y])[x])))
    struct requirements {
	int nimages;
	int supportedmask;
	int flags;
    };
    struct filter {
	struct filter *next, *previous;
	struct queue *queue;
	CONST struct filteraction *action;
	struct image *image, *childimage;
	struct requirements req;
	struct fractal_context *fractalc;
	void *data;
	CONST char *name;
	int flags;
	int imageversion;	/*For detection whether image changed or not */
	void (*wait_function) (struct filter * f);
	/*stuff for wait_function */
	int pos, max, incalculation, readyforinterrupt, interrupt;
	CONST char *pass;
    };
    struct initdata {
	void (*wait_function) (struct filter * f);
	struct image *image;
	struct fractal_context *fractalc;
	int flags;
    };
    struct filteraction {
	CONST char *name;
	CONST char *shortname;
	int flags;
	struct filter *(*getinstance) (CONST struct filteraction * a);
	void (*destroyinstance) (struct filter * f);
	int (*doit) (struct filter * f, int flags, int time);
	int (*requirement) (struct filter * f, struct requirements * r);
	int (*initialize) (struct filter * f, struct initdata * i);
	void (*convertup) (struct filter * f, int *x, int *y);
	void (*convertdown) (struct filter * f, int *x, int *y);
	void (*removefilter) (struct filter * f);
    };
    struct queue {
	struct filter *first, *last;
	int isinitialized;
	struct filter *palettechg;
	struct image *saveimage;
    };


#define datalost(f,i) (((i)->flags&DATALOST)||((f)->imageversion&&(f)->imageversion!=(i)->image->version))
/*filter actions */

    extern unsigned int col_diff[3][512];
    struct filter *createfilter(CONST struct filteraction *fa);
    struct queue *create_queue(struct filter *f);
    void insertfilter(struct filter *f1, struct filter *f2);
    void removefilter(struct filter *f);
    void addfilter(struct filter *f1, struct filter *f2);
    int initqueue(struct queue *q);

/*Filter utility functions */
    int reqimage(struct filter *f, struct requirements *req,
		 int supportedmask, int flags);
    int inherimage(struct filter *f, struct initdata *data, int flags,
		   int width, int height, struct palette *palette,
		   float pixelwidth, float pixelheight);
    void destroyinheredimage(struct filter *f);
    void updateinheredimage(struct filter *f);

    void inhermisc(struct filter *f, CONST struct initdata *i);

/*image actions */

    void flipgeneric(struct image *img);
    struct image *create_image_lines(int width, int height,
				     int nimages, pixel_t ** lines1,
				     pixel_t ** lines2,
				     struct palette *palette,
				     void (*flip) (struct image * img),
				     int flags, float pixelwidth,
				     float pixelheight);
    struct image *create_image_cont(int width, int height,
				    int scanlinesize, int nimages,
				    pixel_t * buf1, pixel_t * buf2,
				    struct palette *palette,
				    void (*flip) (struct image * img),
				    int flags, float pixelwidth,
				    float pixelheight);
    struct image *create_image_mem(int width, int height, int nimages,
				   struct palette *palette,
				   float pixelwidth, float pixelheight);
    struct image *create_subimage(struct image *simg, int width,
				  int height, int nimages,
				  struct palette *palette,
				  float pixelwidth, float pixelheight);

    void destroy_image(struct image *img);
    void clear_image(struct image *img);

/*palette */

    int bytesperpixel(int type) CONSTF;
    void bestfit_init(void);
    struct palette *createpalette(int start, int end, int type, int flags,
				  int maxentries,
				  int (*alloccolor) (struct palette * pal,
						     int init, int r,
						     int g, int b),
				  void (*setcolor) (struct palette * pal,
						    int start, int end,
						    rgb_t * rgb),
				  void (*allocfinished) (struct palette *
							 pal),
				  void (*cyclecolors) (struct palette *
						       pal, int direction),
				  union paletteinfo *info);
    void destroypalette(struct palette *palette);
    int mkdefaultpalette(struct palette *palette);
    int mkstereogrampalette(struct palette *palette);
    int mkstarfieldpalette(struct palette *palette);
    int mkblurpalette(struct palette *palette);
    int mkgraypalette(struct palette *palette);
    int mkrgb(struct palette *palette);
    int mkpalette(struct palette *palette, int seed, int algorithm);
    int shiftpalette(struct palette *palette, int n);
    void preallocpalette(struct palette *pal);
    struct palette *clonepalette(struct palette *palette);
    void restorepalette(struct palette *dest, struct palette *src);
    void convertupgeneric(struct filter *f, int *x, int *y);
    void convertdowngeneric(struct filter *f, int *x, int *y);
    int fixedalloccolor(struct palette *palette, int init, int r, int g,
			int b) CONSTF;

#define setfractalpalette(f,p) if((f)->fractalc->palette==(f)->image->palette) (f)->fractalc->palette=(p)

#ifdef STRUECOLOR24
#define TRUECOLOR24CASE(x) case 3:x;break;
#else
#define TRUECOLOR24CASE(x)
#endif

#ifdef STRUECOLOR16
#define SUPPORT16
#endif
#ifdef SUPPORT16
#define TRUECOLOR16CASE(x) case 2:x;break;
#else
#define TRUECOLOR16CASE(x)
#endif

#define drivercall(i,x1,x2,x3,x4) switch((i).bytesperpixel) { \
  TRUECOLOR24CASE(x3); \
  TRUECOLOR16CASE(x2); \
  case 1:x1;break; \
  case 4:x4; \
}
#ifdef SMBITMAPS
#define SBITMAPS
#else
#ifdef SLBITMAPS
#define SBITMAPS
#endif
#endif

#ifdef SBITMAPS
#define SCONVERTORS
#else
#ifdef SFIXEDCOLOR
#define SCONVERTORS
#endif
#endif

#ifdef __cplusplus
}
#endif
#include "formulas.h"
#endif