-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathBitmap.cpp
484 lines (379 loc) · 10.4 KB
/
Bitmap.cpp
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
/*
Pixcen - A windows platform low level pixel editor for C64
Copyright (C) 2013 John Hammarberg ([email protected])
This file is part of Pixcen.
Pixcen is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Pixcen is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Pixcen. If not, see <http://www.gnu.org/licenses/>.
*/
#include "StdAfx.h"
#include "C64Interface.h"
Bitmap::Bitmap(int x, int y, int backbuffers)
{
mode = BITMAP;
if(x%8!=0)throw _T("X must be divisible by 8");
if(y%8!=0)throw _T("Y must be divisible by 8");
xsize=x;
ysize=y;
xcell=8;
ycell=8;
Create(y * (x/8), 0, (y/8) * (x/8), backbuffers);
infouse[0].use = InfoUse::INFO_INDEX_LOW;
infouse[0].pp = &screen;
infouse[1].use = InfoUse::INFO_INDEX_HIGH;
infouse[1].pp = &screen;
DuplicateGlobalsToBackBuffers();
}
Bitmap::~Bitmap()
{
}
void Bitmap::GetSaveFormats(narray<autoptr<SaveFormat>,int> &fmt)
{
__super::GetSaveFormats(fmt);
if(xsize==320 && ysize == 200)
{
fmt.add(new SaveFormat(_T("Art Studio"),_T("art"),true));
fmt.add(new SaveFormat(_T("Doodle"),_T("dd"),true));
fmt.add(new SaveFormat(_T("Doodle compressed"),_T("jj"),true));
fmt.add(new SaveFormat(_T("C64 Exe"),_T("prg"),false));
fmt.add(new SaveFormat(_T("Multipaint"), _T("bin"), true));
}
}
void Bitmap::GetLoadFormats(narray<autoptr<SaveFormat>,int> &fmt)
{
fmt.add(new SaveFormat(_T("Art Studio"),_T("art"),true,320,200,BITMAP));
fmt.add(new SaveFormat(_T("Doodle"),_T("dd;ddl;jj"),true,320,200,BITMAP));
fmt.add(new SaveFormat(_T("Multipaint"), _T("bin;binhi"), true, 320, 200, BITMAP));
}
nstr Bitmap::IdentifyFile(nmemfile &file)
{
nstr ex;
if (file.len() == 88000)
{
BYTE *ptr = file;
static const BYTE cmp[] = { 0xFF,0x00,0x00,0x0F,0x28,0x00,0x19 };
if (!memcmp(ptr + 1, cmp, 7))
{
ex = "binhi";
return ex;
}
}
unsigned short addr;
file >> addr;
if((file.len() == 9002 || file.len() == 9009) && addr == 0x2000)
{
ex = _T("art");
}
else if(file.len() == 9218 && (addr == 0x2000 || addr == 0x1c00))
{
ex = _T("dd");
}
else if(addr == 0x2000)
{
BYTE buffer[9216];
//Compressed doodle uses same RLE as compressed Koala
if(MCBitmap::DecompressKoalaStream(((BYTE *)file)+2, int(file.len()-2), buffer, 9216) == 9216)
{
ex = _T("jj");
}
}
return ex;
}
void Bitmap::Load(nmemfile &file, LPCTSTR type, int version)
{
__super::Load(file,type,version);
if(lstrcmpi(_T("art"),type)==0)
{
if(file.len() != 9002 && file.len() != 9009)
{
throw _T("Invalid Art Studio file size");
}
unsigned short addr;
file >> addr;
file.read(map, 8000);
file.read(screen, 1000);
*border = GuessBorderColor();
}
else if(lstrcmpi(_T("dd"),type)==0)
{
if(file.len() != 9218)
{
throw _T("Invalid Doodle file size");
}
unsigned short addr;
file >> addr;
file.read(screen, 1000);
file.read(24);
file.read(map, 8000);
*border = GuessBorderColor();
}
else if(lstrcmpi(_T("jj"),type)==0)
{
BYTE buffer[9216]; //Decompress will skip PRG header
if(MCBitmap::DecompressKoalaStream(((BYTE *)file)+2, int(file.len()-2), buffer, 9216)!=9216)
{
throw _T("Invalid Doodle stream");
}
memcpy(screen, buffer, 1000);
memcpy(map, buffer+1024, 8000);
*border = GuessBorderColor();
}
else if (lstrcmpi(_T("binhi"), type) == 0)
{
if (file.len() != 88000)
{
throw _T("Invalid Multipaint file size");
}
file.read(border, 1);
// Skip to the bitmap
file.setpos(0x0400);
BYTE b;
for (int y = 0; y < 200; y++)
{
for (int x = 0; x < 40; x++)
{
int mapindex = (y / 8) * 320 + y % 8 + x * 8;
BYTE tmp = 0;
for (int bit = 7; bit >= 0; bit--)
{
file >> b;
tmp |= b << bit;
}
map[mapindex] = tmp;
}
}
// Skip to the screen and color data
file.setpos(0x10000);
for (int y = 0; y < 25; y++)
{
for (int x = 0; x < 40; x++)
{
file >> b;
screen[y*40+x] = b << 4;
}
//Skip 7 lines of duplicate data (space for FLI?)
file.read(40 * 7);
}
for (int y = 0; y < 25; y++)
{
for (int x = 0; x < 40; x++)
{
file >> b;
screen[y * 40 + x] |= b;
}
//Skip 7 lines of duplicate data (space for FLI?)
file.read(40 * 7);
}
}
else if(lstrcmpi(_T("raw"),type)==0)
{
size_t len = file.len();
if(len % 8 != 0)
{
//Throw PRG header
unsigned short tmp;
file >> tmp;
len -= 2;
}
xsize = 40*8;
ysize = int(((len+319)/320)*8);
Destroy();
Create(ysize * (xsize/8), 0, (ysize/8) * (xsize/8));
memset(screen,0xbc,(xsize/8) * (ysize/8));
file.read(map, len);
}
}
void Bitmap::Save(nmemfile &file, LPCTSTR type)
{
__super::Save(file, type);
if (lstrcmpi(_T("art"), type) == 0)
{
if (xsize != 320 || ysize != 200)
throw _T("Buffers are not in standard hires format");
file << unsigned short(0x2000);
file.write(map, 8000);
file.write(screen, 1000);
}
else if (lstrcmpi(_T("dd"), type) == 0)
{
if (xsize != 320 || ysize != 200)
throw _T("Buffers are not in standard hires format");
file << unsigned short(0x5c00);
file.write(screen, 1000);
for (int r = 0; r < 24; r++)file << BYTE(0);
file.write(map, 8000);
for (int r = 0; r < 192; r++)file << BYTE(0);
}
else if (lstrcmpi(_T("jj"), type) == 0)
{
if (xsize != 320 || ysize != 200)
throw _T("Buffers are not in standard hires format");
BYTE src[9216];
ZeroMemory(src, sizeof(src));
memcpy(src, screen, 1000);
memcpy(src + 1024, map, 8000);
BYTE buffer[9216 * 3];
int size = MCBitmap::CompressKoalaStream(src, 9216, buffer, 9216 * 3);
if (size == -1)
{
throw _T("Failed compressing Doodle");
}
file << unsigned short(0x5c00);
file.write(buffer, size);
}
else if (lstrcmpi(_T("prg"), type) == 0)
{
if (xsize != 320 || ysize != 200)
throw _T("Buffers are not in standard hires format");
static unsigned char viewer[] = {
0x00,0x1f,0x78,0xa9,0x3b,0x8d,0x11,0xd0,0xa9,0x08,0x8d,0x16,0xd0,0xa9,0x18,0x8d,
0x18,0xd0,0xa2,0x00,0xbd,0x40,0x3f,0x9d,0x00,0x04,0xbd,0x40,0x40,0x9d,0x00,0x05,
0xbd,0x40,0x41,0x9d,0x00,0x06,0xbd,0x40,0x42,0x9d,0x00,0x07,0xe8,0xd0,0xe5,0xad,
0x28,0x43,0x8d,0x20,0xd0,0x4c,0x33,0x1f };
file.write(viewer, sizeof(viewer));
int pad = sizeof(viewer) - 2; //minus prg header
while (pad < 256)
{
file << BYTE(0);
pad++;
}
file.write(map, 8000);
file.write(screen, 1000);
file << *border;
B2File bbin, bbout;
bbin.size = file.len();
bbin.data = (byte *)file.detach();
B2Crunch(&bbin, &bbout, 0x1f00);
B2FreeFile(&bbin);
file.attach(bbout.data, bbout.size, true);
}
else if (lstrcmpi(_T("bin"), type) == 0)
{
if (xsize != 320 || ysize != 200)
throw _T("Buffers are not in standard hires format");
file << *border;
//Just to not confuse Multipaint in any way, write all unidentified metadata (some are settings) produced by Multipaint
static const BYTE head[] = { 0xFF,0x00,0x00,0x0F,0x28,0x00,0x19 };
file.write(head, sizeof(head));
for (int r = 0; r < 4; r++)
file << BYTE(0);
file << BYTE(3);
while (file.getpos() < 0x103)
file << BYTE(0);
static const BYTE meta[] = { 0xFF,0xFF,0xFF,0x68,0x37,0x2B,0x70,0xA4,0xB2,0x6F,0x3D,0x86,0x58,0x8D,0x43,0x35,0x28,0x79,0xB8,0xC7,0x6F,0x6F,0x4F,0x25,0x43,0x39,0x00,0x9A,0x67,0x59,0x44,0x44,0x44,0x6C,0x6C,0x6C,0x9A,0xD2,0x84,0x6C,0x5E,0xB5,0x95,0x95,0x95 };
file.write(meta, sizeof(meta));
while (file.getpos() < 0x400)
file << BYTE(0);
//Write bitmap
for (int y = 0; y < 200; y++)
{
for (int x = 0; x < 40; x++)
{
int mapindex = (y / 8) * 320 + y % 8 + x * 8;
BYTE tmp = map[mapindex];
for (int bit = 7; bit >= 0; bit--)
{
file << BYTE((tmp >> bit) & 1);
}
}
}
// Fill up to screen and color data
while (file.getpos() < 0x10000)
file << BYTE(0);
for (int y = 0; y < 25; y++)
{
for (int l = 0; l < 8; l++)
{
for (int x = 0; x < 40; x++)
{
file << BYTE(screen[y * 40 + x] >> 4);
}
}
}
for (int y = 0; y < 25; y++)
{
for (int l = 0; l < 8; l++)
{
for (int x = 0; x < 40; x++)
{
file << BYTE(screen[y * 40 + x] & 0xf);
}
}
}
// Fill up to end
while (file.getpos() < 88000)
file << BYTE(0);
}
}
BYTE Bitmap::GetPixelInternal(int x, int y)
{
ASSERT(x>=0 && x<xsize && y>=0 && y<ysize);
int cx = x/8;
int cy = y/8;
int d=(map[cy * xsize + cx * 8 + (y%8)] >> (1*(7-(x%8)))) & 1;
BYTE b;
switch(d)
{
case 0:
b = screen[cy * (xsize/8) + cx] & 0x0f;
break;
case 1:
b = screen[cy * (xsize/8) + cx] >> 4;
break;
}
return b;
}
BYTE Bitmap::GetPixel(int x, int y)
{
return GetPixelInternal(x, y);
}
void Bitmap::SetPixel(int x, int y, BYTE col)
{
ASSERT(x>=0 && x<xsize && y>=0 && y<ysize);
int cx = x/8;
int cy = y/8;
int ci = cy * (xsize/8) + cx;
int mask = ResolveMask1(ci, col, GetMask(x,y));
if(mask == -1)
return;
SetColor(ci, mask, col);
int mi = cy * xsize + cx * 8 + (y%8);
int filtermask = ~(1 << (1*(7-(x%8))));
mask = mask << (1*(7-(x%8)));
map[mi] = (map[mi] & filtermask) | mask;
}
void Bitmap::Import(CImage &img)
{
ClearBackBuffer();
{
ImportHelper help(this, img, false);
help.ReduceColors(2, NULL, 0);
}
*border = GuessBorderColor();
}
int Bitmap::GetMask(int x, int y)
{
int cx = x/8;
int cy = y/8;
return (map[cy * xsize + cx * 8 + (y%8)] >> (1*(7-(x%8)))) & 1;
}
void Bitmap::SwapCellColours(int cx, int cy)
{
int mi = GetMapIndexFromCell(cx, cy), t;
for (t = 0; t < sizecell; t++)
map[mi + t] = map[mi + t] ^ 0xFF; // invert it
CellInfo info;
GetCellInfo(cx, cy, 1, 1, &info);
int oldColour0 = info.col[0];
int oldColour1 = info.col[1];
int ci = cy * (xsize / 8) + cx;
SetColor(ci, 0, oldColour1);
SetColor(ci, 1, oldColour0);
}