1 /*
2  * This software is copyrighted as noted below.  It may be freely copied,
3  * modified, and redistributed, provided that the copyright notice is
4  * preserved on all copies.
5  *
6  * There is no warranty or other guarantee of fitness for this software,
7  * it is provided solely "as is".  Bug reports or fixes may be sent
8  * to the author, who may or may not act on them as he desires.
9  *
10  * You may not include this software in a program or other software product
11  * without supplying the source, or without informing the end-user that the
12  * source is available for no extra charge.
13  *
14  * If you modify this software, you should include a notice giving the
15  * name of the person performing the modification, the date of modification,
16  * and the reason for such modification.
17  */
18 /*
19  * heightfield.c - convert a bitmap to Rayshade's heightfield
20  *
21  * Author:      Raul Rivero
22  *              Mathematics Dept.
23  *              University of Oviedo
24  * Date:        Thu Aug 27 1992
25  * Copyright (c) 1992, Raul Rivero
26  *
27  */
28 
29 #include <lug.h>
30 #include <lugfnts.h>
31 
32 /*
33  * Sorry but I will not comment this program.
34  */
35 
write_hf_file(name,image)36 write_hf_file( name, image )
37 char *name;
38 bitmap_hdr *image;
39 {
40   FILE *handle;
41 
42   /* Open the file descriptor */
43   if ( name != NULL )
44     handle = Fopen( name, "wb" );
45   else handle = stdout;
46 
47   /* Write the bitmap */
48   write_hf( handle, image, (double) 0.5 );
49 
50   /* Close the file */
51   Fclose( handle );
52 
53 }
54 
write_hf_file_scale(name,image,scale)55 write_hf_file_scale( name, image, scale )
56 char *name;
57 bitmap_hdr *image;
58 double scale;
59 {
60   FILE *handle;
61 
62   /* Open the file descriptor */
63   if ( name != NULL )
64     handle = Fopen( name, "wb" );
65   else handle = stdout;
66 
67   /* Write the bitmap */
68   write_hf( handle, image, scale );
69 
70   /* Close the file */
71   Fclose( handle );
72 
73 }
74 
write_hf(handle,bitmap,scale)75 write_hf( handle, bitmap, scale )
76 FILE *handle;
77 bitmap_hdr *bitmap;
78 double scale;
79 {
80   register int i, j;
81   int totalsize;
82   float *buffer, *ptr;
83   byte *r;
84 
85   if ( bitmap->xsize != bitmap->ysize ) {
86     fprintf( stderr, "Sorry, not squared image\n" );
87     exit( 1 );
88   }
89 
90   totalsize = bitmap->xsize * bitmap->ysize * sizeof( float );
91   ptr = buffer = (float *) Malloc( totalsize );
92   r = bitmap->r;
93   for ( i = 0; i < bitmap->ysize; i++ ) {
94     for ( j = 0; j < bitmap->xsize; j++ ) {
95       *ptr++ = ((float) *r++ / 255.) * scale;
96     }
97   }
98 
99   Fwrite( &(bitmap->xsize), sizeof(int), 1, handle );
100   Fwrite( buffer, totalsize, 1, handle );
101 
102   free( buffer );
103 }
104