PALETTE * gr_read_pal( file * fp ) { PALETTE * pal; int i; uint8_t colors[256*3] ; if ( !file_read( fp, colors, sizeof( colors ) ) ) return NULL ; for ( i = 0; i < 768; i++ ) colors[i] <<= 2; #ifdef COLORSPACE_BGR for ( i = 0; i < 768; i+=3 ){ uint8_t temp_color =colors[i]; colors[i]=colors[i+2]; colors[i+2]=temp_color; } #endif pal = pal_new_rgb(( uint8_t * )colors ); pal_refresh( pal ); if ( !sys_pixel_format->palette ) { sys_pixel_format->palette = pal_new( pal ); /* pal_use( sys_pixel_format->palette ); */ palette_changed = 1 ; } return pal ; }
GRAPH * gr_read_png( const char * filename ) { GRAPH * bitmap ; unsigned int n, x ; uint16_t * ptr ; uint32_t * ptr32 ; uint32_t * orig ; uint32_t * row ; uint32_t Rshift, Gshift, Bshift ; uint32_t Rmask, Gmask, Bmask ; png_bytep * rowpointers ; png_structp png_ptr ; png_infop info_ptr, end_info ; png_uint_32 width, height, rowbytes; int depth, color ; /* Abre el fichero y se asegura de que screen está inicializada */ file * png = file_open( filename, "rb" ) ; if ( !png ) return NULL; /* Prepara las estructuras internas */ png_ptr = png_create_read_struct( PNG_LIBPNG_VER_STRING, 0, 0, 0 ) ; if ( !png_ptr ) { file_close( png ); return NULL; } info_ptr = png_create_info_struct( png_ptr ) ; end_info = png_create_info_struct( png_ptr ) ; if ( !info_ptr || !end_info ) { png_destroy_read_struct( &png_ptr, &info_ptr, &end_info ) ; file_close( png ) ; return NULL; } /* Rutina de error */ #if (PNG_LIBPNG_VER>=10500) if ( setjmp( png_jmpbuf( png_ptr ) ) ) #else if ( setjmp( png_ptr->jmpbuf ) ) #endif { png_destroy_read_struct( &png_ptr, &info_ptr, &end_info ) ; file_close( png ) ; return NULL; } /* Recupera información sobre el PNG */ png_set_read_fn( png_ptr, png, user_read_data ) ; png_read_info( png_ptr, info_ptr ) ; png_get_IHDR( png_ptr, info_ptr, &width, &height, &depth, &color, 0, 0, 0 ) ; row = malloc( sizeof( uint32_t ) * width ); if ( !row ) { png_destroy_read_struct( &png_ptr, &info_ptr, &end_info ) ; file_close( png ) ; return NULL; } rowpointers = malloc( sizeof( png_bytep ) * height ); if ( !rowpointers ) { png_destroy_read_struct( &png_ptr, &info_ptr, &end_info ) ; free( row ) ; file_close( png ) ; return NULL; } /* Configura los distintos modos disponibles */ /* if ( ( color == PNG_COLOR_TYPE_GRAY && depth == 1 ) || color == PNG_COLOR_TYPE_GRAY_ALPHA ) { png_set_gray_to_rgb( png_ptr ); if ( color == PNG_COLOR_TYPE_GRAY ) png_set_filler( png_ptr, 0xFF, PNG_FILLER_AFTER ) ; } */ if ( depth == 16 ) png_set_strip_16( png_ptr ) ; if ( color == PNG_COLOR_TYPE_RGB ) png_set_filler( png_ptr, 0xFF, PNG_FILLER_AFTER ) ; png_set_bgr( png_ptr ) ; /* Recupera el fichero, convirtiendo a 16 bits si es preciso */ rowbytes = png_get_rowbytes( png_ptr, info_ptr ) ; bitmap = bitmap_new( 0, width, height, ( color == PNG_COLOR_TYPE_GRAY ) ? depth : ( color == PNG_COLOR_TYPE_PALETTE ) ? 8 : ( sys_pixel_format->depth == 16 ? 16 : 32 ) ) ; if ( !bitmap ) { png_destroy_read_struct( &png_ptr, &info_ptr, &end_info ) ; free( rowpointers ) ; free( row ) ; file_close( png ) ; return NULL; } if ( color == PNG_COLOR_TYPE_GRAY ) { for ( n = 0 ; n < height ; n++ ) rowpointers[n] = (( uint8_t* )bitmap->data ) + n * bitmap->pitch ; png_read_image( png_ptr, rowpointers ) ; if ( depth == 8 ) { uint8_t colors[256 * 3]; uint8_t * p = colors; for ( n = 0; n < 256 ; n++ ) { * p++ = n; * p++ = n; * p++ = n; } bitmap->format->palette = pal_new_rgb(( uint8_t * )colors ); pal_refresh( bitmap->format->palette ); } // png_read_rows( png_ptr, rowpointers, 0, height ) ; } else if ( color == PNG_COLOR_TYPE_PALETTE ) { /* Read the color palette */ png_colorp png_palette = ( png_colorp ) png_malloc( png_ptr, 256 * sizeof( png_color ) ) ; if ( !png_palette ) { bitmap_destroy( bitmap ); png_destroy_read_struct( &png_ptr, &info_ptr, &end_info ) ; free( rowpointers ) ; free( row ) ; file_close( png ) ; return NULL; } png_get_PLTE( png_ptr, info_ptr, &png_palette, ( int * ) &n ) ; uint8_t colors[256 * 3]; uint8_t * p = colors; for ( n = 0; n < 256 ; n++ ) { * p++ = png_palette[n].red; * p++ = png_palette[n].green; * p++ = png_palette[n].blue; } bitmap->format->palette = pal_new_rgb(( uint8_t * )colors ); pal_refresh( bitmap->format->palette ); if ( !sys_pixel_format->palette ) { sys_pixel_format->palette = pal_new( bitmap->format->palette ); /* pal_use( bitmap->format->palette ); */ palette_changed = 1 ; } for ( n = 0 ; n < height ; n++ ) rowpointers[n] = (( uint8_t* )bitmap->data ) + n * bitmap->pitch ; png_read_image( png_ptr, rowpointers ) ; /* If the depth is less than 8, expand the pixel values */ if ( depth == 4 ) { for ( n = 0; n < height; n++ ) { char * orig, * dest; orig = ( char * ) ( rowpointers[n] ); dest = orig + width - 1; orig += ( width - 1 ) / 2; for ( x = width; x--; ) { *dest-- = ( *orig >> ((( 1 - ( x & 0x01 ) ) << 2 ) ) ) & 0x0F ; if ( !( x & 0x01 ) ) orig--; } } } else if ( depth == 2 )
GRAPH * gr_read_pcx( const char * filename ) { PCXheader header ; file * file ; int width, height, x, y, p, count ; GRAPH * bitmap ; uint8_t * ptr, ch ; file = file_open( filename, "rb" ) ; if ( !file ) return NULL ; file_read( file, &header, sizeof( header ) ) ; /* Arrange the data for big-endian machines */ ARRANGE_WORD( &header.Xmax ); ARRANGE_WORD( &header.Xmin ); ARRANGE_WORD( &header.Ymax ); ARRANGE_WORD( &header.Ymin ); ARRANGE_WORD( &header.BytesPerLine ); ARRANGE_WORD( &header.PaletteInfo ); ARRANGE_WORD( &header.HDpi ); ARRANGE_WORD( &header.VDpi ); ARRANGE_WORD( &header.HscreenSize ); ARRANGE_WORD( &header.VscreenSize ); width = header.Xmax - header.Xmin + 1 ; height = header.Ymax - header.Ymin + 1 ; bitmap = bitmap_new( 0, width, height, ( header.BitsPerPixel == 8 ) ? 8 : 16 ) ; if ( !bitmap ) { file_close( file ); return NULL; } if ( width > header.BytesPerLine ) { bitmap_destroy( bitmap ); file_close( file ); return NULL; } if ( header.BitsPerPixel == 8 ) { for ( y = 0 ; y < height ; y++ ) for ( p = 0 ; p < header.NPlanes ; p++ ) { ptr = ( uint8_t * )bitmap->data + bitmap->pitch * y ; for ( x = 0 ; x < header.BytesPerLine ; ) { if ( file_read( file, &ch, 1 ) < 1 ) { bitmap_destroy( bitmap ); file_close( file ); return NULL; } if (( ch & 0xC0 ) == 0xC0 ) { count = ( ch & 0x3F ) ; file_read( file, &ch, 1 ) ; } else { count = 1 ; } while ( count-- ) { *ptr = ch ; x++ ; ptr += header.NPlanes ; } } } if ( file_read( file, &ch, 1 ) == 1 && ch == 0x0c ) { uint8_t colors[256 * 3] ; if ( file_read( file, colors, sizeof( colors ) ) ) { bitmap->format->palette = pal_new_rgb(( uint8_t * )colors ); pal_refresh( bitmap->format->palette ); if ( !sys_pixel_format->palette ) { sys_pixel_format->palette = pal_new( bitmap->format->palette ); /* pal_use( bitmap->format->palette ); */ palette_changed = 1 ; } } } } else { bitmap_destroy( bitmap ); file_close( file ); return NULL; } bitmap->modified = 0 ; bitmap_analize( bitmap ); return bitmap ; }