Back to index

extremetuxracer  0.5beta
Public Member Functions | Static Public Member Functions | Public Attributes | Private Member Functions
pp::ReaderPNG Class Reference

#include <png_reader.h>

Inheritance diagram for pp::ReaderPNG:
Inheritance graph
[legend]
Collaboration diagram for pp::ReaderPNG:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 ReaderPNG (const char *fileName)
 ~ReaderPNG ()
bool writeToFile (const char *fileName) const

Static Public Member Functions

static ImagereadFile (const char *fileName)

Public Attributes

unsigned short width
unsigned short height
unsigned char depth
unsigned char * data

Private Member Functions

void loadData (png_structp png_ptr, int depth)
 loads rgb or rgba into the data buffer

Detailed Description

Definition at line 28 of file png_reader.h.


Constructor & Destructor Documentation

pp::ReaderPNG::ReaderPNG ( const char *  fileName)

Definition at line 24 of file png_reader.cpp.

{
       png_byte header[4]; 
       
       data = NULL; 
       
       FILE *fp = fopen(fileName, "rb");
    if (!fp){
              return;
    }
       
    fread(header, 1, 4, fp);
    bool is_png = !png_sig_cmp(header, 0, 4);
       if (!is_png)
    {
              fclose(fp);
        return;
       }
              
    png_structp png_ptr = png_create_read_struct
       (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
              
    if (!png_ptr){
              fclose(fp);
        return;
       }

       
    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);
              fclose(fp);
        return;
    }

    png_infop end_info = png_create_info_struct(png_ptr);
    if (!end_info)
    {
        png_destroy_read_struct(&png_ptr, &info_ptr,
          (png_infopp)NULL);
              fclose(fp);
        return;
    }

       png_init_io(png_ptr, fp);
       png_set_sig_bytes(png_ptr, 4);
       
       png_read_info(png_ptr, info_ptr);
              
       png_uint_32 width, height;
       int bit_depth, color_type, interlace_type;
       
       
       png_get_IHDR(png_ptr, info_ptr, &width, &height,
       &bit_depth, &color_type, &interlace_type,int_p_NULL, int_p_NULL);
       
       if(bit_depth == 16)
        png_set_strip_16(png_ptr);
       
       if(color_type == PNG_COLOR_TYPE_PALETTE &&
        bit_depth <= 8)
       {
              png_set_expand(png_ptr);
              png_read_update_info(png_ptr, info_ptr);
              png_get_IHDR(png_ptr, info_ptr, &width, &height,
                        &bit_depth, &color_type, &interlace_type,int_p_NULL, int_p_NULL);
       }
       
       if( color_type == PNG_COLOR_TYPE_GRAY ||
              color_type == PNG_COLOR_TYPE_GRAY_ALPHA ){
              png_set_gray_to_rgb(png_ptr);
              png_read_update_info(png_ptr, info_ptr);
              png_get_IHDR(png_ptr, info_ptr, &width, &height,
                        &bit_depth, &color_type, &interlace_type,int_p_NULL, int_p_NULL);
       }
              
       this->width=width;
       this->height=height;
                     
       if ( color_type==PNG_COLOR_TYPE_RGB){
              depth=3;
              loadData(png_ptr, 3);
       } else if ( color_type==PNG_COLOR_TYPE_RGB_ALPHA){
              depth=4;
              loadData(png_ptr, 4);
       } else {
              fflush(stdout);                    
              data = NULL;
       }
       
       png_read_end(png_ptr, info_ptr);
       png_destroy_read_struct(&png_ptr, &info_ptr, png_infopp_NULL);
       fclose(fp);
}

Here is the call graph for this function:

Definition at line 135 of file png_reader.cpp.

{
       if(data!=NULL) delete(data);
}

Member Function Documentation

void pp::ReaderPNG::loadData ( png_structp  png_ptr,
int  depth 
) [private]

loads rgb or rgba into the data buffer

Definition at line 122 of file png_reader.cpp.

{
       png_bytep *row_pointers = new png_bytep[height];
       data = new unsigned char[width*height*depth];
              
       for (unsigned int i=0; i<height; i++)
              row_pointers[height-1-i]=data + i*width*depth;
       
       png_read_image(png_ptr, row_pointers);
       delete [] row_pointers;
}

Here is the caller graph for this function:

Image * pp::Image::readFile ( const char *  fileName) [static, inherited]

Definition at line 54 of file image.cpp.

{
       int len = strlen(fileName);
       
       // PNG Image?
       if(!strcmp(fileName+len-3,"png")){
              Image* image=new ReaderPNG(fileName);
              
              // check if the image contains valid data
              if(image->data!=NULL){
                     return image;        
              } else {
                     delete image;
                     return NULL;
              }
       }else{
              // try it as rgb (should we check for this first?)
              Image* image=new ReaderRGB(fileName);
              return image;        
       }
}

Here is the caller graph for this function:

bool pp::Image::writeToFile ( const char *  fileName) const [inherited]

Definition at line 77 of file image.cpp.

{
       WriterPPM writer(fileName,*this);
       
       return true;
}

Here is the caller graph for this function:


Member Data Documentation

unsigned char* pp::Image::data [inherited]

Definition at line 32 of file image.h.

unsigned char pp::Image::depth [inherited]

Definition at line 31 of file image.h.

unsigned short pp::Image::height [inherited]

Definition at line 30 of file image.h.

unsigned short pp::Image::width [inherited]

Definition at line 30 of file image.h.


The documentation for this class was generated from the following files: