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