This project has moved. For the latest updates, please go here.

Image corruption issue

Jan 22, 2016 at 9:13 PM
I am using Magick.NET to convert TIFF images from a check scanner into to a banking industry standard.

There is an image validator tool that is reporting some errors when certain images are processed using Magick.NET-Q16-AnyCPU net40-client 6.8.9.601.

The specific error messages from the validator:
  1. FillOrder: Tiff value is a defined variance
  2. End of Facsimile Block: Tiff image segment does not end with EOFB
In fact, simply loading the image and rewriting it without any manipulation will cause these compliance issues.
   static void Main(string[] args)
    {
        Console.WriteLine(MagickNET.Version);

        var bytes = File.ReadAllBytes(@"C:\images\WinIDAImages\2016-01-05T10-52-26-715_22_front.tiff");
        using (var magickImage = new MagickImage(bytes))
        {
            Console.WriteLine("Before ToByteArray");

            // raw
            var magickImageBytes = magickImage.ToByteArray();
            File.WriteAllBytes(@"C:\images\WinIDAImages\magick.NET_raw.tiff", magickImageBytes);

            Console.WriteLine();
        }
        Console.WriteLine("Press any key");
        Console.ReadKey();
    }
Using ImageMagick 6.9.3 as follows the validator does not report the compliance issues:
convert 2016-01-05T10-52-26-715_22_front.tiff IM6_raw.tiff
Using ImageMagick 6.9.3 as follows the validator does not report any compliance issues:
convert 2016-01-05T10-52-26-715_22_front.tiff -monochrome -colorspace Gray -compress Group4 -set units PixelsPerInch -density 240 IM6Converted.tif
So my questions:
  1. Is this a Magick.NET issue? I am not certain if Magick.NET wraps ImageMagick 6.9.3 or a different version of ImageMagick that exhibits this issue
  2. Depending on above - is there a version of Magick.NET that wraps ImageMagick 6.9.3? (that may mitigate this issue)
  3. Any recommendations for workaround? We prefer not to use Magick.NET 7.0.0.0022 because it is beta and we experienced some different issues with that version of the tool. We also had some issues with 7.0.0.0008
I can provide the TIFF files if that will help

Thank you in advance

Art
Coordinator
Jan 22, 2016 at 10:37 PM
Edited Jan 22, 2016 at 10:38 PM
  1. This is most likely an ImageMagick issue. We keep improving it and your version of Magick.NET 6.8.9.601 wraps ImageMagick 6.8.9-6 (released at 2014-07-25) so it is possible that this version contains a bug.
  2. There is no version of Magick.NET that will be using 6.9.3 because I switched to ImageMagick 7. It should be possible to build it yourself and I could assist you with that but I would advise you to continue reading.
  3. Magick.NET 7.0.0.0022 uses ImageMagick 7 that is currently in beta but we are planning to do an official release sometime this year. I would definitely recommend you to upgrade to the latest version and test if that resolves your issue. If you run into any other issues with that version please let me know so we can fix it. If we don't know there is a problem it will be hard to fix it :)
Jan 25, 2016 at 9:14 PM
Thank you.

I have repeated my conversion experiments using Magick.NET 7.0.0.0022.

While the testing is not yet exhaustive, using Magick.NET 7.0.0.0022 to convert TIFF images from a check scanner into to the banking industry standard format appears to produce compliant TIFF files.
This is an improvement over both 6.8.9.601 and 7.0.0.008
(with 7.0.0.008 we had some TIFF images that were all black when converted)

The issue I mentioned earlier is when using Magick.NET 7.0.0.0022 to convert JPG to the required TIFF format. Of 3 test files, 2 of the converted images appear to be all white.

I have isolated this to assigning the Group4 compression method:
    static void Main(string[] args)
    {
        Console.WriteLine(MagickNET.Version);

        const string srcFile = @"C:\dev\Sandbox\JpegToTiff\JpegToTiff\image_front.jpg";
        // const string srcFile = @"C:\dev\Sandbox\JpegToTiff\JpegToTiff\image_front3.jpg
        const string destFile = @"C:\images\WinIDAImages\magick.NET_raw.jpg";

        var bytes = File.ReadAllBytes(srcFile);
        var magickReadSettings = new MagickReadSettings
        {
            UseMonochrome = true
        };
        using (var magickImage = new MagickImage(bytes, magickReadSettings))
        {
            Console.WriteLine("Before ToByteArray");

            magickImage.ResolutionUnits = Resolution.PixelsPerInch;
            magickImage.ColorSpace = ColorSpace.Gray;
            magickImage.Density = new PointD(200, 200);
            magickImage.Format = MagickFormat.Tif;
            //group4 compression
            magickImage.CompressionMethod = CompressionMethod.Group4;
            File.WriteAllBytes(destFile, magickImage.ToByteArray());

            Console.WriteLine();
        }
        Console.WriteLine("Press any key");
        Console.ReadKey();
    }
