Mac / COCOA : Imbuf

- replace libtiff by calls to Cocoa services to load/save tiff files
(Libtiff, dynamically linked is not distributed with OS X, and would have had to be shipped for all four architectures)

The imb_cocoaLoadImage & imb_cocoaSaveImage are generic towards the bitmap format, and thus can handle TIFF, GIF, JPG, JP2000, BMP and raw camera formats (read-only for these), even if today only TIFF is used as the other formats are already handled.

- CMake updated
- scons updated (Thx to Jens Verwiebe)
This commit is contained in:
2009-10-29 18:06:49 +00:00
parent f5d62d885b
commit b1a832a7e2
10 changed files with 490 additions and 9 deletions

View File

@@ -217,6 +217,11 @@ if env['WITH_BF_OPENMP'] == 1:
env.Append(CPPFLAGS=['-fopenmp'])
env.Append(CXXFLAGS=['-fopenmp'])
if env['WITH_GHOST_COCOA'] == True:
env.Append(CFLAGS=['-DGHOST_COCOA'])
env.Append(CXXFLAGS=['-DGHOST_COCOA'])
env.Append(CPPFLAGS=['-DGHOST_COCOA'])
#check for additional debug libnames
if env.has_key('BF_DEBUG_LIBS'):

View File

@@ -10,10 +10,10 @@ sources = env.Glob('intern/*.cpp')
if window_system == 'darwin':
sources += env.Glob('intern/*.mm')
if env['WITH_GHOST_COCOA'] == True:
env.Append(CFLAGS=['-DGHOST_COCOA'])
env.Append(CXXFLAGS=['-DGHOST_COCOA'])
env.Append(CPPFLAGS=['-DGHOST_COCOA'])
#if env['WITH_GHOST_COCOA'] == True:
# env.Append(CFLAGS=['-DGHOST_COCOA'])
# env.Append(CXXFLAGS=['-DGHOST_COCOA'])
# env.Append(CPPFLAGS=['-DGHOST_COCOA'])
#defs = ''
#if env['WITH_GHOST_COCOA']:

View File

