Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9ffe6ca

Browse files
dmcdougallmdboom
authored andcommitted
Fix dpi issue for bitmaps on the OS X backend
Bitmaps were being generated from the NSView. NSImages, by default, always use a dpi of 72. To generate a bitmap of a different size, the image must be redrawn into a new rect. To resample the image at a different resolution, a conversion is made to use the new dpi.
1 parent 0ae5762 commit 9ffe6ca

File tree

2 files changed

+19
-10
lines changed

2 files changed

+19
-10
lines changed

lib/matplotlib/backends/backend_macosx.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ def _print_bitmap(self, filename, *args, **kwargs):
316316
width, height = self.figure.get_size_inches()
317317
width, height = width*dpi, height*dpi
318318
filename = unicode(filename)
319-
self.write_bitmap(filename, width, height)
319+
self.write_bitmap(filename, width, height, dpi)
320320
self.figure.dpi = old_dpi
321321

322322
def print_bmp(self, filename, *args, **kwargs):

src/_macosx.m

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3509,16 +3509,16 @@ static void _data_provider_release(void* info, const void* data, size_t size)
35093509
int n;
35103510
const unichar* characters;
35113511
NSSize size;
3512-
double width, height;
3512+
double width, height, dpi;
35133513

35143514
if(!view)
35153515
{
35163516
PyErr_SetString(PyExc_RuntimeError, "NSView* is NULL");
35173517
return NULL;
35183518
}
35193519
/* NSSize contains CGFloat; cannot use size directly */
3520-
if(!PyArg_ParseTuple(args, "u#dd",
3521-
&characters, &n, &width, &height)) return NULL;
3520+
if(!PyArg_ParseTuple(args, "u#ddd",
3521+
&characters, &n, &width, &height, &dpi)) return NULL;
35223522
size.width = width;
35233523
size.height = height;
35243524

@@ -3543,16 +3543,24 @@ static void _data_provider_release(void* info, const void* data, size_t size)
35433543
if (invalid) [view setNeedsDisplay: YES];
35443544

35453545
NSImage* image = [[NSImage alloc] initWithData: data];
3546-
[image setScalesWhenResized: YES];
3547-
[image setSize: size];
3548-
data = [image TIFFRepresentation];
3549-
[image release];
3546+
NSImage *resizedImage = [[NSImage alloc] initWithSize:size];
3547+
3548+
[resizedImage lockFocus];
3549+
[image drawInRect:NSMakeRect(0, 0, width, height) fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0];
3550+
[resizedImage unlockFocus];
3551+
data = [resizedImage TIFFRepresentation];
3552+
3553+
NSBitmapImageRep* rep = [NSBitmapImageRep imageRepWithData:data];
3554+
3555+
NSSize pxlSize = NSMakeSize(rep.pixelsWide, rep.pixelsHigh);
3556+
NSSize newSize = NSMakeSize(72.0 * pxlSize.width / dpi, 72.0 * pxlSize.height / dpi);
3557+
3558+
[rep setSize:newSize];
35503559

35513560
if (! [extension isEqualToString: @"tiff"] &&
35523561
! [extension isEqualToString: @"tif"])
35533562
{
35543563
NSBitmapImageFileType filetype;
3555-
NSBitmapImageRep* bitmapRep = [NSBitmapImageRep imageRepWithData: data];
35563564
if ([extension isEqualToString: @"bmp"])
35573565
filetype = NSBMPFileType;
35583566
else if ([extension isEqualToString: @"gif"])
@@ -3567,9 +3575,10 @@ static void _data_provider_release(void* info, const void* data, size_t size)
35673575
return NULL;
35683576
}
35693577

3570-
data = [bitmapRep representationUsingType:filetype properties:nil];
3578+
data = [rep representationUsingType:filetype properties:nil];
35713579
}
35723580

3581+
data = [rep representationUsingType:NSTIFFFileType properties:nil];
35733582
[data writeToFile: filename atomically: YES];
35743583
[pool release];
35753584

0 commit comments

Comments
 (0)