[C++][Help!] Create Image from 2D array or Vector, while storing RGB in a type

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
TheProfessor
Posts: 10
Joined: 2014-04-11T10:14:47-07:00
Authentication code: 6789

[C++][Help!] Create Image from 2D array or Vector, while storing RGB in a type

Post by TheProfessor »

Hello, for convenience reasons I would like to store RGB(A) data in a struct or similar, populate a 2D array (preferred) or std::vector and then create an image.

Googling online, I found one example but it doesn't work for me.

Code: Select all

	typedef uint16_t WORD;

	typedef struct mRGB {
		WORD red;
		WORD green;
		WORD blue;
		//double alpha;
	};

	std::vector<mRGB> rgbv;

	mRGB rgbs;

        // this is just to test things
	for (int i = 0; i < 512 * 512; i++) {
		rgbs.red = rand() % 255;
		rgbs.green = rand() % 255;
		rgbs.blue = rand() % 255;

		rgbv.push_back(rgbs);
	}

	Magick::Blob blob(&rgbv[0], rgbv.size() * sizeof(rgbv[0]));

	Magick::Image image;
	image.magick("png");
	image.size("512x512");
	image.magick("RGB");	
	image.read(blob);
The result cannot be opened.

If I add image.magick("png"); the result is all black.

I haven't found any examples for a 2D array or vector of vectors.

TheProfessor
Posts: 10
Joined: 2014-04-11T10:14:47-07:00
Authentication code: 6789

Re: [C++][Help!] Create Image from 2D array or Vector, while storing RGB in a type

Post by TheProfessor »

Bump; is there a convenient way to go from 2D array/vector to a image?

snibgo
Posts: 12910
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: [C++][Help!] Create Image from 2D array or Vector, while storing RGB in a type

Post by snibgo »

I have no experience of reading from blobs, but I think they need to be in a format that IM can read, eg JPEG or PNG or whatever.

The function ImportImagePixels() accepts an array, and parameters for width, height, channels and so on.
snibgo's IM pages: im.snibgo.com

Post Reply