bitblt.c

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