Initial revision

Thu, 27 Dec 2001 11:04:43 +0000

author
eric
date
Thu, 27 Dec 2001 11:04:43 +0000
changeset 2
8ca2aaf0513f
parent 1
a6dc00305f0d
child 3
317de52d8a63

Initial revision

bitblt.c file | annotate | diff | revisions
bitblt.h file | annotate | diff | revisions
bitblt_test.c file | annotate | diff | revisions
     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 +}
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/bitblt.h	Thu Dec 27 11:04:43 2001 +0000
     2.3 @@ -0,0 +1,44 @@
     2.4 +typedef unsigned char u8;
     2.5 +typedef unsigned int u32;
     2.6 +typedef int boolean;
     2.7 +
     2.8 +typedef struct Point
     2.9 +{
    2.10 +  u32 x;
    2.11 +  u32 y;
    2.12 +} Point;
    2.13 +
    2.14 +typedef struct Rect
    2.15 +{
    2.16 +  Point upper_left;
    2.17 +  Point lower_right;
    2.18 +} Rect;
    2.19 +
    2.20 +typedef struct Bitmap
    2.21 +{
    2.22 +  u8 *bits;
    2.23 +  u32 width;
    2.24 +  u32 height;
    2.25 +  u32 rowbytes;
    2.26 +} Bitmap;
    2.27 +
    2.28 +
    2.29 +#define TF_SRC 0xc
    2.30 +#define TF_AND 0x8
    2.31 +#define TF_OR  0xe
    2.32 +#define TF_XOR 0x6
    2.33 +
    2.34 +
    2.35 +Bitmap *create_bitmap (u32 width, u32 height);
    2.36 +void free_bitmap (Bitmap *bitmap);
    2.37 +boolean get_pixel (Bitmap *bitmap, Point coord);
    2.38 +void set_pixel (Bitmap *bitmap, Point coord, boolean value);
    2.39 +
    2.40 +Bitmap *bitblt (Bitmap *src_bitmap,
    2.41 +		Rect src_rect,
    2.42 +		Bitmap *dest_bitmap,
    2.43 +		Point dest_upper_left,
    2.44 +		boolean flip_horizontal,
    2.45 +		boolean flip_vertical,
    2.46 +		boolean transpose,
    2.47 +		int tfn);
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/bitblt_test.c	Thu Dec 27 11:04:43 2001 +0000
     3.3 @@ -0,0 +1,99 @@
     3.4 +#include <stdio.h>
     3.5 +
     3.6 +#include "bitblt.h"
     3.7 +
     3.8 +
     3.9 +#define WIDTH 20
    3.10 +#define HEIGHT 9
    3.11 +
    3.12 +char test_data [HEIGHT][WIDTH] =
    3.13 +{
    3.14 +  ".....XXXXXXXXXX.....",
    3.15 +  ".....XX.......X.....",
    3.16 +  "XXXXXX.X......XXXXXX",
    3.17 +  ".....X..X.....X.....",
    3.18 +  ".....X...X....X.....",
    3.19 +  ".....X....X...X.....",
    3.20 +  ".....X.....X..X.....",
    3.21 +  ".....XXXXXXXXXX.....",
    3.22 +  ".....XXXXXXXXXX....."
    3.23 +};
    3.24 +
    3.25 +Bitmap *setup (void)
    3.26 +{
    3.27 +  Bitmap *b;
    3.28 +  Point p;
    3.29 +
    3.30 +  b = create_bitmap (WIDTH, HEIGHT);
    3.31 +  if (! b)
    3.32 +    return (NULL);
    3.33 +
    3.34 +  for (p.y = 0; p.y < HEIGHT; p.y++)
    3.35 +    for (p.x = 0; p.x < WIDTH; p.x++)
    3.36 +      set_pixel (b, p, test_data [p.y][p.x] == 'X');
    3.37 +
    3.38 +  return (b);
    3.39 +}
    3.40 +
    3.41 +void print_bitmap (FILE *o, Bitmap *b)
    3.42 +{
    3.43 +  Point p;
    3.44 +  printf ("rowbytes: %d\n", b->rowbytes);
    3.45 +  for (p.y = 0; p.y < b->height; p.y++)
    3.46 +    {
    3.47 +      for (p.x = 0; p.x < b->width; p.x++)
    3.48 +	fputc (".X" [get_pixel (b, p)], o);
    3.49 +      fprintf (o, "\n");
    3.50 +    }
    3.51 +}
    3.52 +
    3.53 +
    3.54 +int main (int argc, char *argv[])
    3.55 +{
    3.56 +  Bitmap *b;
    3.57 +  Bitmap *b2;
    3.58 +  Rect r;
    3.59 +  Point p;
    3.60 +
    3.61 +  b = setup ();
    3.62 +  if (! b)
    3.63 +    {
    3.64 +      fprintf (stderr, "setup failed\n");
    3.65 +      exit (2);
    3.66 +    }
    3.67 +
    3.68 +  print_bitmap (stdout, b);
    3.69 +
    3.70 +  printf ("\n");
    3.71 +
    3.72 +#if 0
    3.73 +  b2 = create_bitmap (b->height, b->width);
    3.74 +  if (! b2)
    3.75 +    {
    3.76 +      fprintf (stderr, "create_bitmap failed\n");
    3.77 +      exit (2);
    3.78 +    }
    3.79 +#endif
    3.80 +
    3.81 +  r.upper_left.x = r.upper_left.y = 0;
    3.82 +  r.lower_right.x = b->width;
    3.83 +  r.lower_right.y = b->height;
    3.84 +  p.x = p.y = 0;
    3.85 +
    3.86 +  b2 = bitblt (b, r,
    3.87 +	       NULL, p,
    3.88 +	       0, 0, 
    3.89 +	       1,  /* transpose */
    3.90 +	       TF_SRC);
    3.91 +  if (! b2)
    3.92 +    {
    3.93 +      fprintf (stderr, "bitblt failed\n");
    3.94 +      exit (2);
    3.95 +    }
    3.96 +
    3.97 +  print_bitmap (stdout, b2);
    3.98 +
    3.99 +  exit (0);
   3.100 +}
   3.101 +
   3.102 +