glut:can someone recommend a simple png loading lib for vc2010

Started by
5 comments, last by SHOKWAV 11 years, 4 months ago
hi

i would like someone recommending a simple png loading library that i do not have to build from source code, which i only have to link it to vc 2010.

i tried to build the freeImage, libpng and SOIL, all failed. i don't know what happened but my vc kept saying that the project is not available or the sln is corrupted, things like that. if you insisting that i use one of the library above, you will have to walk me thru the process of building the library.

thanks in advance
Advertisement
Why are you using png files? Maybe it is that your source is png, and you have a lot of them? But if that isn't the case, consider using simple bmp files instead. There is no reason to use png files for the compressed functionality. Your artists shouldn't use png files anyway, they should store the source in either gimp XCF format or Photoshop PSD.

  • When you load it into OpenGL, they have to be decompressed anyway (or using one of the OpenGL compressed formats).
  • If you compress images to save disk space, then it is probably an optimization that is not needed. Disk space is cheap today, and most applications don't have that high amounts of image data to make a difference.
  • If you want to minimize the size of the installation package, then the installer generators will do a compression that is just as good anyway.
  • If you worry about loading time from disk, then again, you can transform the images into one of the OpenGL compressed formats, and save that on disk.
  • Loading bmp is very easy. You can find source code that fits on one screen.
[size=2]Current project: Ephenation.
[size=2]Sharing OpenGL experiences: http://ephenationopengl.blogspot.com/
Hello,

I tend to just use libpng directly.

The following is a very simple example of loading the png into a byte array (ready for uploading to the graphics card)

http://zarb.org/~gc/html/libpng.html

If you would like a library that generates the OpenGL texture for you, then perhaps also have a look at OpenIL (DevIL).
http://tinyurl.com/shewonyay - Thanks so much for those who voted on my GF's Competition Cosplay Entry for Cosplayzine. She won! I owe you all beers :)

Mutiny - Open-source C++ Unity re-implementation.
Defile of Eden 2 - FreeBSD and OpenBSD binaries of our latest game.

I tend to just use libpng directly.

The following is a very simple example of loading the png into a byte array (ready for uploading to the graphics card)

http://zarb.org/~gc/html/libpng.html

If you would like a library that generates the OpenGL texture for you, then perhaps also have a look at OpenIL (DevIL).


hi Karsten_

thank for replying.

where do i get the unistd.h file? vc said it was not able to find it.

my problem is not know how to use it, it is how to link it to vs c++ 2010. i might be wrong, i still haven't read the file very carefully, yet. do i have to link the project to libpng.lib before using the program? if that is the case, i still don't know how to link it to libPNG. it would be nice if you could walk me through the process of compiling the libpng lib and building the libPNG.lib.
hi Karsten_

it seemed that the program you provided does not save it to a texture where i can use it to display it to the screen. it just save it to a file, it that right? maybe you did not know that i was using vc 2010 with opengl 3.3?
Although it does write to a file, you can just ignore this function.

Just have a look at read_png_file(char*); to learn how to read in the .png file and then perhaps take a look at the process_file(); function to see how to use the byte array.

If this still doesnt help, let me know and I will upload a wrapped version of this code from my project that should also generate the OpenGL texture for you.
http://tinyurl.com/shewonyay - Thanks so much for those who voted on my GF's Competition Cosplay Entry for Cosplayzine. She won! I owe you all beers :)

Mutiny - Open-source C++ Unity re-implementation.
Defile of Eden 2 - FreeBSD and OpenBSD binaries of our latest game.
Here's what I'm currently using, straight from libpng.


