_imagingcms.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644
  1. /*
  2. * pyCMS
  3. * a Python / PIL interface to the littleCMS ICC Color Management System
  4. * Copyright (C) 2002-2003 Kevin Cazabon
  5. * kevin@cazabon.com
  6. * http://www.cazabon.com
  7. * Adapted/reworked for PIL by Fredrik Lundh
  8. * Copyright (c) 2009 Fredrik Lundh
  9. * Updated to LCMS2
  10. * Copyright (c) 2013 Eric Soroos
  11. *
  12. * pyCMS home page: http://www.cazabon.com/pyCMS
  13. * littleCMS home page: http://www.littlecms.com
  14. * (littleCMS is Copyright (C) 1998-2001 Marti Maria)
  15. *
  16. * Originally released under LGPL. Graciously donated to PIL in
  17. * March 2009, for distribution under the standard PIL license
  18. */
  19. #define COPYRIGHTINFO "\
  20. pyCMS\n\
  21. a Python / PIL interface to the littleCMS ICC Color Management System\n\
  22. Copyright (C) 2002-2003 Kevin Cazabon\n\
  23. kevin@cazabon.com\n\
  24. http://www.cazabon.com\n\
  25. "
  26. #define PY_SSIZE_T_CLEAN
  27. #include "Python.h" // Include before wchar.h so _GNU_SOURCE is set
  28. #include "wchar.h"
  29. #include "datetime.h"
  30. #include "lcms2.h"
  31. #include "Imaging.h"
  32. #include "py3.h"
  33. #define PYCMSVERSION "1.0.0 pil"
  34. /* version history */
  35. /*
  36. 1.0.0 pil Integrating littleCMS2
  37. 0.1.0 pil integration & refactoring
  38. 0.0.2 alpha: Minor updates, added interfaces to littleCMS features, Jan 6, 2003
  39. - fixed some memory holes in how transforms/profiles were created and passed back to Python
  40. due to improper destructor setup for PyCObjects
  41. - added buildProofTransformFromOpenProfiles() function
  42. - eliminated some code redundancy, centralizing several common tasks with internal functions
  43. 0.0.1 alpha: First public release Dec 26, 2002
  44. */
  45. /* known to-do list with current version:
  46. Verify that PILmode->littleCMStype conversion in findLCMStype is correct for all
  47. PIL modes (it probably isn't for the more obscure ones)
  48. Add support for creating custom RGB profiles on the fly
  49. Add support for checking presence of a specific tag in a profile
  50. Add support for other littleCMS features as required
  51. */
  52. /*
  53. INTENT_PERCEPTUAL 0
  54. INTENT_RELATIVE_COLORIMETRIC 1
  55. INTENT_SATURATION 2
  56. INTENT_ABSOLUTE_COLORIMETRIC 3
  57. */
  58. /* -------------------------------------------------------------------- */
  59. /* wrapper classes */
  60. /* a profile represents the ICC characteristics for a specific device */
  61. typedef struct {
  62. PyObject_HEAD
  63. cmsHPROFILE profile;
  64. } CmsProfileObject;
  65. static PyTypeObject CmsProfile_Type;
  66. #define CmsProfile_Check(op) (Py_TYPE(op) == &CmsProfile_Type)
  67. static PyObject*
  68. cms_profile_new(cmsHPROFILE profile)
  69. {
  70. CmsProfileObject* self;
  71. self = PyObject_New(CmsProfileObject, &CmsProfile_Type);
  72. if (!self)
  73. return NULL;
  74. self->profile = profile;
  75. return (PyObject*) self;
  76. }
  77. static PyObject*
  78. cms_profile_open(PyObject* self, PyObject* args)
  79. {
  80. cmsHPROFILE hProfile;
  81. char* sProfile;
  82. if (!PyArg_ParseTuple(args, "s:profile_open", &sProfile))
  83. return NULL;
  84. hProfile = cmsOpenProfileFromFile(sProfile, "r");
  85. if (!hProfile) {
  86. PyErr_SetString(PyExc_IOError, "cannot open profile file");
  87. return NULL;
  88. }
  89. return cms_profile_new(hProfile);
  90. }
  91. static PyObject*
  92. cms_profile_fromstring(PyObject* self, PyObject* args)
  93. {
  94. cmsHPROFILE hProfile;
  95. char* pProfile;
  96. Py_ssize_t nProfile;
  97. #if PY_VERSION_HEX >= 0x03000000
  98. if (!PyArg_ParseTuple(args, "y#:profile_frombytes", &pProfile, &nProfile))
  99. return NULL;
  100. #else
  101. if (!PyArg_ParseTuple(args, "s#:profile_fromstring", &pProfile, &nProfile))
  102. return NULL;
  103. #endif
  104. hProfile = cmsOpenProfileFromMem(pProfile, nProfile);
  105. if (!hProfile) {
  106. PyErr_SetString(PyExc_IOError, "cannot open profile from string");
  107. return NULL;
  108. }
  109. return cms_profile_new(hProfile);
  110. }
  111. static PyObject*
  112. cms_profile_tobytes(PyObject* self, PyObject* args)
  113. {
  114. char *pProfile =NULL;
  115. cmsUInt32Number nProfile;
  116. PyObject* CmsProfile;
  117. cmsHPROFILE *profile;
  118. PyObject* ret;
  119. if (!PyArg_ParseTuple(args, "O", &CmsProfile)){
  120. return NULL;
  121. }
  122. profile = ((CmsProfileObject*)CmsProfile)->profile;
  123. if (!cmsSaveProfileToMem(profile, pProfile, &nProfile)) {
  124. PyErr_SetString(PyExc_IOError, "Could not determine profile size");
  125. return NULL;
  126. }
  127. pProfile = (char*)malloc(nProfile);
  128. if (!pProfile) {
  129. PyErr_SetString(PyExc_IOError, "Out of Memory");
  130. return NULL;
  131. }
  132. if (!cmsSaveProfileToMem(profile, pProfile, &nProfile)) {
  133. PyErr_SetString(PyExc_IOError, "Could not get profile");
  134. free(pProfile);
  135. return NULL;
  136. }
  137. #if PY_VERSION_HEX >= 0x03000000
  138. ret = PyBytes_FromStringAndSize(pProfile, (Py_ssize_t)nProfile);
  139. #else
  140. ret = PyString_FromStringAndSize(pProfile, (Py_ssize_t)nProfile);
  141. #endif
  142. free(pProfile);
  143. return ret;
  144. }
  145. static void
  146. cms_profile_dealloc(CmsProfileObject* self)
  147. {
  148. (void) cmsCloseProfile(self->profile);
  149. PyObject_Del(self);
  150. }
  151. /* a transform represents the mapping between two profiles */
  152. typedef struct {
  153. PyObject_HEAD
  154. char mode_in[8];
  155. char mode_out[8];
  156. cmsHTRANSFORM transform;
  157. } CmsTransformObject;
  158. static PyTypeObject CmsTransform_Type;
  159. #define CmsTransform_Check(op) (Py_TYPE(op) == &CmsTransform_Type)
  160. static PyObject*
  161. cms_transform_new(cmsHTRANSFORM transform, char* mode_in, char* mode_out)
  162. {
  163. CmsTransformObject* self;
  164. self = PyObject_New(CmsTransformObject, &CmsTransform_Type);
  165. if (!self)
  166. return NULL;
  167. self->transform = transform;
  168. strcpy(self->mode_in, mode_in);
  169. strcpy(self->mode_out, mode_out);
  170. return (PyObject*) self;
  171. }
  172. static void
  173. cms_transform_dealloc(CmsTransformObject* self)
  174. {
  175. cmsDeleteTransform(self->transform);
  176. PyObject_Del(self);
  177. }
  178. /* -------------------------------------------------------------------- */
  179. /* internal functions */
  180. static const char*
  181. findICmode(cmsColorSpaceSignature cs)
  182. {
  183. switch (cs) {
  184. case cmsSigXYZData: return "XYZ";
  185. case cmsSigLabData: return "LAB";
  186. case cmsSigLuvData: return "LUV";
  187. case cmsSigYCbCrData: return "YCbCr";
  188. case cmsSigYxyData: return "YXY";
  189. case cmsSigRgbData: return "RGB";
  190. case cmsSigGrayData: return "L";
  191. case cmsSigHsvData: return "HSV";
  192. case cmsSigHlsData: return "HLS";
  193. case cmsSigCmykData: return "CMYK";
  194. case cmsSigCmyData: return "CMY";
  195. default: return ""; /* other TBA */
  196. }
  197. }
  198. static cmsUInt32Number
  199. findLCMStype(char* PILmode)
  200. {
  201. if (strcmp(PILmode, "RGB") == 0) {
  202. return TYPE_RGBA_8;
  203. }
  204. else if (strcmp(PILmode, "RGBA") == 0) {
  205. return TYPE_RGBA_8;
  206. }
  207. else if (strcmp(PILmode, "RGBX") == 0) {
  208. return TYPE_RGBA_8;
  209. }
  210. else if (strcmp(PILmode, "RGBA;16B") == 0) {
  211. return TYPE_RGBA_16;
  212. }
  213. else if (strcmp(PILmode, "CMYK") == 0) {
  214. return TYPE_CMYK_8;
  215. }
  216. else if (strcmp(PILmode, "L") == 0) {
  217. return TYPE_GRAY_8;
  218. }
  219. else if (strcmp(PILmode, "L;16") == 0) {
  220. return TYPE_GRAY_16;
  221. }
  222. else if (strcmp(PILmode, "L;16B") == 0) {
  223. return TYPE_GRAY_16_SE;
  224. }
  225. else if (strcmp(PILmode, "YCCA") == 0) {
  226. return TYPE_YCbCr_8;
  227. }
  228. else if (strcmp(PILmode, "YCC") == 0) {
  229. return TYPE_YCbCr_8;
  230. }
  231. else if (strcmp(PILmode, "LAB") == 0) {
  232. // LabX equivalent like ALab, but not reversed -- no #define in lcms2
  233. return (COLORSPACE_SH(PT_LabV2)|CHANNELS_SH(3)|BYTES_SH(1)|EXTRA_SH(1));
  234. }
  235. else {
  236. /* take a wild guess... but you probably should fail instead. */
  237. return TYPE_GRAY_8; /* so there's no buffer overrun... */
  238. }
  239. }
  240. #define Cms_Min(a, b) ((a) < (b) ? (a) : (b))
  241. static int
  242. pyCMSgetAuxChannelChannel (cmsUInt32Number format, int auxChannelNdx)
  243. {
  244. int numColors = T_CHANNELS(format);
  245. int numExtras = T_EXTRA(format);
  246. if (T_SWAPFIRST(format) && T_DOSWAP(format)) {
  247. // reverse order, before anything but last extra is shifted last
  248. if (auxChannelNdx == numExtras - 1)
  249. return numColors + numExtras - 1;
  250. else
  251. return numExtras - 2 - auxChannelNdx;
  252. }
  253. else if (T_SWAPFIRST(format)) {
  254. // in order, after color channels, but last extra is shifted to first
  255. if (auxChannelNdx == numExtras - 1)
  256. return 0;
  257. else
  258. return numColors + 1 + auxChannelNdx;
  259. }
  260. else if (T_DOSWAP(format)) {
  261. // reverse order, before anything
  262. return numExtras - 1 - auxChannelNdx;
  263. }
  264. else {
  265. // in order, after color channels
  266. return numColors + auxChannelNdx;
  267. }
  268. }
  269. static void
  270. pyCMScopyAux (cmsHTRANSFORM hTransform, Imaging imDst, const Imaging imSrc)
  271. {
  272. cmsUInt32Number dstLCMSFormat;
  273. cmsUInt32Number srcLCMSFormat;
  274. int numSrcExtras;
  275. int numDstExtras;
  276. int numExtras;
  277. int ySize;
  278. int xSize;
  279. int channelSize;
  280. int srcChunkSize;
  281. int dstChunkSize;
  282. int e;
  283. // trivially copied
  284. if (imDst == imSrc)
  285. return;
  286. dstLCMSFormat = cmsGetTransformOutputFormat(hTransform);
  287. srcLCMSFormat = cmsGetTransformInputFormat(hTransform);
  288. // currently, all Pillow formats are chunky formats, but check it anyway
  289. if (T_PLANAR(dstLCMSFormat) || T_PLANAR(srcLCMSFormat))
  290. return;
  291. // copy only if channel format is identical, except OPTIMIZED is ignored as it
  292. // does not affect the aux channel
  293. if (T_FLOAT(dstLCMSFormat) != T_FLOAT(srcLCMSFormat)
  294. || T_FLAVOR(dstLCMSFormat) != T_FLAVOR(srcLCMSFormat)
  295. || T_ENDIAN16(dstLCMSFormat) != T_ENDIAN16(srcLCMSFormat)
  296. || T_BYTES(dstLCMSFormat) != T_BYTES(srcLCMSFormat))
  297. return;
  298. numSrcExtras = T_EXTRA(srcLCMSFormat);
  299. numDstExtras = T_EXTRA(dstLCMSFormat);
  300. numExtras = Cms_Min(numSrcExtras, numDstExtras);
  301. ySize = Cms_Min(imSrc->ysize, imDst->ysize);
  302. xSize = Cms_Min(imSrc->xsize, imDst->xsize);
  303. channelSize = T_BYTES(dstLCMSFormat);
  304. srcChunkSize = (T_CHANNELS(srcLCMSFormat) + T_EXTRA(srcLCMSFormat)) * channelSize;
  305. dstChunkSize = (T_CHANNELS(dstLCMSFormat) + T_EXTRA(dstLCMSFormat)) * channelSize;
  306. for (e = 0; e < numExtras; ++e) {
  307. int y;
  308. int dstChannel = pyCMSgetAuxChannelChannel(dstLCMSFormat, e);
  309. int srcChannel = pyCMSgetAuxChannelChannel(srcLCMSFormat, e);
  310. for (y = 0; y < ySize; y++) {
  311. int x;
  312. char* pDstExtras = imDst->image[y] + dstChannel * channelSize;
  313. const char* pSrcExtras = imSrc->image[y] + srcChannel * channelSize;
  314. for (x = 0; x < xSize; x++)
  315. memcpy(pDstExtras + x * dstChunkSize, pSrcExtras + x * srcChunkSize, channelSize);
  316. }
  317. }
  318. }
  319. static int
  320. pyCMSdoTransform(Imaging im, Imaging imOut, cmsHTRANSFORM hTransform)
  321. {
  322. int i;
  323. if (im->xsize > imOut->xsize || im->ysize > imOut->ysize)
  324. return -1;
  325. Py_BEGIN_ALLOW_THREADS
  326. // transform color channels only
  327. for (i = 0; i < im->ysize; i++)
  328. cmsDoTransform(hTransform, im->image[i], imOut->image[i], im->xsize);
  329. // lcms by default does nothing to the auxiliary channels leaving those
  330. // unchanged. To do "the right thing" here, i.e. maintain identical results
  331. // with and without inPlace, we replicate those channels to the output.
  332. //
  333. // As of lcms 2.8, a new cmsFLAGS_COPY_ALPHA flag is introduced which would
  334. // do the same thing automagically. Unfortunately, lcms2.8 is not yet widely
  335. // enough available on all platforms, so we polyfill it here for now.
  336. pyCMScopyAux(hTransform, imOut, im);
  337. Py_END_ALLOW_THREADS
  338. return 0;
  339. }
  340. static cmsHTRANSFORM
  341. _buildTransform(cmsHPROFILE hInputProfile, cmsHPROFILE hOutputProfile, char *sInMode, char *sOutMode, int iRenderingIntent, cmsUInt32Number cmsFLAGS)
  342. {
  343. cmsHTRANSFORM hTransform;
  344. Py_BEGIN_ALLOW_THREADS
  345. /* create the transform */
  346. hTransform = cmsCreateTransform(hInputProfile,
  347. findLCMStype(sInMode),
  348. hOutputProfile,
  349. findLCMStype(sOutMode),
  350. iRenderingIntent, cmsFLAGS);
  351. Py_END_ALLOW_THREADS
  352. if (!hTransform)
  353. PyErr_SetString(PyExc_ValueError, "cannot build transform");
  354. return hTransform; /* if NULL, an exception is set */
  355. }
  356. static cmsHTRANSFORM
  357. _buildProofTransform(cmsHPROFILE hInputProfile, cmsHPROFILE hOutputProfile, cmsHPROFILE hProofProfile, char *sInMode, char *sOutMode, int iRenderingIntent, int iProofIntent, cmsUInt32Number cmsFLAGS)
  358. {
  359. cmsHTRANSFORM hTransform;
  360. Py_BEGIN_ALLOW_THREADS
  361. /* create the transform */
  362. hTransform = cmsCreateProofingTransform(hInputProfile,
  363. findLCMStype(sInMode),
  364. hOutputProfile,
  365. findLCMStype(sOutMode),
  366. hProofProfile,
  367. iRenderingIntent,
  368. iProofIntent,
  369. cmsFLAGS);
  370. Py_END_ALLOW_THREADS
  371. if (!hTransform)
  372. PyErr_SetString(PyExc_ValueError, "cannot build proof transform");
  373. return hTransform; /* if NULL, an exception is set */
  374. }
  375. /* -------------------------------------------------------------------- */
  376. /* Python callable functions */
  377. static PyObject *
  378. buildTransform(PyObject *self, PyObject *args) {
  379. CmsProfileObject *pInputProfile;
  380. CmsProfileObject *pOutputProfile;
  381. char *sInMode;
  382. char *sOutMode;
  383. int iRenderingIntent = 0;
  384. int cmsFLAGS = 0;
  385. cmsHTRANSFORM transform = NULL;
  386. if (!PyArg_ParseTuple(args, "O!O!ss|ii:buildTransform", &CmsProfile_Type, &pInputProfile, &CmsProfile_Type, &pOutputProfile, &sInMode, &sOutMode, &iRenderingIntent, &cmsFLAGS))
  387. return NULL;
  388. transform = _buildTransform(pInputProfile->profile, pOutputProfile->profile, sInMode, sOutMode, iRenderingIntent, cmsFLAGS);
  389. if (!transform)
  390. return NULL;
  391. return cms_transform_new(transform, sInMode, sOutMode);
  392. }
  393. static PyObject *
  394. buildProofTransform(PyObject *self, PyObject *args)
  395. {
  396. CmsProfileObject *pInputProfile;
  397. CmsProfileObject *pOutputProfile;
  398. CmsProfileObject *pProofProfile;
  399. char *sInMode;
  400. char *sOutMode;
  401. int iRenderingIntent = 0;
  402. int iProofIntent = 0;
  403. int cmsFLAGS = 0;
  404. cmsHTRANSFORM transform = NULL;
  405. if (!PyArg_ParseTuple(args, "O!O!O!ss|iii:buildProofTransform", &CmsProfile_Type, &pInputProfile, &CmsProfile_Type, &pOutputProfile, &CmsProfile_Type, &pProofProfile, &sInMode, &sOutMode, &iRenderingIntent, &iProofIntent, &cmsFLAGS))
  406. return NULL;
  407. transform = _buildProofTransform(pInputProfile->profile, pOutputProfile->profile, pProofProfile->profile, sInMode, sOutMode, iRenderingIntent, iProofIntent, cmsFLAGS);
  408. if (!transform)
  409. return NULL;
  410. return cms_transform_new(transform, sInMode, sOutMode);
  411. }
  412. static PyObject *
  413. cms_transform_apply(CmsTransformObject *self, PyObject *args)
  414. {
  415. Py_ssize_t idIn;
  416. Py_ssize_t idOut;
  417. Imaging im;
  418. Imaging imOut;
  419. int result;
  420. if (!PyArg_ParseTuple(args, "nn:apply", &idIn, &idOut))
  421. return NULL;
  422. im = (Imaging) idIn;
  423. imOut = (Imaging) idOut;
  424. result = pyCMSdoTransform(im, imOut, self->transform);
  425. return Py_BuildValue("i", result);
  426. }
  427. /* -------------------------------------------------------------------- */
  428. /* Python-Callable On-The-Fly profile creation functions */
  429. static PyObject *
  430. createProfile(PyObject *self, PyObject *args)
  431. {
  432. char *sColorSpace;
  433. cmsHPROFILE hProfile;
  434. cmsFloat64Number dColorTemp = 0.0;
  435. cmsCIExyY whitePoint;
  436. cmsBool result;
  437. if (!PyArg_ParseTuple(args, "s|d:createProfile", &sColorSpace, &dColorTemp))
  438. return NULL;
  439. if (strcmp(sColorSpace, "LAB") == 0) {
  440. if (dColorTemp > 0.0) {
  441. result = cmsWhitePointFromTemp(&whitePoint, dColorTemp);
  442. if (!result) {
  443. PyErr_SetString(PyExc_ValueError, "ERROR: Could not calculate white point from color temperature provided, must be float in degrees Kelvin");
  444. return NULL;
  445. }
  446. hProfile = cmsCreateLab2Profile(&whitePoint);
  447. } else {
  448. hProfile = cmsCreateLab2Profile(NULL);
  449. }
  450. }
  451. else if (strcmp(sColorSpace, "XYZ") == 0) {
  452. hProfile = cmsCreateXYZProfile();
  453. }
  454. else if (strcmp(sColorSpace, "sRGB") == 0) {
  455. hProfile = cmsCreate_sRGBProfile();
  456. }
  457. else {
  458. hProfile = NULL;
  459. }
  460. if (!hProfile) {
  461. PyErr_SetString(PyExc_ValueError, "failed to create requested color space");
  462. return NULL;
  463. }
  464. return cms_profile_new(hProfile);
  465. }
  466. /* -------------------------------------------------------------------- */
  467. /* profile methods */
  468. static PyObject *
  469. cms_profile_is_intent_supported(CmsProfileObject *self, PyObject *args)
  470. {
  471. cmsBool result;
  472. int intent;
  473. int direction;
  474. if (!PyArg_ParseTuple(args, "ii:is_intent_supported", &intent, &direction))
  475. return NULL;
  476. result = cmsIsIntentSupported(self->profile, intent, direction);
  477. /* printf("cmsIsIntentSupported(%p, %d, %d) => %d\n", self->profile, intent, direction, result); */
  478. return PyInt_FromLong(result != 0);
  479. }
  480. #ifdef _WIN32
  481. #ifdef _WIN64
  482. #define F_HANDLE "K"
  483. #else
  484. #define F_HANDLE "k"
  485. #endif
  486. static PyObject *
  487. cms_get_display_profile_win32(PyObject* self, PyObject* args)
  488. {
  489. char filename[MAX_PATH];
  490. cmsUInt32Number filename_size;
  491. BOOL ok;
  492. HANDLE handle = 0;
  493. int is_dc = 0;
  494. if (!PyArg_ParseTuple(args, "|" F_HANDLE "i:get_display_profile", &handle, &is_dc))
  495. return NULL;
  496. filename_size = sizeof(filename);
  497. if (is_dc) {
  498. ok = GetICMProfile((HDC) handle, &filename_size, filename);
  499. } else {
  500. HDC dc = GetDC((HWND) handle);
  501. ok = GetICMProfile(dc, &filename_size, filename);
  502. ReleaseDC((HWND) handle, dc);
  503. }
  504. if (ok)
  505. return PyUnicode_FromStringAndSize(filename, filename_size-1);
  506. Py_INCREF(Py_None);
  507. return Py_None;
  508. }
  509. #endif
  510. /* -------------------------------------------------------------------- */
  511. /* Helper functions. */
  512. static PyObject*
  513. _profile_read_mlu(CmsProfileObject* self, cmsTagSignature info)
  514. {
  515. PyObject *uni;
  516. char *lc = "en";
  517. char *cc = cmsNoCountry;
  518. cmsMLU *mlu;
  519. cmsUInt32Number len;
  520. wchar_t *buf;
  521. if (!cmsIsTag(self->profile, info)) {
  522. Py_INCREF(Py_None);
  523. return Py_None;
  524. }
  525. mlu = cmsReadTag(self->profile, info);
  526. if (!mlu) {
  527. Py_INCREF(Py_None);
  528. return Py_None;
  529. }
  530. len = cmsMLUgetWide(mlu, lc, cc, NULL, 0);
  531. if (len == 0) {
  532. Py_INCREF(Py_None);
  533. return Py_None;
  534. }
  535. buf = malloc(len);
  536. if (!buf) {
  537. PyErr_SetString(PyExc_IOError, "Out of Memory");
  538. return NULL;
  539. }
  540. /* Just in case the next call fails. */
  541. buf[0] = '\0';
  542. cmsMLUgetWide(mlu, lc, cc, buf, len);
  543. // buf contains additional junk after \0
  544. uni = PyUnicode_FromWideChar(buf, wcslen(buf));
  545. free(buf);
  546. return uni;
  547. }
  548. static PyObject*
  549. _profile_read_int_as_string(cmsUInt32Number nr)
  550. {
  551. PyObject* ret;
  552. char buf[5];
  553. buf[0] = (char) ((nr >> 24) & 0xff);
  554. buf[1] = (char) ((nr >> 16) & 0xff);
  555. buf[2] = (char) ((nr >> 8) & 0xff);
  556. buf[3] = (char) (nr & 0xff);
  557. buf[4] = 0;
  558. #if PY_VERSION_HEX >= 0x03000000
  559. ret = PyUnicode_DecodeASCII(buf, 4, NULL);
  560. #else
  561. ret = PyString_FromStringAndSize(buf, 4);
  562. #endif
  563. return ret;
  564. }
  565. static PyObject*
  566. _profile_read_signature(CmsProfileObject* self, cmsTagSignature info)
  567. {
  568. unsigned int *sig;
  569. if (!cmsIsTag(self->profile, info)) {
  570. Py_INCREF(Py_None);
  571. return Py_None;
  572. }
  573. sig = (unsigned int *) cmsReadTag(self->profile, info);
  574. if (!sig) {
  575. Py_INCREF(Py_None);
  576. return Py_None;
  577. }
  578. return _profile_read_int_as_string(*sig);
  579. }
  580. static PyObject*
  581. _xyz_py(cmsCIEXYZ* XYZ)
  582. {
  583. cmsCIExyY xyY;
  584. cmsXYZ2xyY(&xyY, XYZ);
  585. return Py_BuildValue("((d,d,d),(d,d,d))", XYZ->X, XYZ->Y, XYZ->Z, xyY.x, xyY.y, xyY.Y);
  586. }
  587. static PyObject*
  588. _xyz3_py(cmsCIEXYZ* XYZ)
  589. {
  590. cmsCIExyY xyY[3];
  591. cmsXYZ2xyY(&xyY[0], &XYZ[0]);
  592. cmsXYZ2xyY(&xyY[1], &XYZ[1]);
  593. cmsXYZ2xyY(&xyY[2], &XYZ[2]);
  594. return Py_BuildValue("(((d,d,d),(d,d,d),(d,d,d)),((d,d,d),(d,d,d),(d,d,d)))",
  595. XYZ[0].X, XYZ[0].Y, XYZ[0].Z,
  596. XYZ[1].X, XYZ[1].Y, XYZ[1].Z,
  597. XYZ[2].X, XYZ[2].Y, XYZ[2].Z,
  598. xyY[0].x, xyY[0].y, xyY[0].Y,
  599. xyY[1].x, xyY[1].y, xyY[1].Y,
  600. xyY[2].x, xyY[2].y, xyY[2].Y);
  601. }
  602. static PyObject*
  603. _profile_read_ciexyz(CmsProfileObject* self, cmsTagSignature info, int multi)
  604. {
  605. cmsCIEXYZ* XYZ;
  606. if (!cmsIsTag(self->profile, info)) {
  607. Py_INCREF(Py_None);
  608. return Py_None;
  609. }
  610. XYZ = (cmsCIEXYZ*) cmsReadTag(self->profile, info);
  611. if (!XYZ) {
  612. Py_INCREF(Py_None);
  613. return Py_None;
  614. }
  615. if (multi)
  616. return _xyz3_py(XYZ);
  617. else
  618. return _xyz_py(XYZ);
  619. }
  620. static PyObject*
  621. _profile_read_ciexyy_triple(CmsProfileObject* self, cmsTagSignature info)
  622. {
  623. cmsCIExyYTRIPLE* triple;
  624. if (!cmsIsTag(self->profile, info)) {
  625. Py_INCREF(Py_None);
  626. return Py_None;
  627. }
  628. triple = (cmsCIExyYTRIPLE*) cmsReadTag(self->profile, info);
  629. if (!triple) {
  630. Py_INCREF(Py_None);
  631. return Py_None;
  632. }
  633. /* Note: lcms does all the heavy lifting and error checking (nr of
  634. channels == 3). */
  635. return Py_BuildValue("((d,d,d),(d,d,d),(d,d,d)),",
  636. triple->Red.x, triple->Red.y, triple->Red.Y,
  637. triple->Green.x, triple->Green.y, triple->Green.Y,
  638. triple->Blue.x, triple->Blue.y, triple->Blue.Y);
  639. }
  640. static PyObject*
  641. _profile_read_named_color_list(CmsProfileObject* self, cmsTagSignature info)
  642. {
  643. cmsNAMEDCOLORLIST* ncl;
  644. int i, n;
  645. char name[cmsMAX_PATH];
  646. PyObject* result;
  647. if (!cmsIsTag(self->profile, info)) {
  648. Py_INCREF(Py_None);
  649. return Py_None;
  650. }
  651. ncl = (cmsNAMEDCOLORLIST*) cmsReadTag(self->profile, info);
  652. if (ncl == NULL) {
  653. Py_INCREF(Py_None);
  654. return Py_None;
  655. }
  656. n = cmsNamedColorCount(ncl);
  657. result = PyList_New(n);
  658. if (!result) {
  659. Py_INCREF(Py_None);
  660. return Py_None;
  661. }
  662. for (i = 0; i < n; i++) {
  663. PyObject* str;
  664. cmsNamedColorInfo(ncl, i, name, NULL, NULL, NULL, NULL);
  665. str = PyUnicode_FromString(name);
  666. if (str == NULL) {
  667. Py_DECREF(result);
  668. Py_INCREF(Py_None);
  669. return Py_None;
  670. }
  671. PyList_SET_ITEM(result, i, str);
  672. }
  673. return result;
  674. }
  675. static cmsBool _calculate_rgb_primaries(CmsProfileObject* self, cmsCIEXYZTRIPLE* result)
  676. {
  677. double input[3][3] = { { 1, 0, 0 }, { 0, 1, 0 }, { 0, 0, 1 } };
  678. cmsHPROFILE hXYZ;
  679. cmsHTRANSFORM hTransform;
  680. /* https://littlecms2.blogspot.com/2009/07/less-is-more.html */
  681. // double array of RGB values with max on each identity
  682. hXYZ = cmsCreateXYZProfile();
  683. if (hXYZ == NULL)
  684. return 0;
  685. // transform from our profile to XYZ using doubles for highest precision
  686. hTransform = cmsCreateTransform(self->profile, TYPE_RGB_DBL,
  687. hXYZ, TYPE_XYZ_DBL,
  688. INTENT_RELATIVE_COLORIMETRIC,
  689. cmsFLAGS_NOCACHE | cmsFLAGS_NOOPTIMIZE);
  690. cmsCloseProfile(hXYZ);
  691. if (hTransform == NULL)
  692. return 0;
  693. cmsDoTransform(hTransform, (void*) input, result, 3);
  694. cmsDeleteTransform(hTransform);
  695. return 1;
  696. }
  697. static cmsBool _check_intent(int clut, cmsHPROFILE hProfile, cmsUInt32Number Intent, cmsUInt32Number UsedDirection)
  698. {
  699. if (clut) {
  700. return cmsIsCLUT(hProfile, Intent, UsedDirection);
  701. }
  702. else {
  703. return cmsIsIntentSupported(hProfile, Intent, UsedDirection);
  704. }
  705. }
  706. #define INTENTS 200
  707. static PyObject*
  708. _is_intent_supported(CmsProfileObject* self, int clut)
  709. {
  710. PyObject* result;
  711. int n;
  712. int i;
  713. cmsUInt32Number intent_ids[INTENTS];
  714. char *intent_descs[INTENTS];
  715. result = PyDict_New();
  716. if (result == NULL) {
  717. Py_INCREF(Py_None);
  718. return Py_None;
  719. }
  720. n = cmsGetSupportedIntents(INTENTS,
  721. intent_ids,
  722. intent_descs);
  723. for (i = 0; i < n; i++) {
  724. int intent = (int) intent_ids[i];
  725. PyObject* id;
  726. PyObject* entry;
  727. /* Only valid for ICC Intents (otherwise we read invalid memory in lcms cmsio1.c). */
  728. if (!(intent == INTENT_PERCEPTUAL || intent == INTENT_RELATIVE_COLORIMETRIC
  729. || intent == INTENT_SATURATION || intent == INTENT_ABSOLUTE_COLORIMETRIC))
  730. continue;
  731. id = PyInt_FromLong((long) intent);
  732. entry = Py_BuildValue("(OOO)",
  733. _check_intent(clut, self->profile, intent, LCMS_USED_AS_INPUT) ? Py_True : Py_False,
  734. _check_intent(clut, self->profile, intent, LCMS_USED_AS_OUTPUT) ? Py_True : Py_False,
  735. _check_intent(clut, self->profile, intent, LCMS_USED_AS_PROOF) ? Py_True : Py_False);
  736. if (id == NULL || entry == NULL) {
  737. Py_XDECREF(id);
  738. Py_XDECREF(entry);
  739. Py_XDECREF(result);
  740. Py_INCREF(Py_None);
  741. return Py_None;
  742. }
  743. PyDict_SetItem(result, id, entry);
  744. }
  745. return result;
  746. }
  747. /* -------------------------------------------------------------------- */
  748. /* Python interface setup */
  749. static PyMethodDef pyCMSdll_methods[] = {
  750. {"profile_open", cms_profile_open, 1},
  751. {"profile_frombytes", cms_profile_fromstring, 1},
  752. {"profile_fromstring", cms_profile_fromstring, 1},
  753. {"profile_tobytes", cms_profile_tobytes, 1},
  754. /* profile and transform functions */
  755. {"buildTransform", buildTransform, 1},
  756. {"buildProofTransform", buildProofTransform, 1},
  757. {"createProfile", createProfile, 1},
  758. /* platform specific tools */
  759. #ifdef _WIN32
  760. {"get_display_profile_win32", cms_get_display_profile_win32, 1},
  761. #endif
  762. {NULL, NULL}
  763. };
  764. static struct PyMethodDef cms_profile_methods[] = {
  765. {"is_intent_supported", (PyCFunction) cms_profile_is_intent_supported, 1},
  766. {NULL, NULL} /* sentinel */
  767. };
  768. static PyObject*
  769. _profile_getattr(CmsProfileObject* self, cmsInfoType field)
  770. {
  771. // UNDONE -- check that I'm getting the right fields on these.
  772. // return PyUnicode_DecodeFSDefault(cmsTakeProductName(self->profile));
  773. //wchar_t buf[256]; -- UNDONE need wchar_t for unicode version.
  774. char buf[256];
  775. cmsUInt32Number written;
  776. written = cmsGetProfileInfoASCII(self->profile,
  777. field,
  778. "en",
  779. "us",
  780. buf,
  781. 256);
  782. if (written) {
  783. return PyUnicode_FromString(buf);
  784. }
  785. // UNDONE suppressing error here by sending back blank string.
  786. return PyUnicode_FromString("");
  787. }
  788. static PyObject*
  789. cms_profile_getattr_product_desc(CmsProfileObject* self, void* closure)
  790. {
  791. PyErr_WarnEx(PyExc_DeprecationWarning,
  792. "product_desc is deprecated. Use Unicode profile_description instead.", 1);
  793. // description was Description != 'Copyright' || or "%s - %s" (manufacturer, model) in 1.x
  794. return _profile_getattr(self, cmsInfoDescription);
  795. }
  796. /* use these four for the individual fields.
  797. */
  798. static PyObject*
  799. cms_profile_getattr_product_description(CmsProfileObject* self, void* closure)
  800. {
  801. PyErr_WarnEx(PyExc_DeprecationWarning,
  802. "product_description is deprecated. Use Unicode profile_description instead.", 1);
  803. return _profile_getattr(self, cmsInfoDescription);
  804. }
  805. static PyObject*
  806. cms_profile_getattr_product_model(CmsProfileObject* self, void* closure)
  807. {
  808. PyErr_WarnEx(PyExc_DeprecationWarning,
  809. "product_model is deprecated. Use Unicode model instead.", 1);
  810. return _profile_getattr(self, cmsInfoModel);
  811. }
  812. static PyObject*
  813. cms_profile_getattr_product_manufacturer(CmsProfileObject* self, void* closure)
  814. {
  815. PyErr_WarnEx(PyExc_DeprecationWarning,
  816. "product_manufacturer is deprecated. Use Unicode manufacturer instead.", 1);
  817. return _profile_getattr(self, cmsInfoManufacturer);
  818. }
  819. static PyObject*
  820. cms_profile_getattr_product_copyright(CmsProfileObject* self, void* closure)
  821. {
  822. PyErr_WarnEx(PyExc_DeprecationWarning,
  823. "product_copyright is deprecated. Use Unicode copyright instead.", 1);
  824. return _profile_getattr(self, cmsInfoCopyright);
  825. }
  826. static PyObject*
  827. cms_profile_getattr_rendering_intent(CmsProfileObject* self, void* closure)
  828. {
  829. return PyInt_FromLong(cmsGetHeaderRenderingIntent(self->profile));
  830. }
  831. static PyObject*
  832. cms_profile_getattr_pcs(CmsProfileObject* self, void* closure)
  833. {
  834. PyErr_WarnEx(PyExc_DeprecationWarning,
  835. "pcs is deprecated. Use padded connection_space instead.", 1);
  836. return PyUnicode_DecodeFSDefault(findICmode(cmsGetPCS(self->profile)));
  837. }
  838. static PyObject*
  839. cms_profile_getattr_color_space(CmsProfileObject* self, void* closure)
  840. {
  841. PyErr_WarnEx(PyExc_DeprecationWarning,
  842. "color_space is deprecated. Use padded xcolor_space instead.", 1);
  843. return PyUnicode_DecodeFSDefault(findICmode(cmsGetColorSpace(self->profile)));
  844. }
  845. /* New-style unicode interfaces. */
  846. static PyObject*
  847. cms_profile_getattr_copyright(CmsProfileObject* self, void* closure)
  848. {
  849. return _profile_read_mlu(self, cmsSigCopyrightTag);
  850. }
  851. static PyObject*
  852. cms_profile_getattr_target(CmsProfileObject* self, void* closure)
  853. {
  854. return _profile_read_mlu(self, cmsSigCharTargetTag);
  855. }
  856. static PyObject*
  857. cms_profile_getattr_manufacturer(CmsProfileObject* self, void* closure)
  858. {
  859. return _profile_read_mlu(self, cmsSigDeviceMfgDescTag);
  860. }
  861. static PyObject*
  862. cms_profile_getattr_model(CmsProfileObject* self, void* closure)
  863. {
  864. return _profile_read_mlu(self, cmsSigDeviceModelDescTag);
  865. }
  866. static PyObject*
  867. cms_profile_getattr_profile_description(CmsProfileObject* self, void* closure)
  868. {
  869. return _profile_read_mlu(self, cmsSigProfileDescriptionTag);
  870. }
  871. static PyObject*
  872. cms_profile_getattr_screening_description(CmsProfileObject* self, void* closure)
  873. {
  874. return _profile_read_mlu(self, cmsSigScreeningDescTag);
  875. }
  876. static PyObject*
  877. cms_profile_getattr_viewing_condition(CmsProfileObject* self, void* closure)
  878. {
  879. return _profile_read_mlu(self, cmsSigViewingCondDescTag);
  880. }
  881. static PyObject*
  882. cms_profile_getattr_creation_date(CmsProfileObject* self, void* closure)
  883. {
  884. cmsBool result;
  885. struct tm ct;
  886. result = cmsGetHeaderCreationDateTime(self->profile, &ct);
  887. if (! result) {
  888. Py_INCREF(Py_None);
  889. return Py_None;
  890. }
  891. return PyDateTime_FromDateAndTime(1900 + ct.tm_year, ct.tm_mon, ct.tm_mday,
  892. ct.tm_hour, ct.tm_min, ct.tm_sec, 0);
  893. }
  894. static PyObject*
  895. cms_profile_getattr_version(CmsProfileObject* self, void* closure)
  896. {
  897. cmsFloat64Number version = cmsGetProfileVersion(self->profile);
  898. return PyFloat_FromDouble(version);
  899. }
  900. static PyObject*
  901. cms_profile_getattr_icc_version(CmsProfileObject* self, void* closure)
  902. {
  903. return PyInt_FromLong((long) cmsGetEncodedICCversion(self->profile));
  904. }
  905. static PyObject*
  906. cms_profile_getattr_attributes(CmsProfileObject* self, void* closure)
  907. {
  908. cmsUInt64Number attr;
  909. cmsGetHeaderAttributes(self->profile, &attr);
  910. /* This works just as well on Windows (LLP64), 32-bit Linux
  911. (ILP32) and 64-bit Linux (LP64) systems. */
  912. return PyLong_FromUnsignedLongLong((unsigned long long) attr);
  913. }
  914. static PyObject*
  915. cms_profile_getattr_header_flags(CmsProfileObject* self, void* closure)
  916. {
  917. cmsUInt32Number flags = cmsGetHeaderFlags(self->profile);
  918. return PyInt_FromLong(flags);
  919. }
  920. static PyObject*
  921. cms_profile_getattr_header_manufacturer(CmsProfileObject* self, void* closure)
  922. {
  923. return _profile_read_int_as_string(cmsGetHeaderManufacturer(self->profile));
  924. }
  925. static PyObject*
  926. cms_profile_getattr_header_model(CmsProfileObject* self, void* closure)
  927. {
  928. return _profile_read_int_as_string(cmsGetHeaderModel(self->profile));
  929. }
  930. static PyObject*
  931. cms_profile_getattr_device_class(CmsProfileObject* self, void* closure)
  932. {
  933. return _profile_read_int_as_string(cmsGetDeviceClass(self->profile));
  934. }
  935. /* Duplicate of pcs, but uninterpreted. */
  936. static PyObject*
  937. cms_profile_getattr_connection_space(CmsProfileObject* self, void* closure)
  938. {
  939. return _profile_read_int_as_string(cmsGetPCS(self->profile));
  940. }
  941. /* Duplicate of color_space, but uninterpreted. */
  942. static PyObject*
  943. cms_profile_getattr_xcolor_space(CmsProfileObject* self, void* closure)
  944. {
  945. return _profile_read_int_as_string(cmsGetColorSpace(self->profile));
  946. }
  947. static PyObject*
  948. cms_profile_getattr_profile_id(CmsProfileObject* self, void* closure)
  949. {
  950. cmsUInt8Number id[16];
  951. cmsGetHeaderProfileID(self->profile, id);
  952. return PyBytes_FromStringAndSize((char *) id, 16);
  953. }
  954. static PyObject*
  955. cms_profile_getattr_is_matrix_shaper(CmsProfileObject* self, void* closure)
  956. {
  957. return PyBool_FromLong((long) cmsIsMatrixShaper(self->profile));
  958. }
  959. static PyObject*
  960. cms_profile_getattr_technology(CmsProfileObject* self, void* closure)
  961. {
  962. return _profile_read_signature(self, cmsSigTechnologyTag);
  963. }
  964. static PyObject*
  965. cms_profile_getattr_colorimetric_intent(CmsProfileObject* self, void* closure)
  966. {
  967. return _profile_read_signature(self, cmsSigColorimetricIntentImageStateTag);
  968. }
  969. static PyObject*
  970. cms_profile_getattr_perceptual_rendering_intent_gamut(CmsProfileObject* self, void* closure)
  971. {
  972. return _profile_read_signature(self, cmsSigPerceptualRenderingIntentGamutTag);
  973. }
  974. static PyObject*
  975. cms_profile_getattr_saturation_rendering_intent_gamut(CmsProfileObject* self, void* closure)
  976. {
  977. return _profile_read_signature(self, cmsSigSaturationRenderingIntentGamutTag);
  978. }
  979. static PyObject*
  980. cms_profile_getattr_red_colorant(CmsProfileObject* self, void* closure)
  981. {
  982. if (!cmsIsMatrixShaper(self->profile)) {
  983. Py_INCREF(Py_None);
  984. return Py_None;
  985. }
  986. return _profile_read_ciexyz(self, cmsSigRedColorantTag, 0);
  987. }
  988. static PyObject*
  989. cms_profile_getattr_green_colorant(CmsProfileObject* self, void* closure)
  990. {
  991. if (!cmsIsMatrixShaper(self->profile)) {
  992. Py_INCREF(Py_None);
  993. return Py_None;
  994. }
  995. return _profile_read_ciexyz(self, cmsSigGreenColorantTag, 0);
  996. }
  997. static PyObject*
  998. cms_profile_getattr_blue_colorant(CmsProfileObject* self, void* closure)
  999. {
  1000. if (!cmsIsMatrixShaper(self->profile)) {
  1001. Py_INCREF(Py_None);
  1002. return Py_None;
  1003. }
  1004. return _profile_read_ciexyz(self, cmsSigBlueColorantTag, 0);
  1005. }
  1006. static PyObject*
  1007. cms_profile_getattr_media_white_point_temperature(CmsProfileObject *self, void* closure)
  1008. {
  1009. cmsCIEXYZ* XYZ;
  1010. cmsCIExyY xyY;
  1011. cmsFloat64Number tempK;
  1012. cmsTagSignature info = cmsSigMediaWhitePointTag;
  1013. cmsBool result;
  1014. if (!cmsIsTag(self->profile, info)) {
  1015. Py_INCREF(Py_None);
  1016. return Py_None;
  1017. }
  1018. XYZ = (cmsCIEXYZ*) cmsReadTag(self->profile, info);
  1019. if (!XYZ) {
  1020. Py_INCREF(Py_None);
  1021. return Py_None;
  1022. }
  1023. if (XYZ == NULL || XYZ->X == 0) {
  1024. Py_INCREF(Py_None);
  1025. return Py_None;
  1026. }
  1027. cmsXYZ2xyY(&xyY, XYZ);
  1028. result = cmsTempFromWhitePoint(&tempK, &xyY);
  1029. if (!result) {
  1030. Py_INCREF(Py_None);
  1031. return Py_None;
  1032. }
  1033. return PyFloat_FromDouble(tempK);
  1034. }
  1035. static PyObject*
  1036. cms_profile_getattr_media_white_point(CmsProfileObject* self, void* closure)
  1037. {
  1038. return _profile_read_ciexyz(self, cmsSigMediaWhitePointTag, 0);
  1039. }
  1040. static PyObject*
  1041. cms_profile_getattr_media_black_point(CmsProfileObject* self, void* closure)
  1042. {
  1043. return _profile_read_ciexyz(self, cmsSigMediaBlackPointTag, 0);
  1044. }
  1045. static PyObject*
  1046. cms_profile_getattr_luminance(CmsProfileObject* self, void* closure)
  1047. {
  1048. return _profile_read_ciexyz(self, cmsSigLuminanceTag, 0);
  1049. }
  1050. static PyObject*
  1051. cms_profile_getattr_chromatic_adaptation(CmsProfileObject* self, void* closure)
  1052. {
  1053. return _profile_read_ciexyz(self, cmsSigChromaticAdaptationTag, 1);
  1054. }
  1055. static PyObject*
  1056. cms_profile_getattr_chromaticity(CmsProfileObject* self, void* closure)
  1057. {
  1058. return _profile_read_ciexyy_triple(self, cmsSigChromaticityTag);
  1059. }
  1060. static PyObject*
  1061. cms_profile_getattr_red_primary(CmsProfileObject* self, void* closure)
  1062. {
  1063. cmsBool result = 0;
  1064. cmsCIEXYZTRIPLE primaries;
  1065. if (cmsIsMatrixShaper(self->profile))
  1066. result = _calculate_rgb_primaries(self, &primaries);
  1067. if (! result) {
  1068. Py_INCREF(Py_None);
  1069. return Py_None;
  1070. }
  1071. return _xyz_py(&primaries.Red);
  1072. }
  1073. static PyObject*
  1074. cms_profile_getattr_green_primary(CmsProfileObject* self, void* closure)
  1075. {
  1076. cmsBool result = 0;
  1077. cmsCIEXYZTRIPLE primaries;
  1078. if (cmsIsMatrixShaper(self->profile))
  1079. result = _calculate_rgb_primaries(self, &primaries);
  1080. if (! result) {
  1081. Py_INCREF(Py_None);
  1082. return Py_None;
  1083. }
  1084. return _xyz_py(&primaries.Green);
  1085. }
  1086. static PyObject*
  1087. cms_profile_getattr_blue_primary(CmsProfileObject* self, void* closure)
  1088. {
  1089. cmsBool result = 0;
  1090. cmsCIEXYZTRIPLE primaries;
  1091. if (cmsIsMatrixShaper(self->profile))
  1092. result = _calculate_rgb_primaries(self, &primaries);
  1093. if (! result) {
  1094. Py_INCREF(Py_None);
  1095. return Py_None;
  1096. }
  1097. return _xyz_py(&primaries.Blue);
  1098. }
  1099. static PyObject*
  1100. cms_profile_getattr_colorant_table(CmsProfileObject* self, void* closure)
  1101. {
  1102. return _profile_read_named_color_list(self, cmsSigColorantTableTag);
  1103. }
  1104. static PyObject*
  1105. cms_profile_getattr_colorant_table_out(CmsProfileObject* self, void* closure)
  1106. {
  1107. return _profile_read_named_color_list(self, cmsSigColorantTableOutTag);
  1108. }
  1109. static PyObject*
  1110. cms_profile_getattr_is_intent_supported (CmsProfileObject* self, void* closure)
  1111. {
  1112. return _is_intent_supported(self, 0);
  1113. }
  1114. static PyObject*
  1115. cms_profile_getattr_is_clut (CmsProfileObject* self, void* closure)
  1116. {
  1117. return _is_intent_supported(self, 1);
  1118. }
  1119. static const char*
  1120. _illu_map(int i)
  1121. {
  1122. switch(i) {
  1123. case 0:
  1124. return "unknown";
  1125. case 1:
  1126. return "D50";
  1127. case 2:
  1128. return "D65";
  1129. case 3:
  1130. return "D93";
  1131. case 4:
  1132. return "F2";
  1133. case 5:
  1134. return "D55";
  1135. case 6:
  1136. return "A";
  1137. case 7:
  1138. return "E";
  1139. case 8:
  1140. return "F8";
  1141. default:
  1142. return NULL;
  1143. }
  1144. }
  1145. static PyObject*
  1146. cms_profile_getattr_icc_measurement_condition (CmsProfileObject* self, void* closure)
  1147. {
  1148. cmsICCMeasurementConditions* mc;
  1149. cmsTagSignature info = cmsSigMeasurementTag;
  1150. const char *geo;
  1151. if (!cmsIsTag(self->profile, info)) {
  1152. Py_INCREF(Py_None);
  1153. return Py_None;
  1154. }
  1155. mc = (cmsICCMeasurementConditions*) cmsReadTag(self->profile, info);
  1156. if (!mc) {
  1157. Py_INCREF(Py_None);
  1158. return Py_None;
  1159. }
  1160. if (mc->Geometry == 1)
  1161. geo = "45/0, 0/45";
  1162. else if (mc->Geometry == 2)
  1163. geo = "0d, d/0";
  1164. else
  1165. geo = "unknown";
  1166. return Py_BuildValue("{s:i,s:(ddd),s:s,s:d,s:s}",
  1167. "observer", mc->Observer,
  1168. "backing", mc->Backing.X, mc->Backing.Y, mc->Backing.Z,
  1169. "geo", geo,
  1170. "flare", mc->Flare,
  1171. "illuminant_type", _illu_map(mc->IlluminantType));
  1172. }
  1173. static PyObject*
  1174. cms_profile_getattr_icc_viewing_condition (CmsProfileObject* self, void* closure)
  1175. {
  1176. cmsICCViewingConditions* vc;
  1177. cmsTagSignature info = cmsSigViewingConditionsTag;
  1178. if (!cmsIsTag(self->profile, info)) {
  1179. Py_INCREF(Py_None);
  1180. return Py_None;
  1181. }
  1182. vc = (cmsICCViewingConditions*) cmsReadTag(self->profile, info);
  1183. if (!vc) {
  1184. Py_INCREF(Py_None);
  1185. return Py_None;
  1186. }
  1187. return Py_BuildValue("{s:(ddd),s:(ddd),s:s}",
  1188. "illuminant", vc->IlluminantXYZ.X, vc->IlluminantXYZ.Y, vc->IlluminantXYZ.Z,
  1189. "surround", vc->SurroundXYZ.X, vc->SurroundXYZ.Y, vc->SurroundXYZ.Z,
  1190. "illuminant_type", _illu_map(vc->IlluminantType));
  1191. }
  1192. static struct PyGetSetDef cms_profile_getsetters[] = {
  1193. /* Compatibility interfaces. */
  1194. { "product_desc", (getter) cms_profile_getattr_product_desc },
  1195. { "product_description", (getter) cms_profile_getattr_product_description },
  1196. { "product_manufacturer", (getter) cms_profile_getattr_product_manufacturer },
  1197. { "product_model", (getter) cms_profile_getattr_product_model },
  1198. { "product_copyright", (getter) cms_profile_getattr_product_copyright },
  1199. { "pcs", (getter) cms_profile_getattr_pcs },
  1200. { "color_space", (getter) cms_profile_getattr_color_space },
  1201. /* New style interfaces. */
  1202. { "rendering_intent", (getter) cms_profile_getattr_rendering_intent },
  1203. { "creation_date", (getter) cms_profile_getattr_creation_date },
  1204. { "copyright", (getter) cms_profile_getattr_copyright },
  1205. { "target", (getter) cms_profile_getattr_target },
  1206. { "manufacturer", (getter) cms_profile_getattr_manufacturer },
  1207. { "model", (getter) cms_profile_getattr_model },
  1208. { "profile_description", (getter) cms_profile_getattr_profile_description },
  1209. { "screening_description", (getter) cms_profile_getattr_screening_description },
  1210. { "viewing_condition", (getter) cms_profile_getattr_viewing_condition },
  1211. { "version", (getter) cms_profile_getattr_version },
  1212. { "icc_version", (getter) cms_profile_getattr_icc_version },
  1213. { "attributes", (getter) cms_profile_getattr_attributes },
  1214. { "header_flags", (getter) cms_profile_getattr_header_flags },
  1215. { "header_manufacturer", (getter) cms_profile_getattr_header_manufacturer },
  1216. { "header_model", (getter) cms_profile_getattr_header_model },
  1217. { "device_class", (getter) cms_profile_getattr_device_class },
  1218. { "connection_space", (getter) cms_profile_getattr_connection_space },
  1219. /* Similar to color_space, but with full 4-letter signature (including trailing whitespace). */
  1220. { "xcolor_space", (getter) cms_profile_getattr_xcolor_space },
  1221. { "profile_id", (getter) cms_profile_getattr_profile_id },
  1222. { "is_matrix_shaper", (getter) cms_profile_getattr_is_matrix_shaper },
  1223. { "technology", (getter) cms_profile_getattr_technology },
  1224. { "colorimetric_intent", (getter) cms_profile_getattr_colorimetric_intent },
  1225. { "perceptual_rendering_intent_gamut", (getter) cms_profile_getattr_perceptual_rendering_intent_gamut },
  1226. { "saturation_rendering_intent_gamut", (getter) cms_profile_getattr_saturation_rendering_intent_gamut },
  1227. { "red_colorant", (getter) cms_profile_getattr_red_colorant },
  1228. { "green_colorant", (getter) cms_profile_getattr_green_colorant },
  1229. { "blue_colorant", (getter) cms_profile_getattr_blue_colorant },
  1230. { "red_primary", (getter) cms_profile_getattr_red_primary },
  1231. { "green_primary", (getter) cms_profile_getattr_green_primary },
  1232. { "blue_primary", (getter) cms_profile_getattr_blue_primary },
  1233. { "media_white_point_temperature", (getter) cms_profile_getattr_media_white_point_temperature },
  1234. { "media_white_point", (getter) cms_profile_getattr_media_white_point },
  1235. { "media_black_point", (getter) cms_profile_getattr_media_black_point },
  1236. { "luminance", (getter) cms_profile_getattr_luminance },
  1237. { "chromatic_adaptation", (getter) cms_profile_getattr_chromatic_adaptation },
  1238. { "chromaticity", (getter) cms_profile_getattr_chromaticity },
  1239. { "colorant_table", (getter) cms_profile_getattr_colorant_table },
  1240. { "colorant_table_out", (getter) cms_profile_getattr_colorant_table_out },
  1241. { "intent_supported", (getter) cms_profile_getattr_is_intent_supported },
  1242. { "clut", (getter) cms_profile_getattr_is_clut },
  1243. { "icc_measurement_condition", (getter) cms_profile_getattr_icc_measurement_condition },
  1244. { "icc_viewing_condition", (getter) cms_profile_getattr_icc_viewing_condition },
  1245. { NULL }
  1246. };
  1247. static PyTypeObject CmsProfile_Type = {
  1248. PyVarObject_HEAD_INIT(NULL, 0)
  1249. "PIL._imagingcms.CmsProfile", /*tp_name */
  1250. sizeof(CmsProfileObject), 0,/*tp_basicsize, tp_itemsize */
  1251. /* methods */
  1252. (destructor) cms_profile_dealloc, /*tp_dealloc*/
  1253. 0, /*tp_print*/
  1254. 0, /*tp_getattr*/
  1255. 0, /*tp_setattr*/
  1256. 0, /*tp_compare*/
  1257. 0, /*tp_repr*/
  1258. 0, /*tp_as_number */
  1259. 0, /*tp_as_sequence */
  1260. 0, /*tp_as_mapping */
  1261. 0, /*tp_hash*/
  1262. 0, /*tp_call*/
  1263. 0, /*tp_str*/
  1264. 0, /*tp_getattro*/
  1265. 0, /*tp_setattro*/
  1266. 0, /*tp_as_buffer*/
  1267. Py_TPFLAGS_DEFAULT, /*tp_flags*/
  1268. 0, /*tp_doc*/
  1269. 0, /*tp_traverse*/
  1270. 0, /*tp_clear*/
  1271. 0, /*tp_richcompare*/
  1272. 0, /*tp_weaklistoffset*/
  1273. 0, /*tp_iter*/
  1274. 0, /*tp_iternext*/
  1275. cms_profile_methods, /*tp_methods*/
  1276. 0, /*tp_members*/
  1277. cms_profile_getsetters, /*tp_getset*/
  1278. };
  1279. static struct PyMethodDef cms_transform_methods[] = {
  1280. {"apply", (PyCFunction) cms_transform_apply, 1},
  1281. {NULL, NULL} /* sentinel */
  1282. };
  1283. static PyObject*
  1284. cms_transform_getattr_inputMode(CmsTransformObject* self, void* closure)
  1285. {
  1286. return PyUnicode_FromString(self->mode_in);
  1287. }
  1288. static PyObject*
  1289. cms_transform_getattr_outputMode(CmsTransformObject* self, void* closure)
  1290. {
  1291. return PyUnicode_FromString(self->mode_out);
  1292. }
  1293. static struct PyGetSetDef cms_transform_getsetters[] = {
  1294. { "inputMode", (getter) cms_transform_getattr_inputMode },
  1295. { "outputMode", (getter) cms_transform_getattr_outputMode },
  1296. { NULL }
  1297. };
  1298. static PyTypeObject CmsTransform_Type = {
  1299. PyVarObject_HEAD_INIT(NULL, 0)
  1300. "CmsTransform", sizeof(CmsTransformObject), 0,
  1301. /* methods */
  1302. (destructor) cms_transform_dealloc, /*tp_dealloc*/
  1303. 0, /*tp_print*/
  1304. 0, /*tp_getattr*/
  1305. 0, /*tp_setattr*/
  1306. 0, /*tp_compare*/
  1307. 0, /*tp_repr*/
  1308. 0, /*tp_as_number */
  1309. 0, /*tp_as_sequence */
  1310. 0, /*tp_as_mapping */
  1311. 0, /*tp_hash*/
  1312. 0, /*tp_call*/
  1313. 0, /*tp_str*/
  1314. 0, /*tp_getattro*/
  1315. 0, /*tp_setattro*/
  1316. 0, /*tp_as_buffer*/
  1317. Py_TPFLAGS_DEFAULT, /*tp_flags*/
  1318. 0, /*tp_doc*/
  1319. 0, /*tp_traverse*/
  1320. 0, /*tp_clear*/
  1321. 0, /*tp_richcompare*/
  1322. 0, /*tp_weaklistoffset*/
  1323. 0, /*tp_iter*/
  1324. 0, /*tp_iternext*/
  1325. cms_transform_methods, /*tp_methods*/
  1326. 0, /*tp_members*/
  1327. cms_transform_getsetters, /*tp_getset*/
  1328. };
  1329. static int
  1330. setup_module(PyObject* m) {
  1331. PyObject *d;
  1332. PyObject *v;
  1333. d = PyModule_GetDict(m);
  1334. CmsProfile_Type.tp_new = PyType_GenericNew;
  1335. /* Ready object types */
  1336. PyType_Ready(&CmsProfile_Type);
  1337. PyType_Ready(&CmsTransform_Type);
  1338. Py_INCREF(&CmsProfile_Type);
  1339. PyModule_AddObject(m, "CmsProfile", (PyObject *)&CmsProfile_Type);
  1340. d = PyModule_GetDict(m);
  1341. v = PyUnicode_FromFormat("%d.%d", LCMS_VERSION / 100, LCMS_VERSION % 100);
  1342. PyDict_SetItemString(d, "littlecms_version", v);
  1343. return 0;
  1344. }
  1345. #if PY_VERSION_HEX >= 0x03000000
  1346. PyMODINIT_FUNC
  1347. PyInit__imagingcms(void) {
  1348. PyObject* m;
  1349. static PyModuleDef module_def = {
  1350. PyModuleDef_HEAD_INIT,
  1351. "_imagingcms", /* m_name */
  1352. NULL, /* m_doc */
  1353. -1, /* m_size */
  1354. pyCMSdll_methods, /* m_methods */
  1355. };
  1356. m = PyModule_Create(&module_def);
  1357. if (setup_module(m) < 0)
  1358. return NULL;
  1359. PyDateTime_IMPORT;
  1360. return m;
  1361. }
  1362. #else
  1363. PyMODINIT_FUNC
  1364. init_imagingcms(void)
  1365. {
  1366. PyObject *m = Py_InitModule("_imagingcms", pyCMSdll_methods);
  1367. setup_module(m);
  1368. PyDateTime_IMPORT;
  1369. }
  1370. #endif