Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/src/engine/subwindow.c
blob: 19b78bf90def945ff36401048da4c423797f159f (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
#ifndef _plan9_
#include <config.h>
#ifdef NO_MALLOC_H
#include <stdlib.h>
#else
#include <malloc.h>
#endif
#include <stdio.h>		/*for NULL */
#include <string.h>		/*for memcpy */
#else
#include <u.h>
#include <libc.h>
#include <stdio.h>
#endif
#include <filter.h>
#include <zoom.h>
struct subdata {
    struct filter *second;
    struct image *image;
    pixel_t **currlines;
    int recal;
    int forpversion, forversion;
    number_t pre, pim;
};
void subwindow_setsecond(struct filter *f, struct filter *f1)
{
    struct subdata *s = (struct subdata *) f->data;
    s->second = f1;
}

static void myflip(struct image *image)
{
    struct subdata *s = (struct subdata *) image->data;
    flipgeneric(image);
    s->image->flip(s->image);
    s->currlines = s->image->currlines;
}

static int requirement(struct filter *f, struct requirements *r)
{
    r->nimages = 2;
    r->flags |= IMAGEDATA;
    return (f->next->action->requirement(f->next, r));
}

extern CONST struct filteraction threed_filter;

static int initialize(struct filter *f, struct initdata *i)
{
    struct subdata *s = (struct subdata *) f->data;
    int x;
    int val = 0;
    pixel_t **lines1, **lines2 = NULL;
    double size;
    int width, height;
    int threed = 0;
    struct filter *f1 = f;

    inhermisc(f, i);
    if (datalost(f, i))
	s->recal = 1;
    while (f1) {
	if (f1->action == &threed_filter)
	    threed = 1;
	f1 = f1->next;
    }
    f->imageversion = i->image->version;
    if (f->childimage != NULL)
	destroy_image(f->childimage);
    s->image = f->image = i->image;
    s->image->flags |= PROTECTBUFFERS;
    s->currlines = f->image->currlines;
    s->forpversion = f->image->palette->version;
    s->forversion = f->fractalc->version;
    if (f->image->width * f->image->pixelwidth <
	f->image->height * f->image->pixelheight)
	size = f->image->width * f->image->pixelwidth / 2;
    else
	size = f->image->height * f->image->pixelheight / 2;
    width = (int) (size / f->image->pixelwidth);
    height = (int) (size / f->image->pixelheight);
    /*fractalc_resize_to(f->fractalc,size,size); */
    lines1 = (pixel_t **) malloc(sizeof(*lines1) * height);
    if (f->image->nimages == 2)
	lines2 = (pixel_t **) malloc(sizeof(*lines2) * height);
    if (lines1 == NULL)
	return 0;
    if (f->image->nimages == 2 && lines2 == NULL) {
	free(lines1);
	return 0;
    }
    for (x = 0; x < height; x++) {
	lines1[x] =
	    i->image->currlines[x + (threed ? f->image->height / 3 : 0)];
	if (f->image->nimages == 2)
	    lines2[x] =
		i->image->oldlines[x +
				   (threed ? f->image->height / 3 : 0)];
    }
    if (f->image->nimages == 2)
	for (x = 0; x < f->image->height; x++) {
	    memcpy(f->image->oldlines[x], f->image->currlines[x],
		   f->image->width * f->image->bytesperpixel);
	}
    f->childimage = i->image =
	create_image_lines(width, height, f->image->nimages, lines1,
			   lines2, i->image->palette, myflip, FREELINES,
			   f->image->pixelwidth, f->image->pixelheight);
    if (i->image == NULL) {
	free(lines1);
	free(lines2);
	return 0;
    }
    f->childimage->data = s;
    x = f->previous->action->initialize(f->previous, i);
    if (!x)
	return 0;
    if (s->second != NULL) {
	i->image = f->image;
	val = s->second->action->initialize(s->second, i);
	if (!val)
	    return 0;
    }
    return (x | val);

}

static struct filter *getinstance(CONST struct filteraction *a)
{
    struct filter *f = createfilter(a);
    struct subdata *s = (struct subdata *) calloc(1, sizeof(*s));
    f->name = "Subwindow";
    f->data = s;
    s->second = NULL;
    return (f);
}

static void destroyinstance(struct filter *f)
{
    free(f->data);
    if (f->childimage != NULL)
	destroy_image(f->childimage);
    free(f);
}

static int doit(struct filter *f, int flags, int time)
{
    int val = 0, m, vold;
    vinfo vs;
    vrect rs;
    float wwidth, wheight;
    struct subdata *s = (struct subdata *) f->data;
    static int v;
    if (s->second != NULL
	&& (s->recal || s->forpversion != f->image->palette->version
	    || s->forversion != f->fractalc->version)) {
	int x;
	if (s->recal)
	    f->fractalc->version++;
	s->forpversion = f->image->palette->version;
	s->forversion = f->fractalc->version;
	s->recal = 1;
	val = (s->second->action->doit(s->second, flags, time));
	if (val & ANIMATION)
	    return val;
	s->recal = 0;
	if (f->image->nimages == 2)
	    for (x = 0; x < f->image->height; x++) {
		memcpy(f->image->oldlines[x], f->image->currlines[x],
		       f->image->width * f->image->bytesperpixel);
	    }
    }
    if (s->currlines != f->image->currlines && f->childimage->nimages == 2)
	flipgeneric(f->childimage), s->currlines = f->image->currlines;
    /*FIXME: ugly hack for new julia mode */
    v++;
    wwidth = f->fractalc->windowwidth;
    wheight = f->fractalc->windowheight;
    f->fractalc->windowwidth =
	f->previous->image->width * f->previous->image->pixelwidth;
    f->fractalc->windowheight =
	f->previous->image->height * f->previous->image->pixelheight;
    vs = f->fractalc->s;
    rs = f->fractalc->rs;
    f->fractalc->s = f->fractalc->currentformula->v;
    if (f->fractalc->currentformula->calculate_julia) {
	f->fractalc->s.cr = f->fractalc->s.ci = 0;
	f->fractalc->s.rr = f->fractalc->s.ri = 4;	/*FIXME should be set to real formula's bailout */
    }
    update_view(f->fractalc);
    m = f->fractalc->mandelbrot;
    vold = f->fractalc->version;
    if (s->pre != f->fractalc->pre || s->pim != f->fractalc->pim) {
	f->fractalc->version = v;
	s->pre = f->fractalc->pre;
	s->pim = f->fractalc->pim;
    }
    f->fractalc->mandelbrot = 0;
    val = f->previous->action->doit(f->previous, flags, time) | val;
    f->fractalc->mandelbrot = m;
    f->fractalc->version = vold;
    f->fractalc->s = vs;
    f->fractalc->rs = rs;
    f->fractalc->windowwidth = wwidth;
    f->fractalc->windowheight = wheight;
    return val;
}

static void myremove(struct filter *f)
{
    /*fractalc_resize_to(f->fractalc,f->queue->last->image->width*f->queue->last->image->pixelwidth,f->queue->last->image->height*f->queue->last->image->pixelheight); */
}

static void convertdown(struct filter *f, int *x, int *y)
{
    struct subdata *s = (struct subdata *) f->data;
    if (s->second != NULL)
	s->second->action->convertdown(s->second, x, y);
    if (f->previous != NULL)
	f->previous->action->convertdown(f->previous, x, y);
}


CONST struct filteraction subwindow_filter = {
    "Subwindow",
    "Subwindow",
    0,
    getinstance,
    destroyinstance,
    doit,
    requirement,
    initialize,
    convertupgeneric,
    convertdown,
    myremove
};