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