• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Resize NSImage without anti-aliasing
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Resize NSImage without anti-aliasing


  • Subject: Resize NSImage without anti-aliasing
  • From: Takaaki Naganoya <email@hidden>
  • Date: Tue, 21 Mar 2017 23:34:39 +0900

Hi,

We have a lot of NSImage resizing routines. But they make resized NSImage with anti-aliasing.
Is there a way to scale NSImage without anti-aliasing? 

<AppleScript 1>
on resizeNSImage:aSourceImg |scale|:aScale
set aSize to aSourceImg's |size|()
set newWidth to (width of aSize) * aScale
set newheight to (height of aSize) * aScale


set targFrame to current application's NSMakeRect(0, 0, newWidth, newheight)
set targImage to current application's NSImage's alloc()'s initWithSize:{newWidth, newheight}


set aKey to {current application's NSImageHintInterpolation}
set aObj to current application's NSNumber's numberWithInt:(current application's NSImageInterpolationLow)
set hintRec to current application's NSDictionary's dictionaryWithObject:aObj forKey:aKey


targImage's lockFocus()


aSourceImg's drawInRect:targFrame fromRect:(current application's NSZeroRect) operation:(current application's NSCompositeCopy) fraction:1.0 respectFlipped:true hints:hintRec


targImage's unlockFocus()


return targImage
end resizeNSImage:|scale|:
</AppleScript 1>

<AppleScript 2>
on resizedImage:aSourceImg toScale:imgScale
if (aSourceImg's isValid()) as boolean = false then error "Invalid NSImage"


set aSize to aSourceImg's |size|()
set aWidth to (aSize's width) * imgScale
set aHeight to (aSize's height) * imgScale


set aRep to current application's NSBitmapImageRep's alloc()'s initWithBitmapDataPlanes:(missing value) pixelsWide:aWidth pixelsHigh:aHeight bitsPerSample:8 samplesPerPixel:4 hasAlpha:true isPlanar:false colorSpaceName:(current application's NSCalibratedRGBColorSpace) bytesPerRow:0 bitsPerPixel:0


set newSize to {width:aWidth, height:aHeight}
aRep's setSize:newSize


current application's NSGraphicsContext's saveGraphicsState()
current application's NSGraphicsContext's setCurrentContext:(current application's NSGraphicsContext's graphicsContextWithBitmapImageRep:aRep)


aSourceImg's drawInRect:(current application's NSMakeRect(0, 0, aWidth, aHeight)) fromRect:(current application's NSZeroRect) operation:(current application's NSCompositeCopy) fraction:(1.0)


current application's NSGraphicsContext's restoreGraphicsState()


set newImg to current application's NSImage's alloc()'s initWithSize:newSize
newImg's addRepresentation:aRep


return newImg
end resizedImage:toScale:
</AppleScript 2>

--
Takaaki Naganoya
email@hidden
http://piyocast.com/as/



 _______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden

  • Follow-Ups:
    • Re: Resize NSImage without anti-aliasing
      • From: Shane Stanley <email@hidden>
  • Prev by Date: Re: GUI Scripting bug with text ranges
  • Next by Date: Re: My script won't be running stably
  • Previous by thread: Re: GUI Scripting bug with text ranges
  • Next by thread: Re: Resize NSImage without anti-aliasing
  • Index(es):
    • Date
    • Thread