How to define type and depth of output file?

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
marlam

How to define type and depth of output file?

Post by marlam »

Hello everybody!

In my application, I create an image and then write it. I'm having trouble defining the output type (grayscale) and depth (8 bit).
The saved image contains the data I expect, but it is always RGB and uses 16 bit per channel.

Here is a small example of what I'm doing:

Code: Select all

#include <wand/MagickWand.h>

int main(int argc, char *argv[]) {
    unsigned long w = 120, h = 120;
    MagickWandGenesis();
    MagickWand *magick_wand = NewMagickWand();
    PixelWand *magick_pixel_wand = NewPixelWand();
    PixelSetColor(magick_pixel_wand, "none");

    MagickNewImage(magick_wand, w, h, magick_pixel_wand);
    MagickSetImageType(magick_wand, GrayscaleType);
    MagickSetImageChannelDepth(magick_wand, AllChannels, 8);
    MagickSetImageDepth(magick_wand, 8);

    PixelIterator *magick_it = NewPixelIterator(magick_wand);
    for (unsigned long y = 0; y < h; y++) {
        PixelWand **magick_pixels = PixelGetNextIteratorRow(magick_it, &w);
        for (unsigned long x = 0; x < w; x++) {
            PixelSetRed(magick_pixels[x], static_cast<float>(x + y) / 255.0f);
            PixelSetGreen(magick_pixels[x], static_cast<float>(x + y) / 255.0f);
            PixelSetBlue(magick_pixels[x], static_cast<float>(x + y) / 255.0f);
        }
        (void)PixelSyncIterator(magick_it);
    }
    MagickWriteImage(magick_wand, "out.png");
    magick_wand = DestroyMagickWand(magick_wand);
    magick_it = DestroyPixelIterator(magick_it);
    MagickWandTerminus();
    return 0;
}
What am I doing wrong?
Post Reply