Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes a bug in drawing bitmap images in the macosx backend for handling device scaling #2939

Merged
merged 1 commit into from Apr 3, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 18 additions & 16 deletions src/_macosx.m
Expand Up @@ -2240,6 +2240,14 @@ static CGRect _find_enclosing_rect(CGPoint points[3])
return 0;
}


static CGFloat _get_device_scale(CGContextRef cr)
{
CGSize pixelSize = CGContextConvertSizeToDeviceSpace(cr, CGSizeMake(1,1));
return pixelSize.width;
}


static PyObject*
GraphicsContext_draw_gouraud_triangle (GraphicsContext* self, PyObject* args)

Expand Down Expand Up @@ -2998,17 +3006,8 @@ static void _data_provider_release(void* info, const void* data, size_t size)
Py_DECREF(image);
}

/* Consider the drawing origin to be in user coordinates
* but the image size to be in device coordinates */
static void draw_image_user_coords_device_size(CGContextRef cr, CGImageRef im,
float x, float y, npy_intp ncols, npy_intp nrows)
{
CGRect dst;
dst.origin = CGPointMake(x,y);
dst.size = CGContextConvertSizeToUserSpace(cr, CGSizeMake(ncols,nrows));
dst.size.height = fabs(dst.size.height); /* believe it or not... */
CGContextDrawImage(cr, dst, im);
}



static PyObject*
GraphicsContext_draw_mathtext(GraphicsContext* self, PyObject* args)
Expand Down Expand Up @@ -3090,16 +3089,18 @@ static void draw_image_user_coords_device_size(CGContextRef cr, CGImageRef im,
return NULL;
}

CGFloat deviceScale = _get_device_scale(cr);

if (angle==0.0)
{
draw_image_user_coords_device_size(cr, bitmap, x, y, ncols, nrows);
CGContextDrawImage(cr, CGRectMake(x, y, ncols/deviceScale, nrows/deviceScale), bitmap);
}
else
{
CGContextSaveGState(cr);
CGContextTranslateCTM(cr, x, y);
CGContextRotateCTM(cr, angle*M_PI/180);
draw_image_user_coords_device_size(cr, bitmap, 0, 0, ncols, nrows);
CGContextDrawImage(cr, CGRectMake(0, 0, ncols/deviceScale, nrows/deviceScale), bitmap);
CGContextRestoreGState(cr);
}
CGImageRelease(bitmap);
Expand Down Expand Up @@ -3189,7 +3190,9 @@ static void draw_image_user_coords_device_size(CGContextRef cr, CGImageRef im,
return NULL;
}

draw_image_user_coords_device_size(cr, bitmap, x, y, ncols, nrows);
CGFloat deviceScale = _get_device_scale(cr);

CGContextDrawImage(cr, CGRectMake(x, y, ncols/deviceScale, nrows/deviceScale), bitmap);
CGImageRelease(bitmap);

Py_INCREF(Py_None);
Expand All @@ -3206,8 +3209,7 @@ static void draw_image_user_coords_device_size(CGContextRef cr, CGImageRef im,
return NULL;
}

CGSize pixelSize = CGContextConvertSizeToDeviceSpace(cr, CGSizeMake(1,1));
return PyFloat_FromDouble(pixelSize.width);
return PyFloat_FromDouble(_get_device_scale(cr));
}


Expand Down