@@ -24,7 +24,13 @@
#
# ***** END GPL LICENSE BLOCK *****
FILE(GLOB SRC intern/*.c)
IF(WITH_COCOA)
FILE(GLOB SRC intern/*.c intern/*.m)
LIST(REMOVE_ITEM SRC "${CMAKE_CURRENT_SOURCE_DIR}/intern/dynlibtiff.c")
LIST(REMOVE_ITEM SRC "${CMAKE_CURRENT_SOURCE_DIR}/intern/tiff.c")
ELSE(WITH_COCOA)
FILE(GLOB SRC intern/*.c)
ENDIF(WITH_COCOA)
SET(INC
. ../makesdna ../../../intern/guardedalloc ../../../intern/memutil ../blenlib
@@ -54,7 +60,7 @@ IF(WITH_FFMPEG)
ADD_DEFINITIONS(-DWITH_FFMPEG)
ENDIF(WITH_FFMPEG)
if(WITH_DDS)
IF(WITH_DDS)
ADD_DEFINITIONS(-DWITH_DDS)
ENDIF(WITH_DDS)

View File

@@ -2,10 +2,15 @@
Import ('env')
sources = env.Glob('intern/*.c')
if env['WITH_GHOST_COCOA']:
sources += env.Glob('intern/*.m')
sources.remove('intern/dynlibtiff.c')
sources.remove('intern/tiff.c')
incs = '. ../makesdna #/intern/guardedalloc #/intern/memutil ../blenlib'
incs += ' ../avi ../blenkernel'
incs += ' ' + env['BF_JPEG_INC']
incs += ' ' + env['BF_PNG_INC']
incs += ' ' + env['BF_TIFF_INC']

View File

@@ -0,0 +1,43 @@
/*
* IMB_cocoa.h
*
* $Id: IMB_cocoa.h 13161 2008-01-07 19:13:47Z hos $
*
* ***** BEGIN GPL LICENSE BLOCK *****
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* Contributor(s): Damien Plisson 10/2009
*
* ***** END GPL LICENSE BLOCK *****
*/
/**
* \file IMB_cocoa.h
* \ingroup imbuf
* \brief Function declarations for imbuf_cocoa.m
*/
#ifndef IMB_COCOA_H
#define IMB_COCOA_H
/* Foward declaration of ImBuf structure. */
struct ImBuf;
/* Declarations for imbuf_cocoa.m */
struct ImBuf *imb_cocoaLoadImage(unsigned char *mem, int size, int flags);
short imb_cocoaSaveImage(struct ImBuf *ibuf, char *name, int flags);
#endif /* IMB_COCOA_H */

View File

@@ -0,0 +1,382 @@
/*
* imbuf_coca.m
*
* $Id: imbuf_cocoa.m 17958 2008-12-19 19:11:02Z blendix $
*
* ***** BEGIN GPL LICENSE BLOCK *****
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* Contributor(s): Damien Plisson 10/2009
*
* ***** END GPL LICENSE BLOCK *****
*/
/**
* Provides image file loading and saving for Blender, via Cocoa.
*
*/
#include <string.h>
#import <Cocoa/Cocoa.h>
#include "imbuf.h"
#include "imbuf_patch.h"
#include "IMB_cocoa.h"
#include "BKE_global.h"
#include "BKE_colortools.h"
#include "IMB_imbuf_types.h"
#include "IMB_imbuf.h"
#include "IMB_allocimbuf.h"
#pragma mark load/save functions
/**
* Loads an image from the supplied buffer
*
* Loads any Core Graphics supported type
* Currently is : TIFF, BMP, JPEG, GIF, PNG, DIB, ICO, and various RAW formats
*
* @param mem: Memory containing the bitmap image
* @param size: Size of the mem buffer.
* @param flags: If flags has IB_test set then the file is not actually loaded,
* but all other operations take place.
*
* @return: A newly allocated ImBuf structure if successful, otherwise NULL.
*/
struct ImBuf *imb_cocoaLoadImage(unsigned char *mem, int size, int flags)
{
struct ImBuf *ibuf = NULL;
uint32 width, height;
uchar *rasterRGB = NULL;
uchar *rasterRGBA = NULL;
uchar *toIBuf = NULL;
int x, y, to_i, from_i;
NSData *data;
NSBitmapImageRep *bitmapImage;
NSBitmapImageRep *blBitmapFormatImageRGB,*blBitmapFormatImageRGBA;
NSAutoreleasePool *pool;
pool = [[NSAutoreleasePool alloc] init];
data = [NSData dataWithBytes:mem length:size];
bitmapImage = [[NSBitmapImageRep alloc] initWithData:data];
if (!bitmapImage) {
fprintf(stderr, "imb_cocoaLoadImage: error loading image\n");
[pool drain];
return NULL;
}
width = [bitmapImage pixelsWide];
height = [bitmapImage pixelsHigh];
/* allocate the image buffer */
ibuf = IMB_allocImBuf(width, height, 32/*RGBA*/, 0, 0);
if (!ibuf) {
fprintf(stderr,
"imb_cocoaLoadImage: could not allocate memory for the " \
"image.\n");
[bitmapImage release];
[pool drain];
return NULL;
}
/* read in the image data */
if (!(flags & IB_test)) {
/* allocate memory for the ibuf->rect */
imb_addrectImBuf(ibuf);
/* Convert the image in a RGBA 32bit format */
/* As Core Graphics does not support contextes with non premutliplied alpha,
we need to get alpha key values in a separate batch */
/* First get RGB values w/o Alpha to avoid pre-multiplication, 32bit but last byte is unused */
blBitmapFormatImageRGB = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:NULL
pixelsWide:width
pixelsHigh:height
bitsPerSample:8 samplesPerPixel:3 hasAlpha:NO isPlanar:NO
colorSpaceName:NSCalibratedRGBColorSpace
bitmapFormat:0
bytesPerRow:4*width
bitsPerPixel:32/*RGB format padded to 32bits*/];
[NSGraphicsContext saveGraphicsState];
[NSGraphicsContext setCurrentContext:[NSGraphicsContext graphicsContextWithBitmapImageRep:blBitmapFormatImageRGB]];
[bitmapImage draw];
[NSGraphicsContext restoreGraphicsState];
rasterRGB = (uchar*)[blBitmapFormatImageRGB bitmapData];
if (rasterRGB == NULL) {
[bitmapImage release];
[blBitmapFormatImageRGB release];
[pool drain];
return NULL;
}
/* Then get Alpha values by getting the RGBA image (that is premultiplied btw) */
blBitmapFormatImageRGBA = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:NULL
pixelsWide:width
pixelsHigh:height
bitsPerSample:8 samplesPerPixel:4 hasAlpha:YES isPlanar:NO
colorSpaceName:NSCalibratedRGBColorSpace
bitmapFormat:0
bytesPerRow:4*width
bitsPerPixel:32/* RGBA */];
[NSGraphicsContext saveGraphicsState];
[NSGraphicsContext setCurrentContext:[NSGraphicsContext graphicsContextWithBitmapImageRep:blBitmapFormatImageRGBA]];
[bitmapImage draw];
[NSGraphicsContext restoreGraphicsState];
rasterRGBA = (uchar*)[blBitmapFormatImageRGBA bitmapData];
if (rasterRGBA == NULL) {
[bitmapImage release];
[blBitmapFormatImageRGB release];
[blBitmapFormatImageRGBA release];
[pool drain];
return NULL;
}
/*Copy the image to ibuf, flipping it vertically*/
toIBuf = (uchar*)ibuf->rect;
for (x = 0; x < width; x++) {
for (y = 0; y < height; y++) {
to_i = (height-y-1)*width + x;
from_i = y*width + x;
toIBuf[4*to_i] = rasterRGB[4*from_i]; /* R */
toIBuf[4*to_i+1] = rasterRGB[4*from_i+1]; /* G */
toIBuf[4*to_i+2] = rasterRGB[4*from_i+2]; /* B */
toIBuf[4*to_i+3] = rasterRGBA[4*from_i+3]; /* A */
}
}
[blBitmapFormatImageRGB release];
[blBitmapFormatImageRGBA release];
}
/* release the cocoa objects */
[bitmapImage release];
[pool drain];
if (ENDIAN_ORDER == B_ENDIAN) IMB_convert_rgba_to_abgr(ibuf);
/* return successfully */
return (ibuf);
}
/**
* Saves an image to a file.
*
* ImBuf structures with 1, 3 or 4 bytes per pixel (GRAY, RGB, RGBA
* respectively) are accepted, and interpreted correctly.
*
* Accepted formats: TIFF, GIF, BMP, PNG, JPEG, JPEG2000
*
* @param ibuf: Image buffer.
* @param name: Name of the image file to create.
* @param flags: Currently largely ignored.
*
* @return: 1 if the function is successful, 0 on failure.
*/
#define FTOUSHORT(val) ((val >= 1.0f-0.5f/65535)? 65535: (val <= 0.0f)? 0: (unsigned short)(val*65535.0f + 0.5f))
short imb_cocoaSaveImage(struct ImBuf *ibuf, char *name, int flags)
{
uint16 samplesperpixel, bitspersample;
unsigned char *from = NULL, *to = NULL;
unsigned short *to16 = NULL;
float *fromf = NULL;
int x, y, from_i, to_i, i;
int success;
BOOL hasAlpha;
NSString* colorSpace;
NSBitmapImageRep *blBitmapFormatImage;
NSData *dataToWrite;
NSDictionary *imageProperties;
NSAutoreleasePool *pool;
if (!ibuf) return FALSE;
if (!name) return FALSE;
/* check for a valid number of bytes per pixel. Like the PNG writer,
* the TIFF writer supports 1, 3 or 4 bytes per pixel, corresponding
* to gray, RGB, RGBA respectively. */
samplesperpixel = (uint16)((ibuf->depth + 7) >> 3);
switch (samplesperpixel) {
case 4: /*RGBA type*/
hasAlpha = YES;
colorSpace = NSCalibratedRGBColorSpace;
break;
case 3: /*RGB type*/
hasAlpha = NO;
colorSpace = NSCalibratedRGBColorSpace;
break;
case 1:
hasAlpha = NO;
colorSpace = NSCalibratedWhiteColorSpace;
break;
default:
fprintf(stderr,
"imb_cocoaSaveImage: unsupported number of bytes per "
"pixel: %d\n", samplesperpixel);
return (0);
}
if((ibuf->ftype & TIF_16BIT) && ibuf->rect_float)
bitspersample = 16;
else
bitspersample = 8;
pool = [[NSAutoreleasePool alloc] init];
/* Create bitmap image rep in blender format */
blBitmapFormatImage = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:NULL
pixelsWide:ibuf->x
pixelsHigh:ibuf->y
bitsPerSample:bitspersample samplesPerPixel:samplesperpixel hasAlpha:hasAlpha isPlanar:NO
colorSpaceName:colorSpace
bitmapFormat:NSAlphaNonpremultipliedBitmapFormat
bytesPerRow:(ibuf->x*bitspersample*samplesperpixel/8)
bitsPerPixel:(bitspersample*samplesperpixel)];
if (!blBitmapFormatImage) {
[pool drain];
return FALSE;
}
/* setup pointers */
if(bitspersample == 16) {
fromf = ibuf->rect_float;
to16 = (unsigned short*)[blBitmapFormatImage bitmapData];
}
else {
from = (unsigned char*)ibuf->rect;
to = (unsigned char*)[blBitmapFormatImage bitmapData];
}
/* copy pixel data. While copying, we flip the image vertically. */
for (x = 0; x < ibuf->x; x++) {
for (y = 0; y < ibuf->y; y++) {
from_i = 4*(y*ibuf->x+x);
to_i = samplesperpixel*((ibuf->y-y-1)*ibuf->x+x);
if(bitspersample == 16) {
if (ibuf->profile == IB_PROFILE_SRGB) {
switch (samplesperpixel) {
case 4 /*RGBA*/:
to16[to_i] = FTOUSHORT(linearrgb_to_srgb(fromf[from_i]));
to16[to_i+1] = FTOUSHORT(linearrgb_to_srgb(fromf[from_i+1]));
to16[to_i+2] = FTOUSHORT(linearrgb_to_srgb(fromf[from_i+2]));
to16[to_i+3] = FTOUSHORT(fromf[from_i+3]);
break;
case 3 /*RGB*/:
to16[to_i] = FTOUSHORT(linearrgb_to_srgb(fromf[from_i]));
to16[to_i+1] = FTOUSHORT(linearrgb_to_srgb(fromf[from_i+1]));
to16[to_i+2] = FTOUSHORT(linearrgb_to_srgb(fromf[from_i+2]));
break;
case 1 /*BW*/:
to16[to_i] = FTOUSHORT(linearrgb_to_srgb(fromf[from_i]));
break;
}
}
else {
for (i = 0; i < samplesperpixel; i++, to_i++, from_i++)
to16[to_i] = FTOUSHORT(fromf[from_i]);
}
}
else {
/* 8bits per sample*/
for (i = 0; i < samplesperpixel; i++, to_i++, from_i++)
to[to_i] = from[from_i];
}
}
}
/* generate file data */
if (IS_tiff(ibuf)) {
dataToWrite = [blBitmapFormatImage TIFFRepresentationUsingCompression:NSTIFFCompressionLZW factor:1.0];
}
else if (IS_png(ibuf)) {
imageProperties = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:false], NSImageInterlaced,
nil];
dataToWrite = [blBitmapFormatImage representationUsingType:NSPNGFileType properties:imageProperties];
}
else if (IS_bmp(ibuf)) {
dataToWrite = [blBitmapFormatImage representationUsingType:NSBMPFileType properties:nil];
}
else {/* JPEG by default */
int quality;
quality = ibuf->ftype & 0xff;
if (quality <= 0) quality = 90; /* Standard quality if wrong supplied*/
if (quality > 100) quality = 100;
imageProperties = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithFloat:quality], NSImageCompressionFactor,
[NSNumber numberWithBool:true], NSImageProgressive,
nil];
dataToWrite = [blBitmapFormatImage representationUsingType:NSJPEGFileType properties:imageProperties];
}
/* Write the file */
success = [dataToWrite writeToFile:[NSString stringWithCString:name encoding:NSISOLatin1StringEncoding]
atomically:YES];
[blBitmapFormatImage release];
[pool drain];
return success;
}
#pragma mark format checking functions
/* Currently, only tiff format is handled, so need to include here function that was previously in tiff.c */
/**
* Checks whether a given memory buffer contains a TIFF file.
*
* FIXME: Possible memory leak if mem is less than IMB_TIFF_NCB bytes long.
* However, changing this will require up-stream modifications.
*
* This method uses the format identifiers from:
* http://www.faqs.org/faqs/graphics/fileformats-faq/part4/section-9.html
* The first four bytes of big-endian and little-endian TIFF files
* respectively are (hex):
* 4d 4d 00 2a
* 49 49 2a 00
* Note that TIFF files on *any* platform can be either big- or little-endian;
* it's not platform-specific.
*
* AFAICT, libtiff doesn't provide a method to do this automatically, and
* hence my manual comparison. - Jonathan Merritt (lancelet) 4th Sept 2005.
*/
#define IMB_TIFF_NCB 4 /* number of comparison bytes used */
int imb_is_a_tiff(void *mem)
{
char big_endian[IMB_TIFF_NCB] = { 0x4d, 0x4d, 0x00, 0x2a };
char lil_endian[IMB_TIFF_NCB] = { 0x49, 0x49, 0x2a, 0x00 };
return ( (memcmp(big_endian, mem, IMB_TIFF_NCB) == 0) ||
(memcmp(lil_endian, mem, IMB_TIFF_NCB) == 0) );
}

