Sign in to follow this  
39ster

SOIL issues (images that arn't power of 2)

Recommended Posts

I'm using the SOIL image loading library and i'm having issues with image files whos resolution isn't a power of two. The images are drawn a little bit blurry. The code i'm using to load the images specifies the "SOIL_FLAG_POWER_OF_TWO".
Image* LoadImage2(const std::string& fileName)
{
    int width, height, channels;
    unsigned char *ht_map = SOIL_load_image
	(
		fileName.c_str(),
		&width, &height, &channels,
		SOIL_LOAD_AUTO
	);

    if(ht_map)
    {
        GLuint texture = SOIL_create_OGL_texture
        (
            ht_map,
            width, height, channels,
            SOIL_CREATE_NEW_ID,
            SOIL_FLAG_POWER_OF_TWO
        );

        SOIL_free_image_data( ht_map );

        if(texture)
            return new Image(texture, width, height);
    }
	return NULL;
}

...

void Graphics::DrawImage(Image* image, float x, float y, int left, int top, int width, int height, SDL_Rect& view)
{
    float textureWidth = float(left + width) / image->GetWidth();
    float textureHeight = float(top + height) / image->GetHeight();
    float textureX = float(left) / image->GetWidth();
    float textureY = float(top) / image->GetHeight();

    glPushMatrix();

    glLoadIdentity();
    glTranslatef(int(x) - view.x, int(y) - view.y, 0);
    glScalef(width, height, 1);

    glBindTexture( GL_TEXTURE_2D, image->GetTexture()) ;
    glBegin( GL_QUADS ) ;
        glTexCoord2f(textureX, textureY); glVertex2f(0, 0);
        glTexCoord2f(textureWidth, textureY); glVertex2f(1, 0);
        glTexCoord2f(textureWidth, textureHeight); glVertex2f(1, 1);
        glTexCoord2f(textureX, textureHeight); glVertex2f(0, 1);
    glEnd() ;
    glPopMatrix();
}
Anyone know the reason?

Share this post


Link to post
Share on other sites
I don't know SOIL myself, from what I understand, you're loading a non-power of 2 image, and SOIL is resizing the image to be a power of two, right?

I'd say the blurriness is just an artifact of the re-scaling.

Share this post


Link to post
Share on other sites
Oh it stretches it. Anyways i figured out a way to fix it. This one doesn't use the SOIL power flag. It will create a new surface that is the correct resolution and copy the old one ontop of it (leaving the extra space blank). When it comes to rendering the surface, you only render the original part.

static int RoundPower(int value)
{
int powValue = 1;

while(powValue < value)
powValue = powValue << 1;
return powValue;
}

Image* LoadImage2(const std::string& fileName)
{
int width, height, channels;
unsigned char *ht_map = SOIL_load_image
(
fileName.c_str(),
&width, &height, &channels,
SOIL_LOAD_AUTO
);

if(ht_map)
{
unsigned char* imageData = ht_map;
int upperWidth = RoundPower(width);
int upperHeight = RoundPower(height);

if(upperWidth != width || upperHeight != height)
{
unsigned char* newImageData = new unsigned char[upperWidth * upperHeight * channels];

for(int y = 0; y < height; ++y)
memcpy(&newImageData[y * upperWidth * channels], &ht_map[y * width * channels], width * channels);

free(imageData);
imageData = newImageData;
}

GLuint texture = SOIL_create_OGL_texture
(
imageData,
upperWidth, upperHeight, channels,
SOIL_CREATE_NEW_ID,
0
);

free(imageData);

if(texture)
return new Image(texture, width, height, upperWidth, upperHeight);
}
return NULL;
}


void Graphics::DrawImage(Image* image, float x, float y, int left, int top, int width, int height, SDL_Rect& view)
{
float textureWidth = float(left + width) / image->GetUpperWidth();
float textureHeight = float(top + height) / image->GetUpperHeight();
float textureX = float(left) / image->GetUpperWidth();
float textureY = float(top) / image->GetUpperHeight();

glPushMatrix();

glLoadIdentity();
glTranslatef(int(x) - view.x, int(y) - view.y, 0);
glScalef(width, height, 1);

glBindTexture( GL_TEXTURE_2D, image->GetTexture()) ;
glBegin( GL_QUADS ) ;
glTexCoord2f(textureX, textureY); glVertex2f(0, 0);
glTexCoord2f(textureWidth, textureY); glVertex2f(1, 0);
glTexCoord2f(textureWidth, textureHeight); glVertex2f(1, 1);
glTexCoord2f(textureX, textureHeight); glVertex2f(0, 1);
glEnd() ;
glPopMatrix();
}


Incase anyone else has this problem.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this