ImageHelpers.cs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. using ImageMagick;
  2. namespace QuestPDF.ConformanceTests.TestEngine;
  3. public static class ImageHelpers
  4. {
  5. public static void ConvertImageIccColorSpaceProfileToVersion2(Stream inputStream, Stream outputStream)
  6. {
  7. using var image = new MagickImage(inputStream);
  8. var iccVersion = GetIccProfileVersion();
  9. if (iccVersion == 2)
  10. {
  11. image.Write(outputStream);
  12. return;
  13. }
  14. if (iccVersion != null)
  15. image.RemoveProfile("icc");
  16. image.ColorSpace = ColorSpace.sRGB;
  17. image.SetProfile(ColorProfile.SRGB);
  18. image.Write(outputStream);
  19. int? GetIccProfileVersion()
  20. {
  21. var imageProfile = image.GetProfile("icc");
  22. if (imageProfile == null)
  23. return null;
  24. var imageProfileRaw = imageProfile.ToByteArray();
  25. if (imageProfileRaw.Length < 12)
  26. return null;
  27. return imageProfileRaw[8];
  28. }
  29. }
  30. public static void ConvertImageIccColorSpaceProfileToVersion2(string inputPath, string outputPath)
  31. {
  32. using var inputStream = File.OpenRead(inputPath);
  33. using var outputStream = File.OpenWrite(outputPath);
  34. ConvertImageIccColorSpaceProfileToVersion2(inputStream, outputStream);
  35. }
  36. }