Permalink
Browse files

Exporting an image as an 8 bit png

  • Loading branch information...
1 parent 0da3cce commit fcbc0c90fa6d76113847dd99d37465d9b7c7e347 @ccgus committed Aug 9, 2016
Showing with 51 additions and 0 deletions.
  1. +51 −0 jstalk/Export Document As 8 BPC 1 Channel PNG.jstalk
View
51 jstalk/Export Document As 8 BPC 1 Channel PNG.jstalk
@@ -0,0 +1,51 @@
+/*
+How to install this plugin:
+1) Choose Acorn's Help ▸ Open Acorn's App Support Folder menu item.
+2) Place this script in the Plug-Ins folder (and make sure it ends with .jstalk)
+3) Restart Acorn. The plugin will now show up in the Filter menu.
+*/
+
+__mocha__.loadFrameworkWithName("ImageIO")
+__mocha__.loadFrameworkWithName("CoreFoundation")
+
+function main(image, doc, layer) {
+
+ var savePanel = [NSSavePanel savePanel];
+
+ [savePanel setExtensionHidden:false];
+ [savePanel setAllowedFileTypes:["png"]];
+ [savePanel setNameFieldStringValue:[[layer layerName] stringByAppendingPathExtension:"png"]];
+
+ if ([savePanel runModal]) {
+
+ var cgimg = [doc newCGImage];
+
+ var cs = CGColorSpaceCreateWithName(kCGColorSpaceGenericGray);
+
+ var context = CGBitmapContextCreate(nil, CGImageGetWidth(cgimg), CGImageGetHeight(cgimg), 8, 0, cs, kCGImageAlphaNone);
+
+ var r = NSMakeRect(0, 0, CGImageGetWidth(cgimg), CGImageGetHeight(cgimg));
+
+ CGContextDrawImage(context, r, cgimg);
+
+ CGImageRelease(cgimg);
+
+ // make a new one to write out.
+ cgimg = CGBitmapContextCreateImage(context);
+
+ var data = [NSMutableData data];
+ var imageDestination = CGImageDestinationCreateWithData(data, "public.png", 1, nil);
+
+ CGImageDestinationAddImage(imageDestination, cgimg, nil);
+ CGImageDestinationFinalize(imageDestination);
+
+ // We're leaking because we crash eventually otherwise. Looks like a bug in coscript
+ // CFRelease(imageDestination);
+
+ [data writeToURL:[savePanel URL] atomically:true];
+
+ // We're leaking because we crash eventually otherwise. Looks like a bug in coscript
+ // CGContextRelease(newContext);
+ }
+}
+

0 comments on commit fcbc0c9

Please sign in to comment.