0

I have the following code which converts a pdf to a tiff file, I have two problems with it.

  1. When my pdf file is composed of two pages for example, i have only the second page converted to tiff.

  2. 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;
}
elhadi dp ıpɐɥןǝ
  • 4,763
  • 2
  • 30
  • 34

1 Answers1

3

When my pdf file is composed of two pages for example, i have only the second page converted to tiff.

You want to reset the image iterator immediately after reading the PDF.

MagickReadImage(mw, argv[1]);
MagickResetIterator(mw);

The tiff file quality is very bad compared to the same pdf file converted using convert command [...]

I would suspect that MagickSetImageResolution should be MagickSetResolution.

// MagickSetImageResolution(mw, 300, 300);
MagickSetResolution(mw, 300, 300);
emcconville
  • 23,800
  • 4
  • 50
  • 66
  • Thank you very much for your answer, using MagickSetResolution has fixed the image quality, but i still have one page, now i can see that the two pages are superposed, i cannot see how to have two distinct pages – elhadi dp ıpɐɥןǝ May 22 '20 at 16:27
  • 1
    Are you not merging the layers? – emcconville May 22 '20 at 16:57
  • when i delete the subsection where i use MagickMergeImageLayers to avoid having transparent pixels, i see that my tiff file contains two pages, however when i use MagickMergeImageLayers, i see only one page. – elhadi dp ıpɐɥןǝ May 22 '20 at 17:34
  • 1
    Sorry, I'm not sure what your attempting to do. If you want to match the CLI, use `MagickSetImageAlphaChannel(mw, RemoveAlphaChannel); ` after set background, and do not call `MagickMergeImageLayers`. – emcconville May 23 '20 at 02:13
  • thank you very much; i have now the expected tiff image by looping over different pages using your advices. – elhadi dp ıpɐɥןǝ May 23 '20 at 09:29