source: oup/releases/0.33a/Tools/TxmpReplace.pas@ 455

Last change on this file since 455 was 54, checked in by alloc, 18 years ago
File size: 5.9 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;
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, OniDataClass;
44
45
46
47procedure TForm_TxmpReplace.SelectFile(fileinfo: TFileInfo);
48var
49 Data: Tdata;
50 mem: TMemoryStream;
51 fadingbyte, depthbyte, storebyte: Byte;
52begin
53 fileid := fileinfo.ID;
54 OniDataConnection.LoadDatFilePart(fileid, $88, SizeOf(fadingbyte), @fadingbyte);
55 OniDataConnection.LoadDatFilePart(fileid, $89, SizeOf(depthbyte), @depthbyte);
56 OniDataConnection.LoadDatFilePart(fileid, $90, SizeOf(storebyte), @storebyte);
57 check_fading.Checked := (fadingbyte and $01) > 0;
58 check_transparency.Checked := (depthbyte and $04) > 0;
59
60 OniImage_Old.LoadFromTXMP(fileid);
61 old_size := OniImage_Old.GetImageDataSize((fadingbyte and $01) > 0);
62 Data := OniImage_Old.GetAsBMP;
63 mem := TMemoryStream.Create;
64 mem.Write(Data[0], Length(Data));
65 mem.Seek(0, soFromBeginning);
66 image_txmppreview.Picture.Bitmap.LoadFromStream(mem);
67 mem.Free;
68
69 group_bmpselect.Enabled := True;
70end;
71
72
73procedure TForm_TxmpReplace.btn_loadClick(Sender: TObject);
74var
75 mem: TMemoryStream;
76 tempd: Tdata;
77begin
78 if opend.Execute then
79 begin
80 OniImage_New.LoadFromBMP(opend.FileName);
81 tempd := OniImage_New.GetAsBMP;
82 mem := TMemoryStream.Create;
83 mem.Write(tempd[0], Length(tempd));
84 mem.Seek(0, soFromBeginning);
85 image_bmppreview.Picture.Bitmap.LoadFromStream(mem);
86 mem.Free;
87 group_options.Enabled := True;
88 end;
89end;
90
91
92
93
94procedure TForm_TxmpReplace.btn_replaceClick(Sender: TObject);
95var
96 newsize: LongWord;
97 old_rawaddr, new_rawaddr: LongWord;
98 oldfading: Byte;
99 tempd: Tdata;
100
101 datbyte: Word;
102begin
103 if filelist.ItemIndex >= 0 then
104 begin
105 OniDataConnection.LoadDatFilePart(fileid, $88, 1, @oldfading);
106 if OniDataConnection.OSisMac then
107 OniDataConnection.UpdateDatFilePart(fileid, $A0, 4, @old_rawaddr)
108 else
109 OniDataConnection.LoadDatFilePart(fileid, $9C, 4, @old_rawaddr);
110
111 if (OniImage_Old.Width <> OniImage_New.Width) or
112 (OniImage_Old.Height <> OniImage_New.Height) then
113 begin
114 if MessageBox(Self.Handle, PChar(
115 'Current image and new image have different size' + CrLf +
116 '(Current: ' + IntToStr(OniImage_Old.Width) +
117 'x' + IntToStr(OniImage_Old.Height) + ' - New: ' +
118 IntToStr(OniImage_New.Width) + 'x' + IntToStr(OniImage_New.Height) + ')' + CrLf +
119 'Replace anyways?'),
120 PChar(filelist.Items.Strings[filelist.ItemIndex]),
121 MB_YESNO) = idNo then
122 Exit;
123 end;
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
140 if (newsize > old_size) and (OniDataConnection.Backend = ODB_Dat) then
141 new_rawaddr := OniDataConnection.AppendRawFile(
142 OniDataConnection.OSisMac, Length(tempd), tempd)
143 else
144 begin
145 new_rawaddr := old_rawaddr;
146 OniDataConnection.UpdateRawFile(fileid, $9C, Length(tempd), tempd);
147 end;
148
149 datbyte := $00;
150 if check_fading.Checked then
151 datbyte := datbyte or $01;
152 OniDataConnection.UpdateDatFilePart(fileid, $88, 1, @datbyte);
153 datbyte := $10;
154 if check_transparency.Checked then
155 datbyte := datbyte or $04;
156 OniDataConnection.UpdateDatFilePart(fileid, $89, 1, @datbyte);
157 OniDataConnection.UpdateDatFilePart(fileid, $8C, 2, @OniImage_New.Width);
158 OniDataConnection.UpdateDatFilePart(fileid, $8E, 2, @OniImage_New.Height);
159 datbyte := $08;
160 OniDataConnection.UpdateDatFilePart(fileid, $90, 1, @datbyte);
161 if OniDataConnection.OSisMac then
162 OniDataConnection.UpdateDatFilePart(fileid, $A0, 4, @new_rawaddr)
163 else
164 OniDataConnection.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.