home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / xicon05.zip / bitmap.c next >
C/C++ Source or Header  |  1993-05-06  |  3KB  |  122 lines

  1. /* This file is bitmap.c (part of XIcon)
  2.  *
  3.  * Copyright (C) 1993 by Norman Walsh
  4.  *
  5.  *   This program is free software; you can redistribute it and/or modify
  6.  *   it under the terms of the GNU General Public License as published by
  7.  *   the Free Software Foundation; either version 2 of the License, or
  8.  *   (at your option) any later version.
  9.  *
  10.  *   This program is distributed in the hope that it will be useful,
  11.  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  *   GNU General Public License for more details.
  14.  *
  15.  *   You should have received a copy of the GNU General Public License
  16.  *   along with this program; if not, write to the Free Software
  17.  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  *
  19.  ************************************************************************/
  20.  
  21. #include "icondata.h"
  22.  
  23. BYTE get_bit(bitmap_info m, int row, int col)
  24. {
  25.   BYTE *ch, b, mask;
  26.   int offset, firstbit, firstbyte;
  27.   int pixels_per_byte, bitnumber;
  28.   int count;
  29.  
  30.   firstbit = ((col - 1) * m.bits_per_pixel) + 1;
  31.   firstbyte = firstbit / 8;
  32.   if ((firstbit % 8 != 0) || (firstbit == 0))
  33.     firstbyte++;
  34.  
  35.   offset = m.bytewidth * (row - 1) + (firstbyte - 1);
  36.   ch = m.map + offset;
  37.  
  38.   pixels_per_byte = 8 / m.bits_per_pixel;
  39.  
  40.   bitnumber = col % pixels_per_byte;
  41.   if (bitnumber == 0)
  42.     bitnumber = pixels_per_byte;
  43.  
  44.   /* Now move the bits for the pixel to the lowest bits in the byte ... */
  45.  
  46.   b = *ch >> ((pixels_per_byte - bitnumber) * m.bits_per_pixel);
  47.  
  48.   /* And mask out the other bits ... */
  49.   mask = 0x01;
  50.   for (count = 1; count < m.bits_per_pixel; count++)
  51.     {
  52.       mask <<= 1;
  53.       mask |= 0x01;
  54.     }
  55.  
  56.   return (b & mask);
  57. }
  58.  
  59. void set_bit(bitmap_info m, int row, int col, BYTE b)
  60. {
  61.   BYTE *ch, mask;
  62.   int offset, firstbit, firstbyte;
  63.   int pixels_per_byte, bitnumber;
  64.   int count;
  65.  
  66.   firstbit = ((col - 1) * m.bits_per_pixel) + 1;
  67.   firstbyte = firstbit / 8;
  68.   if ((firstbit % 8 != 0) || (firstbit == 0))
  69.     firstbyte++;
  70.  
  71.   offset = m.bytewidth * (row - 1) + (firstbyte - 1);
  72.   ch = m.map + offset;
  73.  
  74.   pixels_per_byte = 8 / m.bits_per_pixel;
  75.  
  76.   bitnumber = col % pixels_per_byte;
  77.   if (bitnumber == 0)
  78.     bitnumber = pixels_per_byte;
  79.  
  80.   /* Now move the bits for the pixel over to the correct column... */
  81.  
  82.   b = b << ((pixels_per_byte - bitnumber) * m.bits_per_pixel);
  83.  
  84.   /* And construct a mask for these bits ... */
  85.   mask = 0x01;
  86.   for (count = 1; count < m.bits_per_pixel; count++)
  87.     {
  88.       mask <<= 1;
  89.       mask |= 0x01;
  90.     }
  91.  
  92.   mask = mask << ((pixels_per_byte - bitnumber) * m.bits_per_pixel);
  93.  
  94.   b = (b & mask);
  95.   mask = (mask ^ 0xFF);
  96.  
  97.   *ch = (*ch & mask);
  98.   *ch = (*ch | b);
  99. }
  100.  
  101. void printmap(bitmap_info m)
  102. {
  103.   char *hexdigits = "0123456789ABCDEF";
  104.   int row, col;
  105.   BYTE b;
  106.  
  107.   printf("\n");
  108.  
  109.   if (m.width == 0)
  110.     return;
  111.  
  112.   for (row = 1; row <= m.height; row++)
  113.     {
  114.       for (col = 1; col <= m.width; col++)
  115.     {
  116.       b = get_bit(m, row, col);
  117.       printf("%c", hexdigits[b]);
  118.     }
  119.       printf("\n");
  120.     }
  121. }
  122.