40#define MAX_COMPRESSION_METHOD 13    
   50int main (
int argc, 
char *argv[]) {
 
   57   unsigned char rsvd_hdr[4]; 
 
   72   int bits_per_pixel = 0;    
 
   77   int compression_method=0;  
 
   82   int important_colors = 0;  
 
   90   unsigned char color_map[2][4]; 
 
   96   unsigned image_bytes[544*72];
 
  102   unsigned image_xor = 0x00; 
 
  125   unsigned standard_header [62] = {
 
  126       0x42, 0x4d, 0x3e, 0x99, 0x00, 0x00, 0x00, 0x00,
 
  127       0x00, 0x00, 0x3e, 0x00, 0x00, 0x00, 0x28, 0x00,
 
  128       0x00, 0x00, 0x40, 0x02, 0x00, 0x00, 0x20, 0x02,
 
  129       0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00,
 
  130       0x00, 0x00, 0x00, 0x99, 0x00, 0x00, 0xc4, 0x0e,
 
  131       0x00, 0x00, 0xc4, 0x0e, 0x00, 0x00, 0x00, 0x00,
 
  132       0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
 
  133       0x00, 0x00, 0xff, 0xff, 0xff, 0x00
 
  139   char *infile=
"", *outfile=
"";  
 
  146      for (i = 1; i < argc; i++) {
 
  147         if (argv[i][0] == 
'-') {  
 
  148            switch (argv[i][1]) {
 
  150                  infile = &argv[i][2];
 
  153                  outfile = &argv[i][2];
 
  159                  fprintf (stderr, 
"unibmpbump version %s\n\n", 
VERSION);
 
  163                  if (strcmp (argv[i], 
"--verbose") == 0) {
 
  166                  else if (strcmp (argv[i], 
"--version") == 0) {
 
  167                     fprintf (stderr, 
"unibmpbump version %s\n\n", 
VERSION);
 
  172                  fprintf (stderr, 
"\nSyntax:\n\n");
 
  173                  fprintf (stderr, 
"   unibmpbump ");
 
  174                  fprintf (stderr, 
"-i<Input_File> -o<Output_File>\n\n");
 
  175                  fprintf (stderr, 
"-v or --verbose gives verbose output");
 
  176                  fprintf (stderr, 
" on stderr\n\n");
 
  177                  fprintf (stderr, 
"-V or --version prints version");
 
  178                  fprintf (stderr, 
" on stderr and exits\n\n");
 
  179                  fprintf (stderr, 
"\nExample:\n\n");
 
  180                  fprintf (stderr, 
"   unibmpbump -iuni0101.bmp");
 
  181                  fprintf (stderr, 
" -onew-uni0101.bmp\n\n");
 
  192   if (strlen (infile) > 0) {
 
  193      if ((infp = fopen (infile, 
"r")) == NULL) {
 
  194         fprintf (stderr, 
"Error: can't open %s for input.\n", infile);
 
  201   if (strlen (outfile) > 0) {
 
  202      if ((outfp = fopen (outfile, 
"w")) == NULL) {
 
  203         fprintf (stderr, 
"Error: can't open %s for output.\n", outfile);
 
  215   file_format[2] = 
'\0';  
 
  233   if (strncmp (file_format, 
"BM", 2) != 0) {
 
  234      fprintf (stderr, 
"\nInvalid file format: not file type \"BM\".\n\n");
 
  239      fprintf (stderr, 
"\nFile Header:\n");
 
  240      fprintf (stderr, 
"   File Type:   \"%s\"\n", file_format);
 
  241      fprintf (stderr, 
"   File Size:   %d bytes\n", filesize);
 
  242      fprintf (stderr, 
"   Reserved:   ");
 
  243      for (i = 0; i < 4; i++) fprintf (stderr, 
" 0x%02X", rsvd_hdr[i]);
 
  244      fputc (
'\n', stderr);
 
  245      fprintf (stderr, 
"   Image Start: %d. = 0x%02X = 0%05o\n\n",
 
  246               image_start, image_start, image_start);
 
  265   if (dib_length == 12) { 
 
  271   else if (dib_length >= 40) { 
 
  276      compression_method = 
get_bytes (infp, 4);  
 
  285         true_colors = 1 << bits_per_pixel;
 
  287         true_colors = num_colors;
 
  295      for (i = 40; i < dib_length; i++) (
void)
get_bytes (infp, 1);
 
  299      fprintf (stderr, 
"Device Independent Bitmap (DIB) Header:\n");
 
  300      fprintf (stderr, 
"   DIB Length:  %9d bytes (version = ", dib_length);
 
  302      if      (dib_length ==  12) fprintf (stderr, 
"\"BITMAPCOREHEADER\")\n");
 
  303      else if (dib_length ==  40) fprintf (stderr, 
"\"BITMAPINFOHEADER\")\n");
 
  304      else if (dib_length == 108) fprintf (stderr, 
"\"BITMAPV4HEADER\")\n");
 
  305      else if (dib_length == 124) fprintf (stderr, 
"\"BITMAPV5HEADER\")\n");
 
  306      else fprintf (stderr, 
"unknown)");
 
  307      fprintf (stderr, 
"   Bitmap Width:   %6d pixels\n", image_width);
 
  308      fprintf (stderr, 
"   Bitmap Height:  %6d pixels\n", image_height);
 
  309      fprintf (stderr, 
"   Color Planes:   %6d\n",        num_planes);
 
  310      fprintf (stderr, 
"   Bits per Pixel: %6d\n",        bits_per_pixel);
 
  311      fprintf (stderr, 
"   Compression Method: %2d --> ", compression_method);
 
  313         fprintf (stderr, 
"%s", compression_type [compression_method]);
 
  320      if (compression_method == 0 || compression_method == 11) {
 
  321         fprintf (stderr, 
" (no compression)");
 
  324         fprintf (stderr, 
"Image uses compression; this is unsupported.\n\n");
 
  327      fprintf (stderr, 
"\n");
 
  328      fprintf (stderr, 
"   Image Size:            %5d bytes\n", image_size);
 
  329      fprintf (stderr, 
"   Horizontal Resolution: %5d pixels/meter\n", hres);
 
  330      fprintf (stderr, 
"   Vertical Resolution:   %5d pixels/meter\n", vres);
 
  331      fprintf (stderr, 
"   Number of Colors:      %5d", num_colors);
 
  332      if (num_colors != true_colors) {
 
  333         fprintf (stderr, 
" --> %d", true_colors);
 
  335      fputc (
'\n', stderr);
 
  336      fprintf (stderr, 
"   Important Colors:      %5d", important_colors);
 
  337      if (important_colors == 0)
 
  338         fprintf (stderr, 
" (all colors are important)");
 
  339      fprintf (stderr, 
"\n\n");
 
  345   if (bits_per_pixel <= 8) {
 
  346      for (i = 0; i < 2; i++) {
 
  353      while (i < true_colors) {
 
  358      if (color_map [0][0] >= 128) image_xor = 0xFF;  
 
  362      fprintf (stderr, 
"Color Palette [R, G, B, %s] Values:\n",
 
  363               (dib_length <= 40) ? 
"reserved" : 
"Alpha");
 
  364      for (i = 0; i < 2; i++) {
 
  365         fprintf (stderr, 
"%7d: [", i);
 
  366         fprintf (stderr, 
"%3d,",   color_map [i][0] & 0xFF);
 
  367         fprintf (stderr, 
"%3d,",   color_map [i][1] & 0xFF);
 
  368         fprintf (stderr, 
"%3d,",   color_map [i][2] & 0xFF);
 
  369         fprintf (stderr, 
"%3d]\n", color_map [i][3] & 0xFF);
 
  371      if (image_xor == 0xFF) fprintf (stderr, 
"Will Invert Colors.\n");
 
  372      fputc (
'\n', stderr);
 
  380   if (image_width != 560 && image_width != 576) {
 
  381      fprintf (stderr, 
"\nUnsupported image width: %d\n", image_width);
 
  382      fprintf (stderr, 
"Width should be 560 or 576 pixels.\n\n");
 
  386   if (image_height != 544) {
 
  387      fprintf (stderr, 
"\nUnsupported image height: %d\n", image_height);
 
  388      fprintf (stderr, 
"Height should be 544 pixels.\n\n");
 
  392   if (num_planes != 1) {
 
  393      fprintf (stderr, 
"\nUnsupported number of planes: %d\n", num_planes);
 
  394      fprintf (stderr, 
"Number of planes should be 1.\n\n");
 
  398   if (bits_per_pixel != 1) {
 
  399      fprintf (stderr, 
"\nUnsupported number of bits per pixel: %d\n",
 
  401      fprintf (stderr, 
"Bits per pixel should be 1.\n\n");
 
  405   if (compression_method != 0 && compression_method != 11) {
 
  406      fprintf (stderr, 
"\nUnsupported compression method: %d\n",
 
  408      fprintf (stderr, 
"Compression method should be 1 or 11.\n\n");
 
  412   if (true_colors != 2) {
 
  413      fprintf (stderr, 
"\nUnsupported number of colors: %d\n", true_colors);
 
  414      fprintf (stderr, 
"Number of colors should be 2.\n\n");
 
  423   for (i = 0; i < 62; i++) fputc (standard_header[i], outfp);
 
  431   for (i = 0; i < 544; i++) {
 
  436      if (image_width == 560) {  
 
  437         image_bytes[k++] = 0xFF;
 
  438         image_bytes[k++] = 0xFF;
 
  440      for (j = 0; j < 70; j++) {  
 
  441         image_bytes[k++] = (
get_bytes (infp, 1) & 0xFF) ^ image_xor;
 
  448      if (image_width == 560) {
 
  452         image_bytes[k++] = (
get_bytes (infp, 1) & 0xFF) ^ image_xor;
 
  453         image_bytes[k++] = (
get_bytes (infp, 1) & 0xFF) ^ image_xor;
 
  461   if (image_width == 560) {
 
  465   for (i = 0; i < 544 * 576 / 8; i++) {
 
  466      fputc (image_bytes[i], outfp);
 
  489   unsigned char inchar[4];
 
  492   for (i = 0; i < nbytes; i++) {
 
  493      if (fread (&inchar[i], 1, 1, infp) != 1) {
 
  497   for (i = nbytes; i < 4; i++) inchar[i] = 0;
 
  499   inword = ((inchar[3] & 0xFF) << 24) | ((inchar[2] & 0xFF) << 16) |
 
  500            ((inchar[1] & 0xFF) <<  8) |  (inchar[0] & 0xFF);
 
  521   char zero_pattern[16] = {
 
  522       0x00, 0x00, 0x00, 0x00, 0x18, 0x24, 0x42, 0x42,
 
  523       0x42, 0x42, 0x42, 0x42, 0x24, 0x18, 0x00, 0x00
 
  527   unsigned hgrid[72] = {
 
  528       0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe,
 
  529       0x00, 0x81, 0x81, 0x00, 0x00, 0x81, 0x81, 0x00,
 
  530       0x00, 0x81, 0x81, 0x00, 0x00, 0x81, 0x81, 0x00,
 
  531       0x00, 0x81, 0x81, 0x00, 0x00, 0x81, 0x81, 0x00,
 
  532       0x00, 0x81, 0x81, 0x00, 0x00, 0x81, 0x81, 0x00,
 
  533       0x00, 0x81, 0x81, 0x00, 0x00, 0x81, 0x81, 0x00,
 
  534       0x00, 0x81, 0x81, 0x00, 0x00, 0x81, 0x81, 0x00,
 
  535       0x00, 0x81, 0x81, 0x00, 0x00, 0x81, 0x81, 0x00,
 
  536       0x00, 0x81, 0x81, 0x00, 0x00, 0x81, 0x81, 0x00
 
  544   for (i = 543 - 8; i > 544 - 24; i--) {
 
  546      image_bytes [offset + 0] = image_bytes [offset + 2];
 
  547      image_bytes [offset + 1] = image_bytes [offset + 3];
 
  548      image_bytes [offset + 2] = image_bytes [offset + 4];
 
  549      image_bytes [offset + 3] = image_bytes [offset + 4] =
 
  550         ~zero_pattern[15 - j--] & 0xFF;
 
  556   for (i = 0; i < 16; i++) { 
 
  557      for (j = 0; j < 16; j++) { 
 
  559         offset = (32 * 72 * i) + (9 * 72) + (4 * j) + 8;
 
  560         for (k = 0; k < 16; k++) { 
 
  561            glyph_row = (image_bytes [offset + 0] << 24) |
 
  562                        (image_bytes [offset + 1] << 16) |
 
  563                        (image_bytes [offset + 2] <<  8) |
 
  564                        (image_bytes [offset + 3]);
 
  565            last_pixel = glyph_row & 1;  
 
  567            glyph_row &=  0x0FFFFFFE;
 
  569            glyph_row |=  0xF0000000 | last_pixel;
 
  570            image_bytes [offset + 3] = glyph_row & 0xFF;
 
  572            image_bytes [offset + 2] = glyph_row & 0xFF;
 
  574            image_bytes [offset + 1] = glyph_row & 0xFF;
 
  576            image_bytes [offset + 0] = glyph_row & 0xFF;
 
  583   for (i = 0; i <= 16; i++) {
 
  584      offset = 32 * 72 * i;
 
  585      for (j = 0; j < 72; j++) {
 
  586         image_bytes [offset + j] = hgrid [j];
 
int main(int argc, char *argv[])
The main function.
#define VERSION
Version of this program.
void regrid(unsigned *image_bytes)
After reading in the image, shift it.
#define MAX_COMPRESSION_METHOD
Maximum supported compression method.
unsigned get_bytes(FILE *infp, int nbytes)
Get from 1 to 4 bytes, inclusive, from input file.