@@ -3509,16 +3509,16 @@ static void _data_provider_release(void* info, const void* data, size_t size)
3509
3509
int n;
3510
3510
const unichar * characters;
3511
3511
NSSize size;
3512
- double width, height;
3512
+ double width, height, dpi ;
3513
3513
3514
3514
if (!view)
3515
3515
{
3516
3516
PyErr_SetString (PyExc_RuntimeError, " NSView* is NULL" );
3517
3517
return NULL ;
3518
3518
}
3519
3519
/* 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 ;
3522
3522
size.width = width;
3523
3523
size.height = height;
3524
3524
@@ -3543,33 +3543,42 @@ static void _data_provider_release(void* info, const void* data, size_t size)
3543
3543
if (invalid) [view setNeedsDisplay: YES ];
3544
3544
3545
3545
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];
3569
3547
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 ;
3571
3578
}
3572
3579
3580
+ data = [rep representationUsingType: filetype properties: nil ];
3581
+
3573
3582
[data writeToFile: filename atomically: YES ];
3574
3583
[pool release ];
3575
3584
0 commit comments