18
18
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
21
Dr. David Kirkby, e-mail drkirkby@ntlworld.com
21
Dr. David Kirkby, e-mail drkirkby at ntlworld.com
25
27
#ifdef HAVE_STRING_H
26
28
#include <string.h>
53
57
struct Bitmap_File_Head_Struct Bitmap_File_Head;
54
58
struct Bitmap_Head_Struct Bitmap_Head;
55
59
int ColormapSize, Maps;
56
bmp_buff=ustring(0,0x50);
60
int length_of_file; /* Appently Photoshop versions < 7.01
61
don't write this into Bitmap_Head.biSizeIm
62
so it causes a problem. So we will work out
63
the length by seeking to the end of the file
64
and finding the position of the file
67
bmp_buff=ustring(0,BITMAP_HEADER_SIZE);
57
68
if(strcmp(filename,"-")==0)
62
73
fp=fopen(filename,"rb");
65
77
fprintf(stderr,"cannot open %s\n", filename);
66
78
exit_with_msg_and_exit_code("",CANT_OPEN_FILE_FOR_READING);
81
/* deternine the length of the file, as its not always
82
written into the bitmap. I thought it needed to be, but
83
apparently it does not and photoshop versions < 7.01
85
if( fseek(fp, 0, SEEK_END) == -1)
87
fprintf(stderr," Can't seek to the end of the file in read_bitmap_file_headers.c\n");
90
if( (length_of_file=ftell(fp)) == -1)
91
exit_with_msg_and_exit_code("Can't find length of file in read_bitmap_file_headers.c",FTELL_FAILURE);
93
if( (fseek(fp, 0, SEEK_SET) ) == -1)
95
fprintf(stderr," Can't seek to the beggining of the file in read_bitmap_file_headers.c\n");
68
98
/* Read the .bmp file header into a bmp_buff */
69
if (!(fread(bmp_buff, 1,0x36,fp))||(strncmp((char *) bmp_buff,"BM",2)))
99
if (!(fread(bmp_buff, 1,BITMAP_HEADER_SIZE,fp))||(strncmp((char *) bmp_buff,"BM",2)))
71
101
fprintf(stderr,"%s is not a valid BMP file\n", filename);
72
102
exit_with_msg_and_exit_code("",NOT_A_VALID_BITMAP_FILE);
88
118
Bitmap_File_Head.zzMagic[0x0] = bmp_buff[0];
89
119
Bitmap_File_Head.zzMagic[0x1] = bmp_buff[1];
90
Bitmap_File_Head.bfSize = bmp_buff[0x2] + (bmp_buff[3] + (bmp_buff[4] + (bmp_buff[5] << 8) << 8) <<8);
120
Bitmap_File_Head.bfSize = bmp_buff[0x2] + ((bmp_buff[3] + ((bmp_buff[4] + (bmp_buff[5] << 8)) << 8)) <<8);
91
121
Bitmap_File_Head.zzHotX = bmp_buff[0x6] + (bmp_buff[7] << 8);
92
122
Bitmap_File_Head.zzHotY = bmp_buff[0x8] + (bmp_buff[0x09] << 8);
93
Bitmap_File_Head.bfOffs = bmp_buff[0x0a] + (bmp_buff[0xb] + (bmp_buff[0xc] + (bmp_buff[0x0d] << 8) << 8) <<8);
94
Bitmap_File_Head.biSize = bmp_buff[0x0E] + (bmp_buff[0x0f] + (bmp_buff[0x10] + (bmp_buff[0x11] << 8) << 8) <<8);
123
Bitmap_File_Head.bfOffs = bmp_buff[0x0a] + ((bmp_buff[0xb] + ((bmp_buff[0xc] + (bmp_buff[0x0d] << 8)) << 8)) <<8);
124
Bitmap_File_Head.biSize = bmp_buff[0x0E] + ((bmp_buff[0x0f] + ((bmp_buff[0x10] + (bmp_buff[0x11] << 8)) << 8)) <<8);
96
126
printf("Bitmap_File_Head.bfSize = %d \n",Bitmap_File_Head.bfSize);
97
127
printf("Bitmap_File_Head.zzHotX = %d\n",Bitmap_File_Head.zzHotX);
100
130
printf("Bitmap_File_Head.biSize = %d\n\n",Bitmap_File_Head.biSize);
103
Bitmap_Head.biWidth=bmp_buff[0x12] + (bmp_buff[0x13] + (bmp_buff[0x14] + (bmp_buff[0x15] << 8) << 8) <<8);
104
Bitmap_Head.biHeight=bmp_buff[0x16] + (bmp_buff[0x17] + (bmp_buff[0x18] + (bmp_buff[0x19] << 8) << 8) <<8);
133
Bitmap_Head.biWidth=bmp_buff[0x12] + ((bmp_buff[0x13] + ((bmp_buff[0x14] + (bmp_buff[0x15] << 8)) << 8)) <<8);
134
Bitmap_Head.biHeight=bmp_buff[0x16] + ((bmp_buff[0x17] + ((bmp_buff[0x18] + (bmp_buff[0x19] << 8)) << 8)) <<8);
105
135
Bitmap_Head.biPlanes = bmp_buff[0x1A] + (bmp_buff[0x1b] << 8);
106
136
Bitmap_Head.biBitCnt = bmp_buff[0x1C] + (bmp_buff[0x1d] << 8);
107
Bitmap_Head.biCompr= bmp_buff[0x1E] + (bmp_buff[0x1f] + (bmp_buff[0x20] + (bmp_buff[0x21] << 8) << 8) <<8);
108
Bitmap_Head.biSizeIm=bmp_buff[0x22] + (bmp_buff[0x23] + (bmp_buff[0x24] + (bmp_buff[0x25] << 8) << 8) <<8);
109
Bitmap_Head.biXPels = bmp_buff[0x26] + (bmp_buff[0x27] + (bmp_buff[0x28] + (bmp_buff[0x29] << 8) << 8) <<8);
110
Bitmap_Head.biYPels= bmp_buff[0x2A] + (bmp_buff[0x2b] + (bmp_buff[0x2c] + (bmp_buff[0x2d] << 8) << 8) <<8);
111
Bitmap_Head.biClrUsed = bmp_buff[0x2E] + (bmp_buff[0x2f] + (bmp_buff[0x30] + (bmp_buff[0x31] << 8) << 8) <<8);
112
Bitmap_Head.biClrImp = bmp_buff[0x32] + (bmp_buff[0x33] + (bmp_buff[0x34] + (bmp_buff[0x35] << 8) << 8) <<8);
137
Bitmap_Head.biCompr= bmp_buff[0x1E] + ((bmp_buff[0x1f] + ((bmp_buff[0x20] + (bmp_buff[0x21] << 8)) << 8)) <<8);
138
Bitmap_Head.biSizeIm=bmp_buff[0x22] + ((bmp_buff[0x23] + ((bmp_buff[0x24] + (bmp_buff[0x25] << 8)) << 8)) <<8);
139
/* I thought the length of the image was always stored in Bitmap_Head.biSizeIm, but
140
this appears not to be so. Hence it is now calculated from the length of the file
142
Bitmap_Head.biSizeIm=length_of_file-BITMAP_HEADER_SIZE;
143
Bitmap_Head.biXPels = bmp_buff[0x26] + ((bmp_buff[0x27] + ((bmp_buff[0x28] + (bmp_buff[0x29] << 8)) << 8)) <<8);
144
Bitmap_Head.biYPels= bmp_buff[0x2A] + ((bmp_buff[0x2b] + ((bmp_buff[0x2c] + (bmp_buff[0x2d] << 8)) << 8)) <<8);
145
Bitmap_Head.biClrUsed = bmp_buff[0x2E] + ((bmp_buff[0x2f] + ((bmp_buff[0x30] + (bmp_buff[0x31] << 8)) << 8)) <<8);
146
Bitmap_Head.biClrImp = bmp_buff[0x32] + ((bmp_buff[0x33] + ((bmp_buff[0x34] + (bmp_buff[0x35] << 8)) << 8)) <<8);
115
149
if(Bitmap_Head.biBitCnt!=24)
126
160
printf("Bitmap_Head.biBitCnt =%d =0x%x\n",Bitmap_Head.biBitCnt,Bitmap_Head.biBitCnt);
127
161
printf("Bitmap_Head.biCompr =%d =0x%x\n",Bitmap_Head.biCompr,Bitmap_Head.biCompr );
128
162
printf("Bitmap_Head.biSizeIm =%d =0x%x\n",Bitmap_Head.biSizeIm,Bitmap_Head.biSizeIm);
163
printf("size by ftell=%d\n", length_of_file);
129
164
printf("Bitmap_Head.biXPels =%d =0x%x\n",Bitmap_Head.biXPels,Bitmap_Head.biXPels);
130
165
printf("Bitmap_Head.biYPels =%d =0x%x\n",Bitmap_Head.biYPels,Bitmap_Head.biYPels);
131
166
printf("Bitmap_Head.biClrUsed =%d =0x%x\n",Bitmap_Head.biClrUsed,Bitmap_Head.biClrUsed);
162
197
*height=Bitmap_Head.biHeight;
163
198
*offset=Bitmap_File_Head.bfOffs;
164
199
*size=Bitmap_Head.biSizeIm;
200
if( *size < 3 * (*width) * (*height) )
202
fprintf(stderr,"Internal error in read_bitmap_file_headers.c\n");