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

Author Topic: If Image.Create can only work in powers of two, why...  (Read 3639 times)

0 Members and 1 Guest are viewing this topic.

Joshua Flynn

  • Full Member
  • ***
  • Posts: 133
    • View Profile
If Image.Create can only work in powers of two, why...
« on: April 26, 2011, 02:09:46 pm »
...Can I set a render window size to 755 by 3, and load an image that is 173 by 371?

I don't understand why creating has to conform to the powers of two, but already created images that are loaded and render windows, don't?

Either the graphics card can render non-powers of two, or it can't. But it cannot logically be both or neither.

I really need to be able to create images that are a non-power of two to be able to properly render a graph.

So my help question is thus:

What part of the SFML code do I alter and recompile to disable the power of two lock that appears to be completely unnecessary?

Or, alternately;

How do I create a non-power of two image.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
If Image.Create can only work in powers of two, why...
« Reply #1 on: April 26, 2011, 02:30:30 pm »
SFML handles non-power-of-two dimensions automatically, so you shouldn't have any problem.

Show me your code and I'll try to find out why you fail to create a NPOT image.
Laurent Gomila - SFML developer

Joshua Flynn

  • Full Member
  • ***
  • Posts: 133
    • View Profile
If Image.Create can only work in powers of two, why...
« Reply #2 on: April 26, 2011, 02:35:41 pm »
Quote from: "Laurent"
SFML handles non-power-of-two dimensions automatically, so you shouldn't have any problem.

Show me your code and I'll try to find out why you fail to create a NPOT image.


I had tried to develop a work around to use powers of two, but unfortunately, it's really inefficient for disk sectors (files are smaller than the smallest sector and waste space - there could be thousands of them if I use the power of two method - and I don't have the memory to spare).

Here's a basic test version:
Code: [Select]
#include <iostream>
#include <SFML/Graphics.hpp>

using namespace std;

int main()
{
    sf::Image Test;
    Test.Create(2400,500,sf::Color(0,0,0));
    //Test.LoadFromFile("Test.jpg");

    sf::Sprite Sprite;

    Sprite.SetImage(Test);
    Sprite.SetPosition(0,0);

    sf::RenderWindow App(sf::VideoMode(800,400,32),"Graphics");

    while(App.IsOpened())
    {
        sf::Event Event;
        while(App.GetEvent(Event))
        {
            if(Event.Type == sf::Event::Closed)
            {
                App.Close();
            }
        }
        App.Clear();


        App.Draw(Sprite);
        App.Display();
    }

    return 0;
}


Error output is:
"Failed to create image, its internal image size is too high (4096x512) "

The thing is, I don't want a power of two, I want to be able to create the precise dimension in the same way I can load and render an image with the precise dimensions. [Test.jpg is a non-power of two image that loads without issues.]

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
If Image.Create can only work in powers of two, why...
« Reply #3 on: April 26, 2011, 02:52:38 pm »
It's not a power-of-two problem. Like the message says, the size is too high for your graphics card. You see power of two numbers because SFML internally adjusts the dimensions when the graphics cards requires them to be power of two.
Laurent Gomila - SFML developer

Joshua Flynn

  • Full Member
  • ***
  • Posts: 133
    • View Profile
If Image.Create can only work in powers of two, why...
« Reply #4 on: April 26, 2011, 02:56:34 pm »
Quote from: "Laurent"
It's not a power-of-two problem. Like the message says, the size is too high for your graphics card. You see power of two numbers because SFML internally adjusts the dimensions when the graphics cards requires them to be power of two.


But that is why I said was able to set the render window to non-powers of two like 755 by 3 and load images (and render them) of 173 by 371.

It makes no sense my graphics card can't render powers of two, when it clearly, and quite obviously, does!

I could technically bypass (the unnecessary power of two lock) by simply loading up an image with a non-power of two, then setting pixels on it, and saving it.

Joshua Flynn

  • Full Member
  • ***
  • Posts: 133
    • View Profile
If Image.Create can only work in powers of two, why...
« Reply #5 on: April 26, 2011, 02:59:26 pm »
Look! The bypass even works!

I can edit and save a non-power of two image. Why can I not 'create' one?

Code: [Select]
#include <iostream>
#include <SFML/Graphics.hpp>

using namespace std;

int main()
{
    sf::Image Test;
    //Test.Create(2400,500,sf::Color(0,0,0));
    Test.LoadFromFile("Test.jpg");

    sf::Sprite Sprite;

    Sprite.SetImage(Test);
    Sprite.SetPosition(0,0);

    int X = 0;
    while(X < Test.GetWidth())
    {
        Test.SetPixel(X,0,sf::Color());
        X++;
    }

    sf::RenderWindow App(sf::VideoMode(800,400,32),"Graphics");

    while(App.IsOpened())
    {
        sf::Event Event;
        while(App.GetEvent(Event))
        {
            if(Event.Type == sf::Event::Closed)
            {
                App.Close();
            }
        }
        App.Clear();


        App.Draw(Sprite);
        App.Display();
    }

    Test.SaveToFile("NonPowerOfTwo.jpg");
    return 0;
}

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
If Image.Create can only work in powers of two, why...
« Reply #6 on: April 26, 2011, 03:14:13 pm »
You didn't understand me. Please calm down and forget about this power-of-two thing.

Your only problem is that the size of your image (2400) is too high for your graphics card. It's probably limited to 2048 or 1024.

Call "Test.Create(713, 317)", it will work.
Laurent Gomila - SFML developer

Joshua Flynn

  • Full Member
  • ***
  • Posts: 133
    • View Profile
If Image.Create can only work in powers of two, why...
« Reply #7 on: April 26, 2011, 03:21:13 pm »
Quote from: "Laurent"
You didn't understand me. Please calm down and forget about this power-of-two thing.

Your only problem is that the size of your image (2400) is too high for your graphics card. It's probably limited to 2048 or 1024.

Call "Test.Create(713, 317)", it will work.


So logically it's possible for a non power of two so long as it doesn't exceed size?

Okay, that's good. Thank you for the help so far!

One last question:

Why does the image rendering only display white for the image but no colour/details?

And you've done great work with SFML by the way - very useful.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
If Image.Create can only work in powers of two, why...
« Reply #8 on: April 26, 2011, 03:23:55 pm »
Quote
So logically it's possible for a non power of two so long as it doesn't exceed size?

Absolutely.

Quote
Why does the image rendering only display white for the image but no colour/details?

With the piece of code above, or another one?

Quote
And you've done great work with SFML by the way - very useful.

Thanks ;)
Laurent Gomila - SFML developer

Joshua Flynn

  • Full Member
  • ***
  • Posts: 133
    • View Profile
If Image.Create can only work in powers of two, why...
« Reply #9 on: April 26, 2011, 03:26:58 pm »
Quote from: "Laurent"

With the piece of code above, or another one?


With the code above, although I'd presume the same for the code (the other code hasn't yet got rendering whilst I'm testing out any assumptions to avoid bugs).

How would I find out maximum dimensions? What is the smallest dimension a machine could have?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
If Image.Create can only work in powers of two, why...
« Reply #10 on: April 26, 2011, 03:47:43 pm »
Quote
How would I find out maximum dimensions?

With SFML 1, you can't. This feature has been added to SFML 2.

Quote
What is the smallest dimension a machine could have?

512x512 is a safe minimum.
Laurent Gomila - SFML developer