You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

coreimage.m 3.0 KiB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * libpipi Pathetic image processing interface library
  3. * Copyright (c) 2004-2008 Sam Hocevar <sam@zoy.org>
  4. * 2008 Jean-Yves Lamoureux <jylam@lnxscene.org>
  5. * All Rights Reserved
  6. *
  7. * $Id$
  8. *
  9. * This library is free software. It comes without any warranty, to
  10. * the extent permitted by applicable law. You can redistribute it
  11. * and/or modify it under the terms of the Do What The Fuck You Want
  12. * To Public License, Version 2, as published by Sam Hocevar. See
  13. * http://sam.zoy.org/wtfpl/COPYING for more details.
  14. */
  15. /*
  16. * coreimage.m: CoreImage (OSX) I/O functions
  17. */
  18. #import "coreimage.h"
  19. #ifdef USE_COCOA
  20. #import <CIImage.h>
  21. pipi_image_t *pipi_load_coreimage(const char *name)
  22. {
  23. NSAutoreleasePool *autoreleasepool = [[NSAutoreleasePool alloc] init];
  24. NSString *n = [NSString stringWithCString: name];
  25. CIImage *source;
  26. NSURL *url = [NSURL fileURLWithPath:n];
  27. source = [CIImage imageWithContentsOfURL:url];
  28. CGRect extent = [source extent];
  29. size_t w = (size_t)extent.size.width;
  30. size_t h = (size_t)extent.size.height;
  31. NSBitmapImageRep * myImage;
  32. myImage = [[NSBitmapImageRep alloc] initWithCIImage:source];
  33. pipi_image_t *img;
  34. img = pipi_new(w, h);
  35. img->p[PIPI_PIXELS_RGBA_C].pixels = [myImage bitmapData];
  36. img->p[PIPI_PIXELS_RGBA_C].w = w;
  37. img->p[PIPI_PIXELS_RGBA_C].h = h;
  38. img->p[PIPI_PIXELS_RGBA_C].pitch = ([myImage bytesPerRow]/8) * img->w;
  39. img->p[PIPI_PIXELS_RGBA_C].bpp = [myImage bitsPerPixel];
  40. img->p[PIPI_PIXELS_RGBA_C].bytes = ([myImage bitsPerPixel]/8) * img->w * img->h;
  41. img->last_modified = PIPI_PIXELS_RGBA_C;
  42. img->codec_priv = (void*)[myImage bitmapFormat];
  43. pipi_pixels_t *p = pipi_getpixels(img, PIPI_PIXELS_RGBA_C);
  44. [autoreleasepool release];
  45. return img;
  46. }
  47. int pipi_save_coreimage(pipi_image_t *img, const char *name)
  48. {
  49. NSAutoreleasePool *autoreleasepool = [[NSAutoreleasePool alloc] init];
  50. printf("%d\n", img->last_modified);
  51. pipi_pixels_t *p = pipi_getpixels(img, PIPI_PIXELS_RGBA_C);
  52. NSString *n = [NSString stringWithCString: name];
  53. NSBitmapImageRep *bitmap = [[NSBitmapImageRep alloc]
  54. initWithBitmapDataPlanes:NULL
  55. pixelsWide:p->w
  56. pixelsHigh:p->h
  57. bitsPerSample:8
  58. samplesPerPixel:4
  59. hasAlpha:YES
  60. isPlanar:NO
  61. colorSpaceName:NSCalibratedRGBColorSpace
  62. bitmapFormat: 0//(NSBitmapFormat)img->codec_priv
  63. bytesPerRow:p->w*4
  64. bitsPerPixel:32
  65. ];
  66. if(bitmap == nil) return -1;
  67. memcpy([bitmap bitmapData], p->pixels, p->w*p->h*4);
  68. [[bitmap representationUsingType:NSPNGFileType properties:nil] writeToFile:n atomically:YES];
  69. [autoreleasepool release];
  70. return 1;
  71. }
  72. #endif