aboutsummaryrefslogtreecommitdiff
path: root/DPIO.C
blob: b62677bb52676c7f6a41bdca216af343543be168 (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
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
/*----------------------------------------------------------------------*/
/*									*/
/*		dpio.c -- Picture and Brush IO				*/
/*									*/
/*----------------------------------------------------------------------*/

#include <system.h>
#include <prism.h>
#include "librarie\dos.h" /* for file io */
#include <dpiff.h>
#include <fnreq.h>

extern struct Window *mainW;
extern SHORT nColors;
extern struct TmpRas tmpRas;
extern SHORT  xShft, yShft, nColors;
extern struct ViewPort *vport;
extern BMOB curbr;
extern struct BitMap hidbm;
extern Box screenBox;
extern BOOL skipRefresh;
extern SHORT curFormat,curxpc,curpen,curDepth;
extern SHORT LoadBrColors[], prevColors[];
extern Range cycles[];
    
UBYTE loadStr[] = "Load";
UBYTE saveStr[] = "Save";

#define local static


BOOL CombPathName(comb,path,finame) UBYTE *comb,*path,*finame; {
    if (strlen(finame) <= 0) return(NO);
    strcpy(comb,path);
    ConcatPath(comb,finame);
    return(YES);
    }

BOOL DoFnReq(title,doname,path,name) UBYTE *title,*doname,*path,*name; {
    BOOL res;
    UndoSave();
    FreeTmpRas(); /* make room for Intuition to save screen under req*/
    res = (BOOL)FN_Request(mainW,PMapX(20),PMapY(24),
				path,name,NULL,title,NULL, doname);
    AllocTmpRas();
    magRefresh();
    mainRefresh();
    return(res);
    }

/*----------------------------------------------------------------------*/
/*  Picture								*/
/*----------------------------------------------------------------------*/
extern UBYTE picpath[31];
extern UBYTE picname[31];
    
ZapPicName() { picname[0] = 0; }

BOOL MakePicName(nm) UBYTE nm[]; {return(CombPathName(nm,picpath, picname));}

BOOL DoPicFnReq(title,doname) UBYTE *title, *doname;{
    return(DoFnReq(title,doname,picpath,picname)); 
    }

    
/* ILBM IO */

/* try to resize the pic to fit the one being read in */
local BOOL badFit;
BOOL ResizePic(mbm,bmhdr) MaskBM *mbm; BitMapHeader *bmhdr; {
    if ((bmhdr->h == hidbm.Rows)&&(bmhdr->w == hidbm.BytesPerRow*8))
	{
	if (bmhdr->nPlanes< hidbm.Depth) ClearBitMap(&hidbm);
	return(SUCCESS);
	}
    else return(FAIL);
    }

NoOpen(name) char *name; { InfoMessage(" Couldn't Open file:",name); }
    
NoSave(name) char *name; { InfoMessage(" Couldn't Save to file:",name); }
    
LoadPicNamed(name) char *name; {
    LONG file;
    MaskBM mbm;
    WORD colors[32];
    file = Open(name, MODE_OLDFILE);
    if (file==0) { NoOpen(name); picname[0]=0; return(FAIL);}
    ZZZCursor();
    GetColors(colors); /* default values if fewer planes loaded */
    mbm.flags = MBM_HAS_CMAP;
    mbm.bitmap = &hidbm;
    mbm.ranges = cycles;
    mbm.nRange = MAXNCYCS;
    badFit = NO;
    if (SUCCESS == GetMaskBM(file, &mbm, colors,
			 &ResizePic,tmpRas.RasPtr, tmpRas.Size)) {
	KillCCyc();
	GetColors(prevColors);
	LoadRGB4(vport,colors,nColors);
	}
    else {
	if (IffErr()==BAD_FIT) InfoMessage(" Wrong format:", name);
	else InfoMessage(" Couldn't load file",name);
	ZapPicName();
	}
    Close(file);
    UnZZZCursor();
    }

local BYTE xAspRat[3] = {x320x200Aspect,x640x200Aspect,x640x400Aspect};
local BYTE yAspRat[3] = {y320x200Aspect,y640x200Aspect,y640x400Aspect};

BOOL SavePicNamed(name) char *name; {
    LONG file;
    WORD colors[32];
    BOOL res;
    MaskBM mbm;
    GetColors(colors);
    ZZZCursor();
    file = Open(name, MODE_NEWFILE);
    if (file==0) { NoSave(name); return(FAIL); }
    mbm.flags = MBM_HAS_CMAP|MBM_HAS_RANGES;
    mbm.nRange = MAXNCYCS;
    mbm.ranges = cycles;
    mbm.bitmap = &hidbm;
    mbm.w = hidbm.BytesPerRow*8;
    mbm.mask = NULL;
    mbm.masking = mskHasTransparentColor;
    mbm.xpcolor = curxpc;
    mbm.pos.x = mbm.pos.y = 0;
    res  = PutMaskBM(file, &mbm, colors, (UBYTE *)tmpRas.RasPtr, tmpRas.Size);
    if (res) {
	InfoMessage(" Couldn't save file",name);
	ZapPicName();
	}
    else WriteProjectIcon(picpath,picname);
    Close(file);
    UnZZZCursor();
    return(res);
    }

ChkFNRes(res) BOOL res; {
    if (res== FN_NoSuchDrawer) {
	InfoMessage(" Non existent drawer","");
	}
    }

#ifdef DOWB
LoadPic() {
    UBYTE nm[60];
    OffRefrAfterOv();
    if (MakePicName(nm)) {
	LoadPicNamed(nm);
	Str(picname);
	}
    OnRefrAfterOv();
    PaneRefrAll();
    }
#endif
    
OpenPic() {  UBYTE nm[60];
    BOOL res;
    InitRead(); /* bring in the reader overlay*/
    res  = DoPicFnReq("Load Picture",loadStr);
    if (res == FN_OK) {
	if (MakePicName(nm)) {
	    LoadPicNamed(nm);
	    StuffSaveStr(picname);
	    }
	}
    else ChkFNRes(res);
    PaneRefrAll();
    }

local UBYTE bkdotpic[] = "backup.pic";

extern BOOL largeMemory;
    

void SaveCurPic() {
    UBYTE nm[60],bkupname[60];  
    LONG lock;
    LONG err;
    if (MakePicName(nm)) {
	lock = Lock(nm, ACCESS_WRITE);
	if (lock) {
	    CombPathName(bkupname,picpath,bkdotpic);
	    UnLock(lock);
	    if (DeleteFile(bkupname) == 0) {
		err = IoErr();
		if (err != ERROR_OBJECT_NOT_FOUND)    return;
		}
	    if (Rename(nm,bkupname) == 0) return;
	    }
	SavePicNamed(nm);   
	StuffSaveStr(picname);
	}
    else InfoMessage("Invalid File name", "");
    }

SavePicAs() {
    BOOL res;
    InitWrite(); /* bring in the writer overlay*/
    res = DoPicFnReq("Save Picture", saveStr);
    if (res == FN_OK)  SaveCurPic();
    else ChkFNRes(res);
    PaneRefrAll();
    }

SavePic() {  
    InitWrite(); /* bring in the writer overlay*/
    UndoSave();
	if (strlen(picname) == 0) SavePicAs();
    else {
	if (!largeMemory) InfoMessage(" Insert data disk","in disk drive");
	SaveCurPic();
	}
    PaneRefrAll();
    }

/*----------------------------------------------------------------------*/
/*  Brush IO								*/
/*----------------------------------------------------------------------*/
extern UBYTE brspath[31];
extern UBYTE brsname[31];

ZapBrushName() { brsname[0] = 0; }
BOOL MakeBrsName(nm) UBYTE nm[]; {return(CombPathName(nm,brspath, brsname));}
BOOL DoBrsFnReq(title,doname) UBYTE *title,*doname;{return(DoFnReq(title,doname,brspath,
	brsname));}


SaveBrsNamed(name) char *name; {
    LONG file; 
    BOOL res;
    WORD colors[32];
    MaskBM mbm;
    GetColors(colors);
    ZZZCursor();
    file = Open(name, MODE_NEWFILE);
    if (file==0) { NoSave(name); return(FAIL); }
    mbm.flags = MBM_HAS_CMAP|MBM_HAS_GRAB;
    mbm.bitmap = curbr.pict.bm;
    mbm.w = curbr.pict.box.w;
    mbm.masking = mskHasTransparentColor;
    mbm.mask = NULL;
    mbm.xpcolor = curbr.xpcolor;
    mbm.pos.x = mbm.pos.y = 0;
    mbm.grab.x = curbr.xoffs;
    mbm.grab.y = curbr.yoffs;
    res = PutMaskBM(file, &mbm, colors,(UBYTE *)tmpRas.RasPtr,tmpRas.Size);
    if (res) { brsname[0] = 0; InfoMessage(" Couldn't save file",name);}
    else WriteBrushIcon(brspath,brsname);
    Close(file);
    UnZZZCursor();
    }

/* try to resize the brush to fit the one being read in */

local BOOL cantFit;

#ifdef SIMPLEWAY
BOOL ResizeBrush(mbm,bmhdr) MaskBM *mbm; BitMapHeader *bmhdr; {
    if (BMOBNewSize(&curbr, bmhdr->nPlanes, bmhdr->w,bmhdr->h)) {
	cantFit = YES;
	return(FAIL);
	}
    ClearBitMap(curbr.pict.bm);
    mbm->mask = curbr.mask;
    return(SUCCESS);
    }

#else
BOOL ResizeBrush(mbm,bmhdr) MaskBM *mbm; BitMapHeader *bmhdr; {
    SHORT depth = bmhdr->nPlanes;
    do  {if (!BMOBNewSize(&curbr, depth, bmhdr->w, bmhdr->h)) 
	   goto alldone;
	} while ((--depth)>=curDepth);
    cantFit = YES;
    return(FAIL);
    alldone:
	ClearBitMap(curbr.pict.bm);
	mbm->mask = curbr.mask;
	return(SUCCESS);
    }
#endif

LoadBrsNamed(name) char *name; {
    LONG file;
    MaskBM mbm;
    file = Open(name, MODE_OLDFILE);
    if (file==0) { NoOpen(name); brsname[0] = 0; return(FAIL); }
    ZZZCursor();
    mbm.flags = 0;
    mbm.bitmap = curbr.pict.bm;
    mbm.mask = curbr.mask;
    GetColors(LoadBrColors); /* default values if fewer planes loaded */
    RestoreBrush();
    cantFit = NO;
    if (SUCCESS == GetMaskBM(file, &mbm, LoadBrColors,
			 &ResizeBrush,tmpRas.RasPtr, tmpRas.Size))
	{
	if (mbm.flags&MBM_HAS_GRAB) {
	    curbr.xoffs = mbm.grab.x;
	    curbr.yoffs = mbm.grab.y; 
	    }
	else {
	    curbr.xoffs = curbr.pict.bm->BytesPerRow*4;
	    curbr.yoffs = curbr.pict.bm->Rows/2;
	    }
	curbr.xpcolor = mbm.xpcolor;
	if (mbm.masking == (UBYTE)mskHasTransparentColor) BMOBMask(&curbr);
	UserBr();
	}
    else { 
	InfoMessage(" Couldn't load brush",name); 
	if (cantFit) {
	    SelPen(0); 
	    SetNextIMode(IM_draw); 
	    }
	else ZapBrushName(); 
	}
    Close(file);
    UnZZZCursor();
    }
    
GetBrs() { BOOL res;
    UBYTE nm[60];
    InitRead(); /* bring in the reader overlay*/
    res = DoBrsFnReq("Load Brush",loadStr);
    if (res==FN_OK) {if (MakeBrsName(nm)) LoadBrsNamed(nm);}
    else ChkFNRes(res);
    PaneRefrAll();
    }

void SaveBrsAs() {  UBYTE nm[60];
    BOOL res;
    InitWrite(); /* bring in the writer overlay*/
    if (curpen!=USERBRUSH) {
	InfoMessage(" Cant save Built-in", "   brushes ");
	}
    else {
	res = DoBrsFnReq("Save Brush", saveStr);
	if (res == FN_OK) {if (MakeBrsName(nm)) SaveBrsNamed(nm);}
	else ChkFNRes(res);
	}
     PaneRefrAll();
    }