summaryrefslogtreecommitdiff
path: root/DevIL/src-IL/src/il_profiles.cpp
blob: 695f9a2c3b8def835284f60113808297d85ebcaa (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
//-----------------------------------------------------------------------------
//
// ImageLib Sources
// Copyright (C) 2000-2017 by Denton Woods
// Last modified: 01/23/2001 <--Y2K Compliant! =]
//
// Filename: src-IL/src/il_profiles.cpp
//
// Description: Colour profile handler
//
//-----------------------------------------------------------------------------

#include "il_internal.h"
#ifndef IL_NO_LCMS

#ifdef PACKAGE_NAME
#define IL_PACKAGE_NAME PACKAGE_NAME;
#undef  PACKAGE_NAME
#endif

#if (!defined(_WIN32) && !defined(_WIN64))
	#define NON_WINDOWS 1
	/*#ifdef LCMS_NODIRINCLUDE
		#include <lcms.h>
	#else
		#include <lcms/lcms.h>
	#endif*/
	#include <lcms2.h>
#else
	#if defined(IL_USE_PRAGMA_LIBS)
		#if defined(_MSC_VER) || defined(__BORLANDC__)
			#ifndef _DEBUG
				#pragma comment(lib, "lcms2_static.lib")
			#else
				#pragma comment(lib, "lcms2_static.lib")
			#endif
		#endif
	#endif

	#include <lcms2.h>
#endif//_WIN32

#ifdef PACKAGE_NAME
#undef PACKAGE_NAME
#endif

#ifdef IL_PACKAGE_NAME
#define PACKAGE_NAME IL_PACKAGE_NAME
#undef  IL_PACKAGE_NAME
#endif

#endif//IL_NO_LCMS

ILboolean ILAPIENTRY ilApplyProfile(ILstring InProfile, ILstring OutProfile)
{
#ifndef IL_NO_LCMS
	cmsHPROFILE		hInProfile, hOutProfile;
	cmsHTRANSFORM	hTransform;
	ILubyte			*Temp;
	ILint			Format=0;
#ifdef _UNICODE
	char AnsiName[512];
#endif//_UNICODE

	if (iCurImage == NULL) {
		ilSetError(IL_ILLEGAL_OPERATION);
		return IL_FALSE;
	}

	switch (iCurImage->Type)
	{
		case IL_BYTE:
		case IL_UNSIGNED_BYTE:
			switch (iCurImage->Format)
			{
				case IL_LUMINANCE:
					Format = TYPE_GRAY_8;
					break;
				case IL_RGB:
					Format = TYPE_RGB_8;
					break;
				case IL_BGR:
					Format = TYPE_BGR_8;
					break;
				case IL_RGBA:
					Format = TYPE_RGBA_8;
					break;
				case IL_BGRA:
					Format = TYPE_BGRA_8;
					break;
				default:
					ilSetError(IL_INTERNAL_ERROR);
					return IL_FALSE;
			}
			break;

		case IL_SHORT:
		case IL_UNSIGNED_SHORT:
			switch (iCurImage->Format)
			{
				case IL_LUMINANCE:
					Format = TYPE_GRAY_16;
					break;
				case IL_RGB:
					Format = TYPE_RGB_16;
					break;
				case IL_BGR:
					Format = TYPE_BGR_16;
					break;
				case IL_RGBA:
					Format = TYPE_RGBA_16;
					break;
				case IL_BGRA:
					Format = TYPE_BGRA_16;
					break;
				default:
					ilSetError(IL_INTERNAL_ERROR);
					return IL_FALSE;
			}
			break;

		// These aren't supported right now.
		case IL_INT:
		case IL_UNSIGNED_INT:
		case IL_FLOAT:
		case IL_DOUBLE:
			ilSetError(IL_ILLEGAL_OPERATION);
			return IL_FALSE;
	}


	if (InProfile == NULL) {
		if (!iCurImage->Profile || !iCurImage->ProfileSize) {
			ilSetError(IL_INVALID_PARAM);
			return IL_FALSE;
		}
		hInProfile = iCurImage->Profile;
	}
	else {
#ifndef _UNICODE
 		hInProfile = cmsOpenProfileFromFile(InProfile, "r");
#else
		wcstombs(AnsiName, InProfile, 512);
		hInProfile = cmsOpenProfileFromFile(AnsiName, "r");
#endif//_UNICODE
	}
#ifndef _UNICODE
 	hOutProfile = cmsOpenProfileFromFile(OutProfile, "r");
#else
	wcstombs(AnsiName, OutProfile, 512);
	hOutProfile = cmsOpenProfileFromFile(AnsiName, "r");
#endif//_UNICODE

	hTransform = cmsCreateTransform(hInProfile, Format, hOutProfile, Format, INTENT_PERCEPTUAL, 0);

	Temp = (ILubyte*)ialloc(iCurImage->SizeOfData);
	if (Temp == NULL) {
		return IL_FALSE;
	}

	cmsDoTransform(hTransform, iCurImage->Data, Temp, iCurImage->SizeOfData / 3);

	ifree(iCurImage->Data);
	iCurImage->Data = Temp;

	cmsDeleteTransform(hTransform);
	if (InProfile != NULL)
		cmsCloseProfile(hInProfile);
	cmsCloseProfile(hOutProfile);

#endif//IL_NO_LCMS

	return IL_TRUE;
}