forked from teamhimeh/simutrans
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simgraph8.c
3408 lines (2831 loc) · 73.9 KB
/
simgraph8.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) 2001 Hansjörg Malthaner
*
* This file is part of the Simugraph engine and may not be used
* in other projects without written permission of the author.
*
* Usage for Iso-Angband is granted.
*/
/* simgraph.c
*
* Versuch einer Graphic fuer Simulationsspiele
* Hj. Malthaner, Aug. 1997
*
*
* 3D, isometrische Darstellung
*
*
*
* 18.11.97 lineare Speicherung fuer Images -> hoehere Performance
* 22.03.00 run längen Speicherung fuer Images -> hoehere Performance
* 15.08.00 dirty tile verwaltung fuer effizientere updates
*/
#undef DEBUG_FLUSH_BUFFER
//#define DEBUG_FLUSH_BUFFER
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <math.h>
#include <zlib.h>
#include "pathes.h"
#include "simtypes.h"
#include "simsys.h"
#include "simmem.h"
#include "simdebug.h"
#include "besch/bild_besch.h"
//#include "utils/writepcx.h"
//#include "utils/image_encoder.h"
#ifdef _MSC_VER
#include <io.h>
#include <direct.h>
#define W_OK 2
#else
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#endif
#include "simgraph.h"
/*
* Hajo: RGB 888 padded to 32 bit
*/
typedef unsigned int PIXRGB;
/*
* Hajo: paletted
*/
typedef unsigned char PIXVAL;
/*
* Use C implementation of image drawing routines
*/
//#define USE_C
// -------------- static data --------------
static int softpointer = -1;
static int standard_pointer = -1;
/*
* die icon leiste muss neu gezeichnet werden wenn der
* Mauszeiger darueber schwebt
*/
int old_my = -1;
/* Flag, if we have Unicode font => do unicode (UTF8) support! *
* @author prissi
* @date 29.11.04
*/
static bool has_unicode = false;
#ifdef USE_SMALL_FONT
static font_type large_font, small_font;
#else
static font_type large_font;
#define small_font (large_font)
#endif
// needed for gui
int large_font_height=10;
PIXVAL colortable_8bit[3*256]=
{
65, 96, 122,
105, 135, 165,
146, 176, 210,
187, 216, 255,
110, 110, 110,
148, 148, 148,
186, 186, 186,
225, 225, 225,
37, 74, 142,
60, 106, 177,
83, 138, 212,
107, 170, 247,
137, 110, 19,
175, 155, 28,
212, 200, 36,
250, 245, 45,
102, 43, 22,
147, 61, 30,
191, 79, 37,
237, 98, 44,
47, 77, 21,
68, 118, 33,
90, 159, 45,
112, 200, 58,
14, 105, 97,
19, 149, 137,
25, 192, 176,
31, 236, 217,
105, 41, 148,
146, 67, 180,
186, 92, 211,
227, 118, 244,
0x57, 0x65, 0x6F, // Dark windows, lit yellowish at night
0x7F, 0x9B, 0xF1, // Lighter windows, lit blueish at night
0xFF, 0xFF, 0x53, // Yellow light
0xFF, 0x21, 0x1D, // Red light
0x01, 0xDD, 0x01, // Green light
0x6B, 0x6B, 0x6B, // Non-darkening grey 1 (menus)
0x9B, 0x9B, 0x9B, // Non-darkening grey 2 (menus)
0xB3, 0xB3, 0xB3, // non-darkening grey 3 (menus)
0xC9, 0xC9, 0xC9, // Non-darkening grey 4 (menus)
0xDF, 0xDF, 0xDF, // Non-darkening grey 5 (menus)
0xE3, 0xE3, 0xFF, // Nearly white light at day, yellowish light at night
0xC1, 0xB1, 0xD1, // Windows, lit yellow
0x4D, 0x4D, 0x4D, // Windows, lit yellow
32, 64, 255, // blue for non-zomables (non darkening)
80, 150, 30, // grass green
255, 32, 0, // very red
222, 52, 52, // light red
225, 192, 0, // red yellow
160, 80, 0, // orange
180, 128, 96, // earth color
136, 171, 211, // light player color
57, 94, 124, // player color
128, 255, 128, // blueish green
225, 255, 128, // very light green
161, 110, 120, // normal darkening colors again
84, 125, 118,
38, 21, 21,
165, 186, 145,
91, 60, 88,
255, 255, 251,
66, 97, 93,
176, 163, 197,
124, 90, 138,
238, 242, 232,
132, 156, 176,
185, 164, 144,
75, 96, 65,
184, 205, 208,
97, 77, 118,
174, 130, 140,
26, 40, 36,
143, 181, 143,
90, 66, 63,
232, 219, 219,
93, 137, 134,
191, 188, 160,
197, 183, 210,
49, 42, 63,
150, 99, 133,
82, 68, 102,
113, 164, 137,
212, 229, 222,
40, 36, 26,
72, 52, 79,
22, 39, 22,
21, 21, 38,
0, 0, 0,
22, 22, 22,
46, 46, 46,
61, 61, 61,
78, 78, 78,
95, 95, 95,
113, 113, 113,
132, 132, 132,
152, 152, 152,
172, 172, 172,
191, 191, 191,
211, 211, 211,
231, 231, 231,
247, 247, 247,
255, 255, 255,
96, 22, 13,
135, 46, 22,
170, 122, 104,
220, 195, 186,
198, 125, 107,
248, 199, 190,
128, 101, 86,
142, 118, 100,
150, 150, 107,
178, 153, 110,
206, 156, 114,
212, 188, 121,
254, 231, 197,
247, 209, 162,
129, 78, 56,
183, 129, 91,
238, 180, 126,
247, 222, 131,
254, 254, 138,
173, 69, 31,
212, 90, 39,
219, 97, 104,
100, 75, 24,
128, 79, 28,
156, 115, 37,
156, 82, 31,
191, 117, 42,
202, 145, 49,
219, 121, 45,
225, 152, 52,
107, 107, 31,
135, 110, 34,
141, 141, 42,
169, 145, 45,
204, 179, 55,
212, 212, 62,
232, 184, 59,
239, 215, 65,
114, 114, 97,
100, 122, 79,
121, 145, 104,
184, 184, 117,
156, 181, 114,
226, 226, 193,
204, 226, 161,
255, 255, 204,
49, 70, 39,
72, 72, 21,
176, 176, 52,
191, 216, 125,
163, 212, 120,
198, 247, 131,
170, 244, 128,
79, 103, 28,
113, 138, 38,
148, 173, 49,
120, 169, 45,
184, 207, 59,
128, 201, 52,
44, 69, 18,
52, 92, 23,
64, 129, 31,
85, 135, 34,
92, 166, 41,
99, 197, 48,
134, 232, 59,
106, 229, 55,
87, 111, 94,
164, 188, 180,
136, 185, 176,
199, 223, 190,
171, 220, 186,
206, 254, 197,
48, 90, 63,
93, 142, 100,
128, 178, 110,
100, 173, 107,
146, 200, 169,
143, 216, 183,
135, 209, 117,
8, 41, 8,
19, 84, 18,
19, 64, 51,
31, 104, 86,
37, 135, 93,
72, 170, 104,
45, 143, 159,
66, 214, 238,
42, 150, 133,
53, 189, 170,
43, 190, 41,
114, 139, 162,
157, 157, 173,
122, 122, 162,
157, 181, 208,
235, 235, 255,
207, 231, 255,
200, 200, 248,
179, 228, 252,
51, 76, 83,
73, 98, 119,
101, 150, 165,
94, 119, 159,
88, 88, 152,
172, 197, 245,
165, 165, 238,
144, 192, 242,
66, 116, 155,
83, 114, 185,
100, 144, 206,
137, 161, 235,
130, 130, 228,
116, 189, 238,
109, 158, 232,
102, 126, 224,
25, 31, 94,
41, 79, 122,
43, 55, 135,
53, 85, 147,
38, 111, 152,
81, 154, 228,
74, 123, 221,
67, 92, 214,
74, 28, 22,
107, 83, 89,
114, 96, 116,
168, 144, 169,
185, 160, 176,
223, 192, 223,
255, 238, 255,
228, 203, 253,
68, 47, 48,
135, 87, 94,
150, 126, 166,
178, 129, 169,
213, 164, 180,
193, 169, 242,
221, 172, 245,
241, 168, 183,
116, 91, 156,
144, 94, 159,
172, 98, 162,
163, 90, 97,
190, 104, 159,
191, 94, 100,
214, 141, 238,
94, 26, 65,
97, 25, 131,
142, 64, 112,
140, 54, 165,
184, 83, 200,
207, 109, 232,
241, 255, 255
};
// the palette for the display ...
static PIXVAL textur_palette[256*3];
// the palette actually used for the display ...
static PIXVAL day_pal[256*3];
// the palette with player colors
static PIXVAL special_pal[256*3];
/*
* Hajo: Currently selected color set for player 0 (0..15)
*/
static int selected_player_color_set = 0;
/*
* Hajo: Image map descriptor struture
*/
struct imd {
unsigned char base_x; // current min x offset
unsigned char base_w; // current width
unsigned char base_y; // current min y offset
unsigned char base_h; // current width
unsigned char x; // current (zoomed) min x offset
unsigned char w; // current (zoomed) width
unsigned char y; // current (zoomed) min y offset
unsigned char h; // current (zoomed) width
unsigned int len; // base image data size (used for allocation purposes only)
unsigned char recode_flags[4]; // first byte: needs recode, second byte: code normal, second byte: code for player1
PIXVAL * data; // current data, zoomed and adapted to output format RGB 555 or RGB 565
PIXVAL * zoom_data; // zoomed original data
PIXVAL * base_data; // original image data
PIXVAL * player_data; // current data coded for player1 (since many building belong to him)
}; /* exactly 32 bit: This should make thigns faster ... */
// offsets in the recode array
#define NEED_REZOOM (0)
#define NEED_NORMAL_RECODE (1)
#define NEED_PLAYER_RECODE (2)
#define ZOOMABLE (3)
int disp_width = 640;
int disp_height = 480;
/*
* Image table
*/
struct imd *images = NULL;
/*
* Number of loaded images
*/
static int anz_images = 0;
/*
* Number of allocated entries for images
* (>= anz_images)
*/
static int alloc_images = 0;
/*
* Output framebuffer
*/
PIXVAL *textur = NULL;
/*
* Hajo: Current clipping rectangle
*/
static struct clip_dimension clip_rect;
/*
* Hajo: dirty tile management strcutures
*/
#define DIRTY_TILE_SIZE 16
#define DIRTY_TILE_SHIFT 4
static unsigned char *tile_dirty = NULL;
static unsigned char *tile_dirty_old = NULL;
static int tiles_per_line = 0;
static int tile_lines = 0;
static int tile_buffer_length = 0;
static int light_level = 0;
static int color_level = 1;
static int night_shift = -1;
#define LIGHT_COUNT 13
/*
* Hajo: speical colors during daytime
*/
static const int day_lights[LIGHT_COUNT] =
{
0x57656F, // Dark windows, lit yellowish at night
0x7F9BF1, // Lighter windows, lit blueish at night
0xFFFF53, // Yellow light
0xFF211D, // Red light
0x01DD01, // Green light
0x6B6B6B, // Non-darkening grey 1 (menus)
0x9B9B9B, // Non-darkening grey 2 (menus)
0xB3B3B3, // non-darkening grey 3 (menus)
0xC9C9C9, // Non-darkening grey 4 (menus)
0xDFDFDF, // Non-darkening grey 5 (menus)
0xE3E3FF, // Nearly white light at day, yellowish light at night
0xC1B1D1, // Windows, lit yellow
0x4D4D4D, // Windows, lit yellow
};
/*
* Hajo: speical colors during nighttime
*/
static const int night_lights[LIGHT_COUNT] =
{
0xD3C380, // Dark windows, lit yellowish at night
0x80C3D3, // Lighter windows, lit blueish at night
0xFFFF53, // Yellow light
0xFF211D, // Red light
0x01DD01, // Green light
0x6B6B6B, // Non-darkening grey 1 (menus)
0x9B9B9B, // Non-darkening grey 2 (menus)
0xB3B3B3, // non-darkening grey 3 (menus)
0xC9C9C9, // Non-darkening grey 4 (menus)
0xDFDFDF, // Non-darkening grey 5 (menus)
0xFFFFE3, // Nearly white light at day, yellowish light at night
0xD3C380, // Windows, lit yellow
0xD3C380, // Windows, lit yellow
};
PIXVAL *conversion_table=0;
/*
* Hajo: tile raster width
*/
int tile_raster_width = 16;
static int base_tile_raster_width = 16;
/*
* Hajo: Zoom factor
*/
static int zoom_factor = 1;
// -------------- Function prototypes --------------
void display_img_nc(int h, const int xp, const int yp, const PIXVAL *);
static void rezoom();
static void calc_base_pal_from_night_shift(const int night);
/**
* Zeichnet Bild mit Clipping
* @author Hj. Malthaner
*/
static void
display_img_wc(int h, const int xp, const int yp, const PIXVAL *sp);
/**
* Convert a certain image data to actual output data
* @author Hj. Malthaner
*/
static void recode_img_src16_target8( const int nr, PIXVAL *src, PIXVAL *target, bool non_night);
/**
* Convert base image data to actual image size
* @author Hj. Malthaner
*/
static void rezoom_img( const unsigned int n );
/**
* If arguments are supposed to be changed during the call
* use this makro instead of pixcopy()
*/
//#define PCPY(d, s, l) while(l--) *d++ = rgbmap[*s++];
//#define PCPY(d, s, l) while(l--) *d++ = *s++;
// #define PCPY(d, s, l) if(runlen) do {*d++ = *s++;} while(--l)
// Hajo: unrolled loop is about 5 percent faster
#define PCPY(d, s, l) if(l & 1) {*d++ = *s++; l--;} {unsigned short *ld =(unsigned short *)d; const unsigned short *ls =(const unsigned short *)s; d+=l; s+=l; l >>= 1; while(l--) {*ld++ = *ls++;}}
// -------------- Functions --------------
int get_zoom_factor()
{
return zoom_factor;
}
void set_zoom_factor(int z)
{
if(z>0) {
zoom_factor = z;
tile_raster_width = base_tile_raster_width/z;
}
fprintf(stderr, "set_zoom_factor() : factor=%d\n", zoom_factor);
rezoom();
}
#ifdef _MSC_VER
#define mark_tile_dirty(x,y) \
do { \
const int bit = (int)(x)+(int)(y)*tiles_per_line; \
\
/* assert(bit/8 < tile_buffer_length); */ \
\
tile_dirty[bit >> 3] |= 1 << (bit & 7); \
} while(FALSE)
#else
static inline void mark_tile_dirty(const int x, const int y)
{
const int bit = x+y*tiles_per_line;
// assert(bit/8 < tile_buffer_length);
tile_dirty[bit >> 3] |= 1 << (bit & 7);
}
#endif
#ifdef _MSC_VER
#define mark_tiles_dirty(x1, x2, y) \
do { \
int bit = y * tiles_per_line + x1; \
const int end = bit + ((int)(x2)-(int)(x1)); \
\
do { \
/* assert(bit/8 < tile_buffer_length);*/ \
\
tile_dirty[bit >> 3] |= 1 << (bit & 7); \
} while(++bit <= end); \
} while(FALSE)
#else
static inline void mark_tiles_dirty(const int x1, const int x2, const int y)
{
int bit = y * tiles_per_line + x1;
const int end = bit + (x2-x1);
do {
// assert(bit/8 < tile_buffer_length);
tile_dirty[bit >> 3] |= 1 << (bit & 7);
} while(++bit <= end);
}
#endif
#ifdef _MSC_VER
static int is_tile_dirty(const int x, const int y) {
const int bit = x+y*tiles_per_line;
const int bita = bit >> 3;
const int bitb = 1 << (bit & 7);
return (tile_dirty[bita] & (bitb)) |
(tile_dirty_old[bita] & (bitb));
}
#else
static inline int is_tile_dirty(const int x, const int y) {
const int bit = x+y*tiles_per_line;
const int bita = bit >> 3;
const int bitb = 1 << (bit & 7);
return (tile_dirty[bita] & (bitb)) |
(tile_dirty_old[bita] & (bitb));
}
#endif
/**
* Markiert ein Tile as schmutzig, ohne Clipping
* @author Hj. Malthaner
*/
static void mark_rect_dirty_nc(int x1, int y1, int x2, int y2)
{
// floor to tile size
x1 >>= DIRTY_TILE_SHIFT;
y1 >>= DIRTY_TILE_SHIFT;
x2 >>= DIRTY_TILE_SHIFT;
y2 >>= DIRTY_TILE_SHIFT;
/*
assert(x1 >= 0);
assert(x1 < tiles_per_line);
assert(y1 >= 0);
assert(y1 < tile_lines);
assert(x2 >= 0);
assert(x2 < tiles_per_line);
assert(y2 >= 0);
assert(y2 < tile_lines);
*/
for(; y1<=y2; y1++) {
#if 1//def USE_C
mark_tiles_dirty(x1, x2, y1);
#else
int bit = y1 * tiles_per_line + x1;
const int end = bit + (x2-x1);
do {
// assert(bit/8 < tile_buffer_length);
tile_dirty[bit >> 3] |= 1 << (bit & 7);
bit++;
} while(bit <= end);
#endif
}
}
/**
* Markiert ein Tile as schmutzig, mit Clipping
* @author Hj. Malthaner
*/
void mark_rect_dirty_wc(int x1, int y1, int x2, int y2)
{
// Hajo: inside display ?
if(x2 >= 0 &&
y2 >= 0 &&
x1 < disp_width &&
y1 < disp_height) {
if(x1 < 0)
x1 = 0;
if(y1 < 0)
y1 = 0;
if(x2 >= disp_width) {
x2 = disp_width-1;
}
if(y2 >= disp_height) {
y2 = disp_height-1;
}
mark_rect_dirty_nc(x1, y1, x2, y2);
}
}
/*
0x57656F, // Dark windows, lit yellowish at night
0x7F9BF1, // Lighter windows, lit blueish at night
0xFFFF53, // Yellow light
0xFF211D, // Red light
0x01DD01, // Green light
0x6B6B6B, // Non-darkening grey 1 (menus)
0x9B9B9B, // Non-darkening grey 2 (menus)
0xB3B3B3, // non-darkening grey 3 (menus)
0xC9C9C9, // Non-darkening grey 4 (menus)
0xDFDFDF, // Non-darkening grey 5 (menus)
0xE3E3FF, // Nearly white light at day, yellowish light at night
0xC1B1D1, // Windows, lit yellow
0x4D4D4D, // Windows, lit yellow
*/
#define SPECIAL 29
PIXVAL resample_player[SPECIAL]=
{
0, 0, 1, 1, 2, 3, 3
};
void init_16_to_8_conversion()
{
int red, green, blue;
printf("Building 16Bit to 8Bit conversion table ... ");
if(day_pal[0]==0) {
// not yet init?
memcpy( day_pal, colortable_8bit, 8*256 );
}
conversion_table = guarded_malloc( 32768+256 );
for( red=0; red<256; red+= 8) {
for( green=0; green<256; green+=8 ) {
for( blue=0; blue<256; blue+=8 ) {
const int index = (red<<7)|(green<<2)|(blue>>3);
PIXVAL best_match=88;
long distance=0x00FFFFFF;
int i;
for( i=4; i<256; i++ ) {
if(i==32) {
i += 24;
}
long new_dist = abs(red-day_pal[i*3])+abs(green-day_pal[i*3+1])+abs(blue-day_pal[i*3+2]);
if(new_dist<distance) {
distance = new_dist;
best_match = i;
}
}
conversion_table[index] = best_match;
}
}
}
// new the special colors
int index;
for( index=0; index<7; index++ ) {
conversion_table[index+32768] = index/2;
}
for( index=8; index<16; index++ ) {
conversion_table[index+32768] = 88;
}
for( index=16; index<40; index++ ) {
conversion_table[index+32768] = 16+index;
}
printf("ok.\n");
}
/**
* Convert a certain image data to actual output data for a certain player
* @author prissi
*/
static void recode_img_src16_target8( int h, PIXVAL *src16, PIXVAL *target, bool non_dark )
{
if(conversion_table==0) {
init_16_to_8_conversion();
}
unsigned short *src=(unsigned short *)src16;
if(h>0) {
do {
unsigned char runlen = *target ++ = *src++;
// eine Zeile dekodieren
do {
// clear run is always ok
runlen = *target++ = *src++;
// now just convert the color pixels
if(non_dark) {
while(runlen--) {
unsigned short pix = *src++;
*target++ = conversion_table[pix];
}
}
else {
// find best match by foot (slowly, but not often used)
while(runlen--) {
unsigned short pix = *src++;
long red=(pix>>7)&0x00F8;
long green=(pix>>2)&0x00F8;
long blue=(pix<<3)&0x00F8;
if(pix>0x8000) {
if(pix<0x8008) {
*target++ = pix>0x8004?52:53;
continue;
}
if(pix==0x8010 || pix==0x8011 || (pix>=0x801A && pix<0x801C)) {
pix = conversion_table[pix];
red = day_pal[pix*3];
green = day_pal[pix*3+1];
blue = day_pal[pix*3+2];
}
else {
*target++ = (pix&0x00FF)+16;
continue;
}
}
PIXVAL best_match=88;
long distance=red*red+green*green+blue*blue;
int i;
for( i=32+2; i<56; i++ ) {
if(i==42) {
i = 45;
}
long new_dist = (red-colortable_8bit[i*3])*(red-colortable_8bit[i*3])+(green-colortable_8bit[i*3+1])*(green-colortable_8bit[i*3+1])+(blue-colortable_8bit[i*3+2])*(blue-colortable_8bit[i*3+2]);
if(new_dist<distance) {
distance = new_dist;
best_match = i;
}
}
*target ++ = best_match;
}
}
// next clea run or zero = end
} while( (runlen = *target ++ = *src++) );
} while(--h);
}
}
/**
* Rezooms all images
* @author Hj. Malthaner
*/
static void rezoom()
{
int n;
for(n=0; n<anz_images; n++) {
images[n].recode_flags[NEED_REZOOM] = images[n].recode_flags[ZOOMABLE] && (images[n].base_h>0);
images[n].recode_flags[NEED_NORMAL_RECODE] = 128;
images[n].recode_flags[NEED_PLAYER_RECODE] = 128; // color will be set next time
// rezoom_img(n);
} // for
}
/**
* Convert base image data to actual image size
* @author prissi (to make this much faster) ...
*/
static void rezoom_img( const unsigned int n )
{
// Hajo: may this image be zoomed
if(n<anz_images && images[n].base_h>0) {
// we may need night conversion afterwards
images[n].recode_flags[NEED_REZOOM] = FALSE;
images[n].recode_flags[NEED_NORMAL_RECODE] = 128;
images[n].recode_flags[NEED_PLAYER_RECODE] = 128;
// just restore original size?
if(zoom_factor<=1) {
// this we can do be a simple copy ...
images[n].x = images[n].base_x;
images[n].w = images[n].base_w;
images[n].y = images[n].base_y;
images[n].h = images[n].base_h;
if(images[n].zoom_data!=NULL) {
guarded_free( images[n].zoom_data );
images[n].zoom_data = NULL;
}
return;
}
// now we want to downsize the image
// just divede the sizes
images[n].x = images[n].base_x/zoom_factor;
images[n].y = images[n].base_y/zoom_factor;
images[n].w = (images[n].base_x+images[n].base_w)/zoom_factor - images[n].x;
images[n].h = images[n].base_h/zoom_factor;
// images[n].h = ((images[n].base_y%zoom_factor)+images[n].base_h)/zoom_factor;
if(images[n].h>0 && images[n].w>0) {
// just recalculate the image in the new size
unsigned char y_left = (images[n].base_y+zoom_factor-1)%zoom_factor;
unsigned char h = images[n].base_h;
static PIXVAL line[512];
PIXVAL *src = images[n].base_data;
PIXVAL *dest, *last_dest;
// decode/recode linewise
unsigned int last_color=255; // ==255 to keep compiler happy
if(images[n].zoom_data==NULL) {
// normal len is ok, since we are only skipping parts ...
images[n].zoom_data = guarded_malloc( sizeof(PIXVAL)*images[n].len );
}
last_dest = dest = images[n].zoom_data;
do { // decode/recode line
unsigned int runlen;
unsigned int color = 0;
PIXVAL *p = line;
const int imgw=images[n].base_x+images[n].base_w;
// left offset, which was left by division
runlen = images[n].base_x%zoom_factor;
while( runlen-- ) {
*p++ = 54;
}
// decode line
runlen = *src++;
color -= runlen;
do {
// clear run
while( runlen-- ) {
*p++ = 54;
}
// color pixel
runlen = *src++;
color += runlen;
while( runlen-- ) {
*p++ = *src++;
}
} while( (runlen = *src++) );
if(y_left==0 || last_color<color) {
// required; but if the following are longer, take them instead (aviods empty pixels)
// so we have to set/save the beginning
unsigned char i, step=0;
if(y_left==0) {
last_dest = dest;
}
else {
dest = last_dest;
}
// encode this line
do {
// check length of transparent pixels
for( i=0; line[step]==54 && step<imgw; i++, step+=zoom_factor )
;