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/genl.c $
21  * $Revision: 1.4 $
22  * $Author: kevin $
23  * $Date: 1994/08/16 12:48:26 $
24  *
25  * Routines to linearly texture map a flat8 bitmap to a generic canvas.
26  *
27  * This file is part of the 2d library.
28  *
29  */
30 
31 #include "cnvdat.h"
32 #include "fl8tf.h"
33 #include "fl8tmapdv.h"
34 #include "gente.h"
35 #include "poly.h"
36 #include "tmapint.h"
37 #include "vtab.h"
38 
39 // prototypes
40 int gri_lin_umap_loop(grs_tmap_loop_info *tli);
41 
42 /*extern "C"
43 {
44 int Handle_LinClut_Loop_PPC(fix u, fix v, fix du, fix dv, fix dx,
45                                                                                                                 grs_tmap_loop_info
46 *tli, uchar *start_pdest, uchar *t_bits, int32_t gr_row, uchar *t_clut, uchar	t_wlog, uint32_t t_mask);
47 }*/
48 
Handle_LinClut_Loop_C(fix u,fix v,fix du,fix dv,fix dx,grs_tmap_loop_info * tli,uchar * start_pdest,uchar * t_bits,int32_t gr_row,uchar * t_clut,uchar t_wlog,uint32_t t_mask)49 int Handle_LinClut_Loop_C(fix u, fix v, fix du, fix dv, fix dx, grs_tmap_loop_info *tli, uchar *start_pdest,
50                           uchar *t_bits, int32_t gr_row, uchar *t_clut, uchar t_wlog, uint32_t t_mask) {
51     register int x, k;
52     uchar *p_dest;
53     register fix rx, lx;
54 
55     rx = tli->right.x;
56     lx = tli->left.x;
57     tli->y += tli->n;
58 
59     do {
60         if ((x = fix_ceil(rx) - fix_ceil(lx)) > 0) {
61             x = fix_ceil(lx) - lx;
62 
63             k = fix_div(fix_make(1, 0), dx);
64             du = fix_mul_asm_safe(du, k);
65             dv = fix_mul_asm_safe(dv, k);
66 
67             u += fix_mul(du, x);
68             v += fix_mul(dv, x);
69 
70             // copy out tli-> stuff into locals
71             p_dest = start_pdest + fix_cint(lx);
72             x = fix_cint(rx) - fix_cint(lx);
73 
74             for (; x > 0; x--) {
75                 *(p_dest++) = t_clut[t_bits[((fix_fint(v) << t_wlog) + fix_fint(u)) & t_mask]];
76                 u += du;
77                 v += dv;
78             }
79         } else if (x < 0)
80             return TRUE; // punt this tmap
81 
82         u = (tli->left.u += tli->left.du);
83         tli->right.u += tli->right.du;
84         du = tli->right.u - u;
85         v = (tli->left.v += tli->left.dv);
86         tli->right.v += tli->right.dv;
87         dv = tli->right.v - v;
88         lx += tli->left.dx;
89         rx += tli->right.dx;
90         dx = rx - lx;
91         start_pdest += gr_row;
92     } while (--(tli->n) > 0);
93 
94     tli->right.x = rx;
95     tli->left.x = lx;
96 
97     return FALSE; // tmap OK
98 }
99 
gri_lin_umap_loop(grs_tmap_loop_info * tli)100 int gri_lin_umap_loop(grs_tmap_loop_info *tli) {
101     fix u, v, du, dv, dx, d;
102 
103     // locals used to store copies of tli-> stuff, so its in registers on the PPC
104     register int x, k;
105     uchar *p_dest;
106     uchar temp_pix;
107     int32_t *t_vtab;
108     uchar *t_bits;
109     uchar *t_clut;
110     uchar t_wlog;
111     uint32_t t_mask;
112     int32_t gr_row;
113     uchar *start_pdest;
114     int32_t inv;
115 
116     u = tli->left.u;
117     du = tli->right.u - u;
118     v = tli->left.v;
119     dv = tli->right.v - v;
120     dx = tli->right.x - tli->left.x;
121 
122     t_vtab = tli->vtab;
123     t_clut = tli->clut;
124     t_mask = tli->mask;
125     t_wlog = tli->bm.wlog;
126 
127     t_bits = tli->bm.bits;
128     gr_row = grd_bm.row;
129     start_pdest = grd_bm.bits + (gr_row * (tli->y));
130 
131     // handle PowerPC loop
132     if (tli->bm.hlog == (GRL_OPAQUE | GRL_LOG2 | GRL_CLUT))
133         return (Handle_LinClut_Loop_C(u, v, du, dv, dx, tli, start_pdest, t_bits, gr_row, t_clut, t_wlog, t_mask));
134 
135     do {
136         if ((d = fix_ceil(tli->right.x) - fix_ceil(tli->left.x)) > 0) {
137             d = fix_ceil(tli->left.x) - tli->left.x;
138 
139 #if InvDiv
140             k = fix_div(fix_make(1, 0), dx);
141             du = fix_mul_asm_safe(du, k);
142             dv = fix_mul_asm_safe(dv, k);
143 #else
144             du = fix_div(du, dx);
145             dv = fix_div(dv, dx);
146 #endif
147             u += fix_mul(du, d);
148             v += fix_mul(dv, d);
149 
150             // copy out tli-> stuff into locals
151             p_dest = start_pdest + fix_cint(tli->left.x);
152             x = fix_cint(tli->right.x) - fix_cint(tli->left.x);
153 
154             switch (tli->bm.hlog) {
155             case GRL_OPAQUE:
156                 for (; x > 0 && v >= 0; x--) {
157                     k = t_vtab[fix_fint(v)] + fix_fint(u);
158                     *(p_dest++) = t_bits[k]; // gr_fill_upixel(t_bits[k],x,y);
159                     u += du;
160                     v += dv;
161                 }
162                 break;
163             case GRL_TRANS:
164                 for (; x > 0 && v >= 0; x--) {
165                     k = t_vtab[fix_fint(v)] + fix_fint(u);
166                     temp_pix = t_bits[k];
167                     if (temp_pix != 0)
168                         *p_dest = temp_pix; // gr_fill_upixel(t_bits[k],x,y);
169                     p_dest++;
170                     u += du;
171                     v += dv;
172                 }
173                 break;
174             case GRL_OPAQUE | GRL_LOG2:
175                 for (; x > 0 && v >= 0; x--) {
176                     k = ((fix_fint(v) << t_wlog) + fix_fint(u)) & t_mask;
177                     *(p_dest++) = t_bits[k]; // gr_fill_upixel(t_bits[k],x,y);
178                     u += du;
179                     v += dv;
180                 }
181                 break;
182             case GRL_TRANS | GRL_LOG2:
183                 for (; x > 0 && v >= 0; x--) {
184                     k = ((fix_fint(v) << t_wlog) + fix_fint(u)) & t_mask;
185                     temp_pix = t_bits[k];
186                     if (temp_pix != 0)
187                         *p_dest = temp_pix; // gr_fill_upixel(t_bits[k],x,y);
188                     p_dest++;
189                     u += du;
190                     v += dv;
191                 }
192                 break;
193             case GRL_OPAQUE | GRL_CLUT:
194                 for (; x > 0 && v >= 0; x--) {
195                     k = t_vtab[fix_fint(v)] + fix_fint(u);
196                     *(p_dest++) = t_clut[t_bits[k]]; // gr_fill_upixel(tli->clut[t_bits[k]],x,y);
197                     u += du;
198                     v += dv;
199                 }
200                 break;
201             case GRL_TRANS | GRL_CLUT:
202                 for (; x > 0 && v >= 0; x--) {
203                     k = t_vtab[fix_fint(v)] + fix_fint(u);
204                     k = t_bits[k];
205                     if (k != 0)
206                         *p_dest = t_clut[k]; // gr_fill_upixel(tli->clut[k],x,y);
207                     p_dest++;
208                     u += du;
209                     v += dv;
210                 }
211                 break;
212                 // handled in special case now
213                 /*     case GRL_OPAQUE|GRL_LOG2|GRL_CLUT:
214                         for (; x>0; x--) {
215                            k=((fix_fint(v)<<t_wlog)+fix_fint(u))&t_mask;
216                            *(p_dest++) = t_clut[t_bits[k]];		//
217                    gr_fill_upixel(tli->clut[t_bits[k]],x,y); u+=du; v+=dv;
218                         }
219                         break;*/
220             case GRL_TRANS | GRL_LOG2 | GRL_CLUT:
221                 for (; x > 0 && v >= 0; x--) {
222                     k = ((fix_fint(v) << t_wlog) + fix_fint(u)) & t_mask;
223                     k = t_bits[k];
224                     if (k != 0)
225                         *p_dest = t_clut[k]; // gr_fill_upixel(tli->clut[k],x,y);
226                     p_dest++;
227                     u += du;
228                     v += dv;
229                 }
230                 break;
231             }
232         } else if (d < 0)
233             return TRUE; /* punt this tmap */
234 
235         u = (tli->left.u += tli->left.du);
236         tli->right.u += tli->right.du;
237         du = tli->right.u - u;
238         v = (tli->left.v += tli->left.dv);
239         tli->right.v += tli->right.dv;
240         dv = tli->right.v - v;
241         tli->left.x += tli->left.dx;
242         tli->right.x += tli->right.dx;
243         dx = tli->right.x - tli->left.x;
244         tli->y++;
245         start_pdest += gr_row;
246     } while (--(tli->n) > 0);
247     return FALSE; /* tmap OK */
248 }
249 
gri_trans_lin_umap_init(grs_tmap_loop_info * tli)250 void gri_trans_lin_umap_init(grs_tmap_loop_info *tli) {
251     if ((tli->bm.row == (1 << tli->bm.wlog)) && (tli->bm.h == (1 << tli->bm.hlog))) {
252         tli->mask = (1 << (tli->bm.hlog + tli->bm.wlog)) - 1;
253         tli->bm.hlog = GRL_TRANS | GRL_LOG2;
254     } else {
255         tli->vtab = gr_make_vtab(&(tli->bm));
256         tli->bm.hlog = GRL_TRANS;
257     }
258     tli->loop_func = (void (*)())gri_lin_umap_loop;
259     tli->right_edge_func = (void (*)())gri_uvx_edge;
260     tli->left_edge_func = (void (*)())gri_uvx_edge;
261 }
262 
gri_opaque_lin_umap_init(grs_tmap_loop_info * tli)263 void gri_opaque_lin_umap_init(grs_tmap_loop_info *tli) {
264     if ((tli->bm.row == (1 << tli->bm.wlog)) && (tli->bm.h == (1 << tli->bm.hlog))) {
265         tli->mask = (1 << (tli->bm.hlog + tli->bm.wlog)) - 1;
266         tli->bm.hlog = GRL_OPAQUE | GRL_LOG2;
267     } else {
268         tli->vtab = gr_make_vtab(&(tli->bm));
269         tli->bm.hlog = GRL_OPAQUE;
270     }
271     tli->loop_func = (void (*)())gri_lin_umap_loop;
272     tli->right_edge_func = (void (*)())gri_uvx_edge;
273     tli->left_edge_func = (void (*)())gri_uvx_edge;
274 }
275 
gri_trans_clut_lin_umap_init(grs_tmap_loop_info * tli)276 void gri_trans_clut_lin_umap_init(grs_tmap_loop_info *tli) {
277     if ((tli->bm.row == (1 << tli->bm.wlog)) && (tli->bm.h == (1 << tli->bm.hlog))) {
278         tli->mask = (1 << (tli->bm.hlog + tli->bm.wlog)) - 1;
279         tli->bm.hlog = GRL_TRANS | GRL_LOG2 | GRL_CLUT;
280     } else {
281         tli->vtab = gr_make_vtab(&(tli->bm));
282         tli->bm.hlog = GRL_TRANS | GRL_CLUT;
283     }
284     tli->loop_func = (void (*)())gri_lin_umap_loop;
285     tli->right_edge_func = (void (*)())gri_uvx_edge;
286     tli->left_edge_func = (void (*)())gri_uvx_edge;
287 }
288 
gri_opaque_clut_lin_umap_init(grs_tmap_loop_info * tli)289 void gri_opaque_clut_lin_umap_init(grs_tmap_loop_info *tli) {
290     if ((tli->bm.row == (1 << tli->bm.wlog)) && (tli->bm.h == (1 << tli->bm.hlog))) {
291         tli->mask = (1 << (tli->bm.hlog + tli->bm.wlog)) - 1;
292         tli->bm.hlog = GRL_OPAQUE | GRL_LOG2 | GRL_CLUT;
293     } else {
294         tli->vtab = gr_make_vtab(&(tli->bm));
295         tli->bm.hlog = GRL_OPAQUE | GRL_CLUT;
296     }
297     tli->loop_func = (void (*)())gri_lin_umap_loop;
298     tli->right_edge_func = (void (*)())gri_uvx_edge;
299     tli->left_edge_func = (void (*)())gri_uvx_edge;
300 }
301