Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


We're also offering banner ads on our site from just $5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


#ActualSHOKWAV

Posted 08 December 2012 - 08:36 PM

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[i] = (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);
}

#1SHOKWAV

Posted 08 December 2012 - 08:35 PM

Here's what I'm currently using.

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[i] = (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);
}

PARTNERS