FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Fix dpi issue for bitmaps on the OS X backend by dmcdougall · Pull Request #1230 · matplotlib/matplotlib · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .m  (1) .py  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
2 changes: 1 addition & 1 deletion lib/matplotlib/backends/backend_macosx.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ def _print_bitmap(self, filename, *args, **kwargs):
width, height = self.figure.get_size_inches()
width, height = width*dpi, height*dpi
filename = unicode(filename)
self.write_bitmap(filename, width, height)
self.write_bitmap(filename, width, height, dpi)
self.figure.dpi = old_dpi

def print_bmp(self, filename, *args, **kwargs):
Expand Down
63 changes: 36 additions & 27 deletions src/_macosx.m
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -3509,16 +3509,16 @@ static void _data_provider_release(void* info, const void* data, size_t size)
int n;
const unichar* characters;
NSSize size;
double width, height;
double width, height, dpi;

if(!view)
{
PyErr_SetString(PyExc_RuntimeError, "NSView* is NULL");
return NULL;
}
/* NSSize contains CGFloat; cannot use size directly */
if(!PyArg_ParseTuple(args, "u#dd",
&characters, &n, &width, &height)) return NULL;
if(!PyArg_ParseTuple(args, "u#ddd",
&characters, &n, &width, &height, &dpi)) return NULL;
size.width = width;
size.height = height;

Expand All @@ -3543,33 +3543,42 @@ static void _data_provider_release(void* info, const void* data, size_t size)
if (invalid) [view setNeedsDisplay: YES];

NSImage* image = [[NSImage alloc] initWithData: data];
[image setScalesWhenResized: YES];
[image setSize: size];
data = [image TIFFRepresentation];
[image release];

if (! [extension isEqualToString: @"tiff"] &&
! [extension isEqualToString: @"tif"])
{
NSBitmapImageFileType filetype;
NSBitmapImageRep* bitmapRep = [NSBitmapImageRep imageRepWithData: data];
if ([extension isEqualToString: @"bmp"])
filetype = NSBMPFileType;
else if ([extension isEqualToString: @"gif"])
filetype = NSGIFFileType;
else if ([extension isEqualToString: @"jpg"] ||
[extension isEqualToString: @"jpeg"])
filetype = NSJPEGFileType;
else if ([extension isEqualToString: @"png"])
filetype = NSPNGFileType;
else
{ PyErr_SetString(PyExc_ValueError, "Unknown file type");
return NULL;
}
NSImage *resizedImage = [[NSImage alloc] initWithSize:size];

data = [bitmapRep representationUsingType:filetype properties:nil];
[resizedImage lockFocus];
[image drawInRect:NSMakeRect(0, 0, width, height) fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0];
[resizedImage unlockFocus];
data = [resizedImage TIFFRepresentation];
[image release];
[resizedImage release];

NSBitmapImageRep* rep = [NSBitmapImageRep imageRepWithData:data];

NSSize pxlSize = NSMakeSize(rep.pixelsWide, rep.pixelsHigh);
NSSize newSize = NSMakeSize(72.0 * pxlSize.width / dpi, 72.0 * pxlSize.height / dpi);

[rep setSize:newSize];

NSBitmapImageFileType filetype;
if ([extension isEqualToString: @"bmp"])
filetype = NSBMPFileType;
else if ([extension isEqualToString: @"gif"])
filetype = NSGIFFileType;
else if ([extension isEqualToString: @"jpg"] ||
[extension isEqualToString: @"jpeg"])
filetype = NSJPEGFileType;
else if ([extension isEqualToString: @"png"])
filetype = NSPNGFileType;
else if ([extension isEqualToString: @"tiff"] ||
[extension isEqualToString: @"tif"])
filetype = NSTIFFFileType;
else
{ PyErr_SetString(PyExc_ValueError, "Unknown file type");
return NULL;
}

data = [rep representationUsingType:filetype properties:nil];

[data writeToFile: filename atomically: YES];
[pool release];

Expand Down

Back | FazBrowse Home | New Git URL