[d5d988]: mos.cpp Maximize Restore History

Download this file

mos.cpp    1584 lines (1444 with data), 41.7 kB

   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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
// mos.cpp: implementation of the Cmos class.
// portions came from the SK's bam handler
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "chitem.h"
#include "chitemDlg.h"
#include "mos.h"
#include "zlib.h"
#include "oct_quan.h"
#define PIXEL_INCREMENT 17
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// INF_MOS_FRAMEDATA helper class
//////////////////////////////////////////////////////////////////////
static int colourorder(const void *a, const void *b)
{
int asum, bsum;
int tmp;
int i;
//transparent colours go first
if((*(unsigned long *) a&0xffffff)==TRANSPARENT_GREEN)
{
return -1;
}
if((*(unsigned long *) b&0xffffff)==TRANSPARENT_GREEN)
{
return 1;
}
asum=0;
bsum=0;
for(i=0;i<3;i++)
{
tmp=*(((BYTE *) a)+i);
asum+=squares[tmp];
tmp=*(((BYTE *) b)+i);
bsum+=squares[tmp];
}
if(asum>bsum) return -1;
return asum<bsum;
}
DWORD INF_MOS_FRAMEDATA::GetColor(int x, int y)
{
return Palette[pFrameData[x+y*nWidth]];
}
void INF_MOS_FRAMEDATA::OrderPalette()
{
BYTE *pClr;
int i;
//mark palette so we'll know the original order
for(i=0;i<256;i++)
{
pClr = (BYTE *) (Palette+i);
*(pClr+3) = (unsigned char) i;
}
qsort(Palette, 256, 4, colourorder);
ReorderPixels();
//change the palette back to clean
for(i=0;i<256;i++)
{
pClr = (BYTE *) (Palette+i);
*(pClr+3) = 0;
}
}
void INF_MOS_FRAMEDATA::ReorderPixels()
{
int idx, color;
int i;
for(i=0;i<nFrameSize;i++)
{
color=pFrameData[i];
for(idx=0;idx<256;idx++)
{
if(*((BYTE *) (Palette+idx)+3)==color )
{
pFrameData[i]=(BYTE) idx;
break;
}
}
}
}
void INF_MOS_FRAMEDATA::MarkPixels()
{
BYTE *pClr;
int color;
int i;
//mark palette so we'll know which are unused
for(i=0;i<256;i++)
{
*((BYTE *) (Palette+i)+3)=1;
}
for(i=0;i<nFrameSize;i++)
{
color=pFrameData[i];
*((BYTE *) (Palette+color)+3)=0;
}
//drop unused entries (turning them black)
for(i=0;i<256;i++)
{
pClr = (BYTE *) (Palette+i);
if(*(pClr+3) ) memset(pClr,0,sizeof(COLORREF));
else *(pClr+3) = 0;
}
}
int INF_MOS_FRAMEDATA::TakePaletteData(const BYTE *pRawBits)
{
memcpy(&Palette,pRawBits,sizeof(Palette) );
return 0;
}
int INF_MOS_FRAMEDATA::TakeBmpData(const BYTE *pRawBits, int row, int offset)
{
memcpy(pFrameData+row*nWidth,pRawBits+offset,nWidth);
return nWidth;
}
//color reduction using Ian Ashdown's octree algorithm
int INF_MOS_FRAMEDATA::TakeWholeBmpData(const DWORD *pRawBits)
{
OctQuant oc;
return oc.BuildTree(pRawBits,pFrameData, CPoint(nHeight,nWidth), Palette);
}
bool INF_MOS_FRAMEDATA::IsTransparent(DWORD x, DWORD y)
{
if(x>=nWidth) return true;
if(y>=nHeight) return true;
return !pFrameData[y*nWidth+x];
}
int INF_MOS_FRAMEDATA::TakeMosData(const BYTE *pRawBits, DWORD width, DWORD height, bool deepen)
{
DWORD x,y;
nWidth=width;
nHeight=height;
nFrameSize=nWidth*nHeight;
if(pFrameData) delete [] pFrameData;
pFrameData=new BYTE[nFrameSize];
if(!pFrameData) return -3; //memory problem
if(pRawBits) //null pointer means only allocation
{
memcpy(pFrameData, pRawBits, nFrameSize);
if(deepen)
{
for(y=0;y<height;y++) for(x=0;x<width;x++)
{
if(IsTransparent(x-1,y) && IsTransparent(x+1,y) &&
IsTransparent(x,y-1) && IsTransparent(x,y+1) )
{
pFrameData[y*width+x]=0;
}
}
}
}
else
{
memset(pFrameData, 0, nFrameSize);
}
return 0;
}
int INF_MOS_FRAMEDATA::ExpandMosLine(char *pBuffer, int nSourceOff)
{
DWORD i;
for(i=0;i<nWidth;i++)
{
memcpy(pBuffer,Palette+pFrameData[nSourceOff],3);
pBuffer+=3;
nSourceOff++;
}
return 3*nWidth;
}
bool INF_MOS_FRAMEDATA::ExpandMosBitsWhole(COLORREF clrReplace, COLORREF clrTransparent, COLORREF *pDIBits, int nOffset, int wWidth)
{
int nCount;
int nPixelCount;
nCount=nWidth;
for(nPixelCount=0;nPixelCount < nFrameSize;nPixelCount++)
{
// If it is not compressed, still need to catch the transparent pixels and
// fill with the transparent color.
if(memcmp(Palette+pFrameData[nPixelCount],&clrReplace, 3) )
{
pDIBits[nPixelCount+nOffset]=Palette[pFrameData[nPixelCount]];
}
else
{
if(clrTransparent!=TRANSPARENT_GREEN)
{
pDIBits[nPixelCount+nOffset]=clrTransparent;
}
}
nCount--;
if(!nCount) //reached end of line
{
nOffset+=wWidth-nWidth;
nCount=nWidth;
}
}
return true;
}
int INF_MOS_FRAMEDATA::SaturateTransparency()
{
unsigned char pixel;
DWORD x,y;
int ret;
ret=0;
for(y=0;y<nHeight;y++) for(x=0;x<nWidth;x++)
{
if(!pFrameData[y*nWidth+x])
{
if(x) pixel = pFrameData[y*nWidth+x-1];
else pixel = pFrameData[y*nWidth+x+1];
pFrameData[y*nWidth+x]=pixel; //second attempt to remove transparency
ret=1;
}
}
return ret;
}
void INF_MOS_FRAMEDATA::AllocateTransparency()
{
DWORD x,y;
if(Palette[0]==TRANSPARENT_GREEN) return;
Palette[255]=Palette[0];
Palette[0]=TRANSPARENT_GREEN;
for(y=0;y<nHeight;y++) for(x=0;x<nWidth;x++)
{
if(!pFrameData[y*nWidth+x])
{
pFrameData[y*nWidth+x]=255;
}
}
return;
}
void INF_MOS_FRAMEDATA::FlipTile()
{
unsigned char *pos;
DWORD x,y;
unsigned char tmp;
if(!pFrameData) return;
pos=pFrameData;
for(y=0;y<nHeight;y++)
{
for(x=0;x<nWidth/2;x++)
{
tmp=pos[x];
pos[x]=pos[nWidth-x-1];
pos[nWidth-x-1]=tmp;
}
pos+=nWidth;
}
}
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
Cmos::Cmos()
{
memset(&mosheader,0,sizeof(INF_MOS_HEADER) );
memset(&tisheader,0,sizeof(tis_header) );
memset(&c_header,0,sizeof(INF_MOSC_HEADER) );
memcpy(&mosheader,"MOS V1 ",8);
memcpy(&c_header,"MOSCV1 ",8);
m_pFrameData=NULL;
m_pOffsets=NULL;
m_pclrDIBits=NULL;
m_DIBsize=0;
m_pData=NULL;
m_overlay=0;
m_drawclosed=0;
m_tileheaders=NULL;
m_tileindices=NULL;
}
Cmos::~Cmos()
{
new_mos();
}
void Cmos::new_mos()
{
KillData();
KillFrameData();
KillOffsets();
if(m_pclrDIBits)
{
delete [] m_pclrDIBits;
m_pclrDIBits=NULL;
m_DIBsize=0;
}
memset(&mosheader,0,sizeof(INF_MOS_HEADER) );
memset(&tisheader,0,sizeof(tis_header) );
memset(&c_header,0,sizeof(INF_MOSC_HEADER) );
memcpy(&mosheader,"MOS V1 ",8);
memcpy(&c_header,"MOSCV1 ",8);
m_overlay=0;
m_drawclosed=0;
m_tileheaders=NULL;
m_tileindices=NULL;
mosheader.dwBlockSize=64;
m_name.Empty();
m_changed=false;
}
int Cmos::RetrieveTisFrameCount(int fhandle, int ml)
{
if(ml<0)
{
read(fhandle,&tisheader,sizeof(tis_header) );
}
else SetTisParameters(ml);
return tisheader.numtiles;
}
int Cmos::GetFrameCount()
{
return tisheader.numtiles;
}
void Cmos::SetOverlay(int overlay, wed_tilemap *tileheaders, short *tileindices, Cmos *overlaymos)
{
if(overlay>0 && overlay<10)
{
m_overlay=1<<overlay;
m_friend=overlaymos;
}
else
{
m_overlay=overlay;
m_friend=NULL;
}
m_tileheaders=tileheaders;
m_tileindices=tileindices;
}
int Cmos::ResolveFrameNumber(int framenumber)
{
int idx;
if(!m_tileheaders) return framenumber;
if(m_tileheaders[framenumber].flags&m_overlay)
{
if(m_overlay)
{
idx=m_tileheaders[framenumber].alternate;
m_overlaytile=0;
return idx;
}
idx=m_tileheaders[framenumber].firsttileprimary;
if(m_tileindices) return m_tileindices[idx];
return idx;
}
if(m_drawclosed)
{
idx=m_tileheaders[framenumber].alternate;
if(idx!=-1) return idx;
}
idx=m_tileheaders[framenumber].firsttileprimary;
if(m_tileindices) return m_tileindices[idx];
return idx;
}
CPoint Cmos::GetFrameSize(DWORD nFrameWanted)
{
if (tisheader.numtiles <= nFrameWanted ) //not enough frames
return (DWORD) -1;
return CPoint(m_pFrameData[nFrameWanted].nWidth,m_pFrameData[nFrameWanted].nHeight);
}
unsigned char *Cmos::GetFrameBuffer(DWORD nFrameWanted)
{
if (tisheader.numtiles <= nFrameWanted ) //not enough frames
return NULL;
return m_pFrameData[nFrameWanted].pFrameData;
}
COLORREF Cmos::GetPixelData(int x, int y)
{
int nFrameWanted;
COLORREF *Palette;
nFrameWanted=ResolveFrameNumber(y/tisheader.pixelsize*mosheader.wColumn+x/tisheader.pixelsize);
Palette=GetFramePalette(nFrameWanted);
return Palette[m_pFrameData[nFrameWanted].pFrameData[y%tisheader.pixelsize*m_pFrameData[nFrameWanted].nWidth+x%tisheader.pixelsize]];
}
int Cmos::TakeOneFrame(DWORD nFrameWanted, COLORREF *prFrameBuffer, int factor)
{
int r,g,b; //color sums
DWORD x,y;
int z,w;
int point;
COLORREF color;
int sz, sw;
int oz, ow;
int rf;
if (tisheader.numtiles <= nFrameWanted ) //not enough frames
return -1;
point=0;
sw=nFrameWanted/mosheader.wColumn*tisheader.pixelsize*tisheader.pixelsize/factor;
sz=nFrameWanted%mosheader.wColumn*tisheader.pixelsize*tisheader.pixelsize/factor;
rf=tisheader.pixelsize/factor;
for(y=0;y<m_pFrameData[nFrameWanted].nHeight;y++)
{
for(x=0;x<m_pFrameData[nFrameWanted].nWidth;x++)
{
r=g=b=0;
oz=sz+x*tisheader.pixelsize/factor;
for(z=oz;z<oz+rf;z++)
{
ow=sw+y*tisheader.pixelsize/factor;
for(w=ow;w<ow+rf;w++)
{/////get the color value and sum it up
color=the_mos.GetPixelData(z,w);
r+=*(BYTE *) &color;
g+=*(((BYTE *) &color)+1);
b+=*(((BYTE *) &color)+2);
}
}
z=rf*rf;
prFrameBuffer[point++]=RGB(r/z,g/z,b/z); //average
}
}
return m_pFrameData[nFrameWanted].TakeWholeBmpData(prFrameBuffer);
}
unsigned long *Cmos::GetFramePalette(DWORD nFrameWanted)
{
if(!m_pFrameData) return NULL;
if (tisheader.numtiles <= nFrameWanted ) //not enough frames
return NULL;
return m_pFrameData[nFrameWanted].Palette;
}
bool Cmos::MakeBitmap(COLORREF clrTrans, Cmos &host, int nMode, int nXpos, int nYpos)
{
int nColumn, nRow, nBase, nOffset, nLimit;
DWORD nFrameWanted;
if(!m_pFrameData) return false;
if(!(nMode&BM_OVERLAY)) return false;
if(nMode&BM_MATCH) clrTrans=(DWORD) -1;
else if(nMode&BM_INVERT) clrTrans=(DWORD) -2;
//the size of the original bitmap
nColumn=host.mosheader.wColumn*host.mosheader.dwBlockSize;
nRow=host.mosheader.wRow*host.mosheader.dwBlockSize;
nBase=nXpos+nYpos*nColumn;
nLimit=host.mosheader.wRow-(nYpos+63)/host.mosheader.dwBlockSize;
for(nYpos=0;nYpos<mosheader.wRow && nYpos<nLimit;nYpos++)
{
nOffset=nBase+mosheader.dwBlockSize*nColumn*nYpos;
for(nXpos=0;nXpos<mosheader.wColumn;nXpos++)
{
m_overlaytile=-1;
nFrameWanted=ResolveFrameNumber(nYpos*mosheader.wColumn+nXpos);
if(!m_friend) m_overlaytile=-1;
if(m_overlaytile!=-1)
{
m_friend->m_pFrameData[m_overlaytile].ExpandMosBitsWhole(TRANSPARENT_GREEN,clrTrans,host.GetDIB(),nOffset, nColumn);
}
if(nFrameWanted<tisheader.numtiles)
{
if(!m_pFrameData[nFrameWanted].ExpandMosBitsWhole(TRANSPARENT_GREEN,m_overlaytile!=-1?TRANSPARENT_GREEN:clrTrans,host.GetDIB(),nOffset, nColumn))
{
return false;
}
}
nOffset+=mosheader.dwBlockSize;
}
}
return true;
}
bool Cmos::MakeBitmapWhole(COLORREF clrTrans, HBITMAP &hBitmap, int clipx, int clipy, int maxclipx, int maxclipy, bool norender)
{
int nXpos,nYpos;
int nOffset;
DWORD nFrameWanted;
bool ret;
if(hBitmap)
{
DeleteObject(hBitmap);
hBitmap=NULL;
}
if(!m_pFrameData) return false;
if(!maxclipx || maxclipx*mosheader.dwBlockSize>(DWORD) mosheader.wWidth+63)
{
maxclipx=(mosheader.wWidth+63)/mosheader.dwBlockSize;
}
if(!maxclipy || maxclipy*mosheader.dwBlockSize>(DWORD) mosheader.wHeight+63)
{
maxclipy=(mosheader.wHeight+63)/mosheader.dwBlockSize;
}
m_pixelwidth=(maxclipx-clipx)*mosheader.dwBlockSize;
m_pixelheight=(maxclipy-clipy)*mosheader.dwBlockSize;
if(m_pixelwidth<0) m_pixelwidth=0;
if(m_pixelheight<0) m_pixelheight=0;
nXpos=m_pixelwidth*m_pixelheight;
//check for overflow
if(nXpos<=0) return false;
if(m_DIBsize!=nXpos)
{
if(m_pclrDIBits) delete [] m_pclrDIBits;
m_pclrDIBits=new COLORREF[nXpos];
if(!m_pclrDIBits) return false;
m_DIBsize=nXpos;
}
//fill area with base transparency
while(nXpos--)
{
m_pclrDIBits[nXpos]=clrTrans;
}
for(nYpos=0;(nYpos+clipy<maxclipy) /*&& (nYpos<VIEW_MAXEXTENT)*/;nYpos++)
{
nOffset=mosheader.dwBlockSize*m_pixelwidth*nYpos;
for(nXpos=0;(nXpos+clipx<maxclipx) /*&& (nXpos<VIEW_MAXEXTENT)*/;nXpos++)
{
m_overlaytile=-1;
nFrameWanted=ResolveFrameNumber((nYpos+clipy)*mosheader.wColumn+nXpos+clipx);
if(!m_friend) m_overlaytile=-1;
if(m_overlaytile!=-1)
{
m_friend->m_pFrameData[m_overlaytile].ExpandMosBitsWhole(TRANSPARENT_GREEN,clrTrans,m_pclrDIBits,nOffset, m_pixelwidth);
}
if(nFrameWanted<tisheader.numtiles)
{
if (!m_pFrameData[nFrameWanted].ExpandMosBitsWhole(TRANSPARENT_GREEN,m_overlaytile!=-1?TRANSPARENT_GREEN:clrTrans,m_pclrDIBits,nOffset, m_pixelwidth))
{
delete [] m_pclrDIBits;
m_pclrDIBits=NULL;
m_DIBsize=0;
return false;
}
}
nOffset+=mosheader.dwBlockSize;
}
}
if(norender) return false;
ret=MakeBitmapExternal(m_pclrDIBits,m_pixelwidth,m_pixelheight,hBitmap);
return ret;
}
int Cmos::MakeTransparent(DWORD nFrameWanted, DWORD redgreenblue, int limit)
{
int i;
int nHits;
if(tisheader.numtiles <= nFrameWanted ) return -1;
nHits=0;
// don't force transparent index to be 0
for(i=0;i<256;i++)
{
if(ChiSquare((BYTE *) &redgreenblue,(BYTE *) (m_pFrameData[nFrameWanted].Palette+i))<=limit )
{
m_pFrameData[nFrameWanted].Palette[i]=TRANSPARENT_GREEN;
nHits++;
}
}
return nHits;
}
int Cmos::WriteTisToFile(int fhandle, int clipx, int clipy, int maxclipx, int maxclipy)
{
int esize;
DWORD i;
DWORD x,y;
BYTE *zerobuffer;
int ret;
DWORD tmpsave;
DWORD nFrameWanted;
esize=tisheader.pixelsize*tisheader.pixelsize;
zerobuffer=new BYTE[esize];
if(!zerobuffer) return -3;
memset(zerobuffer,0,esize);
ret=0;
//this hack makes sure the header contains as many tiles as we wanted
memcpy(&tisheader.filetype,"TIS V1 ",8);
tmpsave=tisheader.numtiles;
if(maxclipx!=-1)
{
tisheader.numtiles=(maxclipy-clipy+1)*(maxclipx-clipx+1);
}
if(write(fhandle, &tisheader, sizeof(tis_header) )!=sizeof(tis_header) )
{
ret=-2;
goto endofquest;
}
//end of hack
//this hack makes sure we write out the tiles we wanted
//maximum contains the last tile
for(i=0;i<tisheader.numtiles;i++)
{
if(maxclipx!=-1)
{
y=i/(maxclipx-clipx+1);
x=i%(maxclipx-clipx+1);
nFrameWanted=ResolveFrameNumber( (y+clipy)*mosheader.wColumn+x+clipx);
}
else nFrameWanted=i;
esize=sizeof(palettetype); //sizeof palettetype
if(write(fhandle,m_pFrameData[nFrameWanted].Palette,esize )!=esize)
{
ret=-2;
goto endofquest;
}
esize=tisheader.pixelsize*tisheader.pixelsize;
if(esize!=m_pFrameData[nFrameWanted].nFrameSize)
{
//outputting incomplete tiles
ret=1;
for(y=0;y<m_pFrameData[nFrameWanted].nHeight;y++)
{
esize-=write(fhandle,m_pFrameData[nFrameWanted].pFrameData+y*m_pFrameData[nFrameWanted].nWidth,m_pFrameData[nFrameWanted].nWidth);
esize-=write(fhandle,zerobuffer,tisheader.pixelsize-m_pFrameData[nFrameWanted].nWidth);
}
esize-=write(fhandle,zerobuffer,(tisheader.pixelsize-y)*tisheader.pixelsize);
if(esize)
{
ret=-2;
goto endofquest;
}
}
else
{
//outputting complete tile
if(write(fhandle,m_pFrameData[nFrameWanted].pFrameData,esize)!=esize)
{
ret=-2;
goto endofquest;
}
}
}
endofquest:
tisheader.numtiles=tmpsave;
delete [] zerobuffer;
return ret;
}
int Cmos::GetImageWidth(int clipx, int maxclipx)
{
if(!m_pFrameData) return -1;
if(!maxclipx && !clipx)
{
return mosheader.wWidth;
}
return (maxclipx-clipx)*mosheader.dwBlockSize+m_pFrameData[maxclipx].nWidth;
}
int Cmos::GetImageHeight(int clipy, int maxclipy)
{
if(!m_pFrameData) return -1;
if(!maxclipy && !clipy)
{
return mosheader.wHeight;
}
return (maxclipy-clipy)*mosheader.dwBlockSize+m_pFrameData[mosheader.wColumn*maxclipy].nHeight;
}
int Cmos::CollectFrameData(int fhandle, DWORD *prFrameBuffer, LPBYTE pcBuffer, DWORD height,
DWORD scanline, DWORD nFrameCol, DWORD cols, DWORD rows)
{
DWORD x,y;
for(y=0;y<tisheader.pixelsize;y++)
{
if(height<y) break;
lseek(fhandle,nFrameCol+(height-y)*scanline,SEEK_SET);
if(read(fhandle,pcBuffer,cols*rows)!=(int) (cols*rows))
{
return -2;
}
for(x=0;x<cols;x++)
{
//pcBuffer consists of bytes, so we multiply x with 3 or 4
memcpy(prFrameBuffer+y*cols+x,pcBuffer+x*rows,rows);
}
}
return 0;
}
int Cmos::GetFrameData(DWORD *prFrameBuffer, LPBYTE pRawData, DWORD height,
DWORD scanline, DWORD nFrameCol,
DWORD cols, DWORD rows)
{
DWORD x,y;
for(y=0;y<tisheader.pixelsize;y++)
{
if(height<y) break;
for(x=0;x<cols;x++)
{
//pcBuffer consists of bytes, so we multiply x with 3 or 4
memcpy(prFrameBuffer+y*cols+x,pRawData+nFrameCol+(height-y)*scanline+x*rows,rows);
}
}
return 0;
}
int Cmos::ReducePalette(int fhandle, bmp_header &sHeader, LPBYTE pcBuffer,
DWORD scanline, DWORD nSourceOff)
{
DWORD *prFrameBuffer=NULL; //buffer for the whole frame (raw form)
LPBYTE pRawData=NULL;
int ret;
DWORD rows,cols;
DWORD nFrameRow; //the pixel row (scanline) of this frame
DWORD nFrameCol; //
DWORD nFrameWanted;
//the size of the pixel (3 or 4)
rows=sHeader.bits>>3;
//allocate a temporary buffer for the raw frame data
prFrameBuffer=new COLORREF [tisheader.pixelsize*tisheader.pixelsize];
if(!prFrameBuffer)
{
ret=-3;
goto endofquest;
}
//we try to load the whole picture into memory,
//if we don't succeed we'll load it in pieces
pRawData=new BYTE [sHeader.height*scanline];
if(pRawData)
{
if(read(fhandle,pRawData,sHeader.height*scanline)!=(int) (sHeader.height*scanline) )
{
ret=-2;
goto endofquest;
}
}
ret=0;
((CChitemDlg *) AfxGetMainWnd())->start_progress(tisheader.numtiles, "Reducing palette...");
memset(prFrameBuffer,0,tisheader.pixelsize*tisheader.pixelsize*sizeof(DWORD) );
//generate palette for frames
//if a color collides, make intelligent color reduction
//if it is only 3 bytes expand palette to 4 bytes
for(nFrameWanted=0;nFrameWanted<tisheader.numtiles;nFrameWanted++)
{
((CChitemDlg *) AfxGetMainWnd())->set_progress(nFrameWanted);
//collect a frame's data in prFrameBuffer
//cols is the pixel number of the frame
cols=m_pFrameData[nFrameWanted].nWidth;
nFrameRow=tisheader.pixelsize*(nFrameWanted/mosheader.wColumn);
nFrameCol=nFrameWanted%mosheader.wColumn*tisheader.pixelsize*rows;
//cols is the width of the frame, rows is the size of a pixel
if(pRawData)
{
GetFrameData(prFrameBuffer, pRawData, sHeader.height-nFrameRow-1,
scanline, nFrameCol, cols, rows);
}
else
{
ret=CollectFrameData(fhandle, prFrameBuffer, pcBuffer, sHeader.height-nFrameRow-1,
scanline, nSourceOff+nFrameCol, cols, rows);
}
if(ret) goto endofquest;
//generates a palette and palette indices from raw binary data
m_nQualityLoss+=m_pFrameData[nFrameWanted].TakeWholeBmpData(prFrameBuffer);
}
m_nQualityLoss=m_nQualityLoss/nFrameWanted;
endofquest:
if(prFrameBuffer) delete [] prFrameBuffer;
if(pRawData) delete [] pRawData;
((CChitemDlg *) AfxGetMainWnd())->end_progress();
return ret;
}
//reads and converts a bitmap to .tis or .mos format
//BMP header: BITMAPFILEHEADER + BITMAPINFOHEADER
int Cmos::ReadBmpFromFile(int fhandle, int ml)
{
bmp_header sHeader; //BMP header
LPBYTE pcBuffer; //buffer for a scanline
DWORD scanline; //size of a scanline in bytes (ncols * bytesize of a pixel)
palettetype sPalette; //256 * bytesize of color
DWORD palettesize; //size of the palette in file (ncolor * bytesize of color)
long original;
DWORD cols, rows;
DWORD x,y;
DWORD nFrameWanted;//the number of the tile of the mos/tis
DWORD nFrameRow; //the pixel row (scanline) of this frame
DWORD nSourceOff; //the source offset in the original scanline (which we cut up)
int ret;
new_mos();
m_nQualityLoss=0;
pcBuffer=NULL;
memset(&sPalette,0,sizeof(sPalette) );
original=tell(fhandle);
if(ml<0) ml=filelength(fhandle);
if(ml<0) return -2;
if(read(fhandle,&sHeader,sizeof(bmp_header))!=sizeof(bmp_header) )
{
return -2;
}
if((*(unsigned short *) sHeader.signature)!='MB') return -2; //BM
if(sHeader.fullsize>(DWORD) ml) return -2;
if(sHeader.headersize!=0x28) return -2;
if(sHeader.planes!=1) return -1; //unsupported
if(sHeader.compression!=BI_RGB) return -1; //unsupported
if(sHeader.height<0 || sHeader.width<0) return -1; //unsupported
if(sHeader.bits==4) return -1; //unsupported for mos, tis
//read palette if we got one
if(sHeader.bits==8)
{
if(!sHeader.colors)
{
sHeader.colors=256;
}
palettesize=sHeader.colors*sizeof(RGBQUAD);
if(read(fhandle,&sPalette, palettesize)!=(int) palettesize)
{
return -2;
}
}
else
{
palettesize=0;
}
//check if the file fits in the claimed length
if(sHeader.offset<palettesize+sHeader.headersize+((unsigned char *) &sHeader.headersize-(unsigned char *) &sHeader) ) return -2;
scanline=sHeader.width*(sHeader.bits>>3);
if(scanline&3) scanline+=4-(scanline&3);
if(scanline*sHeader.height+sHeader.offset>sHeader.fullsize) return -2;
//check if we are on the position we wanted to be
nSourceOff=tell(fhandle);
if(sHeader.offset!=nSourceOff-original ) return -2;
//create the frames, set up the internal headers
//a bit hackish
SetTisParameters(0);
cols=(sHeader.width+tisheader.pixelsize-1)/tisheader.pixelsize;
rows=(sHeader.height+tisheader.pixelsize-1)/tisheader.pixelsize;
SetTisParameters(rows*cols*(tisheader.pixelsize*tisheader.pixelsize+sizeof(palettetype)));
TisToMos(cols,rows);
mosheader.wWidth=(WORD) (sHeader.width);
mosheader.wHeight=(WORD) (sHeader.height);
m_nResX=mosheader.wWidth%mosheader.dwBlockSize;
m_nResY=mosheader.wHeight%mosheader.dwBlockSize;
if(!tisheader.numtiles) return -2;
//quest started, go to endofquest if in trouble
m_pclrDIBits=new COLORREF[tisheader.pixelsize*tisheader.pixelsize];
if(!m_pclrDIBits)
{
ret=-3;
goto endofquest;
}
m_pFrameData=new INF_MOS_FRAMEDATA[tisheader.numtiles];
if(!m_pFrameData)
{
ret=-3;
goto endofquest;
}
pcBuffer=new BYTE[scanline];
if(!pcBuffer)
{
ret=-3;
goto endofquest;
}
//lets allocate memory for all frames
for(y=0;y<tisheader.numtiles;y++)
{
//if it has a palette then we take it now
if(sHeader.bits==8) m_pFrameData[y].TakePaletteData((LPBYTE) &sPalette);
//calculate the size of this frame
if(!((y+1)%mosheader.wColumn))
{
cols=m_nResX;
if(!cols) cols=tisheader.pixelsize;
}
else cols=tisheader.pixelsize;
if(y>=(DWORD) (mosheader.wRow-1)*mosheader.wColumn)
{
rows=m_nResY;
if(!rows) rows=tisheader.pixelsize;
}
else rows=tisheader.pixelsize;
ret=m_pFrameData[y].TakeMosData(0,cols,rows, false); //setting the size of the frame now
if(ret) goto endofquest; //TakeMosData allocates memory, if it failed we escape
}
//now we just have to fill the palettes and bitmap frames
ret=-1; //unsupported mode
switch(sHeader.bits)
{
case 8: //this format has a palette
//copy palettes to all frame palettes
//throw bitmaps into separate frames, y starts from bottom up
for(y=0;y<mosheader.wHeight;y++)
{
if(read(fhandle, pcBuffer, scanline)!=(int ) scanline)
{
ret=-2;
goto endofquest;
}
//which frame gets this stuff?
nFrameWanted=(sHeader.height-y-1)/tisheader.pixelsize*mosheader.wColumn;
nFrameRow=(sHeader.height-y-1)%tisheader.pixelsize;
nSourceOff=0;
for(x=0;x<mosheader.wColumn;x++)
{
nSourceOff+=m_pFrameData[nFrameWanted++].TakeBmpData(pcBuffer, nFrameRow, nSourceOff);
}
}
ret=0;
break;
case 24: case 32: //loading the file in small pieces, only one frame at a time
ret=ReducePalette(fhandle, sHeader, pcBuffer, scanline, nSourceOff);
break;
}
endofquest:
if(pcBuffer) delete [] pcBuffer;
if(ret) new_mos(); //destroy crippled structures
return ret; //return value
}
int Cmos::WriteTisToBmpFile(int fhandle, int clipx, int clipy, int maxclipx, int maxclipy)
{
char *pcBuffer;
int maxx, maxy;
int x,y;
int nFrameWanted;
int nSourceOff;
int nScanLine;
int nFullLine;
int ret;
ret=0;
if(maxclipx==-1)
{
maxclipx=mosheader.wColumn-1;
}
if(maxclipy==-1)
{
maxclipy=mosheader.wRow-1;
}
maxx=GetImageWidth(clipx, maxclipx);
maxy=GetImageHeight(clipy, maxclipy);
nScanLine=GetScanLineLength(maxx, 24);
pcBuffer=new char[nScanLine];
if(!pcBuffer) return -3;
memset(pcBuffer,0,nScanLine);
if(!CreateBmpHeader(fhandle, maxx, maxy, 3))
{
ret=-2;
goto endofquest;
}
for(y=maxy-1;y>=0;y--) //backwards???
{
nFullLine=0;
for(x=clipx;x<=maxclipx;x++)
{
nFrameWanted=ResolveFrameNumber((y/mosheader.dwBlockSize+clipy)*mosheader.wColumn+x);
nSourceOff=m_pFrameData[nFrameWanted].nWidth*(y%mosheader.dwBlockSize);
nFullLine+=m_pFrameData[nFrameWanted++].ExpandMosLine(pcBuffer+nFullLine,nSourceOff);
}
if(write(fhandle,pcBuffer,nScanLine)!=nScanLine)
{
ret=-2;
goto endofquest;
}
}
endofquest:
delete [] pcBuffer;
return ret;
}
int Cmos::WriteMosToFile(int fhandle)
{
BYTE *pCData;
int ret;
unsigned long tmpsize;
if(ImplodeMosData()) return -4; //internal error
if(m_bCompressed)
{
tmpsize=c_header.nExpandSize; //allocate space
pCData=new BYTE[tmpsize];
if(!pCData) return -3; // out of memory
ret=compress(pCData,&tmpsize,m_pData, c_header.nExpandSize); //nDatasize is the uncompressed length
if(!ret) //we couldn't compress it
{
if(write(fhandle,&c_header, sizeof(c_header))!=sizeof(c_header) )
{
delete [] pCData;
KillData();
return -2;
}
if(write(fhandle, pCData, tmpsize)!=(int) tmpsize)
{
delete [] pCData;
KillData();
return -2;
}
delete [] pCData;
KillData();
return 0;
}
delete [] pCData;
//we cannot compress it, ok, lets save it as uncompressed
m_bCompressed=false;
lseek(fhandle,0,SEEK_SET);
}
if(write(fhandle, m_pData, m_nDataSize)!=(int) m_nDataSize)
{
KillData();
return -2;
}
KillData();
m_changed=false;
return 0;
}
void Cmos::SetTisParameters(int size)
{
tisheader.offset=sizeof(tisheader);
tisheader.pixelsize=64;
//palette + tiledata
tisheader.tilelength=sizeof(palettetype)+tisheader.pixelsize*tisheader.pixelsize;
tisheader.numtiles=size/tisheader.tilelength;
TisToMos(1,tisheader.numtiles);
}
int Cmos::TisToMos(DWORD x, DWORD y)
{
if(x*y>tisheader.numtiles) return -1;
//mos compatibility
mosheader.wColumn=(WORD) x;
mosheader.wRow=(WORD) y;
mosheader.wWidth=(WORD) (tisheader.pixelsize*x);
mosheader.wHeight=(WORD) (tisheader.pixelsize*y);
mosheader.dwBlockSize=tisheader.pixelsize;
mosheader.dwPaletteOffset=sizeof(mosheader);
return 0;
}
int Cmos::Allocate(DWORD x, DWORD y)
{
DWORD i;
int nCol,nRow;
int nWidth,nHeight;
int ret;
tisheader.pixelsize=64;
mosheader.dwPaletteOffset=sizeof(mosheader);
mosheader.dwBlockSize=tisheader.pixelsize;
mosheader.wWidth=(unsigned short) x;
mosheader.wHeight=(unsigned short) y;
m_nResX=mosheader.wWidth%mosheader.dwBlockSize;
m_nResY=mosheader.wHeight%mosheader.dwBlockSize;
mosheader.wColumn=(unsigned short) ((x+tisheader.pixelsize-1)/tisheader.pixelsize);
mosheader.wRow=(unsigned short) ((y+tisheader.pixelsize-1)/tisheader.pixelsize);
tisheader.numtiles=mosheader.wColumn*mosheader.wRow;
m_nQualityLoss=0;
//creating the frames
m_pFrameData = new INF_MOS_FRAMEDATA[tisheader.numtiles];
if(!m_pFrameData)
{
tisheader.numtiles=0;
return -3;
}
for(i=0;i<tisheader.numtiles;i++)
{
nWidth=nHeight=tisheader.pixelsize;
nRow=i/mosheader.wColumn;
nCol=i%mosheader.wColumn;
if((nCol==mosheader.wColumn-1) && m_nResX) nWidth=m_nResX;
if((nRow==mosheader.wRow-1) && m_nResY) nHeight=m_nResY;
ret=m_pFrameData[i].TakeMosData(0,nWidth,nHeight,false);
if(ret) return ret;
}
return 0;
}
int Cmos::ReadTisFromFile(int fhandle, int ml, int header)
{
int esize;
DWORD i;
new_mos();
if(ml<0) ml=filelength(fhandle);
if(header)
{
read(fhandle,&tisheader,sizeof(tis_header) );
// ml-=sizeof(tis_header);
mosheader.wRow=(WORD) tisheader.numtiles;
mosheader.wColumn=1;
mosheader.wHeight=(WORD) (tisheader.pixelsize*tisheader.numtiles);
mosheader.wWidth=(WORD) tisheader.pixelsize;
mosheader.dwBlockSize=tisheader.pixelsize;
}
else SetTisParameters(ml);
m_pclrDIBits=new COLORREF[tisheader.pixelsize*tisheader.pixelsize];
if(!m_pclrDIBits) return -3;
m_pFrameData=new INF_MOS_FRAMEDATA[tisheader.numtiles];
if(!m_pFrameData) return -3;
for(i=0;i<tisheader.numtiles;i++)
{
if(read(fhandle, m_pFrameData[i].Palette,sizeof(palettetype) )!=sizeof(palettetype) )
{
return -2;
}
esize=tisheader.pixelsize*tisheader.pixelsize;
m_pFrameData[i].pFrameData=new BYTE[esize];
if(!m_pFrameData[i].pFrameData) return -3; //out of memory
m_pFrameData[i].nFrameSize=esize;
m_pFrameData[i].nHeight=m_pFrameData[i].nWidth=tisheader.pixelsize;
if(read(fhandle, m_pFrameData[i].pFrameData, esize)!=esize)
{
return -2;
}
}
return 0;
}
int Cmos::ReadMosFromFile(int fhandle, int ml)
{
BYTE *pCData;
DWORD dwSize;
int ret;
if(ml<0) ml=filelength(fhandle);
if((ml<sizeof(INF_MOS_HEADER)) || (ml>10000000) ) return -2; //file is bad
if(read(fhandle,&c_header,sizeof(INF_MOSC_HEADER) )!=sizeof(INF_MOSC_HEADER) )
{
return -2;
}
m_bCompressed=true;
//not a compressed header, we read the raw data and compress the bam if needed (on save, if compressed=true)
if(c_header.chSignature[3]!='C')
{
// If this is an uncompressed bam we can just attach the buffer to the
// bam file object and be done with it.
m_bCompressed=false;
m_nDataSize=ml;
m_pData = new BYTE[m_nDataSize];
if (!m_pData)
return -3;
dwSize=m_nDataSize-sizeof(c_header);
memcpy(m_pData,&c_header, sizeof(c_header) ); //move the pre-read part to the header
if(read(fhandle,m_pData+sizeof(c_header),dwSize)!=(int) dwSize)
{
KillData();
return -2;
}
memcpy(&c_header,"MOSCV1 ",8);
c_header.nExpandSize=m_nDataSize;
goto endofquest;
}
ml-=sizeof(INF_MOSC_HEADER);
pCData = new BYTE[ml];
if (!pCData)
{
return -3;
}
if(read(fhandle, pCData, ml) !=ml)
{
delete [] pCData;
return -2;//cannot read compressed data
}
if(c_header.nExpandSize>10000000) //don't handle bams larger than 10M
{
delete [] pCData;
return -2; //illegal data
}
m_nDataSize=c_header.nExpandSize;
m_pData = new BYTE[m_nDataSize];
if (!m_pData)
return -3;
dwSize=c_header.nExpandSize;
if (uncompress(m_pData,&c_header.nExpandSize,pCData,ml) != Z_OK)
{
KillData();
delete [] pCData;
return -2; //uncompress failed
}
delete [] pCData;
if(dwSize!=c_header.nExpandSize)
{
KillData();
return -2; //pre-saved uncompressed length isn't the same, why ?
}
endofquest:
ret=ExplodeMosData();
KillData();
return ret;
}
int Cmos::ImplodeMosData()
{
int nCount;
int nFrameSize; //total frame data size (must sum up to wheight*wwidth)
int esize;
int i, j;
int nHeight, nWidth;
if(!m_pOffsets)
{
m_pOffsets=new DWORD[tisheader.numtiles];
}
if(!m_pOffsets) return -3;
//calculating the full size of the uncompressed mos
m_nDataSize=sizeof(INF_MOS_HEADER);
m_nDataSize+=mosheader.wRow*mosheader.wColumn*sizeof(palettetype); //adding palettes
m_nDataSize+=mosheader.wRow*mosheader.wColumn*sizeof(DWORD); //adding frameoffsets
nCount=0;
nFrameSize=0;
nHeight=mosheader.dwBlockSize;
for(i=0;i<mosheader.wRow;i++)
{
if((i==mosheader.wRow-1) && m_nResY) nHeight=m_nResY;
nWidth=mosheader.dwBlockSize;
for(j=0;j<mosheader.wColumn;j++)
{
if((j==mosheader.wColumn-1) && m_nResX) nWidth=m_nResX;
esize=nWidth*nHeight;
m_pOffsets[nCount]=nFrameSize;
nFrameSize+=esize;
nCount++;
}
}
if(nFrameSize!=mosheader.wHeight*mosheader.wWidth) return -2;
m_nDataSize+=nFrameSize; //adding pixels
//allocating bytes for m_nDataSize
m_pData = new BYTE[m_nDataSize];
if(!m_pData) return -3;
memcpy(m_pData,&mosheader,sizeof(INF_MOS_HEADER) );
fullsize=sizeof(INF_MOS_HEADER);
for(i=0;i<nCount;i++)
{
memcpy(m_pData+fullsize,&m_pFrameData[i].Palette,sizeof(palettetype) );
fullsize+=sizeof(palettetype);
}
esize=nCount*sizeof(DWORD);
memcpy(m_pData+fullsize,m_pOffsets,esize);
fullsize+=esize;
for(i=0;i<nCount;i++)
{
esize=m_pFrameData[i].nFrameSize;
memcpy(m_pData+fullsize,m_pFrameData[i].pFrameData,esize);
fullsize+=esize;
}
//now nActPos should equal m_nDataSize
//if not, then our code is wrong
if(fullsize!=m_nDataSize)
{
KillData();
return -4;
}
c_header.nExpandSize=m_nDataSize;
return 0;
}
int Cmos::ExplodeMosData()
{
int nCount;
int ret, gret;
int esize;
int i,j;
int nHeight, nWidth;
DWORD nFrameSize;
bool ietmebug=false;
KillFrameData();
KillOffsets();
gret=0;
if(memcmp(m_pData,"MOS V1 ",8))
{
return -4;
}
memcpy(&mosheader,m_pData,sizeof(INF_MOS_HEADER) ); //get header
fullsize=sizeof(INF_MOS_HEADER);
if(mosheader.dwPaletteOffset!=fullsize) return -2;
if(fullsize>m_nDataSize) return -2;
if(mosheader.dwBlockSize<8 || mosheader.dwBlockSize>256) return -2;
if(m_pclrDIBits) delete [] m_pclrDIBits;
m_DIBsize=mosheader.dwBlockSize*mosheader.dwBlockSize;
m_pclrDIBits=new COLORREF[m_DIBsize];
if(!m_pclrDIBits)
{
m_DIBsize=0;
return -3;
}
m_nResX=mosheader.wWidth%mosheader.dwBlockSize;
m_nResY=mosheader.wHeight%mosheader.dwBlockSize;
esize=(mosheader.wWidth+mosheader.dwBlockSize-1)/mosheader.dwBlockSize;
if(esize!=mosheader.wColumn)
{
if(!m_nResX && (esize==mosheader.wColumn-1) )
{
//ietme saves an empty column if the width is divideable by blocksize
//we do this hack to fix the problem on the fly
ietmebug=true;
}
else return -2;
}
if((mosheader.wHeight+mosheader.dwBlockSize-1)/mosheader.dwBlockSize!=mosheader.wRow)
{
return -2;
}
if(ietmebug) tisheader.numtiles=(mosheader.wColumn-1)*mosheader.wRow;
else tisheader.numtiles=mosheader.wColumn*mosheader.wRow;
m_pFrameData=new INF_MOS_FRAMEDATA[tisheader.numtiles];
if(!m_pFrameData) return -3;
nCount=0;
for(i=0;i<mosheader.wRow;i++)
{
for(j=0;j<mosheader.wColumn;j++)
{
//skipping palette for the empty frame
if( (j!=mosheader.wColumn-1) || !ietmebug)
{
if(fullsize+sizeof(palettetype)>m_nDataSize) return -2;
m_pFrameData[nCount++].TakePaletteData(m_pData+fullsize);
}
fullsize+=sizeof(palettetype);
}
}
m_pOffsets=new DWORD[tisheader.numtiles];
if(!m_pOffsets) return -3;
nCount=0;
for(i=0;i<mosheader.wRow;i++)
{
for(j=0;j<mosheader.wColumn;j++)
{
//skipping palette for the empty frame
if( (j!=mosheader.wColumn-1) || !ietmebug)
{
if(fullsize+sizeof(DWORD)>m_nDataSize) return -2;
m_pOffsets[nCount++]=* (DWORD *) (m_pData+fullsize);
}
fullsize+=sizeof(DWORD);
}
}
esize=mosheader.wWidth*mosheader.wHeight;
if(fullsize+esize>m_nDataSize) return -2;
nCount=0;
nFrameSize=0;
nHeight=mosheader.dwBlockSize;
for(i=0;i<mosheader.wRow;i++)
{
if((i==mosheader.wRow-1) && m_nResY) nHeight=m_nResY;
nWidth=mosheader.dwBlockSize;
for(j=0;j<mosheader.wColumn;j++)
{
if((j==mosheader.wColumn-1) )
{
if(m_nResX || ietmebug) nWidth=m_nResX;
}
esize=nWidth*nHeight;
if(nWidth) //ietmebug
{
if(nFrameSize!=m_pOffsets[nCount]) return -2;
ret=m_pFrameData[nCount].TakeMosData(m_pData+fullsize+nFrameSize, nWidth, nHeight,false);
if(ret)
{
new_mos(); //delete the crippled data
return ret;
}
nFrameSize+=esize;
nCount++;
}
}
}
if(ietmebug) mosheader.wColumn--;
if(fullsize+nFrameSize!=m_nDataSize) return -1;
if(ietmebug)
{
return 1; //ietme mos bug successfully fixed
}
return gret;
}
void Cmos::DropUnusedPalette()
{
DWORD nFrame;
//do the pixel change
for(nFrame=0;nFrame<tisheader.numtiles;nFrame++)
{
m_pFrameData[nFrame].MarkPixels();
m_pFrameData[nFrame].OrderPalette();
}
}
DWORD Cmos::GetColor(DWORD x, DWORD y)
{
DWORD nFrameWanted;
if(x>=mosheader.wWidth || y>=mosheader.wHeight) return 0;
nFrameWanted=(x/mosheader.dwBlockSize)+(y/mosheader.dwBlockSize)*mosheader.wColumn;
return m_pFrameData[nFrameWanted].GetColor(x%mosheader.dwBlockSize,y%mosheader.dwBlockSize);
}
int Cmos::FlipTile(DWORD nFrameWanted)
{
if(nFrameWanted>=tisheader.numtiles) return -1;
m_pFrameData[nFrameWanted].FlipTile();
return 0;
}
//this doesn't adjust alternate tiles bigger than the removed one
int Cmos::RemoveTile(DWORD tile)
{
INF_MOS_FRAMEDATA *newframedata;
DWORD i;
if(tile>=tisheader.numtiles) return -1;
newframedata=new INF_MOS_FRAMEDATA[--tisheader.numtiles];
if(!newframedata)
{
tisheader.numtiles++;
return -3;
}
memcpy(newframedata,m_pFrameData,tile*sizeof(INF_MOS_FRAMEDATA));
memcpy(newframedata+tile,m_pFrameData+tile+1,(tisheader.numtiles-tile)*sizeof(INF_MOS_FRAMEDATA));
delete m_pFrameData[tile].pFrameData; //delete the removed frame
//detach pixel pointers so they don't get freed!
for(i=0;i<=tisheader.numtiles;i++)
{
m_pFrameData[i].pFrameData=NULL;
}
delete [] m_pFrameData;
m_pFrameData=newframedata;
return 0;
}
//this function replaces an existing tile with new palette/bitmap data
//the tile comes from another tileset
int Cmos::SetFrameData(DWORD nFrameWanted, INF_MOS_FRAMEDATA *oldframe)
{
if(nFrameWanted>=tisheader.numtiles) return -1;
if(oldframe==NULL) return -1;
m_pFrameData[nFrameWanted].TakePaletteData((LPBYTE) oldframe->Palette);
m_pFrameData[nFrameWanted].TakeMosData(oldframe->pFrameData,oldframe->nWidth,oldframe->nHeight,false);
m_changed=true;
return 0;
}
int Cmos::AddTileCopy(DWORD original, unsigned char *optionalpixels, int flags)
{
INF_MOS_FRAMEDATA *newframedata;
DWORD i;
int ret;
if(tisheader.numtiles>=32767) return -1;
if(original>=tisheader.numtiles) return -1;
ret=tisheader.numtiles;
newframedata=new INF_MOS_FRAMEDATA[tisheader.numtiles+1];
if(!newframedata)
{
return -3;
}
memcpy(newframedata,m_pFrameData,tisheader.numtiles*sizeof(INF_MOS_FRAMEDATA));
newframedata[tisheader.numtiles].TakePaletteData((unsigned char *) newframedata[original].Palette);
if(optionalpixels || (flags&2) )
{
newframedata[tisheader.numtiles].TakeMosData(optionalpixels,
newframedata[original].nWidth,newframedata[original].nHeight, flags&1);
}
else
{
newframedata[tisheader.numtiles].TakeMosData(newframedata[original].pFrameData,
newframedata[original].nWidth,newframedata[original].nHeight,false);
}
//create transparent green color on 0. palette entry
if(flags&2)
{
newframedata[tisheader.numtiles].Palette[255]=newframedata[tisheader.numtiles].Palette[0];
newframedata[tisheader.numtiles].Palette[0]=TRANSPARENT_GREEN;
}
else newframedata[tisheader.numtiles].AllocateTransparency();
//detach pixel pointers so they don't get freed!
for(i=0;i<tisheader.numtiles;i++)
{
m_pFrameData[i].pFrameData=NULL;
}
tisheader.numtiles++;
delete [] m_pFrameData;
m_pFrameData=newframedata;
m_changed=true;
return ret;
}
int Cmos::SaturateTransparency(DWORD tile, bool createcopy, bool deepen)
{
int ret;
unsigned char *backup;
if(tile>=tisheader.numtiles) return -1;
if(createcopy)
{
backup = new unsigned char[m_pFrameData[tile].nFrameSize];
if(!backup) return -3;
memcpy(backup, m_pFrameData[tile].pFrameData,m_pFrameData[tile].nFrameSize);
}
else backup=NULL;
ret=m_pFrameData[tile].SaturateTransparency();
if(ret>0)
{
m_changed=true;
ret=AddTileCopy(tile, backup, deepen);
}
else ret=-1;
if(backup) delete [] backup;
return ret;
}