comparison Gui/bitmap/png/png.c @ 1693:d237c5d4b216

GUI version n-1
author arpi
date Sat, 25 Aug 2001 21:04:29 +0000
parents
children 1c9bca9154fd
comparison
equal deleted inserted replaced
1692:6c98e425c091 1693:d237c5d4b216
1
2 #include <stdlib.h>
3
4 #include "./png.h"
5 #include "../../error.h"
6 #include <png.h>
7
8 typedef struct
9 {
10 unsigned int Width;
11 unsigned int Height;
12 unsigned int Depth;
13 unsigned int Alpha;
14
15 unsigned int Components;
16 unsigned char * Data;
17 unsigned char * Palette;
18 } pngRawInfo;
19
20 int pngLoadRawF( FILE *fp,pngRawInfo *pinfo )
21 {
22 unsigned char header[8];
23 png_structp png;
24 png_infop info;
25 png_infop endinfo;
26 png_bytep data;
27 png_bytep * row_p;
28 png_uint_32 width,height;
29 int depth,color;
30 png_uint_32 i;
31
32 if ( !pinfo ) return 1;
33
34 fread( header,1,8,fp );
35 if ( !png_check_sig( header,8 ) ) return 1;
36
37 png=png_create_read_struct( PNG_LIBPNG_VER_STRING,NULL,NULL,NULL );
38 info=png_create_info_struct( png );
39 endinfo=png_create_info_struct( png );
40
41 png_init_io( png,fp );
42 png_set_sig_bytes( png,8 );
43 png_read_info( png,info );
44 png_get_IHDR( png,info,&width,&height,&depth,&color,NULL,NULL,NULL );
45
46 pinfo->Width=width;
47 pinfo->Height=height;
48 pinfo->Depth=depth;
49
50 data=( png_bytep ) malloc( png_get_rowbytes( png,info )*height );
51 row_p=( png_bytep * ) malloc( sizeof( png_bytep )*height );
52 for ( i=0; i < height; i++ ) row_p[i]=&data[png_get_rowbytes( png,info )*i];
53
54 png_read_image( png,row_p );
55 free( row_p );
56
57 if ( color == PNG_COLOR_TYPE_PALETTE )
58 {
59 int cols;
60 png_get_PLTE( png,info,( png_colorp * ) &pinfo->Palette,&cols );
61 }
62 else pinfo->Palette=NULL;
63
64 if ( color&PNG_COLOR_MASK_ALPHA )
65 {
66 if ( color&PNG_COLOR_MASK_PALETTE || color == PNG_COLOR_TYPE_GRAY_ALPHA ) pinfo->Components=2;
67 else pinfo->Components=4;
68 pinfo->Alpha=8;
69 }
70 else
71 {
72 if ( color&PNG_COLOR_MASK_PALETTE || color == PNG_COLOR_TYPE_GRAY ) pinfo->Components=1;
73 else pinfo->Components=3;
74 pinfo->Alpha=0;
75 }
76 pinfo->Data=data;
77
78 png_read_end( png,endinfo );
79 png_destroy_read_struct( &png,&info,&endinfo );
80
81 return 0;
82 }
83
84 int pngLoadRaw( const char * filename,pngRawInfo * pinfo )
85 {
86 int result;
87 FILE *fp=fopen( filename,"rb" );
88
89 if ( !fp ) return 1;
90 result=pngLoadRawF( fp,pinfo );
91 if ( fclose( fp ) != 0 )
92 {
93 if ( result )
94 {
95 free( pinfo->Data );
96 free( pinfo->Palette );
97 }
98 return 1;
99 }
100 return 0;
101 }
102
103 int pngRead( unsigned char * fname,txSample * bf )
104 {
105 pngRawInfo raw;
106
107 if ( pngLoadRaw( fname,&raw ) )
108 {
109 #ifdef DEBUG
110 dbprintf( 4,"[png] file read error ( %s ).\n",fname );
111 #endif
112 return 1;
113 }
114 bf->Width=raw.Width;
115 bf->Height=raw.Height;
116 bf->BPP=( raw.Depth * raw.Components ) + raw.Alpha;
117 bf->ImageSize=bf->Width * bf->Height * ( bf->BPP / 8 );
118 if ( ( bf->Image=malloc( bf->ImageSize ) ) == NULL )
119 {
120 #ifdef DEBUG
121 dbprintf( 4,"[png] Not enough memory for image buffer.\n" );
122 #endif
123 return 2;
124 }
125 memcpy( bf->Image,raw.Data,bf->ImageSize );
126 free( raw.Data );
127 #ifdef DEBUG
128 dbprintf( 4,"[png] filename: %s.\n",fname );
129 dbprintf( 4,"[png] size: %dx%d bits: %d\n",bf->Width,bf->Height,bf->BPP );
130 dbprintf( 4,"[png] imagesize: %lu\n",bf->ImageSize );
131 #endif
132 return 0;
133 }