Jump to content
  • Advertisement
Sign in to follow this  
guitarstar26

How to find the dimensions of an image(png) using C++ and DX9

This topic is 2982 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello,

I'm looking for a simple function that can read the image dimensions of a .png file. Currently I am creating a texture from the image and reading those dimensions, but some hardware doesn't support non power of 2 texture sizes and thus reports dimensions of 512x512 for something that is 350x400. This causes major flaws in my program.

P.S. Is it good practice to always use power of 2 textures and stretch them to get the desired size?

Thank you for reading


EDIT: I found a possible solution.

D3DXGetImageInfoFromFile()
or
D3DXGetImageInfoFromResource()

both seem to fit exactly what I need.

[Edited by - guitarstar26 on August 21, 2010 5:36:40 AM]

Share this post


Link to post
Share on other sites
Advertisement

void CPng::Read ( std::istream& Input )
{
errno = 0;
this->Input = &Input;

//streampos Pos = Input.tellg();

// ---------- Check signature
png_byte Header[ 8 ];
memset( Header, 0, sizeof Header );

Input.read( reinterpret_cast<char*>( Header ), sizeof( Header ) );

if ( png_sig_cmp( Header, 0, sizeof Header ) )
{
throw CInfoException( __FILE__, __LINE__, "Error reading PNG header" );
}

png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, (png_voidp)0, 0, 0 );
if( !png_ptr ) throw CInfoException( __FILE__, __LINE__, "Error creating png read struct" );
png_set_read_fn ( png_ptr, reinterpret_cast<png_voidp>( this ), user_read_data );
png_set_error_fn ( png_ptr, reinterpret_cast<png_voidp>( this ), user_error_fn, user_warning_fn );

png_infop info_ptr = png_create_info_struct(png_ptr);
if( !info_ptr )
{
png_destroy_read_struct( &png_ptr, (png_infopp)NULL, (png_infopp)NULL );
throw CInfoException( __FILE__, __LINE__, "Error PNG creating info struct" );
}

png_infop end_info = png_create_info_struct( png_ptr );
if (!end_info)
{
png_destroy_read_struct( &png_ptr, &info_ptr, (png_infopp)NULL );
throw CInfoException( __FILE__, __LINE__, "Error PNG creating end info struct" );
}

png_set_sig_bytes(png_ptr, sizeof Header );

try
{
png_read_info(png_ptr, info_ptr);


// -- png_read_png( png_ptr, info_ptr, PNG_TRANSFORM_IDENTITY, NULL );

if( png_ptr->bit_depth != 8 )
{
ErrorLog << "Sorry only support 32 bit images (RGB(A()" << End;
throw CInfoException( __FILE__, __LINE__, "Sorry only support 32 bit images (RGB(A))" );
}
/*
if( png_ptr->color_type == PNG_COLOR_TYPE_RGB )
{

}
*/

if( png_ptr->color_type != PNG_COLOR_TYPE_RGB_ALPHA &&
png_ptr->color_type != PNG_COLOR_TYPE_RGB
)
{
ErrorLog << "Sorry only support RGBA images" << End;
throw CInfoException( __FILE__, __LINE__, "Sorry only support RGBA images" );
}
Height = info_ptr->height;
Width = info_ptr->width;

Share this post


Link to post
Share on other sites
Quote:
Original post by guitarstar26
P.S. Is it good practice to always use power of 2 textures and stretch them to get the desired size?


Depending on what tools you are using, making all your textures - i.e., the actual underlying image files - powers of 2 may be a good idea, in order to avoid blurring from resampling. Do not stretch the images yourself, because that is doing yourself the thing that you want to avoid :) Instead, pad the images with empty space, and adjust the texture coordinates accordingly. Now there is only one transformation step, when the texture is applied to the surface.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!