Index: oup/current/Global/OniImgClass.pas
===================================================================
--- oup/current/Global/OniImgClass.pas	(revision 199)
+++ oup/current/Global/OniImgClass.pas	(revision 204)
@@ -30,16 +30,16 @@
     constructor Create;
     procedure Free;
+
+    function GetImageSize(MipMaps: Boolean): Integer;
+
+    function LoadFromFile(filename: String): Boolean;
+    function WriteToFile(filename: String): Boolean;
+    procedure SaveDataToStream(MipMaps: Boolean; var Target: TStream);
+    procedure DrawOnCanvas(Canvas: TCanvas; Index: Integer);
+
     function Load(ConnectionID, FileID: Integer): Boolean;
-    function LoadFromPSpc(ConnectionID, FileID: Integer): Boolean;
     function LoadFromTXMP(ConnectionID, FileID: Integer): Boolean;
     function LoadFromTXMB(ConnectionID, FileID: Integer): Boolean;
-
-    procedure SaveDataToStream(MipMaps: Boolean; var Target: TStream);
-
-    function LoadFromFile(filename: String): Boolean;
-    function WriteToFile(filename: String): Boolean;
-
-    procedure DrawOnCanvas(Canvas: TCanvas; Index: Integer);
-    function GetImageSize(MipMaps: Boolean): Integer;
+    function LoadFromPSpc(ConnectionID, FileID: Integer): Boolean;
   published
   end;
@@ -49,4 +49,110 @@
 
 uses Img_DDSTypes, ImagingComponents;
+
+
+function TOniImage.GetImage(MipGen: Integer): TImageData;
+begin
+  if MipGen <= Length(FImages) then
+  begin
+    InitImage(Result);
+    CloneImage(FImages[MipGen-1], Result);
+  end;
+end;
+
+
+function TOniImage.GetWidth(MipGen: Integer): Integer;
+begin
+  if MipGen <= Length(FImages) then
+    Result := FImages[MipGen-1].Width
+  else
+    Result := -1;
+end;
+
+
+function TOniImage.GetHeight(MipGen: Integer): Integer;
+begin
+  if MipGen <= Length(FImages) then
+    Result := FImages[MipGen-1].Height
+  else
+    Result := -1;
+end;
+
+
+function TOniImage.GetImageFormat: TImageFormat;
+begin
+  if Length(FImages) > 0 then
+    Result := FImages[0].Format
+  else
+    Result := ifUnknown;
+end;
+
+procedure TOniImage.SetImageFormat(Format: TImageFormat);
+var
+  i: Integer;
+begin
+  if Length(FImages) > 0 then
+    for i := 0 to High(FImages) do
+      ConvertImage(FImages[i], Format);
+end;
+
+
+function TOniImage.pGetImageFormatInfo: TImageFormatInfo;
+begin
+  if Length(FImages) > 0 then
+    GetImageFormatInfo(FImages[0].Format, Result);
+end;
+
+
+function TOniImage.GetHasMipMaps: Boolean;
+begin
+  Result := Length(FImages) > 1;
+end;
+
+
+
+
+constructor TOniImage.Create;
+begin
+end;
+
+procedure TOniImage.Free;
+begin
+  FreeImagesInArray(FImages);
+end;
+
+
+
+
+function TOniImage.GetImageSize(MipMaps: Boolean): Integer;
+var
+  i: Integer;
+begin
+  if Length(FImages) > 0 then
+  begin
+    Result := FImages[0].Size;
+    if mipmaps then
+      for i := 1 to High(FImages) do
+        Result := Result + FImages[i].Size;
+  end
+  else
+    Result := -1;
+end;
+
+
+
+
+function TOniImage.LoadFromFile(filename: String): Boolean;
+begin
+  Result := LoadMultiImageFromFile(filename, FImages);
+  if not Result then
+    ShowMessage('Couldn''t load image file');
+end;
+
+
+function TOniImage.WriteToFile(filename: String): Boolean;
+begin
+  Result := SaveMultiImageToFile(filename, FImages);
+end;
+
 
 
@@ -71,76 +177,57 @@
 
 
-constructor TOniImage.Create;
-begin
-end;
-
-
-
-procedure TOniImage.Free;
-begin
-  FreeImagesInArray(FImages);
-end;
-
-
-
-
-function TOniImage.GetImage(MipGen: Integer): TImageData;
-begin
-  if MipGen <= Length(FImages) then
-  begin
-    InitImage(Result);
-    CloneImage(FImages[MipGen-1], Result);
-  end;
-end;
-
-
-
-function TOniImage.GetWidth(MipGen: Integer): Integer;
-begin
-  if MipGen <= Length(FImages) then
-    Result := FImages[MipGen-1].Width
-  else
-    Result := -1;
-end;
-
-
-function TOniImage.GetHeight(MipGen: Integer): Integer;
-begin
-  if MipGen <= Length(FImages) then
-    Result := FImages[MipGen-1].Height
-  else
-    Result := -1;
-end;
-
-
-function TOniImage.GetImageFormat: TImageFormat;
-begin
-  if Length(FImages) > 0 then
-    Result := FImages[0].Format
-  else
-    Result := ifUnknown;
-end;
-
-procedure TOniImage.SetImageFormat(Format: TImageFormat);
-var
+
+procedure TOniImage.SaveDataToStream(MipMaps: Boolean; var Target: TStream);
+var
+  images: TDynImageDataArray;
+  mem: TMemoryStream;
   i: Integer;
 begin
