GRASS Programmer's Manual  6.4.2(2012)
cairodriver/read_bmp.c
Go to the documentation of this file.
00001 
00002 #include <stdio.h>
00003 #include <stdlib.h>
00004 #include <string.h>
00005 
00006 #include <grass/gis.h>
00007 #include "cairodriver.h"
00008 
00009 static unsigned int get_2(const unsigned char **q)
00010 {
00011     const unsigned char *p = *q;
00012     unsigned int n = (p[0] << 0) | (p[1] << 8);
00013 
00014     *q += 2;
00015     return n;
00016 }
00017 
00018 static unsigned int get_4(const unsigned char **q)
00019 {
00020     const unsigned char *p = *q;
00021     unsigned int n = (p[0] << 0) | (p[1] << 8) | (p[2] << 16) | (p[3] << 24);
00022 
00023     *q += 4;
00024     return n;
00025 }
00026 
00027 static int read_bmp_header(const unsigned char *p)
00028 {
00029     if (*p++ != 'B')
00030         return 0;
00031     if (*p++ != 'M')
00032         return 0;
00033 
00034     if (get_4(&p) != HEADER_SIZE + width * height * 4)
00035         return 0;
00036 
00037     get_4(&p);
00038 
00039     if (get_4(&p) != HEADER_SIZE)
00040         return 0;
00041 
00042     if (get_4(&p) != 40)
00043         return 0;
00044 
00045     if (get_4(&p) != width)
00046         return 0;
00047     if (get_4(&p) != -height)
00048         return 0;
00049 
00050     get_2(&p);
00051     if (get_2(&p) != 32)
00052         return 0;
00053 
00054     if (get_4(&p) != 0)
00055         return 0;
00056     if (get_4(&p) != width * height * 4)
00057         return 0;
00058 
00059     get_4(&p);
00060     get_4(&p);
00061     get_4(&p);
00062     get_4(&p);
00063 
00064     return 1;
00065 }
00066 
00067 void read_bmp(void)
00068 {
00069     char header[HEADER_SIZE];
00070     FILE *input;
00071 
00072     input = fopen(file_name, "rb");
00073     if (!input)
00074         G_fatal_error("cairo:: couldn't open input file %s", file_name);
00075 
00076     if (fread(header, sizeof(header), 1, input) != 1)
00077         G_fatal_error("cairo:: invalid input file %s", file_name);
00078 
00079     if (!read_bmp_header(header))
00080         G_fatal_error("cairo:: invalid BMP header for %s", file_name);
00081 
00082     fread(grid, stride, height, input);
00083 
00084     fclose(input);
00085 }
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines