aboutsummaryrefslogtreecommitdiff
path: root/BITMAPS.C
blob: c929d5fee12d55542c56ce39d7b3c7390d5d668f (plain)
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
/*----------------------------------------------------------------------*/
/*  									*/
/*     bitmaps.c  -- BitMap operations  (c) Electronic Arts 		*/
/*  									*/
/*   	6/22/85   Created	Dan Silva  				*/
/*   	7/11/85   Added MakeMask Dan Silva 				*/
/*   	8/1/85    Added prism functions Dan Silva			*/
/*----------------------------------------------------------------------*/
/* Note: all Bitmap allocating functions will first try to free		*/
/*  the planes of the BitMap. Initially all bm.Plane[] values should	*/
/*  be set to NULL. 							*/
/*----------------------------------------------------------------------*/

#include <system.h>
#include <prism.h>
#define KPrintF printf
extern struct RastPort tempRP;
extern struct TmpRas tmpRas;
extern BOOL haveWBench;

BOOL dbgalloc = NO;
       	
prbm(bm)  struct BitMap *bm;  {
#ifdef DBGBM
    dprintf("BitMap: bpr=%ld, rows= %ld, deep = %ld \n",
	bm->BytesPerRow, bm->Rows, bm->Depth);
#endif
    }
    
static AllocErr(s) char *s; { 
#ifdef DBGBM
    if (dbgalloc) dprintf(" Alloc or Freeing error in %s\n",s);   
#endif
    }

#define MINAVAILMEM 13000

/* Allocate planes seperately-- wont work for BOBs */
/* This routine Does NOT free planes first */
TmpAllocBitMap(bm)  struct BitMap *bm;  {
    SHORT i;
    int psize = BMPlaneSize(bm);
    setmem(bm->Planes, 8*sizeof(LONG), 0);
    for (i=0; i<bm->Depth; i++) {
	bm->Planes[i]= (PLANEPTR)ChipAlloc(psize);
	if (bm->Planes[i]==NULL){
	    if (dbgalloc){   
#ifdef DBGBM
		dprintf(" AllocBitmap failure: \n");
		prbm(bm);
#endif
		}
	    FreeBitMap(bm);
	    return(FAIL); 
	    }
	}
    return(SUCCESS);
    }

AllocBitMap(bm)  struct BitMap *bm;  {
    BOOL res = TmpAllocBitMap(bm);
    if (res==FAIL) return(FAIL);
    if (AvailMem(MEMF_CHIP|MEMF_PUBLIC) < MINAVAILMEM) {
	FreeBitMap(bm);
	return(FAIL);
	}
    return(SUCCESS);
    }
    
UBYTE *BMAllocMask(bm) struct BitMap *bm; {
    return( (UBYTE *) ChipAlloc(bm->BytesPerRow * bm->Rows) );
    }

FreeBitMap(bm) struct BitMap *bm; {
    SHORT i;
    for (i=0; i<bm->Depth; i++)  if (bm->Planes[i] != NULL) {
	DFree(bm->Planes[i]);
    	bm->Planes[i] = NULL;
	}
    }
    
NewSizeBitMap(bm,deep,w,h)  struct BitMap *bm; SHORT deep,w,h;   {
    int ret;
    int bw = BytesNeeded(w);
    if ((bm->Planes[0] != NULL)&&(bm->Depth==deep)&&
	(bm->BytesPerRow==bw)&&(bm->Rows==h)) return(0);
    FreeBitMap(bm);
    bm->Depth = deep;
    bm->BytesPerRow = bw;
    bm->Rows = h;
    ret = AllocBitMap(bm);
    if (ret) AllocErr("NewSizeBitMap");
    return(ret);
    }

ClearBitMap(bm) struct BitMap *bm; {
    SHORT i;
    for (i=0; i<bm->Depth; i++) 	
	BltClear(bm->Planes[i],(bm->Rows<<16)+bm->BytesPerRow, 3);
    }

SetBitMap(bm,color) struct BitMap *bm; SHORT color; {
    tempRP.BitMap = bm;
    SetRast(&tempRP,color);
    }

/* USE WITH CAUTION: WILL OVERWRITE Planes[] POINTERS */
BlankBitMap(bm) struct BitMap *bm; {
    setmem(bm, sizeof(struct BitMap), 0);
    }

/* This assures that bitmap b is the same size as a */    
MakeEquivBM(a,b) struct BitMap *a,*b; {
    int ret;
    if (ret=NewSizeBitMap(b,a->Depth,a->BytesPerRow*8,a->Rows))
	AllocErr("MakeEquivBM");
    return(ret);
    }

FillBitMap(pb,v) struct BitMap *pb; int v; {
    SHORT i;
    for (i=0; i<pb->Depth; i++) setmem(pb->Planes[i],BMPlaneSize(pb),v);
    }

