aboutsummaryrefslogtreecommitdiff
path: root/STRETCH.C
blob: 9a37a3ed70848c4db1796ff006de588f521fbb64 (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
/*----------------------------------------------------------------------*/
/*  									*/
/*     stretch.c  -- Brush Stretching Operations			*/
/*  									*/
/*----------------------------------------------------------------------*/

#include <system.h>
#include <prism.h>
#include <pnts.h>

extern SHORT curpen;
extern Dims newDim;
extern BMOB origBr,curbr;
extern PointRec pnts;
extern BOOL wasShift;
extern void (*killMode)(), (*nop)();
extern void obclr();
extern Box screenBox;
extern PaintMode paintMode;

/* #define DBGSTRCH */

#define local 

Dims newDim;
SHORT savePntMode=0;

LONG MulDiv(a,b,c) LONG a,b,c; {  return( (a*b)/c ); }

void FixOffs() { 
    SetPaintMode(savePntMode);
    curbr.xoffs = MulDiv(origBr.xoffs,newDim.w,origBr.pict.box.w); 
    curbr.yoffs = MulDiv(origBr.yoffs,newDim.h,origBr.pict.box.h);  
    }

SHORT StrBMOB(src,dst,newW,newH) BMOB *src,*dst; SHORT newW,newH; {
    Box sbox,dbox;
    if ( BMOBNewSize(dst,src->pict.bm->Depth,newW,newH)) {
#ifdef DBGSTRCH
	dprintf(" StrBMOB: couldnt alloc bm \n");
#endif
	return(FAIL);
	}
    sbox.x = sbox.y = dbox.x = dbox.y = 0;
    sbox.w = src->pict.box.w;
    sbox.h = src->pict.box.h;
    dbox.w = newW;
    dbox.h = newH;
    if( StrBits(src->pict.bm,&sbox,dst->pict.bm,&dbox,REPOP)){
#ifdef DBGSTRCH
	dprintf(" StrBMOB: couldnt do stretch \n");
#endif
	return(FAIL);
	}
    MakeMask(dst->pict.bm,dst->mask,dst->xpcolor,NEGATIVE);
    return(SUCCESS);
    }


void StrBrTo(w,h) int w,h; {
    if (curpen!=USERBRUSH) return;
    /* let it be stretched up to 1/2 screen size in area */
    if ((w<1)||(h<1)||(w*h >(screenBox.w*screenBox.h/2))) return;
    if (SaveBrush(STRETCHED)) return;
    ZZZCursor();
    newDim.w = w;
    newDim.h = h;
    if (StrBMOB(&origBr,&curbr,newDim.w,newDim.h)) RestoreBrush();
    else FixOffs();
    UnZZZCursor();
    }

void BrHalf()  { StrBrTo(curbr.pict.box.w/2, curbr.pict.box.h/2); }
void BrDubl()  { StrBrTo(curbr.pict.box.w*2, curbr.pict.box.h*2); }
void BrDublX() { StrBrTo(curbr.pict.box.w*2, curbr.pict.box.h); }
void BrDublY() { StrBrTo(curbr.pict.box.w, curbr.pict.box.h*2); }

/* ---------------Stretch brush----------------- */
    
Point bpl={0};
    
/* Begin stretching brush (button down) */

/* remember the upper left corner */
beginstr() {
    bpl.x = mx-curbr.xoffs;
    bpl.y = my-curbr.yoffs;
    }

#define oldW origBr.pict.box.w
#define oldH origBr.pict.box.h

LONG LongMult(a,b,c) LONG a,b,c; { return( a*b); }
    
local void doStretch() {
    newDim.w = mx-bpl.x + 1;
    newDim.h = my-bpl.y + 1;
    if  ( (newDim.w<=0) || (newDim.h<=0) ) return;
    if (wasShift) {
	if (LongMult(newDim.h,oldW) > LongMult(newDim.w,oldH)) 
	    newDim.w =   MulDiv(oldW,newDim.h,oldH);
	    else newDim.h = MulDiv(oldH,newDim.w,oldW);
	}
    if (StrBMOB(&origBr,&curbr,newDim.w,newDim.h))   {
#ifdef DBGSTRCH
	dprintf(" Can't make the brush that big! (w= %d, h = %d)\n",
			newDim.w, newDim.h); 
#endif
	ClearFB();
	RestoreBrush();
	killMode = nop;
	SetPaintMode(savePntMode);
	AbortIMode();
	return;
	} 
    curbr.xoffs = (mx - bpl.x);
    curbr.yoffs = (my - bpl.y);
    obmv();
    }

void IMStrBrush(restart) BOOL restart; {
    if (!restart) {
	if (curpen!=USERBRUSH) {RevertIMode(); return;}
	if (SaveBrush(STRETCHED)) {RevertIMode();  return;}
	/* hold at the lower right for stretching */
	curbr.xoffs = curbr.pict.box.w - 1;   
	curbr.yoffs = curbr.pict.box.h - 1;
	savePntMode = paintMode;
	SetPaintMode(Mask);
	}
    IModeProcs(&obmv,&obmv,&obclr,&beginstr,&obmv,
	&doStretch,&obclr, FixOffs);
    killMode = &FixOffs; 
    }

/*----------------------------------------------------------------------*/
/* << StrBits >>   Stretching BltBitMap					*/
/*  Does not handle negative stretches (use FlipX, FlipY first)		*/
/*  since X stretching is slower, order the X- and Y- stretching to     */
/*	minimize the X stretching 					*/
/*  									*/
/*----------------------------------------------------------------------*/

#undef sx
#undef sy

local SHORT sx,sy,sw,sh,dx,dy,dw,dh;
    
StrX(sbm,sbox,dbm,dbox,minterm)
    struct BitMap *sbm,*dbm;
    Box *sbox,*dbox;
    SHORT minterm;
    {
    SHORT xs,xd,xs0,xd0,xslast,s,dxmax;
    sy = sbox->y; dy = dbox->y; 
    sw = sbox->w-1; dw = dbox->w-1;    sh = sbox->h;
    dxmax = dbox->x + dw;
    xs0 = xs = xslast = sbox->x; xd0 = dbox->x;   
    s = dw/2; 
    for (xd = dbox->x+1 ; xd <= dxmax; xd++ ) {
	s += sw;
	while ( s >= dw ) { s -= dw; xs++; } 
	if (xs != xslast + 1 ) { 
	    BltBitMap(sbm,xs0,sy,dbm,xd0,dy,xd-xd0,sh,minterm,0xff,NULL);
	    xs0 = xs;    xd0 = xd;
	    }
	xslast = xs;
	}
    BltBitMap(sbm,xs0,sy,dbm,xd0,dy,xd-xd0,sh,minterm,0xff,NULL);   
    }

StrY(sbm,sbox,dbm,dbox,minterm)
    struct BitMap *sbm,*dbm;
    Box *sbox,*dbox;
    SHORT minterm;
    {
    SHORT ys,yd,ys0,yd0,yslast,s,dymax;
    sx = sbox->x; dx = dbox->x; 
    sh = sbox->h-1; dh = dbox->h -1;    sw = sbox->w;
    dymax = dbox->y + dh;
    ys0 = ys = yslast = sbox->y; yd0 = dbox->y; 
    s = dh/2;
    for (yd = dbox->y+1; yd <= dymax; yd++ ) {
	s += sh;
	while ( s >= dh ) { s -= dh; ys++; }
	if (ys != yslast + 1)  {
	    BltBitMap(sbm,sx,ys0,dbm,dx,yd0,sw,yd-yd0,minterm,0xff,NULL);
	    ys0 = ys;    yd0 = yd;
	    }
	yslast = ys;
	}
    BltBitMap(sbm,sx,ys0,dbm,dx,yd0,sw,yd-yd0,minterm,0xff,NULL);
    }

StrBits(srcBM,sbox,dstBM,dbox,mint) 
    struct BitMap *srcBM;     /* source bimap */
    Box *sbox;			/* source rectangle */
    struct BitMap  *dstBM;	/* destination bitmap */
    Box *dbox;			/* destination rectangle */
    SHORT mint;			/* minterm for blitter */
    {
    SHORT depth;
    Box tbox;
    struct BitMap tmpBM;
    if ( (sbox->w == dbox->w) && (sbox->h == dbox->h)) {
	BltBitMap(srcBM,sbox->x,sbox->y,dstBM,dbox->x,dbox->y,
		sbox->w,sbox->h,mint,0xff,NULL);
	return(0);
	}
    if ((dbox->h == 0)||(dbox->w == 0)) return(0);
    if ( dbox->w == sbox->w) {StrY(srcBM,sbox,dstBM,dbox,mint);	return(0); }
    if ( dbox->h == sbox->h) {StrX(srcBM,sbox,dstBM,dbox,mint);	return(0); }
    
    depth = dstBM->Depth;
    
    if (dbox->h > sbox->h) {   /*---  Getting Taller ---*/
	InitBitMap(&tmpBM,depth,dbox->w,sbox->h);
	if (TmpAllocBitMap(&tmpBM)) return(FAIL);
	tbox.x = 0; tbox.y = 0; tbox.w = dbox->w; tbox.h = sbox->h;
	StrX(srcBM, sbox, &tmpBM, &tbox, REPOP);
	StrY(&tmpBM, &tbox, dstBM, dbox, mint);
	}
    else {         /*----  Getting Shorter ----*/ 
	InitBitMap(&tmpBM,depth,sbox->w,dbox->h);
	if (TmpAllocBitMap(&tmpBM)) return(FAIL);
	tbox.x = 0; tbox.y = 0; tbox.w = sbox->w; tbox.h = dbox->h;
	StrY(srcBM, sbox, &tmpBM, &tbox, REPOP);
	StrX(&tmpBM, &tbox, dstBM, dbox, mint);
	}
    FreeBitMap(&tmpBM);
    return(SUCCESS);
    }

static SHORT dum;
static SHORT dum1 = 2;