1 /*
2 
3 Copyright (C) 2015-2018 Night Dive Studios, LLC.
4 
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9 
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14 
15 You should have received a copy of the GNU General Public License
16 along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 
18 */
19 /*
20  * $Source: r:/prj/lib/src/2d/RCS/fl8fl8m.c $
21  * $Revision: 1.3 $
22  * $Author: kevin $
23  * $Date: 1994/10/04 18:46:16 $
24  *
25  * Routines for masking flat 8 bitmaps into a flat 8 canvas
26  * through a stencil.
27  *
28  * This file is part of the 2d library.
29  *
30  * $Log: fl8fl8m.c $
31  * Revision 1.3  1994/10/04  18:46:16  kevin
32  * added clut fill mode specific function.  Renamed old proc.
33  *
34  * Revision 1.2  1994/08/16  18:29:35  kevin
35  * fixed several bugs.
36  *
37  * Revision 1.1  1994/08/16  13:14:22  kevin
38  * Initial revision
39  *
40  */
41 
42 #include "bitmap.h"
43 #include "clip.h"
44 #include "cnvdat.h"
45 #include "fl8tf.h"
46 #include "grs.h"
47 #include "lg.h"
48 #include <string.h>
49 
50 extern int gen_flat8_bitmap(grs_bitmap *bm, short x, short y);
gri_flat8_mask_bitmap(grs_bitmap * bm,short x,short y,grs_stencil * sten)51 int gri_flat8_mask_bitmap(grs_bitmap *bm, short x, short y, grs_stencil *sten) {
52     if (sten == NULL)
53         return gen_flat8_bitmap(bm, x, y);
54     else {
55         int yf = y + bm->h;
56         uchar *dst;
57         uchar *src = bm->bits - x;
58         if (yf > grd_clip.bot)
59             yf = grd_clip.bot;
60         if (y < grd_clip.top) {
61             src += (grd_clip.top - y) * bm->row;
62             y = grd_clip.top;
63         }
64         dst = grd_bm.bits + y * grd_bm.row;
65         for (; y < yf; y++) {
66             int xi = x, xf = x + bm->w;
67             grs_sten_elem *s;
68 #ifdef NEW_STENCILS
69             s = &(sten[y]);
70 #else
71             s = &((sten->elem)[y]);
72 #endif
73             for (; s->r <= xi;) {
74                 s = s->n;
75                 if (s == NULL)
76                     break;
77             }
78             if (s != NULL) {
79                 if (s->l > xi)
80                     xi = s->l;
81                 if (s->r < xf)
82                     xf = s->r;
83                 if (xf > xi) {
84                     if (bm->flags & BMF_TRANS) {
85                         int i;
86                         for (i = xi; i < xf; i++)
87                             if (src[i])
88                                 dst[i] = src[i];
89                     } else {
90                         LG_memmove(dst + xi, src + xi, xf - xi);
91                     }
92                 }
93             }
94             src += bm->row;
95             dst += grd_bm.row;
96         }
97     }
98     return CLIP_NONE; /* actually, who knows? */
99 }
100 
101 extern int gen_flat8_clut_bitmap(grs_bitmap *bm, short x, short y, uchar *clut);
gri_flat8_mask_fill_clut_bitmap(grs_bitmap * bm,short x,short y,grs_stencil * sten)102 int gri_flat8_mask_fill_clut_bitmap(grs_bitmap *bm, short x, short y, grs_stencil *sten) {
103     uchar *clut = (uchar *)(grd_gc.fill_parm);
104     if (sten == NULL)
105         return gen_flat8_clut_bitmap(bm, x, y, clut);
106     else {
107         int yf = y + bm->h;
108         uchar *dst;
109         uchar *src = bm->bits - x;
110         if (yf > grd_clip.bot)
111             yf = grd_clip.bot;
112         if (y < grd_clip.top) {
113             src += (grd_clip.top - y) * bm->row;
114             y = grd_clip.top;
115         }
116         dst = grd_bm.bits + y * grd_bm.row;
117         for (; y < yf; y++) {
118             int xi = x, xf = x + bm->w;
119             grs_sten_elem *s;
120 #ifdef NEW_STENCILS
121             s = &(sten[y]);
122 #else
123             s = &((sten->elem)[y]);
124 #endif
125             for (; s->r <= xi;) {
126                 s = s->n;
127                 if (s == NULL)
128                     break;
129             }
130             if (s != NULL) {
131                 if (s->l > xi)
132                     xi = s->l;
133                 if (s->r < xf)
134                     xf = s->r;
135                 if (xf > xi) {
136                     int i;
137                     if (bm->flags & BMF_TRANS) {
138                         for (i = xi; i < xf; i++)
139                             if (src[i])
140                                 dst[i] = clut[src[i]];
141                     } else {
142                         for (i = xi; i < xf; i++)
143                             dst[i] = clut[src[i]];
144                     }
145                 }
146             }
147             src += bm->row;
148             dst += grd_bm.row;
149         }
150     }
151     return CLIP_NONE; /* actually, who knows? */
152 }
153