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

Last change on this file since 3020 was 3020, checked in by Jean-Yves Lamoureux, 14 years ago
  • CoreImage? loader fails cleanly when specified image doesn't exist
File size: 3.5 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#import <CIImage.h>
24
25
26static int pipi_free_coreimage(pipi_image_t *img);
27
28pipi_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   
35    source = [CIImage imageWithContentsOfURL:url];
36   
37    if(source == NULL) return NULL;
38   
39    CGRect extent = [source extent];
40    size_t w = (size_t)extent.size.width;
41    size_t h = (size_t)extent.size.height;
42
43
44    NSBitmapImageRep * myImage;
45    myImage = [[NSBitmapImageRep alloc] initWithCIImage:source];
46
47    pipi_image_t *img;
48    img = pipi_new(w, h);
49    img->p[PIPI_PIXELS_RGBA_C].pixels = [myImage bitmapData];
50    img->p[PIPI_PIXELS_RGBA_C].w      = w;
51    img->p[PIPI_PIXELS_RGBA_C].h      = h;
52    img->p[PIPI_PIXELS_RGBA_C].pitch  = ([myImage bytesPerRow]/8) * img->w;
53    img->p[PIPI_PIXELS_RGBA_C].bpp    = [myImage bitsPerPixel];
54    img->p[PIPI_PIXELS_RGBA_C].bytes  = ([myImage bitsPerPixel]/8) * img->w * img->h;
55    img->last_modified                = PIPI_PIXELS_RGBA_C;
56
57    img->codec_priv = (struct pipi_codec_coreimage *) malloc(sizeof(struct pipi_codec_coreimage *));
58    struct pipi_codec_coreimage *infos = (struct pipi_codec_coreimage *) img->codec_priv;
59    infos->format = [myImage bitmapFormat];
60
61    pipi_pixels_t *p = pipi_getpixels(img, PIPI_PIXELS_RGBA_C);
62   
63    img->codec_free = pipi_free_coreimage;
64   
65    [autoreleasepool release];
66    return img;
67}
68
69
70int pipi_save_coreimage(pipi_image_t *img, const char *name)
71{
72    NSAutoreleasePool *autoreleasepool = [[NSAutoreleasePool alloc] init];
73    printf("%d\n", img->last_modified);
74    pipi_pixels_t *p = pipi_getpixels(img, PIPI_PIXELS_RGBA_C);
75
76    NSString *n = [NSString stringWithCString: name];
77    NSBitmapImageRep *bitmap = [[NSBitmapImageRep alloc]
78                                initWithBitmapDataPlanes:NULL
79                                pixelsWide:p->w
80                                pixelsHigh:p->h
81                                bitsPerSample:8
82                                samplesPerPixel:4
83                                hasAlpha:YES
84                                isPlanar:NO
85                                colorSpaceName:NSCalibratedRGBColorSpace
86                                bitmapFormat: 0//(NSBitmapFormat)img->codec_priv
87                                bytesPerRow:p->w*4
88                                bitsPerPixel:32
89                                ];
90    if(bitmap == nil) return -1;
91    memcpy([bitmap bitmapData], p->pixels, p->w*p->h*4);
92
93    [[bitmap representationUsingType:NSPNGFileType properties:nil] writeToFile:n  atomically:YES];
94    [autoreleasepool release];
95
96    return 1;
97}
98
99/*
100 * XXX: The following functions are local.
101 */
102
103static int pipi_free_coreimage(pipi_image_t *img)
104{
105    return 0;
106}
107
108#endif
Note: See TracBrowser for help on using the repository browser.