Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

183 lignes
5.8 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_U8].w = w;
  38. img->p[PIPI_PIXELS_RGBA_U8].h = h;
  39. img->p[PIPI_PIXELS_RGBA_U8].pitch = ([myImage bytesPerRow]/8) * img->w;
  40. img->p[PIPI_PIXELS_RGBA_U8].bpp = [myImage bitsPerPixel];
  41. img->p[PIPI_PIXELS_RGBA_U8].bytes = ([myImage bitsPerPixel]/8) * img->w * img->h;
  42. img->last_modified = PIPI_PIXELS_RGBA_U8;
  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_U8].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_U8].pixels = tmp;
  73. img->p[PIPI_PIXELS_RGBA_U8].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_get_pixels(img, PIPI_PIXELS_RGBA_U8);
  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. pipi_pixels_t *p = pipi_get_pixels(img, PIPI_PIXELS_RGBA_U8);
  86. int i;
  87. char *data = p->pixels;
  88. for(i = 0; i < img->w*img->h; i++)
  89. {
  90. unsigned char r = data[i*4 + 0];
  91. unsigned char g = data[i*4 + 1];
  92. unsigned char b = data[i*4 + 2];
  93. unsigned char a = data[i*4 + 3];
  94. /* R */ data[i*4 + 0] = b;
  95. /* G */ data[i*4 + 1] = g;
  96. /* B */ data[i*4 + 2] = r;
  97. /* A */ data[i*4 + 3] = a;
  98. }
  99. NSString *n = [NSString stringWithCString: name];
  100. NSBitmapImageRep *bitmap = [[NSBitmapImageRep alloc]
  101. initWithBitmapDataPlanes:NULL
  102. pixelsWide:p->w
  103. pixelsHigh:p->h
  104. bitsPerSample:8
  105. samplesPerPixel:4
  106. hasAlpha:YES
  107. isPlanar:NO
  108. colorSpaceName:NSCalibratedRGBColorSpace
  109. bitmapFormat: 0//(NSBitmapFormat)img->codec_priv
  110. bytesPerRow:p->w*4
  111. bitsPerPixel:32
  112. ];
  113. if(bitmap == nil) return -1;
  114. memcpy([bitmap bitmapData], data, p->w*p->h*4);
  115. NSBitmapImageFileType type = NSPNGFileType;
  116. if(strlen(name) > 4)
  117. {
  118. char *ext = (char*)&name[strlen(name) - 4];
  119. if( !strncasecmp(ext, ".png", 3)) type = NSPNGFileType;
  120. else if(!strncasecmp(ext, "jpeg", 4)) type = NSJPEGFileType;
  121. else if(!strncasecmp(ext, ".jpg", 3)) type = NSJPEGFileType;
  122. else if(!strncasecmp(ext, ".bmp", 3)) type = NSBMPFileType;
  123. else if(!strncasecmp(ext, ".tif", 3)) type = NSTIFFFileType;
  124. else if(!strncasecmp(ext, ".tiff", 3)) type = NSTIFFFileType;
  125. else if(!strncasecmp(ext, ".gif", 3)) type = NSGIFFileType;
  126. else if(!strncasecmp(ext, ".bmp", 3)) type = NSBMPFileType;
  127. else if(!strncasecmp(ext, ".jp2", 3)) type = NSJPEG2000FileType;
  128. else if(!strncasecmp(ext, ".j2k", 3)) type = NSJPEG2000FileType;
  129. }
  130. [[bitmap representationUsingType:type properties:nil] writeToFile:n atomically:YES];
  131. [autoreleasepool release];
  132. return 1;
  133. }
  134. /*
  135. * XXX: The following functions are local.
  136. */
  137. static int pipi_free_coreimage(pipi_image_t *img)
  138. {
  139. if(img->codec_priv)
  140. free(img->codec_priv);
  141. return 0;
  142. }
  143. #endif