views:

71

answers:

1

Hi, I've downloaded the dll's for ImageMagick and am wondering if anybody knows of some example code to accomplish a simple task:

I have generated an image in C++ and have the buffer in RGB format. I need to convert it to PDF format (without writing to a file) before sending it over a TCP socket.

Is this doable with ImageMagick (or any other library)?

+2  A: 

Assuming you're using Magick++ Magick::Image has a constructor that can create an image from an in-memory blob and a write() method that can write a PDF (and a bunch of other formats) to an in-memoy blob as well.

Note that on my Linux machine ImageMagick appears to be creating a few temporary files during operation. I'm not sure that meets your requirement but it might be configurable.

You can probably get a good idea from this code snippet:

#include <iostream>
#include <Magick++.h>

using namespace std;
using namespace Magick;

static char imageData[] = {
    /* ... */
};

int main(int argc, char** argv)
{
        /* Initialize the library */
        InitializeMagick(*argv);

        /* Instantiate an image from RGB data */
        Image image(4,         // Width
                    14,        // Height
                    "RGB",     // Color components ordering
                    CharPixel, // Components storage type
                    imageData);// Image data

        /* Write pdf in memory */
        Blob b;
        image.write(&b, string("pdf"));

        /* write pdf data to cout */
        /* it should be easy to send it over a socket instead */
        cout.write(static_cast<const char*>(b.data()), b.length());

        return 0;
}

Edit: I should probably add that writing binary data to cout on Windows will cause troubles unless you switch the output stream to binary mode. The code above is just a short sample so I've ignored that.

Alexandre Jasmin