+static boolean ValidClippedRectangle(Bitmap *bitmap, int *x, int *y,
+ int *width, int *height)
+{
+ /* skip if rectangle completely outside bitmap */
+
+ if (*x + *width <= 0 ||
+ *y + *height <= 0 ||
+ *x >= bitmap->width ||
+ *y >= bitmap->height)
+ return FALSE;
+
+ /* clip if rectangle overlaps bitmap */
+
+ if (*x < 0)
+ {
+ *x = 0;
+ *width += *x;
+ }
+ else if (*x + *width > bitmap->width)
+ *width = bitmap->width - *x;
+
+ if (*y < 0)
+ {
+ *y = 0;
+ *height += *y;
+ }
+ else if (*y + *height > bitmap->height)
+ *height = bitmap->height - *y;
+
+ return TRUE;
+}
+