Advertisement

conversion problem

Started by May 18, 2023 05:31 PM
4 comments, last by GameDevCorn 1 year, 5 months ago

Hello,

I only have a small issue I would like some help with.

I originally loaded a graphic with stbi. It created a unsigned char* for the pixel data.

I then loaded that pixel data into a vector<unsigned char> image.

I am no longer using that function but i still need the data to be store in image for another function.

I've tried getting the pixels from the surfaces by casting it into a unsigned char* and then load it

into the vector like before. It returns nothing. I've watched it in the debugger watch window.

The only way for me to get anything is to cast it as a void*. So the question now is…

How do I convert it for my vector? Here's some examples of what I've tried and failed.

unsigned char* Pixels1;
int texImgSize = crpSurf1->w * crpSurf1->h * 4;
//int texImgSize = crpSurf1->pitch * crpSurf1->h;
Pixels1 = static_cast<unsigned char*>(crpSurf1->pixels);
image.clear();
const unsigned char* v = (unsigned char *) Pixels1;
std::vector<unsigned char>Pixels(v, v + texImgSize );
image = Pixels;

and this:

void* Pixels1; 
int texImgSize = crpSurf1->w * crpSurf1->h * 4;
//int texImgSize = crpSurf1->pitch * crpSurf1->h;
Pixels1 = (void*)crpSurf1->pixels;
image.clear();
const unsigned char* v = (unsigned char *) Pixels1;
std::vector<unsigned char>Pixels(v, v + texImgSize );
image = Pixels;

In the void* code. I have data stored in Pixels1 but lose it when converting.

const unsigned char* v = (unsigned char *) Pixels1;

v = “”

memcpy has been my friend in different section of my code. I just haven't found out how to do it yet.

Any real help will be much appreciated.

I'll mention you in my source code along with the others I've learned from.

Thanks

GameDevCorn said:
I've tried getting the pixels from the surfaces by casting it into a unsigned char* and then load it into the vector like before. It returns nothing. I've watched it in the debugger watch window.

a few things come to mind:

  • did you lock the surface before accessing its pixels? it may be required for hardware-accelerated SDL surfaces (IIRC).
  • the width of the surface is not necessarily width; use pitch instead. a row of pixels may require a specific alignment
  • the number of bytes per pixel is not necessarily 4; use the surface's format->BytesPerPixel.


that aside, the difference between the two snippets suggests to me that the problem lies elsewhere, outside this particular snippet.

Advertisement

Thanks for replying.

I didn't lock the surface. I didn't think you had to if you're not changing anything.

I used the pixels right below this code to write to a texture. I locked the texture.

the difference between the two snippets suggests to me that the problem lies elsewhere, outside this particular snippet.

That's all to the function. I store the pixels in a vector and also write those pixels to a texture.

GameDevCorn said:
std::vectorPixels(v, v + texImgSize );
image = Pixels;

Pixels is the vector class, in particular it's not the data stored in the vector. Afaik you need "Pixels.data" for that.

Thanks to all who replied.

I ended up not using the vector but using the surface and a Uint8*

to achieve the same goal.

int imgSize = crpSurf1→pitch * crpSurf→h;
Uint8* image;
image = (Uint8*)malloc(imgSize);
image = (Uint8*)crpSurf→pixels;

It took some time because I look all over the net and tried different things.

Including std::span. In the end, the above code is what I went with.

This topic is closed to new replies.

Advertisement