Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 
 

168 righe
5.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. if(source == NULL) return NULL;
  30. CGRect extent = [source extent];
  31. size_t w = (size_t)extent.size.width;
  32. size_t h = (size_t)extent.size.height;
  33. NSBitmapImageRep * myImage;
  34. myImage = [[NSBitmapImageRep alloc] initWithCIImage:source];
  35. pipi_image_t *img;
  36. img = pipi_new(w, h);
  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. /* CoreImage feeds us with BGRA while we need RGBA, so convert it.
  44. * We also need to get a pitch==(w*bpp) in order to pipi to opper properly.
  45. */
  46. int pitch = (img->p[PIPI_PIXELS_RGBA_C].bpp/8);
  47. unsigned char *tmp = (unsigned char*)malloc(h*w*pitch);
  48. unsigned char *orig = (unsigned char*)[myImage bitmapData];
  49. int x, y, k=0, o=0, a=[myImage bytesPerRow] - (w*([myImage bitsPerPixel]/8));
  50. for(y=0; y<h; y++)
  51. {
  52. for(x=0; x<w*pitch; x+=4)
  53. {
  54. if(!([myImage bitmapFormat] & NSAlphaFirstBitmapFormat))
  55. {
  56. tmp[k+2] = orig[o];
  57. tmp[k+1] = orig[o+1];
  58. tmp[k+0] = orig[o+2];
  59. tmp[k+3] = orig[o+3];
  60. } else
  61. {
  62. tmp[k+0] = orig[o];
  63. tmp[k+1] = orig[o+1];
  64. tmp[k+2] = orig[o+2];
  65. tmp[k+3] = orig[o+3];
  66. }
  67. o+=4;
  68. k+=4;
  69. }
  70. o+=a;
  71. }
  72. img->p[PIPI_PIXELS_RGBA_C].pixels = tmp;
  73. img->p[PIPI_PIXELS_RGBA_C].pitch = w*([myImage bitsPerPixel]/8);
  74. img->codec_priv = (struct pipi_codec_coreimage *) malloc(sizeof(struct pipi_codec_coreimage *));
  75. struct pipi_codec_coreimage *infos = (struct pipi_codec_coreimage *) img->codec_priv;
  76. infos->format = [myImage bitmapFormat];
  77. pipi_pixels_t *p = pipi_getpixels(img, PIPI_PIXELS_RGBA_C);
  78. img->codec_free = pipi_free_coreimage;
  79. [autoreleasepool release];
  80. return img;
  81. }
  82. int pipi_save_coreimage(pipi_image_t *img, const char *name)
  83. {
  84. NSAutoreleasePool *autoreleasepool = [[NSAutoreleasePool alloc] init];
  85. printf("%d\n", img->last_modified);
  86. pipi_pixels_t *p = pipi_getpixels(img, PIPI_PIXELS_RGBA_C);
  87. NSString *n = [NSString stringWithCString: name];
  88. NSBitmapImageRep *bitmap = [[NSBitmapImageRep alloc]
  89. initWithBitmapDataPlanes:NULL
  90. pixelsWide:p->w
  91. pixelsHigh:p->h
  92. bitsPerSample:8
  93. samplesPerPixel:4
  94. hasAlpha:YES
  95. isPlanar:NO
  96. colorSpaceName:NSCalibratedRGBColorSpace
  97. bitmapFormat: 0//(NSBitmapFormat)img->codec_priv
  98. bytesPerRow:p->w*4
  99. bitsPerPixel:32
  100. ];
  101. if(bitmap == nil) return -1;
  102. memcpy([bitmap bitmapData], p->pixels, p->w*p->h*4);
  103. NSBitmapImageFileType type = NSPNGFileType;
  104. if(strlen(name) > 4)
  105. {
  106. char *ext = (char*)&name[strlen(name) - 4];
  107. if( !strncasecmp(ext, ".png", 3)) type = NSPNGFileType;
  108. else if(!strncasecmp(ext, "jpeg", 4)) type = NSJPEGFileType;
  109. else if(!strncasecmp(ext, ".jpg", 3)) type = NSJPEGFileType;
  110. else if(!strncasecmp(ext, ".bmp", 3)) type = NSBMPFileType;
  111. else if(!strncasecmp(ext, ".tif", 3)) type = NSTIFFFileType;
  112. else if(!strncasecmp(ext, ".tiff", 3)) type = NSTIFFFileType;
  113. else if(!strncasecmp(ext, ".gif", 3)) type = NSGIFFileType;
  114. else if(!strncasecmp(ext, ".bmp", 3)) type = NSBMPFileType;
  115. else if(!strncasecmp(ext, ".jp2", 3)) type = NSJPEG2000FileType;
  116. else if(!strncasecmp(ext, ".j2k", 3)) type = NSJPEG2000FileType;
  117. }
  118. [[bitmap representationUsingType:type properties:nil] writeToFile:n atomically:YES];
  119. [autoreleasepool release];
  120. return 1;
  121. }
  122. /*
  123. * XXX: The following functions are local.
  124. */
  125. static int pipi_free_coreimage(pipi_image_t *img)
  126. {
  127. if(img->codec_priv)
  128. free(img->codec_priv);
  129. return 0;
  130. }
  131. #endif