forked from alekmaul/gfx2pce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgfx2pce.c
1839 lines (1566 loc) · 43.4 KB
/
gfx2pce.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
/*---------------------------------------------------------------------------------
Copyright (C) 2012-2017
Alekmaul
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any
damages arising from the use of this software.
Permission is granted to anyone to use this software for any
purpose, including commercial applications, and to alter it and
redistribute it freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you
must not claim that you wrote the original software. If you use
this software in a product, an acknowledgment in the product
documentation would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and
must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
Image converter for pcengine.
Parts from pcx2snes from Neviksti
palette rounded option from Artemio Urbina
BMP BI_RLE8 compression support by Andrey Beletsky
***************************************************************************/
// 18/05/10 add offset per tile for map generation
//INCLUDES
#include "gfx2pce.h"
enum FILE_TYPE {
BMP_FILE = 1,
PCX_FILE,
TGA_FILE,
PNG_FILE
};
int quietmode=0; // 0 = not quiet, 1 = i can't say anything :P
int blanktile=0; // 1 = blank tile generated
int palette_rnd=0; // 1 = round palette up & down
int tilebase=0x1000; // default offset for tilebase
//// F U N C T I O N S //////////////////////////////////////////////////////////
int PCX_Load(char *filename, pcx_picture_ptr image)
{
// this function loads a pcx file into a picture structure, the actual image
// data for the pcx file is decompressed and expanded into a secondary buffer
// within the picture structure, the separate images can be grabbed from this
// buffer later. also the header and palette are loaded
long num_bytes,index;
long count;
long x,y;
unsigned char data;
pcx_header *header;
FILE *fp;
// open the file
fp = fopen(filename,"rb");
if(fp==NULL)
{
printf("\nERROR: Can't open file [%s]",filename);
return 0;
}
// load the header
header = &image->header;
fread(header, 1, 128, fp);
header->width++;
header->height++;
// check to make sure this is a 256 color PCX
if( (header->manufacturer != 10) ||
(header->encoding != 1) ||
(header->bits_per_pixel != 8) ||
(header->num_color_planes != 1) ||
(header->palette_type != 1) )
{
printf("\nERROR: File [%s] is not recognized as a 256 color PCX.",filename);
fclose(fp);
return 0;
}
//allocate memory for the picture + 64 empty lines
image->buffer = malloc( (size_t)(header->height+64)*header->width );
if(image->buffer == NULL)
{
printf("\nERROR: Can't allocate enough memory for the picture.");
fclose(fp);
return 0;
}
//initally clear the memory (to make those extra lines be blank)
memset(image->buffer,0,(size_t)(header->height+64)*header->width);
// load the data and decompress into buffer
count=0;
for(y=0; y < header->height; y++)
{
for(x=0; x < header->width; x++)
{
// get the first piece of data
data = getc(fp);
// is this a rle?
//if ( (data>=192) && (data<=255))
if (data>=192)
{
// how many bytes in run?
num_bytes = data-192;
x += (num_bytes-1);
// get the actual data for the run
data = getc(fp);
// replicate data in buffer num_bytes times
while( num_bytes-- > 0)
image->buffer[count++] = data;
} // end if rle
else
{
// actual data, just copy it into buffer at next location
image->buffer[count++] = data;
} // end else not rle
} //end of x loop
//get rid of the padding byte if there is one
if( x < header->bytes_per_line)
data = getc(fp);
} //end of y loop
//Get the Palette header (one byte, should equal 12)
data = getc(fp);
if(data != 12)
{
printf("\nERROR: Couldn't find palette header [%s]",filename);
free(image->buffer);
fclose(fp);
return 0;
}
//get the pallete data
for (index=0; index<256; index++)
{
image->palette[index].red = (getc(fp) >> 2);
image->palette[index].green = (getc(fp) >> 2);
image->palette[index].blue = (getc(fp) >> 2);
}
//check to make sure there weren't errors while reading the file
if(ferror(fp))
{
printf("\nERROR: Error reading file [%s]",filename);
free(image->buffer);
fclose(fp);
return 0;
}
fclose(fp);
return -1;
} // end PCX_Load
void BMP_BI_RLE8_Load(pcx_picture_ptr image,
const bmp_header* const bmphead, const bmp_info_header* const bmpinfohead,
FILE* fp)
{
// BI_RLE8 decompress according to:
// https://technet.microsoft.com/ru-ru/dd183383
unsigned long line, i, count;
// offset in image buffer where current line starts
unsigned int pos;
unsigned char ch, ch2;
// start from bottom line
line = bmpinfohead->biHeight;
pos = (line-1) * bmpinfohead->biWidth;
count = 0;
// read all image bytes
while (count < bmpinfohead->biSizeImage)
{
ch = getc(fp);
++count;
if (ch)
{
// repeat byte
ch2 = getc(fp);
++count;
for (i = 0; i < ch; ++i) image->buffer[pos++] = ch2;
continue;
}
// escape char
ch = getc(fp);
++count;
if (ch == 0)
{
// End of line.
// go one line up
--line;
// start of this line.
pos = (line-1) * bmpinfohead->biWidth;
}
else if (ch == 1)
{
// End of bitmap.
break;
}
else if (ch == 2)
{
// Delta.
// The two bytes following the escape contain unsigned values
// indicating the horizontal and vertical offsets of the next pixel
// from the current position.
ch = getc(fp);
++count;
// go right in the buffer
pos += ch;
ch = getc(fp);
++count;
// go given lines up
line -= ch;
pos -= bmpinfohead->biWidth * ch;
}
else
{
// Absolute mode.
// The second byte represents the number of bytes that follow,
// each of which contains the color index of a single pixel.
ch = getc(fp);
++count;
for (i = 0; i < ch; ++i)
{
image->buffer[pos++] = getc(fp);
++count;
}
if (i%2)
{
// Each run must be aligned on a word boundary.
// Read and throw away the placeholder.
ch2 = getc(fp);
++count;
}
}
}
} // end BMP_BI_RLE8_Load
int BMP_Load(char *filename, pcx_picture_ptr image)
{
// this function loads a bmp file into a picture structure, the actual image
// data for the bmp file is decompressed and expanded into a secondary buffer
// within the picture structure, the separate images can be grabbed from this
// buffer later. also the header and palette are loaded
FILE *fp;
int index,i;
pcx_header *header;
bmp_header bmphead;
bmp_info_header bmpinfohead;
// open the file
fp = fopen(filename,"rb");
if(fp==NULL)
{
printf("\nERROR: Can't open file [%s]",filename);
return 0;
}
// check to see if it is a valid bitmap file
if (fread(&bmphead, sizeof(bmp_header), 1, fp) < 1)
{
printf("\nERROR: File [%s] has no correct BMP header.",filename);
fclose(fp);
return 0;
}
if (bmphead.bfType != BF_TYPE)
{
printf("\nERROR: File [%s] is not recognized as a BMP file.",filename);
fclose(fp);
return 0;
}
// check to see if it is a valid bitmap file
if (fread(&bmpinfohead, sizeof(bmpinfohead), 1, fp) < 1)
{
printf("\nERROR: File [%s] has no correct BMP info header.",filename);
fclose(fp);
return 0;
}
if (bmpinfohead.biBitCount != 8 || (bmpinfohead.biCompression != 0 && bmpinfohead.biCompression != 1 /*BI_RLE8*/))
{
printf("\nERROR: File [%s] is not a valid BMP file: 256 colors, non-compressed or BI_RLE8 supported.",filename);
fclose(fp);
return 0;
}
// seek to palette
fseek(fp, sizeof(bmp_header) + bmpinfohead.biSize, 0);
// initally clear the palette if there are less then 256 colors in the file
memset(image->palette, 0, (size_t)(256 * sizeof(RGB_color)));
// read the palette information
for (index = 0; index<256; index++)
{
image->palette[index].blue = getc(fp) >> 2;
image->palette[index].green = getc(fp) >> 2;
image->palette[index].red = getc(fp) >> 2;
//data=getc(fp);
getc(fp);
}
header = &image->header;
header->width = bmpinfohead.biWidth;
header->height = bmpinfohead.biHeight;
// allocate memory for the picture + 64 empty lines
image->buffer = malloc( (size_t)(header->height+64) * header->width );
if(image->buffer == NULL)
{
printf("\nERROR: Can't allocate enough memory for the picture.");
fclose(fp);
return 0;
}
// initally clear the memory (to make those extra lines be blank)
memset(image->buffer,0,(size_t)(header->height+64) * header->width);
// seek to image data
fseek(fp, bmphead.bfOffBits, 0);
// read the bitmap
if (bmpinfohead.biCompression == 0)
{
for(index=(header->height-1) * header->width;index>=0;index-=header->width)
for(i=0;i<header->width;i++)
image->buffer[index+i] = getc(fp);
}
else if (bmpinfohead.biCompression == 1)
{
// BI_RLE8
BMP_BI_RLE8_Load(image, &bmphead, &bmpinfohead, fp);
}
// check to make sure there weren't errors while reading the file
if(ferror(fp))
{
printf("\nERROR: Error reading file [%s]",filename);
free(image->buffer);
fclose(fp);
return 0;
}
fclose(fp);
return -1;
} // end BMP_Load
int TGA_Load(char *filename, pcx_picture_ptr image)
{
// this function loads a tga file into a picture structure, the actual image
// data for the bmp file is decompressed and expanded into a secondary buffer
// within the picture structure, the separate images can be grabbed from this
// buffer later. also the header and palette are loaded
FILE *fp;
long index,i;
tga_header tgahead;
pcx_header *header;
// open the file
fp = fopen(filename,"rb");
if(fp==NULL)
{
printf("\nERROR: Can't open file [%s]",filename);
return 0;
}
// check to see if it is a valid bitmap file
if (fread(&tgahead, sizeof(tga_header), 1, fp) < 1)
{
printf("\nERROR: File [%s] has no correct TGA header.",filename);
fclose(fp);
return 0;
}
//check to make sure there weren't errors while reading the file
if(ferror(fp))
{
printf("\nERROR: Error reading file [%s]",filename);
free(image->buffer);
fclose(fp);
return 0;
}
if (tgahead.BPP != 8 || tgahead.ImageType != 1)
{
printf("\nERROR: File [%s] is not a valid indexed 256 colors TGA file.",filename);
fclose(fp);
return 0;
}
header = &image->header;
header->width = tgahead.Width;
header->height = tgahead.Height;
//allocate memory for the picture + 64 empty lines
image->buffer = malloc( (size_t)(header->height+64) * header->width );
if(image->buffer == NULL)
{
printf("\nERROR: Can't allocate enough memory for the picture.");
fclose(fp);
return 0;
}
//initally clear the memory (to make those extra lines be blank)
memset(image->buffer,0,(size_t)(header->height+64) * header->width);
// read the palette information
for(index=0;index<256;index++)
{
image->palette[index].blue = getc(fp) >> 2;
image->palette[index].green = getc(fp) >> 2;
image->palette[index].red = getc(fp) >> 2;
}
// read the bitmap
for(index=(header->height-1) * header->width;index>=0;index-=header->width)
for(i=0;i<header->width;i++)
image->buffer[index+i] = getc(fp);
fclose(fp);
return -1;
} // end TGA_Load
int PNG_Load(char *filename, pcx_picture_ptr image)
{
unsigned error,sz,bpp;
long i,index;
unsigned char *pngimage;
unsigned char* png = 0;
size_t pngsize;
LodePNGState state;
unsigned int width, height;// , wal,hal;
pcx_header *header;
/*optionally customize the state*/
lodepng_state_init(&state);
// no conversion of color (to keep palette mode)
state.decoder.color_convert = 0;
error = lodepng_load_file(&png, &pngsize, filename);
if (!error) {
error = lodepng_decode(&pngimage, &width, &height, &state, png, pngsize);
}
if(error) {
printf("\nERROR: Decoder error %u: %s\n", error, lodepng_error_text(error));
free(png);
lodepng_state_cleanup(&state);
free(pngimage);
return 0;
}
bpp = state.info_raw.bitdepth;
if ( (bpp != 4) && (bpp != 8)) {
printf("\nERROR: File [%s] is not a valid bbp value (%d bpp).",filename,bpp);
free(png);
lodepng_state_cleanup(&state);
free(pngimage);
return 0;
}
if (state.info_raw.colortype != LCT_PALETTE)
{
printf("\nERROR: File [%s] is not a valid indexed palette mode (mode %d).",filename,state.info_raw.colortype);
free(png);
lodepng_state_cleanup(&state);
free(pngimage);
return 0;
}
// read the palette information
sz=state.info_png.color.palettesize;
for(index=0;index<sz;index++) {
image->palette[index].red = state.info_png.color.palette[(index*4) + 0]>>2;
image->palette[index].green = state.info_png.color.palette[(index*4) + 1]>>2;
image->palette[index].blue = state.info_png.color.palette[(index*4) + 2]>>2;
}
// get png information
header = &image->header;
header->width = width;
header->height = height;
printf("\nWidth %d Height %d",header->width,header->height);
//allocate memory for the picture + 64 empty lines
image->buffer = malloc( (size_t)(header->height+64) * header->width );
if(image->buffer == NULL) {
printf("\nERROR: Can't allocate enough memory for the picture.");
return 0;
}
//initally clear the memory (to make those extra lines be blank)
memset(image->buffer,0,(size_t)(header->height+64) * header->width);
// 4 bpps conversion
if (bpp==4) {
for (index = 0; index < header->height; index++) {
for(i=0;i<header->width;i++)
image->buffer[index+i] = pngimage[i +index*header->height];
}
/*
// get buffer size
*size = (wAligned / 2) * hAligned;
// and alloc
result = malloc(*size);
srcPix = 0;
for (i = 0; i < h; i++)
{
unsigned char *dst = &result[i * (wAligned / 2)];
memset(dst, 0, wAligned / 2);
for (j = 0; j < w; j++)
{
unsigned char v;
if (srcPix & 1) v = (out[srcPix / 2] >> 4) & 0xF;
else v = (out[srcPix / 2] >> 0) & 0xF;
srcPix++;
if (j & 1) dst[j / 2] = (dst[j / 2] & 0x0F) | (v << 4);
else dst[j / 2] = (dst[j / 2] & 0xF0) | (v << 0);
}
}
for(;i < hAligned; i++)
memset(&result[i * (wAligned / 2)], 0, wAligned / 2);
*/
}
// 8 bpps conversion
else {
for (index = 0; index < header->height; index++) {
for(i=0;i<header->width;i++) {
image->buffer[i+(header->width*index)] = pngimage[i+(header->width*index)];
}
}
}
free(png);
lodepng_state_cleanup(&state);
free(pngimage);
return -1;
} // end PNG_Load
//////////////////////////////////////////////////////////////////////////////
void PutWord(int data, FILE *fp)
{
putc(LOW_BYTE(data),fp);
putc(HI_BYTE(data),fp);
} //end of PutWord
//////////////////////////////////////////////////////////////////////////////
unsigned char *ArrangeBlocks( unsigned char *img, int width, int height,
int size, int *xsize, int *ysize, int new_width, int border)
{
/*
** img = image buffer
** width = width (in pixels) of image buffer
** height = height (in pixels) of image buffer
**
** size = size (in pixels) of image blocks in the image
** *xsize = number of image block horizontally in image block grid
** *ysize = number of image block vertically in image block grid
**
** border = how wide of a border surrounds the image blocks
** new_width = how wide (in pixels) you want the new buffer to be
** must be a multiple of size
**
**
** returns:
** pointer to new buffer, and updates xsize and ysize
**
*/
unsigned char *buffer;
int rows, num;
int i,j, line;
int x,y;
if (quietmode == 0)
printf("\nwidth=%d, height=%d, size=%d, *xsize=%d, *ysize=%d, new_width=%d, border=%d",
width, height, size, *xsize, *ysize, new_width, border);
//get number of full image block rows in the new buffer
rows = (*xsize)*(*ysize)/(new_width/size); // rows = num_blocks / new_xsize
//if it doesn't divide evenly, add another full row
if ( ((*xsize)*(*ysize))%(new_width/size) != 0 )
rows++;
if (quietmode == 0)
printf("\nrows=%d",rows);
//get memory for the new buffer
buffer = malloc( (size_t) rows*size*new_width );
if(buffer == NULL) {
printf("\nERROR: Can't allocate enough memory for the buffer in ArrangeBlocks.");
return 0;
}
//initially clear the buffer, so if there are empty image blocks
//or incomplete blocks, the empty parts will be blank
memset(buffer,0,(size_t) rows*size*new_width );
//position in new buffer (x,y) where x and y are in pixel co-ordinates
x=0;
y=0;
//go through each image block(i,j) where i and j are in block co-ordinates
for(j=0; j < *ysize; j++)
for(i=0; i < *xsize; i++)
{
//move each line of the block into the new buffer
//don't worry about reading past the end of the image here
//there is an extra 64 lines to read in.
for(line=0;line<size;line++)
{
//find out how much to copy
//this is needed because the screen mode files may not be
//a multiple of 8 pixels wide
//or no-border files may have the wrong width
num = width - (i*(size+border) + border);
if(num>size)
num=size;
memcpy( &buffer[ (y+line)*new_width + x ],
&img[ (j*(size+border) + line + border)*width + i*(size+border) + border ],
num);
}
//move to the next location in the new buffer
x+=size;
if(x >= new_width)
{
x=0;
y+=size;
}
}
*xsize = new_width/size;
*ysize = rows;
return buffer;
} //end of ArrangeBlocks()
//////////////////////////////////////////////////////////////////////////////
int *MakeMap(unsigned char *img, int *num_tiles,
int xsize, int ysize, int tile_x, int tile_y, int colors, int rearrange, int pal_entry, int tile_reduction)
{
int *map;
unsigned char blank[64];
//int tiles = *num_tiles;
int newtiles;
int blank_absent;
int current; //the current tile we're looking at
int i,t, palette;
int x,y;
//int x_offset, y_offset;
//find x_offset,y_offset
//don't center
//x_offset=0;
//y_offset=0;
//allocate map
map=malloc((size_t)tile_x*tile_y*sizeof(int));
if(map==NULL) {
printf("\nERROR: Can't allocate enough memory for the map in MakeMap.");
return 0;
}
//clear map
memset(map,0,tile_x*tile_y*sizeof(int));
//if the palette has been rearranged... save the palette number
//if(rearrange)
{
current=0;
for(y=0;y<ysize;y++)
for(x=0;x<xsize;x++)
{
//get the palette number
palette = (img[current*64] >> 4) & 0x07;
t = ((palette+pal_entry) << 12);
//put tile number in map
if(tile_x==64 && tile_y==32) // 64x32 screen
{
if(x<32)
map[y*32 + x]=t;
else
map[(y+32)*32+x-32]=t;
}
else if(tile_x==32 && tile_y==64) // 32x64 screen
map[y*32+x]=t;
else if(tile_x==64 && tile_y==64) // 64x64 screen
{
if(y<32)
if(x<32)
map[y*32+x]=t;
else
map[(y+32)*32+x-32]=t;
else
if(x<32)
map[(y+64-32)*32+x]=t;
else
map[(y+96-32)*32+x-32]=t;
}
else //32x32 or 128x128 screen
map[y*tile_x+x]=t;
//goto the next tile
current++;
}
} //end of if(rearrange)
//truncate the colors if necessary
if(colors != 256)
{
t = colors - 1; //color truncation mask
for(i=0;i<xsize*ysize*64;i++)
img[i] = img[i] & t;
}
//make a blank tile
memset(blank,0,64);
//I want tile #0 to be blank..
//is it?
if (blanktile==1 )
{
if( memcmp(blank,img,64) == 0 )
{
blank_absent=0;
current=1;
t=0;
newtiles=1;
}
else
{
blank_absent=1;
current=1;
t=1;
newtiles=1;
}
}
else
{
blank_absent=0;
current=1;
t=0;
newtiles=1;
}
//save the first tilemap piece
map[0] += t;
for(y=0;y<ysize;y++)
for(x=0;x<xsize;x++)
{
//if we already processed this, move on
if(x==0 && y==0)
continue;
// if no reduction
if (tile_reduction) {
//is the current tile blank?
if( memcmp(blank,&img[current*64],64) == 0 )
t=0;
else
{
//check for matches with previous tiles if tile_reduction on
for(i=0;i<newtiles;i++)
if( memcmp(&img[i*64],&img[current*64],64) == 0 )
break;
//is it a new tile?
if(i==newtiles)
{
// yes -> add it
memcpy(&img[newtiles*64],&img[current*64],64);
t=newtiles+blank_absent;
newtiles++;
}
else
{ // no -> find what tile number it is
t=i+blank_absent;
}
}
}
// else, always a new tile
else {
i = newtiles;
// yes -> add it
memcpy(&img[newtiles*64],&img[current*64],64);
t=newtiles+blank_absent;
newtiles++;
}
//put tile number in map
if(tile_x==64 && tile_y==32) // 64x32 screen
{
if(x<32)
map[y*32 + x] += t;
else
map[(y+32)*32+x-32] += t;
}
else if(tile_x==32 && tile_y==64) // 32x64 screen
map[y*32+x] += t;
else if(tile_x==64 && tile_y==64) // 64x64 screen
{
if(y<32)
if(x<32)
map[y*32+x] += t;
else
map[(y+32)*32+x-32] += t;
else
if(x<32)
map[(y+64-32)*32+x] += t;
else
map[(y+96-32)*32+x-32] += t;
}
else //32x32 or 128x128 screen
map[y*tile_x+x] += t;
//goto the next tile
current++;
}
//also return the number of new tiles
//make it negative if we need to add the blank tile
if (blank_absent)
*num_tiles = -newtiles;
else
*num_tiles = newtiles;
return map;
}//end of MakeMap
//////////////////////////////////////////////////////////////////////////////
int RearrangePalette(unsigned char *buffer, int *palette,
int num_tiles, int colors)
{
int final[8];
int num_final;
int *combos;//holds sorted list of colors in combo of each tile
int *num; //holds number of colors in each combo
int *list; //for sorting combos
int n;
int new_palette[256];
int color_table[256];
int index, last_index;
int test, test2;
int num_miss;
int data;
int i,ii;
//get memory
num=malloc(num_tiles*sizeof(int));
if(num==NULL)
{
printf("\nERROR: Not enough memory to do rearrangement calculations.\n");
return 0;
}
combos=malloc(num_tiles*16*sizeof(int));
if(combos==NULL)
{
printf("\nERROR: Not enough memory to do rearrangement calculations.\n");
free(num);
return 0;
}
list=malloc(num_tiles*sizeof(int));
if(list==NULL)
{
printf("\nERROR: Not enough memory to do rearrangement calculations.\n");
free(combos);
free(num);
return 0;
}
//clear 'color combo' lists
memset(combos,0,num_tiles*16*sizeof(int));
//start each list having one color... color zero
for(i=0;i<num_tiles;i++)
num[i]=1;
//if two colors have the same RGB values...
//replace all instances of the redundant color with the first color
for(i=0;i<256;i++)
for(ii=i+1;ii<256;ii++)
if(palette[i]==palette[ii]) {
for(index=0; index < num_tiles*8*8; index++)
if(buffer[index]==ii)
buffer[index]=i;
}
//now, build up the 'color combo' list...
for(index=0;index<num_tiles;index++)
for(i=0;i<64;i++)
{
data=buffer[index*64+i];
//is this color already in the list?
for(ii=0;ii<num[index];ii++)
if(combos[index*16+ii]==data)
break;
//if not add it to the list
if(ii==num[index])
{
if(num[index]==colors) //combo is full
{
printf("\nERROR: Detected more colors in one 8x8 tile than is allowed.\n");
free(list);
free(combos);
free(num);
return 0;
}
combos[index*16+ii]=data;
num[index]++;
}
}
//now sort combos in order of number of colors (greatest to least)
//here's some more horrid code... I know this is all messy and
//slow, but hey... I just don't care right now.
n=0;
for(ii=colors;ii>0;ii--)
for(i=0;i<num_tiles;i++)
if(num[i]==ii)
list[n++]=i;
//ok, now try to combine the combos
last_index=-1;
for(num_final=0;num_final<9;num_final++)
{
//start looking for next 'non-combined' combo in the list
for(index=last_index+1; index<num_tiles; index++)
if(num[list[index]]>0)
break;
//if none... we're done
if(index==num_tiles)
break;
// test = combo # of new 'final combo'
test=list[index];
last_index=index;
//check if we've failed