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

Last change on this file since 4081 was 4081, checked in by Jean-Yves Lamoureux, 13 years ago
  • Fixed Mac OSX compilation and Xcode project file
File size: 5.8 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
24static int pipi_free_coreimage(pipi_image_t *img);
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   
33    source = [CIImage imageWithContentsOfURL:url];
34   
35    if(source == NULL) return NULL;
36   
37    CGRect extent = [source extent];
38    size_t w = (size_t)extent.size.width;
39    size_t h = (size_t)extent.size.height;
40
41
42    NSBitmapImageRep * myImage;
43    myImage = [[NSBitmapImageRep alloc] initWithCIImage:source];
44
45    pipi_image_t *img;
46    img = pipi_new(w, h);
47
48    img->p[PIPI_PIXELS_RGBA_U8].w      = w;
49    img->p[PIPI_PIXELS_RGBA_U8].h      = h;
50    img->p[PIPI_PIXELS_RGBA_U8].pitch  = ([myImage bytesPerRow]/8) * img->w;
51    img->p[PIPI_PIXELS_RGBA_U8].bpp    = [myImage bitsPerPixel];
52    img->p[PIPI_PIXELS_RGBA_U8].bytes  = ([myImage bitsPerPixel]/8) * img->w * img->h;
53    img->last_modified                = PIPI_PIXELS_RGBA_U8;
54
55   
56    /* CoreImage feeds us with BGRA while we need RGBA, so convert it.
57     * We also need to get a pitch==(w*bpp) in order to pipi to opper properly.
58     */
59   
60    int pitch = (img->p[PIPI_PIXELS_RGBA_U8].bpp/8);
61    unsigned char *tmp  = (unsigned char*)malloc(h*w*pitch);
62    unsigned char *orig = (unsigned char*)[myImage bitmapData];
63    int x, y, k=0, o=0, a=[myImage bytesPerRow] - (w*([myImage bitsPerPixel]/8));
64
65    for(y=0; y<h; y++)
66    {
67        for(x=0; x<w*pitch; x+=4)
68        {
69            if(!([myImage bitmapFormat] & NSAlphaFirstBitmapFormat))
70            {
71                tmp[k+2] = orig[o];
72                tmp[k+1] = orig[o+1];
73                tmp[k+0] = orig[o+2];
74                tmp[k+3] = orig[o+3];
75            } else
76            {
77                tmp[k+0] = orig[o];
78                tmp[k+1] = orig[o+1];
79                tmp[k+2] = orig[o+2];
80                tmp[k+3] = orig[o+3];                   
81            }
82               
83            o+=4;
84            k+=4;
85        }
86        o+=a;
87    }
88    img->p[PIPI_PIXELS_RGBA_U8].pixels = tmp;
89    img->p[PIPI_PIXELS_RGBA_U8].pitch  = w*([myImage bitsPerPixel]/8);
90   
91   
92   
93   
94    img->codec_priv = (struct pipi_codec_coreimage *) malloc(sizeof(struct pipi_codec_coreimage *));
95    struct pipi_codec_coreimage *infos = (struct pipi_codec_coreimage *) img->codec_priv;
96    infos->format = [myImage bitmapFormat];
97
98    pipi_pixels_t *p = pipi_get_pixels(img, PIPI_PIXELS_RGBA_U8);
99   
100    img->codec_free = pipi_free_coreimage;
101   
102    [autoreleasepool release];
103    return img;
104}
105
106
107int pipi_save_coreimage(pipi_image_t *img, const char *name)
108{
109    NSAutoreleasePool *autoreleasepool = [[NSAutoreleasePool alloc] init];
110    pipi_pixels_t *p = pipi_get_pixels(img, PIPI_PIXELS_RGBA_U8);
111
112    int i;
113    char *data = p->pixels;
114    for(i = 0; i < img->w*img->h; i++)
115    {
116        unsigned char r = data[i*4 + 0];
117        unsigned char g = data[i*4 + 1];
118        unsigned char b = data[i*4 + 2];
119        unsigned char a = data[i*4 + 3];
120       
121        /* R */    data[i*4 + 0] = b;
122        /* G */    data[i*4 + 1] = g;
123        /* B */    data[i*4 + 2] = r;
124        /* A */    data[i*4 + 3] = a;   
125    }
126   
127   
128    NSString *n = [NSString stringWithCString: name];
129    NSBitmapImageRep *bitmap = [[NSBitmapImageRep alloc]
130                                initWithBitmapDataPlanes:NULL
131                                pixelsWide:p->w
132                                pixelsHigh:p->h
133                                bitsPerSample:8
134                                samplesPerPixel:4
135                                hasAlpha:YES
136                                isPlanar:NO
137                                colorSpaceName:NSCalibratedRGBColorSpace
138                                bitmapFormat: 0//(NSBitmapFormat)img->codec_priv
139                                bytesPerRow:p->w*4
140                                bitsPerPixel:32
141                                ];
142    if(bitmap == nil) return -1;
143    memcpy([bitmap bitmapData], data, p->w*p->h*4);
144
145    NSBitmapImageFileType type = NSPNGFileType;
146   
147   
148    if(strlen(name) > 4)
149    {
150        char *ext = (char*)&name[strlen(name) - 4];
151        if(     !strncasecmp(ext, ".png",  3)) type = NSPNGFileType;
152        else if(!strncasecmp(ext, "jpeg",  4)) type = NSJPEGFileType;
153        else if(!strncasecmp(ext, ".jpg",  3)) type = NSJPEGFileType;
154        else if(!strncasecmp(ext, ".bmp",  3)) type = NSBMPFileType;
155        else if(!strncasecmp(ext, ".tif",  3)) type = NSTIFFFileType;
156        else if(!strncasecmp(ext, ".tiff", 3)) type = NSTIFFFileType;
157        else if(!strncasecmp(ext, ".gif",  3)) type = NSGIFFileType;
158        else if(!strncasecmp(ext, ".bmp",  3)) type = NSBMPFileType;
159        else if(!strncasecmp(ext, ".jp2",  3)) type = NSJPEG2000FileType;
160        else if(!strncasecmp(ext, ".j2k",  3)) type = NSJPEG2000FileType;
161    }
162   
163    [[bitmap representationUsingType:type properties:nil] writeToFile:n  atomically:YES];
164    [autoreleasepool release];
165
166    return 1;
167}
168
169/*
170 * XXX: The following functions are local.
171 */
172
173static int pipi_free_coreimage(pipi_image_t *img)
174{
175    if(img->codec_priv)
176        free(img->codec_priv);
177    return 0;
178}
179
180#endif
Note: See TracBrowser for help on using the repository browser.