1693
|
1
|
|
2 #ifndef __MY_BMP
|
|
3 #define __MY_BMP
|
|
4
|
|
5 /*
|
|
6 0.1 : BMP type.
|
|
7 2.5 : File size.
|
|
8 6.7 : Res.
|
|
9 8.9 : Res.
|
|
10 10.13 : Offset of bitmap.
|
|
11 14.17 : Header size.
|
|
12 18.21 : X size.
|
|
13 22.25 : Y size.
|
|
14 26.27 : Number of planes.
|
|
15 28.29 : Number of bits per pixel.
|
|
16 30.33 : Compression flag.
|
|
17 34.37 : Image data size in bytes.
|
|
18 38.41 : Res
|
|
19 42.45 : Res
|
|
20 46.49 : Res
|
|
21 50.53 : Res
|
|
22 */
|
|
23
|
2080
|
24 #include <stdio.h>
|
1693
|
25 #include <stdlib.h>
|
2080
|
26 #include <string.h>
|
1693
|
27
|
|
28 #include "bmp.h"
|
|
29 #include "../bitmap.h"
|
|
30 #include "../../error.h"
|
|
31
|
|
32 int bmpRead( unsigned char * fname,txSample * bF )
|
|
33 {
|
|
34 unsigned char bmpHeader[54];
|
|
35 FILE * BMP;
|
|
36 unsigned long i;
|
|
37 unsigned char * line;
|
|
38 int linesize;
|
|
39
|
|
40
|
|
41 if ( (BMP=fopen( fname,"rt" )) == NULL )
|
|
42 {
|
|
43 #ifdef DEBUG
|
|
44 dbprintf( 4,"[bmp] File not found ( %s ).\n",fname );
|
|
45 #endif
|
|
46 return 1;
|
|
47 }
|
|
48 if ( (i=fread( bmpHeader,54,1,BMP )) != 1 )
|
|
49 {
|
|
50 #ifdef DEBUG
|
|
51 dbprintf( 4,"[bmp] Header read error ( %s ).\n",fname );
|
|
52 #endif
|
|
53 return 2;
|
|
54 }
|
|
55 // memcpy( &bF->Size,&bmpHeader[2],4 );
|
|
56 memcpy( &bF->Width,&bmpHeader[18],4 );
|
|
57 memcpy( &bF->Height,&bmpHeader[22],4 );
|
|
58 memcpy( &bF->BPP,&bmpHeader[28],2 );
|
|
59 // memcpy( &bF->ImageSize,&bmpHeader[34],4 );
|
|
60 bF->ImageSize=( bF->Width * bF->Height ) * ( bF->BPP / 8 );
|
|
61
|
|
62 if ( bF->BPP < 24 )
|
|
63 {
|
|
64 #ifdef DEBUG
|
|
65 dbprintf( 4,"[bmp] Sorry, this loader not supported 16 bit or less ...\n" );
|
|
66 #endif
|
|
67 return 3;
|
|
68 }
|
|
69
|
|
70 #ifdef DEBUG
|
|
71 dbprintf( 4,"[bmp] filename: %s\n",fname );
|
|
72 dbprintf( 4,"[bmp] size: %dx%d bits: %d\n",bF->Width,bF->Height,bF->BPP );
|
|
73 dbprintf( 4,"[bmp] imagesize: %lu\n",bF->ImageSize );
|
|
74 #endif
|
|
75
|
|
76 if ( ( bF->Image=malloc( bF->ImageSize ) ) == NULL )
|
|
77 {
|
|
78 #ifdef DEBUG
|
|
79 dbprintf( 4,"[bmp] Not enough memory for image buffer.\n" );
|
|
80 #endif
|
|
81 return 4;
|
|
82 }
|
|
83
|
|
84 if ( (i=fread( bF->Image,bF->ImageSize,1,BMP )) != 1 )
|
|
85 {
|
|
86 #ifdef DEBUG
|
|
87 dbprintf( 4,"[bmp] Image read error.\n" );
|
|
88 #endif
|
|
89 return 5;
|
|
90 }
|
|
91
|
|
92 fclose( BMP );
|
|
93
|
|
94 linesize=bF->Width * ( bF->BPP / 8 );
|
|
95 if ( (line=malloc( linesize )) == NULL )
|
|
96 {
|
|
97 #ifdef DEBUG
|
|
98 dbprintf( 4,"[bmp] Not enough memory for flipping.\n" );
|
|
99 #endif
|
|
100 return 6;
|
|
101 }
|
|
102
|
|
103 for ( i=0;i < bF->Height / 2;i++ )
|
|
104 {
|
|
105 memcpy( line,&bF->Image[ i * linesize ],linesize );
|
|
106 memcpy( &bF->Image[ i * linesize ],&bF->Image[ ( bF->Height - i - 1 ) * linesize ],linesize );
|
|
107 memcpy( &bF->Image[ ( bF->Height - i - 1 ) * linesize ],line,linesize );
|
|
108 }
|
|
109 free( line );
|
|
110
|
|
111 return 0;
|
|
112 }
|
|
113
|
2082
|
114 #endif
|
|
115
|