write_bmp.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*!
  2. \file lib/cairodriver/write_bmp.c
  3. \brief GRASS cairo display driver - write bitmap (lower level functions)
  4. (C) 2007-2008 by Lars Ahlzen and the GRASS Development Team
  5. This program is free software under the GNU General Public License
  6. (>=v2). Read the file COPYING that comes with GRASS for details.
  7. \author Lars Ahlzen <lars ahlzen.com> (original contibutor)
  8. \author Glynn Clements
  9. */
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <grass/gis.h>
  14. #include <grass/glocale.h>
  15. #include "cairodriver.h"
  16. static unsigned char *put_2(unsigned char *p, unsigned int n)
  17. {
  18. *p++ = n & 0xFF;
  19. n >>= 8;
  20. *p++ = n & 0xFF;
  21. return p;
  22. }
  23. static unsigned char *put_4(unsigned char *p, unsigned int n)
  24. {
  25. *p++ = n & 0xFF;
  26. n >>= 8;
  27. *p++ = n & 0xFF;
  28. n >>= 8;
  29. *p++ = n & 0xFF;
  30. n >>= 8;
  31. *p++ = n & 0xFF;
  32. return p;
  33. }
  34. static void make_bmp_header(unsigned char *p)
  35. {
  36. *p++ = 'B';
  37. *p++ = 'M';
  38. p = put_4(p, HEADER_SIZE + ca.width * ca.height * 4);
  39. p = put_4(p, 0);
  40. p = put_4(p, HEADER_SIZE);
  41. p = put_4(p, 40);
  42. p = put_4(p, ca.width);
  43. p = put_4(p, -ca.height);
  44. p = put_2(p, 1);
  45. p = put_2(p, 32);
  46. p = put_4(p, 0);
  47. p = put_4(p, ca.width * ca.height * 4);
  48. p = put_4(p, 0);
  49. p = put_4(p, 0);
  50. p = put_4(p, 0);
  51. p = put_4(p, 0);
  52. }
  53. void cairo_write_bmp(void)
  54. {
  55. unsigned char header[HEADER_SIZE];
  56. FILE *output;
  57. output = fopen(ca.file_name, "wb");
  58. if (!output)
  59. G_fatal_error(_("Cairo: unable to open output file <%s>"),
  60. ca.file_name);
  61. memset(header, 0, sizeof(header));
  62. make_bmp_header(header);
  63. fwrite(header, sizeof(header), 1, output);
  64. fwrite(ca.grid, ca.stride, ca.height, output);
  65. fclose(output);
  66. }