3

Trying to convert a 24 bpp bitmap to black & white TIFF with CCITT group 4 compression. The result is a TIFF 1 bpp image as expected but it is uncompressed.

I'm using FreePascal which has magickwand bindings and the status is never MagickFalse:

MagickWandGenesis;
wand := NewMagickWand;
try
  status := MagickReadImage(wand,PChar(InputFile));
  if (status = MagickFalse) then HandleError;

  status := MagickSetImageFormat(wand,'TIFF');
  if (status = MagickFalse) then HandleError;

  // convert to black & white/lineart
  status := MagickSetImageType(wand,BilevelType);
  if (status = MagickFalse) then HandleError;

  // Group4Compression seems defined as 4 which 
  // apparently doesn't match imagemagick source. Bug:
  //http://mantis.freepascal.org/view.php?id=26723
  status := MagickSetImageCompression(wand,CompressionType(7)); //was Group4Compression
  if (status = MagickFalse) then HandleError;

  // Apparently set(image)compresionquality and
  // stripimage are necessary to actually compress
  status := MagickSetImageCompressionQuality(wand,0);
  if (status = MagickFalse) then HandleError;
  status := MagickStripImage(wand);
  if (status = MagickFalse) then HandleError;

  status := MagickWriteImage(wand,PChar(OutputFile));
  if (status = MagickFalse) then HandleError;

finally
  wand := DestroyMagickWand(wand);
end;
MagickWandTerminus;

Source image at http://filehorst.de/d/bmqjzDuB

Original (faulty) program source code at http://filehorst.de/d/bluhjivq

Original (faulty) output image at http://filehorst.de/d/bhlbjHgp

What am I doing wrong?

Edit: solved; got the solution off-site: the CompressionType enum in the FreePascal bindings were probably out of date - Group4Compression was 4 (IIRC) while it should be 7.

I'll give the bounty to Mark Setchell as his answer was a necessary part of the solution. Source code above updated with correct version.

reiniero
  • 428
  • 6
  • 14
  • Maybe also set magicksetimagecompressionquality? See also http://www.imagemagick.org/discourse-server/viewtopic.php?f=6&t=15373#p54307 for the only codefragment I could find with group4 compression – Marco van de Voort Sep 13 '14 at 15:41
  • No, that doesn't help... – reiniero Sep 14 '14 at 08:26
  • Are you using the latest version of ImageMagick? This might be a bug that has been fixed. – dlemstra Sep 14 '14 at 09:13
  • Can you post a link to the output file you get? – Mark Setchell Sep 14 '14 at 09:20
  • Output file: http://filehorst.de/d/bhlbjHgp; using dlls dated 20/2/2013 on Windows (having problems with my Linux system which did use latest imagemagick). If a newer version successfully generates the output, please let me know (meanwhile, I'll go hunting for a new version/trying to fix my imagemagick linking problems on Linux)! – reiniero Sep 14 '14 at 11:59
  • Please do not edit your question and insert "Fixed" or "Solved". It makes your question "not a question". Add your solution and corrected code as an answer. – Jongware Sep 20 '14 at 10:08
  • @Jongware: did you notice the title was edited back? Please indicate how I should have handled temporarily keeping SO people from wasting time on a solved question while I was contacting the off-site guy who gave me the solution about writing an SO answer so he could get the bounty. Thank you. – reiniero Sep 21 '14 at 08:10

1 Answers1

0

With the PHP version at least, it seems that setting the compression type does not actually compress the image - see comments at bottom here.

It also shows in all examples I have found that you must subsequently call MagickSetImageCompressionQuality() and StripImage() to actually do the compression - see here.

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • Thanks. Unfortunately, adding MagickSetImageCompressionQuality (with either 0 or 100) and MagickStripImage just before writing has no effect. I'll try and convert the code to Python or PHP and run it on a Linux test system to see if that does work... – reiniero Sep 14 '14 at 15:07
  • Thanks, Mark, I did need to add those calls but faced another problem; solved - see above. – reiniero Sep 15 '14 at 11:45
  • Delighted to have been of assistance! Good luck with your project and thank you :-) – Mark Setchell Sep 15 '14 at 12:20