Created
February 28, 2013 03:18
-
-
Save abhibeckert/5053904 to your computer and use it in GitHub Desktop.
takes an image and crops it to f it your displays on OS X
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// main.m | |
// span-image-across-displays | |
// | |
// Created by Abhi Beckert on 2013-2-19. | |
// Copyright (c) 2013 Abhi Beckert. All rights reserved. | |
// | |
#import <Cocoa/Cocoa.h> | |
int main(int argc, const char * argv[]) | |
{ | |
@autoreleasepool { | |
if (argc != 2) { | |
printf("Usage: span-image-across-displays example.jpg\n"); | |
return 0; | |
} | |
// find image | |
NSURL *imgURL = [NSURL fileURLWithPath:[NSString stringWithCString:argv[1] encoding:NSUTF8StringEncoding]]; | |
// load image | |
NSImage *sourceImage = [[NSImage alloc] initWithContentsOfURL:imgURL]; | |
NSSize sourceImageSize= sourceImage.size; | |
NSLog(@"image: %@", NSStringFromSize(sourceImageSize)); | |
// load screen area | |
NSRect screenArea = NSMakeRect(0, 0, 0, 0); | |
for (NSScreen *screen in [NSScreen screens]) { | |
NSLog(@"screen %@: %@", screen, NSStringFromRect([screen frame])); | |
screenArea.origin.x = MIN(NSMinX(screen.frame), NSMinX(screenArea)); | |
screenArea.origin.y = MIN(NSMinY(screen.frame), NSMinY(screenArea)); | |
}; | |
for (NSScreen *screen in [NSScreen screens]) { | |
screenArea.size.width = MAX(NSMaxX(screen.frame), NSMaxX(screenArea)) - screenArea.origin.x; | |
screenArea.size.height = MAX(NSMaxY(screen.frame), NSMaxY(screenArea)) - screenArea.origin.y; | |
}; | |
NSLog(@"screen area: %@", NSStringFromRect(screenArea)); | |
// figure out resize ratio | |
float imageScale = MAX(screenArea.size.width / sourceImageSize.width, screenArea.size.height / sourceImageSize.height); | |
NSLog(@"scale image by: %f", imageScale); | |
NSPoint imageOffset = NSMakePoint(0 - roundf((roundf(sourceImageSize.width * imageScale) - screenArea.size.width) / 2), 0 - roundf((roundf(sourceImageSize.height * imageScale) - screenArea.size.height) / 2)); | |
NSLog(@"offset image: %@", NSStringFromPoint(imageOffset)); | |
// create image for each screen | |
int counter = 0; | |
for (NSScreen *screen in [NSScreen screens]) { | |
counter++; | |
NSURL *newImageURL = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@ %i.jpg", imgURL.path.stringByDeletingPathExtension, counter]]; | |
NSImage *newImage = [[NSImage alloc] initWithSize:screen.frame.size]; | |
[newImage lockFocus]; | |
[sourceImage drawInRect:NSMakeRect(0, 0, screen.frame.size.width, screen.frame.size.height) fromRect:NSMakeRect((0 - (imageOffset.x / imageScale)) + ((screen.frame.origin.x - screenArea.origin.x) / imageScale), (0 - (imageOffset.y / imageScale)) + ((screen.frame.origin.y - screenArea.origin.y) / imageScale), screen.frame.size.width / imageScale, screen.frame.size.height / imageScale) operation:NSCompositeCopy fraction:1.0]; | |
[newImage unlockFocus]; | |
[[[NSBitmapImageRep imageRepWithData:[newImage TIFFRepresentation]] representationUsingType:NSJPEGFileType properties:@{NSImageCompressionFactor: [NSNumber numberWithFloat:0.9]}] writeToURL:newImageURL atomically:YES]; | |
NSLog(@"wrote %@ to %@", newImage, newImageURL); | |
[[NSWorkspace sharedWorkspace] setDesktopImageURL:newImageURL forScreen:screen options:nil error:NULL]; | |
} | |
} | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment