Pixels to binary and back with memcpy


I am writing some simple recorder / player applications that I plan to implement to raspberry pi.

The writer application generates lowrez animations and saves them into a binary file. The file contains a header with information about the animation, most important the number of values per frame.

The reader application will use the information in the header to send the data to the LED screens. For now I am making a simple test using ofPixels in both ends, comparing results to see if it is looking promising.

It is not.

The data coming played by the binary file looks scrambled. I wonder if I am messing up the datatypes?
The code is a bit involved to share but it looks like this:

  • load binary to memory

  • move pointer to binary to correct position ( current frame * bytes_per_frame )

  • copy from pointer bytes_per_frame into a ofPixels

    // avoid having to type animations[current_animation] all the time
    animationData_WS *anim = &animations[current_animation];

    int pointTo = anim->frame_size * loaded_frame_in_file;

    //reset pointer (just in case)
    anim->data = (char *)anim->animation_buffer.getData();
    anim->data += HEADER_SIZE;
    anim->data += anim->frame_size * loaded_frame_in_file;

    // ofPixels is for visualization only, data should end up in a UDP or so
    ofPixels pixels;

    pixels.allocate(anim->w, anim->h, 3);

    memcpy(pixels, anim->data, anim->frame_size);

Hi there,
I don’t know anything about your source data, and i don’t understand very well what you mean by “values per frame”…

Anyway, I guess you could try:
allocate(w, h, OF_IMAGE_COLOR);

Also there is no need to use allocate + memcopy, of provides all in one method:
setFromPixels(source, w, h, OF_IMAGE_COLOR);

Hey JordI!

Thank you for this, I see I should work on explained the problem better.

What I am trying to do is read binary data from a file, the data are pixel values for each frame an animation. sort of like writing an ofPixels to file every frame.

I write the binary do this by creating a buffer, making the calculations for the animation into the buffer and writing it into the file.

    int buffer_size = NUM_PIXELS * 3;
    file.open(ofToDataPath(DATA_PATH).c_str(), ios::out | ios::app| ios::binary );
    char buffer[buffer_size];
    // write into buffer 
    buffer[sample_index] = (char)val;
    file.write(buffer, buffer_size);

Reading the data in the other end is pretty similar, I create a buffer and a pointer to the buffer. values_per_frame tells me how many (char) should I move the pointer each frame

anim->data = (char *)anim->animation_buffer.getData();

I haven’t been able to successfully read the data back into ofPixels.
I guess I will keep trying, thank you anyway for your help

what @jordi points out is correct. when you do:

// ofPixels is for visualization only, data should end up in a UDP or so
ofPixels pixels;

pixels.allocate(anim->w, anim->h, 3);

memcpy(pixels, anim->data, anim->frame_size);

you are trying to copy the memory from data into the pixels object which is not correct. the pixels object internally allocates memory to store the pixels and that’s where you should be copying the bnary data you are getting from the file.

As jordi said the easiest way would be to do:

ofPixels pixels;
pixels.setFromPixels(anim->data, animd->w, anim->h, OF_PIXELS_RGB);

you can do pretty much the same with an ofImage (using OF_IMAGE_COLOR instead of OF_PIXELS_RGB) if you want and it will get it directly into a texture as well which will allow you to show it on screen right away

Oh right actually I was not so correct. I gave you an answer for an ofImage instead of ofPixels, which is what you use. @arturo explained it much better. So as far as I understand there are 2 solutions:

memcpy(pixels->getPixels(), anim->data, anim->frame_size);

or as @arturo said:

pixels.setFromPixels(anim->data, animd->w, anim->h, OF_PIXELS_RGB);

Second one is preferred.