1 //******************************************************************************
5 //******************************************************************************
8 Copyright 1993-1999, Be Incorporated. All Rights Reserved.
9 This file may be used under the terms of the Be Sample Code License.
23 #ifndef _INTERFACE_DEFS_H
24 #include <InterfaceDefs.h>
27 /*------------------------------------------------------------*/
29 static TShowBit
*tsbb
;
30 static long niter
= 256;
31 static uchar palette
[256];
34 void TShowBit::MouseDown(BPoint where
)
36 if (!this->Window()->IsActive()) {
37 this->Window()->Activate(TRUE
);
38 this->Window()->UpdateIfNeeded();
44 if ((modifiers() & B_SHIFT_KEY
) == 0) {
45 change_selection(where
.x
, where
.y
);
46 if ((selection
.bottom
- selection
.top
) < 4)
54 TShowBit::MessageReceived(BMessage
* message
)
56 switch(message
->what
) {
57 case B_MOUSE_WHEEL_CHANGED
:
60 float change
= message
->FindFloat("be:wheel_delta_y");
62 GetMouse(&where
, 0, 0);
68 selection
.top
= (where
.y
+ selection
.top
) / 2;
69 selection
.bottom
= (where
.y
+ selection
.bottom
) / 2;
70 selection
.left
= (where
.x
+ selection
.left
) / 2;
71 selection
.right
= (where
.x
+ selection
.right
) / 2;
74 selection
.top
= - where
.y
+ selection
.top
* 2;
75 selection
.bottom
= - where
.y
+ selection
.bottom
* 2;
76 selection
.left
= - where
.x
+ selection
.left
* 2;
77 selection
.right
= - where
.x
+ selection
.right
* 2;
83 BView::MessageReceived(message
);
88 void TShowBit::redraw_mand()
94 if (modifiers() & B_SHIFT_KEY
) {
99 px0
= px
+ (scale
* (selection
.left
/ (1.0*size_x
)));
100 py0
= py
+ (scale
* (selection
.top
/ (1.0*size_x
)));
101 scale0
= scale
* ((selection
.bottom
-selection
.top
) / (1.0*size_x
));
103 px
= px0
; py
= py0
; scale
= scale0
;
105 selection
.Set(-1000, -1000, -1000, -1000);
106 mand(px
, py
, scale
, scale
);
110 /*------------------------------------------------------------*/
112 void TShowBit::set_iter(long it
)
117 selection
.Set(-1000, -1000, -1000, -1000);
118 mand(px
, py
, scale
, scale
);
122 /*------------------------------------------------------------*/
124 void TShowBit::set_palette(long code
)
126 rgb_color c
= {0, 0, 0, 255};
129 BScreen
screen( Window() );
132 for (i
= 0; i
< 256; i
++)
133 palette
[i
] = (i
>> 1) & 0x1f;
136 for (i
= 0; i
< 256; i
++) {
139 c
.blue
= 256-(i
- i
* 5);
140 palette
[i
] = screen
.IndexForColor(c
);
145 for (i
= 0; i
< 256; i
++) {
148 c
.blue
= 256-(i
* 3);
149 palette
[i
] = screen
.IndexForColor(c
);
154 for (i
= 0; i
< 256; i
++) {
158 palette
[i
] = screen
.IndexForColor(c
);
161 mand(px
, py
, scale
, scale
);
164 /*------------------------------------------------------------*/
166 TShowBit::TShowBit(BRect r
, uint32 resizeMask
, uint32 flags
) :
167 BView(r
, "", resizeMask
, flags
| B_WILL_DRAW
| B_PULSE_NEEDED
)
175 bitmap_r
.Set(0, 0, size_x
- 1, size_y
- 1);
176 selection
.Set(-1000, -1000, -1000, -1000);
179 the_bitmap
= new BBitmap(bitmap_r
, B_COLOR_8_BIT
);
180 bits
= (char *)the_bitmap
->Bits();
181 memset(bits
, 0x00, size_x
*size_y
);
188 /*------------------------------------------------------------*/
190 TShowBit::~TShowBit()
195 /*------------------------------------------------------------*/
197 void TShowBit::Draw(BRect update_rect
)
199 DrawBitmap(the_bitmap
, BPoint(0, 0));
203 /*------------------------------------------------------------*/
206 int iterate_double(double a
, double b
)
212 int i
= 0, iter
= niter
;
220 y
= (2.0 * x
* y
) + b
;
224 if ((xsq
+ ysq
) > 4.0)
230 /*------------------------------------------------------------*/
231 //extern "C" int iterate(float a, float b);
232 /*------------------------------------------------------------*/
234 int iterate_float(float a
, float b
)
250 y
= (2.0f
* x
* y
) + b
;
254 if ((xsq
+ ysq
) > 4.0f
)
260 /*------------------------------------------------------------*/
268 /*------------------------------------------------------------*/
270 int32
__calc1(void *arg
)
272 tsbb
->manda(vvx
, vvy
, ssx
, ssx
);
276 /*------------------------------------------------------------*/
278 int32
__calc2(void *arg
)
280 tsbb
->mandb(vvx
, vvy
, ssx
, ssx
);
284 /*------------------------------------------------------------*/
290 /*------------------------------------------------------------*/
292 void TShowBit::mand(double vx
, double vy
, double sx
, double sy
)
294 vvx
= vx
; vvy
= vy
; ssx
= sx
;
295 t1_done
= 0; t2_done
= 0;
297 precompute(vx
, vy
, sx
, sy
);
299 resume_thread(spawn_thread(__calc1
, "calc1", B_NORMAL_PRIORITY
, NULL
));
300 resume_thread(spawn_thread(__calc2
, "calc2", B_NORMAL_PRIORITY
, NULL
));
304 /*------------------------------------------------------------*/
306 void TShowBit::Pulse()
308 // PRINT(("pulsing (%d)\n", busy));
310 Draw(BRect(0,0,0,0));
311 if (t1_done
&& t2_done
) {
318 /*------------------------------------------------------------*/
320 void TShowBit::precompute(double vx
, double vy
, double sx
, double sy
)
330 for (y
= 0; y
< 32; y
++) {
333 if (scale
< 0.000025 || niter
!= 256) {
334 for (x
= 0; x
< 32; x
++) {
336 pc
[x
][y
] = iterate_double(cx
, cy
);
340 for (x
= 0; x
< 32; x
++) {
342 pc
[x
][y
] = iterate_float(cx
, cy
);
347 /*------------------------------------------------------------*/
349 void TShowBit::mandb(double vx
, double vy
, double sx
, double sy
)
355 uchar
*bits
= (uchar
*)the_bitmap
->Bits();
361 sx
= sx
/ (size_x
* 1.0);
362 sy
= sy
/ (size_y
* 1.0);
367 for (y
= 1; y
< size_y
; y
+=2) {
371 b0
= bits
+ (y
* size_x
);
372 for (bx
= 0; bx
< size_x
; bx
+= 12) {
379 if (v
== pc
[x12
+1][y12
] &&
380 v
== pc
[x12
][y12
+1] &&
381 v
== pc
[x12
-1][y12
] &&
382 v
== pc
[x12
][y12
-1] &&
383 v
== pc
[x12
-2][y12
]) {
384 for (x
= bx
; x
< (bx
+12); x
++) {
386 *b0
++ = palette
[v
& 0xff];
390 if (scale
< 0.000025 || niter
!= 256) {
391 for (x
= bx
; x
< (bx
+12); x
++) {
393 v
= iterate_double(cx
, cy
);
394 *b0
++ = palette
[v
& 0xff];
398 for (x
= bx
; x
< (bx
+12); x
++) {
400 v
= iterate_float(cx
, cy
);
401 *b0
++ = palette
[v
& 0xff];
410 /*------------------------------------------------------------*/
412 void TShowBit::manda(double vx
, double vy
, double sx
, double sy
)
418 uchar
*bits
= (uchar
*)the_bitmap
->Bits();
424 sx
= sx
/ (size_x
* 1.0);
425 sy
= sy
/ (size_y
* 1.0);
429 for (y
= 0; y
< size_y
; y
+=2) {
433 b0
= bits
+ (y
* size_x
);
434 for (bx
= 0; bx
< size_x
; bx
+= 12) {
441 if (v
== pc
[x12
+1][y12
] &&
442 v
== pc
[x12
][y12
+1] &&
443 v
== pc
[x12
-1][y12
] &&
444 v
== pc
[x12
][y12
-1] &&
445 v
== pc
[x12
-2][y12
]) {
446 for (x
= bx
; x
< (bx
+12); x
++) {
448 *b0
++ = palette
[v
& 0xff];
452 if (scale
< 0.000025 || niter
!= 256) {
453 for (x
= bx
; x
< (bx
+12); x
++) {
455 v
= iterate_double(cx
, cy
);
456 *b0
++ = palette
[v
& 0xff];
460 for (x
= bx
; x
< (bx
+12); x
++) {
462 v
= iterate_float(cx
, cy
);
463 *b0
++ = palette
[v
& 0xff];
473 /*------------------------------------------------------------*/
475 long TShowBit::limit_v(long v
)
477 if (v
> (size_y
- 1))
485 /*------------------------------------------------------------*/
487 long TShowBit::limit_h(long v
)
489 if (v
> (size_x
- 1))
497 /*------------------------------------------------------------*/
499 BRect
TShowBit::sort_rect(BRect
*aRect
)
505 if (tmp_rect
.bottom
< tmp_rect
.top
) {
506 tmp
= (long)tmp_rect
.top
;
507 tmp_rect
.top
= tmp_rect
.bottom
;
508 tmp_rect
.bottom
= tmp
;
511 if (tmp_rect
.left
> tmp_rect
.right
) {
512 tmp
= (long) tmp_rect
.right
;
513 tmp_rect
.right
= tmp_rect
.left
;
517 tmp_rect
.top
= limit_v(tmp_rect
.top
);
518 tmp_rect
.left
= limit_h(tmp_rect
.left
);
519 tmp_rect
.bottom
= limit_v(tmp_rect
.bottom
);
520 tmp_rect
.right
= limit_h(tmp_rect
.right
);
525 /*------------------------------------------------------------*/
527 void TShowBit::clip(long *h
, long *v
)
529 if (*h
> (size_x
- 1))
533 if (*v
> (size_y
- 1))
539 /*------------------------------------------------------------*/
541 char TShowBit::has_selection()
543 if (((selection
.bottom
- selection
.top
) + (selection
.right
- selection
.left
)) < 5)
550 void TShowBit::change_selection(long h
, long v
)
564 old_select
= selection
;
566 SetDrawingMode(B_OP_INVERT
);
570 GetMouse(&where
, &buttons
);
576 max
= std::max(std::abs(height
), std::abs(width
));
578 v0
= (v0
> v
) ? (v
+ max
) : (v
- max
);
579 h0
= (h0
> h
) ? (h
+ max
) : (h
- max
);
582 new_select
.right
= h0
;
583 new_select
.bottom
= v0
;
585 if ((old_select
.top
!= new_select
.top
)
586 || (old_select
.bottom
!= new_select
.bottom
)
587 || (old_select
.right
!= new_select
.right
)
588 || (old_select
.left
!= new_select
.left
)) {
590 tmp_rect
= sort_rect(&new_select
);
591 StrokeRect(tmp_rect
);
593 tmp_rect
= sort_rect(&old_select
);
594 StrokeRect(tmp_rect
);
596 old_select
= new_select
;
603 selection
= sort_rect(&new_select
);
604 if (!has_selection()) {
605 StrokeRect(selection
);
606 selection
.Set(-1000, -1000, -1000, -1000);
608 SetDrawingMode(B_OP_COPY
);