Question

I have a vector something of this sort

typedef struct
{
  int r,g,b;
} __attribute__((__packed__))
rgbs;

vector<rgbs> rgbt;

rgbs rgb;
for(int row=0;row<100; row++)
{
  for(int col=0;col<100; col++)
  {
     rgb.r=col+row;
     rgb.g=col+row;
     rgb.b=col+row;
     rgbt.push_back(rgb);
  }
}

What will be an efficient way to form a new image out of this vector using magick++;

Was it helpful?

Solution 2

Zaffy was right::

32 bit integer cannot be used in this case as the sizeof pixelpacket pointer to a red, blue or green pixel is 16 bit.

So, we need to use 16 bit unsigned integer from the stdint library

#include<stdint.h>
typedef uint16_t WORD;

Now in the structure rgbs we initialise WORD r,g,b instead.

The way to initialise Blob is : Blob blob(address of the pixel data, size of data) So, the code will be something like this:

Blob blob(&rgbt[0],(rgbt.size()*sizeof(rgbt[0]));
Image image;
image.size("100x100");
image.magick("RGB");
image.read(blob);

OTHER TIPS

I customized an example from Magick++ API documentation for you.

Blob blob( &rgbt[0], rgbt.size() / sizeof(rgbt[0]) );
Image image;
image.size( "100x100") 
image.magick( "RGB" ); 
image.read( blob);

But I am not sure if you should use one byte unsigned char instead of 4 bytes int. Give it a try and you'll see.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top