/*
 *  libpipi       Pathetic image processing interface library
 *  Copyright (c) 2004-2008 Sam Hocevar <sam@zoy.org>
 *                2008 Jean-Yves Lamoureux <jylam@lnxscene.org>
 *                All Rights Reserved
 *
 *  $Id$
 *
 *  This library is free software. It comes without any warranty, to
 *  the extent permitted by applicable law. You can redistribute it
 *  and/or modify it under the terms of the Do What The Fuck You Want
 *  To Public License, Version 2, as published by Sam Hocevar. See
 *  http://sam.zoy.org/wtfpl/COPYING for more details.
 */

/*
 * coreimage.m: CoreImage (OSX) I/O functions
 */

#import "coreimage.h"

#ifdef USE_COCOA

#import <CIImage.h>

pipi_image_t *pipi_load_coreimage(const char *name)
{
    NSAutoreleasePool *autoreleasepool = [[NSAutoreleasePool alloc] init];
    NSString *n = [NSString stringWithCString: name];
    CIImage *source;
    NSURL *url = [NSURL fileURLWithPath:n];
    source = [CIImage imageWithContentsOfURL:url];
    CGRect extent = [source extent];
    size_t w = (size_t)extent.size.width;
    size_t h = (size_t)extent.size.height;


    NSBitmapImageRep * myImage;
    myImage = [[NSBitmapImageRep alloc] initWithCIImage:source];

    pipi_image_t *img;
    img = pipi_new(w, h);
    img->p[PIPI_PIXELS_RGBA_C].pixels = [myImage bitmapData];
    img->p[PIPI_PIXELS_RGBA_C].w = w;
    img->p[PIPI_PIXELS_RGBA_C].h = h;
    img->p[PIPI_PIXELS_RGBA_C].pitch = ([myImage bytesPerRow]/8) * img->w;
    img->p[PIPI_PIXELS_RGBA_C].bpp = [myImage bitsPerPixel];
    img->p[PIPI_PIXELS_RGBA_C].bytes = ([myImage bitsPerPixel]/8) * img->w * img->h;
    img->last_modified = PIPI_PIXELS_RGBA_C;

    img->codec_priv = (void*)[myImage bitmapFormat];


    pipi_pixels_t *p = pipi_getpixels(img, PIPI_PIXELS_RGBA_C);

    [autoreleasepool release];
    return img;
}


int pipi_save_coreimage(pipi_image_t *img, const char *name)
{
    NSAutoreleasePool *autoreleasepool = [[NSAutoreleasePool alloc] init];
    printf("%d\n", img->last_modified);
    pipi_pixels_t *p = pipi_getpixels(img, PIPI_PIXELS_RGBA_C);

    NSString *n = [NSString stringWithCString: name];
    NSBitmapImageRep *bitmap = [[NSBitmapImageRep alloc]
                                initWithBitmapDataPlanes:NULL
                                pixelsWide:p->w
                                pixelsHigh:p->h
                                bitsPerSample:8
                                samplesPerPixel:4
                                hasAlpha:YES
                                isPlanar:NO
                                colorSpaceName:NSCalibratedRGBColorSpace
                                bitmapFormat: 0//(NSBitmapFormat)img->codec_priv
                                bytesPerRow:p->w*4
                                bitsPerPixel:32
                                ];
    if(bitmap == nil) return -1;
    memcpy([bitmap bitmapData], p->pixels, p->w*p->h*4);

    [[bitmap representationUsingType:NSPNGFileType properties:nil] writeToFile:n  atomically:YES];
    [autoreleasepool release];

    return 1;
}


#endif
