missed one!
[debian/atlc] / src / read_bitmap_file_headers.c
1 /* atlc - arbitrary transmission line calculator, for the analysis of
2 transmission lines are directional couplers. 
3
4 Copyright (C) 2002. Dr. David Kirkby, PhD (G8WRB).
5
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation; either package_version 2
9 of the License, or (at your option) any later package_version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
19 USA.
20
21 Dr. David Kirkby, e-mail drkirkby at gmail.com 
22
23 */
24
25 #include "config.h"
26
27 #ifdef HAVE_STRING_H
28 #include <string.h>
29 #endif
30
31 #ifdef HAVE_STRINGS_H
32 #include <strings.h>
33 #endif
34
35 #ifdef HAVE_STDLIB_H
36 #include <stdlib.h>
37 #endif
38
39 #include "definitions.h"
40 #include "exit_codes.h"
41
42 unsigned char *bmp_buff;
43
44 /* We make the bitmap header public, as we write it back 
45 later, when saving bmp files. It saves a lot of hassle,
46 just writing what we read back, rather than correctly 
47 calculating all the data in the header correctly */
48
49 #define BITMAP_HEADER_SIZE 0x36 /* 54 */
50
51 void read_bitmap_file_headers(char *filename, int *offset, size_t *size, int *width, int *height)
52 {
53    FILE *fp;
54
55    struct Bitmap_File_Head_Struct Bitmap_File_Head;
56    struct Bitmap_Head_Struct Bitmap_Head;
57    int ColormapSize, Maps;
58    int length_of_file; /* Appently Photoshop versions < 7.01
59                        don't write this into Bitmap_Head.biSizeIm
60                        so it causes a problem. So we will work out
61                        the length by seeking to the end of the file
62                        and finding the position of the file 
63                        pointer */
64
65    bmp_buff=ustring(0,BITMAP_HEADER_SIZE);
66    if(strcmp(filename,"-")==0)
67    {
68       fp=stdin;   
69    }
70    else  
71       fp=fopen(filename,"rb");
72
73    if(fp==NULL)
74    {
75       fprintf(stderr,"cannot open %s\n", filename);
76       exit_with_msg_and_exit_code("",CANT_OPEN_FILE_FOR_READING);
77    }
78
79    /* deternine the length of the file, as its not always 
80    written into the bitmap. I thought it needed to be, but
81    apparently it does not and photoshop versions < 7.01 
82    don't do it. */
83    if( fseek(fp, 0, SEEK_END)  == -1)
84    {
85      fprintf(stderr," Can't seek to the end of the file in read_bitmap_file_headers.c\n");
86      exit(FSEEK_FAILURE);
87    }
88    if( (length_of_file=ftell(fp))  == -1)
89      exit_with_msg_and_exit_code("Can't find length of file in read_bitmap_file_headers.c",FTELL_FAILURE);
90
91    if( (fseek(fp, 0, SEEK_SET) ) == -1)
92    {
93      fprintf(stderr," Can't seek to the beggining of the file in read_bitmap_file_headers.c\n");
94      exit(FSEEK_FAILURE);
95    }
96    /* Read the .bmp file header into a bmp_buff */
97    if (!(fread(bmp_buff, 1,BITMAP_HEADER_SIZE,fp))||(strncmp((char *) bmp_buff,"BM",2)))
98    {
99       fprintf(stderr,"%s is not a valid BMP file\n", filename);
100       exit_with_msg_and_exit_code("",NOT_A_VALID_BITMAP_FILE);
101    }
102
103 /* On most machines, sizeof(short)==2 and sizeof(int)==4. This is so no matter
104 if the machine is 32 or 64 bis. An exception is the Cray Y-MP, which has  
105 sizeof(short)=8
106 sizeof(int)=8
107 sizeof(long)=8.
108
109 In this case, it is much more difficult to write the header for the bitmap. But in
110 the aid of portability, this is done. So these is a section of code that will work
111 even if sizeof(short)=8 and sizeof(int)=8. See below for that. */
112
113
114    /* Read the bmp_buff into the two structures we want */
115
116    Bitmap_File_Head.zzMagic[0x0] = bmp_buff[0];
117    Bitmap_File_Head.zzMagic[0x1] = bmp_buff[1];
118    Bitmap_File_Head.bfSize = bmp_buff[0x2] + ((bmp_buff[3] + ((bmp_buff[4] + (bmp_buff[5] << 8)) << 8)) <<8);
119    Bitmap_File_Head.zzHotX = bmp_buff[0x6] + (bmp_buff[7] << 8);
120    Bitmap_File_Head.zzHotY = bmp_buff[0x8] + (bmp_buff[0x09] << 8);
121    Bitmap_File_Head.bfOffs = bmp_buff[0x0a] + ((bmp_buff[0xb] + ((bmp_buff[0xc] + (bmp_buff[0x0d] << 8)) << 8)) <<8);
122    Bitmap_File_Head.biSize = bmp_buff[0x0E] + ((bmp_buff[0x0f] + ((bmp_buff[0x10] + (bmp_buff[0x11] << 8)) << 8)) <<8);
123    Bitmap_Head.biWidth=bmp_buff[0x12] + ((bmp_buff[0x13] + ((bmp_buff[0x14] + (bmp_buff[0x15] << 8)) << 8)) <<8);
124    Bitmap_Head.biHeight=bmp_buff[0x16] + ((bmp_buff[0x17] + ((bmp_buff[0x18] + (bmp_buff[0x19] << 8)) << 8)) <<8);
125    Bitmap_Head.biPlanes = bmp_buff[0x1A] + (bmp_buff[0x1b] << 8);
126    Bitmap_Head.biBitCnt = bmp_buff[0x1C] + (bmp_buff[0x1d] << 8);
127    Bitmap_Head.biCompr= bmp_buff[0x1E] + ((bmp_buff[0x1f] + ((bmp_buff[0x20] + (bmp_buff[0x21] << 8)) << 8)) <<8);
128    Bitmap_Head.biSizeIm=bmp_buff[0x22] + ((bmp_buff[0x23] + ((bmp_buff[0x24] + (bmp_buff[0x25] << 8)) << 8)) <<8);
129    /* I thought the length of the image was always stored in Bitmap_Head.biSizeIm, but 
130    this appears not to be so. Hence it is now calculated from the length of the file
131    */ 
132    Bitmap_Head.biSizeIm=length_of_file-BITMAP_HEADER_SIZE;
133    Bitmap_Head.biXPels  = bmp_buff[0x26] + ((bmp_buff[0x27] + ((bmp_buff[0x28] + (bmp_buff[0x29] << 8)) << 8)) <<8);
134    Bitmap_Head.biYPels= bmp_buff[0x2A] + ((bmp_buff[0x2b] + ((bmp_buff[0x2c] + (bmp_buff[0x2d] << 8)) << 8)) <<8);
135    Bitmap_Head.biClrUsed = bmp_buff[0x2E] + ((bmp_buff[0x2f] + ((bmp_buff[0x30] + (bmp_buff[0x31] << 8)) << 8)) <<8);
136    Bitmap_Head.biClrImp  = bmp_buff[0x32] + ((bmp_buff[0x33] + ((bmp_buff[0x34] + (bmp_buff[0x35] << 8)) << 8)) <<8);
137
138    Maps=4;
139    if(Bitmap_Head.biBitCnt!=24)
140    {
141       fprintf(stderr,"Sorry, the .bmp bitmap must have 24 bits per colour,\n");
142       fprintf(stderr,"but it has %d bits. Resave the \n",Bitmap_Head.biBitCnt);
143       fprintf(stderr,"image using 24-bit colour\n");
144       exit_with_msg_and_exit_code("mage using 24-bit colour",BITMAP_NOT_24_BIT);
145    }
146    ColormapSize = (Bitmap_File_Head.bfOffs - Bitmap_File_Head.biSize - 14) / Maps;
147
148    if ((Bitmap_Head.biClrUsed == 0) && (Bitmap_Head.biBitCnt <= 8))
149            Bitmap_Head.biClrUsed = ColormapSize;
150
151    /* Sanity checks */
152
153    if (Bitmap_Head.biHeight == 0 || Bitmap_Head.biWidth == 0) 
154    {
155       fprintf(stderr,"error reading BMP file header of %s - width or height is zero\n",filename);
156       exit_with_msg_and_exit_code("",WIDTH_OR_HEIGHT_ZERO_IN_BITMAP);
157    } 
158    if (Bitmap_Head.biPlanes != 1) 
159    {
160       fprintf(stderr,"error reading BMP file header of %s - bitplanes not equal to 1\n",filename);
161       exit_with_msg_and_exit_code("",BITPLANES_NOT_1_IN_BITMAP);
162    }
163    if (ColormapSize > 256 || Bitmap_Head.biClrUsed > 256)
164    {
165       fprintf(stderr,"error reading BMP file header of %s - colourmap size error\n",filename);
166       exit_with_msg_and_exit_code("",COLOURMAP_GREATER_THAN_256);
167    }
168    /* Windows and OS/2 declare filler so that rows are a multiple of
169       word length (32 bits == 4 bytes)
170    */
171
172    fclose(fp);
173    *width=Bitmap_Head.biWidth;
174    *height=Bitmap_Head.biHeight;
175    *offset=Bitmap_File_Head.bfOffs;
176    *size=Bitmap_Head.biSizeIm;
177    if( *size < 3 * (*width) * (*height) )
178    {
179      fprintf(stderr,"Internal error in read_bitmap_file_headers.c\n"); 
180      exit(1);
181    }
182 }