ViewVC Help
View File | Revision Log | View Changeset | Root Listing
root/Oni2/oup/current/Tools/TxmpReplace.pas
(Generate patch)

Comparing:
oup/rewrite/Tools/TxmpReplace.pas (file contents), Revision 93 by alloc, Thu Jan 18 17:15:59 2007 UTC vs.
oup/current/Tools/TxmpReplace.pas (file contents), Revision 204 by alloc, Sun May 27 20:03:41 2007 UTC

# Line 3 | Line 3 | interface
3   uses
4    Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
5    Dialogs, Template, StdCtrls, ExtCtrls,
6 <  Functions, Data, OniImgClass, Menus, Buttons;
6 >  Functions, Data, OniImgClass, Menus, Buttons, TypeDefs;
7  
8   type
9    TForm_TxmpReplace = class(TForm_ToolTemplate)
# Line 27 | Line 27 | type
27      procedure btn_saveClick(Sender: TObject);
28      procedure btn_loadClick(Sender: TObject);
29      procedure btn_replaceClick(Sender: TObject);
30 +    procedure Splitter1Moved(Sender: TObject);
31    private
32      OniImage_Old: TOniImage;
33      OniImage_New: TOniImage;
# Line 40 | Line 41 | var
41  
42   implementation
43   {$R *.dfm}
44 < uses Main, OniDataClass;
44 > uses Main, ConnectionManager, ImagingTypes;
45  
46  
47  
48   procedure TForm_TxmpReplace.SelectFile(fileinfo: TFileInfo);
49   var
49  Data: Tdata;
50  mem:  TMemoryStream;
50    fadingbyte, depthbyte, storebyte: Byte;
51   begin
52    fileid := fileinfo.ID;
53 <  Connection.LoadDatFilePart(fileid, $88, SizeOf(fadingbyte), @fadingbyte);
54 <  Connection.LoadDatFilePart(fileid, $89, SizeOf(depthbyte), @depthbyte);
55 <  Connection.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(connection, fileid);
60 <  old_size := OniImage_Old.GetImageDataSize((fadingbyte and $01) > 0);
61 <  Data := OniImage_Old.GetAsBMP;
62 <  mem  := TMemoryStream.Create;
63 <  mem.Write(Data[0], Length(Data));
65 <  mem.Seek(0, soFromBeginning);
66 <  image_txmppreview.Picture.Bitmap.LoadFromStream(mem);
67 <  mem.Free;
53 >  ConManager.Connection[ConnectionID].LoadDatFilePart(fileid, $88, SizeOf(fadingbyte), @fadingbyte);
54 >  ConManager.Connection[ConnectionID].LoadDatFilePart(fileid, $89, SizeOf(depthbyte), @depthbyte);
55 >  ConManager.Connection[ConnectionID].LoadDatFilePart(fileid, $90, SizeOf(storebyte), @storebyte);
56 >
57 >  OniImage_Old.LoadFromTXMP(ConnectionID, fileid);
58 >  old_size := OniImage_Old.GetImageSize(True);
59 >  OniImage_Old.DrawOnCanvas(image_txmppreview.Canvas, 1);
60 >
61 >  check_fading.Checked := OniImage_Old.HasMipMaps;
62 > //  check_transparency.Checked := (depthbyte and $04) > 0;
63 >  check_transparency.Checked := storebyte in [0, 2, 7];
64  
65    group_bmpselect.Enabled := True;
66   end;
67  
68  
69 + procedure TForm_TxmpReplace.Splitter1Moved(Sender: TObject);
70 + begin
71 +  inherited;
72 +  image_txmppreview.Picture.Assign(nil);
73 +  image_bmppreview.Picture.Assign(nil);
74 +  if Length(OniImage_Old.Images) > 0 then
75 +    OniImage_Old.DrawOnCanvas(image_txmppreview.Canvas, 1);
76 +  if Length(OniImage_New.Images) > 0 then
77 +    OniImage_New.DrawOnCanvas(image_bmppreview.Canvas, 1);
78 + end;
79 +
80   procedure TForm_TxmpReplace.btn_loadClick(Sender: TObject);
74 var
75  mem:   TMemoryStream;
76  tempd: Tdata;
81   begin
82    if opend.Execute then
83    begin
84 <    OniImage_New.LoadFromBMP(opend.FileName);
85 <    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;
84 >    OniImage_New.LoadFromFile(opend.FileName);
85 >    OniImage_New.DrawOnCanvas(image_bmppreview.Canvas, 1);
86      group_options.Enabled := True;
87    end;
88   end;
# Line 93 | Line 92 | end;
92  
93   procedure TForm_TxmpReplace.btn_replaceClick(Sender: TObject);
94   var
95 <  newsize: LongWord;
96 <  old_rawaddr, new_rawaddr: LongWord;
95 >  newsize: Integer;
96 >  old_rawaddr, new_rawaddr: Integer;
97    oldfading: Byte;
99  tempd:     Tdata;
100
98    datbyte: Word;
99 +  mem: TMemoryStream;
100 +  new_storetype: Byte;
101 +  i: Integer;
102 + const
103 +  powers: array[0..8] of Integer = (1, 2, 4, 8, 16, 32, 64, 128, 256);
104   begin
105    if filelist.ItemIndex >= 0 then
106    begin
107 <    Connection.LoadDatFilePart(fileid, $88, 1, @oldfading);
108 <    if Connection.OSisMac then
109 <      Connection.UpdateDatFilePart(fileid, $A0, 4, @old_rawaddr)
107 >    ConManager.Connection[ConnectionID].LoadDatFilePart(fileid, $88, 1, @oldfading);
108 >    if not (ConManager.Connection[ConnectionID].DataOS = DOS_WIN) then
109 >      ConManager.Connection[ConnectionID].LoadDatFilePart(fileid, $A0, 4, @old_rawaddr)
110      else
111 <      Connection.LoadDatFilePart(fileid, $9C, 4, @old_rawaddr);
111 >      ConManager.Connection[ConnectionID].LoadDatFilePart(fileid, $9C, 4, @old_rawaddr);
112  
113 <    if (OniImage_Old.Width <> OniImage_New.Width) or
114 <      (OniImage_Old.Height <> OniImage_New.Height) then
113 >    if (OniImage_New.Width[1] > 256) or (OniImage_New.Height[1] > 256) then
114 >    begin
115 >      ShowMessage('Widht and height have to be smaller than or equal to 256.');
116 >      Exit;
117 >    end;
118 >    for i := 0 to High(powers) do
119 >      if OniImage_New.Width[1] = powers[i] then
120 >        Break;
121 >    if i = Length(powers) then
122      begin
123 <      if MessageBox(Self.Handle, PChar(
124 <        'Current image and new image have different size' + CrLf +
125 <        '(Current: ' + IntToStr(OniImage_Old.Width) +
126 <        'x' + IntToStr(OniImage_Old.Height) + ' - New: ' +
127 <        IntToStr(OniImage_New.Width) + 'x' + IntToStr(OniImage_New.Height) + ')' + CrLf +
128 <        'Replace anyways?'),
129 <        PChar(filelist.Items.Strings[filelist.ItemIndex]),
130 <        MB_YESNO) = idNo then
123 >      ShowMessage('Width has to be a power of 2 (1, 2, 4, 8, 16 ...)');
124 >      Exit;
125 >    end;
126 >    for i := 0 to High(powers) do
127 >      if OniImage_New.Height[1] = powers[i] then
128 >        Break;
129 >    if i = Length(powers) then
130 >    begin
131 >      ShowMessage('Height has to be a power of 2 (1, 2, 4, 8, 16 ...)');
132 >      Exit;
133 >    end;
134 >
135 >    if (OniImage_Old.Width[1] <> OniImage_New.Width[1]) or
136 >      (OniImage_Old.Height[1] <> OniImage_New.Height[1]) then
137 >    begin
138 >      if MessageBox(Self.Handle,
139 >            PChar(
140 >              'Current image and new image have different size' + CrLf +
141 >              '(Current: ' + IntToStr(OniImage_Old.Width[1]) + 'x' +
142 >              IntToStr(OniImage_Old.Height[1]) + ' - New: ' +
143 >              IntToStr(OniImage_New.Width[1]) + 'x' +
144 >              IntToStr(OniImage_New.Height[1]) +
145 >              ')' + CrLf + 'Replace anyway?'),
146 >            PChar(filelist.Items.Strings[filelist.ItemIndex]), MB_YESNO) = idNo then
147          Exit;
148      end;
149  
150 <    if check_fading.Checked then
151 <      if not OniImage_New.GetMipMappedImage(tempd) then
152 <        if MessageBox(Self.Handle,
153 <          PChar('Can not create a MipMapped-image (probably because of a wrong dimension).' +
154 <          #13 + #10 + 'Do you want to continue without MipMapping?'), PChar('Warning'),
155 <          MB_YESNO) = ID_YES then
156 <          check_fading.Checked := False
157 <        else
158 <          Exit;
159 <
160 <    if not check_fading.Checked then
161 <      tempd := OniImage_New.GetAsData;
162 <
163 <    newsize := OniImage_New.GetImageDataSize(check_fading.Checked);
164 <
165 <    if (newsize > old_size) and (Connection.Backend = ODB_Dat) then
166 <      new_rawaddr := Connection.AppendRawFile(
167 <        Connection.OSisMac, Length(tempd), tempd)
150 >    mem := TMemoryStream.Create;
151 >
152 >    case OniImage_New.Format of
153 >      ifX1R5G5B5: new_storetype := 1;
154 >      ifA1R5G5B5: new_storetype := 2;
155 >      ifA4R4G4B4: new_storetype := 0;
156 >      ifA8R8G8B8:
157 >        begin
158 >          new_storetype := 8;
159 >          OniImage_New.Format := ifX8R8G8B8;
160 >        end;
161 >      ifX8R8G8B8: new_storetype := 8;
162 >      ifDXT1: new_storetype := 9;
163 >    else
164 >      if OniImage_New.FormatInfo.HasAlphaChannel then
165 >        ShowMessage('Loaded image has an alpha-channel.' + #13#10 +
166 >                    'Because the format is neither ARGB1555' +#13#10 +
167 >                    'nor ARGB4444 it can not be imported without conversion.' + #13#10 +
168 >                    'It is converted to RGB888, so alpha gets dropped.' + #13#10 +
169 >                    'If you need alpha you have to save your image in' + #13#10 +
170 >                    'one of the previously named formats.');  
171 >      OniImage_New.Format := ifX8R8G8B8;
172 >      new_storetype := 8;
173 >    end;
174 >
175 >    OniImage_New.SaveDataToStream(check_fading.Checked, TStream(mem));
176 >
177 >    newsize := mem.Size;
178 >    mem.Seek(0, soFromBeginning);
179 >
180 >    if (newsize > old_size) and (ConManager.Connection[ConnectionID].Backend = DB_ONI) then
181 >      new_rawaddr := ConManager.Connection[ConnectionID].AppendRawFile(
182 >        not (ConManager.Connection[ConnectionID].DataOS = DOS_WIN), mem)
183      else
184      begin
185        new_rawaddr := old_rawaddr;
186 <      Connection.UpdateRawFile(fileid, $9C, Length(tempd), tempd);
186 >      ConManager.Connection[ConnectionID].UpdateRawFile(fileid, $9C, mem);
187      end;
188  
149    datbyte := $00;
189      if check_fading.Checked then
190 <      datbyte := datbyte or $01;
191 <    Connection.UpdateDatFilePart(fileid, $88, 1, @datbyte);
190 >      oldfading := oldfading or $01
191 >    else
192 >      oldfading := oldfading and (not Byte($01));
193 >    ConManager.Connection[ConnectionID].UpdateDatFilePart(fileid, $88, 1, @datbyte);
194      datbyte := $10;
195 <    if check_transparency.Checked then
196 <      datbyte := datbyte or $04;
197 <    Connection.UpdateDatFilePart(fileid, $89, 1, @datbyte);
198 <    Connection.UpdateDatFilePart(fileid, $8C, 2, @OniImage_New.Width);
199 <    Connection.UpdateDatFilePart(fileid, $8E, 2, @OniImage_New.Height);
200 <    datbyte := $08;
201 <    Connection.UpdateDatFilePart(fileid, $90, 1, @datbyte);
202 <    if Connection.OSisMac then
203 <      Connection.UpdateDatFilePart(fileid, $A0, 4, @new_rawaddr)
195 > //    if check_transparency.Checked then
196 > //      datbyte := datbyte or $04;
197 >    ConManager.Connection[ConnectionID].UpdateDatFilePart(fileid, $89, 1, @datbyte);
198 >    datbyte := OniImage_New.Width[1];
199 >    ConManager.Connection[ConnectionID].UpdateDatFilePart(fileid, $8C, 2, @datbyte);
200 >    datbyte := OniImage_New.Height[1];
201 >    ConManager.Connection[ConnectionID].UpdateDatFilePart(fileid, $8E, 2, @datbyte);
202 >    ConManager.Connection[ConnectionID].UpdateDatFilePart(fileid, $90, 1, @new_storetype);
203 >    if not (ConManager.Connection[ConnectionID].DataOS = DOS_WIN) then
204 >      ConManager.Connection[ConnectionID].UpdateDatFilePart(fileid, $A0, 4, @new_rawaddr)
205      else
206 <      Connection.UpdateDatFilePart(fileid, $9C, 4, @new_rawaddr);
206 >      ConManager.Connection[ConnectionID].UpdateDatFilePart(fileid, $9C, 4, @new_rawaddr);
207  
208      ShowMessage('TXMP-image replaced');
209 +    Self.listClick(Self);
210    end;
211   end;
212  
# Line 187 | Line 230 | begin
230    OniImage_New := TOniImage.Create;
231    Self.AllowedExts := 'TXMP';
232    Self.OnNewFileSelected := SelectFile;
233 +  opend.Filter := saved.Filter;
234   end;
235  
236  
# Line 195 | Line 239 | end;
239   procedure TForm_TxmpReplace.btn_saveClick(Sender: TObject);
240   begin
241    if saved.Execute then
242 <    OniImage_Old.WriteToBMP(saved.FileName);
242 >    OniImage_Old.WriteToFile(saved.FileName);
243   end;
244  
245   begin

Diff Legend

Removed lines
+ Added lines
< Changed lines (old)
> Changed lines (new)