source: libpipi/trunk/pipi/codec/coreimage.m @ 2905

Last change on this file since 2905 was 2905, checked in by Sam Hocevar, 12 years ago

Remove tabs in the code here and there.

File size: 3.0 KB
Line 
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
24#import <CIImage.h>
25
26pipi_image_t *pipi_load_coreimage(const char *name)
27{
28    NSAutoreleasePool *autoreleasepool = [[NSAutoreleasePool alloc] init];
29    NSString *n = [NSString stringWithCString: name];
30    CIImage *source;
31    NSURL *url = [NSURL fileURLWithPath:n];
32    source = [CIImage imageWithContentsOfURL:url];
33    CGRect extent = [source extent];
34    size_t w = (size_t)extent.size.width;
35    size_t h = (size_t)extent.size.height;
36
37
38    NSBitmapImageRep * myImage;
39    myImage = [[NSBitmapImageRep alloc] initWithCIImage:source];
40
41    pipi_image_t *img;
42    img = pipi_new(w, h);
43    img->p[PIPI_PIXELS_RGBA_C].pixels = [myImage bitmapData];
44    img->p[PIPI_PIXELS_RGBA_C].w = w;
45    img->p[PIPI_PIXELS_RGBA_C].h = h;
46    img->p[PIPI_PIXELS_RGBA_C].pitch = ([myImage bytesPerRow]/8) * img->w;
47    img->p[PIPI_PIXELS_RGBA_C].bpp = [myImage bitsPerPixel];
48    img->p[PIPI_PIXELS_RGBA_C].bytes = ([myImage bitsPerPixel]/8) * img->w * img->h;
49    img->last_modified = PIPI_PIXELS_RGBA_C;
50
51    img->codec_priv = (void*)[myImage bitmapFormat];
52
53
54    pipi_pixels_t *p = pipi_getpixels(img, PIPI_PIXELS_RGBA_C);
55
56    [autoreleasepool release];
57    return img;
58}
59
60
61int pipi_save_coreimage(pipi_image_t *img, const char *name)
62{
63    NSAutoreleasePool *autoreleasepool = [[NSAutoreleasePool alloc] init];
64    printf("%d\n", img->last_modified);
65    pipi_pixels_t *p = pipi_getpixels(img, PIPI_PIXELS_RGBA_C);
66
67    NSString *n = [NSString stringWithCString: name];
68    NSBitmapImageRep *bitmap = [[NSBitmapImageRep alloc]
69                                initWithBitmapDataPlanes:NULL
70                                pixelsWide:p->w
71                                pixelsHigh:p->h
72                                bitsPerSample:8
73                                samplesPerPixel:4
74                                hasAlpha:YES
75                                isPlanar:NO
76                                colorSpaceName:NSCalibratedRGBColorSpace
77                                bitmapFormat: 0//(NSBitmapFormat)img->codec_priv
78                                bytesPerRow:p->w*4
79                                bitsPerPixel:32
80                                ];
81    if(bitmap == nil) return -1;
82    memcpy([bitmap bitmapData], p->pixels, p->w*p->h*4);
83
84    [[bitmap representationUsingType:NSPNGFileType properties:nil] writeToFile:n  atomically:YES];
85    [autoreleasepool release];
86
87    return 1;
88}
89
90
91#endif
Note: See TracBrowser for help on using the repository browser.