-  if Length(FImages) > 0 then
-    for i := 0 to High(FImages) do
-      ConvertImage(FImages[i], Format);
-end;
-
-
-function TOniImage.pGetImageFormatInfo: TImageFormatInfo;
-begin
-  if Length(FImages) > 0 then
-    GetImageFormatInfo(FImages[0].Format, Result);
-end;
-
-
-function TOniImage.GetHasMipMaps: Boolean;
-begin
-  Result := Length(FImages) > 1;
-end;
+  if Length(FImages) = 0 then
+    Exit;
+  if MipMaps then
+  begin
+    if Length(FImages) = 1 then
+    begin
+      if not GenerateMipMaps(FImages[0], 0, images) then
+      begin
+        ShowMessage('Could not generate MipMaps');
+        Exit;
+      end;
+    end
+    else
+    begin
+      SetLength(images, Length(FImages));
+      for i := 0 to High(FImages) do
+        CloneImage(FImages[i], images[i]);
+    end;
+    mem := TMemoryStream.Create;
+    if not SaveMultiImageToStream('dds', mem, images) then
+    begin
+      ShowMessage('Could not save images to stream');
+      Exit;
+    end;
+    FreeImagesInArray(images);
+  end
+  else
+  begin
+    mem := TMemoryStream.Create;
+    if not SaveImageToStream('dds', mem, FImages[0]) then
+    begin
+      ShowMessage('Could not save image to stream');
+      Exit;
+    end;
+  end;
+  if not Assigned(Target) then
+    Target := TMemoryStream.Create;
+
+  mem.Seek(128, soFromBeginning);
+  Target.CopyFrom(mem, mem.Size - 128);
+  mem.Free;
+  Target.Seek(0, soFromBeginning);
+end;
+
+
+
 
 
@@ -163,160 +250,4 @@
 
 
-function TOniImage.LoadFromPSpc(ConnectionID, FileID: Integer): Boolean;
-type
-  TPoint = packed record
-    X, Y: Word;
-  end;
-
-  TPSpc = packed record
-    p1:   array[0..8] of TPoint;
-    p2:   array[0..8] of TPoint;
-    TXMP: Integer;
-  end;
-
-  TPart = packed record
-    x_txmp, y_txmp: Word;
-    x_pspc, y_pspc: Word;
-    w, h:    Word;
-    imgdata: TImageData;
-    used:    Boolean;
-  end;
-const
-  PartMatch: array[0..8] of Byte = (0, 3, 6, 1, 4, 7, 2, 5, 8);
-  stretch_x: Integer = 1;
-  stretch_y: Integer = 1;
-var
-  x, y: Word;
-  i: Integer;
-
-  PSpc:     TPSpc;
-  txmpimg:  TOniImage;
-//  txmpdata: TByteData;
-
-  parts:    array[0..8] of TPart;
-  part:     Byte;
-  cols:     array[0..2] of Word;
-  rows:     array[0..2] of Word;
-  col, row: Byte;
-
-  pspcimage: TImageData;
-begin
-  ConManager.Connection[ConnectionID].LoadDatFilePart(fileid, $08, SizeOf(PSpc), @PSpc);
-  PSpc.TXMP := PSpc.TXMP div 256;
-  if PSpc.TXMP = 0 then
-  begin
-    Result := False;
-    Exit;
-  end;
-  txmpimg := TOniImage.Create;
-  txmpimg.Load(ConnectionID, PSpc.TXMP);
-  CloneImage(txmpimg.Image[1], pspcimage);
-  txmpimg.Free;
-
-  with pspc do
-  begin
-    for i := 0 to 2 do
-    begin
-      cols[i] := 0;
-      rows[i] := 0;
-    end;
-    for i := 0 to 8 do
-    begin
-      part := PartMatch[i];
-      col  := i div 3;
-      row  := i mod 3;
-      if (p2[i].X > 0) or (p2[i].Y > 0) then
-      begin
-        parts[part].x_txmp := p1[i].X;// - 1;
-        parts[part].y_txmp := p1[i].Y;// - 1;
-        parts[part].x_pspc := 0;
-        if col > 0 then
-          for x := 0 to col - 1 do
-            Inc(parts[part].x_pspc, cols[x]);
-        parts[part].y_pspc := 0;
-        if row > 0 then
-          for y := 0 to row - 1 do
-            Inc(parts[part].y_pspc, rows[y]);
-        parts[part].w := Max(p2[i].X - p1[i].X, 1);// + 1;
-        parts[part].h := Max(p2[i].Y - p1[i].Y, 1);// + 1;
-        parts[part].used := True;
-        cols[col] := parts[part].w;
-        rows[row] := parts[part].h;
-
-        NewImage(parts[part].w, parts[part].h, pspcimage.Format, parts[part].imgdata);
-
-        CopyRect(pspcimage,
-            parts[part].x_txmp, parts[part].y_txmp, parts[part].w, parts[part].h,
-            parts[part].imgdata, 0, 0);
-      end
-      else
-      begin
-        parts[part].used := False;
-      end;
-    end;
-
-  end;
-
-  for i := 0 to 8 do
-  begin
-    if parts[i].used then
-    begin
-//      SaveImageToFile('M:\' + IntToStr(i) + '.bmp', parts[i].imgdata);
-    end;
-  end;
-
-  SetLength(FImages, 1);
-  x := cols[0] + cols[1] * stretch_x + cols[2];
-  y := rows[0] + rows[1] * stretch_y + rows[2];
-
-  NewImage(x, y, pspcimage.Format, FImages[0]);
-
-  x := 0;
-  for col := 0 to 2 do
-  begin
-    y := 0;
-    for row := 0 to 2 do
-    begin
-      part := row*3 + col;
-      if parts[part].used then
-      begin
-        if (row = 1) and (col = 1) then
-          StretchRect(parts[part].imgdata, 0, 0, parts[part].w, parts[part].h,
-              FImages[0], x, y, parts[part].w * stretch_x, parts[part].h * stretch_y, rfNearest)
-        else
-        if (row = 1) then
-          StretchRect(parts[part].imgdata, 0, 0, parts[part].w, parts[part].h,
-              FImages[0], x, y, parts[part].w, parts[part].h * stretch_y, rfNearest)
-        else
-        if (col = 1) then
-          StretchRect(parts[part].imgdata, 0, 0, parts[part].w, parts[part].h,
-              FImages[0], x, y, parts[part].w * stretch_x, parts[part].h, rfNearest)
-        else
-          CopyRect(parts[part].imgdata, 0, 0, parts[part].w, parts[part].h,
-              FImages[0], x, y );
-        if row = 1 then
-          y := y + parts[part].h * stretch_y
-        else
-          y := y + parts[part].h;
-      end;
-    end;
-    if cols[col] > 0 then
-    begin
-      if (col = 1) then
-        x := x + parts[part].w * stretch_x
-      else
-        x := x + parts[part].w;
-    end;
-  end;
-
-  FreeImage(pspcimage);
-  for i := 0 to 8 do
-    if parts[i].used then
-      FreeImage(parts[i].imgdata);
-end;
-
-
-
-
 function TOniImage.LoadFromTXMP(ConnectionID, FileID: Integer): Boolean;
 var
