missed one!
[debian/atlc] / src / write_bitmap_out.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 #ifdef HAVE_STDIO_H
40 #include <stdio.h>
41 #endif
42
43 #include "definitions.h"
44 #include "exit_codes.h"
45
46 /* names, colours and Ers are all arrays of 10. It would be better they were 
47 in a structure as they are all linked closely, but they are not and I
48 can't be bothered to change it */
49
50
51 extern int W, H;
52
53 /* This function can be used on any routines that writes .bmp files */
54
55 void write_bitmap_out(unsigned char *byte_aligned_image_data, FILE *image_data_fp,int sizeof_image, int W, int H)
56 {
57    unsigned char *buff;
58
59    struct Bitmap_File_Head_Struct Bitmap_File_Head;
60    struct Bitmap_Head_Struct Bitmap_Head;
61
62 #ifdef DEBUG
63    printf("sizeof_image = %d in write_bitmap_out\n", sizeof_image);
64 #endif
65
66    buff=ustring(0,0x36);
67
68    /* fprintf(stderr,"file size=%ld\n",temp_long); */
69
70    Bitmap_File_Head.zzMagic[0]='B';
71    Bitmap_File_Head.zzMagic[1]='M';
72    Bitmap_File_Head.bfSize=sizeof_image+0x36;
73    Bitmap_File_Head.zzHotX=0; 
74    Bitmap_File_Head.zzHotY=0; 
75    Bitmap_File_Head.bfOffs=54; 
76    Bitmap_File_Head.biSize=0x28;
77
78
79    Bitmap_Head.biWidth=W;
80    Bitmap_Head.biHeight=H;
81    Bitmap_Head.biPlanes=1;
82    Bitmap_Head.biBitCnt=24;
83    Bitmap_Head.biCompr=0;
84    Bitmap_Head.biSizeIm=sizeof_image;
85    Bitmap_Head.biXPels=W*10;  /* Why ??? XXX */
86    Bitmap_Head.biYPels=H*10;  /* Why ??? XXX */
87    Bitmap_Head.biClrUsed=0;
88    Bitmap_Head.biClrImp=0;
89
90    buff[0x00] = (unsigned char) (Bitmap_File_Head.zzMagic[0]);
91    buff[0x01] = (unsigned char) (Bitmap_File_Head.zzMagic[1]);
92 /*
93    buff[0x02] = Bitmap_File_Head.bfSize);
94    buff[0x03] = Bitmap_File_Head.bfSize >> 8);
95    buff[0x04] = Bitmap_File_Head.bfSize >> 16);
96    buff[0x05] = Bitmap_File_Head.bfSize >> 24);
97    */
98
99    buff[0x02] =  (unsigned char) (Bitmap_File_Head.bfSize);
100    buff[0x03] =  (unsigned char) (Bitmap_File_Head.bfSize >> 8);
101    buff[0x04] =  (unsigned char) (Bitmap_File_Head.bfSize >> 16);
102    buff[0x05] =  (unsigned char) (Bitmap_File_Head.bfSize >> 24);
103
104
105    buff[0x06] = (unsigned char) (Bitmap_File_Head.zzHotX);
106    buff[0x07] = (unsigned char) (Bitmap_File_Head.zzHotX >> 8);
107
108    buff[0x08] = (unsigned char) (Bitmap_File_Head.zzHotY);
109    buff[0x09] = (unsigned char) (Bitmap_File_Head.zzHotY >> 8);
110
111    buff[0x0a] = (unsigned char) (Bitmap_File_Head.bfOffs);
112    buff[0x0b] = (unsigned char) (Bitmap_File_Head.bfOffs>> 8);
113    buff[0x0c] = (unsigned char) (Bitmap_File_Head.bfOffs>> 16);
114    buff[0x0d] = (unsigned char) (Bitmap_File_Head.bfOffs>> 24);
115
116    buff[0x0e] = (unsigned char) (Bitmap_File_Head.biSize);
117    buff[0x0f] = (unsigned char) (Bitmap_File_Head.biSize>> 8);
118    buff[0x10] = (unsigned char) (Bitmap_File_Head.biSize>> 16);
119    buff[0x11] = (unsigned char) (Bitmap_File_Head.biSize>> 24);
120
121    /* Write contents of Bitmap_Head_Struct */
122
123    buff[0x12] = (unsigned char) (Bitmap_Head.biWidth);
124    buff[0x13] = (unsigned char) (Bitmap_Head.biWidth>> 8);
125    buff[0x14] = (unsigned char) (Bitmap_Head.biWidth>> 16);
126    buff[0x15] = (unsigned char) (Bitmap_Head.biWidth>> 24);
127
128    buff[0x16] = (unsigned char) (Bitmap_Head.biHeight);
129    buff[0x17] = (unsigned char) (Bitmap_Head.biHeight>> 8);
130    buff[0x18] = (unsigned char) (Bitmap_Head.biHeight>> 16);
131    buff[0x19] = (unsigned char) (Bitmap_Head.biHeight>> 24);
132
133    buff[0x1a] = (unsigned char) (Bitmap_Head.biPlanes);
134    buff[0x1b] = (unsigned char) (Bitmap_Head.biPlanes>> 8);
135
136    buff[0x1c] = (unsigned char) (Bitmap_Head.biBitCnt);
137    buff[0x1d] = (unsigned char) (Bitmap_Head.biBitCnt>> 8);
138
139    buff[0x1e] = (unsigned char) (Bitmap_Head.biCompr);
140    buff[0x1f] = (unsigned char) (Bitmap_Head.biCompr>> 8);
141    buff[0x20] = (unsigned char) (Bitmap_Head.biCompr>> 16);
142    buff[0x21] = (unsigned char) (Bitmap_Head.biCompr>> 24);
143
144    buff[0x22] = (unsigned char) (Bitmap_Head.biSizeIm);
145    buff[0x23] = (unsigned char) (Bitmap_Head.biSizeIm>> 8);
146    buff[0x24] = (unsigned char) (Bitmap_Head.biSizeIm>> 16);
147    buff[0x25] = (unsigned char) (Bitmap_Head.biSizeIm>> 24);
148
149    buff[0x26] = (unsigned char) (Bitmap_Head.biXPels);
150    buff[0x27] = (unsigned char) (Bitmap_Head.biXPels>> 8);
151    buff[0x28] = (unsigned char) (Bitmap_Head.biXPels>> 16);
152    buff[0x29] = (unsigned char) (Bitmap_Head.biXPels>> 24);
153
154    buff[0x2a] = (unsigned char) (Bitmap_Head.biYPels);
155    buff[0x2b] = (unsigned char) (Bitmap_Head.biYPels>> 8);
156    buff[0x2c] = (unsigned char) (Bitmap_Head.biYPels>> 16);
157    buff[0x2d] = (unsigned char) (Bitmap_Head.biYPels>> 24);
158
159    buff[0x2e] = (unsigned char) (Bitmap_Head.biClrUsed);
160    buff[0x2f] = (unsigned char) (Bitmap_Head.biClrUsed>> 8);
161    buff[0x30] = (unsigned char) (Bitmap_Head.biClrUsed>> 16);
162    buff[0x31] = (unsigned char) (Bitmap_Head.biClrUsed>> 24);
163
164    buff[0x32] = (unsigned char) (Bitmap_Head.biClrImp);
165    buff[0x33] = (unsigned char) (Bitmap_Head.biClrImp>> 8);
166    buff[0x34] = (unsigned char) (Bitmap_Head.biClrImp>> 16);
167    buff[0x35] = (unsigned char) (Bitmap_Head.biClrImp>> 24);
168
169    if( fwrite((void *) buff, 0x36, 1, image_data_fp) != 1)
170     exit_with_msg_and_exit_code("Error#1: Failed to write bitmap file in write_bitmap_out.c",WRITE_FAILURE);
171
172
173    /* Now the reader is written, finally write the image */
174    if( fwrite(byte_aligned_image_data,(size_t) sizeof_image,1,image_data_fp) != 1)
175     exit_with_msg_and_exit_code("Error#1: Failed to write bitmap file in write_bitmap_out.c",WRITE_FAILURE);
176    if( fclose(image_data_fp) != 0)
177      exit_with_msg_and_exit_code("failed to close file in write_bitmap_out.c",CANT_CLOSE_FILE);
178 }