read from shared mem out

did anybody use the shared mem out already ?

i have the pointer to the shared memory
void *data = shm->getMemory();
(see derivative.ca/wiki/index.php … y_In_Touch)

i would like to copy the image data byte-by-byte from shared memory to another buffer.

for example, i have the pointer to a new buffer
unsigned char* bufferPointer = new unsigned char[3202403];

i would like to do something like this
for (int i=0; i<3202403; i++) {
buffer[i] = data[i];
}
this won’t work because the have diferent type.
i am not understanding exactly what i get when i do void *data = shm->getMemory();

You can cast a pointer to anything you want.
So you can do
char c = (char)data;
And now you can write to c, it points to the same memory as data, but it’s just interpreted as chars.

If you were writting floats you can do
float f = (float)data;

i did this
// copy data to tempbuffer
unsigned char* tempbuffer = new unsigned char[3202404];
tempbuffer = (unsigned char*)shm->getMemory();

the data in the tempbuffer 1) header + 2) imagedata, right ?

i want to use header object to figure out the offset to the image data in the shared memory data. right ?

int DataOffset = header->dataOffset;

or would i use the getImage() function in the header and to get imagedata ?

// Both the sender and the reciever can use this to get the poitner to the actual
// image data (as long as dataOffset is set beforehand).
float image = (float)header->getImage(); // For floating point data

(at bottom of page
derivative.ca/wiki/index.php … Memory_TOP)

just thinking wouldn’t it be nice if you could set the PIXEL Format in the Shared Mem Out TOP ?
after i get the image data i must convert from rgba to rgb,

like this

int j = 0;
for (int i=0; i<3202404; i++) {

if ((i+1)%4 != 0)
{
rgbbuffer[j] = rgbabuffer[i];
j++;
}
}

don’t know whats faster

Hey, I’m not sure if you left out code here, but this isn’t a copy operation. You create local memory, and a pointer to it (tempbuffer). Then you make tempbuffer point to the shared memory, this is a memory leak as you’ve now lost the memory that you allocated with the ‘new’ operation.

Generally the best way to do this is cast the shared memory to the header first (Because the start of the memory is where the header is). This is in the wiki article.

 TOP_SharedMemHeader *header = (TOP_SharedMemHeader*)shm->getMemory();

Once you have the header, you can get the image with getImage();

char *image = (char*)header->getImage();

Remember since you are dealing with pointers here, you haven’t done any actual memory copy yet.
Next you’ll likely want to copy the memory to a local buffer

unsigned char* tempbuffer = new unsigned char[header->width * header->height * 4]; memcpy(tempbuffer, image, header->width * header->height * sizeof(char) * 4);

Unfortunately GPUs don’t support RGB or BGR formats natively, it’s always RGBA or BGRA. So asking the GPU for RGB or BGR would slow down the download operation more, which is already slow. You’re better off doing it yourself on the CPU.

Also don’t forget that the Shared Mem Out gives you the data in BGRA format (which is the fastest way to download from the GPU).