bitblt.c

Sat, 29 Dec 2001 17:45:43 +0000

author
eric
date
Sat, 29 Dec 2001 17:45:43 +0000
changeset 11
30d18cf8bb67
parent 4
86a98659662f
child 33
44d823824a46
permissions
-rw-r--r--

*** empty log message ***

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