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