glprgr.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /* glprgr.c */
  2. /***********************************************************************
  3. * This code is part of GLPK (GNU Linear Programming Kit).
  4. *
  5. * Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
  6. * 2009, 2010 Andrew Makhorin, Department for Applied Informatics,
  7. * Moscow Aviation Institute, Moscow, Russia. All rights reserved.
  8. * E-mail: <mao@gnu.org>.
  9. *
  10. * GLPK is free software: you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation, either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * GLPK is distributed in the hope that it will be useful, but WITHOUT
  16. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  17. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
  18. * License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with GLPK. If not, see <http://www.gnu.org/licenses/>.
  22. ***********************************************************************/
  23. #define _GLPSTD_ERRNO
  24. #define _GLPSTD_STDIO
  25. #include "glpenv.h"
  26. #include "glprgr.h"
  27. #define xfault xerror
  28. /***********************************************************************
  29. * NAME
  30. *
  31. * rgr_write_bmp16 - write 16-color raster image in BMP file format
  32. *
  33. * SYNOPSIS
  34. *
  35. * #include "glprgr.h"
  36. * int rgr_write_bmp16(const char *fname, int m, int n, const char
  37. * map[]);
  38. *
  39. * DESCRIPTION
  40. *
  41. * The routine rgr_write_bmp16 writes 16-color raster image in
  42. * uncompressed BMP file format (Windows bitmap) to a binary file whose
  43. * name is specified by the character string fname.
  44. *
  45. * The parameters m and n specify, respectively, the number of rows and
  46. * the numbers of columns (i.e. height and width) of the raster image.
  47. *
  48. * The character array map has m*n elements. Elements map[0, ..., n-1]
  49. * correspond to the first (top) scanline, elements map[n, ..., 2*n-1]
  50. * correspond to the second scanline, etc.
  51. *
  52. * Each element of the array map specifies a color of the corresponding
  53. * pixel as 8-bit binary number XXXXIRGB, where four high-order bits (X)
  54. * are ignored, I is high intensity bit, R is red color bit, G is green
  55. * color bit, and B is blue color bit. Thus, all 16 possible colors are
  56. * coded as following hexadecimal numbers:
  57. *
  58. * 0x00 = black 0x08 = dark gray
  59. * 0x01 = blue 0x09 = bright blue
  60. * 0x02 = green 0x0A = bright green
  61. * 0x03 = cyan 0x0B = bright cyan
  62. * 0x04 = red 0x0C = bright red
  63. * 0x05 = magenta 0x0D = bright magenta
  64. * 0x06 = brown 0x0E = yellow
  65. * 0x07 = light gray 0x0F = white
  66. *
  67. * RETURNS
  68. *
  69. * If no error occured, the routine returns zero; otherwise, it prints
  70. * an appropriate error message and returns non-zero. */
  71. static void put_byte(FILE *fp, int c)
  72. { fputc(c, fp);
  73. return;
  74. }
  75. static void put_word(FILE *fp, int w)
  76. { /* big endian */
  77. put_byte(fp, w);
  78. put_byte(fp, w >> 8);
  79. return;
  80. }
  81. static void put_dword(FILE *fp, int d)
  82. { /* big endian */
  83. put_word(fp, d);
  84. put_word(fp, d >> 16);
  85. return;
  86. }
  87. int rgr_write_bmp16(const char *fname, int m, int n, const char map[])
  88. { FILE *fp;
  89. int offset, bmsize, i, j, b, ret = 0;
  90. if (!(1 <= m && m <= 32767))
  91. xfault("rgr_write_bmp16: m = %d; invalid height\n", m);
  92. if (!(1 <= n && n <= 32767))
  93. xfault("rgr_write_bmp16: n = %d; invalid width\n", n);
  94. fp = fopen(fname, "wb");
  95. if (fp == NULL)
  96. { xprintf("rgr_write_bmp16: unable to create `%s' - %s\n",
  97. fname, strerror(errno));
  98. ret = 1;
  99. goto fini;
  100. }
  101. offset = 14 + 40 + 16 * 4;
  102. bmsize = (4 * n + 31) / 32;
  103. /* struct BMPFILEHEADER (14 bytes) */
  104. /* UINT bfType */ put_byte(fp, 'B'), put_byte(fp, 'M');
  105. /* DWORD bfSize */ put_dword(fp, offset + bmsize * 4);
  106. /* UINT bfReserved1 */ put_word(fp, 0);
  107. /* UNIT bfReserved2 */ put_word(fp, 0);
  108. /* DWORD bfOffBits */ put_dword(fp, offset);
  109. /* struct BMPINFOHEADER (40 bytes) */
  110. /* DWORD biSize */ put_dword(fp, 40);
  111. /* LONG biWidth */ put_dword(fp, n);
  112. /* LONG biHeight */ put_dword(fp, m);
  113. /* WORD biPlanes */ put_word(fp, 1);
  114. /* WORD biBitCount */ put_word(fp, 4);
  115. /* DWORD biCompression */ put_dword(fp, 0 /* BI_RGB */);
  116. /* DWORD biSizeImage */ put_dword(fp, 0);
  117. /* LONG biXPelsPerMeter */ put_dword(fp, 2953 /* 75 dpi */);
  118. /* LONG biYPelsPerMeter */ put_dword(fp, 2953 /* 75 dpi */);
  119. /* DWORD biClrUsed */ put_dword(fp, 0);
  120. /* DWORD biClrImportant */ put_dword(fp, 0);
  121. /* struct RGBQUAD (16 * 4 = 64 bytes) */
  122. /* CGA-compatible colors: */
  123. /* 0x00 = black */ put_dword(fp, 0x000000);
  124. /* 0x01 = blue */ put_dword(fp, 0x000080);
  125. /* 0x02 = green */ put_dword(fp, 0x008000);
  126. /* 0x03 = cyan */ put_dword(fp, 0x008080);
  127. /* 0x04 = red */ put_dword(fp, 0x800000);
  128. /* 0x05 = magenta */ put_dword(fp, 0x800080);
  129. /* 0x06 = brown */ put_dword(fp, 0x808000);
  130. /* 0x07 = light gray */ put_dword(fp, 0xC0C0C0);
  131. /* 0x08 = dark gray */ put_dword(fp, 0x808080);
  132. /* 0x09 = bright blue */ put_dword(fp, 0x0000FF);
  133. /* 0x0A = bright green */ put_dword(fp, 0x00FF00);
  134. /* 0x0B = bright cyan */ put_dword(fp, 0x00FFFF);
  135. /* 0x0C = bright red */ put_dword(fp, 0xFF0000);
  136. /* 0x0D = bright magenta */ put_dword(fp, 0xFF00FF);
  137. /* 0x0E = yellow */ put_dword(fp, 0xFFFF00);
  138. /* 0x0F = white */ put_dword(fp, 0xFFFFFF);
  139. /* pixel data bits */
  140. b = 0;
  141. for (i = m - 1; i >= 0; i--)
  142. { for (j = 0; j < ((n + 7) / 8) * 8; j++)
  143. { b <<= 4;
  144. b |= (j < n ? map[i * n + j] & 15 : 0);
  145. if (j & 1) put_byte(fp, b);
  146. }
  147. }
  148. fflush(fp);
  149. if (ferror(fp))
  150. { xprintf("rgr_write_bmp16: write error on `%s' - %s\n",
  151. fname, strerror(errno));
  152. ret = 1;
  153. }
  154. fini: if (fp != NULL) fclose(fp);
  155. return ret;
  156. }
  157. /* eof */