View File

@@ -53,11 +53,16 @@
#include "IMB_hamx.h"
#include "IMB_jpeg.h"
#include "IMB_bmp.h"
#include "IMB_tiff.h"
#include "IMB_radiance_hdr.h"
#include "IMB_dpxcineon.h"
#include "BKE_global.h"
#if defined(__APPLE__) && defined(GHOST_COCOA)
#include "IMB_cocoa.h"
#else
#include "IMB_tiff.h"
#endif
#ifdef WITH_OPENJPEG
#include "IMB_jp2.h"
#endif
@@ -152,11 +157,19 @@ ImBuf *IMB_ibImageFromMemory(int *mem, int size, int flags) {
ibuf = imb_loadcineon((uchar *)mem, size, flags);
if (ibuf) return(ibuf);
#if defined(__APPLE__) && defined(GHOST_COCOA)
ibuf = imb_cocoaLoadImage((uchar *)mem, size, flags);
if(ibuf) {
ibuf->ftype = TIF;
return ibuf;
}
#else
if (G.have_libtiff) {
ibuf = imb_loadtiff((uchar *)mem, size, flags);
if (ibuf) return(ibuf);
}
#endif
ibuf = imb_loadhdr((uchar*)mem, size, flags);
if (ibuf) return (ibuf);

View File

@@ -51,14 +51,22 @@
#include "IMB_amiga.h"
#include "IMB_png.h"
#include "IMB_bmp.h"
#include "IMB_tiff.h"
#include "IMB_radiance_hdr.h"
#if defined(__APPLE__) && defined(GHOST_COCOA)
#include "IMB_cocoa.h"
#else
#include "IMB_tiff.h"
#endif
#ifdef WITH_OPENJPEG
#include "IMB_jp2.h"
#endif
#ifdef WITH_OPENEXR
#include "openexr/openexr_api.h"
#endif
#ifdef WITH_DDS
#include "dds/dds_api.h"
#endif
@@ -110,11 +118,21 @@ short IMB_saveiff(struct ImBuf *ibuf, char *name, int flags)
IMB_rect_from_float(ibuf);
return imb_saveiris(ibuf, name, flags);
}
#if defined(__APPLE__) && defined(GHOST_COCOA)
if (IS_tiff(ibuf)) {
if(ibuf->rect==NULL && ibuf->rect_float)
IMB_rect_from_float(ibuf);
return imb_cocoaSaveImage(ibuf, name, flags);
}
#else
if (G.have_libtiff && IS_tiff(ibuf)) {
if(ibuf->rect==NULL && ibuf->rect_float)
IMB_rect_from_float(ibuf);
return imb_savetiff(ibuf, name, flags);
}
#endif
#ifdef WITH_OPENEXR
if (IS_openexr(ibuf)) {
return imb_save_openexr(ibuf, name, flags);

View File

@@ -247,7 +247,9 @@ void WM_exit(bContext *C)
BPY_end_python();
#endif
#if !(defined(__APPLE__) && defined(GHOST_COCOA))
libtiff_exit();
#endif
#ifdef WITH_QUICKTIME
quicktime_exit();

View File

@@ -368,8 +368,10 @@ int main(int argc, char **argv)
switch(argv[a][1]) {
case 'a': /* -b was not given, play an animation */
#if !(defined(__APPLE__) && defined(GHOST_COCOA))
/* exception here, see below, it probably needs happens after qt init? */
libtiff_init();
#endif
// XXX playanim(argc-1, argv+1);
exit(0);
@@ -538,6 +540,10 @@ int main(int argc, char **argv)
#endif /* WITH_QUICKTIME */
#if defined(__APPLE__) && defined(GHOST_COCOA)
/* libtiff is not used, Cocoa services are used instead for tiff I/O */
G.have_libtiff = 1;
#else
/* dynamically load libtiff, if available */
libtiff_init();
if (!G.have_libtiff && (G.f & G_DEBUG)) {
@@ -545,6 +551,7 @@ int main(int argc, char **argv)
printf("Try setting the BF_TIFF_LIB environment variable if you want this support.\n");
printf("Example: setenv BF_TIFF_LIB /usr/lib/libtiff.so\n");
}
#endif
/* OK we are ready for it */