-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsplitsdl.c
executable file
·2859 lines (2578 loc) · 96.6 KB
/
splitsdl.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
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
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifndef OS_WIN
#define DEBUG_MODE 0
#else
#define FAST_LIBRARY 1
#endif
#include "library.c"
#include "libgfx.c"
#include<float.h>
#ifndef OS_WIN
#include <SDL2/SDL.h>
#else
#include <SDL.h>
#endif
#define __FILENAME__ "splitsdl.c"
#define WINDOW_NAME "splitraster+ 40 years Edition"
struct s_hardware_sprite {
unsigned char data[256*3];
unsigned char mask[256];
unsigned char palette[45+3];
int x,y,group,active,moving;
};
struct s_undo {
struct s_hardware_sprite hsp[16];
float floydcoef;
float sharpnesscoef;
float contrastcoef;
int leveldown;
int levelup;
};
void HSP_fill(struct s_hardware_sprite *hsp, int sx, int sy) {
unsigned int c;
c=hsp->data[(sx+sy*16)*3+0]+(hsp->data[(sx+sy*16)*3+1]<<8)+(hsp->data[(sx+sy*16)*3+2]<<16);
hsp->mask[sx+sy*16]=0;
if (sx<15 && hsp->mask[sx+1+sy*16] && (hsp->data[(sx+1+sy*16)*3+0]+(hsp->data[(sx+1+sy*16)*3+1]<<8)+(hsp->data[(sx+1+sy*16)*3+2]<<16))==c) HSP_fill(hsp,sx+1,sy);
if (sx>0 && hsp->mask[sx-1+sy*16] && (hsp->data[(sx-1+sy*16)*3+0]+(hsp->data[(sx-1+sy*16)*3+1]<<8)+(hsp->data[(sx-1+sy*16)*3+2]<<16))==c) HSP_fill(hsp,sx-1,sy);
if (sy<15 && hsp->mask[sx+sy*16+16] && (hsp->data[(sx+sy*16+16)*3+0]+(hsp->data[(sx+sy*16+16)*3+1]<<8)+(hsp->data[(sx+sy*16+16)*3+2]<<16))==c) HSP_fill(hsp,sx,sy+1);
if (sy>0 && hsp->mask[sx+sy*16-16] && (hsp->data[(sx+sy*16-16)*3+0]+(hsp->data[(sx+sy*16-16)*3+1]<<8)+(hsp->data[(sx+sy*16-16)*3+2]<<16))==c) HSP_fill(hsp,sx,sy-1);
}
void HSP_unfill(struct s_hardware_sprite *hsp, int sx, int sy) {
unsigned int c;
c=hsp->data[(sx+sy*16)*3+0]+(hsp->data[(sx+sy*16)*3+1]<<8)+(hsp->data[(sx+sy*16)*3+2]<<16);
hsp->mask[sx+sy*16]=1;
if (sx<15 && !hsp->mask[sx+1+sy*16] && (hsp->data[(sx+1+sy*16)*3+0]+(hsp->data[(sx+1+sy*16)*3+1]<<8)+(hsp->data[(sx+1+sy*16)*3+2]<<16))==c) HSP_unfill(hsp,sx+1,sy);
if (sx>0 && !hsp->mask[sx-1+sy*16] && (hsp->data[(sx-1+sy*16)*3+0]+(hsp->data[(sx-1+sy*16)*3+1]<<8)+(hsp->data[(sx-1+sy*16)*3+2]<<16))==c) HSP_unfill(hsp,sx-1,sy);
if (sy<15 && !hsp->mask[sx+sy*16+16] && (hsp->data[(sx+sy*16+16)*3+0]+(hsp->data[(sx+sy*16+16)*3+1]<<8)+(hsp->data[(sx+sy*16+16)*3+2]<<16))==c) HSP_unfill(hsp,sx,sy+1);
if (sy>0 && !hsp->mask[sx+sy*16-16] && (hsp->data[(sx+sy*16-16)*3+0]+(hsp->data[(sx+sy*16-16)*3+1]<<8)+(hsp->data[(sx+sy*16-16)*3+2]<<16))==c) HSP_unfill(hsp,sx,sy-1);
}
/* from tools/library */
void ObjectArrayAddValue(void **zearray, int *nbfields, int *maxfields, void *zeobject, int object_size)
{
char *dst;
if ((*zearray)==NULL) {
*nbfields=1;
*maxfields=16; // tuning for THIS software
(*zearray)=malloc((*maxfields)*object_size);
} else {
*nbfields=(*nbfields)+1;
if (*nbfields>=*maxfields) {
*maxfields=(*maxfields)*2;
(*zearray)=realloc((*zearray),(*maxfields)*object_size);
}
}
dst=((char *)(*zearray))+((*nbfields)-1)*object_size;
memcpy(dst,zeobject,object_size);
}
/* super cube things */
struct s_color_box
{
int x,y,z; /* 3D position */
int w,h,d; /* 3D size */
};
struct s_super_pixel {
int r,v,b;
};
struct s_super_cell {
struct s_super_pixel *pixel;
int nbpixel,maxpixel;
int reference;
};
/*
v1.0
algo de recherche basé sur la distance entre chaque couleur
recherche de la première couleur au plus bas du supercube
palette peut être pré-initialisée
calculs avec sub-précision due au supercube
*/
int colorz_create_palette(unsigned char *data, int size, unsigned char *palette,int start)
{
static int farfrom[4096];
static int first=1;
int cube[4096]={0};
int maxcoul,i,j,c,r=0,total,idx;
/* stats */
double pseudo_variance[3];
int population_trigger;
int split[3];
int ar,av,ab;
int imin,imax,nmax;
double dmax,distance,mindistance;
/* super cube */
struct s_super_cell supercube[4096];
struct s_super_pixel superpixel;
struct s_super_pixel referencepixel[16];
int howmany,hsp=0;
int x,y,z;
/* préparation du supercube */
if (start==-1) {
start=0;
hsp=1;
}
memset(supercube,0,sizeof(supercube));
maxcoul=start;
howmany=16-hsp;
for (i=0;i<start;i++) {
idx=(palette[i*3+0]>>4)+(palette[i*3+1]>>4)*16+(palette[i*3+2]>>4)*256;
superpixel.r=palette[i*3+0];
superpixel.v=palette[i*3+1];
superpixel.b=palette[i*3+2];
referencepixel[r++]=superpixel;
howmany--;
ObjectArrayAddValue((void **)&supercube[idx].pixel,&supercube[idx].nbpixel,&supercube[idx].maxpixel,&superpixel,sizeof(struct s_super_pixel));
}
/* construction du super cube */
for (i=0;i<size;i++) {
idx=(data[i*3+0]>>4)+(data[i*3+1]>>4)*16+(data[i*3+2]>>4)*256;
superpixel.r=data[i*3+0];
superpixel.v=data[i*3+1];
superpixel.b=data[i*3+2];
ObjectArrayAddValue((void **)&supercube[idx].pixel,&supercube[idx].nbpixel,&supercube[idx].maxpixel,&superpixel,sizeof(struct s_super_pixel));
/* on ne compte que le premier point */
if (supercube[idx].nbpixel==1) maxcoul++;
}
/* pour chaque point du super cube on sait combien on a d'occurences */
if (maxcoul<16-hsp) {
/* full "almost exact" colorz */
r=0;
for (i=0;i<4096;i++) {
if (supercube[i].nbpixel) referencepixel[r++]=supercube[i].pixel[0]; // service minimum
}
} else {
/***********************************************
c o l o r a p p r o x i m a t i o n
***********************************************/
/* on prend la première couleur du supercube qui est la plus éloignée du centre! */
for (i=0;i<4096;i++) if (supercube[i].nbpixel) break;
/* à cet endroit on peut arriver avec des références pré-établies et ne chercher que d'autres couleurs :) */
if (!r) {
referencepixel[0]=supercube[i].pixel[0];howmany--;r=1;
}
while (howmany) {
/* tant qu'il nous reste des couleurs de référence disponibles */
imax=-1;nmax=0;dmax=-1;
/* on parcourt notre tableau */
for (i=0;i<4096;i++) {
for (j=0;j<supercube[i].nbpixel;j++) {
mindistance=65536*3;
/* on compare la couleur courante avec toutes les références! */
for (c=0;c<r;c++) {
distance=(supercube[i].pixel[j].r-referencepixel[c].r)*(supercube[i].pixel[j].r-referencepixel[c].r)+
(supercube[i].pixel[j].v-referencepixel[c].v)*(supercube[i].pixel[j].v-referencepixel[c].v)+
(supercube[i].pixel[j].b-referencepixel[c].b)*(supercube[i].pixel[j].b-referencepixel[c].b);
if (distance<mindistance) mindistance=distance;
}
if (mindistance>dmax) {
/* on se garde l'index sous le coude si elle est plus éloignée que les autres */
dmax=mindistance;
imax=i;
nmax=j;
}
}
}
/* nouvelle référence */
if (imax>=0) referencepixel[r]=supercube[imax].pixel[nmax]; else {printf("internal error: imax MUST find a color\n");exit(-1);}
howmany--;
r++;
}
//for (i=0;i<r;i++) printf("reference[%02d]=(%02X/%02X/%02X)\n",i,referencepixel[i].r,referencepixel[i].b,referencepixel[i].v);
/* on donne aux couleurs restantes une référence par défaut */
for (i=0;i<4096;i++) {
if (supercube[i].reference==-1) {
/* on compare la couleur courante avec toutes les références! */
j=imin=0;
mindistance=65536*3;
for (c=0;c<r;c++) {
distance=(supercube[i].pixel[j].r-referencepixel[c].r)*(supercube[i].pixel[j].r-referencepixel[c].r)+
(supercube[i].pixel[j].v-referencepixel[c].v)*(supercube[i].pixel[j].v-referencepixel[c].v)+
(supercube[i].pixel[j].b-referencepixel[c].b)*(supercube[i].pixel[j].b-referencepixel[c].b);
if (distance<mindistance) {
mindistance=distance;
imin=c;
}
}
supercube[i].reference=imin;
}
}
}
for (i=0;i<r;i++) {
palette[i*3+0]=referencepixel[i].r&0xF0;
palette[i*3+1]=referencepixel[i].v&0xF0;
palette[i*3+2]=referencepixel[i].b&0xF0;
}
for (i=0;i<4096;i++) {
if (supercube[i].pixel) free(supercube[i].pixel);
}
return r;
}
int __GetIDXFromPalette(unsigned char *palette, int r, int v, int b,int maxcoul)
{
#undef FUNC
#define FUNC "GetIDXFromPalette"
int imin,vmin,va;
int i;
if (r<0) r=0; else if (r>255) r=255;
if (v<0) v=0; else if (v>255) v=255;
if (b<0) b=0; else if (b>255) b=255;
for (i=0;i<maxcoul;i++) {
if (palette[i*3+0]==r && palette[i*3+1]==v && palette[i*3+2]==b) return i;
}
vmin=65536*32;
imin=0;
for (i=0;i<maxcoul;i++) {
va=((int)palette[i*3+0]-r)*((int)palette[i*3+0]-r)+((int)palette[i*3+1]-v)*((int)palette[i*3+1]-v)+((int)palette[i*3+2]-b)*((int)palette[i*3+2]-b);
if (va<vmin) {
vmin=va;
imin=i;
}
}
//logwarn("palette is approximative regarding the datas... RVB:%X%02X%02X -> %X%02X%02X",r,v,b,palette[imin*3+0],palette[imin*3+1],palette[imin*3+2]);
return imin;
}
int GetIDXFromPalette(unsigned char *palette, int r, int v, int b) {
return __GetIDXFromPalette(palette, r,v,b,16);
}
struct s_rastamix {
unsigned int asicad[4]; //asicad1,asicad2,asicad3,asicad4;
unsigned int asicol[4]; //asicol1, asicol2, asicol3, asicol4;
unsigned int hspad;
unsigned int hspcol1,hspcol2;
unsigned char palette[48];
int l;
unsigned char clipped[384]; // HSP index +1 ou zero si rien
int adr;
};
//#define DBG printf("line %d\n",__LINE__);
#define DBG ;
int compare_hsp(const void * a, const void * b)
{
struct s_hardware_sprite *c,*d;
int ret;
c=(struct s_hardware_sprite *)a;
d=(struct s_hardware_sprite *)b;
/* tri de haut en bas et de gauche à droite */
ret=c->y-d->y;
if (ret) return ret; else return c->x-d->x;
}
int Build(char *filename, int dithering, int uniform)
{
#undef FUNC
#define FUNC "Build"
enum e_module {
E_MODULE_QUANTIZATION=0,
E_MODULE_PALETTE,
E_MODULE_EDITION
};
enum e_module current_module=E_MODULE_QUANTIZATION;
/* libSDL2 */
SDL_Window *win=NULL;
SDL_Surface *surface=NULL,*screen;
int *pixels,ok=1,gmx,gmy,ms;
const unsigned char *state;
unsigned int *membitmap;
int nokey=0;
#define BLACK_COLOR 0xFF000000;
unsigned int myblack=BLACK_COLOR;
#define GREEN_COLOR 0xFF00FF00;
unsigned int mygreen=GREEN_COLOR;
#define CYAN_COLOR 0xFFFF8888;
unsigned int mycyan=CYAN_COLOR;
unsigned int myg=GREEN_COLOR;
#define RED_COLOR 0xFF0000FF;
unsigned int myr=RED_COLOR;
#define BLUE_COLOR 0xFFFF0000;
unsigned int myb=BLUE_COLOR;
#define VIOLET_COLOR 0xFFFF1080
unsigned int myviolet=VIOLET_COLOR;
#define GREY_COLOR 0xFF777777
unsigned int mygrey=GREY_COLOR;
#define WHITE_COLOR 0xFFFFFFFF
unsigned int mywhite=WHITE_COLOR;
int winx,winy;
int redo=2,redraw=1,reset=1;
/*-----------*/
struct s_png_info *photo=NULL;
struct s_raw_info rawinfo={0};
int i,j,l,m,n,v;
int *cptpalette;
int width,height;
float vmin,vmax,vpal,totpal;
int ligne[192],adr,adrcode,numligne;
unsigned char cpc[65536]={0};
char cpccode[65536]={0};
unsigned char refpalette[16*3];
unsigned char tmppalette[16*3];
int checkipal[16];
int checknewpal[16];
int cptnewpal[16];
int cptrefpal[16];
int newcoloridx[4],newcolorpush;
int iref;
/*--------------------*/
char sprfilename[PATH_MAX];
char asmfilename[PATH_MAX];
char cprasmfilename[PATH_MAX];
char scrfilename1[PATH_MAX];
char scrfilename2[PATH_MAX];
char xmlfilename[PATH_MAX];
char previewfilename[PATH_MAX];
/*--------------------*/
struct s_xml_field *resource=NULL;
struct s_xml_field *xmlhsp=NULL,*xmlhsptab=NULL,*xmlparam=NULL,*xmlrasta=NULL,*xmlrastatab=NULL;
unsigned char *data,*preview;
int x,y,down=0,downspace=0,mx,my,sx,sy;
struct s_hardware_sprite hsp[16]={0};
int nbhsp=0,curhsp=0;
struct s_rastamix *rastamix=NULL;
unsigned int asicolinit[32]={0};
int group,igroup;
int toggle_greentea=1;
int c,compute=0,computeimage=0;
int omx,omy,rm,vm,bm,cm,lasty,lastsp,previouslasty;
/* v3 */
float floydcoef=0.3;
float sharpnesscoef=0.0;
float contrastcoef=0.0;
/* v5 */
int leveldown=0;
int levelup=255;
int reboot,k,computeglobal=1;
int imin,imax,icol,ipal,idark1,idark2;
int tic,pr,pv,pb,pix1,pix2,zepix;
int asicad;
int clipcol[16];
int artefacts,artebis,wasmoving=0;
int maskvalue=-1;
/* v7 */
int legacy_display=1;
int nopfirst;
/* v8 */
int refgmx,refgmy,cumulgmx,cumulgmy;
int hspset_display=0,locked=0;
int keycursorx=0,keycursory=0;
int currentpen=5;
/* ergo */
int touchfloyd=0, touchsharp=0, touchcontrast=0, touchlevel=0, touchmask=0;
if (strstr(filename,"png")) {
strcpy(sprfilename,filename);
TxtReplace(sprfilename,".png",".spr",0);
strcpy(xmlfilename,filename);
TxtReplace(xmlfilename,".png",".xml",0);
sprintf(cprasmfilename,"%scpr",filename);
TxtReplace(cprasmfilename,".pngcpr","cpr.asm",0);
sprintf(previewfilename,"%spreview",filename);
TxtReplace(previewfilename,".pngpreview","preview.png",0);
strcpy(asmfilename,filename);
TxtReplace(asmfilename,".png",".asm",0);
strcpy(scrfilename1,filename);
TxtReplace(scrfilename1,".png",".sc1",0);
strcpy(scrfilename2,filename);
TxtReplace(scrfilename2,".png",".sc2",0);
photo=PNGRead24(filename);
if (!photo) {
logerr("cannot read PNG");
exit(ABORT_ERROR);
}
loginfo("PNG image is %dx%dx%d",photo->width,photo->height,photo->bit_depth);
if (photo->color_type!=PNG_COLOR_TYPE_RGB) {
logerr("PNG image must be RGB without transparency",filename);
exit(ABORT_ERROR);
}
}
if (strstr(filename,"bmp")) {
unsigned char *bpixels;
int bwidth, bheight;
strcpy(sprfilename,filename);
TxtReplace(sprfilename,".bmp",".spr",0);
strcpy(xmlfilename,filename);
TxtReplace(xmlfilename,".bmp",".xml",0);
sprintf(cprasmfilename,"%scpr",filename);
TxtReplace(cprasmfilename,".bmpcpr","cpr.asm",0);
sprintf(previewfilename,"%spreview",filename);
TxtReplace(previewfilename,".bmppreview","preview.png",0);
strcpy(asmfilename,filename);
TxtReplace(asmfilename,".bmp",".asm",0);
strcpy(scrfilename1,filename);
TxtReplace(scrfilename1,".bmp",".sc1",0);
strcpy(scrfilename2,filename);
TxtReplace(scrfilename2,".bmp",".sc2",0);
printf("load BMP file\n");
if (loadBMP(filename,&bpixels,&bwidth,&bheight)) {
logerr("cannot read BMP");
exit(ABORT_ERROR);
}
photo=calloc(sizeof(struct s_png_info),1);
photo->color_type=PNG_COLOR_TYPE_RGB;
photo->width=bwidth;
photo->height=bheight;
photo->bit_depth=8;
photo->data=MemMalloc(bwidth*bheight*3);
// from RGBA to RGB
for (l=0;l<bwidth*bheight;l++) {
bpixels[l*3+0]=bpixels[l*4+0];
bpixels[l*3+1]=bpixels[l*4+1];
bpixels[l*3+2]=bpixels[l*4+2];
}
for (l=0;l<bheight;l++) {
memcpy(photo->data+bwidth*3*l,bpixels+bwidth*3*(bheight-1-l),bwidth*3);
}
MemFree(bpixels);
if (!photo) {
logerr("cannot read BMP");
exit(ABORT_ERROR);
}
loginfo("BMP image is %dx%dx%d",photo->width,photo->height,photo->bit_depth);
if (photo->color_type!=PNG_COLOR_TYPE_RGB) {
logerr("PNG image must be RGB without transparency",filename);
exit(ABORT_ERROR);
}
}
if (!photo) {
logerr("Please use .bmp or .png files...");
exit(ABORT_ERROR);
}
if (photo->width!=384 || photo->height>273 || photo->height<16) {
float xmul;
int newypict=272;
if (photo->width!=384) logwarn("PNG file [%s] is not 384 pixels width",filename);
if (photo->height>273) logwarn("PNG file [%s] more than 273 pixels height",filename);
if (photo->width!=384) {
xmul=384.0/(float)photo->width;
newypict=photo->height*xmul;
if (newypict>273) newypict=273; // et tant pis pour le ratio!
}
photo->data=ImageReduction(photo->data,photo->width,photo->height,384,newypict);
photo->width=384;
photo->height=newypict;
}
/* on alloue le max */
data=MemMalloc(384*273*3);
preview=MemCalloc(384*273*3);
/* data est la REFERENCE ultime */
memcpy(data,photo->data,384*photo->height*3);
rastamix=MemCalloc(sizeof(struct s_rastamix)*273);
if (SDL_Init(SDL_INIT_EVERYTHING)!=0) {
fprintf(stderr,"SDL_Init error\n");
return -1;
}
loginfo("SDL_Init");
winx=(384+256)*2;
winy=273*2+1;
if ((win=SDL_CreateWindow(WINDOW_NAME, 30, 30, winx, winy, SDL_WINDOW_SHOWN))==NULL) {
fprintf(stderr,"SDL_CreateWindow error\n");
return -1;
}
SDL_SetWindowPosition(win,SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED);
screen=SDL_GetWindowSurface(win);
if ((surface=SDL_CreateRGBSurface(0,winx,winy,32,0,0,0,0))==NULL) { // default mask for 32 bits pixels
fprintf(stderr,"SDL_CreateRGBSurface error\n");
return -1;
}
width=photo->width;
height=photo->height;
curhsp=nbhsp=0;
if (FileExists(xmlfilename)) {
unsigned char *zemask;
unsigned char *zedata;
int tmpheight;
resource=XMLLoadFile(xmlfilename);
//XMLDumpTree(resource);
if ((xmlparam=XMLGetField(resource,"height"))!=NULL) tmpheight=atoi(XMLGetFieldAttrValueFromField(xmlparam,"v"));
if (height!=photo->height) {
XMLFreeField(resource);
logerr("XML loaded but not applied because declared height is different from current picture!\n");
} else {
if ((xmlparam=XMLGetField(resource,"locked"))!=NULL) locked=atoi(XMLGetFieldAttrValueFromField(xmlparam,"v"));
if (locked) {
current_module=E_MODULE_EDITION;
} else {
if ((xmlparam=XMLGetField(resource,"module"))!=NULL) current_module=atoi(XMLGetFieldAttrValueFromField(xmlparam,"v"));
switch (current_module) {
case E_MODULE_EDITION:
case E_MODULE_PALETTE:locked=1; // temporaire
break;
case E_MODULE_QUANTIZATION:
locked=0;
break;
}
}
if ((xmlparam=XMLGetField(resource,"keycursorx"))!=NULL) keycursorx=atoi(XMLGetFieldAttrValueFromField(xmlparam,"v"));
if ((xmlparam=XMLGetField(resource,"keycursory"))!=NULL) keycursory=atoi(XMLGetFieldAttrValueFromField(xmlparam,"v"));
if ((xmlparam=XMLGetField(resource,"floyd"))!=NULL) floydcoef=atof(XMLGetFieldAttrValueFromField(xmlparam,"v"));
if ((xmlparam=XMLGetField(resource,"sharp"))!=NULL) sharpnesscoef=atof(XMLGetFieldAttrValueFromField(xmlparam,"v"));
if ((xmlparam=XMLGetField(resource,"contrast"))!=NULL) contrastcoef=atof(XMLGetFieldAttrValueFromField(xmlparam,"v"));
if ((xmlparam=XMLGetField(resource,"levelup"))!=NULL) levelup=atoi(XMLGetFieldAttrValueFromField(xmlparam,"v"));
if ((xmlparam=XMLGetField(resource,"leveldown"))!=NULL) leveldown=atoi(XMLGetFieldAttrValueFromField(xmlparam,"v"));
if ((xmlhsp=XMLGetField(resource,"sprites"))!=NULL) {
xmlhsptab=XMLGetFieldMulti(xmlhsp,"sprite",&nbhsp);
for (i=0;i<nbhsp;i++) {
hsp[i].x=atoi(XMLGetFieldAttrValueFromField(&xmlhsptab[i],"x"));
hsp[i].y=atoi(XMLGetFieldAttrValueFromField(&xmlhsptab[i],"y"));
hsp[i].group=atoi(XMLGetFieldAttrValueFromField(&xmlhsptab[i],"group"));
/* on récupère aussi la palette */
for (j=0;j<45;j++) {
char bufname[128];
sprintf(bufname,"p%02d",j);
hsp[i].palette[j]=atoi(XMLGetFieldAttrValueFromField(&xmlhsptab[i],bufname));
}
/* recup du masque */
zemask=XMLGetFieldValue(&xmlhsptab[i],"mask");
if (zemask && strlen(zemask)>=256) {
for (l=0;l<256;l++) {
hsp[i].mask[l]=zemask[l]-'0';
}
} else {
printf("WARNING: no MASK for HSP %d in XML\n",i);
}
for (l=0;l<256;l++) {
if (hsp[i].mask[l]) hsp[i].mask[l]=1;
}
/* recup des DATA si présentes et mode locked */
if (locked) {
zedata=XMLGetFieldValue(&xmlhsptab[i],"data");
if (zedata && strlen(zedata)>=768*2) {
for (l=0;l<768;l++) {
if (zedata[l*2+0]<='9' && zedata[l*2+0]>='0')
hsp[i].data[l]=zedata[l*2+0]-'0';
else
hsp[i].data[l]=zedata[l*2+0]-'A'+10;
hsp[i].data[l]<<=4;
if (zedata[l*2+1]<='9' && zedata[l*2+1]>='0')
hsp[i].data[l]+=zedata[l*2+1]-'0';
else
hsp[i].data[l]+=zedata[l*2+1]-'A'+10;
}
} else {
printf("WARNING: no DATA for HSP %d in XML\n",i);
}
}
}
}
if (locked) {
loginfo("Loading back rastamix structs");
if ((xmlparam=XMLGetField(resource,"rastainit"))!=NULL) {
char buftmp[256];
for (l=0;l<31;l++) {
sprintf(buftmp,"asicolinit%02d",l);
asicolinit[l]=atoi(XMLGetFieldAttrValueFromField(xmlparam,buftmp));
}
} else {
logerr("XML corrupted no init colors!\n");
exit(-2);
}
if ((xmlrasta=XMLGetField(resource,"rastamixtab"))!=NULL) {
char buftmp[256];
int nbrasta;
xmlrastatab=XMLGetFieldMulti(xmlrasta,"rastamix",&nbrasta);
if (!xmlrastatab || nbrasta!=height) {
XMLFreeField(resource);
printf("XML corrupted (rastamix)!\n");
exit(-1);
}
for (i=0;i<nbrasta;i++) {
rastamix[i].adr=atoi(XMLGetFieldAttrValueFromField(&xmlrastatab[i],"adr"));
rastamix[i].hspad=atoi(XMLGetFieldAttrValueFromField(&xmlrastatab[i],"hspad"));
rastamix[i].hspcol1=atoi(XMLGetFieldAttrValueFromField(&xmlrastatab[i],"hspcol1"));
rastamix[i].hspcol2=atoi(XMLGetFieldAttrValueFromField(&xmlrastatab[i],"hspcol2"));
rastamix[i].l=atoi(XMLGetFieldAttrValueFromField(&xmlrastatab[i],"l"));
for (l=0;l<4;l++) {
sprintf(buftmp,"asicad%02d",l);
rastamix[i].asicad[l]=atoi(XMLGetFieldAttrValueFromField(&xmlrastatab[i],buftmp));
if (i>1 && (rastamix[i].asicad[l]<0x6400 || rastamix[i].asicad[l]>0x641F)) {
printf("XML corrupted rastaline %d asicoladr[%d]=%X\n",i,l,rastamix[i].asicad[l]);
exit(-2);
}
sprintf(buftmp,"asicol%02d",l);
rastamix[i].asicol[l]=atoi(XMLGetFieldAttrValueFromField(&xmlrastatab[i],buftmp));
}
for (l=0;l<48;l++) {
sprintf(buftmp,"p%02d",l);
rastamix[i].palette[l]=atoi(XMLGetFieldAttrValueFromField(&xmlrastatab[i],buftmp));
}
memset(rastamix[i].clipped,0,sizeof(rastamix[i].clipped));
}
}
/* reconstruction des infos de clipping à partir des masques */
for (i=nbhsp-1;i>=0;i--) {
l=0;
for (y=0;y<16;y++)
for (x=0;x<16;x++) {
if (hsp[i].mask[l]) {
rastamix[hsp[i].y+y].clipped[hsp[i].x+x]=i+1; // index du sprite+1 dans les infos de clipping
}
l++;
}
}
loginfo("Loading back preview");
zedata=XMLGetFieldValue(resource,"preview");
if (zedata && strlen(zedata)>=384*3*2*photo->height) {
for (l=0;l<384*3*photo->height;l++) {
if (zedata[l*2+0]<='9' && zedata[l*2+0]>='0')
preview[l]=zedata[l*2+0]-'0';
else
preview[l]=zedata[l*2+0]-'A'+10;
preview[l]<<=4;
if (zedata[l*2+1]<='9' && zedata[l*2+1]>='0')
preview[l]+=zedata[l*2+1]-'0';
else
preview[l]+=zedata[l*2+1]-'A'+10;
}
} else {
printf("XML preview corrupted\n");
exit(-2);
}
loginfo("Loading back cpcdata");
zedata=XMLGetFieldValue(resource,"cpcdata");
if (zedata && strlen(zedata)>=65536) {
for (l=0;l<32768;l++) {
if (zedata[l*2+0]<='9' && zedata[l*2+0]>='0')
cpc[l]=zedata[l*2+0]-'0';
else
cpc[l]=zedata[l*2+0]-'A'+10;
cpc[l]<<=4;
if (zedata[l*2+1]<='9' && zedata[l*2+1]>='0')
cpc[l]+=zedata[l*2+1]-'0';
else
cpc[l]+=zedata[l*2+1]-'A'+10;
}
} else {
printf("XML cpc data corrupted\n");
exit(-2);
}
MemFree(xmlrastatab);
}
}
MemFree(xmlhsptab);
XMLFreeField(resource);
loginfo("XML loaded");
}
omx=-1;omy=-1;
while (redo) {
if (!locked) {
if (reset) {
reset=0;
redraw=1;
compute=1;
computeimage=1;
for (i=0;i<nbhsp;i++) {
l=0;
for (y=hsp[i].y;y<hsp[i].y+16;y++)
for (x=hsp[i].x;x<hsp[i].x+16;x++) {
hsp[i].data[l++]=data[(y*width+x)*3+0];
hsp[i].data[l++]=data[(y*width+x)*3+1];
hsp[i].data[l++]=data[(y*width+x)*3+2];
}
}
}
DBG
/************************************ compute GFX colorz ***********************************/
if (computeglobal) {
unsigned char *newpix;
computeglobal=0;
/* reset data */
memcpy(photo->data,data,384*photo->height*3);
if (sharpnesscoef>0.1) {
newpix=ImageSharpness(photo->data,384,photo->height,sharpnesscoef*20);
MemFree(photo->data);
photo->data=newpix;
}
if (contrastcoef>0.1) {
newpix=ImageContrast(photo->data,384,photo->height,contrastcoef*20);
MemFree(photo->data);
photo->data=newpix;
}
if (leveldown>3 || levelup<252) {
newpix=ImageLevel(photo->data,384,photo->height,leveldown,levelup);
MemFree(photo->data);
photo->data=newpix;
}
compute=1;
}
if (computeimage || compute) {
float scanfloyd[192*3*2];
float or,ov,ob,qr,qv,qb;
unsigned char scanline[192*3*2];
unsigned char clippedscanline[192*3*2];
int cr,cv,cb,sizeclip;
int refl,ihsp,imaskhsp,xhsp,flagclip;
computeimage=0;
compute=0;
adr=numligne=0; // amstrad memory
/********************************************************
c o m p u t e H S P c o l o r s
********************************************************/
/* reset mask positions & hsp raster infos */
for (i=0;i<photo->height;i++) {
memset(rastamix[i].clipped,0,sizeof(rastamix[i].clipped));
rastamix[i].hspad=0;
rastamix[i].hspcol1=0;
rastamix[i].hspcol2=0;
}
/* on met à jour quel sprite est actif avant de les réordonner */
for (i=0;i<nbhsp;i++) hsp[i].active=0; hsp[curhsp].active=1;
/* on réordonne les sprites dans l'ordre d'affichage de haut en bas ET gauche à droite */
qsort(hsp,nbhsp,sizeof(struct s_hardware_sprite),compare_hsp);
/* on récupère l'index du sprite actif pour ne pas que l'éditeur se perde */
for (i=0;i<nbhsp;i++) {
if (hsp[i].active) {
curhsp=i;
if (wasmoving) wasmoving=1+curhsp;
}
}
DBG
/* on découpe en groupes selon l'espacement vertical entre les sprites hards */
group=0;
hsp[0].group=group; /* fix premier sprite noir à l'init */
for (i=1;i<nbhsp;i++) {
if (hsp[i-1].y+16+8<hsp[i].y) {
group++;
}
hsp[i].group=group;
}
/* reduce color for each group */
for (igroup=0;igroup<=group;igroup++) {
float floydHSP[18*18];
int bunchidx;
int startx,endx,starty,endy;
/* on alloc le max théorique */
rawinfo.data=MemMalloc(256*16*3);
/* make a bunch of pixels to compute colorz */
bunchidx=0;
for (i=0;i<nbhsp;i++) {
if (hsp[i].group==igroup) {
l=0;
for (y=0;y<16;y++)
for (x=0;x<16;x++) {
if (hsp[i].mask[l]) {
rawinfo.data[bunchidx+0]=photo->data[(hsp[i].x+x+(hsp[i].y+y)*384)*3+0];
rawinfo.data[bunchidx+1]=photo->data[(hsp[i].x+x+(hsp[i].y+y)*384)*3+1];
rawinfo.data[bunchidx+2]=photo->data[(hsp[i].x+x+(hsp[i].y+y)*384)*3+2];
bunchidx+=3;
}
l++;
}
}
}
/* on calcule la palette sur le groupe */
if (bunchidx) {
rawinfo.nbcolor=colorz_create_palette(rawinfo.data,bunchidx/3,tmppalette,-1); // -1 means 16-1 -> 15 colorz MAX
} else {
rawinfo.nbcolor=0;
}
/* fill unused colors with zeros */
while (rawinfo.nbcolor<15) {
tmppalette[rawinfo.nbcolor*3+0]=0;
tmppalette[rawinfo.nbcolor*3+1]=0;
tmppalette[rawinfo.nbcolor*3+2]=0;
rawinfo.nbcolor++;
}
/* on met à jour le clipped mask de l'écran */
for (i=lastsp=0;i<nbhsp;i++) {
if (hsp[i].group==igroup) {
memcpy(hsp[i].palette,tmppalette,sizeof(tmppalette));
lasty=hsp[i].y+16;
lastsp=i;
}
}
for (i=nbhsp-1;i>=0;i--) {
l=0;
for (y=0;y<16;y++)
for (x=0;x<16;x++) {
if (hsp[i].mask[l]) {
rastamix[hsp[i].y+y].clipped[hsp[i].x+x]=i+1; // index du sprite+1 dans les infos de clipping
}
l++;
}
}
if (igroup==0) {
/* first group colors initialised outside rastamix */
x=0;
for (j=16;j<31;j++) {
asicolinit[j]=((hsp[lastsp].palette[x*3+1]>>4)<<8)|(hsp[lastsp].palette[x*3+0]&0xF0)|((hsp[lastsp].palette[x*3+2]>>4)&0xF);
x++;
}
} else {
DBG
/* insert colors in rastamix */
asicad=0x6422;
x=0;
for (j=previouslasty;j<previouslasty+8;j++) {
rastamix[j].hspad=asicad;
asicad+=4;
rastamix[j].hspcol1=((hsp[lastsp].palette[x*3+1]>>4)<<8)|(hsp[lastsp].palette[x*3+0]&0xF0)|((hsp[lastsp].palette[x*3+2]>>4)&0xF);
x++;
rastamix[j].hspcol2=((hsp[lastsp].palette[x*3+1]>>4)<<8)|(hsp[lastsp].palette[x*3+0]&0xF0)|((hsp[lastsp].palette[x*3+2]>>4)&0xF);
x++;
}
}
previouslasty=lasty;
DBG
ImageRAWFreeStruct(&rawinfo);
}
DBG
/*************************************************************************************************************
**************************************************************************************************************
C A L C U L D E S P A L E T T E S P O U R T O U T L ' E C R A N
**************************************************************************************************************
*************************************************************************************************************/
/****************************************************************************
t r a v a i l s u r l e s 2 p r e m i è r e s l i g n e s
****************************************************************************/
for (j=0;j<192*2;j++) {
scanline[j*3+0]=(photo->data[j*6+0]+photo->data[j*6+3])*0.5;
scanline[j*3+1]=(photo->data[j*6+1]+photo->data[j*6+4])*0.5;
scanline[j*3+2]=(photo->data[j*6+2]+photo->data[j*6+5])*0.5;
}
DBG
/*****************************************************************
on élimine les données cachées par les sprites hard
*****************************************************************/
sizeclip=0;
for (j=0;j<192;j++) {
if (!rastamix[0].clipped[j*2] || !rastamix[0].clipped[j*2+1]) {
clippedscanline[sizeclip*3+0]=scanline[j*3+0];
clippedscanline[sizeclip*3+1]=scanline[j*3+1];
clippedscanline[sizeclip*3+2]=scanline[j*3+2];
sizeclip++;
}
if (!rastamix[1].clipped[j*2] || !rastamix[1].clipped[j*2+1]) {
clippedscanline[sizeclip*3+0]=scanline[j*3+0+192*3];
clippedscanline[sizeclip*3+1]=scanline[j*3+1+192*3];
clippedscanline[sizeclip*3+2]=scanline[j*3+2+192*3];
sizeclip++;
}
}
/* palette commune aux deux premières lignes */
refl=rastamix[0].l=colorz_create_palette(clippedscanline,sizeclip,rastamix[0].palette,0);
rastamix[1].l=refl;
memcpy(rastamix[1].palette,rastamix[0].palette,sizeof(rastamix[0].palette));
DBG
/****************************************************************************************
l i g n e s s u i v a n t e s . . .
****************************************************************************************/
reboot=0;
for (i=2;i<photo->height;i++) {
/* on utilise systématiquement les données de clipping pour constuire la clippedscan line */
sizeclip=0;
for (j=0;j<192;j++) {
/* on fait des pixels mode 0 à partir d'une résolution type mode 1 high color */
scanline[j*3+0]=(photo->data[j*6+0+i*384*3]+photo->data[j*6+3+i*384*3])*0.5;
scanline[j*3+1]=(photo->data[j*6+1+i*384*3]+photo->data[j*6+4+i*384*3])*0.5;
scanline[j*3+2]=(photo->data[j*6+2+i*384*3]+photo->data[j*6+5+i*384*3])*0.5;
if (!rastamix[i].clipped[j*2] || !rastamix[i].clipped[j*2+1]) {
clippedscanline[sizeclip*3+0]=scanline[j*3+0];
clippedscanline[sizeclip*3+1]=scanline[j*3+1];
clippedscanline[sizeclip*3+2]=scanline[j*3+2];
sizeclip++;
}
}
DBG
/****************************************************************************************
on fait une première passe pour avoir une palette complète dès la première ligne
****************************************************************************************/
if (refl<16) {
/* on calcule une palette complète pour la ligne */
l=colorz_create_palette(clippedscanline,sizeclip,tmppalette,0);
for (j=0;j<l && !reboot;j++) {
for (k=0;k<refl;k++) {
if (rastamix[0].palette[k*3+0]==tmppalette[j*3+0] &&
rastamix[0].palette[k*3+1]==tmppalette[j*3+1] &&
rastamix[0].palette[k*3+2]==tmppalette[j*3+2])
break;
}
/* c'est une nouvelle couleur */
if (k<refl) {
rastamix[0].palette[refl*3+0]=tmppalette[j*3+0];
rastamix[0].palette[refl*3+1]=tmppalette[j*3+1];
rastamix[0].palette[refl*3+2]=tmppalette[j*3+2];
refl++;
/* maj de la ligne 1 */
rastamix[1].l=refl;
memcpy(rastamix[1].palette,rastamix[0].palette,48);
if (refl==16) {
reboot=1;
}
}
}
if (reboot) {
i=1;