In this sample, if the CompressionMethod = CompressionMethod.Group4 line is removed then the converted image is not all white. However it is not compliant with the industry required format.

Q: Any suggestions for how to work around this issue?

I can provide the image files if that will help

Thank you in advance

Art
Coordinator
Jan 26, 2016 at 8:16 AM
Could you add your files? This looks like a bug in ImageMagick.
Jan 26, 2016 at 1:18 PM
Coordinator
Jan 26, 2016 at 9:31 PM
Thanks, I will take a look at this later this week.
Jan 29, 2016 at 9:14 PM
Edited Jan 29, 2016 at 9:35 PM
I'm experiencing a similar issue, but when converting a multi-page PDF to a series of single-page TIFFs. Some of the resulting TIFF files are blank and are noticeably smaller than the other files. Like @arthg, I'm seeing this problem when selecting CompressionMethod.Group4 or CompressionMethod.Fax. If I select CompressionMethod.LZW, all resulting TIFF files are valid, but are MUCH larger.

FWIW, If I convert the input PDF manually using the IM convert utility, the output files are all OK. Here is a sample of how I converted the first page.

convert -density 300 -compress group4 "sample6.pdf[0]" sample6p1.tiff
private static void ConvertImageToTiff()
{
  // Log all events
  //MagickNET.SetLogEvents(LogEvents.All | LogEvents.Trace);
  // Set the log handler (all threads use the same handler)
  //MagickNET.Log += DetailedDebugInformationSamples.MagickNET_Log;

  string sampleDocsDirectory = @"E:\projects\ImageProcessing\sampledocs\";
  string sampleFile = "sample6.pdf";

  try
  {
    MagickReadSettings settings = new MagickReadSettings();
    settings.Density = new PointD(300, 300);

    using (MagickImageCollection images = new MagickImageCollection())
    {
      // Add all the pages of the source file to the collection
      images.Read(Path.Combine(sampleDocsDirectory, sampleFile), settings);

      //Show page count
      Console.WriteLine("page count for {0} {1}", sampleFile, images.Count);

      //Base file name
      string baseFileName = Path.GetFileNameWithoutExtension(sampleFile);
      string fmtStr = GetPageNumberFormat(images.Count);
      
      int page = 1;
      foreach (MagickImage image in images)
      {
        switch (image.Format)
        {
          case MagickFormat.Jpeg:
          case MagickFormat.Jpg:
            image.CompressionMethod = CompressionMethod.LZW;
            break;
          case MagickFormat.Pdf:
            image.CompressionMethod = CompressionMethod.Group4;
            //image.CompressionMethod = CompressionMethod.LZW;
            break;
          case MagickFormat.Tif:
          case MagickFormat.Tiff:
          case MagickFormat.Tiff64:
            break;
          default:
            image.CompressionMethod = CompressionMethod.LZW;
            break;
        }
        
        Console.WriteLine("saving file: {0}", baseFileName + ".p" + page.ToString(fmtStr) + ".tif");
        image.Write(sampleDocsDirectory + baseFileName + ".p" + page.ToString(fmtStr) + ".tif");
        page++;
      }
    }

  }
  catch (Exception ex)
  {
    Console.WriteLine("ReadPdf error {0}", ex.Message);
  }
}
I will attach sample input and output image files to workitem 1373.

Thank you for your help!
Coordinator
Jan 30, 2016 at 4:44 PM
It appears this issue has been resolved. I just tried both your example files and I can convert the images without any problems in my development build. That means this will be resolved in the next version of Magick.NET (7.0.0.0100).
Feb 1, 2016 at 4:51 PM
@dlemstra: Thank you for testing. Any idea when we might see that version?
Coordinator
Feb 1, 2016 at 5:31 PM
Edited Feb 1, 2016 at 5:32 PM
I hope to publish this new release sometime this week / weekend. It is quite a big change. You can find some more information about the new version here: https://magick.codeplex.com/discussions/650746.
Feb 1, 2016 at 6:34 PM
x-post: I have tested the dev build referenced above and I'm very happy to report that it has resolved the issues I reported. I'm excitedly awaiting the release of this version!