#include "stdafx.h" #if 0 #include #include #include #include #endif #include "PngJpegImage.h" PngJpegImage::PngJpegImage() { initnull(); } PngJpegImage::~PngJpegImage() { freeself(); } bool PngJpegImage::readFile_PNG( const char *fileName ) { if( !fileName ) return FALSE; // try to open file 1st FILE *fp = fopen( fileName, "rb" ); if( !fp ) { SetLastError( ERROR_FILE_NOT_FOUND ); return FALSE; } // check is it png? fread( b_header, 1, number, fp ); int is_png = !png_sig_cmp( b_header, 0, number ); if( !is_png ) { SetLastError( ERROR_INVALID_DATA ); fclose( fp ); return FALSE; } memset( m_filename, 0, sizeof(m_filename) ); strncpy( m_filename, fileName, sizeof(m_filename)-1 ); // now free possible previous data freeself(); // and read new file // allocate libPNG structs png_ptr = png_create_read_struct( PNG_LIBPNG_VER_STRING, NULL, NULL, NULL ); if( !png_ptr ) { SetLastError( ERROR_NOT_ENOUGH_MEMORY ); png_ptr = NULL; fclose( fp ); return FALSE; } info_ptr = png_create_info_struct( png_ptr ); if( !info_ptr ) { png_destroy_read_struct( &png_ptr, (png_infopp)NULL, (png_infopp)NULL ); SetLastError( ERROR_NOT_ENOUGH_MEMORY ); png_ptr = NULL; info_ptr = NULL; fclose( fp ); return FALSE; } png_init_io( png_ptr, fp ); png_set_sig_bytes( png_ptr, number ); png_read_info( png_ptr, info_ptr ); width = png_get_image_width( png_ptr, info_ptr ); height = png_get_image_height( png_ptr, info_ptr ); bit_depth = png_get_bit_depth( png_ptr, info_ptr ); rowbytes = png_get_rowbytes( png_ptr, info_ptr ); channels = png_get_channels( png_ptr, info_ptr ); color_type = png_get_color_type( png_ptr, info_ptr ); /** // color types. Note that not all combinations are legal #define PNG_COLOR_TYPE_GRAY 0 #define PNG_COLOR_TYPE_PALETTE (PNG_COLOR_MASK_COLOR | PNG_COLOR_MASK_PALETTE) #define PNG_COLOR_TYPE_RGB (PNG_COLOR_MASK_COLOR) #define PNG_COLOR_TYPE_RGB_ALPHA (PNG_COLOR_MASK_COLOR | PNG_COLOR_MASK_ALPHA) #define PNG_COLOR_TYPE_GRAY_ALPHA (PNG_COLOR_MASK_ALPHA) // aliases #define PNG_COLOR_TYPE_RGBA PNG_COLOR_TYPE_RGB_ALPHA #define PNG_COLOR_TYPE_GA PNG_COLOR_TYPE_GRAY_ALPHA **/ // allocate space for image data row_pointers = (unsigned char **)png_malloc( png_ptr, height*sizeof(png_bytep) ); if( !row_pointers ) { png_destroy_read_struct( &png_ptr, &info_ptr, NULL ); SetLastError( ERROR_NOT_ENOUGH_MEMORY ); png_ptr = NULL; info_ptr = NULL; fclose( fp ); return FALSE; } int i = 0; for( i=0; i< // TODO: other color types or set translations while reading for( i=0; i< } } else if( image_is_jpeg ) { if( jpeg_cinfo.out_color_space == JCS_RGB ) { crColor = RGB( p[i][j*3], p[i][j*3+1], p[i][j*3+2] ); } } SetPixel( hdc, x0+j, y0+i, crColor ); } } return TRUE; } bool PngJpegImage::getParams( int *w, int *h, int *bpp, int *ch ) const { if( w ) *w = width; if( h ) *h = height; if( bpp ) *bpp = bit_depth; if( ch ) *ch = channels; //if( ctype ) *ctype = color_type; return TRUE; } bool PngJpegImage::getFileName( char *out, size_t cchMax ) const { if( !out || (cchMax < 1) ) return FALSE; strncpy( out, m_filename, cchMax ); return TRUE; } void PngJpegImage::initnull() { number = 6; memset( b_header, 0, sizeof(b_header) ); m_filename[0] = 0; png_ptr = NULL; info_ptr = NULL; width = height = bit_depth = rowbytes = channels = 0; // image data row_pointers = NULL; // image_is_png = image_is_jpeg = 0; } void PngJpegImage::freeself() { if( image_is_png ) { int i = 0; if( row_pointers ) { for( i=0; i