Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Image doesn't save but gives no error report.  (Read 2516 times)

0 Members and 1 Guest are viewing this topic.

Me-Myself-And-I

  • Jr. Member
  • **
  • Posts: 93
    • View Profile
Image doesn't save but gives no error report.
« on: November 16, 2023, 05:21:37 am »
I searched and searched for an existing topic about this problem because I think I made a similar topic previously. I just couldn't find it.
 
I keep running into this problem every time I make a program that is manipulating images.
Eventually the image does not save as a file and no error report shows up. I just can't figure why it doesn't save. Previous attempts have shown extreme flippancy by influence of non related changes that only sometimes fix it every other time it runs. With this specific program I'm not experiencing any flippancies yet.



string sframe[100];
int numberOfFrames;
RectangleShape shape;
Crop::loadcrop(string filetype)
{
       
        ifstream framelist("frames/frame list.txt");
        for(int a=0;a<100;a++)
        {
                getline (framelist,sframe[a]);
               
                if(sframe[a]=="")
                {
                        numberOfFrames=a-1;
                        break;
                }
                sframe[a]+=filetype;
        }
       
        shape.setOutlineColor(Color::Red);
        shape.setFillColor(Color::Transparent);
        shape.setOutlineThickness(2.f);
        shape.setSize(Vector2f(0,0));
}

int  currentframe=0;
Texture texture;
Sprite sprite;
IntRect rect[100];
IntRect selectrect;
bool firstclick=false; 
bool firsttap=false;   
Crop::setcrop(RenderWindow &window)
{
        if(currentframe<numberOfFrames)
        {
                texture.loadFromFile("frames/"+sframe[currentframe]);
                sprite.setTexture(texture);

                Vector2f MPosition=window.mapPixelToCoords(Mouse::getPosition(window));
               
                if(Mouse::isButtonPressed(Mouse::Left))
                {
                        shape.setPosition(MPosition.x,MPosition.y);
                       
                }
                if(Keyboard::isKeyPressed(Keyboard::Space))
                {
                        shape.setPosition(MPosition.x-shape.getSize().x,MPosition.y-shape.getSize().y);
                }
                shape.setSize(Vector2f(MPosition.x-shape.getPosition().x,MPosition.y-shape.getPosition().y));
                selectrect=IntRect(shape.getPosition().x,shape.getPosition().y,shape.getSize().x,shape.getSize().y);
                if(Keyboard::isKeyPressed(Keyboard::Return))
                {
                        if(firsttap==false)
                        {
                               
                                currentframe++;
                                firsttap=true;
                                rect[currentframe]=selectrect; 
                       
                        }
                }
                else
                {
                        firsttap=false;
                }
               
        window.draw(sprite);
        window.draw(shape);    
        }
        else
        {
                save();
                window.close();
        }
       
}





Crop::save()
{
        Image image[numberOfFrames];
        Vector2i totalsize;


        for(int x=1;x<numberOfFrames+1;x++)
        {
               
                image[x].loadFromFile("frames/"+sframe[x]);
               
               
        }
       
       
       
       
        Image saveimage;

        Vector2i currentsize;
        bool row=true;
        Vector2i sizecheck;
        for(int x=0;x<numberOfFrames;x++)
        {
                if(totalsize.x<1000)
                {
                        totalsize.x+=rect[x].width;
                }
               
                if(sizecheck.x<1000)
                {
                        sizecheck.x+=rect[x].width;
                }
                else
                {
                       
                        totalsize.y+=rect[x].height;
                        sizecheck.x=0; 
                }
               
        }

        saveimage.create(totalsize.x,totalsize.y,Color::Transparent);
       

        for(int x=0;x<numberOfFrames;x++)
        {      
               
                currentsize.x+=rect[x].width;
               

       
                if(currentsize.x<=totalsize.x)
                {
                        //
                        for(int x1=currentsize.x-rect[x].width;x1<currentsize.x;x1++)
                        {
                                for(int y=currentsize.y-rect[x].height;y<currentsize.y;y++)
                                {
                                        saveimage.setPixel(x1,y,image[x].getPixel(x1-currentsize.x,y-currentsize.y));
                                }
                        }      
                }
                else
                {
                        currentsize.y+=rect[x].height;
                        currentsize.x=0;
                }
        }
 
        saveimage.saveToFile("test/sheet.png");
        //WHY DO I KEEP RUNNING INTO THIS PROBLEM WHERE IMAGES DONT SAVE AND SHOW NO ERROR?!
       
}

Ok.So my code is extremely confusing to anyone not familiar with my terms but i'm absolutely certain that the save() function is where the problem is. And it is certain that the images are loaded  into 
Image image[numberOfFrames];
without any problems since these images have been saved as separate files before I tried combining them into one image.The important thing is getting the image called "saveimage" to save to file.

Any ideas what wrong here?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: Image doesn't save but gives no error report.
« Reply #1 on: November 16, 2023, 08:39:39 pm »
So does the file not exist on your disk? Are you checking in the working directory which might be different to your executable directory?
Have you added a break point and observed what happens when saveToFile is called?

