PageRenderTime 41ms CodeModel.GetById 29ms app.highlight 9ms RepoModel.GetById 2ms app.codeStats 0ms

/farmR/src/glprgr.c

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