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