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