viewing source for "outputs/bmp.c"

last commit

commit 2fcfc3b2e8e18bd4ab667b82d939cc2ba9b6a5d3 Author: Andrew Rader <andrew.r.rader@gmail.com> Date: Sun Jan 13 09:25:46 2008 -0800
Initial Commit

source code

01: #include <stdio.h>
02: #include <stdlib.h>
03: #include <string.h>
04: #include <stdint.h>
05: 
06: #include "../vector.h"
07: 
08: typedef struct {
09:     uint8_t id[2];
10:     uint32_t filesize;
11:     uint32_t reserved;
12:     uint32_t h_size;
13:     uint32_t i_size;
14:     uint32_t width;
15:     uint32_t height;
16:     uint16_t bi_planes;
17:     uint16_t bits;
18:     uint32_t bi_compress;
19:     uint32_t bi_size_image;
20:     uint32_t bi_x_ppm;
21:     uint32_t bi_y_ppm;
22:     uint32_t bi_clr_used;
23:     uint32_t bi_clr_import;
24: } bmp_header_t;
25: 
26: int write_img( int w, int h, color ***image, char *file ) {
27:     bmp_header_t bh;
28:     FILE *bmpfile;
29:     int bpl, line, i, j;
30:     char *linebuf;
31: 
32:     bpl = w * 3;    /* for a 24bit image */
33:     if( bpl & 0x0003 ) {
34:         bpl |= 0x0003;
35:         ++bpl;
36:     }
37: 
38:     memset( (char*)&bh, 0, sizeof( bmp_header_t ) );
39:     memcpy( bh.id, "BM", 2 );
40: 
41:     bh.h_size = 54L;
42:     bh.i_size = 0x28L;
43:     bh.width = w;
44:     bh.height = h;
45:     bh.bi_planes = 1;
46:     bh.bits = 24;
47:     bh.bi_compress = 0;
48: 
49:     bh.filesize = bh.h_size + (long)bpl*bh.height;
50: 
51:     bmpfile = fopen( file, "wb" );
52:     if( bmpfile == NULL ) {
53:         fprintf( stderr, "*** error opening bitmap file for writing\n" );
54:         return -1;
55:     }
56: 
57:     /* write the header */
58:     fwrite( &bh.id, 1, 2, bmpfile );
59:     fwrite( &bh.filesize, 4, 1, bmpfile );
60:     fwrite( &bh.reserved, 2, 2, bmpfile );
61:     fwrite( &bh.h_size, 4, 1, bmpfile );
62:     fwrite( &bh.i_size, 4, 1, bmpfile );
63:     fwrite( &bh.width, 4, 1, bmpfile );
64:     fwrite( &bh.height, 4, 1, bmpfile );
65:     fwrite( &bh.bi_planes, 2, 1, bmpfile );
66:     fwrite( &bh.bits, 2, 1, bmpfile );
67:     fwrite( &bh.bi_compress, 4, 1, bmpfile );
68:     fwrite( &bh.bi_size_image, 4, 1, bmpfile );
69:     fwrite( &bh.bi_x_ppm, 4, 1, bmpfile );
70:     fwrite( &bh.bi_y_ppm, 4, 1, bmpfile );
71:     fwrite( &bh.bi_clr_used, 4, 1, bmpfile );
72:     fwrite( &bh.bi_clr_import, 4, 1, bmpfile );
73: 
74:     /* write the image */
75:     linebuf = (char*)calloc( 1, bpl );
76:     if( linebuf == NULL ) {
77:         fprintf( stderr, "*** error allocating memory for file io\n" );
78:         return -1;
79:     }
80: 
81:     for( line = h - 1; line >= 0; line-- ) {
82:         for( i = 0, j = 0; i < w && j < bpl; i++, j+=3 ) {
83:             /* bitmaps are stored BGR */
84:             linebuf[j] = (char)(image[line][i]->z*255.0f);
85:             linebuf[j+1] = (char)(image[line][i]->y*255.0f);
86:             linebuf[j+2] = (char)(image[line][i]->x*255.0f);
87:         }
88:         fwrite( linebuf, 1, bpl, bmpfile );
89:     }
90: 
91:     /* free the line buffer memory */
92:     free( linebuf );
93: 
94:     fclose( bmpfile );
95: 
96:     return 0;
97: }