ImagingNif.pas 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. {
  2. This unit contains image format loader for textures in NIF model files.
  3. Works for NIF version 3 (StarTrek Bridge Commander, ...).
  4. Author: Delfi
  5. }
  6. unit ImagingNIF;
  7. {$I ImagingOptions.inc}
  8. interface
  9. uses
  10. ImagingTypes, Imaging, ImagingFormats, ImagingUtility;
  11. type
  12. { Class for loading and saving NIF images. It can load 24 bit RGB and 32 bit RGBA images}
  13. TNIFFileFormat = class(TImageFileFormat)
  14. protected
  15. procedure Define; override;
  16. function LoadData(Handle: TImagingHandle; var Images: TDynImageDataArray;
  17. OnlyFirstLevel: Boolean): Boolean; override;
  18. public
  19. function TestFormat(Handle: TImagingHandle): Boolean; override;
  20. end;
  21. implementation
  22. const
  23. SNIFFormatName = 'NetImmerse Image';
  24. SNIFMasks = '*.nif';
  25. type
  26. { NIF file header.}
  27. TNIFHeader = packed record
  28. Width: LongWord;
  29. Height: LongWord;
  30. PixelFmt: LongWord;
  31. end;
  32. { TNIFFileFormat class implementation }
  33. procedure TNIFFileFormat.Define;
  34. begin
  35. inherited;
  36. FName := SNIFFormatName;
  37. FFeatures := [ffLoad];
  38. AddMasks(SNIFMasks);
  39. end;
  40. function TNIFFileFormat.LoadData(Handle: TImagingHandle;
  41. var Images: TDynImageDataArray; OnlyFirstLevel: Boolean): Boolean;
  42. var
  43. Hdr: TNIFHeader;
  44. FmtInfo: TImageFormatInfo;
  45. begin
  46. SetLength(Images, 1);
  47. with GetIO, Images[0] do
  48. begin
  49. // Read NIF header
  50. Seek(Handle, 170, smFromBeginning);
  51. Read(Handle, @Hdr.Width, SizeOf(Hdr.Width));
  52. Read(Handle, @Hdr.Height, SizeOf(Hdr.Height));
  53. Read(Handle, @Hdr.PixelFmt, SizeOf(Hdr.PixelFmt));
  54. Seek(Handle, 182, smFromBeginning);
  55. // Determine image format
  56. Format := ifR8G8B8;
  57. if Hdr.PixelFmt = 2 then
  58. Format := ifA8R8G8B8;
  59. NewImage(Hdr.Width, Hdr.Height, Format, Images[0]);
  60. FmtInfo := GetFormatInfo(Format);
  61. Read(Handle, Bits, Size);
  62. SwapChannels(Images[0], ChannelRed, ChannelBlue);
  63. Result := True;
  64. end;
  65. end;
  66. function TNIFFileFormat.TestFormat(Handle: TImagingHandle): Boolean;
  67. var
  68. Hdr: longword;
  69. ReadCount: LongInt;
  70. begin
  71. Result := False;
  72. if Handle <> nil then
  73. begin
  74. ReadCount := GetIO.Read(Handle, @Hdr, SizeOf(Hdr));
  75. if Hdr = 1232364878 then Result := True;
  76. GetIO.Seek(Handle, -ReadCount, smFromCurrent);
  77. end;
  78. end;
  79. initialization
  80. RegisterImageFileFormat(TNIFFileFormat);
  81. end.