-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFormImportImageAsSHP.pas
1615 lines (1479 loc) · 54 KB
/
FormImportImageAsSHP.pas
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
unit FormImportImageAsSHP;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls, ExtDlgs, FileCtrl,shp_file,shp_engine,palette,
Shp_Image_Save_Load, ComCtrls, math, Spin,Undo_Redo, SHP_Engine_CCMs,
SHP_Engine_Resize, SHP_Shadows, SHP_Colour_Bank, Colour_list, SHP_DataMatrix,
FormCanvasResize,SHP_Canvas, SHP_Frame, OSExtDlgs, FormInstall, XPMan;
Const
cINFANTRY = 0;
cBUILDING = 1;
cISOBUILDING = 2;
cBUILDANIM = 3;
cANIMATION = 4;
cCAMEO = 5;
cHYBRID = 6;
c1TEMPERATE = 0;
c1SNOW = 1;
c1URBAN = 2;
c1DESERT = 3;
c1LUNAR = 4;
c1NEWURBAN = 5;
// shp Game IndeX
GIX_TD = 0;
GIX_RA1 = 1;
GIX_TS = 2;
GIX_RA2 = 3;
GIX_NONE = 4;
// shp Type IndeX
TIX_AUTO = 0;
TIX_UNIT = 1; // Infantry also falls here
TIX_BUILDING = 2;
TIX_BUILDANIM = 3;
TIX_ANIMATION = 4;
TIX_CAMEO = 5;
TIX_TEM = 6;
TIX_SNO = 7; // TD Winter falls here
TIX_URB = 8; // RA1 Interior falls here
TIX_DES = 9;
TIX_LUN = 10;
TIX_UBN = 11;
type
TDataList = array of pointer;
TFrmImportImageAsSHP = class(TForm)
BtOK: TButton;
BtCancel: TButton;
Bevel1: TBevel;
OpenPictureDialog: TOpenPictureDialog;
FileListBox: TFileListBox;
ProgressBar: TProgressBar;
OpenPictureDialog1: TOpenPictureDialog;
ColorDialog1: TColorDialog;
PageControl1: TPageControl;
TabSheet1: TTabSheet;
TabSheet2: TTabSheet;
TabSheet3: TTabSheet;
Label1: TLabel;
BtBrowse: TButton;
Image_Location: TEdit;
cbMode: TCheckBox;
Label8: TLabel;
ColourConversionBox: TPanel;
ccmInfurion: TRadioButton;
ccmBanshee3: TRadioButton;
ccmBanshee2: TRadioButton;
Label9: TLabel;
BackgroundOverrideBox: TPanel;
bcNone: TRadioButton;
bcAutoSelect: TRadioButton;
bcCustom: TRadioButton;
bcDefault: TRadioButton;
bcColourEdit: TPanel;
TabSheet4: TTabSheet;
SplitShadowBox: TPanel;
ssRedToRemap: TCheckBox;
ssIgnoreLastColours: TCheckBox;
ssShadow: TCheckBox;
Label7: TLabel;
ConversionOptimizeBox: TPanel;
ocfStyle: TComboBox;
ocfComboOptions: TComboBox;
Label10: TLabel;
ConversionRangeBox: TPanel;
Label3: TLabel;
crFrom: TSpinEdit;
crTo: TSpinEdit;
crCustomFrames: TRadioButton;
crAllFrames: TRadioButton;
Label11: TLabel;
Label2: TLabel;
ResizeOrCanvasBox: TPanel;
rocResize: TRadioButton;
rocMCCanvas: TRadioButton;
FramesSize: TPanel;
fszAuto: TRadioButton;
fszCustom: TRadioButton;
fszWidth: TSpinEdit;
Label4: TLabel;
Label5: TLabel;
fszHeight: TSpinEdit;
DitheringCheck: TCheckBox;
Label6: TLabel;
Panel1: TPanel;
Label14: TLabel;
LbTechPal: TLabel;
CboxUseTech: TCheckBox;
CbxPalette: TComboBoxEx;
CbxTechPalette: TComboBoxEx;
CbxOptGame: TComboBoxEx;
Label15: TLabel;
CbTarget: TComboBox;
LbTargetFrame: TLabel;
SpeTargetFrame: TSpinEdit;
Label16: TLabel;
Label17: TLabel;
Label18: TLabel;
Label19: TLabel;
ItsBox: TPanel;
Label20: TLabel;
itsAS00: TRadioButton;
itsASTransparent: TRadioButton;
ssSplitShadows: TCheckBox;
SHPIDOptions: TPanel;
Label12: TLabel;
CbxSHPGame: TComboBoxEx;
Label13: TLabel;
CbxSHPType: TComboBoxEx;
ccmStructuralis: TRadioButton;
XPManifest: TXPManifest;
ccmDeltaE: TRadioButton;
ccmCHLDifference: TRadioButton;
procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
procedure BtCancelClick(Sender: TObject);
procedure BtBrowseClick(Sender: TObject);
procedure BtOKClick(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure SingleLoadImage(var SHP : TSHP; var Start:colour_element; var List,Last:listed_colour; alg:byte);
procedure MassLoadImage(var SHP : TSHP; var BStart:colour_element; var List,Last:listed_colour; alg:byte);
procedure bcColourEditChange(Sender: TObject);
procedure bcAutoSelectClick(Sender: TObject);
procedure bcDefaultClick(Sender: TObject);
procedure bcCustomClick(Sender: TObject);
procedure bcNoneClick(Sender: TObject);
procedure AutoSelectBackground; overload;
procedure AutoSelectBackground2(const Bitmap:TBitmap);
procedure ocfComboOptionsChange(Sender: TObject);
procedure fszWidthExit(Sender: TObject);
procedure fszHeightExit(Sender: TObject);
procedure fszHeightChange(Sender: TObject);
procedure fszAutoClick(Sender: TObject);
procedure SetAutoSizes;
procedure SetAutoSizes2(Bitmap:TBitmap);
procedure ocfStyleChange(Sender: TObject);
procedure CallForCanvas(var Bitmap:TBitmap; const Width,Height:Word);
procedure cbModeClick(Sender: TObject);
procedure CbxSHPGameChange(Sender: TObject);
function GetComboBoxNoFromString(filename:string; default : string; game: string): cardinal;
procedure CbxOptGameChange(Sender: TObject);
procedure CboxUseTechClick(Sender: TObject);
procedure BuildTargetCombo();
procedure CbTargetChange(Sender: TObject);
procedure crToChange(Sender: TObject);
procedure crFromChange(Sender: TObject);
procedure SpeTargetFrameChange(Sender: TObject);
procedure itsAS00Click(Sender: TObject);
procedure itsASTransparentClick(Sender: TObject);
private
{ Private declarations }
pal_dir : String[3];
pal_prefix : String;
pal_sufix : String[3];
bgcolour : Tcolor;
maxframes : integer;
SHPPalette : TPalette;
DataList : TDataList;
Data_No : Integer;
procedure AutoSelectBackground(const Filename : string; Frame : integer); overload;
public
{ Public declarations }
mode : integer;
end;
implementation
uses FormMain, FormPreview{, FormManualColourMatch};
{$R *.dfm}
procedure TFrmImportImageAsSHP.BtCancelClick(Sender: TObject);
begin
Close;
end;
procedure TFrmImportImageAsSHP.BtBrowseClick(Sender: TObject);
var
x:integer;
temp,ext,filename : string;
ImageList : TStringList;
Bitmap:TBitmap;
begin
if cbMode.Checked then
begin
OpenPictureDialog.InitialDir := FrmMain.ImportDir;
if OpenPictureDialog.Execute then
begin
if fileexists(OpenPictureDialog.FileName) then
begin
Image_Location.Text := OpenPictureDialog.FileName;
ext := AnsiLowerCase(ExtractFileExt(Image_Location.Text));
if CompareStr(ext,'.gif') = 0 then
begin
// GIF code here:
maxframes := GetNumberOfFramesFromGif(Image_Location.Text);
// Enable AutoSelect Background options.
ItsBox.Enabled := true;
itsASTransparent.Checked := true;
end
else
begin
// First Part:
// The following code detects the max ammount of
// frames
// Creates the string list
ImageList := TStringList.Create;
// these files all files in the dir image*.bmp
// (so, it gets 0000, to XXXX)
FileListBox.Directory := ExtractFileDir(Image_Location.Text);
FileListBox.mask := copy(Image_Location.Text,0,length(Image_Location.Text)- length('0000' +ExtractFileExt(Image_Location.Text)))+'*' + ExtractFileExt(Image_Location.Text);
// removes the 0000 and extension from filename,
// so, you can load 0000 to XXXX (check below)
filename := copy(extractfilename(Image_Location.Text),0,length(extractfilename(Image_Location.Text))-length('0000'+ExtractFileExt(Image_Location.Text)));
// this is probably a hack to avoid problems on loading
// on root like C:\image 0000.bmp, D:\image 0000.bmp, etc...
if not (copy(FileListBox.Directory,length(FileListBox.Directory),1) = '\') then
filename := '\' + filename;
ImageList.Clear;
// Fixes prob with mask system finding wrong files.
For x := 0 to FileListBox.Items.Count-1 do
begin
temp := inttostr(x);
if length(temp) < 4 then
repeat
Temp := '0' + Temp;
until length(temp) = 4;
if fileexists(FileListBox.Directory+filename+temp+ExtractFileExt(Image_Location.Text)) then
ImageList.Add(FileListBox.Directory+filename+temp+ExtractFileExt(Image_Location.Text));
end;
maxframes := ImageList.Count;
// Check AutoSelect Background options.
// Enable for PNG
if CompareStr(ext,'.png') = 0 then
begin
ItsBox.Enabled := true;
itsASTransparent.Checked := true;
end
else // Disable for others.
begin
ItsBox.Enabled := false;
itsAS00.Checked := true;
end;
end;
crFrom.MaxValue := maxframes-1;
crTo.MaxValue := maxframes-1;
// Second Part: Interface
// Enable Options
ConversionRangeBox.Enabled := true;
CboxUseTech.Enabled := true;
CboxUseTechClick(sender);
// Code below selects all frames and adds the max
// frame in the Conversion Range settings
crTo.Value := maxframes - 1;
end
else // user put an invalid filename
begin
exit;
end;
end
else // user press cancel
begin
exit;
end;
end
else
begin
OpenPictureDialog1.InitialDir := FrmMain.ImportDir;
if OpenPictureDialog1.Execute then
begin
if fileexists(OpenPictureDialog1.FileName) then
begin
Image_Location.Text := OpenPictureDialog1.FileName;
// Code below adds the max frame in the Conversion
// Range settings
crTo.Value := 0;
maxframes := 1;
// Interface Stuff
// Enable Options
ConversionRangeBox.Enabled := false;
CboxUseTech.Enabled := false;
CboxUseTechClick(sender);
end
else // user places invalid file
begin
exit;
end;
end
else // User press cancel
begin
exit;
end;
end;
// For both modes:
// Enable Options
ConversionOptimizeBox.Enabled := true;
ColourConversionBox.Enabled := true;
ConversionOptimizeBox.Enabled := true;
BackgroundOverrideBox.Enabled := true;
bcColourEdit.Enabled := true;
BtOK.Enabled := true;
SplitShadowBox.Enabled := true;
FramesSize.Enabled := true;
ResizeOrCanvasBox.Enabled := true;
CbxPalette.Enabled := true;
CbTarget.Enabled := true;
CbxSHPGame.Enabled := true;
CbxSHPType.Enabled := true;
SHPIDOptions.Enabled := true;
CbxOptGame.Enabled := true;
CbxOptGame.ItemIndex := GIX_NONE;
// Load first Frame so we check size and autoselect colour;
Bitmap := GetBMPFromImageFile(Image_Location.Text);
FrmMain.ImportDir := extractfiledir(Image_Location.Text);
// Code below runs 'AutoSelect' if that was used in the last time
if bcAutoSelect.Checked then
AutoSelectBackground2(Bitmap);
// Set default values for Frames Size
SetAutoSizes2(Bitmap);
// Get rid of Bitmap:
Bitmap.Free;
// Sets 'All frames' as default for conversion range
crAllFrames.Checked := true;
crFrom.Value := 0;
end;
function colourtogray(colour : cardinal): cardinal;
var
temp : char;
begin
temp := char((GetBValue(colour)*29 + GetGValue(colour)*150 + GetRValue(colour)*77) DIV 256);
Result := RGB(ord(temp),ord(temp),ord(temp));
end;
Function GrayBitmap(var Bitmap:TBitmap; const x,y : integer) : Tbitmap;
var
Temp : Tbitmap;
xx,yy : integer;
begin
Temp := TBitmap.Create;
Temp.Width := Bitmap.Width;
Temp.Height := Bitmap.Height;
for xx := 0 to Bitmap.Width-1 do
for yy := 0 to Bitmap.Height-1 do
if ((xx <> x) and (yy <> y)) or ((xx = x) and (yy = y)) then
Temp.Canvas.Pixels[xx,yy] := Bitmap.Canvas.Pixels[xx,yy]
else
Temp.Canvas.Pixels[xx,yy] := colourtogray(Bitmap.Canvas.Pixels[xx,yy]);
Result := Temp;
end;
procedure TFrmImportImageAsSHP.BtOKClick(Sender: TObject);
var
alg : integer;
Bitmap : TBitmap;
Start : colour_element;
List,Last : listed_colour;
Data : TSHPImageData;
SHP : TSHP;
MyData : TSHPImageData;
OldFrames,Counter : longword;
FrmInstall: TFrmRepairAssistant;
begin
Bitmap := GetBMPFromImageFile(Image_Location.Text); // Load for autoselect
{
if fileexists(FrmMain.paletteschemes[CbxPalette.ItemIndex+1].filename) then
GetPaletteFromFile(FrmMain.paletteschemes[CbxPalette.ItemIndex+1].filename,SHPPalette)
else
begin
ShowMessage('Error! Palette not found! Please, restore it or reinstall ' + SHP_BUILDER_TITLE + '. The program will use unittem.pal from TS.');
GetPaletteFromFile(extractfiledir(ParamStr(0))+'\Palettes\TS\unittem.pal',SHPPalette);
end;
}
// Set palette
if not fileexists(FrmMain.PaletteControl.paletteschemes[CbxPalette.ItemIndex].filename) then
begin
FrmMain.RunInstall;
end;
GetPaletteFromFile(FrmMain.PaletteControl.paletteschemes[CbxPalette.ItemIndex].filename,SHPPalette);
// Prepare The Colour List and Colour Banks
GenerateColourList(SHPPalette,List,Last,BGColour,bcNone.Checked,(ssShadow.Enabled and ssShadow.Checked),(ssIgnoreLastColours.Enabled and ssIgnoreLastColours.Checked),(ssRedToRemap.Enabled and ssRedToRemap.Checked));
PrepareBank(Start,List,Last);
// Set algorithm to be used
{ if ccmAutoSelect.Checked then
begin
alg := AutoSelectALG_Progress(ProgressBar,Bitmap,SHPPalette,List,Last);
showmessage('Selected Colour Conversion Mode: ' + inttostr(alg)); //Enable to debug auto select
end
else if ccmStu.Checked then
alg := 1
else if ccmStu2.Checked then
alg := 2
else if ccmBanshee.Checked then
alg := 3
else} if ccmBanshee2.Checked then
alg := 4
else if ccmBanshee3.Checked then
alg := 5
else if ccmStructuralis.Checked then
alg := 7
else if ccmDeltaE.Checked then
alg := 8
else if ccmCHLDifference.Checked then
alg := 9
else // Infurium
alg := Infurium;
ProgressBar.Visible := false;
Bitmap.Free;
// If file doesnt exist, it gives an error message.
if not fileexists(Image_Location.Text) then
begin
MessageBox(0,'Error: No Valid Image Location Specified','Import Error',0);
Exit;
end;
// this will convert the image.
if (cbMode.Checked) then
MassLoadImage(SHP,Start,List,Last,alg)
else
SingleLoadImage(SHP,Start,List,Last,alg);
// Remove the trash:
ClearColourList(List,Last);
ClearBank(Start);
// Locks FormMain until it's set to the new file
FrmMain.SetIsEditable(False);
// Generate an SHP
if (CbTarget.ItemIndex = Data_No) then
begin // New SHP
if AddNewSHPDataItem(Data,FrmMain.TotalImages,SHP,SHPPalette,FrmMain.PaletteControl.paletteschemes[CbxPalette.ItemIndex].filename,CbxSHPGame.ItemIndex,CbxSHPType.ItemIndex) then
begin
// Generate New Window
if FrmMain.GenerateNewWindow(Data) then
begin
// Set new palette (when it changes)
// FrmMain.SetPalette(extractfiledir(ParamStr(0))+'\Palettes\' + pal_dir + '\' + pal_prefix + pal_sufix + '.pal');
FrmMain.lblPalette.Caption := ' Palette - '+ ExtractFileName(FrmMain.PaletteControl.paletteschemes[CbxPalette.ItemIndex].filename);
LoadNewSHPImageSettings(Data,FrmMain.ActiveForm^);
end
else
begin
ClearUpData(Data);
ShowMessage('MDI Error! Import failed to create window due to lack of memory.');
if MainData^.Next <> nil then
FrmMain.SetIsEditable(True);
close;
end;
end
else
begin
ShowMessage('Warning: Import failed due to lack of memory!');
if MainData^.Next <> nil then
FrmMain.SetIsEditable(True);
close;
end;
end
else // Existing SHP
begin
// We move up the SHP.Header.NumImages frames, starting from Target Frame
// the source.
MyData := DataList[CbTarget.ItemIndex];
// First time, check up Split Shadows:
// The user may change the ammount of frames in the interval
if (ssSplitShadows.Enabled = true) and (ssSplitShadows.Checked = true) then
if (SHP.Header.NumImages mod 2 <> 0) or (MyData^.SHP.Header.NumImages mod 2 <> 0) then
begin
ShowMessage('Notice: Split Shadows feature was disabled. It can only split shadows with even frames, no odds.');
ssSplitShadows.Checked := false;
end;
// If Split shadows
if (ssSplitShadows.Enabled = true) and (ssSplitShadows.Checked = true) then
begin
// Add Undo Item
AddToUndoBlankFrame(MyData^.UndoList,speTargetFrame.Value,speTargetFrame.Value + SHP.Header.NumImages - 1,(MyData^.SHP.Header.NumImages div 2) + speTargetFrame.Value + SHP.Header.NumImages - 1);
// Get OldFrames.
OldFrames := MyData^.SHP.Header.NumImages;
// Generate blank frames
MoveSeveralFrameImagesUp(MyData^.SHP,speTargetFrame.Value,(SHP.Header.NumImages div 2));
MoveSeveralFrameImagesUp(MyData^.SHP,(OldFrames div 2) + speTargetFrame.Value + (SHP.Header.NumImages div 2),(SHP.Header.NumImages div 2));
// Paste buffer on blank frames
For Counter := 0 to (SHP.Header.NumImages div 2)-1 do
begin
MyData^.SHP.Data[speTargetFrame.Value + Counter].FrameImage := copy(SHP.Data[Counter+1].FrameImage);
MyData^.SHP.Data[(MyData^.SHP.Header.NumImages div 2) + speTargetFrame.Value + Counter].FrameImage := copy(SHP.Data[(SHP.Header.NumImages div 2) + Counter + 1].FrameImage);
end;
end
else
begin
// Add Undo Item
AddToUndoBlankFrame(MyData^.UndoList,speTargetFrame.Value,speTargetFrame.Value + SHP.Header.NumImages - 1,false);
// Generate blank frames
MoveSeveralFrameImagesUp(MyData^.SHP,speTargetFrame.Value,SHP.Header.NumImages);
// Paste buffer on blank frames
For Counter := 0 to SHP.Header.NumImages-1 do
begin
MyData^.SHP.Data[speTargetFrame.Value + Counter].FrameImage := copy(SHP.Data[Counter+1].FrameImage);
end;
end;
// Moving pointers to selected data.
FrmMain.ActiveData := MyData;
FrmMain.ActiveForm := Addr(MyData^.Form^.Item);
FrmMain.SetIsEditable(True);
FrmMain.ActiveForm^.SetShadowMode(FrmMain.ActiveForm^.ShadowMode); // Fakes a shadow change so frame lengths are set
FrmMain.UndoUpdate(MyData^.UndoList);
end;
Close;
end;
procedure TFrmImportImageAsSHP.MassLoadImage(var SHP : TSHP; var BStart:colour_element; var List,Last:listed_colour; alg:byte);
var
start,final,x: integer;
ImageList : TStringList;
ext,temp,filename : string;
Bitmap : TBitmap;
begin
// initialize a new SHP file.
NewSHP(SHP,maxframes,fszWidth.Value,fszHeight.Value);
// Creates the string list
ImageList := TStringList.Create;
ext := AnsiLowerCase(ExtractFileExt(Image_Location.Text));
if CompareStr(ext,'.gif') <> 0 then
begin
// these files all files in the dir image*.bmp (so, it gets 0000, to XXXX)
FileListBox.Directory := ExtractFileDir(Image_Location.Text);
FileListBox.mask := copy(Image_Location.Text,0,length(Image_Location.Text)- length('0000' +ExtractFileExt(Image_Location.Text)))+'*' + ExtractFileExt(Image_Location.Text);
// removes the 0000 and extension from filename, so, you can load 0000 to XXXX (check below)
filename := copy(extractfilename(Image_Location.Text),0,length(extractfilename(Image_Location.Text))-length('0000'+ExtractFileExt(Image_Location.Text)));
// this is probably a hack to avoid problems on loading
// on root like C:\image 0000.bmp, D:\image 0000.bmp, etc...
if not (copy(FileListBox.Directory,length(FileListBox.Directory),1) = '\') then
filename := '\' + filename;
// Clear ImageList to start re-check
ImageList.Clear;
// Fixes prob with mask system finding wrong files.
For x := 0 to FileListBox.Items.Count-1 do
begin
temp := inttostr(x);
if length(temp) < 4 then
repeat
Temp := '0' + Temp;
until length(temp) = 4;
if fileexists(FileListBox.Directory+filename+temp+ExtractFileExt(Image_Location.Text)) then
ImageList.Add(FileListBox.Directory+filename+temp+ExtractFileExt(Image_Location.Text));
end;
end;
// Check the conversion range
if crCustomFrames.Checked then
begin
start := Min(crFrom.Value,crTo.Value);
final := Max(crFrom.Value,crTo.Value);
end
else
begin
start := 0;
if maxframes <> 0 then
final := maxframes-1
else
final := 0;
end;
// shows and set Progress bar to user
ProgressBar.Visible := true;
ProgressBar.Max := final - start + 1;
ProgressBar.Position := 0;
// Incase they were set wrong, reset them.
SHP.Header.NumImages := (final - start) + 1;
SetLength(SHP.Data,SHP.Header.NumImages+1);
// This is where we start loading frame by frame. First, non frames
For x := start to (start + ((final - start) div 2)) do
begin
ProgressBar.Position := x - start;
if CompareStr(ext,'.gif') <> 0 then
Bitmap := GetBMPFromImageFile(ImageList.Strings[x])
else
begin
Bitmap := GetBMPFromImageFile(Image_Location.Text,x);
AutoSelectBackground(Image_Location.Text,x);
end;
// Now it checks if the bitmap picture fits the size
// of the SHP.
if (Bitmap.Width <> fszWidth.Value) or (Bitmap.Height <> fszHeight.Value) then
begin
if rocresize.Checked then
Resize_Bitmap_Blocky(Bitmap,fszWidth.Value,fszHeight.Value)
else
CallForCanvas(Bitmap,fszWidth.Value,fszHeight.Value);
end;
// Checks for Hybrid palettes from Tech Buildings
if CboxUseTech.Checked then
if ((x - start) = 3) then
if fileexists(FrmMain.PaletteControl.paletteschemes[CbxTechPalette.ItemIndex].filename) then
begin
ClearColourList(List,Last);
ClearBank(BStart);
GetPaletteFromFile(FrmMain.PaletteControl.paletteschemes[CbxTechPalette.ItemIndex].filename,SHPPalette);
GenerateColourList(SHPPalette,List,Last,BGColour,bcNone.Checked,(ssShadow.Enabled and ssShadow.Checked),(ssIgnoreLastColours.Enabled and ssIgnoreLastColours.Checked),(ssRedToRemap.Enabled and ssRedToRemap.Checked));
PrepareBank(BStart,List,Last);
end
else
if fileexists(FrmMain.PaletteControl.paletteschemes[CbxPalette.ItemIndex].filename) then
begin
ClearColourList(List,Last);
ClearBank(BStart);
GetPaletteFromFile(FrmMain.PaletteControl.paletteschemes[CbxPalette.ItemIndex].filename,SHPPalette);
GenerateColourList(SHPPalette,List,Last,BGColour,bcNone.Checked,(ssShadow.Enabled and ssShadow.Checked),(ssIgnoreLastColours.Enabled and ssIgnoreLastColours.Checked),(ssRedToRemap.Enabled and ssRedToRemap.Checked));
PrepareBank(BStart,List,Last);
end;
// Load frame
LoadFrameImageToSHP(SHP,(x - start) +1,Bitmap,List,Last,BStart,alg,DitheringCheck.Checked);
end;
// Import Shadow Part (if it really wants shadow)
For x := (start + ((final - start) div 2)) + 1 to final do
begin
ProgressBar.Position := x - start;
if CompareStr(ext,'.gif') <> 0 then
Bitmap := GetBMPFromImageFile(ImageList.Strings[x])
else
Bitmap := GetBMPFromImageFile(Image_Location.Text,x);
// Now it checks if the bitmap picture fits the size
// of the SHP.
if (Bitmap.Width <> fszWidth.Value) or (Bitmap.Height <> fszHeight.Value) then
begin
if rocresize.Checked then
Resize_Bitmap_Blocky(Bitmap,fszWidth.Value,fszHeight.Value)
else
begin
CallForCanvas(Bitmap,fszWidth.Value,fszHeight.Value);
end;
end;
// Checks for Hybrid palettes from Tech Buildings
if CboxUseTech.Checked then
if ((x - start) = 7) then
if fileexists(FrmMain.PaletteControl.paletteschemes[CbxTechPalette.ItemIndex].filename) then
GetPaletteFromFile(FrmMain.PaletteControl.paletteschemes[CbxTechPalette.ItemIndex].filename,SHPPalette)
else
if fileexists(extractfiledir(ParamStr(0))+'\Palettes\RA2\unit' + pal_sufix + '.pal') then
GetPaletteFromFile(extractfiledir(ParamStr(0))+'\Palettes\RA2\unit' + pal_sufix + '.pal',SHPPalette);
// Load frame
if not ssShadow.Checked then
LoadFrameImageToSHP(SHP,(x - start) +1,Bitmap,List,Last,BStart,alg,DitheringCheck.Checked)
else
SetFrameImageFrmBMP2WithShadows(SHP,(x - start) + 1,Bitmap,BGColour);
end;
// Hybrid palettes always loads unittem.pal as default
if CboxUseTech.Checked then
GetPaletteFromFile(FrmMain.PaletteControl.paletteschemes[CbxPalette.ItemIndex].filename,SHPPalette);
// Free Btimap
Bitmap.Free;
end;
procedure TFrmImportImageAsSHP.SingleLoadImage(var SHP : TSHP; var Start:colour_element; var List,Last:listed_colour; alg:byte);
type
TRGBPixel = record
r,g,b : Byte;
end;
var
Bitmap :TBitmap;
x,y,xx,yy : integer;
r,g,b : Byte;
diff : Integer;
Min,Max : TRGBPixel;
begin
// Set SHP file and avoid access violations
NewSHP(SHP,1,fszWidth.Value,fszHeight.Value);
SetLength(SHP.Data,SHP.Header.NumImages+1);
// Now it checks if the bitmap picture fits the size
// of the SHP.
Bitmap := GetBMPFromImageFile(Image_Location.Text);
if (Bitmap.Width <> fszWidth.Value) or (Bitmap.Height <> fszHeight.Value) then
begin
if rocresize.Checked then
Resize_Bitmap_Blocky(Bitmap,fszWidth.Value,fszHeight.Value)
else
CallForCanvas(Bitmap,fszWidth.Value,fszHeight.Value);
end;
// Set frame
LoadFrameImageToSHP(SHP,1,Bitmap,List,Last,Start,alg,DitheringCheck.Checked);
Bitmap.Free;
end;
procedure TFrmImportImageAsSHP.CallForCanvas(var Bitmap:TBitmap; const Width,Height:Word);
var
FrmCanvasResize : TFrmCanvasResize;
xb,xe,yb,ye:integer;
w,h:word;
begin
FrmCanvasResize := TFrmCanvasResize.Create(FrmMain);
FrmCanvasResize.Bitmap := Bitmap;
FrmCanvasResize.Height := Height;
FrmCanvasResize.Width := Width;
FrmCanvasResize.LockSize := true;
FrmCanvasResize.ShowModal;
if FrmCanvasResize.changed then
begin
xb := StrToIntDef(FrmCanvasResize.SpinL.text,0);
xe := StrToIntDef(FrmCanvasResize.SpinR.Text,0);
yb := StrToIntDef(FrmCanvasResize.SpinT.Text,0);
ye := StrToIntDef(FrmCanvasResize.SpinB.Text,0);
if (((xb <> 0) or (xe <> 0)) or ((yb <> 0) or (ye <> 0))) then
begin
w := FrmCanvasResize.Bitmap.Width;
h := FrmCanvasResize.Bitmap.Height;
CanvasResize(Bitmap,w,h,-xb,-yb,xe,ye);
end;
end;
FrmCanvasResize.Release;
end;
procedure TFrmImportImageAsSHP.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
if Key = VK_RETURN then
begin
BtOkClick(Sender);
end
else if Key = VK_ESCAPE then
begin
BtCancelClick(Sender);
end;
end;
procedure TFrmImportImageAsSHP.FormShow(Sender: TObject);
var
x : longword;
begin
PageControl1.ActivePage := TabSheet1;
PageControl1.TabIndex := 0;
TabSheet1.Visible := true;
//Manual_Colour_Match.Checked := false;
Image_Location.Text := '';
maxframes := 0;
// Palette Checking
if FileExists(FrmMain.ActiveData^.SHPPaletteFilename) then
GetPaletteFromFile(FrmMain.ActiveData^.SHPPaletteFilename,SHPPalette)
else
GetPaletteFromFile(MainData^.SHPPaletteFilename,SHPPalette);
// Disable Options
ConversionOptimizeBox.Enabled := false;
ConversionRangeBox.Enabled := false;
ColourConversionBox.Enabled := false;
ConversionOptimizeBox.Enabled := false;
BackgroundOverrideBox.Enabled := false;
bcColourEdit.Enabled := false;
BtOK.Enabled := false;
SplitShadowBox.Enabled := false;
ResizeOrCanvasBox.Enabled := false;
// 3.35: Aditions:
CbxTechPalette.Enabled := false;
LbTechPal.Enabled := false;
CbxPalette.Enabled := false;
CbTarget.Enabled := false;
CbxOptGame.Enabled := false;
CbxOptGame.ItemIndex := GIX_NONE;
ItsBox.Enabled := false;
itsAS00.Checked := true;
// Check Box Mode settings
cbMode.Checked := true;
case (FrmMain.alg) of
0: ccmStructuralis.Checked := true; // ccmAutoSelect.Checked := true;
1: ccmStructuralis.Checked := true;
2: ccmStructuralis.Checked := true;
3: ccmStructuralis.Checked := true;
4: ccmBanshee2.Checked := true;
5: ccmBanshee3.Checked := true;
7: ccmStructuralis.Checked := true;
8: ccmDeltaE.Checked := true;
9: ccmCHLDifference.Checked := true;
Infurium: ccmInfurion.Checked := true;
else
ccmStructuralis.Checked := true; // ccmAutoSelect.Checked := true;
end;
// 3.35: Fill Palettes:
for x := Low(FrmMain.PaletteControl.PaletteSchemes) to High(FrmMain.PaletteControl.PaletteSchemes) do
begin
CbxPalette.Items.Add(extractfilename(FrmMain.PaletteControl.paletteschemes[x].filename));
CbxPalette.ItemsEx.ComboItems[x].ImageIndex := FrmMain.PaletteControl.paletteschemes[x].ImageIndex;
CbxTechPalette.Items.Add(extractfilename(FrmMain.PaletteControl.paletteschemes[x].filename));
CbxTechPalette.ItemsEx.ComboItems[x].ImageIndex := FrmMain.PaletteControl.paletteschemes[x].ImageIndex;
end;
CbxPalette.ItemIndex := GetComboBoxNoFromString(FrmMain.ActiveData^.SHPPaletteFilename,'unittem.pal','ts');
CbxTechPalette.ItemIndex := GetComboBoxNoFromString(' ','isotem.pal','ra2');
// 3.35: Update Game & Type at Extra Settings
CbxSHPGame.ItemIndex := 2; // Default is TS.
CbxSHPGameChange(sender);
CbxSHPType.ItemIndex := 0; // Any (Auto);
// Keep 'em disabled.
SHPIDOptions.Enabled := false;
// 3.35: Build target combobox.
BuildTargetCombo;
CbTarget.ItemIndex := Data_No;
CbTargetChange(sender);
end;
// 3.35: This function gets the combo value from a palette.
// Copied from Preferences.
function TFrmImportImageAsSHP.GetComboBoxNoFromString(filename:string; default : string; game: string): cardinal;
var
f_name : string;
x : integer;
begin
f_name := ansilowercase(filename);
result := 0;
for x := Low(FrmMain.PaletteControl.PaletteSchemes) to High(FrmMain.PaletteControl.PaletteSchemes) do
if ansilowercase(FrmMain.PaletteControl.PaletteSchemes[x].Filename) = f_name then
begin
result := x;
exit;
end;
// Try default;
f_name := ExtractFileDir(ParamStr(0)) + '\Palettes\' + game + '\' + default;
f_name := ansilowercase(f_name);
for x := Low(FrmMain.PaletteControl.PaletteSchemes) to High(FrmMain.PaletteControl.PaletteSchemes) do
if ansilowercase(FrmMain.PaletteControl.PaletteSchemes[x].Filename) = f_name then
begin
result := x;
exit;
end;
end;
// 3.35: Get all opened files. Code imported from 3.4 Beta
procedure TFrmImportImageAsSHP.BuildTargetCombo();
var
MyData : TSHPImageData;
begin
// Fill the Paste To File part:
MyData := MainData^.Next;
Data_No := 0;
while MyData <> nil do
begin
// Add data option to user
if MyData^.Filename <> '' then
CbTarget.Items.Add(MyData^.Filename)
else
CbTarget.Items.Add('Untitled ' + inttostr(MyData^.ID));
// Add data option to computer
setlength(DataList,Data_No+1);
DataList[Data_No] := MyData;
inc(Data_No);
// go to the next data
MyData := MyData^.Next;
end;
// Now it adds a "New SHP" option
CbTarget.Items.Add('New SHP File');
setlength(DataList,Data_No+1);
DataList[Data_No] := nil;
end;
procedure TFrmImportImageAsSHP.bcColourEditChange(Sender: TObject);
begin
if ColorDialog1.Execute then
begin
bcColourEdit.Color := ColorDialog1.Color;
// If you click at the colour, it auto-sets to custom
// Leave it in that way.
bcCustom.Checked := true;
bgcolour := ColorDialog1.Color;
end;
end;
procedure TFrmImportImageAsSHP.bcAutoSelectClick(Sender: TObject);
begin
AutoSelectBackground;
ssShadow.Enabled := true;
ssIgnoreLastColours.Enabled := true;
end;
procedure TFrmImportImageAsSHP.bcDefaultClick(Sender: TObject);
var
CustomPalette : TPalette;
begin
if CbxOptGame.ItemIndex <> GIX_NONE then
begin
// Load Palette:
if fileexists(extractfiledir(ParamStr(0))+'\Palettes\'+ pal_dir + '\' + pal_prefix + pal_sufix + '.pal') then
GetPaletteFromFile(extractfiledir(ParamStr(0))+'\Palettes\'+ pal_dir + '\' + pal_prefix + pal_sufix + '.pal',CustomPalette);
bgcolour := CustomPalette[0];
end
else
bgcolour := SHPPalette[0];
bcColourEdit.Color := bgcolour;
ssShadow.Enabled := true;
ssIgnoreLastColours.Enabled := true;
end;
procedure TFrmImportImageAsSHP.bcCustomClick(Sender: TObject);
begin
bgcolour := ColorDialog1.Color;
bcColourEdit.Color := bgcolour;
ssShadow.Enabled := true;
ssIgnoreLastColours.Enabled := true;
end;
procedure TFrmImportImageAsSHP.bcNoneClick(Sender: TObject);
begin
bcColourEdit.Color := clSilver;
ssShadow.Enabled := false;
ssIgnoreLastColours.Enabled := false;
end;
procedure TFrmImportImageAsSHP.AutoSelectBackground(const Filename : string; Frame : integer);
var
Bitmap: TBitmap;
begin
if itsAS00.Checked then