1//******************************************************************************
2//
3//	File:		main.cpp
4//
5//	Description:	show pixmap main test program.
6//
7//	Written by:	Benoit Schillings
8//
9//	Change History:
10//
11//	7/31/92		bgs	new today
12//
13//******************************************************************************
14
15/*
16	Copyright 1992-1999, Be Incorporated.   All Rights Reserved.
17	This file may be used under the terms of the Be Sample Code License.
18*/
19
20#include <math.h>
21#include <OS.h>
22
23#include "tsb.h"
24
25/*------------------------------------------------------------*/
26
27TShowBit	*tsbb;
28
29/*------------------------------------------------------------*/
30
31uchar	palette[256];
32
33/*------------------------------------------------------------*/
34
35void	TShowBit::MouseDown(BPoint where)
36{
37	if ((Modifiers() & SHIFT_KEY) == 0) {
38		change_selection(where.x, where.y);
39	}
40	redraw_mand();
41}
42
43/*------------------------------------------------------------*/
44
45void	TShowBit::redraw_mand()
46{
47	float	px0;
48	float	py0;
49	float	scale0;
50
51	if (Modifiers() & SHIFT_KEY) {
52		px -= (scale / 2.0);
53		py -= (scale / 2.0);
54		scale *= 2.0;
55	}
56	else {
57		px0 = px + (scale * (selection.left / (1.0*size_x)));
58		py0 = py + (scale * (selection.top / (1.0*size_x)));
59		scale0 = scale * ((selection.bottom-selection.top) / (1.0*size_x));
60
61		px = px0; py = py0; scale = scale0;
62	}
63
64	selection.Set(-1000, -1000, -1000, -1000);
65	mand(px, py, scale, scale);
66}
67
68/*------------------------------------------------------------*/
69
70void	TShowBit::set_palette(long code)
71{
72	rgb_color	c;
73	long		i;
74
75	if (code == 0) {
76		for (i = 0; i < 256; i++)
77			palette[i] = (i >> 1) & 0x1f;
78	}
79	if (code == 1) {
80		for (i = 0; i < 256; i++) {
81			c.red = i;
82			c.green = 0;
83			c.blue = 256-i;
84			palette[i] = index_for_color(c);
85		}
86	}
87
88	if (code == 2) {
89		for (i = 0; i < 256; i++) {
90			c.red = i;
91			c.green = i/2;
92			c.blue = 256-i;
93			palette[i] = index_for_color(c);
94		}
95	}
96
97	if (code == 3) {
98		for (i = 0; i < 256; i++) {
99			c.red = 256-i;
100			c.green = i;
101			c.blue = 0;
102			palette[i] = index_for_color(c);
103		}
104	}
105}
106
107/*------------------------------------------------------------*/
108
109void	TShowBit::TShowBit(BRect r, long flags) :
110	BView(r, "", flags, WILL_DRAW)
111{
112	BRect	bitmap_r;
113	long	ref;
114	long	i;
115	char	*bits;
116
117	tsbb = this;
118	set_palette(0);
119	exit_now = 0;
120	bitmap_r.Set(0, 0, size_x - 1, size_y - 1);
121	selection.Set(-1000, -1000, -1000, -1000);
122
123	the_bitmap = new BBitmap(bitmap_r, COLOR_8_BIT);
124	bits = (char *)the_bitmap->Bits();
125	memset(bits, 0x00, size_x*size_y);
126	px = -1.5;
127	py = -1.5;
128	scale = 3.0;
129}
130
131/*------------------------------------------------------------*/
132
133TShowBit::~TShowBit()
134{
135	delete	the_bitmap;
136}
137
138/*------------------------------------------------------------*/
139
140void	TShowBit::Draw(BRect update_rect)
141{
142	DrawBitmap(the_bitmap, BPoint(0, 0));
143}
144
145
146/*------------------------------------------------------------*/
147
148void	TShowBit::demo()
149{
150}
151
152/*------------------------------------------------------------*/
153extern "C" int iterate(float a, float b);
154/*------------------------------------------------------------*/
155
156float	vvx;
157float	vvy;
158float	ssx;
159char	t1_done;
160char	t2_done;
161
162/*------------------------------------------------------------*/
163
164void	__calc1()
165{
166	tsbb->manda(vvx, vvy, ssx, ssx);
167}
168
169/*------------------------------------------------------------*/
170
171void	__calc2()
172{
173	tsbb->mandb(vvx, vvy, ssx, ssx);
174}
175
176/*------------------------------------------------------------*/
177
178uchar	tmp[256];
179uchar	pc[32][32];
180uchar	tmp1[256];
181
182/*------------------------------------------------------------*/
183
184void	TShowBit::mand(float vx, float vy, float sx, float sy)
185{
186	vvx = vx; vvy = vy; ssx = sx;
187	t1_done = 0; t2_done = 0;
188
189	precompute(vx, vy, sx, sy);
190
191	resume_thread(spawn_thread(__calc1, "calc1", 20, 0, 0));
192	resume_thread(spawn_thread(__calc2, "calc2", 20, 0, 0));
193	while(1) {
194		snooze(100000);
195		Draw(BRect(0,0,0,0));
196		if (t1_done && t2_done)
197			break;
198	}
199	Draw(BRect(0,0,0,0));
200}
201
202/*------------------------------------------------------------*/
203
204void	TShowBit::precompute(float vx, float vy, float sx, float sy)
205{
206	long	x, y;
207	long	i;
208	long	nx, ny;
209	float	cx, cy;
210	char	v;
211	char	*p;
212
213	sx = sx / (32.0);
214	sy = sy / (32.0);
215	cy = vy;
216
217	for (y = 0; y < 32; y++) {
218		cy += sy;
219		cx = vx;
220		for (x = 0; x < 32; x++) {
221			cx += sx;
222			pc[x][y] = iterate(cx, cy);
223		}
224	}
225}
226
227/*------------------------------------------------------------*/
228
229void	TShowBit::mandb(float vx, float vy, float sx, float sy)
230{
231	long	x, y;
232	long	bx;
233	long	i;
234	long	nx, ny;
235	float	cx, cy;
236	char	v;
237	char	*p;
238	uchar	*bits = (char *)the_bitmap->Bits();
239	uchar	*b0;
240	long	y12;
241	long	x12;
242
243	sx = sx / (size_x * 1.0);
244	sy = sy / (size_y * 1.0);
245	cy = vy;
246
247	cy += sy;
248	sy *= 2.0;
249	for (y = 1; y < size_y; y+=2) {
250		y12 = y / 12;
251		cy += sy;
252		cx = vx;
253		b0 = bits + (y * size_x);
254		for (bx = 0; bx < size_x; bx += 12) {
255			for (x = bx; x < (bx+12); x++) {
256				cx += sx;
257				v = iterate(cx, cy);
258				*b0++ = palette[v];
259			}
260		}
261	}
262	t2_done = 1;
263}
264
265/*------------------------------------------------------------*/
266
267
268void	TShowBit::manda(float vx, float vy, float sx, float sy)
269{
270	long	x, y;
271	long	i;
272	long	nx, ny;
273	long	bx;
274	float	cx, cy;
275	char	v;
276	char	*p;
277	uchar	*bits = (char *)the_bitmap->Bits();
278	uchar	*b0;
279	long	y12;
280	long	x12;
281
282	sx = sx / (size_x * 1.0);
283	sy = sy / (size_y * 1.0);
284	cy = vy;
285
286	sy *= 2.0;
287	for (y = 0; y < size_y; y+=2) {
288		y12 = y / 12;
289		cy += sy;
290		cx = vx;
291		b0 = bits + (y * size_x);
292		for (bx = 0; bx < size_x; bx += 12) {
293			for (x = bx; x < (bx+12); x++) {
294				cx += sx;
295				v = iterate(cx, cy);
296				*b0++ = palette[v];
297			}
298		}
299	}
300	t1_done = 1;
301}
302
303
304/*------------------------------------------------------------*/
305
306long	TShowBit::limit_v(long v)
307{
308	if (v > (size_y - 1))
309			v = (size_y - 1);
310
311	if (v < 0)
312			v = 0;
313	return(v);
314}
315
316/*------------------------------------------------------------*/
317
318long	TShowBit::limit_h(long v)
319{
320	if (v > (size_x - 1))
321			v = size_x - 1;
322
323	if (v < 0)
324			v = 0;
325	return(v);
326}
327
328/*------------------------------------------------------------*/
329
330BRect	TShowBit::sort_rect(BRect *aRect)
331{
332	BRect	tmp_rect;
333	long	tmp;
334
335	tmp_rect = *aRect;
336	if (tmp_rect.bottom < tmp_rect.top) {
337		tmp = tmp_rect.top;
338		tmp_rect.top = tmp_rect.bottom;
339		tmp_rect.bottom = tmp;
340	}
341
342	if (tmp_rect.left > tmp_rect.right) {
343		tmp = tmp_rect.right;
344		tmp_rect.right = tmp_rect.left;
345		tmp_rect.left = tmp;
346	}
347
348	tmp_rect.top = limit_v(tmp_rect.top);
349	tmp_rect.left = limit_h(tmp_rect.left);
350	tmp_rect.bottom = limit_v(tmp_rect.bottom);
351	tmp_rect.right = limit_h(tmp_rect.right);
352
353	return(tmp_rect);
354}
355
356/*------------------------------------------------------------*/
357
358void	TShowBit::clip(long *h, long *v)
359{
360	if (*h > (size_x - 1))
361			*h = (size_x - 1);
362	if (*h < 0)
363			*h = 0;
364	if (*v > (size_y - 1))
365			*v = size_y - 1;
366	if (*v < 0)
367			*v = 0;
368}
369
370/*------------------------------------------------------------*/
371
372char	TShowBit::has_selection()
373{
374	if (((selection.bottom - selection.top) + (selection.right - selection.left)) < 5)
375		return 0;
376	else
377		return 1;
378}
379
380/*------------------------------------------------------------*/
381
382void	TShowBit::change_selection(long h, long v)
383{
384	ulong	buttons;
385	long	h0;
386	long	v0;
387	BRect	new_select;
388	BRect	old_select;
389	BRect	tmp_rect;
390	long	max;
391	long	width, height;
392
393	clip(&h, &v);
394	new_select.top = v;
395	new_select.left = h;
396	old_select = selection;
397
398	SetDrawingMode(OP_INVERT);
399
400	do {
401		BPoint where;
402		GetMouse(&where, &buttons);
403		h0 = where.x;
404		v0 = where.y;
405		width = h0 - h;
406		height = v0 - v;
407		if (height > width)
408			max = height;
409		else
410			max = height;
411
412		h0 = h+max; v0 = v+max;
413
414		clip(&h0, &v0);
415		new_select.right = h0;
416		new_select.bottom = v0;
417
418		if ((old_select.top != new_select.top) ||
419		    (old_select.bottom != new_select.bottom) ||
420		    (old_select.right != new_select.right) ||
421		    (old_select.left != new_select.left)) {
422
423			tmp_rect = sort_rect(&new_select);
424			StrokeRect(tmp_rect);
425
426			tmp_rect = sort_rect(&old_select);
427			StrokeRect(tmp_rect);
428
429			old_select = new_select;
430			Flush();
431		}
432
433		sleept(20);
434	} while(buttons);
435
436	selection = sort_rect(&new_select);
437	if (!has_selection()) {
438		StrokeRect(selection);
439		selection.Set(-1000, -1000, -1000, -1000);
440	}
441	SetDrawingMode(OP_COPY);
442}
443