简体   繁体   中英

Copying image raw data

I get a pointer to a raw image data from a thread every time a change happens and I want to make a copy of it before another thread uses it. How can I achieve this if I don't know the size of raw data, since it randomly selects a size and gives the raw data? How can I copy the raw data from this pointer?

To make a copy of your raw pointer "data", you need additional information: the size of this data. The raw data points to an image, and so you already have width and height at time of creation; you may also have a bpp (bytes per pixel) if this is not an 8-bit/pixel image. Multiplying these three will give you the raw data size.

Currently, you are only forwarding the address of the raw data. To have access to the other variables, you need to forward the rest as well.

One option (per my comment) is to expand the malloc ed raw data size by the size needed to store width and height (and optionally, bpp ) and store these at the very start.

A better way (because more manageable) would be to create a struct :

struct myData_t {
   int width;
   int height;
   void *data;
} myData, *myDataPtr;

Fill this struct with your data and return its address to your other function(s).

Remember to do a memory clean up in the correct order: first, free myDataPtr->data , and only then free myDataPtr itself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM