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

Skip to content

Commit e6628f8

Browse files
committed
Merge pull request matplotlib#1230 from dmcdougall/mac_dpi
Fix dpi issue for bitmaps on the OS X backend
2 parents dd05f0a + ca36299 commit e6628f8

File tree

2 files changed

+37
-28
lines changed

2 files changed

+37
-28
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: 36 additions & 27 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,33 +3543,42 @@ 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];
3550-
3551-
if (! [extension isEqualToString: @"tiff"] &&
3552-
! [extension isEqualToString: @"tif"])
3553-
{
3554-
NSBitmapImageFileType filetype;
3555-
NSBitmapImageRep* bitmapRep = [NSBitmapImageRep imageRepWithData: data];
3556-
if ([extension isEqualToString: @"bmp"])
3557-
filetype = NSBMPFileType;
3558-
else if ([extension isEqualToString: @"gif"])
3559-
filetype = NSGIFFileType;
3560-
else if ([extension isEqualToString: @"jpg"] ||
3561-
[extension isEqualToString: @"jpeg"])
3562-
filetype = NSJPEGFileType;
3563-
else if ([extension isEqualToString: @"png"])
3564-
filetype = NSPNGFileType;
3565-
else
3566-
{ PyErr_SetString(PyExc_ValueError, "Unknown file type");
3567-
return NULL;
3568-
}
3546+
NSImage *resizedImage = [[NSImage alloc] initWithSize:size];
35693547

3570-
data = [bitmapRep representationUsingType:filetype properties:nil];
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+
[image release];
3553+
[resizedImage release];
3554+
3555+
NSBitmapImageRep* rep = [NSBitmapImageRep imageRepWithData:data];
3556+
3557+
NSSize pxlSize = NSMakeSize(rep.pixelsWide, rep.pixelsHigh);
3558+
NSSize newSize = NSMakeSize(72.0 * pxlSize.width / dpi, 72.0 * pxlSize.height / dpi);
3559+
3560+
[rep setSize:newSize];
3561+
3562+
NSBitmapImageFileType filetype;
3563+
if ([extension isEqualToString: @"bmp"])
3564+
filetype = NSBMPFileType;
3565+
else if ([extension isEqualToString: @"gif"])
3566+
filetype = NSGIFFileType;
3567+
else if ([extension isEqualToString: @"jpg"] ||
3568+
[extension isEqualToString: @"jpeg"])
3569+
filetype = NSJPEGFileType;
3570+
else if ([extension isEqualToString: @"png"])
3571+
filetype = NSPNGFileType;
3572+
else if ([extension isEqualToString: @"tiff"] ||
3573+
[extension isEqualToString: @"tif"])
3574+
filetype = NSTIFFFileType;
3575+
else
3576+
{ PyErr_SetString(PyExc_ValueError, "Unknown file type");
3577+
return NULL;
35713578
}
35723579

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

0 commit comments

Comments
 (0)