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.
 
 
 
 
 
 

105 lines
3.4 KiB

  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. static int pipi_free_coreimage(pipi_image_t *img);
  22. pipi_image_t *pipi_load_coreimage(const char *name)
  23. {
  24. NSAutoreleasePool *autoreleasepool = [[NSAutoreleasePool alloc] init];
  25. NSString *n = [NSString stringWithCString: name];
  26. CIImage *source;
  27. NSURL *url = [NSURL fileURLWithPath:n];
  28. source = [CIImage imageWithContentsOfURL:url];
  29. CGRect extent = [source extent];
  30. size_t w = (size_t)extent.size.width;
  31. size_t h = (size_t)extent.size.height;
  32. NSBitmapImageRep * myImage;
  33. myImage = [[NSBitmapImageRep alloc] initWithCIImage:source];
  34. pipi_image_t *img;
  35. img = pipi_new(w, h);
  36. img->p[PIPI_PIXELS_RGBA_C].pixels = [myImage bitmapData];
  37. img->p[PIPI_PIXELS_RGBA_C].w = w;
  38. img->p[PIPI_PIXELS_RGBA_C].h = h;
  39. img->p[PIPI_PIXELS_RGBA_C].pitch = ([myImage bytesPerRow]/8) * img->w;
  40. img->p[PIPI_PIXELS_RGBA_C].bpp = [myImage bitsPerPixel];
  41. img->p[PIPI_PIXELS_RGBA_C].bytes = ([myImage bitsPerPixel]/8) * img->w * img->h;
  42. img->last_modified = PIPI_PIXELS_RGBA_C;
  43. img->codec_priv = (struct pipi_codec_coreimage *) malloc(sizeof(struct pipi_codec_coreimage *));
  44. struct pipi_codec_coreimage *infos = (struct pipi_codec_coreimage *) img->codec_priv;
  45. infos->format = [myImage bitmapFormat];
  46. pipi_pixels_t *p = pipi_getpixels(img, PIPI_PIXELS_RGBA_C);
  47. img->codec_free = pipi_free_coreimage;
  48. [autoreleasepool release];
  49. return img;
  50. }
  51. int pipi_save_coreimage(pipi_image_t *img, const char *name)
  52. {
  53. NSAutoreleasePool *autoreleasepool = [[NSAutoreleasePool alloc] init];
  54. printf("%d\n", img->last_modified);
  55. pipi_pixels_t *p = pipi_getpixels(img, PIPI_PIXELS_RGBA_C);
  56. NSString *n = [NSString stringWithCString: name];
  57. NSBitmapImageRep *bitmap = [[NSBitmapImageRep alloc]
  58. initWithBitmapDataPlanes:NULL
  59. pixelsWide:p->w
  60. pixelsHigh:p->h
  61. bitsPerSample:8
  62. samplesPerPixel:4
  63. hasAlpha:YES
  64. isPlanar:NO
  65. colorSpaceName:NSCalibratedRGBColorSpace
  66. bitmapFormat: 0//(NSBitmapFormat)img->codec_priv
  67. bytesPerRow:p->w*4
  68. bitsPerPixel:32
  69. ];
  70. if(bitmap == nil) return -1;
  71. memcpy([bitmap bitmapData], p->pixels, p->w*p->h*4);
  72. [[bitmap representationUsingType:NSPNGFileType properties:nil] writeToFile:n atomically:YES];
  73. [autoreleasepool release];
  74. return 1;
  75. }
  76. /*
  77. * XXX: The following functions are local.
  78. */
  79. static int pipi_free_coreimage(pipi_image_t *img)
  80. {
  81. return 0;
  82. }
  83. #endif