NYXImagesHelper.m 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. //
  2. // NYXImagesHelper.m
  3. // NYXImagesKit
  4. //
  5. // Created by Matthias Tretter on 02/06/11.
  6. // Originally Created by @Nyx0uf on 02/05/11.
  7. // Copyright 2012 Nyx0uf. All rights reserved.
  8. // www.cocoaintheshell.com
  9. //
  10. #import "NYXImagesHelper.h"
  11. static CIContext* __ciContext = nil;
  12. static CGColorSpaceRef __rgbColorSpace = NULL;
  13. CGContextRef NYXCreateARGBBitmapContext(const size_t width, const size_t height, const size_t bytesPerRow)
  14. {
  15. /// Use the generic RGB color space
  16. /// We avoid the NULL check because CGColorSpaceRelease() NULL check the value anyway, and worst case scenario = fail to create context
  17. /// Create the bitmap context, we want pre-multiplied ARGB, 8-bits per component
  18. CGContextRef bmContext = CGBitmapContextCreate(NULL, width, height, 8/*Bits per component*/, bytesPerRow, NYXGetRGBColorSpace(), kCGBitmapByteOrderDefault | kCGImageAlphaPremultipliedFirst);
  19. return bmContext;
  20. }
  21. // The following function was taken from the increadibly awesome HockeyKit
  22. // Created by Peter Steinberger on 10.01.11.
  23. // Copyright 2012 Peter Steinberger. All rights reserved.
  24. CGImageRef NYXCreateGradientImage(const size_t pixelsWide, const size_t pixelsHigh, const CGFloat fromAlpha, const CGFloat toAlpha)
  25. {
  26. // gradient is always black-white and the mask must be in the gray colorspace
  27. CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
  28. // create the bitmap context
  29. CGContextRef gradientBitmapContext = CGBitmapContextCreate(NULL, pixelsWide, pixelsHigh, 8, 0, colorSpace, kCGImageAlphaNone);
  30. // define the start and end grayscale values (with the alpha, even though
  31. // our bitmap context doesn't support alpha the gradient requires it)
  32. CGFloat colors[] = {toAlpha, 1.0f, fromAlpha, 1.0f};
  33. // create the CGGradient and then release the gray color space
  34. CGGradientRef grayScaleGradient = CGGradientCreateWithColorComponents(colorSpace, colors, NULL, 2);
  35. CGColorSpaceRelease(colorSpace);
  36. // create the start and end points for the gradient vector (straight down)
  37. CGPoint gradientEndPoint = CGPointZero;
  38. CGPoint gradientStartPoint = (CGPoint){.x = 0.0f, .y = pixelsHigh};
  39. // draw the gradient into the gray bitmap context
  40. CGContextDrawLinearGradient(gradientBitmapContext, grayScaleGradient, gradientStartPoint, gradientEndPoint, kCGGradientDrawsAfterEndLocation);
  41. CGGradientRelease(grayScaleGradient);
  42. // convert the context into a CGImageRef and release the context
  43. CGImageRef theCGImage = CGBitmapContextCreateImage(gradientBitmapContext);
  44. CGContextRelease(gradientBitmapContext);
  45. // return the imageref containing the gradient
  46. return theCGImage;
  47. }
  48. CIContext* NYXGetCIContext(void)
  49. {
  50. if (!__ciContext)
  51. {
  52. NSNumber* num = [[NSNumber alloc] initWithBool:NO];
  53. NSDictionary* opts = [[NSDictionary alloc] initWithObjectsAndKeys:num, kCIContextUseSoftwareRenderer, nil];
  54. __ciContext = [CIContext contextWithOptions:opts];
  55. }
  56. return __ciContext;
  57. }
  58. CGColorSpaceRef NYXGetRGBColorSpace(void)
  59. {
  60. if (!__rgbColorSpace)
  61. {
  62. __rgbColorSpace = CGColorSpaceCreateDeviceRGB();
  63. }
  64. return __rgbColorSpace;
  65. }
  66. void NYXImagesKitRelease(void)
  67. {
  68. if (__rgbColorSpace)
  69. CGColorSpaceRelease(__rgbColorSpace), __rgbColorSpace = NULL;
  70. if (__ciContext)
  71. __ciContext = nil;
  72. }