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

Last change on this file since 3342 was 3342, checked in by Sam Hocevar, 11 years ago

Change _C pixel format suffixes into _U8 for more clarity.

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