You should really be checking the return value of loadFromFile as well as saveToFile
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Image doesn't save but gives no error report.
« Reply #2 on: November 16, 2023, 11:58:32 pm »
I was going to question whether numberOfFrames is actually getting initialized and all that stuff but just creating an image and saving it would still save it regardless of what content it has so have you tried saving it immediately after creation (without the 'faff' of the image copying) and is it saving and can you find that?
Obviously, it would be an empty image but when you can rule out the saving part, you can work out what's changing inbetween those lines that you don't expect.

I searched and searched for an existing topic about this problem because I think I made a similar topic previously. I just couldn't find it.
You can see all of your posts here, if that helps:
https://en.sfml-dev.org/forums/index.php?action=profile;area=showposts;u=24813
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Me-Myself-And-I

  • Jr. Member
  • **
  • Posts: 93
    • View Profile
Re: Image doesn't save but gives no error report.
« Reply #3 on: November 17, 2023, 01:57:15 am »
I figured out why it wasn't showing any error. Directly after this section,the window closes and no messages are sent to the console.
        for(int x=0;x<numberOfFrames;x++)
        {      
               
                currentsize.x+=rect[x].width;
               

       
                if(currentsize.x<=totalsize.x)
                {
                        //
                        for(int x1=currentsize.x-rect[x].width;x1<currentsize.x;x1++)
                        {
                                for(int y=currentsize.y-rect[x].height;y<currentsize.y;y++)
                                {
                                        saveimage.setPixel(x1,y,image[x].getPixel(x1-currentsize.x,y-currentsize.y));
                                }
                        }      
                }
                else
                {
                        currentsize.y+=rect[x].height;
                        currentsize.x=0;
                }
        }


I commented this out.
I'm guessing the applied changes are out of the image's size scope? Does this seem to be what would normally happen if the set pixel was out of scope?

The error that does show now is "Failed to save image "test/sheet.png""




Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Image doesn't save but gives no error report.
« Reply #4 on: November 17, 2023, 03:16:41 am »
Accessing pixels outside of the range of the image is undefined behaviour (you'd be attempting to access memory that is not part of the image) so I would expect a crash is an acceptable response (at least it's letting you know something is wrong!).

If the image is not saving directly after creation then it is not creating properly. Basically, it'll be an empty image. It's worth checking the value of totalsize before using it to create the image and attempt to find out why that might be (0,0) (or far too high - it might be worth initialising it first).

You really should - as mentioned in the first response by eXpl0it3r - be checking the return value of saveToFile. It will clearly be false at this point since it's failing, which shows you need it to tell you that something was wrong that made it fail. You could, for example, output "totalsize" if it's false.

I'd consider tracking all of the values involved whether that be outputting values in various places or using some form of "watch" inside your debugger. I tend to do the former (especially when the project is small) but the latter is likely better in the long run, if possible.

It's worth noting there are some other things that look suspicious too such as the 0-size and 1-(size+1) loops that use numberOfFrames, which I still have no idea as to what value that might be...
« Last Edit: November 17, 2023, 03:20:36 am by Hapax »
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Me-Myself-And-I

  • Jr. Member
  • **
  • Posts: 93
    • View Profile
Re: Image doesn't save but gives no error report.
« Reply #5 on: November 17, 2023, 05:09:49 am »
Thanks for the advice.

hueljannie

  • Newbie
  • *
  • Posts: 1
    • View Profile
Re: Image doesn't save but gives no error report.
« Reply #6 on: December 14, 2023, 03:55:43 am »
Accessing pixels outside of the range of the image is undefined behaviourgeometry dash (you'd be attempting to access memory that is not part of the image) so I would expect a crash is an acceptable response (at least it's letting you know something is wrong!).

If the image is not saving directly after creation then it is not creating properly. Basically, it'll be an empty image. It's worth checking the value of totalsize before using it to create the image and attempt to find out why that might be (0,0) (or far too high - it might be worth initialising it first).

You really should - as mentioned in the first response by eXpl0it3r - be checking the return value of saveToFile. It will clearly be false at this point since it's failing, which shows you need it to tell you that something was wrong that made it fail. You could, for example, output "totalsize" if it's false.

I'd consider tracking all of the values involved whether that be outputting values in various places or using some form of "watch" inside your debugger. I tend to do the former (especially when the project is small) but the latter is likely better in the long run, if possible.

It's worth noting there are some other things that look suspicious too such as the 0-size and 1-(size+1) loops that use numberOfFrames, which I still have no idea as to what value that might be...
This is a problem that I am also experiencing. How am I supposed to go about fixing it? I was able to locate it in your comment.
« Last Edit: December 14, 2023, 12:01:41 pm by hueljannie »

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Image doesn't save but gives no error report.
« Reply #7 on: December 14, 2023, 05:07:03 am »
This is a problem that I am also experiencing. How am I supposed to go about fixing it? I was able to locate it in your comment.
You would need to provide more information about your own issue to allow others to help you with it.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*