[93] | 1 | unit TxmpReplace;
|
---|
| 2 | interface
|
---|
| 3 | uses
|
---|
| 4 | Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
|
---|
| 5 | Dialogs, Template, StdCtrls, ExtCtrls,
|
---|
[97] | 6 | Functions, Data, OniImgClass, Menus, Buttons, TypeDefs;
|
---|
[93] | 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);
|
---|
[194] | 30 | procedure Splitter1Moved(Sender: TObject);
|
---|
[93] | 31 | private
|
---|
| 32 | OniImage_Old: TOniImage;
|
---|
| 33 | OniImage_New: TOniImage;
|
---|
| 34 | old_size: Integer;
|
---|
| 35 | fileid: Integer;
|
---|
| 36 | public
|
---|
| 37 | end;
|
---|
| 38 |
|
---|
| 39 | var
|
---|
| 40 | Form_TxmpReplace: TForm_TxmpReplace;
|
---|
| 41 |
|
---|
| 42 | implementation
|
---|
| 43 | {$R *.dfm}
|
---|
[192] | 44 | uses Main, ConnectionManager, ImagingTypes;
|
---|
[93] | 45 |
|
---|
| 46 |
|
---|
| 47 |
|
---|
| 48 | procedure TForm_TxmpReplace.SelectFile(fileinfo: TFileInfo);
|
---|
| 49 | var
|
---|
| 50 | mem: TMemoryStream;
|
---|
| 51 | fadingbyte, depthbyte, storebyte: Byte;
|
---|
| 52 | begin
|
---|
| 53 | fileid := fileinfo.ID;
|
---|
[105] | 54 | ConManager.Connection[ConnectionID].LoadDatFilePart(fileid, $88, SizeOf(fadingbyte), @fadingbyte);
|
---|
| 55 | ConManager.Connection[ConnectionID].LoadDatFilePart(fileid, $89, SizeOf(depthbyte), @depthbyte);
|
---|
| 56 | ConManager.Connection[ConnectionID].LoadDatFilePart(fileid, $90, SizeOf(storebyte), @storebyte);
|
---|
[93] | 57 |
|
---|
[105] | 58 | OniImage_Old.LoadFromTXMP(ConnectionID, fileid);
|
---|
[192] | 59 | old_size := OniImage_Old.GetImageSize(True);
|
---|
[194] | 60 | OniImage_Old.DrawOnCanvas(image_txmppreview.Canvas, 1);
|
---|
[93] | 61 |
|
---|
[192] | 62 | check_fading.Checked := OniImage_Old.HasMipMaps;
|
---|
| 63 | // check_transparency.Checked := (depthbyte and $04) > 0;
|
---|
| 64 | check_transparency.Checked := storebyte in [0, 2, 7];
|
---|
| 65 |
|
---|
[93] | 66 | group_bmpselect.Enabled := True;
|
---|
| 67 | end;
|
---|
| 68 |
|
---|
| 69 |
|
---|
[194] | 70 | procedure TForm_TxmpReplace.Splitter1Moved(Sender: TObject);
|
---|
| 71 | begin
|
---|
| 72 | inherited;
|
---|
| 73 | image_txmppreview.Picture.Assign(nil);
|
---|
| 74 | image_bmppreview.Picture.Assign(nil);
|
---|
| 75 | if Length(OniImage_Old.Images) > 0 then
|
---|
| 76 | OniImage_Old.DrawOnCanvas(image_txmppreview.Canvas, 1);
|
---|
| 77 | if Length(OniImage_New.Images) > 0 then
|
---|
| 78 | OniImage_New.DrawOnCanvas(image_bmppreview.Canvas, 1);
|
---|
| 79 | end;
|
---|
| 80 |
|
---|
[93] | 81 | procedure TForm_TxmpReplace.btn_loadClick(Sender: TObject);
|
---|
| 82 | var
|
---|
| 83 | mem: TMemoryStream;
|
---|
| 84 | begin
|
---|
| 85 | if opend.Execute then
|
---|
| 86 | begin
|
---|
[192] | 87 | OniImage_New.LoadFromFile(opend.FileName);
|
---|
[193] | 88 | OniImage_New.DrawOnCanvas(image_bmppreview.Canvas, 1);
|
---|
[93] | 89 | group_options.Enabled := True;
|
---|
| 90 | end;
|
---|
| 91 | end;
|
---|
| 92 |
|
---|
| 93 |
|
---|
| 94 |
|
---|
| 95 |
|
---|
| 96 | procedure TForm_TxmpReplace.btn_replaceClick(Sender: TObject);
|
---|
| 97 | var
|
---|
[111] | 98 | newsize: Integer;
|
---|
| 99 | old_rawaddr, new_rawaddr: Integer;
|
---|
[93] | 100 | oldfading: Byte;
|
---|
| 101 | datbyte: Word;
|
---|
[105] | 102 | mem: TMemoryStream;
|
---|
[193] | 103 | new_storetype: Byte;
|
---|
[194] | 104 | formatinfo: TImageFormatInfo;
|
---|
[93] | 105 | begin
|
---|
| 106 | if filelist.ItemIndex >= 0 then
|
---|
| 107 | begin
|
---|
[105] | 108 | ConManager.Connection[ConnectionID].LoadDatFilePart(fileid, $88, 1, @oldfading);
|
---|
| 109 | if not (ConManager.Connection[ConnectionID].DataOS = DOS_WIN) then
|
---|
| 110 | ConManager.Connection[ConnectionID].LoadDatFilePart(fileid, $A0, 4, @old_rawaddr)
|
---|
[93] | 111 | else
|
---|
[105] | 112 | ConManager.Connection[ConnectionID].LoadDatFilePart(fileid, $9C, 4, @old_rawaddr);
|
---|
[93] | 113 |
|
---|
[192] | 114 | if (OniImage_Old.Width[1] <> OniImage_New.Width[1]) or
|
---|
| 115 | (OniImage_Old.Height[1] <> OniImage_New.Height[1]) then
|
---|
[93] | 116 | begin
|
---|
[105] | 117 | if MessageBox(Self.Handle,
|
---|
| 118 | PChar(
|
---|
| 119 | 'Current image and new image have different size' + CrLf +
|
---|
[192] | 120 | '(Current: ' + IntToStr(OniImage_Old.Width[1]) + 'x' +
|
---|
| 121 | IntToStr(OniImage_Old.Height[1]) + ' - New: ' +
|
---|
| 122 | IntToStr(OniImage_New.Width[1]) + 'x' +
|
---|
| 123 | IntToStr(OniImage_New.Height[1]) +
|
---|
[105] | 124 | ')' + CrLf + 'Replace anyway?'),
|
---|
| 125 | PChar(filelist.Items.Strings[filelist.ItemIndex]), MB_YESNO) = idNo then
|
---|
[93] | 126 | Exit;
|
---|
| 127 | end;
|
---|
| 128 |
|
---|
[105] | 129 | mem := TMemoryStream.Create;
|
---|
| 130 |
|
---|
[193] | 131 | case OniImage_New.Format of
|
---|
| 132 | ifA1R5G5B5: new_storetype := 2;
|
---|
| 133 | ifA4R4G4B4: new_storetype := 0;
|
---|
| 134 | ifA8R8G8B8:
|
---|
| 135 | begin
|
---|
| 136 | new_storetype := 8;
|
---|
| 137 | OniImage_New.Format := ifX8R8G8B8;
|
---|
| 138 | end;
|
---|
| 139 | ifX8R8G8B8: new_storetype := 8;
|
---|
| 140 | ifDXT1: new_storetype := 9;
|
---|
| 141 | else
|
---|
[194] | 142 | if OniImage_New.FormatInfo.HasAlphaChannel then
|
---|
| 143 | ShowMessage('Loaded image has an alpha-channel.' + #13#10 +
|
---|
| 144 | 'Because the format is neither ARGB1555' +#13#10 +
|
---|
| 145 | 'nor ARGB4444 it can not be imported without conversion.' + #13#10 +
|
---|
| 146 | 'It is converted to RGB888, so alpha gets dropped.' + #13#10 +
|
---|
| 147 | 'If you need alpha you have to save your image in' + #13#10 +
|
---|
| 148 | 'one of the previously named formats.');
|
---|
[193] | 149 | OniImage_New.Format := ifX8R8G8B8;
|
---|
| 150 | new_storetype := 8;
|
---|
| 151 | end;
|
---|
| 152 |
|
---|
[192] | 153 | OniImage_New.SaveDataToStream(check_fading.Checked, TStream(mem));
|
---|
[93] | 154 |
|
---|
[192] | 155 | newsize := mem.Size;
|
---|
| 156 | mem.Seek(0, soFromBeginning);
|
---|
[93] | 157 |
|
---|
[105] | 158 | if (newsize > old_size) and (ConManager.Connection[ConnectionID].Backend = DB_ONI) then
|
---|
| 159 | new_rawaddr := ConManager.Connection[ConnectionID].AppendRawFile(
|
---|
[193] | 160 | not (ConManager.Connection[ConnectionID].DataOS = DOS_WIN), mem)
|
---|
[93] | 161 | else
|
---|
| 162 | begin
|
---|
| 163 | new_rawaddr := old_rawaddr;
|
---|
[105] | 164 | ConManager.Connection[ConnectionID].UpdateRawFile(fileid, $9C, mem);
|
---|
[93] | 165 | end;
|
---|
| 166 |
|
---|
| 167 | if check_fading.Checked then
|
---|
[195] | 168 | oldfading := oldfading or $01
|
---|
| 169 | else
|
---|
| 170 | oldfading := oldfading and (not Byte($01));
|
---|
[105] | 171 | ConManager.Connection[ConnectionID].UpdateDatFilePart(fileid, $88, 1, @datbyte);
|
---|
[93] | 172 | datbyte := $10;
|
---|
[192] | 173 | // if check_transparency.Checked then
|
---|
| 174 | // datbyte := datbyte or $04;
|
---|
[105] | 175 | ConManager.Connection[ConnectionID].UpdateDatFilePart(fileid, $89, 1, @datbyte);
|
---|
[192] | 176 | datbyte := OniImage_New.Width[1];
|
---|
| 177 | ConManager.Connection[ConnectionID].UpdateDatFilePart(fileid, $8C, 2, @datbyte);
|
---|
| 178 | datbyte := OniImage_New.Height[1];
|
---|
| 179 | ConManager.Connection[ConnectionID].UpdateDatFilePart(fileid, $8E, 2, @datbyte);
|
---|
[193] | 180 | ConManager.Connection[ConnectionID].UpdateDatFilePart(fileid, $90, 1, @new_storetype);
|
---|
[105] | 181 | if not (ConManager.Connection[ConnectionID].DataOS = DOS_WIN) then
|
---|
| 182 | ConManager.Connection[ConnectionID].UpdateDatFilePart(fileid, $A0, 4, @new_rawaddr)
|
---|
[93] | 183 | else
|
---|
[105] | 184 | ConManager.Connection[ConnectionID].UpdateDatFilePart(fileid, $9C, 4, @new_rawaddr);
|
---|
[93] | 185 |
|
---|
| 186 | ShowMessage('TXMP-image replaced');
|
---|
[194] | 187 | Self.listClick(Self);
|
---|
[93] | 188 | end;
|
---|
| 189 | end;
|
---|
| 190 |
|
---|
| 191 |
|
---|
| 192 |
|
---|
| 193 |
|
---|
| 194 | procedure TForm_TxmpReplace.FormClose(Sender: TObject; var Action: TCloseAction);
|
---|
| 195 | begin
|
---|
| 196 | OniImage_Old.Free;
|
---|
| 197 | OniImage_New.Free;
|
---|
| 198 | inherited;
|
---|
| 199 | end;
|
---|
| 200 |
|
---|
| 201 |
|
---|
| 202 |
|
---|
| 203 |
|
---|
| 204 | procedure TForm_TxmpReplace.FormCreate(Sender: TObject);
|
---|
| 205 | begin
|
---|
| 206 | inherited;
|
---|
| 207 | OniImage_Old := TOniImage.Create;
|
---|
| 208 | OniImage_New := TOniImage.Create;
|
---|
| 209 | Self.AllowedExts := 'TXMP';
|
---|
| 210 | Self.OnNewFileSelected := SelectFile;
|
---|
[192] | 211 | opend.Filter := saved.Filter;
|
---|
[93] | 212 | end;
|
---|
| 213 |
|
---|
| 214 |
|
---|
| 215 |
|
---|
| 216 |
|
---|
| 217 | procedure TForm_TxmpReplace.btn_saveClick(Sender: TObject);
|
---|
| 218 | begin
|
---|
| 219 | if saved.Execute then
|
---|
[192] | 220 | OniImage_Old.WriteToFile(saved.FileName);
|
---|
[93] | 221 | end;
|
---|
| 222 |
|
---|
| 223 | begin
|
---|
| 224 | AddToolListEntry('txmpreplace', 'TXMP Replacer', 'TXMP');
|
---|
| 225 | end.
|
---|