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

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