I have the following code which converts a pdf to a tiff file, I have two problems with it.
When my pdf file is composed of two pages for example, i have only the second page converted to tiff.
The tiff file quality is very bad compared to the same pdf file converted using convert command
convert -density 300 file.pdf -depth 8 -alpha remove -background white +repage file.tiff
/* gcc -I/usr/local/include/ImageMagick-7 -DMAGICKCORE_HDRI_ENABLE=1 -DMAGICKCORE_QUANTUM_DEPTH=16 magick.c -lMagickWand-7.Q16HDRI -o magick */
#include <MagickWand/MagickWand.h>
int main(int argc, char *argv[])
{
MagickWand *mw = NULL;
MagickWandGenesis();
mw = NewMagickWand();
MagickSetImageResolution(mw, 300, 300);
MagickReadImage(mw, argv[1]);
PixelWand *color = NewPixelWand();
PixelSetColor(color, "white");
MagickSetImageBackgroundColor(mw, color);
MagickWand *newwand = MagickMergeImageLayers(mw, FlattenLayer);
MagickSetImageCompressionQuality(newwand, 95);
MagickSetFirstIterator(newwand);
MagickSetFormat(newwand, "tiff");
MagickWriteImage(newwand, "/tmp/out.tiff");
mw = DestroyMagickWand(mw);
newwand = DestroyMagickWand(newwand);
MagickWandTerminus();
return 0;
}
SOLUTION: After integration of @emcconville feeds, the function to convert multipage PDF to tiff is as follow:
#include <MagickWand/MagickWand.h>
static void __attribute__((constructor)) mg_ctor(void)
{
MagickWandGenesis();
}
static void __attribute__((destructor)) mg_dtor(void)
{
MagickWandTerminus();
}
/*
* pdf2tiff {pdf file} {output tiff file}
*
*/
int main(int argc, char *argv[])
{
MagickWand *mw = NewMagickWand();
int i = 0;
MagickSetResolution(mw, 300, 300);
MagickReadImage(mw, argv[1]);
PixelWand *color = NewPixelWand();
PixelSetColor(color, "white");
for (i = 0; i < MagickGetNumberImages(mw); i++) {
MagickSetIteratorIndex(mw, i);
MagickSetImageAlphaChannel(mw, RemoveAlphaChannel);
MagickSetImageBackgroundColor(mw, color);
}
MagickResetIterator(mw);
MagickSetFormat(mw, "tiff");
MagickWriteImages(mw, argv[2], 1);
DestroyMagickWand(mw);
DestroyPixelWand(color);
return 0;
}