1 //-----------------------------------------------------------------------------
2 //
3 // ImageLib Sources
4 // Copyright (C) 2000-2002 by Denton Woods
5 // Last modified: 01/23/2001 <--Y2K Compliant! =]
6 //
7 // Filename: src-IL/src/il_profiles.c
8 //
9 // Description: Colour profile handler
10 //
11 //-----------------------------------------------------------------------------
12 
13 #include "il_internal.h"
14 #ifndef IL_NO_LCMS
15 
16 #ifdef PACKAGE_NAME
17 #define IL_PACKAGE_NAME PACKAGE_NAME;
18 #undef  PACKAGE_NAME
19 #endif
20 
21 #if (!defined(_WIN32) && !defined(_WIN64))
22 	#define NON_WINDOWS 1
23 	#ifdef LCMS_NODIRINCLUDE
24 		#include <lcms.h>
25 	#else
26 		#include <lcms/lcms.h>
27 	#endif
28 
29 #else
30 	#if defined(IL_USE_PRAGMA_LIBS)
31 		#if defined(_MSC_VER) || defined(__BORLANDC__)
32 			#ifndef _DEBUG
33 				#pragma comment(lib, "lcms.lib")
34 			#else
35 				#pragma comment(lib, "lcms-d.lib")
36 			#endif
37 		#endif
38 	#endif
39 
40 	#include <lcms.h>
41 #endif//_WIN32
42 
43 #ifdef PACKAGE_NAME
44 #undef PACKAGE_NAME
45 #endif
46 
47 #ifdef IL_PACKAGE_NAME
48 #define PACKAGE_NAME IL_PACKAGE_NAME
49 #undef  IL_PACKAGE_NAME
50 #endif
51 
52 #endif//IL_NO_LCMS
53 
ilApplyProfile(ILstring InProfile,ILstring OutProfile)54 ILboolean ILAPIENTRY ilApplyProfile(ILstring InProfile, ILstring OutProfile)
55 {
56 #ifndef IL_NO_LCMS
57 	cmsHPROFILE		hInProfile, hOutProfile;
58 	cmsHTRANSFORM	hTransform;
59 	ILubyte			*Temp;
60 	ILint			Format=0;
61 #ifdef _UNICODE
62 	char AnsiName[512];
63 #endif//_UNICODE
64 
65 	if (iCurImage == NULL) {
66 		ilSetError(IL_ILLEGAL_OPERATION);
67 		return IL_FALSE;
68 	}
69 
70 	switch (iCurImage->Type)
71 	{
72 		case IL_BYTE:
73 		case IL_UNSIGNED_BYTE:
74 			switch (iCurImage->Format)
75 			{
76 				case IL_LUMINANCE:
77 					Format = TYPE_GRAY_8;
78 					break;
79 				case IL_RGB:
80 					Format = TYPE_RGB_8;
81 					break;
82 				case IL_BGR:
83 					Format = TYPE_BGR_8;
84 					break;
85 				case IL_RGBA:
86 					Format = TYPE_RGBA_8;
87 					break;
88 				case IL_BGRA:
89 					Format = TYPE_BGRA_8;
90 					break;
91 				default:
92 					ilSetError(IL_INTERNAL_ERROR);
93 					return IL_FALSE;
94 			}
95 			break;
96 
97 		case IL_SHORT:
98 		case IL_UNSIGNED_SHORT:
99 			switch (iCurImage->Format)
100 			{
101 				case IL_LUMINANCE:
102 					Format = TYPE_GRAY_16;
103 					break;
104 				case IL_RGB:
105 					Format = TYPE_RGB_16;
106 					break;
107 				case IL_BGR:
108 					Format = TYPE_BGR_16;
109 					break;
110 				case IL_RGBA:
111 					Format = TYPE_RGBA_16;
112 					break;
113 				case IL_BGRA:
114 					Format = TYPE_BGRA_16;
115 					break;
116 				default:
117 					ilSetError(IL_INTERNAL_ERROR);
118 					return IL_FALSE;
119 			}
120 			break;
121 
122 		// These aren't supported right now.
123 		case IL_INT:
124 		case IL_UNSIGNED_INT:
125 		case IL_FLOAT:
126 		case IL_DOUBLE:
127 			ilSetError(IL_ILLEGAL_OPERATION);
128 			return IL_FALSE;
129 	}
130 
131 
132 	if (InProfile == NULL) {
133 		if (!iCurImage->Profile || !iCurImage->ProfileSize) {
134 			ilSetError(IL_INVALID_PARAM);
135 			return IL_FALSE;
136 		}
137 		hInProfile = iCurImage->Profile;
138 	}
139 	else {
140 #ifndef _UNICODE
141  		hInProfile = cmsOpenProfileFromFile(InProfile, "r");
142 #else
143 		wcstombs(AnsiName, InProfile, 512);
144 		hInProfile = cmsOpenProfileFromFile(AnsiName, "r");
145 #endif//_UNICODE
146 	}
147 #ifndef _UNICODE
148  	hOutProfile = cmsOpenProfileFromFile(OutProfile, "r");
149 #else
150 	wcstombs(AnsiName, OutProfile, 512);
151 	hOutProfile = cmsOpenProfileFromFile(AnsiName, "r");
152 #endif//_UNICODE
153 
154 	hTransform = cmsCreateTransform(hInProfile, Format, hOutProfile, Format, INTENT_PERCEPTUAL, 0);
155 
156 	Temp = (ILubyte*)ialloc(iCurImage->SizeOfData);
157 	if (Temp == NULL) {
158 		return IL_FALSE;
159 	}
160 
161 	cmsDoTransform(hTransform, iCurImage->Data, Temp, iCurImage->SizeOfData / 3);
162 
163 	ifree(iCurImage->Data);
164 	iCurImage->Data = Temp;
165 
166 	cmsDeleteTransform(hTransform);
167 	if (InProfile != NULL)
168 		cmsCloseProfile(hInProfile);
169 	cmsCloseProfile(hOutProfile);
170 
171 #endif//IL_NO_LCMS
172 
173 	return IL_TRUE;
174 }
175