Gl Graph

Gl Graph Source of gltexture.cpp


#include "gltexture.h"
 
 
#ifndef NO_PNG
#include <png.h>
/**
 * error function handling
 */
 
void user_error_ptr()
{
    printf("user_error_ptr\n");
    //return 0;
}
void user_error_fn(png_struct_def*, const char* s)
{
    printf("user_error_fn :%s\n",s);
}
void user_warning_fn(png_struct_def*, const char* s)
{
    printf("user_warning_fn:%s\n",s);
}
 
#endif
 
 
GlTexture::GlTexture(std::string filename)
{
    readPng(filename);
 
    // Create one OpenGL texture
    glGenTextures(1, &textureID);
    // "Bind" the newly created texture : all future texture functions will modify this texture
    glBindTexture(GL_TEXTURE_2D, textureID);
    // Give the image to OpenGL
    glTexImage2D(GL_TEXTURE_2D, 0,GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
 
 
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
}
 
 
void GlTexture::printGlError(const char *msg)
{
    if (true)
    {
        GLenum err=glGetError();
        if (err==GL_NO_ERROR) return;
        else if (err==GL_INVALID_ENUM) printf("%s :GL_INVALID_ENUM\n",msg);
        else if (err==GL_INVALID_VALUE) printf("%s :GL_INVALID_VALUE\n",msg);
        else if (err==GL_INVALID_OPERATION) printf("%s :GL_INVALID_OPERATION\n",msg);
        else if (err==GL_INVALID_FRAMEBUFFER_OPERATION) printf("%s :GL_INVALID_FRAMEBUFFER_OPERATION\n",msg);
        else if (err==GL_OUT_OF_MEMORY) printf("%s :GL_OUT_OF_MEMORY\n",msg);
        else if (err==GL_STACK_UNDERFLOW) printf("%s :GL_STACK_UNDERFLOW\n",msg);
        else if (err==GL_STACK_OVERFLOW) printf("%s :GL_STACK_OVERFLOW\n",msg);
        else printf("%s :ERR %i\n",msg,err);
    }
}
 
 
 
 
 
void GlTexture::readPng(std::string filename)
{
#ifdef  NO_PNG
    printf("PNG not activated");
#else
    FILE *fp = fopen(filename.c_str(), "r");
    if (!fp)
    {
        printf(" fopen failed");
        return;
    }
 
    unsigned char header[8];
    fread(header, 1, 8, fp);
    if (png_sig_cmp(header, 0, 8))
        printf("%s do not look like a png image file",filename.c_str());
 
    /* initialize stuff */
    png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, (png_voidp)user_error_ptr,user_error_fn, user_warning_fn);
 
    if (!png_ptr)
    {
        printf(" png_ptr failed");
        fclose(fp);
        return;
    }
 
 
    png_infop info_ptr = png_create_info_struct(png_ptr);
    if (!info_ptr)
    {
        printf("png_create_info_struct failed");
        fclose(fp);
        return;
    }
 
    png_init_io(png_ptr, fp);
    png_set_sig_bytes(png_ptr, 8);
 
    png_read_info(png_ptr, info_ptr);
 
    this->width = png_get_image_width(png_ptr, info_ptr);
    this->height = png_get_image_height(png_ptr, info_ptr);
    png_byte color_type = png_get_color_type(png_ptr, info_ptr);
    png_byte bit_depth = png_get_bit_depth(png_ptr, info_ptr);
 
    if (color_type==PNG_COLOR_TYPE_RGB)
        this->pixelsize=3;
    else if (color_type==PNG_COLOR_TYPE_RGBA)
        this->pixelsize=4;
    else
    {
        printf("wrong color_type");
        fclose(fp);
        return;
    }
    //fill row_pointers
    png_byte ** row_pointers=new png_byte* [this->height];
    unsigned int row_size=png_get_rowbytes(png_ptr,info_ptr);
    for (unsigned int i=0; i<this->height; i++)
        row_pointers[i] = new png_byte [row_size];
 
    png_read_image(png_ptr, row_pointers);
 
    //fill data
    unsigned int imageSize=this->width*this->height*this->pixelsize;
    data= new unsigned char [ imageSize];
 
    for (unsigned int i=0; i<this->height; i++)
        for (unsigned int j=0; j<this->width; j++)
            for (unsigned int k=0; k<this->pixelsize; k++)
                data[i*this->width*this->pixelsize+j*this->pixelsize+k]=row_pointers[i][j*this->pixelsize+k];
    for (unsigned int i=0; i<this->height; i++)
        delete row_pointers[i];
 
    delete row_pointers;
 
    fclose(fp);
#endif
}