JonathanCR
JonathanCR

Reputation: 11

Initialising stbi_uc without loading an image

I have what seems like an elementary question, but I can't find an answer.

I want to initialise a new stbi_uc object, setting its dimensions and so on, without loading an image into it. (This is because the program itself will be creating an image using this object, so there's no need to load anything.) I can't find a way to do this. I'm using a clumsy workaround at the moment where I create a blank SFML image of the desired dimensions, save it to disk, and then load that back into the stbi_uc object:

int width=500;
int height=500;

sf::Image *templateimage=new sf::Image;

templateimage->create(width,height,sf::Color(0,0,0));

bool const ret1(templateimage->saveToFile("Templateimage.tga"));
    if (!ret1) {cerr << "Error writing Templateimage.tga" << endl;}

delete templateimage;

int imagewidth, imageheight, imagechannels;

stbi_uc *myimage=new stbi_uc;

myimage=stbi_load("Templateimage.tga",&imagewidth,&imageheight,&imagechannels,STBI_rgb_alpha);

This works, but it is surely ridiculous. What I really want to do is something like this:

stbi_uc *myimage=new stbi_uc(width,height,channels);

But that's not possible. I've tried this, but it doesn't generate the correct size:

stbi_uc *myimage=new stbi_uc(width*height*channels);

Is there a better way to do this than what I've got now? Even converting the sf::Image directly into the stbi_uc without having to save/load it would be an improvement.

Thank you!

Upvotes: 0

Views: 666

Answers (1)

Sam
Sam

Reputation: 23

i'm sure its too late to answer the question now, however maybe this will help someone in the future. I would just initialize the memory with malloc or calloc.

stbi_uc* myimage = (stbi_uc*)calloc(width * height * channels , 1);

This will create your image, with rgba just set to 0,0,0,0. Now if you wanted to do it a little faster, and you were going to change all pixels anyway, i would use malloc, which is faster but will initialize the image with nonsense.

stbi_uc* myimage = (stbi_uc*)malloc(width * height * channels);

Hope this helps :)

Upvotes: 2

Related Questions