--- oup/rewrite/Tools/TxmpReplace.pas 2007/01/22 23:05:45 97 +++ oup/current/Tools/TxmpReplace.pas 2007/06/04 22:07:29 209 @@ -27,6 +27,7 @@ type procedure btn_saveClick(Sender: TObject); procedure btn_loadClick(Sender: TObject); procedure btn_replaceClick(Sender: TObject); + procedure Splitter1Moved(Sender: TObject); private OniImage_Old: TOniImage; OniImage_New: TOniImage; @@ -40,50 +41,48 @@ var implementation {$R *.dfm} -uses Main, ConnectionManager; +uses Main, ConnectionManager, ImagingTypes, TXMP; procedure TForm_TxmpReplace.SelectFile(fileinfo: TFileInfo); var - Data: TByteData; - mem: TMemoryStream; fadingbyte, depthbyte, storebyte: Byte; begin fileid := fileinfo.ID; - Connection.LoadDatFilePart(fileid, $88, SizeOf(fadingbyte), @fadingbyte); - Connection.LoadDatFilePart(fileid, $89, SizeOf(depthbyte), @depthbyte); - Connection.LoadDatFilePart(fileid, $90, SizeOf(storebyte), @storebyte); - check_fading.Checked := (fadingbyte and $01) > 0; - check_transparency.Checked := (depthbyte and $04) > 0; - - OniImage_Old.LoadFromTXMP(connection, fileid); - old_size := OniImage_Old.GetImageDataSize((fadingbyte and $01) > 0); - Data := OniImage_Old.GetAsBMP; - mem := TMemoryStream.Create; - mem.Write(Data[0], Length(Data)); - mem.Seek(0, soFromBeginning); - image_txmppreview.Picture.Bitmap.LoadFromStream(mem); - mem.Free; + ConManager.Connection[ConnectionID].LoadDatFilePart(fileid, $88, SizeOf(fadingbyte), @fadingbyte); + ConManager.Connection[ConnectionID].LoadDatFilePart(fileid, $89, SizeOf(depthbyte), @depthbyte); + ConManager.Connection[ConnectionID].LoadDatFilePart(fileid, $90, SizeOf(storebyte), @storebyte); + + OniImage_Old.LoadFromTXMP(ConnectionID, fileid); + old_size := OniImage_Old.GetImageSize(True); + OniImage_Old.DrawOnCanvas(image_txmppreview.Canvas, 1); + + check_fading.Checked := OniImage_Old.HasMipMaps; +// check_transparency.Checked := (depthbyte and $04) > 0; + check_transparency.Checked := storebyte in [0, 2, 7]; group_bmpselect.Enabled := True; end; +procedure TForm_TxmpReplace.Splitter1Moved(Sender: TObject); +begin + inherited; + image_txmppreview.Picture.Assign(nil); + image_bmppreview.Picture.Assign(nil); + if Length(OniImage_Old.Images) > 0 then + OniImage_Old.DrawOnCanvas(image_txmppreview.Canvas, 1); + if Length(OniImage_New.Images) > 0 then + OniImage_New.DrawOnCanvas(image_bmppreview.Canvas, 1); +end; + procedure TForm_TxmpReplace.btn_loadClick(Sender: TObject); -var - mem: TMemoryStream; - tempd: Tdata; begin if opend.Execute then begin - OniImage_New.LoadFromBMP(opend.FileName); - tempd := OniImage_New.GetAsBMP; - mem := TMemoryStream.Create; - mem.Write(tempd[0], Length(tempd)); - mem.Seek(0, soFromBeginning); - image_bmppreview.Picture.Bitmap.LoadFromStream(mem); - mem.Free; + OniImage_New.LoadFromFile(opend.FileName); + OniImage_New.DrawOnCanvas(image_bmppreview.Canvas, 1); group_options.Enabled := True; end; end; @@ -93,77 +92,121 @@ end; procedure TForm_TxmpReplace.btn_replaceClick(Sender: TObject); var - newsize: LongWord; - old_rawaddr, new_rawaddr: LongWord; + newsize: Integer; + old_rawaddr, new_rawaddr: Integer; oldfading: Byte; - tempd: Tdata; - datbyte: Word; + mem: TMemoryStream; + new_storetype: Byte; + i: Integer; +const + powers: array[0..8] of Integer = (1, 2, 4, 8, 16, 32, 64, 128, 256); begin if filelist.ItemIndex >= 0 then begin - Connection.LoadDatFilePart(fileid, $88, 1, @oldfading); - if Connection.OSisMac then - Connection.UpdateDatFilePart(fileid, $A0, 4, @old_rawaddr) + ConManager.Connection[ConnectionID].LoadDatFilePart(fileid, $88, 1, @oldfading); + if not (ConManager.Connection[ConnectionID].DataOS = DOS_WIN) then + ConManager.Connection[ConnectionID].LoadDatFilePart(fileid, $A0, 4, @old_rawaddr) else - Connection.LoadDatFilePart(fileid, $9C, 4, @old_rawaddr); + ConManager.Connection[ConnectionID].LoadDatFilePart(fileid, $9C, 4, @old_rawaddr); + + if (OniImage_New.Width[1] > 256) or (OniImage_New.Height[1] > 256) then + begin + ShowMessage('Widht and height have to be smaller than or equal to 256.'); + Exit; + end; + for i := 0 to High(powers) do + if OniImage_New.Width[1] = powers[i] then + Break; + if i = Length(powers) then + begin + ShowMessage('Width has to be a power of 2 (1, 2, 4, 8, 16 ...)'); + Exit; + end; + for i := 0 to High(powers) do + if OniImage_New.Height[1] = powers[i] then + Break; + if i = Length(powers) then + begin + ShowMessage('Height has to be a power of 2 (1, 2, 4, 8, 16 ...)'); + Exit; + end; - if (OniImage_Old.Width <> OniImage_New.Width) or - (OniImage_Old.Height <> OniImage_New.Height) then + if (OniImage_Old.Width[1] <> OniImage_New.Width[1]) or + (OniImage_Old.Height[1] <> OniImage_New.Height[1]) then begin - if MessageBox(Self.Handle, PChar( - 'Current image and new image have different size' + CrLf + - '(Current: ' + IntToStr(OniImage_Old.Width) + - 'x' + IntToStr(OniImage_Old.Height) + ' - New: ' + - IntToStr(OniImage_New.Width) + 'x' + IntToStr(OniImage_New.Height) + ')' + CrLf + - 'Replace anyways?'), - PChar(filelist.Items.Strings[filelist.ItemIndex]), - MB_YESNO) = idNo then + if MessageBox(Self.Handle, + PChar( + 'Current image and new image have different size' + CrLf + + '(Current: ' + IntToStr(OniImage_Old.Width[1]) + 'x' + + IntToStr(OniImage_Old.Height[1]) + ' - New: ' + + IntToStr(OniImage_New.Width[1]) + 'x' + + IntToStr(OniImage_New.Height[1]) + + ')' + CrLf + 'Replace anyway?'), + PChar(filelist.Items.Strings[filelist.ItemIndex]), MB_YESNO) = idNo then Exit; end; - if check_fading.Checked then - if not OniImage_New.GetMipMappedImage(tempd) then - if MessageBox(Self.Handle, - PChar('Can not create a MipMapped-image (probably because of a wrong dimension).' + - #13 + #10 + 'Do you want to continue without MipMapping?'), PChar('Warning'), - MB_YESNO) = ID_YES then - check_fading.Checked := False - else - Exit; - - if not check_fading.Checked then - tempd := OniImage_New.GetAsData; - - newsize := OniImage_New.GetImageDataSize(check_fading.Checked); - - if (newsize > old_size) and (Connection.Backend = ODB_Dat) then - new_rawaddr := Connection.AppendRawFile( - Connection.OSisMac, Length(tempd), tempd) + mem := TMemoryStream.Create; + + case OniImage_New.Format of + ifX1R5G5B5: new_storetype := 1; + ifA1R5G5B5: new_storetype := 2; + ifA4R4G4B4: new_storetype := 0; + ifA8R8G8B8: + begin + new_storetype := 8; + OniImage_New.Format := ifX8R8G8B8; + end; + ifX8R8G8B8: new_storetype := 8; + ifDXT1: new_storetype := 9; + else + if OniImage_New.FormatInfo.HasAlphaChannel then + ShowMessage('Loaded image has an alpha-channel.' + #13#10 + + 'Because the format is neither ARGB1555' +#13#10 + + 'nor ARGB4444 it can not be imported without conversion.' + #13#10 + + 'It is converted to RGB888, so alpha gets dropped.' + #13#10 + + 'If you need alpha you have to save your image in' + #13#10 + + 'one of the previously named formats.'); + OniImage_New.Format := ifX8R8G8B8; + new_storetype := 8; + end; + + OniImage_New.SaveDataToStream(check_fading.Checked, TStream(mem)); + + newsize := mem.Size; + mem.Seek(0, soFromBeginning); + + if (newsize > old_size) and (ConManager.Connection[ConnectionID].Backend = DB_ONI) then + new_rawaddr := ConManager.Connection[ConnectionID].AppendRawFile( + not (ConManager.Connection[ConnectionID].DataOS = DOS_WIN), mem) else begin new_rawaddr := old_rawaddr; - Connection.UpdateRawFile(fileid, $9C, Length(tempd), tempd); + ConManager.Connection[ConnectionID].UpdateRawFile(fileid, $9C, mem); end; - datbyte := $00; if check_fading.Checked then - datbyte := datbyte or $01; - Connection.UpdateDatFilePart(fileid, $88, 1, @datbyte); + oldfading := oldfading or $01 + else + oldfading := oldfading and (not Byte($01)); + ConManager.Connection[ConnectionID].UpdateDatFilePart(fileid, $88, 1, @datbyte); datbyte := $10; - if check_transparency.Checked then - datbyte := datbyte or $04; - Connection.UpdateDatFilePart(fileid, $89, 1, @datbyte); - Connection.UpdateDatFilePart(fileid, $8C, 2, @OniImage_New.Width); - Connection.UpdateDatFilePart(fileid, $8E, 2, @OniImage_New.Height); - datbyte := $08; - Connection.UpdateDatFilePart(fileid, $90, 1, @datbyte); - if Connection.OSisMac then - Connection.UpdateDatFilePart(fileid, $A0, 4, @new_rawaddr) +// if check_transparency.Checked then +// datbyte := datbyte or $04; + ConManager.Connection[ConnectionID].UpdateDatFilePart(fileid, $89, 1, @datbyte); + datbyte := OniImage_New.Width[1]; + ConManager.Connection[ConnectionID].UpdateDatFilePart(fileid, $8C, 2, @datbyte); + datbyte := OniImage_New.Height[1]; + ConManager.Connection[ConnectionID].UpdateDatFilePart(fileid, $8E, 2, @datbyte); + ConManager.Connection[ConnectionID].UpdateDatFilePart(fileid, $90, 1, @new_storetype); + if not (ConManager.Connection[ConnectionID].DataOS = DOS_WIN) then + ConManager.Connection[ConnectionID].UpdateDatFilePart(fileid, $A0, 4, @new_rawaddr) else - Connection.UpdateDatFilePart(fileid, $9C, 4, @new_rawaddr); + ConManager.Connection[ConnectionID].UpdateDatFilePart(fileid, $9C, 4, @new_rawaddr); ShowMessage('TXMP-image replaced'); + Self.listClick(Self); end; end; @@ -181,12 +224,15 @@ end; procedure TForm_TxmpReplace.FormCreate(Sender: TObject); +var + TXMP: TFile_TXMP; begin inherited; OniImage_Old := TOniImage.Create; OniImage_New := TOniImage.Create; Self.AllowedExts := 'TXMP'; Self.OnNewFileSelected := SelectFile; + opend.Filter := saved.Filter; end; @@ -195,7 +241,7 @@ end; procedure TForm_TxmpReplace.btn_saveClick(Sender: TObject); begin if saved.Execute then - OniImage_Old.WriteToBMP(saved.FileName); + OniImage_Old.WriteToFile(saved.FileName); end; begin