GRASS 8 Programmer's Manual 8.6.0dev(2026)-4bb960b182
Loading...
Searching...
No Matches
pngdriver/write_bmp.c
Go to the documentation of this file.
1/*!
2 \file lib/pngdriver/write_bmp.c
3
4 \brief GRASS png display driver - write bitmap (lower level functions)
5
6 SPDX-FileCopyrightText: 2007-2014 Glynn Clements
7 SPDX-FileCopyrightText: GRASS Development Team
8 SPDX-License-Identifier: GPL-2.0-or-later
9
10 \author Glynn Clements
11 */
12
13#include <stdio.h>
14#include <stdlib.h>
15#include <string.h>
16
17#include <grass/gis.h>
18#include "pngdriver.h"
19
20static unsigned char *put_2(unsigned char *p, unsigned int n)
21{
22 *p++ = n & 0xFF;
23 n >>= 8;
24 *p++ = n & 0xFF;
25 return p;
26}
27
28static unsigned char *put_4(unsigned char *p, unsigned int n)
29{
30 *p++ = n & 0xFF;
31 n >>= 8;
32 *p++ = n & 0xFF;
33 n >>= 8;
34 *p++ = n & 0xFF;
35 n >>= 8;
36 *p++ = n & 0xFF;
37 return p;
38}
39
40static void make_bmp_header(unsigned char *p)
41{
42 *p++ = 'B';
43 *p++ = 'M';
44
45 p = put_4(p, HEADER_SIZE + png.width * png.height * 4);
46 p = put_4(p, 0);
47 p = put_4(p, HEADER_SIZE);
48
49 p = put_4(p, 40);
50 p = put_4(p, png.width);
51 p = put_4(p, -png.height);
52 p = put_2(p, 1);
53 p = put_2(p, 32);
54 p = put_4(p, 0);
55 p = put_4(p, png.width * png.height * 4);
56 p = put_4(p, 0);
57 p = put_4(p, 0);
58 p = put_4(p, 0);
59 p = put_4(p, 0);
60}
61
62void write_bmp(void)
63{
64 unsigned char header[HEADER_SIZE];
65 FILE *output;
66 int x, y;
67 unsigned int *p;
68
69 output = fopen(png.file_name, "wb");
70 if (!output)
71 G_fatal_error("PNG: couldn't open output file %s", png.file_name);
72
73 memset(header, 0, sizeof(header));
74 make_bmp_header(header);
75 fwrite(header, sizeof(header), 1, output);
76
77 for (y = 0, p = png.grid; y < png.height; y++) {
78 for (x = 0; x < png.width; x++, p++) {
79 unsigned int c = *p;
80 int r, g, b, a;
81
82 png_get_pixel(c, &r, &g, &b, &a);
83
84 fputc((unsigned char)b, output);
85 fputc((unsigned char)g, output);
86 fputc((unsigned char)r, output);
87 fputc((unsigned char)a, output);
88 }
89 }
90
92}
#define HEADER_SIZE
Definition cairodriver.h:45
void png_get_pixel(unsigned int pixel, int *r, int *g, int *b, int *a)
void void void void G_fatal_error(const char *,...) __attribute__((format(printf
float g
Definition named_colr.c:7
struct png_state png
void write_bmp(void)
GRASS png display driver - header file.
void output(const char *fmt,...)
double b
Definition r_raster.c:37
double r
Definition r_raster.c:37
#define x