25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

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