1 |
unit TxmpReplace; |
2 |
interface |
3 |
uses |
4 |
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, |
5 |
Dialogs, Template, StdCtrls, ExtCtrls, |
6 |
Functions, Data, OniImgClass, Menus, Buttons, TypeDefs; |
7 |
|
8 |
type |
9 |
TForm_TxmpReplace = class(TForm_ToolTemplate) |
10 |
group_options: TGroupBox; |
11 |
btn_replace: TButton; |
12 |
check_transparency: TCheckBox; |
13 |
check_fading: TCheckBox; |
14 |
panel_txmppreview: TPanel; |
15 |
btn_save: TButton; |
16 |
image_txmppreview: TImage; |
17 |
splitter_txmp: TSplitter; |
18 |
group_bmpselect: TGroupBox; |
19 |
image_bmppreview: TImage; |
20 |
panel_load: TPanel; |
21 |
btn_load: TButton; |
22 |
opend: TOpenDialog; |
23 |
saved: TSaveDialog; |
24 |
procedure SelectFile(fileinfo: TFileInfo); |
25 |
procedure FormCreate(Sender: TObject); |
26 |
procedure FormClose(Sender: TObject; var Action: TCloseAction); |
27 |
procedure btn_saveClick(Sender: TObject); |
28 |
procedure btn_loadClick(Sender: TObject); |
29 |
procedure btn_replaceClick(Sender: TObject); |
30 |
private |
31 |
OniImage_Old: TOniImage; |
32 |
OniImage_New: TOniImage; |
33 |
old_size: Integer; |
34 |
fileid: Integer; |
35 |
public |
36 |
end; |
37 |
|
38 |
var |
39 |
Form_TxmpReplace: TForm_TxmpReplace; |
40 |
|
41 |
implementation |
42 |
{$R *.dfm} |
43 |
uses Main, ConnectionManager, ImagingTypes; |
44 |
|
45 |
|
46 |
|
47 |
procedure TForm_TxmpReplace.SelectFile(fileinfo: TFileInfo); |
48 |
var |
49 |
mem: TMemoryStream; |
50 |
fadingbyte, depthbyte, storebyte: Byte; |
51 |
begin |
52 |
fileid := fileinfo.ID; |
53 |
ConManager.Connection[ConnectionID].LoadDatFilePart(fileid, $88, SizeOf(fadingbyte), @fadingbyte); |
54 |
ConManager.Connection[ConnectionID].LoadDatFilePart(fileid, $89, SizeOf(depthbyte), @depthbyte); |
55 |
ConManager.Connection[ConnectionID].LoadDatFilePart(fileid, $90, SizeOf(storebyte), @storebyte); |
56 |
|
57 |
OniImage_Old.LoadFromTXMP(ConnectionID, fileid); |
58 |
old_size := OniImage_Old.GetImageSize(True); |
59 |
OniImage_Old.DrawOnCanvas(image_txmppreview.Canvas, 2); |
60 |
|
61 |
check_fading.Checked := OniImage_Old.HasMipMaps; |
62 |
// check_transparency.Checked := (depthbyte and $04) > 0; |
63 |
check_transparency.Checked := storebyte in [0, 2, 7]; |
64 |
|
65 |
group_bmpselect.Enabled := True; |
66 |
end; |
67 |
|
68 |
|
69 |
procedure TForm_TxmpReplace.btn_loadClick(Sender: TObject); |
70 |
var |
71 |
mem: TMemoryStream; |
72 |
begin |
73 |
if opend.Execute then |
74 |
begin |
75 |
OniImage_New.LoadFromFile(opend.FileName); |
76 |
OniImage_New.DrawOnCanvas(image_bmppreview.Canvas, 1); |
77 |
group_options.Enabled := True; |
78 |
end; |
79 |
end; |
80 |
|
81 |
|
82 |
|
83 |
|
84 |
procedure TForm_TxmpReplace.btn_replaceClick(Sender: TObject); |
85 |
var |
86 |
newsize: Integer; |
87 |
old_rawaddr, new_rawaddr: Integer; |
88 |
oldfading: Byte; |
89 |
datbyte: Word; |
90 |
mem: TMemoryStream; |
91 |
new_storetype: Byte; |
92 |
begin |
93 |
if filelist.ItemIndex >= 0 then |
94 |
begin |
95 |
ConManager.Connection[ConnectionID].LoadDatFilePart(fileid, $88, 1, @oldfading); |
96 |
if not (ConManager.Connection[ConnectionID].DataOS = DOS_WIN) then |
97 |
ConManager.Connection[ConnectionID].LoadDatFilePart(fileid, $A0, 4, @old_rawaddr) |
98 |
else |
99 |
ConManager.Connection[ConnectionID].LoadDatFilePart(fileid, $9C, 4, @old_rawaddr); |
100 |
|
101 |
if (OniImage_Old.Width[1] <> OniImage_New.Width[1]) or |
102 |
(OniImage_Old.Height[1] <> OniImage_New.Height[1]) then |
103 |
begin |
104 |
if MessageBox(Self.Handle, |
105 |
PChar( |
106 |
'Current image and new image have different size' + CrLf + |
107 |
'(Current: ' + IntToStr(OniImage_Old.Width[1]) + 'x' + |
108 |
IntToStr(OniImage_Old.Height[1]) + ' - New: ' + |
109 |
IntToStr(OniImage_New.Width[1]) + 'x' + |
110 |
IntToStr(OniImage_New.Height[1]) + |
111 |
')' + CrLf + 'Replace anyway?'), |
112 |
PChar(filelist.Items.Strings[filelist.ItemIndex]), MB_YESNO) = idNo then |
113 |
Exit; |
114 |
end; |
115 |
|
116 |
mem := TMemoryStream.Create; |
117 |
|
118 |
case OniImage_New.Format of |
119 |
ifA1R5G5B5: new_storetype := 2; |
120 |
ifA4R4G4B4: new_storetype := 0; |
121 |
ifA8R8G8B8: |
122 |
begin |
123 |
new_storetype := 8; |
124 |
OniImage_New.Format := ifX8R8G8B8; |
125 |
end; |
126 |
ifX8R8G8B8: new_storetype := 8; |
127 |
ifDXT1: new_storetype := 9; |
128 |
else |
129 |
OniImage_New.Format := ifX8R8G8B8; |
130 |
new_storetype := 8; |
131 |
end; |
132 |
|
133 |
OniImage_New.SaveDataToStream(check_fading.Checked, TStream(mem)); |
134 |
|
135 |
newsize := mem.Size; |
136 |
mem.Seek(0, soFromBeginning); |
137 |
|
138 |
if (newsize > old_size) and (ConManager.Connection[ConnectionID].Backend = DB_ONI) then |
139 |
new_rawaddr := ConManager.Connection[ConnectionID].AppendRawFile( |
140 |
not (ConManager.Connection[ConnectionID].DataOS = DOS_WIN), mem) |
141 |
else |
142 |
begin |
143 |
new_rawaddr := old_rawaddr; |
144 |
ConManager.Connection[ConnectionID].UpdateRawFile(fileid, $9C, mem); |
145 |
end; |
146 |
|
147 |
datbyte := $00; |
148 |
if check_fading.Checked then |
149 |
datbyte := datbyte or $01; |
150 |
ConManager.Connection[ConnectionID].UpdateDatFilePart(fileid, $88, 1, @datbyte); |
151 |
datbyte := $10; |
152 |
// if check_transparency.Checked then |
153 |
// datbyte := datbyte or $04; |
154 |
ConManager.Connection[ConnectionID].UpdateDatFilePart(fileid, $89, 1, @datbyte); |
155 |
datbyte := OniImage_New.Width[1]; |
156 |
ConManager.Connection[ConnectionID].UpdateDatFilePart(fileid, $8C, 2, @datbyte); |
157 |
datbyte := OniImage_New.Height[1]; |
158 |
ConManager.Connection[ConnectionID].UpdateDatFilePart(fileid, $8E, 2, @datbyte); |
159 |
ConManager.Connection[ConnectionID].UpdateDatFilePart(fileid, $90, 1, @new_storetype); |
160 |
if not (ConManager.Connection[ConnectionID].DataOS = DOS_WIN) then |
161 |
ConManager.Connection[ConnectionID].UpdateDatFilePart(fileid, $A0, 4, @new_rawaddr) |
162 |
else |
163 |
ConManager.Connection[ConnectionID].UpdateDatFilePart(fileid, $9C, 4, @new_rawaddr); |
164 |
|
165 |
ShowMessage('TXMP-image replaced'); |
166 |
end; |
167 |
end; |
168 |
|
169 |
|
170 |
|
171 |
|
172 |
procedure TForm_TxmpReplace.FormClose(Sender: TObject; var Action: TCloseAction); |
173 |
begin |
174 |
OniImage_Old.Free; |
175 |
OniImage_New.Free; |
176 |
inherited; |
177 |
end; |
178 |
|
179 |
|
180 |
|
181 |
|
182 |
procedure TForm_TxmpReplace.FormCreate(Sender: TObject); |
183 |
begin |
184 |
inherited; |
185 |
OniImage_Old := TOniImage.Create; |
186 |
OniImage_New := TOniImage.Create; |
187 |
Self.AllowedExts := 'TXMP'; |
188 |
Self.OnNewFileSelected := SelectFile; |
189 |
opend.Filter := saved.Filter; |
190 |
end; |
191 |
|
192 |
|
193 |
|
194 |
|
195 |
procedure TForm_TxmpReplace.btn_saveClick(Sender: TObject); |
196 |
begin |
197 |
if saved.Execute then |
198 |
OniImage_Old.WriteToFile(saved.FileName); |
199 |
end; |
200 |
|
201 |
begin |
202 |
AddToolListEntry('txmpreplace', 'TXMP Replacer', 'TXMP'); |
203 |
end. |