Grayscale image: Difference between revisions

no edit summary
(Added solution for Action!)
No edit summary
Line 952:
Igual que la entrada de BASIC256
</pre>
 
=={{header|FutureBasic}}==
There are several ways to handle grayscaling images in FB. Here's a function that accepts any of a variety of color images — JPEG, TIFF, PNG, BMP, GIF, etc. — and converts them to grayscale. The function uses a convenient build-in Core Image filter to generate the optimized grayscale image. This code compiles as a standalone application featuring a window with two image views, one showing the original color image, and the other with the converted grayscale image. The app uses a relatively square color image of flowers. It proportionately resizes the image to fit the left hand image view, and displays the converted image in the right hand view.
<lang>
include resources "flowers.jpg"
 
_window = 1
begin enum output 1
_imageviewColor
_imageviewGray
end enum
 
void local fn BuildWindow
 
CGRect r = fn CGRectMake( 0, 0, 580, 300 )
window _window, @"Color to Grayscale", r
 
r = fn CGRectMake( 20, 20, 260, 260 )
imageview _imageviewColor, YES, @"flowers.jpg", r, NSImageScaleAxesIndependently, NSImageAlignCenter, NSImageFramePhoto
 
r = fn CGRectMake( 300, 20, 260, 260 )
imageview _imageviewGray, YES, @"flowers.jpg", r, NSImageScaleAxesIndependently, NSImageAlignCenter, NSImageFramePhoto
end fn
 
local fn GrayscaleImage( image as ImageRef ) as ImageRef
'~'1
CGSize size = fn ImageSize( image )
CGRect bounds = fn CGRectMake( 0, 0, size.width, size.height )
ImageRef finalImage = fn ImageWithSize( size )
CFDataRef dta = fn ImageTIFFRepresentationUsingCompression( image, NSTIFFCompressionNone, 0.0 )
CIImageRef inputImage = fn CIImageWithData( dta )
 
ImageLockFocus( finalImage )
CIFilterRef filter = fn CIFilterWithNameAndInputParameters( @"CIPhotoEffectMono", @{kCIInputImageKey:inputImage} )
CIImageRef outputCIImage = fn CIFilterOutputImage( filter )
CIImageDrawAtPoint( outputCIImage, CGPointZero, bounds, NSCompositeCopy, 1.0 )
ImageUnlockFocus( finalImage )
end fn = finalImage
 
fn BuildWindow
 
ImageRef colorFlowers
ImageRef grayflowers
 
colorFlowers = fn ImageNamed( @"flowers.jpg" )
grayflowers = fn GrayscaleImage( colorFlowers )
ImageViewSetImage( _imageviewGray, grayFlowers )
 
HandleEvents
</lang>
 
 
 
 
 
719

edits