_imagingcms.c 44 KB

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