alpar@1: /* glprgr.c */ alpar@1: alpar@1: /*********************************************************************** alpar@1: * This code is part of GLPK (GNU Linear Programming Kit). alpar@1: * alpar@1: * Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, alpar@1: * 2009, 2010 Andrew Makhorin, Department for Applied Informatics, alpar@1: * Moscow Aviation Institute, Moscow, Russia. All rights reserved. alpar@1: * E-mail: . alpar@1: * alpar@1: * GLPK is free software: you can redistribute it and/or modify it alpar@1: * under the terms of the GNU General Public License as published by alpar@1: * the Free Software Foundation, either version 3 of the License, or alpar@1: * (at your option) any later version. alpar@1: * alpar@1: * GLPK is distributed in the hope that it will be useful, but WITHOUT alpar@1: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY alpar@1: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public alpar@1: * License for more details. alpar@1: * alpar@1: * You should have received a copy of the GNU General Public License alpar@1: * along with GLPK. If not, see . alpar@1: ***********************************************************************/ alpar@1: alpar@1: #define _GLPSTD_ERRNO alpar@1: #define _GLPSTD_STDIO alpar@1: #include "glpenv.h" alpar@1: #include "glprgr.h" alpar@1: #define xfault xerror alpar@1: alpar@1: /*********************************************************************** alpar@1: * NAME alpar@1: * alpar@1: * rgr_write_bmp16 - write 16-color raster image in BMP file format alpar@1: * alpar@1: * SYNOPSIS alpar@1: * alpar@1: * #include "glprgr.h" alpar@1: * int rgr_write_bmp16(const char *fname, int m, int n, const char alpar@1: * map[]); alpar@1: * alpar@1: * DESCRIPTION alpar@1: * alpar@1: * The routine rgr_write_bmp16 writes 16-color raster image in alpar@1: * uncompressed BMP file format (Windows bitmap) to a binary file whose alpar@1: * name is specified by the character string fname. alpar@1: * alpar@1: * The parameters m and n specify, respectively, the number of rows and alpar@1: * the numbers of columns (i.e. height and width) of the raster image. alpar@1: * alpar@1: * The character array map has m*n elements. Elements map[0, ..., n-1] alpar@1: * correspond to the first (top) scanline, elements map[n, ..., 2*n-1] alpar@1: * correspond to the second scanline, etc. alpar@1: * alpar@1: * Each element of the array map specifies a color of the corresponding alpar@1: * pixel as 8-bit binary number XXXXIRGB, where four high-order bits (X) alpar@1: * are ignored, I is high intensity bit, R is red color bit, G is green alpar@1: * color bit, and B is blue color bit. Thus, all 16 possible colors are alpar@1: * coded as following hexadecimal numbers: alpar@1: * alpar@1: * 0x00 = black 0x08 = dark gray alpar@1: * 0x01 = blue 0x09 = bright blue alpar@1: * 0x02 = green 0x0A = bright green alpar@1: * 0x03 = cyan 0x0B = bright cyan alpar@1: * 0x04 = red 0x0C = bright red alpar@1: * 0x05 = magenta 0x0D = bright magenta alpar@1: * 0x06 = brown 0x0E = yellow alpar@1: * 0x07 = light gray 0x0F = white alpar@1: * alpar@1: * RETURNS alpar@1: * alpar@1: * If no error occured, the routine returns zero; otherwise, it prints alpar@1: * an appropriate error message and returns non-zero. */ alpar@1: alpar@1: static void put_byte(FILE *fp, int c) alpar@1: { fputc(c, fp); alpar@1: return; alpar@1: } alpar@1: alpar@1: static void put_word(FILE *fp, int w) alpar@1: { /* big endian */ alpar@1: put_byte(fp, w); alpar@1: put_byte(fp, w >> 8); alpar@1: return; alpar@1: } alpar@1: alpar@1: static void put_dword(FILE *fp, int d) alpar@1: { /* big endian */ alpar@1: put_word(fp, d); alpar@1: put_word(fp, d >> 16); alpar@1: return; alpar@1: } alpar@1: alpar@1: int rgr_write_bmp16(const char *fname, int m, int n, const char map[]) alpar@1: { FILE *fp; alpar@1: int offset, bmsize, i, j, b, ret = 0; alpar@1: if (!(1 <= m && m <= 32767)) alpar@1: xfault("rgr_write_bmp16: m = %d; invalid height\n", m); alpar@1: if (!(1 <= n && n <= 32767)) alpar@1: xfault("rgr_write_bmp16: n = %d; invalid width\n", n); alpar@1: fp = fopen(fname, "wb"); alpar@1: if (fp == NULL) alpar@1: { xprintf("rgr_write_bmp16: unable to create `%s' - %s\n", alpar@1: fname, strerror(errno)); alpar@1: ret = 1; alpar@1: goto fini; alpar@1: } alpar@1: offset = 14 + 40 + 16 * 4; alpar@1: bmsize = (4 * n + 31) / 32; alpar@1: /* struct BMPFILEHEADER (14 bytes) */ alpar@1: /* UINT bfType */ put_byte(fp, 'B'), put_byte(fp, 'M'); alpar@1: /* DWORD bfSize */ put_dword(fp, offset + bmsize * 4); alpar@1: /* UINT bfReserved1 */ put_word(fp, 0); alpar@1: /* UNIT bfReserved2 */ put_word(fp, 0); alpar@1: /* DWORD bfOffBits */ put_dword(fp, offset); alpar@1: /* struct BMPINFOHEADER (40 bytes) */ alpar@1: /* DWORD biSize */ put_dword(fp, 40); alpar@1: /* LONG biWidth */ put_dword(fp, n); alpar@1: /* LONG biHeight */ put_dword(fp, m); alpar@1: /* WORD biPlanes */ put_word(fp, 1); alpar@1: /* WORD biBitCount */ put_word(fp, 4); alpar@1: /* DWORD biCompression */ put_dword(fp, 0 /* BI_RGB */); alpar@1: /* DWORD biSizeImage */ put_dword(fp, 0); alpar@1: /* LONG biXPelsPerMeter */ put_dword(fp, 2953 /* 75 dpi */); alpar@1: /* LONG biYPelsPerMeter */ put_dword(fp, 2953 /* 75 dpi */); alpar@1: /* DWORD biClrUsed */ put_dword(fp, 0); alpar@1: /* DWORD biClrImportant */ put_dword(fp, 0); alpar@1: /* struct RGBQUAD (16 * 4 = 64 bytes) */ alpar@1: /* CGA-compatible colors: */ alpar@1: /* 0x00 = black */ put_dword(fp, 0x000000); alpar@1: /* 0x01 = blue */ put_dword(fp, 0x000080); alpar@1: /* 0x02 = green */ put_dword(fp, 0x008000); alpar@1: /* 0x03 = cyan */ put_dword(fp, 0x008080); alpar@1: /* 0x04 = red */ put_dword(fp, 0x800000); alpar@1: /* 0x05 = magenta */ put_dword(fp, 0x800080); alpar@1: /* 0x06 = brown */ put_dword(fp, 0x808000); alpar@1: /* 0x07 = light gray */ put_dword(fp, 0xC0C0C0); alpar@1: /* 0x08 = dark gray */ put_dword(fp, 0x808080); alpar@1: /* 0x09 = bright blue */ put_dword(fp, 0x0000FF); alpar@1: /* 0x0A = bright green */ put_dword(fp, 0x00FF00); alpar@1: /* 0x0B = bright cyan */ put_dword(fp, 0x00FFFF); alpar@1: /* 0x0C = bright red */ put_dword(fp, 0xFF0000); alpar@1: /* 0x0D = bright magenta */ put_dword(fp, 0xFF00FF); alpar@1: /* 0x0E = yellow */ put_dword(fp, 0xFFFF00); alpar@1: /* 0x0F = white */ put_dword(fp, 0xFFFFFF); alpar@1: /* pixel data bits */ alpar@1: b = 0; alpar@1: for (i = m - 1; i >= 0; i--) alpar@1: { for (j = 0; j < ((n + 7) / 8) * 8; j++) alpar@1: { b <<= 4; alpar@1: b |= (j < n ? map[i * n + j] & 15 : 0); alpar@1: if (j & 1) put_byte(fp, b); alpar@1: } alpar@1: } alpar@1: fflush(fp); alpar@1: if (ferror(fp)) alpar@1: { xprintf("rgr_write_bmp16: write error on `%s' - %s\n", alpar@1: fname, strerror(errno)); alpar@1: ret = 1; alpar@1: } alpar@1: fini: if (fp != NULL) fclose(fp); alpar@1: return ret; alpar@1: } alpar@1: alpar@1: /* eof */