bitblt.c

Wed, 02 Jan 2002 10:17:24 +0000

author
eric
date
Wed, 02 Jan 2002 10:17:24 +0000
changeset 34
8d7bd2fa5db6
parent 33
44d823824a46
child 35
41804cc569ab
permissions
-rw-r--r--

improve floating point literal handling.

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@33 53 if ((coord.x < 0) || (coord.y < 0) ||
eric@33 54 (coord.x >= bitmap->width) || (coord.y >= bitmap->height))
eric@2 55 return (0);
eric@2 56 p = bitmap->bits + coord.y * bitmap->rowbytes + coord.x / 8;
eric@2 57 return ((*p & pixel_mask (coord.x & 7)) != 0);
eric@2 58 }
eric@2 59
eric@2 60 void set_pixel (Bitmap *bitmap, Point coord, boolean value)
eric@2 61 {
eric@2 62 u8 *p;
eric@33 63 if ((coord.x < 0) || (coord.y < 0) ||
eric@33 64 (coord.x >= bitmap->width) || (coord.y >= bitmap->height))
eric@2 65 return;
eric@2 66 p = bitmap->bits + coord.y * bitmap->rowbytes + coord.x / 8;
eric@2 67 if (value)
eric@2 68 *p |= pixel_mask (coord.x & 7);
eric@2 69 else
eric@2 70 *p &= (0xff ^ pixel_mask (coord.x & 7));
eric@2 71 }
eric@2 72
eric@2 73
eric@2 74 Bitmap *bitblt (Bitmap *src_bitmap,
eric@2 75 Rect src_rect,
eric@2 76 Bitmap *dest_bitmap,
eric@2 77 Point dest_upper_left,
eric@3 78 int scan,
eric@2 79 int tfn)
eric@2 80 {
eric@2 81 Point src_point, dest_point;
eric@2 82
eric@2 83 if (! dest_bitmap)
eric@2 84 {
eric@3 85 if (scan & TRANSPOSE)
eric@4 86 dest_bitmap = create_bitmap (dest_upper_left.x + rect_height (src_rect),
eric@4 87 dest_upper_left.y + rect_width (src_rect));
eric@2 88 else
eric@4 89 dest_bitmap = create_bitmap (dest_upper_left.x + rect_width (src_rect),
eric@4 90 dest_upper_left.y + rect_height (src_rect));
eric@2 91 if (! dest_bitmap)
eric@2 92 return (NULL);
eric@2 93 }
eric@2 94
eric@2 95 for (src_point.y = src_rect.upper_left.y;
eric@2 96 src_point.y < src_rect.lower_right.y;
eric@2 97 src_point.y++)
eric@2 98 {
eric@2 99 for (src_point.x = src_rect.upper_left.x;
eric@2 100 src_point.x < src_rect.lower_right.x;
eric@2 101 src_point.x++)
eric@2 102 {
eric@2 103 boolean a, b, c;
eric@2 104
eric@3 105 if (scan & TRANSPOSE)
eric@2 106 {
eric@3 107 dest_point.x = src_point.y - src_rect.upper_left.y;
eric@3 108 dest_point.y = src_point.x - src_rect.upper_left.x;
eric@3 109
eric@3 110 if (scan & FLIP_H)
eric@3 111 dest_point.x = (rect_height (src_rect) - 1) - dest_point.x;
eric@3 112
eric@3 113 if (scan & FLIP_V)
eric@3 114 dest_point.y = (rect_width (src_rect) - 1) - dest_point.y;
eric@2 115 }
eric@2 116 else
eric@2 117 {
eric@3 118 dest_point.x = src_point.x - src_rect.upper_left.x;
eric@3 119 dest_point.y = src_point.y - src_rect.upper_left.y;
eric@3 120
eric@3 121 if (scan & FLIP_H)
eric@3 122 dest_point.x = (rect_width (src_rect) - 1) - dest_point.x;
eric@3 123
eric@3 124 if (scan & FLIP_V)
eric@3 125 dest_point.y = (rect_height (src_rect) - 1) - dest_point.y;
eric@2 126 }
eric@2 127
eric@3 128 dest_point.x += dest_upper_left.x;
eric@3 129 dest_point.y += dest_upper_left.y;
eric@3 130
eric@2 131 a = get_pixel (src_bitmap, src_point);
eric@2 132 b = get_pixel (dest_bitmap, dest_point);
eric@2 133 c = (tfn & (1 << (a * 2 + b))) != 0;
eric@2 134
eric@2 135 set_pixel (dest_bitmap, dest_point, c);
eric@2 136 }
eric@2 137 }
eric@2 138 return (dest_bitmap);
eric@2 139 }