/farmR/src/glprgr.c

https://code.google.com/p/javawfm/ · C · 164 lines · 86 code · 8 blank · 70 comment · 12 complexity · 27cdbbe6734826376782b39a4554df05 MD5 · raw file

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