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

How to read CCITTFaxDecode Group 4 image

Jun 15, 2016 at 3:36 PM
Hi,
I'm using nuget package Magick.Net-Q16-AnyCPU version 7.0.1.500 to read an image.
The image is a CCITTFaxDecode Group 4 image.

My basic code looks like this:
```
                org.pdfclown.bytes.IBuffer ibuf = args.xImage.BaseDataObject.GetBody(false);
                byte[] bytes = ibuf.GetByteArray(0, (int)ibuf.Length);
` int w = (int)args.xImage.Size.Width, h = (int) args.xImage.Size.Height;
                            MagickReadSettings readSettings = new MagickReadSettings();
                            readSettings.CompressionMethod = CompressionMethod.Group4;
                            readSettings.Format = MagickFormat.Fax;
                            readSettings.Width = w;
                            readSettings.Height = h;
                            using (MagickImage image = new MagickImage(bytes, readSettings))
                            {
                                image.Format = MagickFormat.Png;
                                // Write the image to the memorystream
                                image.Write(string.Format("img{0},png", args.pageNo));
                            }`
I'm not sure if I'm setting the readSettings correctly.
With the code as shown I get the error:
Unexpected end-of-file '': No such file or directory @ error/fax.c/ReadFAXImage/189
If I remove the line setting the read format, I get the error
no decode delegate for this image format
' @ error/blob.c/BlobToImage/355

The image is embedded in a pdf document which I'm reading with pdfclown. The pdf renders fine with adobe.

Any suggestions much appreciated.
Martin
Jun 15, 2016 at 7:49 PM
I'm now also setting
readSettings.ColorType = ColorType.Bilevel;
readSettings.UseMonochrome = true;
as per TEXT

I iterated over the following
                                MagickFormat[] formats = new MagickFormat[] { MagickFormat.Fax, MagickFormat.Group4, MagickFormat.Pdf, MagickFormat.Pdfa, MagickFormat.Tif, MagickFormat.Tiff, MagickFormat.Tiff64 };
                                CompressionMethod[] compressionMethods = new CompressionMethod[] { CompressionMethod.Fax, CompressionMethod.Group4 };

None of combinations worked. Results as follows:

External Image 'ImagePart_1' (13 0 R)Exception thrown: 'ImageMagick.MagickCorruptImageErrorException' in Magick.NET-Q16-AnyCPU.dll
format Fax compression Fax Error Unexpected end-of-file '': No such file or directory @ error/fax.c/ReadFAXImage/189
Exception thrown: 'ImageMagick.MagickCorruptImageErrorException' in Magick.NET-Q16-AnyCPU.dll
format Fax compression Group4 Error Unexpected end-of-file '': No such file or directory @ error/fax.c/ReadFAXImage/189
Exception thrown: 'ImageMagick.MagickCoderErrorException' in Magick.NET-Q16-AnyCPU.dll
format Group4 compression Fax Error C:/Users/Martin/AppData/Local/Temp/magick-20064WK1-tZIQbZC8: Bad value 0 for "Orientation" tag. _TIFFVSetField' @ error/tiff.c/TIFFErrors/560
Exception thrown: 'ImageMagick.MagickCoderErrorException' in Magick.NET-Q16-AnyCPU.dll
format Group4 compression Group4 Error C:/Users/Martin/AppData/Local/Temp/magick-20064Y4l8UFyFoFiN: Bad value 0 for "Orientation" tag.
_TIFFVSetField' @ error/tiff.c/TIFFErrors/560
Exception thrown: 'ImageMagick.MagickDelegateErrorException' in Magick.NET-Q16-AnyCPU.dll
format Pdf compression Fax Error PDFDelegateFailed The system cannot find the file specified.
' @ error/pdf.c/ReadPDFImage/783
Exception thrown: 'ImageMagick.MagickDelegateErrorException' in Magick.NET-Q16-AnyCPU.dll
format Pdf compression Group4 Error PDFDelegateFailed
The system cannot find the file specified.
' @ error/pdf.c/ReadPDFImage/783
Exception thrown: 'ImageMagick.MagickDelegateErrorException' in Magick.NET-Q16-AnyCPU.dll
format Pdfa compression Fax Error PDFDelegateFailed The system cannot find the file specified.
' @ error/pdf.c/ReadPDFImage/783
Exception thrown: 'ImageMagick.MagickDelegateErrorException' in Magick.NET-Q16-AnyCPU.dll
format Pdfa compression Group4 Error PDFDelegateFailed
The system cannot find the file specified.
' @ error/pdf.c/ReadPDFImage/783
Exception thrown: 'ImageMagick.MagickCoderErrorException' in Magick.NET-Q16-AnyCPU.dll
format Tif compression Fax Error Not a TIFF file, bad magic number 65535 (0xffff). ' @ error/tiff.c/TIFFErrors/560
Exception thrown: 'ImageMagick.MagickCoderErrorException' in Magick.NET-Q16-AnyCPU.dll
format Tif compression Group4 Error Not a TIFF file, bad magic number 65535 (0xffff).
' @ error/tiff.c/TIFFErrors/560
Exception thrown: 'ImageMagick.MagickCoderErrorException' in Magick.NET-Q16-AnyCPU.dll
format Tiff compression Fax Error Not a TIFF file, bad magic number 65535 (0xffff). ' @ error/tiff.c/TIFFErrors/560
Exception thrown: 'ImageMagick.MagickCoderErrorException' in Magick.NET-Q16-AnyCPU.dll
format Tiff compression Group4 Error Not a TIFF file, bad magic number 65535 (0xffff).
' @ error/tiff.c/TIFFErrors/560
Exception thrown: 'ImageMagick.MagickCoderErrorException' in Magick.NET-Q16-AnyCPU.dll
format Tiff64 compression Fax Error Not a TIFF file, bad magic number 65535 (0xffff). ' @ error/tiff.c/TIFFErrors/560
Exception thrown: 'ImageMagick.MagickCoderErrorException' in Magick.NET-Q16-AnyCPU.dll
format Tiff64 compression Group4 Error Not a TIFF file, bad magic number 65535 (0xffff).
' @ error/tiff.c/TIFFErrors/560
Coordinator
Jun 15, 2016 at 9:34 PM
Edited Jun 15, 2016 at 9:35 PM
Can you share your input image on something like onedrive or dropbox?
Jun 15, 2016 at 9:46 PM
Hi, Thanks for your reply. I'll have to create one that's not sensitive. I'll do that tomorrow. Thanks Martin
Jun 15, 2016 at 10:35 PM
Here's the url to an image TEXT
Here's some information I have about the image
BitsPerComponent 1
ColorSpace DeviceGray
DecodeParams Columns 2484, K -1
Filter CCITTFaxDecode
Height 2503
Length 24 0 R
Name ImagePart_3
SubType Image
Type XObject
Width 2484

Thanks
Martin
Coordinator
Jun 16, 2016 at 7:53 PM
What is the content of the image? If I remove one of the checks I get an all white image. And what is the size of your image?
Jun 16, 2016 at 8:04 PM
Hi
Here is a screen shot of pdf page concerned.
TEXT

I'm not sure how to determine the size the image should be, maybe it's the length attribute from above. I'll have a look :-)

Thanks very much for your help so far.
Martin
Jun 16, 2016 at 8:33 PM
The length of the compressed data stream is 40052 bytes, which is the size of the file
Would it help if I send the image in as part of a single page pdf?
Coordinator
Jun 16, 2016 at 8:46 PM
I did not mean the size in bytes but the dimensions of the image but width and height. It is now using the default 'fax' size (2592x3508). I suspect it is using the wrong decoding and that is why it fails. The format has 3 types of compression (http://www.fileformat.info/format/fax/egff.htm) and I think we only support CCITT Group 3. I will go into the details later tonight. Working on another ImageMagick issue right now.
Jun 16, 2016 at 8:46 PM
Here's a link to the single page pdf, in case it helps
[TEXT](https://1drv.ms/b/s!AuHLKfHr7wNej9YGkB_tvkdBwfZP_Q
Jun 16, 2016 at 8:49 PM
Sorry, I've just seen you latest message.
I see in the code for magick there is talk of a group 4 as well, but I don't know if that's to do with fax or something else.
Thanks very much for your help :-)
Coordinator
Jun 17, 2016 at 2:32 PM
It appears that my assumption was correct. Group4 compression was not support by ImageMagick. I just pushed some changes to add support for that in ImageMagick: http://git.imagemagick.org/repos/ImageMagick/commit/52456ef5ab4b8c1e3f0229674f38765cdc074b2d. This will be available in the next version of Magick.NET
Jun 17, 2016 at 3:17 PM
Hi Dirk, That's awesome - thanks very much!
Should I be able to pull that commit now, or do I need to wait for the next release? When would that be?

Thanks again
Martin
Jun 17, 2016 at 3:34 PM
I think I've got it. I'll just see if I can work out the readSettings :-)
Jun 17, 2016 at 4:25 PM
Hmm - the configure script to make the build environment is failing.
I have the bash shell on my WIndows 10 pc. Added bin folder of visual studio to path env variable. (initially no compiler detected)
Now the linker is failing to find LIBCMT.lib - I believe because it doesn't know where it's located.

I'll see if there's other threads about this.
Thanks
Martin
PS knowing when the next release will be would still be good :-)
Coordinator
Jun 17, 2016 at 5:10 PM
I can publish a new release for you tomorrow, been a month since the last release anyway.
Jun 17, 2016 at 5:12 PM
That's brilliant - thank you :-)
Coordinator
Jun 18, 2016 at 7:34 PM
Edited Jun 18, 2016 at 7:35 PM
I am having problems with one of my unit tests that is failing with the latest ImageMagick code. And I cannot figure out why it fails at the moment. This means that I cannot publish a new release at the moment and I will get back to this topic once I have published the new release.
Jun 18, 2016 at 7:38 PM
Thanks for keeping me informed Dirk :-)
Martin
Coordinator
Jun 21, 2016 at 10:32 PM
After 3 days of tracking down the issue I found the bug yesterday night. It was a bit to late to publish a new release then so I had to do it today after work. I just published a new release that has support for the G4 format. Feel free to reply to this post or start a new topic if you run into another issue.
Jun 22, 2016 at 12:12 PM
Hi Dirk,
Thanks so much for your hard work :-)

I'm exploring it and the samples (convertpdf, detaileddebuginformation) to determine required readersettings.

Thanks again
Martin
Jun 22, 2016 at 1:28 PM
Hi again,
I've got some questions...

I wanted to get some reassurance that imagemagick has good coverage of the images types in my pdfs. So I took a typical pdf, and ran the convertpdf sample over it.
The good news is that it passed.

My next step was to determine the readsettings appropriate to read those images.

Having looked at the source code it's not obvious to me that images read from a pdf are read in the same way as an image from a distinct stream.
For example with my code, I have access to the image body and metadata about the image. I expect to be able to use the metadata to fill in the readSettings and then make a new MagickImage with those bytes and readSettings.

When reading from a pdf the image format is reported as pdf, rather than the specific type (eg jpeg2000)

Are there any notes on how to read images directly from a pdf, with appropriate readSettings, rather than using the technique in the convertpdf sample?

Does that make sense? :-)
Thanks
Martin
Jun 22, 2016 at 4:04 PM
Hi Dirk,

I've got some memory leaks, so I'm going to get on top of those before any more image decoding work.

Thanks
Martin