-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathC64Interface.h
753 lines (564 loc) · 17.8 KB
/
C64Interface.h
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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
/*
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/>.
*/
#pragma once
#include "atlimage.h"
#include "ImageFast.h"
#include "C64Col.h"
#include "narray.h"
#include "nstr.h"
#include "nmemfile.h"
#include "autoptr.h"
#include "b2wrap.h"
#include <map>
#define CURRENT_GPX_FILE_VERSION 4
class SaveFormat
{
public:
SaveFormat(nstr a, nstr b, bool gd, int w=0, int h=0, int typ=-1)
{
name = a;
good = gd;
size_t n;
while((n=b.find(_T(';'))) !=-1)
{
ext.add(b.left(n));
b.cut(0,n+1);
}
ext.add(b);
type=typ;
width = w;
height = h;
}
nstr name;
narray<nstr,int> ext;
bool good;
int width, height;
int type;
bool MatchExt(LPCTSTR ex)
{
for(int r=0;r<ext.count();r++)
{
if(!ext[r].cmpi(ex))
return true;
}
return false;
}
};
class CellInfo
{
public:
int w;
int h;
BYTE col[6];
int lock[6];
int crippled[6];
CellInfo()
{
w=-1;
h=-1;
for(int r=0;r<sizeof(col);r++)
{
col[r]=0xff;
crippled[r]=lock[r]=-1;
}
}
};
class C64Interface
{
public:
typedef enum
{
BITMAP,
MC_BITMAP,
SPRITE,
MC_SPRITE,
CHAR,
MC_CHAR,
UNUSED1,
UNUSED2,
UNRESTRICTED,
W_UNRESTRICTED
}tmode;
typedef enum
{
REPLACE,
NOTHING,
CLOSEST
}toverflow;
C64Interface();
virtual ~C64Interface();
// Must overload
virtual BYTE GetPixel(int x, int y) = NULL;
virtual BYTE GetPixelColourMap(int x, int y) = NULL;
virtual void SetPixel(int x, int y, BYTE color) = NULL;
//Should overload
virtual void GetCellInfo(int cx, int cy, int w, int h, CellInfo *info);
virtual void SetCellInfo(int cx, int cy, int w, int h, CellInfo *info);
virtual int GetPixelWidth(void){return 1;};
virtual void GetSaveFormats(narray<autoptr<SaveFormat>,int> &fmt);
virtual bool CanOptimize(void){return false;}
virtual int GetCharCount(void){return 0;}
static void GetLoadFormats(narray<autoptr<SaveFormat>,int> &fmt);
static nstr IdentifyFile(nmemfile &file){return _T("");};
virtual void CustomCommand(int n){}
virtual void RemapCellColour(int cx, int cy, int colour, int indexTo) {};
virtual void SwapCellColours(int cx, int cy) {};
//protected:
virtual void Import(CImage &img){}
virtual void Save(nmemfile &file, LPCTSTR type);
virtual void Load(nmemfile &file, LPCTSTR type, int version);
virtual void SetFontDisplay(int mode) {}
virtual int GetFontDisplay(void){return 0;}
public:
int GetSizeX(void){return xsize;}
int GetSizeY(void){return ysize;}
int GetCellSizeX(void){return xcell;}
int GetCellSizeY(void){return ycell;}
int GetCellCountX(void){return xsize/xcell;}
int GetCellCountY(void){return ysize/ycell;}
BYTE GetBackground(void){return infouse[0].use != InfoUse::INFO_NO ? *infouse[0].pp[0] : 0;}
int GuessBorderColor(void);
BYTE GetBorderColor(void){return *border;};
COLORREF GetPixelCR(int x, int y){return g_Vic2[GetPixel(x,y)];}
void BeginHistory(void);
void EndHistory(void);
void InheritHistory(C64Interface *old);
void DeleteHistory(void);
bool CanUndo(void){return history_pos > 0 || history_previous;}
bool CanRedo(void){return history_pos < history_redo.count() || history_next;}
C64Interface *Undo(void);
C64Interface *Redo(void);
bool IsDirty(void){return dirty;}
void SetDirty(void){dirty=true;}
void ClearDirty(void){dirty=false;}
LPCTSTR GetFileName(void){return file_name;}
void SetFileName(LPCTSTR s){file_name=s;}
nstr GetFileExt(void)
{
size_t n=file_name.rfind(_T('.'));
if(n==-1)return _T("");
return file_name.mid(n+1);
}
void Save(LPCTSTR pszFileName, LPCTSTR type);
static nstr IdentifyFile(LPCTSTR pszFileName);
static C64Interface *Load(LPCTSTR pszFileName, LPCTSTR type, tmode importmode, int width, int height);
static C64Interface *CreateFromImage(CImage *img, int count, tmode type);
static C64Interface *Allocate(tmode mode, int w, int h, int buffers=1);
virtual C64Interface *CreateFromSelection(int x, int y, int w, int h);
void Optimize(void);
bool IsBitmap(void){return mode == BITMAP || mode == MC_BITMAP;}
bool IsSprite(void){return mode == SPRITE || mode == MC_SPRITE;}
bool IsChar(void){return mode == CHAR || mode == MC_CHAR;}
bool IsMultiColor(void){return mode == MC_CHAR || mode == MC_SPRITE || mode == MC_BITMAP || mode == W_UNRESTRICTED;}
bool IsSingleColor(void){return !IsMultiColor();}
bool IsUnrestricted(void){return mode == W_UNRESTRICTED || mode == UNRESTRICTED;}
tmode GetMode(void){return mode;}
void RenderImage(CImage &img, int startx=0, int starty=0, int width=-1, int height=-1);
void RenderColourUsageImage(CImage & inimg, int startx=0, int starty=0, int width=-1, int height=-1);
void SetBackBuffer(int n);
int GetBackBuffer(void){return rbackbufnum;}
int GetBackBufferCount(void){return rbackbuffers;}
void ClearBackBuffer(void);
int GetPAR(void){return par;}
double GetPARValue(void){return parvalue[par];}
void SetPAR(int n){par=n;}
toverflow GetOverflow(void){return overflow;}
void SetOverflow(toverflow n){overflow = n;}
void SetMetaInt(LPCSTR tag, int value){WCHAR buf[32];_itow(value, buf, 10);meta[tag]=buf;}
int GetMetaInt(LPCSTR tag){return _wtoi(meta[tag]);}
void SetMetaStr(LPCSTR tag, nstr value){meta[tag]=value;}
nstr GetMetaStr(LPCSTR tag){return meta[tag];}
//Specialized threaded for, for cell boundary
template<typename Function>
void paralell_for_ycell(int from, int to, Function &&fn)
{
int csy = GetCellSizeY();
int afrom = (from % csy) ? from + (csy - (from % csy)) : from;
if (afrom >= to)
{
while (from < to)
{
fn(from);
from++;
}
return;
}
int ato = (to % csy) ? to - (to % csy) : to;
if (ato < afrom)ato = afrom;
int s;
waitable *w = nullptr;
int cells = (ato - afrom) / csy;
if (cells)
{
int tnum = g_pThreadPool->get_thread_num();
s = cells < tnum ? cells : tnum;
int slice = (cells / s) * csy;
w = new waitable[s];
for (int r = 0; r < s; r++)
{
int f = r * slice + afrom;
int t = r != s - 1 ? f + slice : ato;
w[r] = schedule([f, t, &fn]
{
for (int r = f; r < t; r++)
{
fn(r);
}
});
}
}
while (from < afrom)
{
fn(from);
from++;
}
while (ato < to)
{
fn(ato);
ato++;
}
if (w)
{
for (int r = 0; r < s; r++)
w[r]->wait();
delete[] w;
}
}
protected:
BYTE *crippled;
BYTE *lock;
BYTE *border;
BYTE *background;
BYTE *ext; //3
BYTE *ext0;
BYTE *ext1;
BYTE *ext2;
tmode mode;
toverflow overflow;
int xsize, ysize; //Size in pixels
int xcell, ycell; //Size of cell in pixels
int sizecell, offsetcell; //Byte size of cell, byte size of cell offset
int rmapsize, rcolorsize, rscreensize, rbackbuffers;
int buffersize;
int rbackbufnum;
BYTE *buffer;
BYTE *tbuffer;
BYTE *map;
BYTE *color;
BYTE *screen;
int par; //0=PC, 1=PAL, 2=NTSC
std::map<nstrc,nstrw> meta;
class InfoUse
{
public:
typedef enum
{
INFO_NO,
INFO_VALUE,
INFO_INDEX,
INFO_INDEX_LOW,
INFO_INDEX_HIGH
}tuse;
InfoUse(){use = INFO_NO;}
tuse use;
BYTE **pp;
};
InfoUse infouse[6];
bool IsMaskGlobal(int mask){return infouse[mask].use == InfoUse::INFO_VALUE || crippled[mask];}
bool IsMaskLocal(int mask){return !IsMaskGlobal(mask);}
int32_t history_pos;
narray<BYTE *,int32_t> history_undo;
narray<BYTE *,int32_t> history_redo;
C64Interface *history_previous, *history_next;
void DeleteHistoryPrevious(void);
void DeleteHistoryNext(void);
void Create(int mapsize, int colorsize, int screensize, int backbuffers=1);
void DuplicateGlobalsToBackBuffers(void);
void Destroy();
void RemapCell1(int cx, int cy, int maskfrom, int maskto);
void RemapCell2(int cx, int cy, int maskfrom, int maskto);
void RemapCell2(int cx, int cy, int w, int h, int maskfrom, int maskto);
void SwapmaskCell2(int cx, int cy, int maskfrom, int maskto);
void SwapmaskCell2(int cx, int cy, int w, int h, int maskfrom, int maskto);
static BYTE RemapByte2(BYTE in, int maskfrom, int maskto);
static BYTE SwapmaskByte2(BYTE in, int maskfrom, int maskto);
void CountCell2(int cx, int cy, int *c);
void CountCell2(int cx, int cy, int w, int h, int *c);
void CountCell1(int cx, int cy, int *c);
void CountCell1(int cx, int cy, int w, int h, int *c);
BYTE GetCommonColorFromMask(int mask, BYTE *index, int size, bool high=false);
BYTE GetColor(int ci, int infoindex);
void SetColor(int ci, int infoindex, BYTE col);
int ResolveMask1(int ci, BYTE &col, int pointmask);
int ResolveMask2(int ci, BYTE &col, int pointmask, bool charmode=false);
int ResolveRemask2(BYTE *c);
int ResolveReplaceRemask2(BYTE *c);
int GetMapIndexFromCell(int cx, int cy){return ((xsize/xcell) * cy + cx) * offsetcell;}
int GetMapIndex(int x, int y){return 0 ;}
void PushLocks(void){memcpy(savelock, lock, 6);}
void PopLocks(void){memcpy(lock, savelock, 6);}
void ResetHistory(void);
class ImportHelper
{
public:
ImportHelper(C64Interface *p, CImage &img, bool wide);
~ImportHelper();
int CountColors(int cx, int cy, int w, int h, int *colcount);
int CountTopColors(int cx, int cy, int w, int h, BYTE *col);
int CountTopColorsPerCell(BYTE *col, int ceil=4);
void ReduceColors(int maxcolor, BYTE *force, int forcecount);
static int HighIndex(int *list, int count)
{
return highindex(list, count);
}
private:
CImageFast &img;
COLORREF GetImagePixel(int x, int y);
BYTE GetPixel(int x, int y) { return p[y*xsize + x]; }
void SetPixel(int x, int y, BYTE c) { p[y*xsize + x] = c; }
int xsize, ysize, xcs, ycs;
C64Interface *parent;
bool wide;
int palette;
BYTE *p;
bool find(BYTE col, BYTE *p, int count)
{
for(int r=0;r<count;r++)
{
if(col == p[r])
return true;
}
return false;
}
static int highindex(int *list, int count)
{
int high = 0;
int best = -1;
for(int r=0;r<count;r++)
{
if(list[r] > high)
{
high = list[r];
best = r;
}
}
return best;
}
};
private:
BYTE savelock[6];
static const double parvalue[3];
nstr file_name;
bool dirty;
void PlayHistory(BYTE *p);
void GetMaskInfo(int cx, int cy, int w, int h, int infoindex, CellInfo &info);
void SetMaskInfo(int cx, int cy, int w, int h, int infoindex, CellInfo &info);
bool mymemspn_low(const BYTE *ptr, BYTE value, int num)
{
for(;num>1;num--,ptr++)
{
if(((*ptr)&0x0f)!=value)
return false;
}
return true;
}
bool mymemspn_high(const BYTE *ptr, BYTE value, int num)
{
for(;num>1;num--,ptr++)
{
if(((*ptr)&0xf0)!=value)
return false;
}
return true;
}
};
class MCBitmap : public C64Interface
{
friend class C64Interface;
friend class Bitmap;
public:
MCBitmap(int x=160, int y=200, int backbuffers=1);
virtual ~MCBitmap();
protected:
BYTE GetPixel(int x, int y);
BYTE GetPixelColourMap(int x, int y);
void SetPixel(int x, int y, BYTE col);
int GetPixelWidth(void){return 2;}
void GetSaveFormats(narray<autoptr<SaveFormat>,int> &fmt);
bool CanOptimize(void){return true;}
void Import(CImage &img);
void Load(nmemfile &file, LPCTSTR type, int version);
void Save(nmemfile &file, LPCTSTR type);
static void GetLoadFormats(narray<autoptr<SaveFormat>,int> &fmt);
static nstr IdentifyFile(nmemfile &file);
void RemapCellColour(int cx, int cy, int colour, int indexTo);
private:
inline BYTE GetPixelInternal(int x, int y);
static int DecompressAmicaStream(const BYTE *stream, int stream_size, BYTE *buffer, int buffer_size);
static int DecompressKoalaStream(const BYTE *stream, int stream_size, BYTE *buffer, int buffer_size);
static int CompressKoalaStream(const BYTE *stream, int stream_size, BYTE *buffer, int buffer_size);
static int DecompressZoomaticStream(const BYTE *stream, int stream_size, BYTE *buffer, int buffer_size);
static int CompressZoomaticStream(const BYTE *stream, int stream_size, BYTE *buffer, int buffer_size);
static int CompressZoomaticStreamFlush(BYTE *buffer, int w, BYTE b, int count, BYTE code);
int GetMask(int x, int y);
};
class Bitmap : public C64Interface
{
friend class C64Interface;
public:
Bitmap(int x=320, int y=200, int backbuffers=1);
virtual ~Bitmap();
protected:
BYTE GetPixel(int x, int y);
BYTE GetPixelColourMap(int x, int y) { return 0; }
void SetPixel(int x, int y, BYTE col);
void GetSaveFormats(narray<autoptr<SaveFormat>,int> &fmt);
bool CanOptimize(void){return true;}
void Import(CImage &img);
void Load(nmemfile &file, LPCTSTR type, int version);
void Save(nmemfile &file, LPCTSTR type);
static void GetLoadFormats(narray<autoptr<SaveFormat>,int> &fmt);
static nstr IdentifyFile(nmemfile &file);
private:
inline BYTE GetPixelInternal(int x, int y);
int GetMask(int x, int y);
void SwapCellColours(int cx, int cy);
};
class Unrestricted : public C64Interface
{
friend class C64Interface;
public:
Unrestricted(int x=320, int y=200, bool wide = false, int backbuffers=1);
virtual ~Unrestricted();
protected:
BYTE GetPixel(int x, int y);
BYTE GetPixelColourMap(int x, int y) { return 0; }
void SetPixel(int x, int y, BYTE col);
void GetCellInfo(int cx, int cy, int w, int h, CellInfo *info);
int GetPixelWidth(void){return mode == W_UNRESTRICTED ? 2 : 1;}
void GetSaveFormats(narray<autoptr<SaveFormat>,int> &fmt);
bool CanOptimize(void){return false;}
void Import(CImage &img);
void Save(nmemfile &file, LPCTSTR type);
C64Interface *CreateFromSelection(int x, int y, int w, int h);
static void GetLoadFormats(narray<autoptr<SaveFormat>,int> &fmt);
private:
inline BYTE GetPixelInternal(int x, int y);
};
class CommonFont : public C64Interface
{
friend class C64Interface;
public:
enum tfontdisplay : int
{
FONT_1X1 = 0,
FONT_1X2,
FONT_2X1,
FONT_2X2
};
protected:
CommonFont();
virtual ~CommonFont();
volatile int charcount;
int CreateCharScreen(narray<BYTE,int> &c, narray<BYTE,int> &s, bool space = false);
void GetSaveFormats(narray<autoptr<SaveFormat>,int> &fmt);
void Save(nmemfile &file, LPCTSTR type);
void Load(nmemfile &file, LPCTSTR type, int version);
void SetFontDisplay(int mode) override;
int GetFontDisplay(void) override {return fontdisplay;}
void TranslateFontDisplay(int &cx, int &cy);
private:
CEvent quit;
HANDLE thread;
tfontdisplay fontdisplay;
virtual int GetCharCount(void){return charcount;}
static unsigned int __stdcall threadentry(void *in);
};
class MCFont : public CommonFont
{
friend class C64Interface;
public:
MCFont(int x, int y, int backbuffers=1);
virtual ~MCFont();
protected:
BYTE GetPixel(int x, int y);
BYTE GetPixelColourMap(int x, int y) { return 0; }
void SetPixel(int x, int y, BYTE col);
void GetSaveFormats(narray<autoptr<SaveFormat>,int> &fmt);
void Import(CImage &img);
void Load(nmemfile &file, LPCTSTR type, int version);
void Save(nmemfile &file, LPCTSTR type);
int GetPixelWidth(void){return 2;};
static void GetLoadFormats(narray<autoptr<SaveFormat>,int> &fmt);
private:
int GetMask(int x, int y);
};
class SFont : public CommonFont
{
friend class C64Interface;
public:
SFont(int x, int y, int backbuffers=1);
virtual ~SFont();
protected:
BYTE GetPixel(int x, int y);
BYTE GetPixelColourMap(int x, int y) { return 0; }
void SetPixel(int x, int y, BYTE col);
void GetSaveFormats(narray<autoptr<SaveFormat>,int> &fmt);
void Import(CImage &img);
void Load(nmemfile &file, LPCTSTR type, int version);
void Save(nmemfile &file, LPCTSTR type);
void CustomCommand(int n);
static void GetLoadFormats(narray<autoptr<SaveFormat>,int> &fmt);
private:
int GetMask(int x, int y);
};
class Sprite : public C64Interface
{
friend class C64Interface;
public:
Sprite(int x, int y, int backbuffers=1);
virtual ~Sprite();
protected:
BYTE GetPixel(int x, int y);
BYTE GetPixelColourMap(int x, int y) { return 0; }
void SetPixel(int x, int y, BYTE color);
static void GetLoadFormats(narray<autoptr<SaveFormat>,int> &fmt);
void GetSaveFormats(narray<autoptr<SaveFormat>,int> &fmt);
void Load(nmemfile &file, LPCTSTR type, int version);
void Save(nmemfile &file, LPCTSTR type);
void Import(CImage &img);
private:
int GetMask(int x, int y);
};
class MCSprite : public C64Interface
{
friend class C64Interface;
public:
MCSprite(int x, int y, int backbuffers=1);
virtual ~MCSprite();
protected:
BYTE GetPixel(int x, int y);
BYTE GetPixelColourMap(int x, int y) { return 0; }
void SetPixel(int x, int y, BYTE color);
static void GetLoadFormats(narray<autoptr<SaveFormat>,int> &fmt);
void GetSaveFormats(narray<autoptr<SaveFormat>,int> &fmt);
void Load(nmemfile &file, LPCTSTR type, int version);
void Save(nmemfile &file, LPCTSTR type);
void Import(CImage &img);
int GetPixelWidth(void){return 2;};
private:
int GetMask(int x, int y);
};