1
0

write_bmp.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * Copyright 2011 Luc Verhaegen <libv@codethink.co.uk>
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sub license,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the
  12. * next paragraph) shall be included in all copies or substantial portions
  13. * of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  21. * DEALINGS IN THE SOFTWARE.
  22. *
  23. */
  24. /*
  25. * Quick 'n Dirty bitmap dumper.
  26. */
  27. #include <stdio.h>
  28. #include <unistd.h>
  29. #include <sys/types.h>
  30. #include <sys/stat.h>
  31. #include <fcntl.h>
  32. #include <string.h>
  33. #include <errno.h>
  34. #include "write_bmp.h"
  35. #define FILENAME_SIZE 1024
  36. struct bmp_header {
  37. unsigned short magic;
  38. unsigned int size;
  39. unsigned int unused;
  40. unsigned int start;
  41. } __attribute__((__packed__));
  42. struct dib_header {
  43. unsigned int size;
  44. unsigned int width;
  45. unsigned int height;
  46. unsigned short planes;
  47. unsigned short bpp;
  48. unsigned int compression;
  49. unsigned int data_size;
  50. unsigned int h_res;
  51. unsigned int v_res;
  52. unsigned int colours;
  53. unsigned int important_colours;
  54. unsigned int red_mask;
  55. unsigned int green_mask;
  56. unsigned int blue_mask;
  57. unsigned int alpha_mask;
  58. unsigned int colour_space;
  59. unsigned int unused[12];
  60. } __attribute__((__packed__));
  61. static void
  62. bmp_header_write(int fd, int width, int height, int bgra, int noflip, int alpha)
  63. {
  64. struct bmp_header bmp_header = {
  65. .magic = 0x4d42,
  66. .size = (width * height * 4) +
  67. sizeof(struct bmp_header) + sizeof(struct dib_header),
  68. .start = sizeof(struct bmp_header) + sizeof(struct dib_header),
  69. };
  70. struct dib_header dib_header = {
  71. .size = sizeof(struct dib_header),
  72. .width = width,
  73. .height = noflip ? -height : height,
  74. .planes = 1,
  75. .bpp = 32,
  76. .compression = 3,
  77. .data_size = 4 * width * height,
  78. .h_res = 0xB13,
  79. .v_res = 0xB13,
  80. .colours = 0,
  81. .important_colours = 0,
  82. .red_mask = 0x000000FF,
  83. .green_mask = 0x0000FF00,
  84. .blue_mask = 0x00FF0000,
  85. .alpha_mask = alpha ? 0xFF000000 : 0x00000000,
  86. .colour_space = 0x57696E20,
  87. };
  88. if (bgra) {
  89. dib_header.red_mask = 0x00FF0000;
  90. dib_header.blue_mask = 0x000000FF;
  91. }
  92. write(fd, &bmp_header, sizeof(struct bmp_header));
  93. write(fd, &dib_header, sizeof(struct dib_header));
  94. }
  95. void
  96. bmp_dump32(char *buffer, unsigned width, unsigned height, bool bgra, const char *filename)
  97. {
  98. int fd;
  99. fd = open(filename, O_WRONLY| O_TRUNC | O_CREAT, 0666);
  100. if (fd == -1) {
  101. printf("Failed to open %s: %s\n", filename, strerror(errno));
  102. return;
  103. }
  104. bmp_header_write(fd, width, height, bgra, false, true);
  105. write(fd, buffer, width * height * 4);
  106. }
  107. void
  108. bmp_dump32_noflip(char *buffer, unsigned width, unsigned height, bool bgra, const char *filename)
  109. {
  110. int fd;
  111. fd = open(filename, O_WRONLY| O_TRUNC | O_CREAT, 0666);
  112. if (fd == -1) {
  113. printf("Failed to open %s: %s\n", filename, strerror(errno));
  114. return;
  115. }
  116. bmp_header_write(fd, width, height, bgra, true, true);
  117. write(fd, buffer, width * height * 4);
  118. }
  119. void
  120. bmp_dump32_ex(char *buffer, unsigned width, unsigned height, bool flip, bool bgra, bool alpha, const char *filename)
  121. {
  122. int fd;
  123. fd = open(filename, O_WRONLY| O_TRUNC | O_CREAT, 0666);
  124. if (fd == -1) {
  125. printf("Failed to open %s: %s\n", filename, strerror(errno));
  126. return;
  127. }
  128. bmp_header_write(fd, width, height, bgra, flip, alpha);
  129. write(fd, buffer, width * height * 4);
  130. }