source: oup/current/Tools/TxmpReplace.pas@ 117

Last change on this file since 117 was 111, checked in by alloc, 18 years ago
File size: 6.3 KB
Line 
1unit TxmpReplace;
2interface
3uses
4 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
5 Dialogs, Template, StdCtrls, ExtCtrls,
6 Functions, Data, OniImgClass, Menus, Buttons, TypeDefs;
7
8type
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
38var
39 Form_TxmpReplace: TForm_TxmpReplace;
40
41implementation
42{$R *.dfm}
43uses Main, ConnectionManager;
44
45
46
47procedure TForm_TxmpReplace.SelectFile(fileinfo: TFileInfo);
48var
49 mem: TMemoryStream;
50 fadingbyte, depthbyte, storebyte: Byte;
51begin
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 check_fading.Checked := (fadingbyte and $01) > 0;
57 check_transparency.Checked := (depthbyte and $04) > 0;
58
59 OniImage_Old.LoadFromTXMP(ConnectionID, fileid);
60 old_size := OniImage_Old.GetImageDataSize((fadingbyte and $01) > 0);
61 mem := TMemoryStream.Create;
62 OniImage_Old.GetAsBMP(TStream(mem));
63 mem.Seek(0, soFromBeginning);
64 image_txmppreview.Picture.Bitmap.LoadFromStream(mem);
65 mem.Free;
66
67 group_bmpselect.Enabled := True;
68end;
69
70
71procedure TForm_TxmpReplace.btn_loadClick(Sender: TObject);
72var
73 mem: TMemoryStream;
74begin
75 if opend.Execute then
76 begin
77 OniImage_New.LoadFromBMP(opend.FileName);
78 mem := TMemoryStream.Create;
79 OniImage_New.GetAsBMP(TStream(mem));
80 mem.Seek(0, soFromBeginning);
81 image_bmppreview.Picture.Bitmap.LoadFromStream(mem);
82 mem.Free;
83 group_options.Enabled := True;
84 end;
85end;
86
87
88
89
90procedure TForm_TxmpReplace.btn_replaceClick(Sender: TObject);
91var
92 newsize: Integer;
93 old_rawaddr, new_rawaddr: Integer;
94 oldfading: Byte;
95 datbyte: Word;
96 mem: TMemoryStream;
97begin
98 if filelist.ItemIndex >= 0 then
99 begin
100 ConManager.Connection[ConnectionID].LoadDatFilePart(fileid, $88, 1, @oldfading);
101 if not (ConManager.Connection[ConnectionID].DataOS = DOS_WIN) then
102 ConManager.Connection[ConnectionID].LoadDatFilePart(fileid, $A0, 4, @old_rawaddr)
103 else
104 ConManager.Connection[ConnectionID].LoadDatFilePart(fileid, $9C, 4, @old_rawaddr);
105
106 if (OniImage_Old.Width <> OniImage_New.Width) or
107 (OniImage_Old.Height <> OniImage_New.Height) then
108 begin
109 if MessageBox(Self.Handle,
110 PChar(
111 'Current image and new image have different size' + CrLf +
112 '(Current: ' + IntToStr(OniImage_Old.Width) + 'x' +
113 IntToStr(OniImage_Old.Height) + ' - New: ' +
114 IntToStr(OniImage_New.Width) + 'x' + IntToStr(OniImage_New.Height) +
115 ')' + CrLf + 'Replace anyway?'),
116 PChar(filelist.Items.Strings[filelist.ItemIndex]), MB_YESNO) = idNo then
117 Exit;
118 end;
119
120 mem := TMemoryStream.Create;
121
122 if check_fading.Checked then
123 if not OniImage_New.GetMipMappedImage(TStream(mem)) then
124 if MessageBox(Self.Handle,
125 PChar('Can not create a MipMapped-image (probably because of a wrong dimension).' +
126 #13 + #10 + 'Do you want to continue without MipMapping?'), PChar('Warning'),
127 MB_YESNO) = ID_YES then
128 check_fading.Checked := False
129 else
130 Exit;
131
132 if not check_fading.Checked then
133 begin
134 mem.Clear;
135 OniImage_New.GetAsData(TStream(mem));
136 end;
137
138 newsize := OniImage_New.GetImageDataSize(check_fading.Checked);
139
140 if (newsize > old_size) and (ConManager.Connection[ConnectionID].Backend = DB_ONI) then
141 new_rawaddr := ConManager.Connection[ConnectionID].AppendRawFile(
142 not (ConManager.Connection[ConnectionID].DataOS = DOS_WIN), mem.Size, mem)
143 else
144 begin
145 new_rawaddr := old_rawaddr;
146 ConManager.Connection[ConnectionID].UpdateRawFile(fileid, $9C, mem);
147 end;
148
149 datbyte := $00;
150 if check_fading.Checked then
151 datbyte := datbyte or $01;
152 ConManager.Connection[ConnectionID].UpdateDatFilePart(fileid, $88, 1, @datbyte);
153 datbyte := $10;
154 if check_transparency.Checked then
155 datbyte := datbyte or $04;
156 ConManager.Connection[ConnectionID].UpdateDatFilePart(fileid, $89, 1, @datbyte);
157 ConManager.Connection[ConnectionID].UpdateDatFilePart(fileid, $8C, 2, @OniImage_New.Width);
158 ConManager.Connection[ConnectionID].UpdateDatFilePart(fileid, $8E, 2, @OniImage_New.Height);
159 datbyte := $08;
160 ConManager.Connection[ConnectionID].UpdateDatFilePart(fileid, $90, 1, @datbyte);
161 if not (ConManager.Connection[ConnectionID].DataOS = DOS_WIN) then
162 ConManager.Connection[ConnectionID].UpdateDatFilePart(fileid, $A0, 4, @new_rawaddr)
163 else
164 ConManager.Connection[ConnectionID].UpdateDatFilePart(fileid, $9C, 4, @new_rawaddr);
165
166 ShowMessage('TXMP-image replaced');
167 end;
168end;
169
170
171
172
173procedure TForm_TxmpReplace.FormClose(Sender: TObject; var Action: TCloseAction);
174begin
175 OniImage_Old.Free;
176 OniImage_New.Free;
177 inherited;
178end;
179
180
181
182
183procedure TForm_TxmpReplace.FormCreate(Sender: TObject);
184begin
185 inherited;
186 OniImage_Old := TOniImage.Create;
187 OniImage_New := TOniImage.Create;
188 Self.AllowedExts := 'TXMP';
189 Self.OnNewFileSelected := SelectFile;
190end;
191
192
193
194
195procedure TForm_TxmpReplace.btn_saveClick(Sender: TObject);
196begin
197 if saved.Execute then
198 OniImage_Old.WriteToBMP(saved.FileName);
199end;
200
201begin
202 AddToolListEntry('txmpreplace', 'TXMP Replacer', 'TXMP');
203end.
Note: See TracBrowser for help on using the repository browser.