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