Friday, October 22, 2021

#1 2021-07-18 11:31:56 am

Fredrik71
Member
Registered: 2019-10-23
Posts: 901

NSWindow, NSImage and NSImageView include Core Image

In this example I have include Core Image to demo a filter 'CIPhotoEffectMono' to be displayed
in NSImageView. The script could be run directly in Script Editor.

The code include: CIImage, CIFilter and NSCIImageRep methods.

Choose a image file that show the effect of the Core Image filter.

Applescript:

use framework "Foundation"
use framework "CoreImage"
use framework "AppKit"
use scripting additions

set theImagePath to POSIX path of (choose file of type {"public.image"} default location (path to pictures folder))
its imageViewWithPath:theImagePath windowSize:{500, 700}

on imageViewWithPath:theImagePath windowSize:{theWidth, theHeight}
   set arguments to {imagePath:theImagePath, width:theWidth, height:theHeight}
   
   if my NSThread's isMainThread() as boolean then
       set arguments to current application's NSDictionary's dictionaryWithDictionary:arguments
       my performImageView:arguments
   else
       my performSelectorOnMainThread:"performImageView:" withObject:arguments waitUntilDone:true
   end if
end imageViewWithPath:windowSize:

on performImageView:arguments
   set {width, height} to {arguments's valueForKey:"width", arguments's valueForKey:"height"}
   -- A representation of an image to be processed or produced by Core Image filters.
   set theCIImage to createCIImageWithPath(arguments's valueForKey:"imagePath")
   -- An image processor that produces an image by manipulating one or more input images or by generating new image data.
   set theCIFilter to createCIFilterWithName("CIPhotoEffectMono", {inputImage:theCIImage})
   -- Creates and returns a representation of an image initialized to the specified Core Image instance.
   set theImageView to createImageRepFromCIImage(theCIFilter)
   
   -- Create the image view.
   set theImageView to createImageView(50, 50, (width as integer) - 100, (height as integer) - 100, theImageView)
   -- Create the window.
   set theWindow to createWindowWithRect(0, 0, width, height)
   theWindow's contentView()'s addSubview:theImageView
   theWindow's setTitle:"Example with ImageView..."
   theWindow's |center|()
   theWindow's makeKeyAndOrderFront:me
end performImageView:

on createCIImageWithPath(theImagePath)
   set theURL to current application's |NSURL|'s fileURLWithPath:theImagePath
   set theCIImage to current application's CIImage's imageWithContentsOfURL:theURL
   return theCIImage
end createCIImageWithPath

on createCIFilterWithName(filterName, inputParameters)
   set theCIFilter to current application's CIFilter's filterWithName:filterName withInputParameters:inputParameters
   set theCIImage to (theCIFilter's valueForKey:(current application's kCIOutputImageKey))
   return theCIImage
end createCIFilterWithName

on createImageRepFromCIImage(theCIImage)
   set theImageRep to current application's NSCIImageRep's imageRepWithCIImage:theCIImage
   set theImage to current application's NSImage's alloc()'s initWithSize:(theImageRep's |size|())
   theImage's addRepresentation:theImageRep
   return theImage
end createImageRepFromCIImage

on createImageView(x, y, width, height, imageView)
   set imageViewSize to current application's NSMakeRect(x, y, width, height)
   set theImageView to current application's NSImageView's alloc()'s initWithFrame:imageViewSize
   theImageView's setImage:imageView
   return theImageView
end createImageView

on createWindowWithRect(x, y, width, height)
   set windowSize to current application's NSMakeRect(x, y, width, height)
   set winStyle to (current application's NSWindowStyleMaskTitled as integer) + (current application's NSWindowStyleMaskClosable as integer)
   set theWindow to current application's NSWindow's alloc()'s initWithContentRect:windowSize styleMask:winStyle backing:2 defer:true
   return theWindow
end createWindowWithRect


if you are the expert, who will you call if its not your imagination.

Offline

 

#2 2021-07-19 07:53:59 am

Fredrik71
Member
Registered: 2019-10-23
Posts: 901

Re: NSWindow, NSImage and NSImageView include Core Image

In this example we add 2 things NSBitmapImageRep and PDFPage together with PDFDocument.
We  could do at least 2 things. We could save a bitmap image and pdf data to file.
To be able to save a bitmap image we need to convert CIImage method to NSBitmapImageRep.
And to save a PDF file we use PDFPage to import imageRep (NSImage) and create a PDFDocument.

In this example I have use source file that is black and white. Apply the filter 'CIMaskToAlpha'
to make a tiff and pdf with alpha. Don't be surprised Apple's Preview do not support PDF with alpha.
Other suggestion for Apple to improve maybe smile, but other application do.

