1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/glprgr.c Mon Dec 06 13:09:21 2010 +0100
1.3 @@ -0,0 +1,165 @@
1.4 +/* glprgr.c */
1.5 +
1.6 +/***********************************************************************
1.7 +* This code is part of GLPK (GNU Linear Programming Kit).
1.8 +*
1.9 +* Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
1.10 +* 2009, 2010 Andrew Makhorin, Department for Applied Informatics,
1.11 +* Moscow Aviation Institute, Moscow, Russia. All rights reserved.
1.12 +* E-mail: <mao@gnu.org>.
1.13 +*
1.14 +* GLPK is free software: you can redistribute it and/or modify it
1.15 +* under the terms of the GNU General Public License as published by
1.16 +* the Free Software Foundation, either version 3 of the License, or
1.17 +* (at your option) any later version.
1.18 +*
1.19 +* GLPK is distributed in the hope that it will be useful, but WITHOUT
1.20 +* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
1.21 +* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
1.22 +* License for more details.
1.23 +*
1.24 +* You should have received a copy of the GNU General Public License
1.25 +* along with GLPK. If not, see <http://www.gnu.org/licenses/>.
1.26 +***********************************************************************/
1.27 +
1.28 +#define _GLPSTD_ERRNO
1.29 +#define _GLPSTD_STDIO
1.30 +#include "glpenv.h"
1.31 +#include "glprgr.h"
1.32 +#define xfault xerror
1.33 +
1.34 +/***********************************************************************
1.35 +* NAME
1.36 +*
1.37 +* rgr_write_bmp16 - write 16-color raster image in BMP file format
1.38 +*
1.39 +* SYNOPSIS
1.40 +*
1.41 +* #include "glprgr.h"
1.42 +* int rgr_write_bmp16(const char *fname, int m, int n, const char
1.43 +* map[]);
1.44 +*
1.45 +* DESCRIPTION
1.46 +*
1.47 +* The routine rgr_write_bmp16 writes 16-color raster image in
1.48 +* uncompressed BMP file format (Windows bitmap) to a binary file whose
1.49 +* name is specified by the character string fname.
1.50 +*
1.51 +* The parameters m and n specify, respectively, the number of rows and
1.52 +* the numbers of columns (i.e. height and width) of the raster image.
1.53 +*
1.54 +* The character array map has m*n elements. Elements map[0, ..., n-1]
1.55 +* correspond to the first (top) scanline, elements map[n, ..., 2*n-1]
1.56 +* correspond to the second scanline, etc.
1.57 +*
1.58 +* Each element of the array map specifies a color of the corresponding
1.59 +* pixel as 8-bit binary number XXXXIRGB, where four high-order bits (X)
1.60 +* are ignored, I is high intensity bit, R is red color bit, G is green
1.61 +* color bit, and B is blue color bit. Thus, all 16 possible colors are
1.62 +* coded as following hexadecimal numbers:
1.63 +*
1.64 +* 0x00 = black 0x08 = dark gray
1.65 +* 0x01 = blue 0x09 = bright blue
1.66 +* 0x02 = green 0x0A = bright green
1.67 +* 0x03 = cyan 0x0B = bright cyan
1.68 +* 0x04 = red 0x0C = bright red
1.69 +* 0x05 = magenta 0x0D = bright magenta
1.70 +* 0x06 = brown 0x0E = yellow
1.71 +* 0x07 = light gray 0x0F = white
1.72 +*
1.73 +* RETURNS
1.74 +*
1.75 +* If no error occured, the routine returns zero; otherwise, it prints
1.76 +* an appropriate error message and returns non-zero. */
1.77 +
1.78 +static void put_byte(FILE *fp, int c)
1.79 +{ fputc(c, fp);
1.80 + return;
1.81 +}
1.82 +
1.83 +static void put_word(FILE *fp, int w)
1.84 +{ /* big endian */
1.85 + put_byte(fp, w);
1.86 + put_byte(fp, w >> 8);
1.87 + return;
1.88 +}
1.89 +
1.90 +static void put_dword(FILE *fp, int d)
1.91 +{ /* big endian */
1.92 + put_word(fp, d);
1.93 + put_word(fp, d >> 16);
1.94 + return;
1.95 +}
1.96 +
1.97 +int rgr_write_bmp16(const char *fname, int m, int n, const char map[])
1.98 +{ FILE *fp;
1.99 + int offset, bmsize, i, j, b, ret = 0;
1.100 + if (!(1 <= m && m <= 32767))
1.101 + xfault("rgr_write_bmp16: m = %d; invalid height\n", m);
1.102 + if (!(1 <= n && n <= 32767))
1.103 + xfault("rgr_write_bmp16: n = %d; invalid width\n", n);
1.104 + fp = fopen(fname, "wb");
1.105 + if (fp == NULL)
1.106 + { xprintf("rgr_write_bmp16: unable to create `%s' - %s\n",
1.107 + fname, strerror(errno));
1.108 + ret = 1;
1.109 + goto fini;
1.110 + }
1.111 + offset = 14 + 40 + 16 * 4;
1.112 + bmsize = (4 * n + 31) / 32;
1.113 + /* struct BMPFILEHEADER (14 bytes) */
1.114 + /* UINT bfType */ put_byte(fp, 'B'), put_byte(fp, 'M');
1.115 + /* DWORD bfSize */ put_dword(fp, offset + bmsize * 4);
1.116 + /* UINT bfReserved1 */ put_word(fp, 0);
1.117 + /* UNIT bfReserved2 */ put_word(fp, 0);
1.118 + /* DWORD bfOffBits */ put_dword(fp, offset);
1.119 + /* struct BMPINFOHEADER (40 bytes) */
1.120 + /* DWORD biSize */ put_dword(fp, 40);
1.121 + /* LONG biWidth */ put_dword(fp, n);
1.122 + /* LONG biHeight */ put_dword(fp, m);
1.123 + /* WORD biPlanes */ put_word(fp, 1);
1.124 + /* WORD biBitCount */ put_word(fp, 4);
1.125 + /* DWORD biCompression */ put_dword(fp, 0 /* BI_RGB */);
1.126 + /* DWORD biSizeImage */ put_dword(fp, 0);
1.127 + /* LONG biXPelsPerMeter */ put_dword(fp, 2953 /* 75 dpi */);
1.128 + /* LONG biYPelsPerMeter */ put_dword(fp, 2953 /* 75 dpi */);
1.129 + /* DWORD biClrUsed */ put_dword(fp, 0);
1.130 + /* DWORD biClrImportant */ put_dword(fp, 0);
1.131 + /* struct RGBQUAD (16 * 4 = 64 bytes) */
1.132 + /* CGA-compatible colors: */
1.133 + /* 0x00 = black */ put_dword(fp, 0x000000);
1.134 + /* 0x01 = blue */ put_dword(fp, 0x000080);
1.135 + /* 0x02 = green */ put_dword(fp, 0x008000);
1.136 + /* 0x03 = cyan */ put_dword(fp, 0x008080);
1.137 + /* 0x04 = red */ put_dword(fp, 0x800000);
1.138 + /* 0x05 = magenta */ put_dword(fp, 0x800080);
1.139 + /* 0x06 = brown */ put_dword(fp, 0x808000);
1.140 + /* 0x07 = light gray */ put_dword(fp, 0xC0C0C0);
1.141 + /* 0x08 = dark gray */ put_dword(fp, 0x808080);
1.142 + /* 0x09 = bright blue */ put_dword(fp, 0x0000FF);
1.143 + /* 0x0A = bright green */ put_dword(fp, 0x00FF00);
1.144 + /* 0x0B = bright cyan */ put_dword(fp, 0x00FFFF);
1.145 + /* 0x0C = bright red */ put_dword(fp, 0xFF0000);
1.146 + /* 0x0D = bright magenta */ put_dword(fp, 0xFF00FF);
1.147 + /* 0x0E = yellow */ put_dword(fp, 0xFFFF00);
1.148 + /* 0x0F = white */ put_dword(fp, 0xFFFFFF);
1.149 + /* pixel data bits */
1.150 + b = 0;
1.151 + for (i = m - 1; i >= 0; i--)
1.152 + { for (j = 0; j < ((n + 7) / 8) * 8; j++)
1.153 + { b <<= 4;
1.154 + b |= (j < n ? map[i * n + j] & 15 : 0);
1.155 + if (j & 1) put_byte(fp, b);
1.156 + }
1.157 + }
1.158 + fflush(fp);
1.159 + if (ferror(fp))
1.160 + { xprintf("rgr_write_bmp16: write error on `%s' - %s\n",
1.161 + fname, strerror(errno));
1.162 + ret = 1;
1.163 + }
1.164 +fini: if (fp != NULL) fclose(fp);
1.165 + return ret;
1.166 +}
1.167 +
1.168 +/* eof */