write_bmp.c 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <grass/gis.h>
  5. #include "cairodriver.h"
  6. static unsigned char *put_2(unsigned char *p, unsigned int n)
  7. {
  8. *p++ = n & 0xFF; n >>= 8;
  9. *p++ = n & 0xFF;
  10. return p;
  11. }
  12. static unsigned char *put_4(unsigned char *p, unsigned int n)
  13. {
  14. *p++ = n & 0xFF; n >>= 8;
  15. *p++ = n & 0xFF; n >>= 8;
  16. *p++ = n & 0xFF; n >>= 8;
  17. *p++ = n & 0xFF;
  18. return p;
  19. }
  20. static void make_bmp_header(unsigned char *p)
  21. {
  22. *p++ = 'B';
  23. *p++ = 'M';
  24. p = put_4(p, HEADER_SIZE + width * height * 4);
  25. p = put_4(p, 0);
  26. p = put_4(p, HEADER_SIZE);
  27. p = put_4(p, 40);
  28. p = put_4(p, width);
  29. p = put_4(p, -height);
  30. p = put_2(p, 1);
  31. p = put_2(p, 32);
  32. p = put_4(p, 0);
  33. p = put_4(p, width * height * 4);
  34. p = put_4(p, 0);
  35. p = put_4(p, 0);
  36. p = put_4(p, 0);
  37. p = put_4(p, 0);
  38. }
  39. void write_bmp(void)
  40. {
  41. char header[HEADER_SIZE];
  42. FILE *output;
  43. output = fopen(file_name, "wb");
  44. if (!output)
  45. G_fatal_error("cairo: couldn't open output file %s", file_name);
  46. make_bmp_header(header);
  47. fwrite(header, sizeof(header), 1, output);
  48. fwrite(grid, stride, height, output);
  49. fclose(output);
  50. }