/* this results in two totally identical BitMap structures */
/* pointing at the same data */
DupBitMap(a,b) struct BitMap *a,*b; {
    FreeBitMap(b);
    movmem(a,b,sizeof(struct BitMap));
    }

/* this makes a copy of the data too */     
CopyBitMap(a,b) struct BitMap *a,*b; {
    if(MakeEquivBM(a,b)){ AllocErr("CopyBitMap"); return(FAIL); }
    WaitBlit();
    BltBitMap(a,0,0,b,0,0,a->BytesPerRow*8,a->Rows,REPOP,0xff,NULL);
    return(SUCCESS);
    }

/*--------------------------------------------------------------*/
/*								*/
/*			Make Mask				*/
/*								*/
/*--------------------------------------------------------------*/
/* 

This makes masks for a bitmap associated with a particular
"special" color.  It will either make a mask which has 1's in it ONLY 
where the bitmap has that special color (a POSITIVE mask) or it will 
make a mask which has 1's in it where the bitmap is NOT that special 
color ( a NEGATIVE mask ).

To make a positive mask for the special color 10110, 
where p<i> is the <i>th plane of the Bitmap: 

	mask  = p4 & (^p3) & p2 & p1 & (^p0);

To a negative mask mask for a Bitmap for a particular "transparent" 
color, you want a 1 where the color of the bitmap is NOT the 
transparent color, zero where it IS is the transparent color .
    
	e.g. if xpcol = 10110 then want
    
	mask = (^p4) | p3 | (^p2) | (^p1) | p0
    
---------------------------------------------------------------------**/
MakeMask(bm,mask,color,sign) 
    struct BitMap *bm;		/* bitmap for which mask is to be made*/
    UBYTE *mask;		/* holds the result: should be as big as
    						 one plane of bm */
    SHORT color;		/* special color	*/
    BOOL sign;			/* TRUE = positive, FALSE = negative */
    {
    SHORT depth,i,mt,bltSize;
    UBYTE op1,op2;
    depth = bm->Depth;
    bltSize = BlitSize(bm->BytesPerRow,bm->Rows);
    if (sign) { mt = REPOP; op1 = ANDOP;   op2 = NOTANDOP;  }
    else     { mt = NOTOP;  op1 = NOTOROP; op2 = OROP;  	}
    if (!(color&1)) mt ^= 0xff;
    for (i=0; i<depth; i++) {
	BltABCD(NULL, bm->Planes[i], mask, mask, bltSize, mt);
	color >>= 1;
	mt = (color&1) ? op1: op2; 
	}
    WaitBlit();
    }


#ifdef theOldWay
MakeMask(bm,mask,color,pos) 
    struct BitMap *bm;		/* bitmapr for which mask is to be made*/
    UBYTE *mask;		/* should be as big as one plane of bm */
    SHORT color;		/* special color	*/
    BOOL pos;			/* TRUE = positive, FALSE = negative */
    
    {
    struct BitMap mbm;
    SHORT depth,i,mt,w;
    PLANEPTR plane0;
    UBYTE op1,op2;
    mbm.BytesPerRow = bm->BytesPerRow;
    mbm.Rows = bm->Rows;    mbm.Depth = 1;    mbm.Flags = 0;
    mbm.Planes[0] = (PLANEPTR)mask;
    depth = bm->Depth;
    w = bm->BytesPerRow*8;
    if (pos) { 	SetBitMap(&mbm,0xff); 	op1 = ANDOP;   op2 = NOTANDOP;  }
    else     { 	ClearBitMap(&mbm); 	op1 = NOTOROP; op2 = OROP;  	}
    bm->Depth = 1;
    plane0 = bm->Planes[0];
    for (i=0; i<depth; i++) {
	bm->Planes[0] = bm->Planes[i];
	mt = (color&1) ? op1: op2; 
	WaitBlit();
	BltBitMap(bm,0,0,&mbm,0,0,w,mbm.Rows,mt,0x01,NULL);
	color >>= 1;
	}
    WaitBlit();
    bm->Depth = depth;
    bm->Planes[0] = plane0;
    }
#endif
/* Changes all pixels in a bitmap which are color number "acol" to
   be color number "bcol" */


BMMapColor(sbm,dbm,acol,bcol) struct BitMap *sbm, *dbm; SHORT acol,bcol; {
    BYTE *tmpmask = tmpRas.RasPtr;
    SHORT w = sbm->BytesPerRow*8;
    MakeMask(sbm,tmpmask,acol,POSITIVE);
    tempRP.BitMap = dbm;
    SetAPen(&tempRP,bcol); 
    SetDrMd(&tempRP,JAM1);
    BltTemplate(tmpmask, 0, sbm->BytesPerRow, &tempRP, 0,0,w,sbm->Rows); 
    }