forked from ghaerr/microwindows
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdevdraw.c
More file actions
1864 lines (1703 loc) · 47.5 KB
/
Copy pathdevdraw.c
File metadata and controls
1864 lines (1703 loc) · 47.5 KB
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 1999,2000,2001,2003,2005,2007,2010 Greg Haerr <greg@censoft.com>
* Portions Copyright (c) 2002 by Koninklijke Philips Electronics N.V.
* Portions Copyright (c) 1991 David I. Bell
*
* Device-independent mid level drawing and color routines.
*
* These routines do the necessary range checking, clipping, and cursor
* overwriting checks, and then call the lower level device dependent
* routines to actually do the drawing. The lower level routines are
* only called when it is known that all the pixels to be drawn are
* within the device area and are visible.
*/
#include <string.h>
#include "swap.h"
#include "device.h"
#include "convblit.h"
extern int gr_mode; /* drawing mode */
extern MWPALENTRY gr_palette[256]; /* current palette*/
extern int gr_firstuserpalentry;/* first user-changable palette entry*/
extern int gr_nextpalentry; /* next available palette entry*/
/* These support drawing dashed lines */
extern uint32_t gr_dashmask; /* An actual bitmask of the dash values */
extern uint32_t gr_dashcount; /* The number of bits defined in the dashmask */
extern int gr_fillmode;
/*static*/ void drawpoint(PSD psd,MWCOORD x, MWCOORD y);
/*static*/ void drawrow(PSD psd,MWCOORD x1,MWCOORD x2,MWCOORD y);
/*static*/ void drawcol(PSD psd,MWCOORD x,MWCOORD y1,MWCOORD y2);
/**
* Set the drawing mode for future calls.
*
* @param mode New drawing mode.
* @return Old drawing mode.
*/
int
GdSetMode(int mode)
{
int oldmode = gr_mode;
gr_mode = mode;
return oldmode;
}
/**
* Set the fill mode for future calls.
*
* @param mode New fill mode.
* @return Old fill mode.
*/
int
GdSetFillMode(int mode)
{
int oldmode = gr_fillmode;
gr_fillmode = mode;
return oldmode;
}
/**
* Set whether or not the background is used for drawing pixmaps and text.
*
* @param flag Flag indicating whether or not to draw the background.
* @return Old value of flag.
*/
MWBOOL
GdSetUseBackground(MWBOOL flag)
{
MWBOOL oldusebg = gr_usebg;
gr_usebg = flag;
return oldusebg;
}
/*
* Set the foreground color for drawing from passed pixel value.
*
* @param psd Screen device.
* @param bg Background pixel value.
*/
MWPIXELVAL
GdSetForegroundPixelVal(PSD psd, MWPIXELVAL fg)
{
MWPIXELVAL oldfg = gr_foreground;
gr_foreground = fg;
return oldfg;
}
/*
* Set the background color for bitmap and text backgrounds
* from passed pixel value.
*
* @param psd Screen device.
* @param bg Background pixel value.
*/
MWPIXELVAL
GdSetBackgroundPixelVal(PSD psd, MWPIXELVAL bg)
{
MWPIXELVAL oldbg = gr_background;
gr_background = bg;
return oldbg;
}
/**
* Set the foreground color for drawing from passed RGB color value.
*
* @param psd Screen device.
* @param fg Foreground RGB color to use for drawing.
* @return Old foreground color.
*/
MWPIXELVAL
GdSetForegroundColor(PSD psd, MWCOLORVAL fg)
{
MWPIXELVAL oldfg = gr_foreground;
gr_foreground = GdFindColor(psd, fg);
gr_foreground_rgb = fg;
return oldfg;
}
/**
* Set the background color for bitmap and text backgrounds
* from passed RGB color value.
*
* @param psd Screen device.
* @param bg Background color to use for drawing.
* @return Old background color.
*/
MWPIXELVAL
GdSetBackgroundColor(PSD psd, MWCOLORVAL bg)
{
MWPIXELVAL oldbg = gr_background;
gr_background = GdFindColor(psd, bg);
gr_background_rgb = bg;
return oldbg;
}
/**
* Set the dash mode for future drawing
*/
void
GdSetDash(uint32_t *mask, int *count)
{
int oldm = gr_dashmask;
int oldc = gr_dashcount;
gr_dashmask = mask? *mask: 0;
gr_dashcount = count? *count: 0;
/* special case for solid line sets no dashcount for speed*/
if (gr_dashcount == 32 && gr_dashmask == 0xFFFFFFFFL)
gr_dashcount = 0;
if (mask) *mask = oldm;
if (count) *count = oldc;
}
/**
* Draw a point using the current clipping region and foreground color.
*
* @param psd Drawing surface.
* @param x X co-ordinate to draw point.
* @param y Y co-ordinate to draw point.
*/
void
GdPoint(PSD psd, MWCOORD x, MWCOORD y)
{
if (GdClipPoint(psd, x, y)) {
psd->DrawPixel(psd, x, y, gr_foreground);
GdFixCursor(psd);
}
}
/**
* Draw an arbitrary line using the current clipping region and foreground color
* If bDrawLastPoint is FALSE, draw up to but not including point x2, y2.
*
* This routine is the only routine that adjusts coordinates for supporting
* two different types of upper levels, those that draw the last point
* in a line, and those that draw up to the last point. All other local
* routines draw the last point. This gives this routine a bit more overhead,
* but keeps overall complexity down.
*
* @param psd Drawing surface.
* @param x1 Start X co-ordinate
* @param y1 Start Y co-ordinate
* @param x2 End X co-ordinate
* @param y2 End Y co-ordinate
* @param bDrawLastPoint TRUE to draw the point at (x2, y2). FALSE to omit it.
*/
void
GdLine(PSD psd, MWCOORD x1, MWCOORD y1, MWCOORD x2, MWCOORD y2,
MWBOOL bDrawLastPoint)
{
int xdelta; /* width of rectangle around line */
int ydelta; /* height of rectangle around line */
int xinc; /* increment for moving x coordinate */
int yinc; /* increment for moving y coordinate */
int rem; /* current remainder */
unsigned int bit = 0; /* used for dashed lines */
MWCOORD temp;
/* See if the line is horizontal or vertical. If so, then call
* special routines.
*/
if (y1 == y2) {
/*
* Adjust coordinates if not drawing last point. Tricky.
*/
if (!bDrawLastPoint) {
if (x1 > x2) {
temp = x1;
x1 = x2 + 1;
x2 = temp;
} else
--x2;
}
/* call faster line drawing routine */
drawrow(psd, x1, x2, y1);
GdFixCursor(psd);
return;
}
if (x1 == x2) {
/*
* Adjust coordinates if not drawing last point. Tricky.
*/
if (!bDrawLastPoint) {
if (y1 > y2) {
temp = y1;
y1 = y2 + 1;
y2 = temp;
} else
--y2;
}
/* call faster line drawing routine */
drawcol(psd, x1, y1, y2);
GdFixCursor(psd);
return;
}
/* See if the line is either totally visible or totally invisible. If
* so, then the line drawing is easy.
*/
switch (GdClipArea(psd, x1, y1, x2, y2)) {
case CLIP_VISIBLE:
/*
* For size considerations, there's no low-level bresenham
* line draw, so we've got to draw all non-vertical
* and non-horizontal lines with per-point
* clipping for the time being
psd->Line(psd, x1, y1, x2, y2, gr_foreground);
GdFixCursor(psd);
return;
*/
break;
case CLIP_INVISIBLE:
return;
}
/* The line may be partially obscured. Do the draw line algorithm
* checking each point against the clipping regions.
*/
xdelta = x2 - x1;
ydelta = y2 - y1;
if (xdelta < 0)
xdelta = -xdelta;
if (ydelta < 0)
ydelta = -ydelta;
xinc = (x2 > x1)? 1 : -1;
yinc = (y2 > y1)? 1 : -1;
/* draw first point*/
if (GdClipPoint(psd, x1, y1))
psd->DrawPixel(psd, x1, y1, gr_foreground);
if (xdelta >= ydelta) {
rem = xdelta / 2;
for (;;) {
if (!bDrawLastPoint && x1 == x2)
break;
x1 += xinc;
rem += ydelta;
if (rem >= xdelta) {
rem -= xdelta;
y1 += yinc;
}
if (gr_dashcount) {
if ((gr_dashmask & (1 << bit)) && GdClipPoint(psd, x1, y1))
psd->DrawPixel(psd, x1, y1, gr_foreground);
bit = (bit + 1) % gr_dashcount;
} else { /* No dashes */
if (GdClipPoint(psd, x1, y1))
psd->DrawPixel(psd, x1, y1, gr_foreground);
}
if (bDrawLastPoint && x1 == x2)
break;
}
} else {
rem = ydelta / 2;
for (;;) {
if (!bDrawLastPoint && y1 == y2)
break;
y1 += yinc;
rem += xdelta;
if (rem >= ydelta) {
rem -= ydelta;
x1 += xinc;
}
/* If we are trying to draw to a dash mask */
if (gr_dashcount) {
if ((gr_dashmask & (1 << bit)) && GdClipPoint(psd, x1, y1))
psd->DrawPixel(psd, x1, y1, gr_foreground);
bit = (bit + 1) % gr_dashcount;
} else { /* No dashes */
if (GdClipPoint(psd, x1, y1))
psd->DrawPixel(psd, x1, y1, gr_foreground);
}
if (bDrawLastPoint && y1 == y2)
break;
}
}
GdFixCursor(psd);
}
/* Draw a point in the foreground color, applying clipping if necessary*/
/*static*/ void
drawpoint(PSD psd, MWCOORD x, MWCOORD y)
{
if (GdClipPoint(psd, x, y))
psd->DrawPixel(psd, x, y, gr_foreground);
}
/* Draw a horizontal line from x1 to and including x2 in the
* foreground color, applying clipping if necessary.
*/
/*static*/ void
drawrow(PSD psd, MWCOORD x1, MWCOORD x2, MWCOORD y)
{
MWCOORD temp;
/* reverse endpoints if necessary */
if (x1 > x2) {
temp = x1;
x1 = x2;
x2 = temp;
}
/* clip to physical device */
if (x1 < 0)
x1 = 0;
if (x2 >= psd->xvirtres)
x2 = psd->xvirtres - 1;
/* check cursor intersect once for whole line */
GdCheckCursor(psd, x1, y, x2, y);
/* If aren't trying to draw a dash, then head for the speed */
if (!gr_dashcount) {
while (x1 <= x2) {
if (GdClipPoint(psd, x1, y)) {
temp = MWMIN(clipmaxx, x2);
psd->DrawHorzLine(psd, x1, temp, y, gr_foreground);
} else
temp = MWMIN(clipmaxx, x2);
x1 = temp + 1;
}
} else {
unsigned bit = 0;
int p; /* must use "int" to handle case when x2 < 0*/
/* We want to draw a dashed line instead */
for (p = x1; p <= x2; p++) {
if ((gr_dashmask & (1 << bit)) && GdClipPoint(psd, p, y))
psd->DrawPixel(psd, p, y, gr_foreground);
bit = (bit + 1) % gr_dashcount;
}
}
}
/* Draw a vertical line from y1 to and including y2 in the
* foreground color, applying clipping if necessary.
*/
/*static*/ void
drawcol(PSD psd, MWCOORD x,MWCOORD y1,MWCOORD y2)
{
MWCOORD temp;
/* reverse endpoints if necessary */
if (y1 > y2) {
temp = y1;
y1 = y2;
y2 = temp;
}
/* clip to physical device */
if (y1 < 0)
y1 = 0;
if (y2 >= psd->yvirtres)
y2 = psd->yvirtres - 1;
/* check cursor intersect once for whole line */
GdCheckCursor(psd, x, y1, x, y2);
if (!gr_dashcount) {
while (y1 <= y2) {
if (GdClipPoint(psd, x, y1)) {
temp = MWMIN(clipmaxy, y2);
psd->DrawVertLine(psd, x, y1, temp, gr_foreground);
} else
temp = MWMIN(clipmaxy, y2);
y1 = temp + 1;
}
} else {
unsigned int p, bit = 0;
/* We want to draw a dashed line instead */
for (p = y1; p <= y2; p++) {
if ((gr_dashmask & (1<<bit)) && GdClipPoint(psd, x, p))
psd->DrawPixel(psd, x, p, gr_foreground);
bit = (bit + 1) % gr_dashcount;
}
}
}
/**
* Draw a rectangle in the foreground color, applying clipping if necessary.
* This is careful to not draw points multiple times in case the rectangle
* is being drawn using XOR.
*
* @param psd Drawing surface.
* @param x Left edge of rectangle.
* @param y Top edge of rectangle.
* @param width Width of rectangle.
* @param height Height of rectangle.
*/
void
GdRect(PSD psd, MWCOORD x, MWCOORD y, MWCOORD width, MWCOORD height)
{
MWCOORD maxx;
MWCOORD maxy;
if (width <= 0 || height <= 0)
return;
maxx = x + width - 1;
maxy = y + height - 1;
drawrow(psd, x, maxx, y);
if (height > 1)
drawrow(psd, x, maxx, maxy);
if (height < 3)
return;
++y;
--maxy;
drawcol(psd, x, y, maxy);
if (width > 1)
drawcol(psd, maxx, y, maxy);
GdFixCursor(psd);
}
/**
* Draw a filled in rectangle in the foreground color, applying
* clipping if necessary.
*
* @param psd Drawing surface.
* @param x1 Left edge of rectangle.
* @param y1 Top edge of rectangle.
* @param width Width of rectangle.
* @param height Height of rectangle.
*/
void
GdFillRect(PSD psd, MWCOORD x1, MWCOORD y1, MWCOORD width, MWCOORD height)
{
uint32_t dm = 0;
int dc = 0;
MWCOORD x2 = x1 + width - 1;
MWCOORD y2 = y1 + height - 1;
if (width <= 0 || height <= 0)
return;
#if MW_FEATURE_SHAPES
/* Stipples and tiles have their own drawing routines */
if (gr_fillmode != MWFILL_SOLID) {
set_ts_origin(x1, y1);
ts_fillrect(psd, x1, y1, width, height);
GdFixCursor(psd);
return;
}
#endif
/* See if the rectangle is either totally visible or totally
* invisible. If so, then the rectangle drawing is easy.
*/
switch (GdClipArea(psd, x1, y1, x2, y2)) {
case CLIP_VISIBLE:
psd->FillRect(psd, x1, y1, x2, y2, gr_foreground);
GdFixCursor(psd);
return;
case CLIP_INVISIBLE:
return;
}
/* Quickly save off the dash settings to avoid problems with drawrow */
GdSetDash(&dm, &dc);
/* The rectangle may be partially obstructed. So do it line by line. */
while (y1 <= y2)
drawrow(psd, x1, x2, y1++);
/* Restore the dash settings */
GdSetDash(&dm, &dc);
GdFixCursor(psd);
}
#if MW_FEATURE_IMAGES
#if MW_FEATURE_PALETTE
/**
* Return true if color is in palette
*
* @param cr Color to look for.
* @param palette Palette to look in.
* @param palsize Size of the palette.
* @return TRUE iff the color is in palette.
*/
MWBOOL
GdColorInPalette(MWCOLORVAL cr,MWPALENTRY *palette,int palsize)
{
int i;
for(i=0; i<palsize; ++i)
if(GETPALENTRY(palette, i) == cr)
return TRUE;
return FALSE;
}
/**
* Create a MWPIXELVAL conversion table between the passed palette
* and the in-use palette. The system palette is loaded/merged according
* to fLoadType.
*
* FIXME: LOADPALETTE and MERGEPALETTE are defined in "device.h"
*
* @param psd Drawing surface.
* @param palette Palette to look in.
* @param palsize Size of the palette.
* @param convtable Destination for the conversion table. Will hold palsize
* entries.
* @param fLoadType LOADPALETTE to set the surface's palette to the passed
* palette, MERGEPALETTE to add the passed colors to the surface
* palette without removing existing colors, or 0.
*/
void
GdMakePaletteConversionTable(PSD psd,MWPALENTRY *palette,int palsize,
MWPIXELVALHW *convtable,int fLoadType)
{
int i;
MWCOLORVAL cr;
int newsize, nextentry;
MWPALENTRY newpal[256];
/*
* Check for load palette completely, or add colors
* from passed palette to system palette until full.
*/
if(psd->pixtype == MWPF_PALETTE) {
switch(fLoadType) {
case LOADPALETTE:
/* Load palette from beginning with image's palette.
* First palette entries are Microwindows colors
* and not changed.
*/
GdSetPalette(psd, gr_firstuserpalentry, palsize, palette);
break;
case MERGEPALETTE:
/* get system palette*/
for(i=0; i<(int)psd->ncolors; ++i)
newpal[i] = gr_palette[i];
/* merge passed palette into system palette*/
newsize = 0;
nextentry = gr_nextpalentry;
/* if color missing and there's room, add it*/
for(i=0; i<palsize && nextentry < (int)psd->ncolors; ++i) {
cr = GETPALENTRY(palette, i);
if(!GdColorInPalette(cr, newpal, nextentry)) {
newpal[nextentry++] = palette[i];
++newsize;
}
}
/* set the new palette if any color was added*/
if(newsize) {
GdSetPalette(psd, gr_nextpalentry, newsize, &newpal[gr_nextpalentry]);
gr_nextpalentry += newsize;
}
break;
}
}
/*
* Build conversion table from inuse system palette and
* passed palette. This will load RGB values directly
* if running truecolor, otherwise it will find the
* nearest color from the inuse palette.
* FIXME: tag the conversion table to the bitmap image
*/
for(i=0; i<palsize; ++i) {
cr = GETPALENTRY(palette, i);
convtable[i] = GdFindColor(psd, cr);
}
}
#endif /* MW_FEATURE_PALETTE*/
static void GdDrawImageByPoint(PSD psd, MWCOORD x, MWCOORD y, PMWIMAGEHDR pimage);
/**
* Draw a color bitmap image in 1, 4, 8, 16, 24 or 32 bits per pixel.
* The Microwindows color image format is DWORD padded bytes, with
* the upper bits corresponding to the left side (identical to
* the MS Windows format). This format is currently different
* than the MWIMAGEBITS format, which uses word-padded bits
* for monochrome display only, where the upper bits in the word
* correspond with the left side.
*
* @param psd Drawing surface.
* @param x Destination X co-ordinate for left of image.
* @param y Destination Y co-ordinate for top of image.
* @param pimage Structure describing the image.
*/
void
GdDrawImage(PSD psd, MWCOORD x, MWCOORD y, PMWIMAGEHDR pimage)
{
int op = MWROP_COPY;
MWBLITFUNC convblit;
MWBLITPARMS parms;
/* use srcover for supported images with alpha*/
if (pimage->data_format & MWIF_HASALPHA)
op = MWROP_SRC_OVER;
/* find conversion blit based on data format*/
convblit = GdFindConvBlit(psd, pimage->data_format, op);
/* if not using new MWIF_ format and convblit drivers, must draw pixel by pixel*/
if (!convblit) {
if (pimage->bpp != 4) /* don't complain about win32 bmp format conversions*/
DPRINTF("GdDrawImage: not RGBA/RGB format or no convblit, using slow GdDrawImageByPoint\n");
GdDrawImageByPoint(psd, x, y, pimage); /* old pixel-by-pixel drawing*/
return;
}
/* use fast conversion blit*/
parms.op = op;
parms.data_format = pimage->data_format;
parms.width = pimage->width;
parms.height = pimage->height;
parms.dstx = x;
parms.dsty = y;
parms.srcx = 0;
parms.srcy = 0;
parms.src_pitch = pimage->pitch;
parms.fg_colorval = gr_foreground_rgb; /* for convblit*/
parms.bg_colorval = gr_background_rgb;
parms.fg_pixelval = gr_foreground; /* for palette mask convblit*/
parms.bg_pixelval = gr_background;
parms.usebg = gr_usebg;
parms.data = pimage->imagebits;
parms.dst_pitch = psd->pitch; /* usually set in GdConversionBlit*/
parms.data_out = psd->addr;
parms.srcpsd = NULL;
GdConvBlitInternal(psd, &parms, convblit);
}
/*
* Alpha drawing using C bitfields. Experimental,
* uses bitfields rather than explicit bit-twiddling.
*/
/* ARGB8888 : 0xaarrggbb order (little endian frame buffer format BB GG RR AA)*/
typedef union {
struct {
/* use processor byte endianness rather than bitfield order*/
#if !MW_CPU_BIG_ENDIAN
unsigned char b;
unsigned char g;
unsigned char r;
unsigned char a; // MSByte on little endian
#else
unsigned char a; // MSByte on big endian
unsigned char r;
unsigned char g;
unsigned char b;
#endif
} f;
unsigned int v;
} ARGB8888;
typedef union {
/* bitfield order should use __BIG_ENDIAN_BITFIELDS, assuming so with big endian byte order*/
struct {
#if !MW_CPU_BIG_ENDIAN
unsigned short b:5;
unsigned short g:6;
unsigned short r:5; // MSBit on little endian
#else
unsigned short r:5; // MSBit on big endian
unsigned short g:6;
unsigned short b:5;
#endif
} f;
unsigned short v;
} RGB565;
typedef union {
/* bitfield order should use __BIG_ENDIAN_BITFIELDS, assuming so with big endian byte order*/
struct {
#if !MW_CPU_BIG_ENDIAN
unsigned short b:5;
unsigned short g:5;
unsigned short r:5;
unsigned short a:1; // MSBit on little endian
#else
unsigned short a:1; // MSBit on big endian
unsigned short r:5;
unsigned short g:5;
unsigned short b:5;
#endif
} f;
unsigned short v;
} RGB555;
typedef union {
/* bitfield order should use __BIG_ENDIAN_BITFIELDS, assuming so with big endian byte order*/
struct {
#if !MW_CPU_BIG_ENDIAN
unsigned short r:5;
unsigned short g:5;
unsigned short b:5;
unsigned short a:1; // MSBit on little endian
#else
unsigned short a:1; // MSBit on big endian
unsigned short b:5;
unsigned short g:5;
unsigned short r:5;
#endif
} f;
unsigned short v;
} RGB1555;
/* slow draw point by point with clipping*/
static void
GdDrawImageByPoint(PSD psd, MWCOORD x, MWCOORD y, PMWIMAGEHDR pimage)
{
MWCOORD minx;
MWCOORD maxx;
MWUCHAR bitvalue = 0;
int bitcount;
MWUCHAR *imagebits;
MWCOORD height, width;
int bpp;
MWPIXELVALHW pixel;
int clip;
int extra, linesize;
int rgborder, alpha;
MWCOLORVAL cr;
MWCOORD yoff;
uint32_t transcolor;
MWPIXELVALHW convtable[256];
height = pimage->height;
width = pimage->width;
/* determine if entire image is clipped out, save clipresult for later*/
clip = GdClipArea(psd, x, y, x + width - 1, y + height - 1);
if(clip == CLIP_INVISIBLE)
return;
transcolor = pimage->transcolor;
bpp = pimage->bpp;
#if MW_FEATURE_PALETTE
/*
* Merge the images's palette and build a palette index conversion table.
*/
if (pimage->bpp <= 8) {
if(!pimage->palette) { /* for jpeg's without a palette*/
for(yoff=0; yoff<pimage->palsize; ++yoff)
convtable[yoff] = yoff;
} else
GdMakePaletteConversionTable(psd, pimage->palette, pimage->palsize,
convtable, MERGEPALETTE);
}
#endif
minx = x;
maxx = x + width - 1;
imagebits = pimage->imagebits;
// /* check for bottom-up image*/
// if(pimage->compression & MWIMAGE_UPSIDEDOWN) {
// y += height - 1;
// yoff = -1;
// } else
yoff = 1;
#define PIX2BYTES(n) (((n)+7)/8)
/* imagebits are dword aligned*/
switch(pimage->bpp) {
default:
case 8:
linesize = width;
break;
case 32:
linesize = width*4;
break;
case 24:
case 18:
linesize = width*3;
break;
case 16:
linesize = width*2;
break;
case 4:
linesize = PIX2BYTES(width<<2);
break;
case 1:
linesize = PIX2BYTES(width);
break;
}
extra = pimage->pitch - linesize;
/* Image format in RGB rather than BGR byte order?*/
rgborder = (pimage->data_format == MWIF_RGB888 || pimage->data_format == MWIF_RGBA8888);
/* handle 32bpp RGBA images with alpha channel*/
if (pimage->data_format == MWIF_RGBA8888) {
int32_t *data = (int32_t *)imagebits;
while (height > 0) {
/*
* Grab 32 bits of data using processor endianness.
* For little endian:
* MWIMAGE_RGB will be long word 0xAABBGGRR (ABGR)
* MWIMAGE_BGR will be long word 0xAARRGGBB (ARGB)
*
* For big endian:
* MWIMAGE_RGB will be long word 0xRRGGBBAA (RGBA)
* MWIMAGE_BGR will be long word 0xBBGGRRAA (BGRA)
*/
cr = *data++;
/*
* Convert to ARGB (MWPIXELVAL little endian framebuffer format)
*/
#if MW_CPU_BIG_ENDIAN
if (rgborder) {
/*
* cr is RGBA, shift RGB >> 8 and A << 24 to ARGB.
*/
cr = ((cr & 0xFFFFFF00UL) >> 8)
| ((cr & 0x000000FFUL) << 24);
} else {
/*
* cr is BGRA, swap endianness to ARGB.
*/
cr = ((cr & 0xFF000000UL) >> 24)
| ((cr & 0x00FF0000UL) >> 8)
| ((cr & 0x0000FF00UL) << 8)
| ((cr & 0x000000FFUL) << 24);
}
#else /* little endian*/
if (rgborder) {
/*
* cr is ABGR, swap R/B to ARGB.
*/
cr = (cr & 0xFF00FF00UL)
| ((cr & 0x00FF0000UL) >> 16)
| ((cr & 0x000000FFUL) << 16);
} else {
/*
* cr is ARGB, no change needed.
*/
}
#endif
/* cr is now in ARGB(0xaarrggbb) format*/
alpha = (cr >> 24);
if (alpha != 0) { /* skip if pixel is fully transparent*/
if (clip == CLIP_VISIBLE || GdClipPoint(psd, x, y)) {
switch (psd->pixtype) {
#if 1 /* alpha blending*/
/* implement alpha blending image draw from image alpha channel*/
case MWPF_TRUECOLOR8888:
case MWPF_TRUECOLOR888:
if (alpha == 255)
pixel = cr; /* both cr and pixel are ARGB (0xAARRGGBB)*/
else {
/* ARGB8888 : 0xAARRGGBB*/
/* MWPIXELVAL : 0x00RRGGBB*/
ARGB8888 fg;
ARGB8888 bg;
fg.v = cr;
bg.v = psd->ReadPixel(psd,x,y);
//bg +=muldiv255(a,fg-bg)
bg.f.r += muldiv255(alpha, fg.f.r - bg.f.r);
bg.f.g += muldiv255(alpha, fg.f.g - bg.f.g);
bg.f.b += muldiv255(alpha, fg.f.b - bg.f.b);
//d += muldiv255(a, 255 - d)
bg.f.a += muldiv255(alpha, 255 - bg.f.a);
//d = muldiv255(d, 255 - a) + a
//bg.f.a = muldiv255(bg.f.a, 255 - alpha) + alpha;
pixel = bg.v; /* endian swap handled with ARGB8888 struct*/
}
break;
case MWPF_TRUECOLORABGR:
if (alpha == 255)
pixel = PIXEL888TOCOLORVAL(cr); /* convert ARGB cr to ABGR pixel*/
else {
/* ARGB8888 : 0xAARRGGBB*/
/* MWPIXELVAL : 0xAABBGGRR*/
ARGB8888 fg;
ARGB8888 bg;
/* tricky code: just swap red/blue from above case for bg pixel*/
fg.v = cr;
bg.v = psd->ReadPixel(psd,x,y);
//bg +=muldiv255(a,fg-bg)
bg.f.b += muldiv255(alpha, fg.f.r - bg.f.b); /* actually bg red*/
bg.f.g += muldiv255(alpha, fg.f.g - bg.f.g);
bg.f.r += muldiv255(alpha, fg.f.b - bg.f.r); /* actually bg blue*/
//d += muldiv255(a, 255 - d)
bg.f.a += muldiv255(alpha, 255 - bg.f.a);
pixel = bg.v; /* endian swap handled with ARGB8888 struct*/
}
break;
case MWPF_TRUECOLOR565:
if (alpha == 255)
pixel = ARGB2PIXEL565(cr);
else {
/* ARGB565 : 0xAARRGGBB*/
/* MWPIXELVAL : r/g/b 5/6/5*/
ARGB8888 fg;
RGB565 bg;
/*
* 1) case: DrawPixel(x,y,c)
* c=(b)rrrrrggg,gggbbbbb (MSB,LSB order)
* memory format at address fb(x,y). i.e. ADDR (lo,hi addr order)
* ADDR[0], ADDR[1]
* little : gggbbbbb,rrrrrggg
* big : rrrrrggg,gggbbbbb
*
* 2) case: ushort c = ReadPixel(x,y)
* ushort c format. (MSB,LSB order)
* MSB LSB
* little : c=(b)rrrrrggg,gggbbbbb
* big : c=(b)rrrrrggg,gggbbbbb
*/
fg.v = cr;
bg.v = psd->ReadPixel(psd,x,y);
//bg +=mulscale(a,fg-bg)
bg.f.r += mulscale(alpha, fg.f.r - (bg.f.r<<3), 11);
bg.f.g += mulscale(alpha, fg.f.g - (bg.f.g<<2), 10);
bg.f.b += mulscale(alpha, fg.f.b - (bg.f.b<<3), 11);
//bg.f.a = 0;
pixel = bg.v;
}