Fixed binary search: no more infinite loops when vendor is unknown.
[tangerine.git] / workbench / hidds / graphics / chunkybm.c
blob703bed07bb3f158ea92f06f670733724f89744a6
1 /*
2 Copyright © 1995-2001, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc: Graphics chunky bitmap class implementation.
6 Lang: english
7 */
9 /****************************************************************************************/
11 #include <proto/exec.h>
12 #include <proto/utility.h>
13 #include <proto/oop.h>
15 #include <exec/memory.h>
16 #include <utility/tagitem.h>
17 #include <oop/oop.h>
19 #include <hidd/graphics.h>
21 #include "graphics_intern.h"
23 #include <string.h>
25 #define DEBUG 0
26 #include <aros/debug.h>
28 /****************************************************************************************/
30 OOP_Object *CBM__Root__New(OOP_Class *cl, OOP_Object *o, struct pRoot_New *msg)
32 struct chunkybm_data *data;
34 IPTR width, height;
36 #if 0
37 UBYTE alignoffset = 15;
38 UBYTE aligndiv = 2;
39 #endif
41 BOOL ok = TRUE;
42 OOP_Object *pf;
43 APTR p_pf = &pf;
44 IPTR bytesperpixel;
46 o = (OOP_Object *)OOP_DoSuperMethod(cl, o, (OOP_Msg)msg);
47 if (NULL == o)
48 return NULL;
50 /* Initialize the instance data to 0 */
51 data = OOP_INST_DATA(cl, o);
52 memset(data, 0, sizeof (*data));
54 OOP_GetAttr(o, aHidd_BitMap_PixFmt, (IPTR *)p_pf);
55 OOP_GetAttr(o, aHidd_BitMap_Width, &width);
56 OOP_GetAttr(o, aHidd_BitMap_Height, &height);
57 /* Get some dimensions of the bitmap */
58 OOP_GetAttr(pf, aHidd_PixFmt_BytesPerPixel, &bytesperpixel);
60 width = (width + 15) & ~15;
62 data->bytesperpixel = bytesperpixel;
63 data->bytesperrow = data->bytesperpixel * width;
65 data->buffer = AllocVec(height * data->bytesperrow, MEMF_ANY|MEMF_CLEAR);
66 if (NULL == data->buffer)
67 ok = FALSE;
69 /* free all on error */
71 if(ok == FALSE)
73 OOP_MethodID dispose_mid = OOP_GetMethodID(IID_Root, moRoot_Dispose);
74 if(o) OOP_CoerceMethod(cl, o, (OOP_Msg)&dispose_mid);
75 o = NULL;
78 return o;
82 /****************************************************************************************/
84 void CBM__Root__Dispose(OOP_Class *cl, OOP_Object *o, OOP_Msg msg)
86 struct chunkybm_data *data;
88 data = OOP_INST_DATA(cl, o);
90 if (NULL != data->buffer)
91 FreeVec(data->buffer);
93 OOP_DoSuperMethod(cl, o, msg);
95 return;
98 /****************************************************************************************/
100 VOID CBM__Hidd_BitMap__PutPixel(OOP_Class *cl, OOP_Object *o,
101 struct pHidd_BitMap_PutPixel *msg)
103 UBYTE *dest;
105 struct chunkybm_data *data;
107 data = OOP_INST_DATA(cl, o);
109 /* bitmap in chunky-mode */
110 dest = data->buffer + msg->x * data->bytesperpixel + msg->y * data->bytesperrow;
112 switch(data->bytesperpixel)
114 case 1:
115 *((UBYTE *) dest) = (UBYTE) msg->pixel;
116 break;
118 case 2:
119 *((UWORD *) dest) = (UWORD) msg->pixel;
120 break;
122 case 3:
123 #if AROS_BIG_ENDIAN
124 dest[0] = (UBYTE)(msg->pixel >> 16) & 0x000000FF;
125 dest[1] = (UBYTE)(msg->pixel >> 8) & 0x000000FF;
126 dest[2] = (UBYTE)msg->pixel & 0x000000FF;
127 #else
128 dest[0] = (UBYTE)msg->pixel & 0x000000FF;
129 dest[1] = (UBYTE)(msg->pixel >> 8) & 0x000000FF;
130 dest[2] = (UBYTE)(msg->pixel >> 16) & 0x000000FF;
131 #endif
132 break;
134 /* if (1 == ( ((IPTR)dest) & 1) )
136 *((UBYTE *) dest++) = (UBYTE) msg->pixel >> 16;
137 *((UWORD *) dest ) = (UWORD) msg->pixel;
139 else
141 *((UWORD *) dest++) = (UWORD) msg->pixel >> 8;
142 *((UBYTE *) dest ) = (UBYTE) msg->pixel;
144 break;
146 case 4:
147 *((ULONG *) dest) = (ULONG) msg->pixel;
148 break;
153 /****************************************************************************************/
155 ULONG CBM__Hidd_BitMap__GetPixel(OOP_Class *cl, OOP_Object *o,
156 struct pHidd_BitMap_GetPixel *msg)
158 HIDDT_Pixel retval = 0;
159 UBYTE *src;
160 struct chunkybm_data *data;
162 data = OOP_INST_DATA(cl, o);
164 src = data->buffer + msg->x * data->bytesperpixel + msg->y * data->bytesperrow;
166 switch(data->bytesperpixel)
168 case 1:
169 retval = (HIDDT_Pixel) *((UBYTE *) src);
170 break;
172 case 2:
173 retval = (HIDDT_Pixel) *((UWORD *) src);
174 break;
176 case 3:
177 #if AROS_BIG_ENDIAN
178 retval = (HIDDT_Pixel) (src[0] << 16) + (src[1] << 8) + src[2];
179 #else
180 retval = (HIDDT_Pixel) (src[2] << 16) + (src[1] << 8) + src[0];
181 #endif
182 break;
184 //(*((UBYTE *) src++) << 16) | *((UWORD *) src));
185 //break;
187 case 4:
188 retval = ((ULONG) *((ULONG *) src));
189 break;
192 return retval;
195 /****************************************************************************************/
197 VOID CBM__Hidd_BitMap__FillRect(OOP_Class *cl, OOP_Object *o, struct pHidd_BitMap_DrawRect *msg)
199 struct chunkybm_data *data =OOP_INST_DATA(cl, o);
200 HIDDT_Pixel fg = GC_FG(msg->gc);
201 HIDDT_DrawMode mode = GC_DRMD(msg->gc);
202 ULONG mod;
204 mod = data->bytesperrow;
206 switch(mode)
208 case vHidd_GC_DrawMode_Copy:
209 switch(data->bytesperpixel)
211 case 1:
212 HIDD_BM_FillMemRect8(o,
213 data->buffer,
214 msg->minX,
215 msg->minY,
216 msg->maxX,
217 msg->maxY,
218 mod,
219 fg);
220 break;
222 case 2:
223 HIDD_BM_FillMemRect16(o,
224 data->buffer,
225 msg->minX,
226 msg->minY,
227 msg->maxX,
228 msg->maxY,
229 mod,
230 fg);
231 break;
233 case 3:
234 HIDD_BM_FillMemRect24(o,
235 data->buffer,
236 msg->minX,
237 msg->minY,
238 msg->maxX,
239 msg->maxY,
240 mod,
241 fg);
242 break;
244 case 4:
245 HIDD_BM_FillMemRect32(o,
246 data->buffer,
247 msg->minX,
248 msg->minY,
249 msg->maxX,
250 msg->maxY,
251 mod,
252 fg);
253 break;
256 break;
258 case vHidd_GC_DrawMode_Invert:
259 HIDD_BM_InvertMemRect(o,
260 data->buffer,
261 msg->minX * data->bytesperpixel,
262 msg->minY,
263 msg->maxX * data->bytesperpixel + data->bytesperpixel - 1,
264 msg->maxY,
265 mod);
266 break;
268 default:
269 OOP_DoSuperMethod(cl, o, (OOP_Msg)msg);
270 break;
272 } /* switch(mode) */
276 /****************************************************************************************/
278 VOID CBM__Hidd_BitMap__PutImage(OOP_Class *cl, OOP_Object *o, struct pHidd_BitMap_PutImage *msg)
280 struct chunkybm_data *data = OOP_INST_DATA(cl, o);
282 switch(msg->pixFmt)
284 case vHidd_StdPixFmt_Native:
285 switch(data->bytesperpixel)
287 case 1:
288 HIDD_BM_CopyMemBox8(o,
289 msg->pixels,
292 data->buffer,
293 msg->x,
294 msg->y,
295 msg->width,
296 msg->height,
297 msg->modulo,
298 data->bytesperrow);
299 break;
301 case 2:
302 HIDD_BM_CopyMemBox16(o,
303 msg->pixels,
306 data->buffer,
307 msg->x,
308 msg->y,
309 msg->width,
310 msg->height,
311 msg->modulo,
312 data->bytesperrow);
313 break;
315 case 3:
316 HIDD_BM_CopyMemBox24(o,
317 msg->pixels,
320 data->buffer,
321 msg->x,
322 msg->y,
323 msg->width,
324 msg->height,
325 msg->modulo,
326 data->bytesperrow);
327 break;
329 case 4:
330 HIDD_BM_CopyMemBox32(o,
331 msg->pixels,
334 data->buffer,
335 msg->x,
336 msg->y,
337 msg->width,
338 msg->height,
339 msg->modulo,
340 data->bytesperrow);
341 break;
343 } /* switch(data->bytesperpixel) */
344 break;
346 case vHidd_StdPixFmt_Native32:
347 switch(data->bytesperpixel)
349 case 1:
350 HIDD_BM_PutMem32Image8(o,
351 msg->pixels,
352 data->buffer,
353 msg->x,
354 msg->y,
355 msg->width,
356 msg->height,
357 msg->modulo,
358 data->bytesperrow);
359 break;
361 case 2:
362 HIDD_BM_PutMem32Image16(o,
363 msg->pixels,
364 data->buffer,
365 msg->x,
366 msg->y,
367 msg->width,
368 msg->height,
369 msg->modulo,
370 data->bytesperrow);
371 break;
373 case 3:
374 HIDD_BM_PutMem32Image24(o,
375 msg->pixels,
376 data->buffer,
377 msg->x,
378 msg->y,
379 msg->width,
380 msg->height,
381 msg->modulo,
382 data->bytesperrow);
383 break;
385 case 4:
386 HIDD_BM_CopyMemBox32(o,
387 msg->pixels,
390 data->buffer,
391 msg->x,
392 msg->y,
393 msg->width,
394 msg->height,
395 msg->modulo,
396 data->bytesperrow);
397 break;
399 } /* switch(data->bytesperpixel) */
400 break;
402 default:
403 OOP_DoSuperMethod(cl, o, (OOP_Msg)msg);
404 break;
406 } /* switch(msg->pixFmt) */
410 /****************************************************************************************/
412 VOID CBM__Hidd_BitMap__GetImage(OOP_Class *cl, OOP_Object *o, struct pHidd_BitMap_GetImage *msg)
414 struct chunkybm_data *data = OOP_INST_DATA(cl, o);
416 switch(msg->pixFmt)
418 case vHidd_StdPixFmt_Native:
419 switch(data->bytesperpixel)
421 case 1:
422 HIDD_BM_CopyMemBox8(o,
423 data->buffer,
424 msg->x,
425 msg->y,
426 msg->pixels,
429 msg->width,
430 msg->height,
431 data->bytesperrow,
432 msg->modulo);
433 break;
435 case 2:
436 HIDD_BM_CopyMemBox16(o,
437 data->buffer,
438 msg->x,
439 msg->y,
440 msg->pixels,
443 msg->width,
444 msg->height,
445 data->bytesperrow,
446 msg->modulo);
447 break;
449 case 3:
450 HIDD_BM_CopyMemBox24(o,
451 data->buffer,
452 msg->x,
453 msg->y,
454 msg->pixels,
457 msg->width,
458 msg->height,
459 data->bytesperrow,
460 msg->modulo);
461 break;
463 case 4:
464 HIDD_BM_CopyMemBox32(o,
465 data->buffer,
466 msg->x,
467 msg->y,
468 msg->pixels,
471 msg->width,
472 msg->height,
473 data->bytesperrow,
474 msg->modulo);
475 break;
477 } /* switch(data->bytesperpix) */
478 break;
480 case vHidd_StdPixFmt_Native32:
481 switch(data->bytesperpixel)
483 case 1:
484 HIDD_BM_GetMem32Image8(o,
485 data->buffer,
486 msg->x,
487 msg->y,
488 msg->pixels,
489 msg->width,
490 msg->height,
491 data->bytesperrow,
492 msg->modulo);
493 break;
495 case 2:
496 HIDD_BM_GetMem32Image16(o,
497 data->buffer,
498 msg->x,
499 msg->y,
500 msg->pixels,
501 msg->width,
502 msg->height,
503 data->bytesperrow,
504 msg->modulo);
505 break;
507 case 3:
508 HIDD_BM_GetMem32Image24(o,
509 data->buffer,
510 msg->x,
511 msg->y,
512 msg->pixels,
513 msg->width,
514 msg->height,
515 data->bytesperrow,
516 msg->modulo);
517 break;
519 case 4:
520 HIDD_BM_CopyMemBox32(o,
521 data->buffer,
522 msg->x,
523 msg->y,
524 msg->pixels,
527 msg->width,
528 msg->height,
529 data->bytesperrow,
530 msg->modulo);
531 break;
533 } /* switch(data->bytesperpixel) */
534 break;
536 default:
537 OOP_DoSuperMethod(cl, o, (OOP_Msg)msg);
538 break;
540 } /* switch(msg->pixFmt) */
544 /****************************************************************************************/
546 VOID CBM__Hidd_BitMap__PutImageLUT(OOP_Class *cl, OOP_Object *o, struct pHidd_BitMap_PutImageLUT *msg)
548 struct chunkybm_data *data = OOP_INST_DATA(cl, o);
550 switch(data->bytesperpixel)
552 case 2:
553 HIDD_BM_CopyLUTMemBox16(o,
554 msg->pixels,
557 data->buffer,
558 msg->x,
559 msg->y,
560 msg->width,
561 msg->height,
562 msg->modulo,
563 data->bytesperrow,
564 msg->pixlut);
565 break;
567 case 3:
568 HIDD_BM_CopyLUTMemBox24(o,
569 msg->pixels,
572 data->buffer,
573 msg->x,
574 msg->y,
575 msg->width,
576 msg->height,
577 msg->modulo,
578 data->bytesperrow,
579 msg->pixlut);
580 break;
582 case 4:
583 HIDD_BM_CopyLUTMemBox32(o,
584 msg->pixels,
587 data->buffer,
588 msg->x,
589 msg->y,
590 msg->width,
591 msg->height,
592 msg->modulo,
593 data->bytesperrow,
594 msg->pixlut);
595 break;
597 default:
598 OOP_DoSuperMethod(cl, o, (OOP_Msg)msg);
600 } /* switch(data->bytesperpixel) */
604 /****************************************************************************************/
606 VOID CBM__Hidd_BitMap__BlitColorExpansion(OOP_Class *cl, OOP_Object *o, struct pHidd_BitMap_BlitColorExpansion *msg)
608 struct chunkybm_data *data = OOP_INST_DATA(cl, o);
609 HIDDT_Pixel fg, bg, pix;
610 ULONG cemd;
611 LONG x, y;
612 ULONG mod, bpp;
613 UBYTE *mem;
614 BOOL opaque;
616 fg = GC_FG(msg->gc);
617 bg = GC_BG(msg->gc);
618 cemd = GC_COLEXP(msg->gc);
620 bpp = data->bytesperpixel;
622 mem = data->buffer + msg->destY * data->bytesperrow + msg->destX * bpp;
623 mod = data->bytesperrow - msg->width * bpp;
625 opaque = (cemd & vHidd_GC_ColExp_Opaque) ? TRUE : FALSE;
627 for (y = 0; y < msg->height; y ++)
629 for (x = 0; x < msg->width; x ++)
631 ULONG is_set;
633 is_set = HIDD_BM_GetPixel(msg->srcBitMap, x + msg->srcX, y + msg->srcY);
634 if (is_set)
636 pix = fg;
638 else if (opaque)
640 pix = bg;
642 else
644 mem += bpp;
645 continue;
648 switch(bpp)
650 case 1:
651 *mem++ = pix;
652 break;
654 case 2:
655 *((UWORD *)mem) = pix;
656 mem += 2;
657 break;
659 case 3:
660 #if AROS_BIG_ENDIAN
661 mem[0] = pix >> 16;
662 mem[1] = pix >> 8;
663 mem[2] = pix;
664 #else
665 mem[0] = pix;
666 mem[1] = pix >> 8;
667 mem[2] = pix >> 16;
668 #endif
669 mem += 3;
670 break;
672 case 4:
673 *((ULONG *)mem) = pix;
674 mem += 4;
675 break;
679 } /* for (each x) */
681 mem += mod;
683 } /* for (each y) */
687 /****************************************************************************************/
689 VOID CBM__Hidd_BitMap__PutTemplate(OOP_Class *cl, OOP_Object *o, struct pHidd_BitMap_PutTemplate *msg)
691 struct chunkybm_data *data = OOP_INST_DATA(cl, o);
693 switch(data->bytesperpixel)
695 case 1:
696 HIDD_BM_PutMemTemplate8(o,
697 msg->gc,
698 msg->template,
699 msg->modulo,
700 msg->srcx,
701 data->buffer,
702 data->bytesperrow,
703 msg->x,
704 msg->y,
705 msg->width,
706 msg->height,
707 msg->inverttemplate);
708 break;
710 case 2:
711 HIDD_BM_PutMemTemplate16(o,
712 msg->gc,
713 msg->template,
714 msg->modulo,
715 msg->srcx,
716 data->buffer,
717 data->bytesperrow,
718 msg->x,
719 msg->y,
720 msg->width,
721 msg->height,
722 msg->inverttemplate);
723 break;
725 case 3:
726 HIDD_BM_PutMemTemplate24(o,
727 msg->gc,
728 msg->template,
729 msg->modulo,
730 msg->srcx,
731 data->buffer,
732 data->bytesperrow,
733 msg->x,
734 msg->y,
735 msg->width,
736 msg->height,
737 msg->inverttemplate);
738 break;
740 case 4:
741 HIDD_BM_PutMemTemplate32(o,
742 msg->gc,
743 msg->template,
744 msg->modulo,
745 msg->srcx,
746 data->buffer,
747 data->bytesperrow,
748 msg->x,
749 msg->y,
750 msg->width,
751 msg->height,
752 msg->inverttemplate);
753 break;
755 default:
756 OOP_DoSuperMethod(cl, o, (OOP_Msg)msg);
757 break;
759 } /* switch(data->bytesperpixel) */
763 /****************************************************************************************/
765 VOID CBM__Hidd_BitMap__PutPattern(OOP_Class *cl, OOP_Object *o, struct pHidd_BitMap_PutPattern *msg)
767 struct chunkybm_data *data = OOP_INST_DATA(cl, o);
769 switch(data->bytesperpixel)
771 case 1:
772 HIDD_BM_PutMemPattern8(o,
773 msg->gc,
774 msg->pattern,
775 msg->patternsrcx,
776 msg->patternsrcy,
777 msg->patternheight,
778 msg->patterndepth,
779 msg->patternlut,
780 msg->invertpattern,
781 msg->mask,
782 msg->maskmodulo,
783 msg->masksrcx,
784 data->buffer,
785 data->bytesperrow,
786 msg->x,
787 msg->y,
788 msg->width,
789 msg->height);
790 break;
792 case 2:
793 HIDD_BM_PutMemPattern16(o,
794 msg->gc,
795 msg->pattern,
796 msg->patternsrcx,
797 msg->patternsrcy,
798 msg->patternheight,
799 msg->patterndepth,
800 msg->patternlut,
801 msg->invertpattern,
802 msg->mask,
803 msg->maskmodulo,
804 msg->masksrcx,
805 data->buffer,
806 data->bytesperrow,
807 msg->x,
808 msg->y,
809 msg->width,
810 msg->height);
811 break;
813 case 3:
814 HIDD_BM_PutMemPattern24(o,
815 msg->gc,
816 msg->pattern,
817 msg->patternsrcx,
818 msg->patternsrcy,
819 msg->patternheight,
820 msg->patterndepth,
821 msg->patternlut,
822 msg->invertpattern,
823 msg->mask,
824 msg->maskmodulo,
825 msg->masksrcx,
826 data->buffer,
827 data->bytesperrow,
828 msg->x,
829 msg->y,
830 msg->width,
831 msg->height);
832 break;
834 case 4:
835 HIDD_BM_PutMemPattern32(o,
836 msg->gc,
837 msg->pattern,
838 msg->patternsrcx,
839 msg->patternsrcy,
840 msg->patternheight,
841 msg->patterndepth,
842 msg->patternlut,
843 msg->invertpattern,
844 msg->mask,
845 msg->maskmodulo,
846 msg->masksrcx,
847 data->buffer,
848 data->bytesperrow,
849 msg->x,
850 msg->y,
851 msg->width,
852 msg->height);
853 break;
855 default:
856 OOP_DoSuperMethod(cl, o, (OOP_Msg)msg);
857 break;
859 } /* switch(data->bytesperpixel) */
863 /****************************************************************************************/