1
1

TgaDetector.cs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. using System.Collections.Generic;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Reflection;
  5. using Masuit.Tools.Mime;
  6. namespace Masuit.Tools.Files.FileDetector.Detectors;
  7. [FormatCategory(FormatCategory.Image)]
  8. internal sealed class TgaDetector : IDetector
  9. {
  10. public string Extension => "tga";
  11. public string Precondition => null;
  12. public string MimeType => new MimeMapper().GetMimeFromExtension("." + Extension);
  13. public List<FormatCategory> FormatCategories => GetType().GetCustomAttributes<FormatCategoryAttribute>().Select(a => a.Category).ToList();
  14. public bool Detect(Stream stream)
  15. {
  16. stream.Position = 3;
  17. int compressionType = stream.ReadByte();
  18. if (compressionType is not (0 or 1 or 2 or 3 or 9 or 10 or 11 or 32 or 33))
  19. {
  20. return false;
  21. }
  22. stream.Position = 17;
  23. int depth = stream.ReadByte();
  24. if (depth is not (8 or 24 or 15 or 16 or 32))
  25. {
  26. return false;
  27. }
  28. stream.Position = 1;
  29. int mapType = stream.ReadByte();
  30. return mapType == 0 || (mapType == 1 && depth == 8);
  31. }
  32. public override string ToString() => "Targa Detector (Experimental)";
  33. }