bitblt.c

changeset 2
8ca2aaf0513f
child 3
317de52d8a63
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/bitblt.c	Thu Dec 27 11:04:43 2001 +0000
     1.3 @@ -0,0 +1,124 @@
     1.4 +#include <stdlib.h>
     1.5 +
     1.6 +#include "bitblt.h"
     1.7 +
     1.8 +static inline u8 pixel_mask (x)
     1.9 +{
    1.10 +#ifdef LSB_LEFT
    1.11 +  return (1 << x);
    1.12 +#else
    1.13 +  return (1 << (7 - x));
    1.14 +#endif
    1.15 +}
    1.16 +
    1.17 +static inline u32 rect_width (Rect r)
    1.18 +{
    1.19 +  return (r.lower_right.x - r.upper_left.x);
    1.20 +}
    1.21 +
    1.22 +static inline u32 rect_height (Rect r)
    1.23 +{
    1.24 +  return (r.lower_right.y - r.upper_left.y);
    1.25 +}
    1.26 +
    1.27 +Bitmap *create_bitmap (u32 width, u32 height)
    1.28 +{
    1.29 +  Bitmap *bitmap;
    1.30 +
    1.31 +  bitmap = calloc (1, sizeof (Bitmap));
    1.32 +  if (! bitmap)
    1.33 +    return (NULL);
    1.34 +  bitmap->width = width;
    1.35 +  bitmap->height = height;
    1.36 +  bitmap->rowbytes = (width - 1) / 8 + 1;
    1.37 +  bitmap->bits = calloc (height * bitmap->rowbytes, 1);
    1.38 +  if (! bitmap->bits)
    1.39 +    {
    1.40 +      free (bitmap);
    1.41 +      return (NULL);
    1.42 +    }
    1.43 +  return (bitmap);
    1.44 +}
    1.45 +
    1.46 +void free_bitmap (Bitmap *bitmap)
    1.47 +{
    1.48 +  free (bitmap->bits);
    1.49 +  free (bitmap);
    1.50 +}
    1.51 +
    1.52 +boolean get_pixel (Bitmap *bitmap, Point coord)
    1.53 +{
    1.54 +  u8 *p;
    1.55 +  if ((coord.x >= bitmap->width) || (coord.y >= bitmap->height))
    1.56 +    return (0);
    1.57 +  p = bitmap->bits + coord.y * bitmap->rowbytes + coord.x / 8;
    1.58 +  return ((*p & pixel_mask (coord.x & 7)) != 0);
    1.59 +}
    1.60 +
    1.61 +void set_pixel (Bitmap *bitmap, Point coord, boolean value)
    1.62 +{
    1.63 +  u8 *p;
    1.64 +  if ((coord.x >= bitmap->width) || (coord.y >= bitmap->height))
    1.65 +    return;
    1.66 +  p = bitmap->bits + coord.y * bitmap->rowbytes + coord.x / 8;
    1.67 +  if (value)
    1.68 +    *p |= pixel_mask (coord.x & 7);
    1.69 +  else
    1.70 +    *p &= (0xff ^ pixel_mask (coord.x & 7));
    1.71 +}
    1.72 +
    1.73 +
    1.74 +Bitmap *bitblt (Bitmap *src_bitmap,
    1.75 +		Rect src_rect,
    1.76 +		Bitmap *dest_bitmap,
    1.77 +		Point dest_upper_left,
    1.78 +		boolean flip_horizontal,
    1.79 +		boolean flip_vertical,
    1.80 +		boolean transpose,
    1.81 +		int tfn)
    1.82 +{
    1.83 +  Point src_point, dest_point;
    1.84 +  boolean src_pixel, dest_pixel;
    1.85 +
    1.86 +  if (! dest_bitmap)
    1.87 +    {
    1.88 +      if (transpose)
    1.89 +	dest_bitmap = create_bitmap (rect_height (src_rect),
    1.90 +				     rect_width (src_rect));
    1.91 +      else
    1.92 +	dest_bitmap = create_bitmap (rect_width (src_rect),
    1.93 +				     rect_height (src_rect));
    1.94 +      if (! dest_bitmap)
    1.95 +	return (NULL);
    1.96 +    }
    1.97 +
    1.98 +  for (src_point.y = src_rect.upper_left.y;
    1.99 +       src_point.y < src_rect.lower_right.y;
   1.100 +       src_point.y++)
   1.101 +    {
   1.102 +      for (src_point.x = src_rect.upper_left.x;
   1.103 +	   src_point.x < src_rect.lower_right.x;
   1.104 +	   src_point.x++)
   1.105 +	{
   1.106 +	  boolean a, b, c;
   1.107 +
   1.108 +	  if (transpose)
   1.109 +	    {
   1.110 +	      dest_point.x = dest_upper_left.x + (src_point.y - src_rect.upper_left.y);
   1.111 +	      dest_point.y = dest_upper_left.y + (src_point.x - src_rect.upper_left.x);
   1.112 +	    }
   1.113 +	  else
   1.114 +	    {
   1.115 +	      dest_point.x = dest_upper_left.x + (src_point.x - src_rect.upper_left.x);
   1.116 +	      dest_point.y = dest_upper_left.y + (src_point.y - src_rect.upper_left.y);
   1.117 +	    }
   1.118 +
   1.119 +	  a = get_pixel (src_bitmap, src_point);
   1.120 +	  b = get_pixel (dest_bitmap, dest_point);
   1.121 +	  c = (tfn & (1 << (a * 2 + b))) != 0;
   1.122 +
   1.123 +	  set_pixel (dest_bitmap, dest_point, c);
   1.124 +	}
   1.125 +    }
   1.126 +  return (dest_bitmap);
   1.127 +}