How to CCITT decode a blob - BlobToImage isn't doing it.

Questions and postings pertaining to the development of ImageMagick, feature enhancements, and ImageMagick internals. ImageMagick source code and algorithms are discussed here. Usage questions which are too arcane for the normal user list should also be posted here.
Post Reply
Minok
Posts: 21
Joined: 2014-04-24T15:51:58-07:00
Authentication code: 6789

How to CCITT decode a blob - BlobToImage isn't doing it.

Post by Minok »

I'm getting a CCITT type 6 (group 4) compressed blob out of a CGM file, that represents the contents of a single tile in that format.
So its 920 bytes worth of CCITT compressed bitonal (B&W) data.

I'm trying to create an imagemagick image out of it but that process just fails and returns null, with error:
NoDecodeDelegateForThisImageFormat `' @ error/blob.c/BlobToImage/358.

Code: Select all

		MagickCoreGenesis(".", MagickTrue);	// initialize the imagemagick environment

		Image* imgMagickImage;			// an imagemagick image handle
		ImageInfo* imgMagickInfo= AcquireImageInfo(); // the imagemagick image info object
		ExceptionInfo* imgMagickException= AcquireExceptionInfo();	// where exception information is stored from calls

		imgMagickImage = BlobToImage(imgMagickInfo, (void*) dataBlob, (size_t)tileSize, imgMagickException);
		//Image* img = BlobToImage(const ImageInfo *image_info,const void *blob, 	const size_t length,ExceptionInfo *exception);

		CatchException(imgMagickException);	// test for and process imagemagick exceptions
		if (!imgMagickImage) { std::cerr << "ERROR: no magicImage returned - could not parse the blob" << std::endl; }

		MagickCoreTerminus();	// close & clean up the imagemagick environment
I've saved the blob to file and looked at it with a hex editor, and compared it with the blob that is stored in the CGM file from which it comes.
It is the same - that is, the blob is the CCITT compressed bit sequence for the compressed data.

Am I missing something here in how to use BlobToImage? When I fed it a blob that was a JPG encoded color image, that returned with the identified JPEG image.

Do I need to decorate the CGM sourced blob with some other stuff before I feed it to BlobToImage?
Minok
Posts: 21
Joined: 2014-04-24T15:51:58-07:00
Authentication code: 6789

Re: How to CCITT decode a blob - BlobToImage isn't doing it.

Post by Minok »

I've managed to find a solution to my problem and a guess a the reason.

Why doesn't it work as described above? I think ImageMagick does not support pure CCITT compressed data - but then it cannot possibly do so, because needed knowledge is not encoded in the CCITT blob such as the dimensions of the image, and a host of other things needed to correctly use the CCITT blob.

So what I did was, knowing the missing information, create a TIFF blob by adding the correct TIFF header and dictionary information, and then calling ImageMagic to convert the blob to an Image.

This amounted to adding the 4 bytes of type 42 intel encoding TIFF, along with an 11 entry TIFF Image File Directory in front of the blob that is the CCITT blob.
The tiff directory specified
  • the tiff compression as 4 (CCITT Group 4)
  • image width, height
  • photometric mode as 0 (pixel value 1 is black, pixel value 0 is white)
  • offset to the CCITT blob ( 154 bytes from the front of the fixed size header I have of 11 entries + storage for x/y resolution)
  • samples per pixel = 1
  • rows of image per strip = image height (ie the whole image in one row, all rows in the one strip)
  • strip bytes count = how many bytes the strip is - ie the size of the CCITT blob
  • resolution unit = 2 (inches) - may not be needed but TIFF docs seemed to suggest it
  • X and Y resolution - an offset to storage of a rational number defining 100dpi in my case
Writing that produced tiff blob to the file system let me verify it was a valid tiff (as Windows would display that file correctly).

BlobToImage then returned and populated some meaningful information about the Image it created.
Post Reply