GRASS Programmer's Manual
6.4.2(2012)
|
00001 00002 #include <stdio.h> 00003 #include <stdlib.h> 00004 #include <string.h> 00005 00006 #include <grass/gis.h> 00007 #include "pngdriver.h" 00008 00009 static unsigned char *put_2(unsigned char *p, unsigned int n) 00010 { 00011 *p++ = n & 0xFF; 00012 n >>= 8; 00013 *p++ = n & 0xFF; 00014 return p; 00015 } 00016 00017 static unsigned char *put_4(unsigned char *p, unsigned int n) 00018 { 00019 *p++ = n & 0xFF; 00020 n >>= 8; 00021 *p++ = n & 0xFF; 00022 n >>= 8; 00023 *p++ = n & 0xFF; 00024 n >>= 8; 00025 *p++ = n & 0xFF; 00026 return p; 00027 } 00028 00029 static void make_bmp_header(unsigned char *p) 00030 { 00031 *p++ = 'B'; 00032 *p++ = 'M'; 00033 00034 p = put_4(p, HEADER_SIZE + width * height * 4); 00035 p = put_4(p, 0); 00036 p = put_4(p, HEADER_SIZE); 00037 00038 p = put_4(p, 40); 00039 p = put_4(p, width); 00040 p = put_4(p, -height); 00041 p = put_2(p, 1); 00042 p = put_2(p, 32); 00043 p = put_4(p, 0); 00044 p = put_4(p, width * height * 4); 00045 p = put_4(p, 0); 00046 p = put_4(p, 0); 00047 p = put_4(p, 0); 00048 p = put_4(p, 0); 00049 } 00050 00051 void write_bmp(void) 00052 { 00053 char header[HEADER_SIZE]; 00054 FILE *output; 00055 int x, y; 00056 unsigned int *p; 00057 00058 output = fopen(file_name, "wb"); 00059 if (!output) 00060 G_fatal_error("PNG: couldn't open output file %s", file_name); 00061 00062 memset(header, 0, sizeof(header)); 00063 make_bmp_header(header); 00064 fwrite(header, sizeof(header), 1, output); 00065 00066 for (y = 0, p = grid; y < height; y++) { 00067 for (x = 0; x < width; x++, p++) { 00068 unsigned int c = *p; 00069 int r, g, b, a; 00070 00071 get_pixel(c, &r, &g, &b, &a); 00072 00073 fputc((unsigned char)b, output); 00074 fputc((unsigned char)g, output); 00075 fputc((unsigned char)r, output); 00076 fputc((unsigned char)a, output); 00077 } 00078 } 00079 00080 fclose(output); 00081 }