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 97 by alloc, Mon Jan 22 23:05:45 2007 UTC vs.
oup/current/Tools/TxmpReplace.pas (file contents), Revision 201 by alloc, Sat May 26 21:36:23 2007 UTC

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

Diff Legend

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