mirror of
https://github.com/bitgapp/eqMac.git
synced 2024-11-22 13:07:26 +03:00
Implemented a function to invert images
This commit is contained in:
parent
24523cd07c
commit
7a6a13e3a8
21
LICENSE
Normal file
21
LICENSE
Normal file
@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2017 Roman Kisil
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
@ -29,5 +29,5 @@
|
||||
+(BOOL)appLaunchedBefore;
|
||||
+ (BOOL)launchOnLogin;
|
||||
+ (void)setLaunchOnLogin:(BOOL)launchOnLogin;
|
||||
|
||||
+(NSImage *)invertedImageFromImage:(NSImage*)image;
|
||||
@end
|
||||
|
@ -242,6 +242,75 @@
|
||||
timeStyle:NSDateFormatterFullStyle];
|
||||
}
|
||||
|
||||
+(NSImage *)invertedImageFromImage:(NSImage*)image{
|
||||
// get width and height as integers, since we'll be using them as
|
||||
// array subscripts, etc, and this'll save a whole lot of casting
|
||||
CGSize size = image.size;
|
||||
int width = size.width;
|
||||
int height = size.height;
|
||||
|
||||
// Create a suitable RGB+alpha bitmap context in BGRA colour space
|
||||
CGColorSpaceRef colourSpace = CGColorSpaceCreateDeviceRGB();
|
||||
unsigned char *memoryPool = (unsigned char *)calloc(width*height*4, 1);
|
||||
CGContextRef context = CGBitmapContextCreate(memoryPool, width, height, 8, width * 4, colourSpace, kCGBitmapByteOrder32Big | kCGImageAlphaPremultipliedLast);
|
||||
CGColorSpaceRelease(colourSpace);
|
||||
|
||||
// draw the current image to the newly created context
|
||||
NSRect imageRect = NSMakeRect(0, 0, image.size.width, image.size.height);
|
||||
CGContextDrawImage(context, CGRectMake(0, 0, width, height), [image CGImageForProposedRect:&imageRect context:NULL hints:nil]);
|
||||
|
||||
// run through every pixel, a scan line at a time...
|
||||
for(int y = 0; y < height; y++)
|
||||
{
|
||||
// get a pointer to the start of this scan line
|
||||
unsigned char *linePointer = &memoryPool[y * width * 4];
|
||||
|
||||
// step through the pixels one by one...
|
||||
for(int x = 0; x < width; x++)
|
||||
{
|
||||
// get RGB values. We're dealing with premultiplied alpha
|
||||
// here, so we need to divide by the alpha channel (if it
|
||||
// isn't zero, of course) to get uninflected RGB. We
|
||||
// multiply by 255 to keep precision while still using
|
||||
// integers
|
||||
int r, g, b;
|
||||
if(linePointer[3])
|
||||
{
|
||||
r = linePointer[0] * 255 / linePointer[3];
|
||||
g = linePointer[1] * 255 / linePointer[3];
|
||||
b = linePointer[2] * 255 / linePointer[3];
|
||||
}
|
||||
else
|
||||
r = g = b = 0;
|
||||
|
||||
// perform the colour inversion
|
||||
r = 255 - r;
|
||||
g = 255 - g;
|
||||
b = 255 - b;
|
||||
|
||||
// multiply by alpha again, divide by 255 to undo the
|
||||
// scaling before, store the new values and advance
|
||||
// the pointer we're reading pixel data from
|
||||
linePointer[0] = r * linePointer[3] / 255;
|
||||
linePointer[1] = g * linePointer[3] / 255;
|
||||
linePointer[2] = b * linePointer[3] / 255;
|
||||
linePointer += 4;
|
||||
}
|
||||
}
|
||||
|
||||
// get a CG image from the context, wrap that into a
|
||||
// NSImage
|
||||
CGImageRef cgImage = CGBitmapContextCreateImage(context);
|
||||
NSImage *returnImage = [[NSImage alloc] initWithCGImage:cgImage size:size];
|
||||
|
||||
// clean up
|
||||
CGImageRelease(cgImage);
|
||||
CGContextRelease(context);
|
||||
free(memoryPool);
|
||||
|
||||
// and return
|
||||
return returnImage;
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
|
Binary file not shown.
@ -0,0 +1,23 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Bucket
|
||||
type = "0"
|
||||
version = "2.0">
|
||||
<Breakpoints>
|
||||
<BreakpointProxy
|
||||
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
|
||||
<BreakpointContent
|
||||
shouldBeEnabled = "Yes"
|
||||
ignoreCount = "0"
|
||||
continueAfterRunningActions = "No"
|
||||
filePath = "Source/Common/Utilities/Utilities.m"
|
||||
timestampString = "522534657.146549"
|
||||
startingColumnNumber = "9223372036854775807"
|
||||
endingColumnNumber = "9223372036854775807"
|
||||
startingLineNumber = "295"
|
||||
endingLineNumber = "295"
|
||||
landmarkName = "+invertedImageFromImage:"
|
||||
landmarkType = "7">
|
||||
</BreakpointContent>
|
||||
</BreakpointProxy>
|
||||
</Breakpoints>
|
||||
</Bucket>
|
Loading…
Reference in New Issue
Block a user