Search code examples
imagemagickzbar

imagemagick : saving MagickExportImagePixels's output blob to gray image file?


zbar engine sample source(zbarimg.c) shows the following.

https://github.com/ZBar/ZBar/blob/master/zbarimg/zbarimg.c

size_t bloblen = width * height;
unsigned char *blobdata = malloc(bloblen);
MagickExportImagePixels(images, 0, 0, width, height, "I", CharPixel, blobdata);

I'd like to see the blobdata. How can I save the blobdata to file?

I made save_imgdata function to save blobdata.

int save_imgdata(char* imgf, int width, int height, char *raw)
{
    PixelWand *p_wand = NULL;
    PixelIterator *iterator = NULL;
    PixelWand **pixels = NULL;
    unsigned long x, y;
    char hex[128];

    //MagickWandGenesis();
    p_wand = NewPixelWand();
    PixelSetColor(p_wand, "gray");
    //PixelSetColor(p_wand, "white");
    MagickWand *m_wand = NewMagickWand(); //CORE_RL_wand_.lib;
    MagickSetImageDepth(m_wand, 8);
    MagickNewImage(m_wand, width, height, p_wand);
    // Get a new pixel iterator 
    iterator = NewPixelIterator(m_wand);
    for (y = 0; y<height; y++) {
        // Get the next row of the image as an array of PixelWands
        pixels = PixelGetNextIteratorRow(iterator, &x);
        // Set the row of wands to a simple gray scale gradient
        for (x = 0; x<width; x++) {
            sprintf(hex, "#%02x", *raw++);
            //sprintf(hex, "#%02%x02%x02x", *raw, *raw, *raw); raw++;
            PixelSetColor(pixels[x], hex);
        }
        // Sync writes the pixels back to the m_wand
        PixelSyncIterator(iterator);
    }
    MagickWriteImage(m_wand, imgf);
    DestroyMagickWand(m_wand);
    return 0;
}

The call of save_imgdata("imgw.bmp", width, height, blobdata) save 24bpp image.

What's wrong of save_imgdata? I want it saves 8bpp gray image file.


Solution

  • Don't bother iterating and building dynamic color/pixel values -- It's slow and resource intensive. If the data came from an export method, than use the import method to restore.

    int save_imgdata(char* imgf, int width, int height, void * raw)
    {
        MagickWand * wand;
        PixelWand * bgcolor;
    
        bgcolor = NewPixelWand();
        PixelSetColor(bgcolor, "WHITE");
        wand = NewMagickWand();
        MagickNewImage(wand, width, height, bgcolor);
        bgcolor = DestroyPixelWand(bgcolor);
        MagickSetImageDepth(wand, 8);
        MagickSetImageColorspace(wand, GRAYColorspace);
        MagickImportImagePixels(wand, 0, 0, width, height, "I", CharPixel, raw);
        MagickQuantizeImage(wand,
                            256,             // Reduce to 8bpp
                            GRAYColorspace,  // Match colorspace
                            0,               // Calculate optimal tree depth
                            MagickTrue,      // Use dither ? This changes in IM-7
                            MagickFalse);    // Messure Error
        MagickWriteImage(wand, imgf);
        wand = DestroyMagickWand(wand);
        return 0;
    }