Welcome to mirror list, hosted at ThFree Co, Russian Federation.

imbuf_cocoa.m « intern « imbuf « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9b627612d2aac3a040db4cdca4d7e029245affa6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/*
 * imbuf_coca.m
 *
 * $Id$
 * 
 * ***** 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * Contributor(s): Damien Plisson 10/2009
 *
 * ***** END GPL LICENSE BLOCK *****
 */

/**
 * Provides image file loading and saving for Blender, via Cocoa.
 *
 */

#include <stdint.h>
#include <string.h>
#import <Cocoa/Cocoa.h>

#include "imbuf.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;
	NSSize bitmapSize;
	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;
	}
	
	bitmapSize.width = [bitmapImage pixelsWide];
	bitmapSize.height = [bitmapImage pixelsHigh];
	
	/* Tell cocoa image resolution is same as current system one */
	[bitmapImage setSize:bitmapSize];
	
	/* allocate the image buffer */
	ibuf = IMB_allocImBuf(bitmapSize.width, bitmapSize.height, 32/*RGBA*/, 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:bitmapSize.width 
																	  pixelsHigh:bitmapSize.height
																   bitsPerSample:8 samplesPerPixel:3 hasAlpha:NO isPlanar:NO
																  colorSpaceName:NSCalibratedRGBColorSpace 
																	bitmapFormat:0
																	 bytesPerRow:4*bitmapSize.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:bitmapSize.width
																		  pixelsHigh:bitmapSize.height
																	   bitsPerSample:8 samplesPerPixel:4 hasAlpha:YES isPlanar:NO
																	  colorSpaceName:NSCalibratedRGBColorSpace
																		bitmapFormat:0
																		 bytesPerRow:4*bitmapSize.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 < bitmapSize.width; x++) {
			for (y = 0; y < bitmapSize.height; y++) {
				to_i = (bitmapSize.height-y-1)*bitmapSize.width + x;
				from_i = y*bitmapSize.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);

	ibuf->ftype = TIF;
	ibuf->profile = IB_PROFILE_SRGB;

	/* 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_t 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_t)((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) );
}