| 1 | unit Tool_TxmpReplace; | 
|---|
| 2 |  | 
|---|
| 3 | interface | 
|---|
| 4 |  | 
|---|
| 5 | uses | 
|---|
| 6 | Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, | 
|---|
| 7 | Dialogs, ExtCtrls, StdCtrls, StrUtils, Code_Functions, Data, Code_OniImgClass; | 
|---|
| 8 |  | 
|---|
| 9 | type | 
|---|
| 10 | TForm_TxmpReplace = class(TForm) | 
|---|
| 11 | panel_12: TPanel; | 
|---|
| 12 | group_txmpselect: TGroupBox; | 
|---|
| 13 | splitter_txmp: TSplitter; | 
|---|
| 14 | list_txmp: TListBox; | 
|---|
| 15 | Splitter1: TSplitter; | 
|---|
| 16 | group_bmpselect: TGroupBox; | 
|---|
| 17 | panel_load: TPanel; | 
|---|
| 18 | btn_load: TButton; | 
|---|
| 19 | image_bmppreview: TImage; | 
|---|
| 20 | opend:    TOpenDialog; | 
|---|
| 21 | group_options: TGroupBox; | 
|---|
| 22 | btn_replace: TButton; | 
|---|
| 23 | check_transparency: TCheckBox; | 
|---|
| 24 | check_fading: TCheckBox; | 
|---|
| 25 | panel_txmppreview: TPanel; | 
|---|
| 26 | btn_save: TButton; | 
|---|
| 27 | image_txmppreview: TImage; | 
|---|
| 28 | saved:    TSaveDialog; | 
|---|
| 29 | procedure FormCreate(Sender: TObject); | 
|---|
| 30 | procedure btn_saveClick(Sender: TObject); | 
|---|
| 31 | procedure FormClose(Sender: TObject; var Action: TCloseAction); | 
|---|
| 32 | procedure btn_replaceClick(Sender: TObject); | 
|---|
| 33 | procedure btn_loadClick(Sender: TObject); | 
|---|
| 34 | procedure list_txmpClick(Sender: TObject); | 
|---|
| 35 | procedure Recreatelist; | 
|---|
| 36 | procedure list_txmpMouseDown(Sender: TObject; Button: TMouseButton; | 
|---|
| 37 | Shift: TShiftState; X, Y: Integer); | 
|---|
| 38 | private | 
|---|
| 39 | OniImage_Old: TOniImage; | 
|---|
| 40 | OniImage_New: TOniImage; | 
|---|
| 41 | public | 
|---|
| 42 | end; | 
|---|
| 43 |  | 
|---|
| 44 | var | 
|---|
| 45 | Form_TxmpReplace: TForm_TxmpReplace; | 
|---|
| 46 |  | 
|---|
| 47 | implementation | 
|---|
| 48 |  | 
|---|
| 49 | uses Main, Code_OniDataClass; | 
|---|
| 50 |  | 
|---|
| 51 | {$R *.dfm} | 
|---|
| 52 |  | 
|---|
| 53 |  | 
|---|
| 54 |  | 
|---|
| 55 | procedure TForm_TxmpReplace.Recreatelist; | 
|---|
| 56 | var | 
|---|
| 57 | files: TStringArray; | 
|---|
| 58 | i:     LongWord; | 
|---|
| 59 | begin | 
|---|
| 60 | list_txmp.Items.Clear; | 
|---|
| 61 | files := OniDataConnection.GetFilesList('TXMP', '', True); | 
|---|
| 62 | if Length(files) > 0 then | 
|---|
| 63 | for i := 0 to High(files) do | 
|---|
| 64 | list_txmp.Items.Add(files[i]); | 
|---|
| 65 | group_bmpselect.Enabled := False; | 
|---|
| 66 | check_transparency.Checked := False; | 
|---|
| 67 | check_fading.Checked := False; | 
|---|
| 68 | end; | 
|---|
| 69 |  | 
|---|
| 70 |  | 
|---|
| 71 |  | 
|---|
| 72 |  | 
|---|
| 73 | procedure TForm_TxmpReplace.list_txmpClick(Sender: TObject); | 
|---|
| 74 | var | 
|---|
| 75 | id:   LongWord; | 
|---|
| 76 | Data: Tdata; | 
|---|
| 77 | mem:  TMemoryStream; | 
|---|
| 78 | fadingbyte, depthbyte, storebyte: Byte; | 
|---|
| 79 | begin | 
|---|
| 80 | id := OniDataConnection.ExtractFileID(list_txmp.Items.Strings[list_txmp.ItemIndex]); | 
|---|
| 81 | OniDataConnection.LoadDatFilePart(id, $88, SizeOf(fadingbyte), @fadingbyte); | 
|---|
| 82 | OniDataConnection.LoadDatFilePart(id, $89, SizeOf(depthbyte), @depthbyte); | 
|---|
| 83 | OniDataConnection.LoadDatFilePart(id, $90, SizeOf(storebyte), @storebyte); | 
|---|
| 84 | check_fading.Checked := (fadingbyte and $01) > 0; | 
|---|
| 85 | check_transparency.Checked := (depthbyte and $04) > 0; | 
|---|
| 86 |  | 
|---|
| 87 | OniImage_Old.LoadFromTXMP(id); | 
|---|
| 88 | Data := OniImage_Old.GetAsBMP; | 
|---|
| 89 | mem  := TMemoryStream.Create; | 
|---|
| 90 | mem.Write(Data[0], Length(Data)); | 
|---|
| 91 | mem.Seek(0, soFromBeginning); | 
|---|
| 92 | image_txmppreview.Picture.Bitmap.LoadFromStream(mem); | 
|---|
| 93 | mem.Free; | 
|---|
| 94 |  | 
|---|
| 95 | group_bmpselect.Enabled := True; | 
|---|
| 96 | end; | 
|---|
| 97 |  | 
|---|
| 98 |  | 
|---|
| 99 |  | 
|---|
| 100 |  | 
|---|
| 101 | procedure TForm_TxmpReplace.list_txmpMouseDown(Sender: TObject; | 
|---|
| 102 | Button: TMouseButton; Shift: TShiftState; X, Y: Integer); | 
|---|
| 103 | var | 
|---|
| 104 | pt: TPoint; | 
|---|
| 105 | begin | 
|---|
| 106 | pt.X := x; | 
|---|
| 107 | pt.Y := y; | 
|---|
| 108 | list_txmp.ItemIndex := list_txmp.ItemAtPos(pt, true); | 
|---|
| 109 | if list_txmp.ItemIndex > -1 then | 
|---|
| 110 | Self.list_txmpClick(Self); | 
|---|
| 111 | end; | 
|---|
| 112 |  | 
|---|
| 113 | procedure TForm_TxmpReplace.btn_loadClick(Sender: TObject); | 
|---|
| 114 | var | 
|---|
| 115 | mem:   TMemoryStream; | 
|---|
| 116 | tempd: Tdata; | 
|---|
| 117 | begin | 
|---|
| 118 | if opend.Execute then | 
|---|
| 119 | begin | 
|---|
| 120 | OniImage_New.LoadFromBMP(opend.FileName); | 
|---|
| 121 | tempd := OniImage_New.GetAsBMP; | 
|---|
| 122 | mem   := TMemoryStream.Create; | 
|---|
| 123 | mem.Write(tempd[0], Length(tempd)); | 
|---|
| 124 | mem.Seek(0, soFromBeginning); | 
|---|
| 125 | image_bmppreview.Picture.Bitmap.LoadFromStream(mem); | 
|---|
| 126 | mem.Free; | 
|---|
| 127 | group_options.Enabled := True; | 
|---|
| 128 | end; | 
|---|
| 129 | end; | 
|---|
| 130 |  | 
|---|
| 131 |  | 
|---|
| 132 |  | 
|---|
| 133 |  | 
|---|
| 134 | procedure TForm_TxmpReplace.btn_replaceClick(Sender: TObject); | 
|---|
| 135 | var | 
|---|
| 136 | id: LongWord; | 
|---|
| 137 |  | 
|---|
| 138 | oldsize, newsize: LongWord; | 
|---|
| 139 | old_rawaddr, new_rawaddr: LongWord; | 
|---|
| 140 | oldfading: Byte; | 
|---|
| 141 | tempd:     Tdata; | 
|---|
| 142 |  | 
|---|
| 143 | datbyte: Word; | 
|---|
| 144 | begin | 
|---|
| 145 | if list_txmp.ItemIndex >= 0 then | 
|---|
| 146 | begin | 
|---|
| 147 | id := OniDataConnection.ExtractFileID(list_txmp.Items.Strings[list_txmp.ItemIndex]); | 
|---|
| 148 | OniDataConnection.LoadDatFilePart(id, $88, 1, @oldfading); | 
|---|
| 149 | if OniDataConnection.OSisMac then | 
|---|
| 150 | OniDataConnection.UpdateDatFilePart(id, $A0, 4, @old_rawaddr) | 
|---|
| 151 | else | 
|---|
| 152 | OniDataConnection.LoadDatFilePart(id, $9C, 4, @old_rawaddr); | 
|---|
| 153 |  | 
|---|
| 154 | if (OniImage_Old.Width <> OniImage_New.Width) or | 
|---|
| 155 | (OniImage_Old.Height <> OniImage_New.Height) then | 
|---|
| 156 | begin | 
|---|
| 157 | if MessageBox(Self.Handle, PChar( | 
|---|
| 158 | 'Current image and new image have different size' + CrLf + | 
|---|
| 159 | '(Current: ' + IntToStr(OniImage_Old.Width) + | 
|---|
| 160 | 'x' + IntToStr(OniImage_Old.Height) + ' - New: ' + | 
|---|
| 161 | IntToStr(OniImage_New.Width) + 'x' + IntToStr(OniImage_New.Height) + ')' + CrLf + | 
|---|
| 162 | 'Replace anyways?'), | 
|---|
| 163 | PChar(list_txmp.Items.Strings[list_txmp.ItemIndex]), | 
|---|
| 164 | MB_YESNO) = idNo then | 
|---|
| 165 | Exit; | 
|---|
| 166 | end; | 
|---|
| 167 |  | 
|---|
| 168 | oldsize := OniImage_Old.GetImageDataSize((oldfading and $01) > 0); | 
|---|
| 169 |  | 
|---|
| 170 | if check_fading.Checked then | 
|---|
| 171 | if not OniImage_New.GetMipMappedImage(tempd) then | 
|---|
| 172 | if MessageBox(Self.Handle, | 
|---|
| 173 | PChar('Can not create a MipMapped-image (probably because of a wrong dimension).' + | 
|---|
| 174 | #13 + #10 + 'Do you want to continue without MipMapping?'), PChar('Warning'), | 
|---|
| 175 | MB_YESNO) = ID_YES then | 
|---|
| 176 | check_fading.Checked := False | 
|---|
| 177 | else | 
|---|
| 178 | Exit; | 
|---|
| 179 |  | 
|---|
| 180 | if not check_fading.Checked then | 
|---|
| 181 | tempd := OniImage_New.GetAsData; | 
|---|
| 182 |  | 
|---|
| 183 | newsize := OniImage_New.GetImageDataSize(check_fading.Checked); | 
|---|
| 184 | ShowMessage(IntToStr(newsize)); | 
|---|
| 185 |  | 
|---|
| 186 | if (newsize > oldsize) and (OniDataConnection.Backend = ODB_Dat) then | 
|---|
| 187 | new_rawaddr := OniDataConnection.AppendRawFile( | 
|---|
| 188 | OniDataConnection.OSisMac, Length(tempd), tempd) | 
|---|
| 189 | else | 
|---|
| 190 | begin | 
|---|
| 191 | new_rawaddr := old_rawaddr; | 
|---|
| 192 | OniDataConnection.UpdateRawFile(id, $9C, Length(tempd), tempd); | 
|---|
| 193 | end; | 
|---|
| 194 |  | 
|---|
| 195 | datbyte := $00; | 
|---|
| 196 | if check_fading.Checked then | 
|---|
| 197 | datbyte := datbyte or $01; | 
|---|
| 198 | OniDataConnection.UpdateDatFilePart(id, $88, 1, @datbyte); | 
|---|
| 199 | datbyte := $10; | 
|---|
| 200 | if check_transparency.Checked then | 
|---|
| 201 | datbyte := datbyte or $04; | 
|---|
| 202 | OniDataConnection.UpdateDatFilePart(id, $89, 1, @datbyte); | 
|---|
| 203 | OniDataConnection.UpdateDatFilePart(id, $8C, 2, @OniImage_New.Width); | 
|---|
| 204 | OniDataConnection.UpdateDatFilePart(id, $8E, 2, @OniImage_New.Height); | 
|---|
| 205 | datbyte := $08; | 
|---|
| 206 | OniDataConnection.UpdateDatFilePart(id, $90, 1, @datbyte); | 
|---|
| 207 | if OniDataConnection.OSisMac then | 
|---|
| 208 | OniDataConnection.UpdateDatFilePart(id, $A0, 4, @new_rawaddr) | 
|---|
| 209 | else | 
|---|
| 210 | OniDataConnection.UpdateDatFilePart(id, $9C, 4, @new_rawaddr); | 
|---|
| 211 |  | 
|---|
| 212 | ShowMessage('TXMP-image replaced'); | 
|---|
| 213 | end; | 
|---|
| 214 | end; | 
|---|
| 215 |  | 
|---|
| 216 |  | 
|---|
| 217 |  | 
|---|
| 218 |  | 
|---|
| 219 | procedure TForm_TxmpReplace.FormClose(Sender: TObject; var Action: TCloseAction); | 
|---|
| 220 | begin | 
|---|
| 221 | OniImage_Old.Free; | 
|---|
| 222 | OniImage_New.Free; | 
|---|
| 223 | Action := caFree; | 
|---|
| 224 | end; | 
|---|
| 225 |  | 
|---|
| 226 |  | 
|---|
| 227 |  | 
|---|
| 228 |  | 
|---|
| 229 | procedure TForm_TxmpReplace.FormCreate(Sender: TObject); | 
|---|
| 230 | begin | 
|---|
| 231 | OniImage_Old := TOniImage.Create; | 
|---|
| 232 | OniImage_New := TOniImage.Create; | 
|---|
| 233 | end; | 
|---|
| 234 |  | 
|---|
| 235 |  | 
|---|
| 236 |  | 
|---|
| 237 |  | 
|---|
| 238 | procedure TForm_TxmpReplace.btn_saveClick(Sender: TObject); | 
|---|
| 239 | begin | 
|---|
| 240 | if saved.Execute then | 
|---|
| 241 | OniImage_Old.WriteToBMP(saved.FileName); | 
|---|
| 242 | end; | 
|---|
| 243 |  | 
|---|
| 244 | end. | 
|---|