1 #ifndef IMAGE_H
2 #define IMAGE_H
3 #include "config.h"
4 
5 typedef unsigned char pixel_t;
6 typedef unsigned char
7     rgb_t[4]; /*4 is better than 3 - makes multiplying easier */
8 struct truec {
9     int rshift, gshift, bshift; /*the shift ammounts */
10     int rprec, gprec, bprec;    /*precisity - 0=8bit, 1=7bit, -1=9bit etc... */
11     unsigned int rmask, gmask, bmask; /*masks */
12     unsigned int mask1, mask2,
13         allmask;     /*Mask1 and mask2 are distinc color masks
14                         allmask are mask for all colors */
15     int byteexact;   /*When every colors is at one byte */
16     int missingbyte; /*for 32bit truecolor and exact byte places one byte is
17                         unused... */
18     int alpha;
19 };
20 union paletteinfo {
21     struct truec truec;
22 };
23 struct palette {
24     int start;
25     int end;
26     int maxentries;
27     int version;
28     int type;
29     unsigned int *pixels;
30     int npreallocated;
31     rgb_t *rgb;
32     int flags;
33     int (*alloccolor)(struct palette *pal, int init, int r, int g, int b);
34     void (*setpalette)(struct palette *pal, int start, int end, rgb_t *rgb);
35     void (*allocfinished)(struct palette *pal);
36     void (*cyclecolors)(struct palette *pal, int direction);
37     int size;   /*number of allocated color entries */
38     void *data; /*userdata */
39     /*Preallocated palette cells */
40     int ncells;
41     unsigned int *index;
42     const rgb_t *prergb;
43     union paletteinfo info;
44 };
45 
46 struct image {
47     float pixelwidth, pixelheight;
48     pixel_t **oldlines;
49     pixel_t **currlines;
50     void (*flip)(struct image *img);
51     int width, height, nimages;
52     int bytesperpixel;
53     int currimage;
54     int flags;
55     int scanline;
56     int version;
57     struct palette *palette;
58     void *data; /*userdata */
59     void (*free)(struct image *img);
60 };
61 
62 #define interpol1(i1, i2, n, mask)                                             \
63     ((((i1) & (mask)) * (n) + ((i2) & (mask)) * (256 - (n))) & ((mask) << 8))
64 #define interpol(i1, i2, n, mr, mg, mb)                                        \
65     ((interpol1(i1, i2, n, mr) + interpol1(i1, i2, n, mg) +                    \
66       interpol1(i1, i2, n, mb)) >>                                             \
67      8)
68 #define intergray(i1, i2, n) (((i1)*n + (i2) * (256 - (n))) >> 8)
69 /*
70  * J.B. Langston 3/13/2008
71  *
72  * The Mac OS X driver requires a 32-bit rgb mask where the most significant
73  * byte is on (e.g., 0xffffff00). This exposed a bug in the interpol macro
74  * that resulted in distorted colors for the smooth coloring modes.
75  * If the interpol macro is applied to such a mask, it causes an overflow
76  * of the 32-bit int, and the left-most color byte is lost.
77  *
78  * I added shiftinterpol macro to handle such masks. It shifts everything 1
79  * byte to the right, performs the calculation, and then shifts everything
80  * back 1 byte to the left when it is done.
81  *
82  * I also created the safeinterpol macro which detects if the most
83  * significant byte in the mask is on, and uses the shiftinterpol macro if
84  * so, or the original interpol macro if not.
85  *
86  * I then modified the interpoltype macro to use the safeinterpol macro
87  * instead of the interpol macro directly.
88  *
89  * J.B. Langston 1/7/2020
90  *
91  * And another bug on Mac. Now macOS won't display the image properly unless
92  * the alpha channel is set to 0xFF, so we force it to 0xFF here using
93  * ~(palette).info.truec.allmask, which gives the bit not used by RGB.
94  */
95 #define shiftinterpol(i1, i2, n, mr, mg, mb)                                   \
96     (interpol((i1) >> 8, (i2) >> 8, n, (mr) >> 8, (mg) >> 8, (mb) >> 8) << 8)
97 #define safeinterpol(i1, i2, n, mr, mg, mb)                                    \
98     ((((mr) | (mg) | (mb)) & 0xff000000)                                       \
99          ? shiftinterpol(i1, i2, n, mr, mg, mb)                                \
100          : interpol(i1, i2, n, mr, mg, mb))
101 #define interpoltype(palette, i1, i2, n)                                       \
102     ((palette).type == GRAYSCALE || (palette).type == LARGEITER                \
103          ? intergray(i1, i2, n)                                                \
104          : safeinterpol(i1, i2, n, (palette).info.truec.rmask,                 \
105                         (palette).info.truec.gmask,                            \
106                         (palette).info.truec.bmask) |                          \
107                ~(palette).info.truec.allmask)
108 /*palette flags */
109 #define UNKNOWNENTRIES 1
110 #define DONOTCHANGE 2
111 #define FINISHLATER 4
112 #define UNFINISHED 8
113 /*image flags */
114 #define FREELINES 1
115 #define FREEDATA 2
116 #define AAIMAGE 4
117 #define PROTECTBUFFERS 8
118 /*palette types supported by most of engine*/
119 #define C256 1
120 #define GRAYSCALE 2
121 #define TRUECOLOR16 4
122 #define TRUECOLOR24 8
123 #define TRUECOLOR 16
124 /*special mage types used internally by XaoS */
125 #define LARGEITER 32
126 #define SMALLITER 64
127 
128 /*palette types handled by the dithering filter*/
129 #define LBITMAP 256
130 #define MBITMAP 512
131 #define LIBITMAP 1024
132 #define MIBITMAP 2048
133 #define FIXEDCOLOR 4096
134 
135 #define ALLMASK (C256 | TRUECOLOR16 | TRUECOLOR24 | LARGEITER | GRAYSCALE)
136 #define BITMAPS (LBITMAP | MBITMAP | LIBITMAP | MIBITMAP)
137 #define MASK1BPP (SMALLITER | C256 | GRAYSCALE)
138 #define MASK2BPP (TRUECOLOR16 | LARGEITER)
139 #define MASK3BPP (TRUECOLOR24)
140 #define MASK4BPP (TRUECOLOR)
141 
142 /*flags for requirements */
143 #define IMAGEDATA 1
144 #define TOUCHIMAGE 2
145 #define NEWIMAGE 4
146 /*flags for initdata */
147 #define DATALOST 1
148 /*flags for doit */
149 #define INTERRUPTIBLE 1
150 #define PALETTEONLY 2
151 /*return flags */
152 #define INEXACT 1
153 #define CHANGED 2
154 #define ANIMATION 4
155 #define INCOMPLETE (1 << 29)
156 /*flags for filters */
157 #define ALLOCEDIMAGE 1 /*used by inherimage mechanizm */
158 #define SHAREDDATA 2
159 
160 #define PALGORITHMS 3
161 #include "pixel_t.h"
162 #define imgetpixel(image, x, y)                                                \
163     ((image)->bytesperpixel == 1                                               \
164          ? (image)->currlines[y][x]                                            \
165          : ((image)->bytesperpixel == 4                                        \
166                 ? ((pixel32_t *)(image)->currlines[y])[x]                      \
167                 : (image)->bytesperpixel == 3                                  \
168                       ? (((pixel16_t *)(image)->currlines[y])[x] +             \
169                          ((image)->currlines[y][3 * (x) + 2] << 16))           \
170                       : (((pixel16_t *)(image)->currlines[y])[x])))
171 struct requirements {
172     int nimages;
173     int supportedmask;
174     int flags;
175 };
176 struct filter {
177     struct filter *next, *previous;
178     struct queue *queue;
179     const struct filteraction *action;
180     struct image *image, *childimage;
181     struct requirements req;
182     struct fractal_context *fractalc;
183     void *data;
184     const char *name;
185     int flags;
186     int imageversion; /*For detection whether image changed or not */
187     void (*wait_function)(struct filter *f);
188     /*stuff for wait_function */
189     int pos, max, incalculation, readyforinterrupt, interrupt;
190     const char *pass;
191 };
192 struct initdata {
193     void (*wait_function)(struct filter *f);
194     struct image *image;
195     struct fractal_context *fractalc;
196     int flags;
197 };
198 struct filteraction {
199     const char *name;
200     const char *shortname;
201     int flags;
202     struct filter *(*getinstance)(const struct filteraction *a);
203     void (*destroyinstance)(struct filter *f);
204     int (*doit)(struct filter *f, int flags, int time);
205     int (*requirement)(struct filter *f, struct requirements *r);
206     int (*initialize)(struct filter *f, struct initdata *i);
207     void (*convertup)(struct filter *f, int *x, int *y);
208     void (*convertdown)(struct filter *f, int *x, int *y);
209     void (*removefilter)(struct filter *f);
210 };
211 struct queue {
212     struct filter *first, *last;
213     int isinitialized;
214     struct filter *palettechg;
215     struct image *saveimage;
216 };
217 
218 #define datalost(f, i)                                                         \
219     (((i)->flags & DATALOST) ||                                                \
220      ((f)->imageversion && (f)->imageversion != (i)->image->version))
221 /*filter actions */
222 
223 extern const struct filteraction interlace_filter, stereogram_filter,
224     subwindow_filter, smalliter_filter, julia_filter, blur_filter, edge_filter,
225     edge2_filter, rotate_filter, starfield_filter, truecolor_filter,
226     fixedcolor_filter, bitmap_filter, emboss_filter, palette_filter,
227     antialias_filter, threed_filter;
228 
229 extern unsigned int col_diff[3][512];
230 struct filter *createfilter(const struct filteraction *fa);
231 struct queue *create_queue(struct filter *f);
232 void insertfilter(struct filter *f1, struct filter *f2);
233 void removefilter(struct filter *f);
234 void addfilter(struct filter *f1, struct filter *f2);
235 int initqueue(struct queue *q);
236 
237 /*Filter utility functions */
238 int reqimage(struct filter *f, struct requirements *req, int supportedmask,
239              int flags);
240 int inherimage(struct filter *f, struct initdata *data, int flags, int width,
241                int height, struct palette *palette, float pixelwidth,
242                float pixelheight);
243 void destroyinheredimage(struct filter *f);
244 void updateinheredimage(struct filter *f);
245 
246 void inhermisc(struct filter *f, const struct initdata *i);
247 
248 /*image actions */
249 
250 void flipgeneric(struct image *img);
251 struct image *create_image_qt(int width, int height, struct palette *palette,
252                               float pixelwidth, float pixelheight);
253 struct image *create_image_lines(int width, int height, int nimages,
254                                  pixel_t **lines1, pixel_t **lines2,
255                                  struct palette *palette,
256                                  void (*flip)(struct image *img), int flags,
257                                  float pixelwidth, float pixelheight);
258 struct image *create_image_cont(int width, int height, int scanlinesize,
259                                 int nimages, pixel_t *buf1, pixel_t *buf2,
260                                 struct palette *palette,
261                                 void (*flip)(struct image *img), int flags,
262                                 float pixelwidth, float pixelheight);
263 struct image *create_image_mem(int width, int height, int nimages,
264                                struct palette *palette, float pixelwidth,
265                                float pixelheight);
266 struct image *create_subimage(struct image *simg, int width, int height,
267                               int nimages, struct palette *palette,
268                               float pixelwidth, float pixelheight);
269 
270 void destroy_image(struct image *img);
271 void clear_image(struct image *img);
272 
273 /*palette */
274 
275 int bytesperpixel(int type);
276 void bestfit_init(void);
277 struct palette *createpalette(
278     int start, int end, int type, int flags, int maxentries,
279     int (*alloccolor)(struct palette *pal, int init, int r, int g, int b),
280     void (*setcolor)(struct palette *pal, int start, int end, rgb_t *rgb),
281     void (*allocfinished)(struct palette *pal),
282     void (*cyclecolors)(struct palette *pal, int direction),
283     union paletteinfo *info);
284 void destroypalette(struct palette *palette);
285 int mkdefaultpalette(struct palette *palette);
286 int mkstereogrampalette(struct palette *palette);
287 int mkstarfieldpalette(struct palette *palette);
288 int mkblurpalette(struct palette *palette);
289 int mkgraypalette(struct palette *palette);
290 int mkrgb(struct palette *palette);
291 int mkpalette(struct palette *palette, int seed, int algorithm);
292 int shiftpalette(struct palette *palette, int n);
293 void preallocpalette(struct palette *pal);
294 struct palette *clonepalette(struct palette *palette);
295 void restorepalette(struct palette *dest, struct palette *src);
296 void convertupgeneric(struct filter *f, int *x, int *y);
297 void convertdowngeneric(struct filter *f, int *x, int *y);
298 int fixedalloccolor(struct palette *palette, int init, int r, int g, int b);
299 void getPaletteColor(struct palette *palette, int seed, int algorithm, int shift, int newColors[101][3]);
300 void getDEFSEGMENTColor(unsigned char newColors[][3]);
301 int mkcustompalette(struct palette *c, unsigned char newColors[27][3]);
302 void rgbtohex (int r, int g, int b, char color[6]);
303 void hextorgb (char *hexcolor, rgb_t color);
304 
305 #define setfractalpalette(f, p)                                                \
306     if ((f)->fractalc->palette == (f)->image->palette)                         \
307     (f)->fractalc->palette = (p)
308 
309 #ifdef STRUECOLOR24
310 #define TRUECOLOR24CASE(x)                                                     \
311     case 3:                                                                    \
312         x;                                                                     \
313         break;
314 #else
315 #define TRUECOLOR24CASE(x)
316 #endif
317 
318 #ifdef STRUECOLOR16
319 #define SUPPORT16
320 #endif
321 #ifdef SUPPORT16
322 #define TRUECOLOR16CASE(x)                                                     \
323     case 2:                                                                    \
324         x;                                                                     \
325         break;
326 #else
327 #define TRUECOLOR16CASE(x)
328 #endif
329 
330 #define drivercall(i, x1, x2, x3, x4)                                          \
331     switch ((i).bytesperpixel) {                                               \
332         TRUECOLOR24CASE(x3);                                                   \
333         TRUECOLOR16CASE(x2);                                                   \
334         case 1:                                                                \
335             x1;                                                                \
336             break;                                                             \
337         case 4:                                                                \
338             x4;                                                                \
339     }
340 #ifdef SMBITMAPS
341 #define SBITMAPS
342 #else
343 #ifdef SLBITMAPS
344 #define SBITMAPS
345 #endif
346 #endif
347 
348 #ifdef SBITMAPS
349 #define SCONVERTORS
350 #else
351 #ifdef SFIXEDCOLOR
352 #define SCONVERTORS
353 #endif
354 #endif
355 
356 #include "formulas.h"
357 #endif
358