bitblt.c

Tue, 01 Jan 2002 10:18:33 +0000

author
eric
date
Tue, 01 Jan 2002 10:18:33 +0000
changeset 33
44d823824a46
parent 11
30d18cf8bb67
child 35
41804cc569ab
permissions
-rw-r--r--

change coordinates to signed.

     1 #include <stdlib.h>
     3 #include "type.h"
     4 #include "bitblt.h"
     6 static inline u8 pixel_mask (x)
     7 {
     8 #ifdef LSB_LEFT
     9   return (1 << x);
    10 #else
    11   return (1 << (7 - x));
    12 #endif
    13 }
    15 static inline u32 rect_width (Rect r)
    16 {
    17   return (r.lower_right.x - r.upper_left.x);
    18 }
    20 static inline u32 rect_height (Rect r)
    21 {
    22   return (r.lower_right.y - r.upper_left.y);
    23 }
    25 Bitmap *create_bitmap (u32 width, u32 height)
    26 {
    27   Bitmap *bitmap;
    29   bitmap = calloc (1, sizeof (Bitmap));
    30   if (! bitmap)
    31     return (NULL);
    32   bitmap->width = width;
    33   bitmap->height = height;
    34   bitmap->rowbytes = (width - 1) / 8 + 1;
    35   bitmap->bits = calloc (height * bitmap->rowbytes, 1);
    36   if (! bitmap->bits)
    37     {
    38       free (bitmap);
    39       return (NULL);
    40     }
    41   return (bitmap);
    42 }
    44 void free_bitmap (Bitmap *bitmap)
    45 {
    46   free (bitmap->bits);
    47   free (bitmap);
    48 }
    50 boolean get_pixel (Bitmap *bitmap, Point coord)
    51 {
    52   u8 *p;
    53   if ((coord.x < 0) || (coord.y < 0) ||
    54       (coord.x >= bitmap->width) || (coord.y >= bitmap->height))
    55     return (0);
    56   p = bitmap->bits + coord.y * bitmap->rowbytes + coord.x / 8;
    57   return ((*p & pixel_mask (coord.x & 7)) != 0);
    58 }
    60 void set_pixel (Bitmap *bitmap, Point coord, boolean value)
    61 {
    62   u8 *p;
    63   if ((coord.x < 0) || (coord.y < 0) ||
    64       (coord.x >= bitmap->width) || (coord.y >= bitmap->height))
    65     return;
    66   p = bitmap->bits + coord.y * bitmap->rowbytes + coord.x / 8;
    67   if (value)
    68     *p |= pixel_mask (coord.x & 7);
    69   else
    70     *p &= (0xff ^ pixel_mask (coord.x & 7));
    71 }
    74 Bitmap *bitblt (Bitmap *src_bitmap,
    75 		Rect src_rect,
    76 		Bitmap *dest_bitmap,
    77 		Point dest_upper_left,
    78 		int scan,
    79 		int tfn)
    80 {
    81   Point src_point, dest_point;
    83   if (! dest_bitmap)
    84     {
    85       if (scan & TRANSPOSE)
    86 	dest_bitmap = create_bitmap (dest_upper_left.x + rect_height (src_rect),
    87 				     dest_upper_left.y + rect_width (src_rect));
    88       else
    89 	dest_bitmap = create_bitmap (dest_upper_left.x + rect_width (src_rect),
    90 				     dest_upper_left.y + rect_height (src_rect));
    91       if (! dest_bitmap)
    92 	return (NULL);
    93     }
    95   for (src_point.y = src_rect.upper_left.y;
    96        src_point.y < src_rect.lower_right.y;
    97        src_point.y++)
    98     {
    99       for (src_point.x = src_rect.upper_left.x;
   100 	   src_point.x < src_rect.lower_right.x;
   101 	   src_point.x++)
   102 	{
   103 	  boolean a, b, c;
   105 	  if (scan & TRANSPOSE)
   106 	    {
   107 	      dest_point.x = src_point.y - src_rect.upper_left.y;
   108 	      dest_point.y = src_point.x - src_rect.upper_left.x;
   110 	      if (scan & FLIP_H)
   111 		dest_point.x = (rect_height (src_rect) - 1) - dest_point.x;
   113 	      if (scan & FLIP_V)
   114 		dest_point.y = (rect_width (src_rect) - 1) - dest_point.y;
   115 	    }
   116 	  else
   117 	    {
   118 	      dest_point.x = src_point.x - src_rect.upper_left.x;
   119 	      dest_point.y = src_point.y - src_rect.upper_left.y;
   121 	      if (scan & FLIP_H)
   122 		dest_point.x = (rect_width (src_rect) - 1) - dest_point.x;
   124 	      if (scan & FLIP_V)
   125 		dest_point.y = (rect_height (src_rect) - 1) - dest_point.y;
   126 	    }
   128 	  dest_point.x += dest_upper_left.x;
   129 	  dest_point.y += dest_upper_left.y;
   131 	  a = get_pixel (src_bitmap, src_point);
   132 	  b = get_pixel (dest_bitmap, dest_point);
   133 	  c = (tfn & (1 << (a * 2 + b))) != 0;
   135 	  set_pixel (dest_bitmap, dest_point, c);
   136 	}
   137     }
   138   return (dest_bitmap);
   139 }