aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/linden/indra/llimagej2coj/llimagej2cquartz.mm
blob: d5eefd18443f474ef55a9ae7adf63f8252355c98 (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
#import <Accelerate/Accelerate.h>
#import <QuartzCore/QuartzCore.h>
#import <Quartz/Quartz.h>

#include "llimagej2cquartz.h"

#if defined(__BIG_ENDIAN__)
	CGImageAlphaInfo const kDefaultAlphaLocation = kCGImageAlphaPremultipliedLast;
#else
	CGImageAlphaInfo const kDefaultAlphaLocation = kCGImageAlphaPremultipliedFirst;
#endif

BOOL decodeJ2CQuartz(LLImageJ2C &base, LLImageRaw &raw_image, F32 decode_time, S32 first_channel, S32 max_channel_count)
{
	U8 *srcData = (U8*)base.getData();
	int srcLen = base.getDataSize();

	llinfos << "[1] compressed image size: '" << srcLen << "'" << llendl;

	int width = base.getWidth();
	int height = base.getHeight();
	int components = base.getComponents();

	S32 channels = components - first_channel;
	if( channels > max_channel_count )
		channels = max_channel_count;

	llinfos << "[2] components: '" << components << "' - channels: '" << channels << "' - first_channel: '" << first_channel << "' - max_channel_count: '" << max_channel_count << "'" << llendl;

	if(components <= first_channel || components > 4)
		return FALSE;

	llinfos << "[3] attempting to decode a texture: '" << width << "'X'" << height << "'@'" << components * 8 << "'" << llendl;

	U8 *tgt = (U8*)raw_image.getData();
	if (!tgt)
		return FALSE;

	raw_image.resize(width, height, channels);

	size_t rowBytes = width * components;
	int realLen = width * height * components;

	llinfos << "[4] allocating buffer of size: '" << realLen << "' to hold temp texture data" << llendl;
	unsigned char* dataplane;

	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
	NSBitmapImageRep *rep = [NSBitmapImageRep alloc];

	switch (components)
	{
		case 1:
		{
			rep = [[rep
				   initWithBitmapDataPlanes:nil
				   pixelsWide:width
				   pixelsHigh:height
				   bitsPerSample:8
				   samplesPerPixel:1
				   hasAlpha:NO
				   isPlanar:NO
				   colorSpaceName:NSDeviceWhiteColorSpace
				   bytesPerRow:rowBytes
				   bitsPerPixel:8
				   ] autorelease];

			memcpy([rep bitmapData], srcData, srcLen);

			dataplane = (unsigned char*)malloc(realLen);
			memcpy(dataplane, [rep bitmapData], realLen);

			[rep release];
		}
		break;

		case 3:
		{
			NSData *data = [NSData dataWithBytes:srcData length:srcLen];
			rep = [rep initWithData:data];

			dataplane = (unsigned char*)malloc(realLen);
			memcpy(dataplane, [rep bitmapData], realLen);

			[data release];
			[rep release];
		}
		break;

		case 4:
		{
			NSData *data = [NSData dataWithBytes:srcData length:srcLen];
			rep = [rep initWithData:data];

			int imgLen = [rep pixelsHigh] * [rep bytesPerRow];
			if (imgLen != realLen)
			{
				llwarns << "decoded image buffer size (" << imgLen << ") != expected buffer size (" << realLen << ") !" << llendl;
				[rep release];
				[data release];
				return FALSE;
			}
			
			dataplane = (unsigned char*)malloc(realLen);
			memcpy(dataplane, [rep bitmapData], realLen);

			vImage_Buffer vb;
			vb.data = dataplane;
			vb.height = [rep pixelsHigh];
			vb.width = [rep pixelsWide];
			vb.rowBytes = [rep bytesPerRow];

			llinfos << "Attempting Alpha Unpremultiplication" << llendl;
			vImageUnpremultiplyData_RGBA8888(&vb, &vb, 0);
			llinfos << "Unpremultiplied Alpha" << llendl;

			llwarns << "after decoding: " << [rep pixelsWide] << "'X'" << [rep pixelsHigh] << "'@'" << [rep bitsPerPixel] << "'" << llendl;

			[rep release];
			[data release];
		}
		break;
	}

	if (dataplane)
	{
		for (int h=height-1; h>=0; h--)
		{
			for (int w=0; w<rowBytes; w+=(first_channel + channels))
			{
				for (int c=first_channel; c<(first_channel + channels); c++)
					memcpy(tgt++, &dataplane[h*rowBytes + w + c], sizeof(unsigned char));
			}
		}

		free(dataplane);

		llinfos << "[5] size of decoded image is: '" << width*height*channels << "'" << llendl;

		return TRUE;
	}
	else
	{
		llwarns << "[5] cannot decode image !" << llendl;
	}

	return FALSE;
}