Applescript:

use framework "Foundation"
use framework "CoreImage"
use framework "Quartz"
use framework "AppKit"
use scripting additions

set theImagePath to POSIX path of (choose file of type {"pdf"} default location (path to pictures folder))
its imageViewWithPath:theImagePath windowSize:{800, 800}

on imageViewWithPath:theImagePath windowSize:{theWidth, theHeight}
   set arguments to {imagePath:theImagePath, width:theWidth, height:theHeight}
   
   if my NSThread's isMainThread() as boolean then
       set arguments to current application's NSDictionary's dictionaryWithDictionary:arguments
       my performImageView:arguments
   else
       my performSelectorOnMainThread:"performImageView:" withObject:arguments waitUntilDone:true
   end if
end imageViewWithPath:windowSize:

on performImageView:arguments
   set {width, height} to {arguments's valueForKey:"width", arguments's valueForKey:"height"}
   -- A representation of an image to be processed or produced by Core Image filters.
   set theCIImage to createCIImageWithPath(arguments's valueForKey:"imagePath")
   -- An image processor that produces an image by manipulating one or more input images or by generating new image data.
   set theCIFilter to createCIFilterWithName("CIMaskToAlpha", {inputImage:theCIImage})
   -- Creates and returns a representation of an image initialized to the specified Core Image instance.
   set theImageRep to createImageRepFromCIImage(theCIFilter)
   
   -- Save a file to desktop as bitmap data
   its createBitmapImageRepFromCIImage(theCIFilter, POSIX path of (path to desktop) & "A_withAlpha.tiff")
   
   -- Save a file to desktop as PDF
   its createPDFFromImageRep(theImageRep, POSIX path of (path to desktop) & "A_withAlpha.pdf")
   
   -- Create the image view.
   set theImageView to createImageView(50, 50, (width as integer) - 100, (height as integer) - 100, theImageRep)
   
   -- Create the window.
   set theWindow to createWindowWithRect(0, 0, width, height)
   theWindow's contentView()'s addSubview:theImageView
   theWindow's setTitle:"Example with ImageView..."
   theWindow's |center|()
   theWindow's makeKeyAndOrderFront:me
end performImageView:

on createPDFFromImageRep(imageRep, saveFile)
   set thePage to current application's PDFPage's alloc()'s initWithImage:imageRep
   set pageData to thePage's dataRepresentation()
   set pdfDoc to current application's PDFDocument's alloc()'s initWithData:pageData
   return pdfDoc's writeToFile:saveFile
end createPDFFromImageRep

on createBitmapImageRepFromCIImage(theCIImage, saveFile)
   set theBitmapImageRep to current application's NSBitmapImageRep's alloc()'s initWithCIImage:theCIImage
   set imageData to theBitmapImageRep's TIFFRepresentation()
   return imageData's writeToFile:saveFile atomically:true
end createBitmapImageRepFromCIImage

on createCIImageWithPath(theImagePath)
   set theURL to current application's |NSURL|'s fileURLWithPath:theImagePath
   set theCIImage to current application's CIImage's imageWithContentsOfURL:theURL
   return theCIImage
end createCIImageWithPath

on createCIFilterWithName(filterName, inputParameters)
   set theCIFilter to current application's CIFilter's filterWithName:filterName withInputParameters:inputParameters
   set theCIImage to (theCIFilter's valueForKey:(current application's kCIOutputImageKey))
   return theCIImage
end createCIFilterWithName

on createImageRepFromCIImage(theCIImage)
   set theImageRep to current application's NSCIImageRep's imageRepWithCIImage:theCIImage
   set theImage to current application's NSImage's alloc()'s initWithSize:(theImageRep's |size|())
   theImage's addRepresentation:theImageRep
   return theImage
end createImageRepFromCIImage

on createImageView(x, y, width, height, imageView)
   set imageViewSize to current application's NSMakeRect(x, y, width, height)
   set theImageView to current application's NSImageView's alloc()'s initWithFrame:imageViewSize
   theImageView's setImage:imageView
   return theImageView
end createImageView

on createWindowWithRect(x, y, width, height)
   set windowSize to current application's NSMakeRect(x, y, width, height)
   set winStyle to (current application's NSWindowStyleMaskTitled as integer) + (current application's NSWindowStyleMaskClosable as integer)
   set theWindow to current application's NSWindow's alloc()'s initWithContentRect:windowSize styleMask:winStyle backing:2 defer:true
   return theWindow
end createWindowWithRect


if you are the expert, who will you call if its not your imagination.

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)