ImageHelpers.cs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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. image.TransformColorSpace(ColorProfiles.SRGB);
  15. image.Write(outputStream);
  16. int? GetIccProfileVersion()
  17. {
  18. var imageProfile = image.GetProfile("icc");
  19. if (imageProfile == null)
  20. return null;
  21. var imageProfileRaw = imageProfile.ToByteArray();
  22. if (imageProfileRaw.Length < 12)
  23. return null;
  24. return imageProfileRaw[8];
  25. }
  26. }
  27. public static void ConvertImageIccColorSpaceProfileToVersion2(string inputPath, string outputPath)
  28. {
  29. using var inputStream = File.OpenRead(inputPath);
  30. using var outputStream = File.OpenWrite(outputPath);
  31. ConvertImageIccColorSpaceProfileToVersion2(inputStream, outputStream);
  32. }
  33. }