int Memento::_GetTextureInfo(int color_type){
switch(color_type){
case PNG_COLOR_TYPE_GRAY:
return (1);
break;
case PNG_COLOR_TYPE_GRAY_ALPHA:
return (2);
break;
case PNG_COLOR_TYPE_RGB:
return (3);
break;
case PNG_COLOR_TYPE_RGB_ALPHA:
return (4);
break;
default:
return (-1);
break;
};
return (-1);
}
unsigned Memento::_LoadImageIntoTexture(std::string const& file, unsigned& width, unsigned& height,
bool clamp_texture){
png_struct* img_read_struct = NULL;
png_info* img_info_struct = NULL;
png_byte img_sig[PNG_SIG_SIZE], *img_pixels = NULL, **img_row_ptrs = NULL;
std::FILE* img_file = NULL;
int img_bit_depth, img_color_type, img_components;
unsigned tex, gl_colors, img_width, img_height;
if(!(img_file = std::fopen(file.c_str(), "rb")))return (PNG_READ_FAILURE);
std::fread(img_sig, PNG_SIG_SIZE, sizeof(png_byte), img_file);
if(png_sig_cmp(img_sig, 0, PNG_SIG_SIZE) != 0){
std::fclose(img_file);
return (PNG_READ_FAILURE);
}
std::rewind(img_file);
if(!(img_read_struct = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL))){
std::fclose(img_file);
return (PNG_READ_FAILURE);
}
if(!(img_info_struct = png_create_info_struct(img_read_struct))){
png_destroy_read_struct(&img_read_struct, NULL, NULL);
std::fclose(img_file);
return (PNG_READ_FAILURE);
}
if(setjmp(png_jmpbuf(img_read_struct))){
png_destroy_read_struct(&img_read_struct, &img_info_struct, NULL);
std::fclose(img_file);
return (PNG_READ_FAILURE);
}
png_init_io(img_read_struct, img_file);
png_read_info(img_read_struct, img_info_struct);
img_bit_depth = png_get_bit_depth(img_read_struct, img_info_struct);
img_color_type = png_get_color_type(img_read_struct, img_info_struct);
if(img_color_type == PNG_COLOR_TYPE_PALETTE)
png_set_palette_to_rgb(img_read_struct);
if(img_color_type == PNG_COLOR_TYPE_GRAY && img_bit_depth < 8){
png_set_expand_gray_1_2_4_to_8(img_read_struct);
}
if(png_get_valid(img_read_struct, img_info_struct, PNG_INFO_tRNS))
png_set_tRNS_to_alpha(img_read_struct);
if(img_bit_depth == 16){
png_set_strip_16(img_read_struct);
}else if(img_bit_depth < 8){
png_set_packing(img_read_struct);
}
png_read_update_info(img_read_struct, img_info_struct);
png_get_IHDR(img_read_struct, img_info_struct, &img_width,
&img_height, &img_bit_depth, &img_color_type, NULL, NULL, NULL);
if((img_components = Memento::_GetTextureInfo(img_color_type)) == -1){
png_destroy_read_struct(&img_read_struct, &img_info_struct, NULL);
std::fclose(img_file);
return (PNG_READ_FAILURE);
}
img_pixels = (png_byte*)malloc(sizeof(png_byte) * (img_width * img_height * img_components));
img_row_ptrs = (png_byte**)malloc(sizeof(png_byte*) * img_height);
for(unsigned i = 0; i < img_height; ++i)
img_row_ptrs = (png_byte*)(img_pixels + (i * img_width * img_components));
png_read_image(img_read_struct, img_row_ptrs);
png_read_end(img_read_struct, NULL);
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
if(clamp_texture){
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
}
(img_components == 4)? (gl_colors = GL_RGBA):(0);
(img_components == 3)? (gl_colors = GL_RGB):(0);
(img_components == 2)? (gl_colors = GL_LUMINANCE_ALPHA):(0);
(img_components == 1)? (gl_colors = GL_LUMINANCE):(0);
glTexImage2D(GL_TEXTURE_2D, 0, img_components, img_width,
img_height, 0, gl_colors, GL_UNSIGNED_BYTE, img_pixels);
glBindTexture(GL_TEXTURE_2D, 0);
png_destroy_read_struct(&img_read_struct, &img_info_struct, NULL);
std::fclose(img_file);
free(img_row_ptrs);
free(img_pixels);
width = img_width;
height = img_height;
return (tex);
}

This topic is closed to new replies.

Advertisement