-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathACKBBM.C
232 lines (202 loc) · 6.68 KB
/
ACKBBM.C
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
// This source file contains the functions needed to read in BBM files.
// (c) 1995 ACK Software (Lary Myers)
//=============================================================================
// This function will return a pointer to a buffer that holds the raw image.
// just free the pointer to delete this buffer. After returning, the array
// colordat will hold the adjusted palette of this pic.
//
// Also, this has been modified to only read in form PBM brushes. form ILBM's
// (the "old" type) are not supported. use the "new" deluxe paint .lbm type
// and do not choose "old".
//=============================================================================
//#include <windows.h>
#include "windowsredef.h"
#include <stdio.h>
//#include <conio.h>
//#include <process.h>
//#include <bios.h>
//#include <fcntl.h>
#include <malloc.h>
//#include <mem.h>
#include "ACK3D.H"
#include "ACKENG.H"
#include "ACKEXT.H"
#include "BBM.H"
extern int errno;
extern char rsName[];
unsigned char colordat[768]; // maximum it can be...256 colors
unsigned char cplanes[8][80]; // setting max at 640 pixels width
// thats 8 pixels per byte per plane
unsigned char *pplanes= &cplanes[0][0]; // for a form pbm
#define MAX_BUF_POS 4096
int rdbufpos;
int rdSize;
UCHAR rdBuffer[MAX_BUF_POS+2];
//=============================================================================
// MEH I can't seem to see this being used anywhere...
//=============================================================================
/*void CloseFile(FILE *fp)
{
fclose(fp);
if (rsHandle)
{
rsHandle = _lopen(rsName,OF_READ);
if (rsHandle < 1)
rsHandle = 0;
}
}*/
unsigned char *AckReadBBM(char *picname)
{
// FILE *pic;
short handle;
form_chunk fchunk;
ChunkHeader chunk;
BitMapHeader bmhd;
// int32_t length,fpos;
char value; // must remain signed, no matter what. ignore any warnings.
short sofar;
short height;
// short pixw;
unsigned char *destx, *savedestx;
rdbufpos = MAX_BUF_POS + 1;
rdSize = MAX_BUF_POS;
if (!rsHandle)
handle = _lopen(picname,OF_READ);
else
{
handle = rsHandle;
_llseek(rsHandle,(int)(rbaTable[(int64_t)(picname)]),SEEK_SET);
}
_lread(handle,&fchunk,sizeof(form_chunk));
if (fchunk.type != FORM)
{
if (!rsHandle)
_lclose(handle);
ErrorCode = ERR_INVALIDFORM;
return(0L);
}
if (fchunk.subtype != ID_PBM)
{
if (!rsHandle)
_lclose(handle);
ErrorCode = ERR_NOPBM;
return(0L);
}
// now lets loop...Because the Chunks can be in any order!
while(1)
{
_lread(handle,&chunk,sizeof(ChunkHeader));
chunk.ckSize = ByteFlipLong(chunk.ckSize);
if (chunk.ckSize & 1) chunk.ckSize ++; // must be word aligned
if(chunk.ckID == ID_BMHD)
{
_lread(handle,&bmhd,sizeof(BitMapHeader));
bmhd.w=iffswab(bmhd.w); // the only things we need.
bmhd.h=iffswab(bmhd.h);
destx = (unsigned char *)AckMalloc((bmhd.w * bmhd.h)+4);
if ( !destx )
{
if (!rsHandle)
_lclose(handle);
ErrorCode = ERR_NOMEMORY;
return(0L);
}
savedestx = destx;
destx[0] = bmhd.w%256;
destx[1] = bmhd.w/256;
destx[2] = bmhd.h%256;
destx[3] = bmhd.h/256;
destx += 4;
continue;
}
if(chunk.ckID == ID_CMAP)
{
short i;
unsigned char r,g;
_lread(handle,colordat,chunk.ckSize);
for (i=0;i<768;i++)
{
r = colordat[i]; // r,g do not stand for red and green
g = r >> 2;
colordat[i] = g;
}
continue;
}
if(chunk.ckID == ID_BODY)
{
for(height = 0; height<bmhd.h; height ++)
{
unsigned char *dest;
dest = (unsigned char *)&(pplanes[0]); // point at first char
sofar = bmhd.w; // number of bytes = 8
if (sofar&1) sofar++;
while (sofar)
{
if (bmhd.compression)
{
value = 0;
_lread(handle,&value,1);
if (value > 0)
{
short len;
len = value +1;
sofar -= len;
if (!(_lread(handle,dest,len)))
{
if (!rsHandle)
_lclose(handle);
ErrorCode = ERR_BADPICFILE;
AckFree(savedestx);
return(0L);
}
dest +=len;
}
else
{
short count;
count = -value; // get amount to dup
count ++;
sofar -= count;
value = 0;
_lread(handle,&value,1);
while (--count >= 0) *dest++ = value;
}
}
else
{
_lread(handle,dest,sofar);
sofar = 0;
}
}
if (sofar < 0)
{
if (!rsHandle)
_lclose(handle);
}
memcpy(destx,pplanes,bmhd.w);
destx += bmhd.w;
}
break; // leave if we've unpacked the BODY
}
_llseek(handle,(int)(chunk.ckSize),SEEK_CUR);
}
if (!rsHandle)
_lclose(handle);
return((unsigned char *)savedestx);
}
int32_t ByteFlipLong(int32_t NUMBER)
{
int32_t Y, T;
short I;
T = NUMBER;
Y=0;for (I=0;I<4;I++){Y = Y | (T & 0xFF);if (I<3) {Y = Y << 8;T = T >> 8;}}
return(Y);
}
short iffswab(unsigned short number)
{
unsigned short xx1,xx2;
unsigned short result;
xx1 = number <<8; xx2 = number >>8; result = xx1|xx2;
return(result);
}
// **** End of Source ****