Changeset 204 for oup/current/Global


Ignore:
Timestamp:
May 27, 2007, 10:03:41 PM (18 years ago)
Author:
alloc
Message:
 
Location:
oup/current/Global
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • oup/current/Global/DatStructureLoader.pas

    r113 r204  
    7575    10000..65535:
    7676      Result := datatype - 10000;
     77  else
     78    Result := 0;
    7779  end;
    7880end;
  • oup/current/Global/Data.pas

    r138 r204  
    44
    55const
    6   Version:   String    = 'v0.34a';
     6  Version:   String    = 'v0.34b';
    77  DBVersion: String    = '0.4';
    88  CrLf:      String[2] = #13 + #10;
  • oup/current/Global/OniImgClass.pas

    r199 r204  
    3030    constructor Create;
    3131    procedure Free;
     32
     33    function GetImageSize(MipMaps: Boolean): Integer;
     34
     35    function LoadFromFile(filename: String): Boolean;
     36    function WriteToFile(filename: String): Boolean;
     37    procedure SaveDataToStream(MipMaps: Boolean; var Target: TStream);
     38    procedure DrawOnCanvas(Canvas: TCanvas; Index: Integer);
     39
    3240    function Load(ConnectionID, FileID: Integer): Boolean;
    33     function LoadFromPSpc(ConnectionID, FileID: Integer): Boolean;
    3441    function LoadFromTXMP(ConnectionID, FileID: Integer): Boolean;
    3542    function LoadFromTXMB(ConnectionID, FileID: Integer): Boolean;
    36 
    37     procedure SaveDataToStream(MipMaps: Boolean; var Target: TStream);
    38 
    39     function LoadFromFile(filename: String): Boolean;
    40     function WriteToFile(filename: String): Boolean;
    41 
    42     procedure DrawOnCanvas(Canvas: TCanvas; Index: Integer);
    43     function GetImageSize(MipMaps: Boolean): Integer;
     43    function LoadFromPSpc(ConnectionID, FileID: Integer): Boolean;
    4444  published
    4545  end;
     
    4949
    5050uses Img_DDSTypes, ImagingComponents;
     51
     52
     53function TOniImage.GetImage(MipGen: Integer): TImageData;
     54begin
     55  if MipGen <= Length(FImages) then
     56  begin
     57    InitImage(Result);
     58    CloneImage(FImages[MipGen-1], Result);
     59  end;
     60end;
     61
     62
     63function TOniImage.GetWidth(MipGen: Integer): Integer;
     64begin
     65  if MipGen <= Length(FImages) then
     66    Result := FImages[MipGen-1].Width
     67  else
     68    Result := -1;
     69end;
     70
     71
     72function TOniImage.GetHeight(MipGen: Integer): Integer;
     73begin
     74  if MipGen <= Length(FImages) then
     75    Result := FImages[MipGen-1].Height
     76  else
     77    Result := -1;
     78end;
     79
     80
     81function TOniImage.GetImageFormat: TImageFormat;
     82begin
     83  if Length(FImages) > 0 then
     84    Result := FImages[0].Format
     85  else
     86    Result := ifUnknown;
     87end;
     88
     89procedure TOniImage.SetImageFormat(Format: TImageFormat);
     90var
     91  i: Integer;
     92begin
     93  if Length(FImages) > 0 then
     94    for i := 0 to High(FImages) do
     95      ConvertImage(FImages[i], Format);
     96end;
     97
     98
     99function TOniImage.pGetImageFormatInfo: TImageFormatInfo;
     100begin
     101  if Length(FImages) > 0 then
     102    GetImageFormatInfo(FImages[0].Format, Result);
     103end;
     104
     105
     106function TOniImage.GetHasMipMaps: Boolean;
     107begin
     108  Result := Length(FImages) > 1;
     109end;
     110
     111
     112
     113
     114constructor TOniImage.Create;
     115begin
     116end;
     117
     118procedure TOniImage.Free;
     119begin
     120  FreeImagesInArray(FImages);
     121end;
     122
     123
     124
     125
     126function TOniImage.GetImageSize(MipMaps: Boolean): Integer;
     127var
     128  i: Integer;
     129begin
     130  if Length(FImages) > 0 then
     131  begin
     132    Result := FImages[0].Size;
     133    if mipmaps then
     134      for i := 1 to High(FImages) do
     135        Result := Result + FImages[i].Size;
     136  end
     137  else
     138    Result := -1;
     139end;
     140
     141
     142
     143
     144function TOniImage.LoadFromFile(filename: String): Boolean;
     145begin
     146  Result := LoadMultiImageFromFile(filename, FImages);
     147  if not Result then
     148    ShowMessage('Couldn''t load image file');
     149end;
     150
     151
     152function TOniImage.WriteToFile(filename: String): Boolean;
     153begin
     154  Result := SaveMultiImageToFile(filename, FImages);
     155end;
     156
    51157
    52158
     
    71177
    72178
    73 constructor TOniImage.Create;
    74 begin
    75 end;
    76 
    77 
    78 
    79 procedure TOniImage.Free;
    80 begin
    81   FreeImagesInArray(FImages);
    82 end;
    83 
    84 
    85 
    86 
    87 function TOniImage.GetImage(MipGen: Integer): TImageData;
    88 begin
    89   if MipGen <= Length(FImages) then
    90   begin
    91     InitImage(Result);
    92     CloneImage(FImages[MipGen-1], Result);
    93   end;
    94 end;
    95 
    96 
    97 
    98 function TOniImage.GetWidth(MipGen: Integer): Integer;
    99 begin
    100   if MipGen <= Length(FImages) then
    101     Result := FImages[MipGen-1].Width
    102   else
    103     Result := -1;
    104 end;
    105 
    106 
    107 function TOniImage.GetHeight(MipGen: Integer): Integer;
    108 begin
    109   if MipGen <= Length(FImages) then
    110     Result := FImages[MipGen-1].Height
    111   else
    112     Result := -1;
    113 end;
    114 
    115 
    116 function TOniImage.GetImageFormat: TImageFormat;
    117 begin
    118   if Length(FImages) > 0 then
    119     Result := FImages[0].Format
    120   else
    121     Result := ifUnknown;
    122 end;
    123 
    124 procedure TOniImage.SetImageFormat(Format: TImageFormat);
    125 var
     179
     180procedure TOniImage.SaveDataToStream(MipMaps: Boolean; var Target: TStream);
     181var
     182  images: TDynImageDataArray;
     183  mem: TMemoryStream;
    126184  i: Integer;
    127185begin
    128   if Length(FImages) > 0 then
    129     for i := 0 to High(FImages) do
    130       ConvertImage(FImages[i], Format);
    131 end;
    132 
    133 
    134 function TOniImage.pGetImageFormatInfo: TImageFormatInfo;
    135 begin
    136   if Length(FImages) > 0 then
    137     GetImageFormatInfo(FImages[0].Format, Result);
    138 end;
    139 
    140 
    141 function TOniImage.GetHasMipMaps: Boolean;
    142 begin
    143   Result := Length(FImages) > 1;
    144 end;
     186  if Length(FImages) = 0 then
     187    Exit;
     188  if MipMaps then
     189  begin
     190    if Length(FImages) = 1 then
     191    begin
     192      if not GenerateMipMaps(FImages[0], 0, images) then
     193      begin
     194        ShowMessage('Could not generate MipMaps');
     195        Exit;
     196      end;
     197    end
     198    else
     199    begin
     200      SetLength(images, Length(FImages));
     201      for i := 0 to High(FImages) do
     202        CloneImage(FImages[i], images[i]);
     203    end;
     204    mem := TMemoryStream.Create;
     205    if not SaveMultiImageToStream('dds', mem, images) then
     206    begin
     207      ShowMessage('Could not save images to stream');
     208      Exit;
     209    end;
     210    FreeImagesInArray(images);
     211  end
     212  else
     213  begin
     214    mem := TMemoryStream.Create;
     215    if not SaveImageToStream('dds', mem, FImages[0]) then
     216    begin
     217      ShowMessage('Could not save image to stream');
     218      Exit;
     219    end;
     220  end;
     221  if not Assigned(Target) then
     222    Target := TMemoryStream.Create;
     223
     224  mem.Seek(128, soFromBeginning);
     225  Target.CopyFrom(mem, mem.Size - 128);
     226  mem.Free;
     227  Target.Seek(0, soFromBeginning);
     228end;
     229
     230
     231
    145232
    146233
     
    163250
    164251
    165 function TOniImage.LoadFromPSpc(ConnectionID, FileID: Integer): Boolean;
    166 type
    167   TPoint = packed record
    168     X, Y: Word;
    169   end;
    170 
    171   TPSpc = packed record
    172     p1:   array[0..8] of TPoint;
    173     p2:   array[0..8] of TPoint;
    174     TXMP: Integer;
    175   end;
    176 
    177   TPart = packed record
    178     x_txmp, y_txmp: Word;
    179     x_pspc, y_pspc: Word;
    180     w, h:    Word;
    181     imgdata: TImageData;
    182     used:    Boolean;
    183   end;
    184 const
    185   PartMatch: array[0..8] of Byte = (0, 3, 6, 1, 4, 7, 2, 5, 8);
    186   stretch_x: Integer = 1;
    187   stretch_y: Integer = 1;
    188 var
    189   x, y: Word;
    190   i: Integer;
    191 
    192   PSpc:     TPSpc;
    193   txmpimg:  TOniImage;
    194 //  txmpdata: TByteData;
    195 
    196   parts:    array[0..8] of TPart;
    197   part:     Byte;
    198   cols:     array[0..2] of Word;
    199   rows:     array[0..2] of Word;
    200   col, row: Byte;
    201 
    202   pspcimage: TImageData;
    203 begin
    204   ConManager.Connection[ConnectionID].LoadDatFilePart(fileid, $08, SizeOf(PSpc), @PSpc);
    205   PSpc.TXMP := PSpc.TXMP div 256;
    206   if PSpc.TXMP = 0 then
    207   begin
    208     Result := False;
    209     Exit;
    210   end;
    211   txmpimg := TOniImage.Create;
    212   txmpimg.Load(ConnectionID, PSpc.TXMP);
    213   CloneImage(txmpimg.Image[1], pspcimage);
    214   txmpimg.Free;
    215 
    216   with pspc do
    217   begin
    218     for i := 0 to 2 do
    219     begin
    220       cols[i] := 0;
    221       rows[i] := 0;
    222     end;
    223     for i := 0 to 8 do
    224     begin
    225       part := PartMatch[i];
    226       col  := i div 3;
    227       row  := i mod 3;
    228       if (p2[i].X > 0) or (p2[i].Y > 0) then
    229       begin
    230         parts[part].x_txmp := p1[i].X;// - 1;
    231         parts[part].y_txmp := p1[i].Y;// - 1;
    232         parts[part].x_pspc := 0;
    233         if col > 0 then
    234           for x := 0 to col - 1 do
    235             Inc(parts[part].x_pspc, cols[x]);
    236         parts[part].y_pspc := 0;
    237         if row > 0 then
    238           for y := 0 to row - 1 do
    239             Inc(parts[part].y_pspc, rows[y]);
    240         parts[part].w := Max(p2[i].X - p1[i].X, 1);// + 1;
    241         parts[part].h := Max(p2[i].Y - p1[i].Y, 1);// + 1;
    242         parts[part].used := True;
    243         cols[col] := parts[part].w;
    244         rows[row] := parts[part].h;
    245 
    246         NewImage(parts[part].w, parts[part].h, pspcimage.Format, parts[part].imgdata);
    247 
    248         CopyRect(pspcimage,
    249             parts[part].x_txmp, parts[part].y_txmp, parts[part].w, parts[part].h,
    250             parts[part].imgdata, 0, 0);
    251       end
    252       else
    253       begin
    254         parts[part].used := False;
    255       end;
    256     end;
    257 
    258   end;
    259 
    260   for i := 0 to 8 do
    261   begin
    262     if parts[i].used then
    263     begin
    264 //      SaveImageToFile('M:\' + IntToStr(i) + '.bmp', parts[i].imgdata);
    265     end;
    266   end;
    267 
    268   SetLength(FImages, 1);
    269   x := cols[0] + cols[1] * stretch_x + cols[2];
    270   y := rows[0] + rows[1] * stretch_y + rows[2];
    271 
    272   NewImage(x, y, pspcimage.Format, FImages[0]);
    273 
    274   x := 0;
    275   for col := 0 to 2 do
    276   begin
    277     y := 0;
    278     for row := 0 to 2 do
    279     begin
    280       part := row*3 + col;
    281       if parts[part].used then
    282       begin
    283         if (row = 1) and (col = 1) then
    284           StretchRect(parts[part].imgdata, 0, 0, parts[part].w, parts[part].h,
    285               FImages[0], x, y, parts[part].w * stretch_x, parts[part].h * stretch_y, rfNearest)
    286         else
    287         if (row = 1) then
    288           StretchRect(parts[part].imgdata, 0, 0, parts[part].w, parts[part].h,
    289               FImages[0], x, y, parts[part].w, parts[part].h * stretch_y, rfNearest)
    290         else
    291         if (col = 1) then
    292           StretchRect(parts[part].imgdata, 0, 0, parts[part].w, parts[part].h,
    293               FImages[0], x, y, parts[part].w * stretch_x, parts[part].h, rfNearest)
    294         else
    295           CopyRect(parts[part].imgdata, 0, 0, parts[part].w, parts[part].h,
    296               FImages[0], x, y );
    297         if row = 1 then
    298           y := y + parts[part].h * stretch_y
    299         else
    300           y := y + parts[part].h;
    301       end;
    302     end;
    303     if cols[col] > 0 then
    304     begin
    305       if (col = 1) then
    306         x := x + parts[part].w * stretch_x
    307       else
    308         x := x + parts[part].w;
    309     end;
    310   end;
    311 
    312   FreeImage(pspcimage);
    313   for i := 0 to 8 do
    314     if parts[i].used then
    315       FreeImage(parts[i].imgdata);
    316 end;
    317 
    318 
    319 
    320 
    321252function TOniImage.LoadFromTXMP(ConnectionID, FileID: Integer): Boolean;
    322253var
     
    331262  _depth: Byte;
    332263begin
    333   Result := True;
     264  Result := False;
    334265  ConManager.Connection[ConnectionID].LoadDatFilePart(fileid, $8C, SizeOf(_width), @_width);
    335266  ConManager.Connection[ConnectionID].LoadDatFilePart(fileid, $8E, SizeOf(_height), @_height);
     
    445376    ConManager.Connection[ConnectionID].LoadRawFile(fileid, $A0, TStream(data));
    446377
    447 //  data.Seek(0, soFromBeginning);
    448 //  data.SaveToFile('m:\test.txmp');
    449 
    450378  data.Seek(0, soFromBeginning);
    451   result := LoadMultiImageFromStream(data, FImages);
     379  Result := LoadMultiImageFromStream(data, FImages);
    452380  data.Free;
    453 {
    454   if result then
    455   begin
    456     for i := 0 to High(FImages) do
    457     begin
    458       data := TMemoryStream.Create;
    459       data.Write(FImages[i].Bits^, FImages[i].Size);
    460       data.Seek(0, soFromBeginning);
    461       data.SaveToFile('m:\test.txmp.'+IntToStr(i));
    462       data.Free;
    463     end;
    464   end;
    465 }
     381
    466382  if not result then
    467383  begin
    468384    ShowMessage('Error while loading file' + #13#10 + DetermineStreamFormat(data));
    469 //    data.SaveToFile('m:\prob.dds');
    470   end;
    471 end;
     385  end;
     386end;
     387
    472388
    473389
     
    485401  width, height: Word;
    486402begin
     403  Result := False;
    487404  ConManager.Connection[ConnectionID].LoadDatFilePart(fileid, $10, SizeOf(width), @width);
    488405  ConManager.Connection[ConnectionID].LoadDatFilePart(fileid, $12, SizeOf(height), @height);
     
    519436  for i := 0 to linkcount - 1 do
    520437    images[i].Free;
    521 end;
    522 
    523 
    524 
    525 procedure TOniImage.SaveDataToStream(MipMaps: Boolean; var Target: TStream);
    526 var
    527   images: TDynImageDataArray;
    528   mem: TMemoryStream;
     438  Result := True;
     439end;
     440
     441
     442
     443
     444
     445function TOniImage.LoadFromPSpc(ConnectionID, FileID: Integer): Boolean;
     446type
     447  TPoint = packed record
     448    X, Y: Word;
     449  end;
     450
     451  TPSpc = packed record
     452    p1:   array[0..8] of TPoint;
     453    p2:   array[0..8] of TPoint;
     454    TXMP: Integer;
     455  end;
     456
     457  TPart = packed record
     458    x_txmp, y_txmp: Word;
     459    x_pspc, y_pspc: Word;
     460    w, h:    Word;
     461    imgdata: TImageData;
     462    used:    Boolean;
     463  end;
     464const
     465  PartMatch: array[0..8] of Byte = (0, 3, 6, 1, 4, 7, 2, 5, 8);
     466  stretch_x: Integer = 1;
     467  stretch_y: Integer = 1;
     468var
     469  x, y: Word;
    529470  i: Integer;
    530 begin
    531   if Length(FImages) = 0 then
     471
     472  PSpc:     TPSpc;
     473  txmpimg:  TOniImage;
     474
     475  parts:    array[0..8] of TPart;
     476  part:     Byte;
     477  cols:     array[0..2] of Word;
     478  rows:     array[0..2] of Word;
     479  col, row: Byte;
     480
     481  pspcimage: TImageData;
     482begin
     483  ConManager.Connection[ConnectionID].LoadDatFilePart(fileid, $08, SizeOf(PSpc), @PSpc);
     484  PSpc.TXMP := PSpc.TXMP div 256;
     485  if PSpc.TXMP = 0 then
     486  begin
     487    Result := False;
    532488    Exit;
    533   if MipMaps then
    534   begin
    535     if Length(FImages) = 1 then
    536     begin
    537       if not GenerateMipMaps(FImages[0], 0, images) then
     489  end;
     490  txmpimg := TOniImage.Create;
     491  txmpimg.Load(ConnectionID, PSpc.TXMP);
     492  CloneImage(txmpimg.Image[1], pspcimage);
     493  txmpimg.Free;
     494
     495  Result := False;
     496
     497  with pspc do
     498  begin
     499    for i := 0 to 2 do
     500    begin
     501      cols[i] := 0;
     502      rows[i] := 0;
     503    end;
     504    for i := 0 to 8 do
     505    begin
     506      part := PartMatch[i];
     507      col  := i div 3;
     508      row  := i mod 3;
     509      if (p2[i].X > 0) or (p2[i].Y > 0) then
    538510      begin
    539         ShowMessage('Could not generate MipMaps');
    540         Exit;
     511        parts[part].x_txmp := p1[i].X;// - 1;
     512        parts[part].y_txmp := p1[i].Y;// - 1;
     513        parts[part].x_pspc := 0;
     514        if col > 0 then
     515          for x := 0 to col - 1 do
     516            Inc(parts[part].x_pspc, cols[x]);
     517        parts[part].y_pspc := 0;
     518        if row > 0 then
     519          for y := 0 to row - 1 do
     520            Inc(parts[part].y_pspc, rows[y]);
     521        parts[part].w := Max(p2[i].X - p1[i].X, 1);// + 1;
     522        parts[part].h := Max(p2[i].Y - p1[i].Y, 1);// + 1;
     523        parts[part].used := True;
     524        cols[col] := parts[part].w;
     525        rows[row] := parts[part].h;
     526
     527        NewImage(parts[part].w, parts[part].h, pspcimage.Format, parts[part].imgdata);
     528
     529        CopyRect(pspcimage,
     530            parts[part].x_txmp, parts[part].y_txmp, parts[part].w, parts[part].h,
     531            parts[part].imgdata, 0, 0);
     532      end
     533      else
     534      begin
     535        parts[part].used := False;
    541536      end;
    542     end
    543     else
    544     begin
    545       SetLength(images, Length(FImages));
    546       for i := 0 to High(FImages) do
    547         CloneImage(FImages[i], images[i]);
    548     end;
    549     mem := TMemoryStream.Create;
    550     if not SaveMultiImageToStream('dds', mem, images) then
    551     begin
    552       ShowMessage('Could not save images to stream');
    553       Exit;
    554     end;
    555     FreeImagesInArray(images);
    556   end
    557   else
    558   begin
    559     mem := TMemoryStream.Create;
    560     if not SaveImageToStream('dds', mem, FImages[0]) then
    561     begin
    562       ShowMessage('Could not save image to stream');
    563       Exit;
    564     end;
    565   end;
    566   if not Assigned(Target) then
    567     Target := TMemoryStream.Create;
    568 
    569 //  mem.Seek(0, soFromBeginning);
    570 //  mem.SaveToFile('m:\dds.dds');
    571 
    572   mem.Seek(128, soFromBeginning);
    573   Target.CopyFrom(mem, mem.Size - 128);
    574   mem.Free;
    575   Target.Seek(0, soFromBeginning);
    576 end;
    577 
    578 
    579 function TOniImage.LoadFromFile(filename: String): Boolean;
    580 begin
    581   if not LoadMultiImageFromFile(filename, FImages) then
    582     ShowMessage('Couldn''t load image file');
    583 end;
    584 
    585 
    586 function TOniImage.WriteToFile(filename: String): Boolean;
    587 begin
    588   SaveMultiImageToFile(filename, FImages);
    589 end;
    590 
    591 
    592 
    593 function TOniImage.GetImageSize(MipMaps: Boolean): Integer;
    594 var
    595   i: Integer;
    596 begin
    597   if Length(FImages) > 0 then
    598   begin
    599     Result := FImages[0].Size;
    600     if mipmaps then
    601       for i := 1 to High(FImages) do
    602         Result := Result + FImages[i].Size;
    603   end
    604   else
    605     Result := -1;
     537    end;
     538
     539  end;
     540
     541  for i := 0 to 8 do
     542  begin
     543    if parts[i].used then
     544    begin
     545//      SaveImageToFile('M:\' + IntToStr(i) + '.bmp', parts[i].imgdata);
     546    end;
     547  end;
     548
     549  SetLength(FImages, 1);
     550  x := cols[0] + cols[1] * stretch_x + cols[2];
     551  y := rows[0] + rows[1] * stretch_y + rows[2];
     552
     553  NewImage(x, y, pspcimage.Format, FImages[0]);
     554
     555  x := 0;
     556  for col := 0 to 2 do
     557  begin
     558    y := 0;
     559    for row := 0 to 2 do
     560    begin
     561      part := row*3 + col;
     562      if parts[part].used then
     563      begin
     564        if (row = 1) and (col = 1) then
     565          StretchRect(parts[part].imgdata, 0, 0, parts[part].w, parts[part].h,
     566              FImages[0], x, y, parts[part].w * stretch_x, parts[part].h * stretch_y, rfNearest)
     567        else
     568        if (row = 1) then
     569          StretchRect(parts[part].imgdata, 0, 0, parts[part].w, parts[part].h,
     570              FImages[0], x, y, parts[part].w, parts[part].h * stretch_y, rfNearest)
     571        else
     572        if (col = 1) then
     573          StretchRect(parts[part].imgdata, 0, 0, parts[part].w, parts[part].h,
     574              FImages[0], x, y, parts[part].w * stretch_x, parts[part].h, rfNearest)
     575        else
     576          CopyRect(parts[part].imgdata, 0, 0, parts[part].w, parts[part].h,
     577              FImages[0], x, y );
     578        if row = 1 then
     579          y := y + parts[part].h * stretch_y
     580        else
     581          y := y + parts[part].h;
     582      end;
     583    end;
     584    if cols[col] > 0 then
     585    begin
     586      if (col = 1) then
     587        x := x + parts[part].w * stretch_x
     588      else
     589        x := x + parts[part].w;
     590    end;
     591  end;
     592
     593  FreeImage(pspcimage);
     594  for i := 0 to 8 do
     595    if parts[i].used then
     596      FreeImage(parts[i].imgdata);
     597
     598  Result := True;
    606599end;
    607600
Note: See TracChangeset for help on using the changeset viewer.