~ubuntu-branches/ubuntu/maverick/atlc/maverick

« back to all changes in this revision

Viewing changes to src/non_gui/read_bitmap_file_headers.c

  • Committer: Bazaar Package Importer
  • Author(s): Bdale Garbee
  • Date: 2005-06-03 04:53:28 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20050603045328-pohnuy5yryiv9s6o
Tags: 4.6.0-1
* new upstream version
* move make check from binary to build target so it doesn't run as root
* lose --with-threads from configure call, since it causes regression suite
  to fail many tests and upstream's README.threads is discouraging
* various tweaks to make lintian happier

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
19
19
USA.
20
20
 
21
 
Dr. David Kirkby, e-mail drkirkby@ntlworld.com 
 
21
Dr. David Kirkby, e-mail drkirkby at ntlworld.com 
22
22
 
23
23
*/
24
24
 
 
25
#include "config.h"
 
26
 
25
27
#ifdef HAVE_STRING_H
26
28
#include <string.h>
27
29
#endif
46
48
 
47
49
/* #define DEBUG */
48
50
 
 
51
#define BITMAP_HEADER_SIZE 0x36 /* 54 */
 
52
 
49
53
void read_bitmap_file_headers(char *filename, int *offset, size_t *size, int *width, int *height)
50
54
{
51
55
   FILE *fp;
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 
 
65
                       pointer */
 
66
 
 
67
   bmp_buff=ustring(0,BITMAP_HEADER_SIZE);
57
68
   if(strcmp(filename,"-")==0)
58
69
   {
59
70
      fp=stdin;   
60
71
   }
61
72
   else  
62
73
      fp=fopen(filename,"rb");
 
74
 
63
75
   if(fp==NULL)
64
76
   {
65
77
      fprintf(stderr,"cannot open %s\n", filename);
66
78
      exit_with_msg_and_exit_code("",CANT_OPEN_FILE_FOR_READING);
67
79
   }
 
80
 
 
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 
 
84
   don't do it. */
 
85
   if( fseek(fp, 0, SEEK_END)  == -1)
 
86
   {
 
87
     fprintf(stderr," Can't seek to the end of the file in read_bitmap_file_headers.c\n");
 
88
     exit(FSEEK_FAILURE);
 
89
   }
 
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);
 
92
 
 
93
   if( (fseek(fp, 0, SEEK_SET) ) == -1)
 
94
   {
 
95
     fprintf(stderr," Can't seek to the beggining of the file in read_bitmap_file_headers.c\n");
 
96
     exit(FSEEK_FAILURE);
 
97
   }
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)))
70
100
   {
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);
87
117
 
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);
95
125
#ifdef DEBUG
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); 
101
131
#endif
102
132
 
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
 
141
   */ 
 
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);
113
147
 
114
148
   Maps=4;
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;
165
 
 
 
200
   if( *size < 3 * (*width) * (*height) )
 
201
   {
 
202
     fprintf(stderr,"Internal error in read_bitmap_file_headers.c\n"); 
 
203
     exit(1);
 
204
  }
166
205
}