@@ -331,5 +262,5 @@
   _depth: Byte;
 begin
-  Result := True;
+  Result := False;
   ConManager.Connection[ConnectionID].LoadDatFilePart(fileid, $8C, SizeOf(_width), @_width);
   ConManager.Connection[ConnectionID].LoadDatFilePart(fileid, $8E, SizeOf(_height), @_height);
@@ -445,29 +376,14 @@
     ConManager.Connection[ConnectionID].LoadRawFile(fileid, $A0, TStream(data));
 
-//  data.Seek(0, soFromBeginning);
-//  data.SaveToFile('m:\test.txmp');
-
   data.Seek(0, soFromBeginning);
-  result := LoadMultiImageFromStream(data, FImages);
+  Result := LoadMultiImageFromStream(data, FImages);
   data.Free;
-{
-  if result then
-  begin
-    for i := 0 to High(FImages) do
-    begin
-      data := TMemoryStream.Create;
-      data.Write(FImages[i].Bits^, FImages[i].Size);
-      data.Seek(0, soFromBeginning);
-      data.SaveToFile('m:\test.txmp.'+IntToStr(i));
-      data.Free;
-    end;
-  end;
-}
+
   if not result then
   begin
     ShowMessage('Error while loading file' + #13#10 + DetermineStreamFormat(data));
-//    data.SaveToFile('m:\prob.dds');
-  end;
-end;
+  end;
+end;
+
 
 
@@ -485,4 +401,5 @@
   width, height: Word;
 begin
+  Result := False;
   ConManager.Connection[ConnectionID].LoadDatFilePart(fileid, $10, SizeOf(width), @width);
   ConManager.Connection[ConnectionID].LoadDatFilePart(fileid, $12, SizeOf(height), @height);
@@ -519,89 +436,165 @@
   for i := 0 to linkcount - 1 do
     images[i].Free;
-end;
-
-
-
-procedure TOniImage.SaveDataToStream(MipMaps: Boolean; var Target: TStream);
-var
-  images: TDynImageDataArray;
-  mem: TMemoryStream;
+  Result := True;
+end;
+
+
+
+
+
+function TOniImage.LoadFromPSpc(ConnectionID, FileID: Integer): Boolean;
+type
+  TPoint = packed record
+    X, Y: Word;
+  end;
+
+  TPSpc = packed record
+    p1:   array[0..8] of TPoint;
+    p2:   array[0..8] of TPoint;
+    TXMP: Integer;
+  end;
+
+  TPart = packed record
+    x_txmp, y_txmp: Word;
+    x_pspc, y_pspc: Word;
+    w, h:    Word;
+    imgdata: TImageData;
+    used:    Boolean;
+  end;
+const
+  PartMatch: array[0..8] of Byte = (0, 3, 6, 1, 4, 7, 2, 5, 8);
+  stretch_x: Integer = 1;
+  stretch_y: Integer = 1;
+var
+  x, y: Word;
   i: Integer;
-begin
-  if Length(FImages) = 0 then
+
+  PSpc:     TPSpc;
+  txmpimg:  TOniImage;
+
+  parts:    array[0..8] of TPart;
+  part:     Byte;
+  cols:     array[0..2] of Word;
+  rows:     array[0..2] of Word;
+  col, row: Byte;
+
+  pspcimage: TImageData;
+begin
+  ConManager.Connection[ConnectionID].LoadDatFilePart(fileid, $08, SizeOf(PSpc), @PSpc);
+  PSpc.TXMP := PSpc.TXMP div 256;
+  if PSpc.TXMP = 0 then
+  begin
+    Result := False;
     Exit;
-  if MipMaps then
-  begin
-    if Length(FImages) = 1 then
-    begin
-      if not GenerateMipMaps(FImages[0], 0, images) then
+  end;
+  txmpimg := TOniImage.Create;
+  txmpimg.Load(ConnectionID, PSpc.TXMP);
+  CloneImage(txmpimg.Image[1], pspcimage);
+  txmpimg.Free;
+
+  Result := False;
+
+  with pspc do
+  begin
+    for i := 0 to 2 do
+    begin
+      cols[i] := 0;
+      rows[i] := 0;
+    end;
+    for i := 0 to 8 do
+    begin
+      part := PartMatch[i];
+      col  := i div 3;
+      row  := i mod 3;
+      if (p2[i].X > 0) or (p2[i].Y > 0) then
       begin
-        ShowMessage('Could not generate MipMaps');
-        Exit;
+        parts[part].x_txmp := p1[i].X;// - 1;
+        parts[part].y_txmp := p1[i].Y;// - 1;
+        parts[part].x_pspc := 0;
+        if col > 0 then
+          for x := 0 to col - 1 do
+            Inc(parts[part].x_pspc, cols[x]);
+        parts[part].y_pspc := 0;
+        if row > 0 then
+          for y := 0 to row - 1 do
+            Inc(parts[part].y_pspc, rows[y]);
+        parts[part].w := Max(p2[i].X - p1[i].X, 1);// + 1;
+        parts[part].h := Max(p2[i].Y - p1[i].Y, 1);// + 1;
+        parts[part].used := True;
+        cols[col] := parts[part].w;
+        rows[row] := parts[part].h;
+
+        NewImage(parts[part].w, parts[part].h, pspcimage.Format, parts[part].imgdata);
+
+        CopyRect(pspcimage,
+            parts[part].x_txmp, parts[part].y_txmp, parts[part].w, parts[part].h,
+            parts[part].imgdata, 0, 0);
+      end
+      else
+      begin
+        parts[part].used := False;
       end;
-    end
-    else
-    begin
-      SetLength(images, Length(FImages));
-      for i := 0 to High(FImages) do
-        CloneImage(FImages[i], images[i]);
-    end;
-    mem := TMemoryStream.Create;
-    if not SaveMultiImageToStream('dds', mem, images) then
-    begin
-      ShowMessage('Could not save images to stream');
-      Exit;
-    end;
-    FreeImagesInArray(images);
-  end
-  else
-  begin
-    mem := TMemoryStream.Create;
-    if not SaveImageToStream('dds', mem, FImages[0]) then
-    begin
-      ShowMessage('Could not save image to stream');
-      Exit;
-    end;
-  end;
-  if not Assigned(Target) then
-    Target := TMemoryStream.Create;
-
-//  mem.Seek(0, soFromBeginning);
-//  mem.SaveToFile('m:\dds.dds');
-
-  mem.Seek(128, soFromBeginning);
-  Target.CopyFrom(mem, mem.Size - 128);
-  mem.Free;
-  Target.Seek(0, soFromBeginning);
-end;
-
-
-function TOniImage.LoadFromFile(filename: String): Boolean;
-begin
-  if not LoadMultiImageFromFile(filename, FImages) then
-    ShowMessage('Couldn''t load image file');
-end;
-
-
-function TOniImage.WriteToFile(filename: String): Boolean;
-begin
-  SaveMultiImageToFile(filename, FImages);
-end;
-
-
-
-function TOniImage.GetImageSize(MipMaps: Boolean): Integer;
-var
-  i: Integer;
-begin
-  if Length(FImages) > 0 then
-  begin
-    Result := FImages[0].Size;
-    if mipmaps then
-      for i := 1 to High(FImages) do
-        Result := Result + FImages[i].Size;
-  end
-  else
-    Result := -1;
+    end;
+
+  end;
+
+  for i := 0 to 8 do
+  begin
+    if parts[i].used then
+    begin
+//      SaveImageToFile('M:\' + IntToStr(i) + '.bmp', parts[i].imgdata);
+    end;
+  end;
+
+  SetLength(FImages, 1);
+  x := cols[0] + cols[1] * stretch_x + cols[2];
+  y := rows[0] + rows[1] * stretch_y + rows[2];
+
+  NewImage(x, y, pspcimage.Format, FImages[0]);
+
+  x := 0;
+  for col := 0 to 2 do
+  begin
+    y := 0;
+    for row := 0 to 2 do
+    begin
+      part := row*3 + col;
+      if parts[part].used then
+      begin
+        if (row = 1) and (col = 1) then
+          StretchRect(parts[part].imgdata, 0, 0, parts[part].w, parts[part].h,
+              FImages[0], x, y, parts[part].w * stretch_x, parts[part].h * stretch_y, rfNearest)
+        else
+        if (row = 1) then
+          StretchRect(parts[part].imgdata, 0, 0, parts[part].w, parts[part].h,
+              FImages[0], x, y, parts[part].w, parts[part].h * stretch_y, rfNearest)
+        else
+        if (col = 1) then
+          StretchRect(parts[part].imgdata, 0, 0, parts[part].w, parts[part].h,
+              FImages[0], x, y, parts[part].w * stretch_x, parts[part].h, rfNearest)
+        else
+          CopyRect(parts[part].imgdata, 0, 0, parts[part].w, parts[part].h,
+              FImages[0], x, y );
+        if row = 1 then
+          y := y + parts[part].h * stretch_y
+        else
+          y := y + parts[part].h;
+      end;
+    end;
+    if cols[col] > 0 then
+    begin
+      if (col = 1) then
+        x := x + parts[part].w * stretch_x
+      else
+        x := x + parts[part].w;
+    end;
+  end;
+
+  FreeImage(pspcimage);
+  for i := 0 to 8 do
+    if parts[i].used then
+      FreeImage(parts[i].imgdata);
+
+  Result := True;
 end;
 
