From 97ab31ce3a681b3ef3384a0e166ba2779558395e Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Sun, 11 Jun 2017 01:04:18 +0200 Subject: Add common.h MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- Makefile | 3 +- common.h | 487 +++++++++++++++++++++++++++++++++++++++++++++++++++ convert-template.c | 399 +++++------------------------------------ double.c | 1 + en_masse-template.c | 17 -- float.c | 1 + libcolour-template.c | 58 +----- long-double.c | 1 + 8 files changed, 540 insertions(+), 427 deletions(-) create mode 100644 common.h diff --git a/Makefile b/Makefile index 5dd9023..91fde95 100644 --- a/Makefile +++ b/Makefile @@ -11,7 +11,8 @@ OBJ =\ long-double.o HDR =\ - libcolour.h + libcolour.h\ + common.h TEMPLATES =\ convert-template.c\ diff --git a/common.h b/common.h new file mode 100644 index 0000000..5c781b6 --- /dev/null +++ b/common.h @@ -0,0 +1,487 @@ +/* See LICENSE file for copyright and license details. */ + +#if defined(__GNUC__) && !defined(__clang__) +# pragma GCC diagnostic ignored "-Wfloat-equal" +#elif defined(__clang__) +# pragma clang diagnostic ignored "-Wcomma" +# pragma clang diagnostic ignored "-Wfloat-equal" +# pragma clang diagnostic ignored "-Wvla" +# pragma clang diagnostic ignored "-Wtautological-compare" +# pragma clang diagnostic ignored "-Wcovered-switch-default" +# pragma clang diagnostic ignored "-Wfloat-conversion" +# pragma clang diagnostic ignored "-Wconversion" +# pragma clang diagnostic ignored "-Wdouble-promotion" +# pragma clang diagnostic ignored "-Wswitch-enum" +# pragma clang diagnostic ignored "-Wcast-align" +#endif + + + +#define PI D(3.14159265358979323846) +#define PI2 (2 * D(3.14159265358979323846)) +#define WASDIV0(X) (xisinf(X) || xisnan(X)) + +#define REGULAR(S, T) ((T) <= (S)->TRANSITION ? (S)->SLOPE * (T) : (1 + (S)->OFFSET) * xpow((T), 1 / (S)->GAMMA) - (S)->OFFSET) +#define INVREGULAR(S, T) ((T) <= (S)->TRANSITIONINV ? (T) / (S)->SLOPE : xpow(((T) + (S)->OFFSET) / (1 + (S)->OFFSET), (S)->GAMMA)) + +#define XTRANSFORM(X, Y, Z, A, B, C, R1C1, R1C2, R1C3, R2C1, R2C2, R2C3, R3C1, R3C2, R3C3)\ + do {\ + TYPE a__ = (A), b__ = (B), c__ = (C);\ + (X) = (R1C1) * a__ + (R1C2) * b__ + (R1C3) * c__;\ + (Y) = (R2C1) * a__ + (R2C2) * b__ + (R2C3) * c__;\ + (Z) = (R3C1) * a__ + (R3C2) * b__ + (R3C3) * c__;\ + } while (0) + +#define TRANSFORM(X, Y, Z, A, B, C, R1C1, R1C2, R1C3, R2C1, R2C2, R2C3, R3C1, R3C2, R3C3)\ + XTRANSFORM((X), (Y), (Z), (A), (B), (C),\ + D(R1C1), D(R1C2), D(R1C3),\ + D(R2C1), D(R2C2), D(R2C3),\ + D(R3C1), D(R3C2), D(R3C3)) + + + +#define SLOPE transfer.regular.slope +#define TRANSITIONINV transfer.regular.transitioninv +#define TRANSITION transfer.regular.transition +#define GAMMA transfer.regular.gamma +#define OFFSET transfer.regular.offset +#define TO_ENCODED_RED transfer.custom.to_encoded_red +#define TO_DECODED_RED transfer.custom.to_decoded_red +#define TO_ENCODED_GREEN transfer.custom.to_encoded_green +#define TO_DECODED_GREEN transfer.custom.to_decoded_green +#define TO_ENCODED_BLUE transfer.custom.to_encoded_blue +#define TO_DECODED_BLUE transfer.custom.to_decoded_blue + + + +#define CIEXYZ_TO_RGB(FROM_X, FROM_Y, FROM_Z, TO_R, TO_G, TO_B, TO_MINV)\ + XTRANSFORM((TO_R), (TO_G), (TO_B), (FROM_X), (FROM_Y), (FROM_Z),\ + (TO_MINV)[0][0], (TO_MINV)[0][1], (TO_MINV)[0][2],\ + (TO_MINV)[1][0], (TO_MINV)[1][1], (TO_MINV)[1][2],\ + (TO_MINV)[2][0], (TO_MINV)[2][1], (TO_MINV)[2][2]) + +#define CIEXYZ_TO_SRGB(FROM_X, FROM_Y, FROM_Z, TO_R, TO_G, TO_B)\ + TRANSFORM((TO_R), (TO_G), (TO_B), (FROM_X), (FROM_Y), (FROM_Z),\ + 3.240446254647737500675930277794, -1.537134761820080575134284117667, -0.498530193022728718155178739835,\ + -0.969266606244679751469561779231, 1.876011959788370209167851498933, 0.041556042214430065351304932619,\ + 0.055643503564352832235773149705, -0.204026179735960239147729566866, 1.057226567722703292062647051353) + +#define YIQ_TO_SRGB(FROM_Y, FROM_I, FROM_Q, TO_R, TO_G, TO_B)\ + TRANSFORM((TO_R), (TO_G), (TO_B), (FROM_Y), (FROM_I), (FROM_Q),\ + 1., 0.95629483232089407263032398986979387700557708740234, 0.62102512544472865396727456754888407886028289794922,\ + 1., -0.27212147408397735492968649850809015333652496337891, -0.64738095351761570928061928498209454119205474853516,\ + 1., -1.10698990856712820018969978264067322015762329101562, 1.70461497549882934343656870623817667365074157714844) + /* ⎛1 1000 cos 33° / 877 1000 sin 33° / 877 ⎞ + ⎜1 9500 sin 33° / 24067 - 299000 * cos 33° / 514799 -9500 cos 33° / 24067 - 299000 * sin 33° / 514799⎟ + ⎝1 -250 sin 33° / 123 250 cos 33° / 123 ⎠ + */ + +#define YDBDR_TO_SRGB(FROM_Y, FROM_DB, FROM_DR, TO_R, TO_G, TO_B)\ + TRANSFORM((TO_R), (TO_G), (TO_B), (FROM_Y), (FROM_DB), (FROM_DR),\ + 1., 0.000092303716148, -0.525912630661865,\ + 1., -0.129132898890509, 0.267899328207599,\ + 1., 0.664679059978955, -0.000079202543533) + +#define YPBPR_TO_SRGB(FROM_Y, FROM_PB, FROM_PR, TO_R, TO_G, TO_B)\ + do {\ + TYPE y__ = (FROM_Y);\ + TYPE r__ = (FROM_PR) + y__;\ + TYPE b__ = (FROM_PB) + y__;\ + (TO_R) = r__;\ + (TO_B) = b__;\ + (TO_G) = (y__ - r__ * D(0.2126) - b__ * D(0.0722)) / D(0.7152);\ + } while (0) + +#define YUV_TO_SRGB(FROM_Y, FROM_U, FROM_V, TO_R, TO_G, TO_B)\ + TRANSFORM((TO_R), (TO_G), (TO_B), (FROM_Y), (FROM_U), (FROM_V),\ + 1., 0.00028328010485821202317155420580263580632163211703, 1.14070449590558520291949662350816652178764343261719,\ + 1., -0.39630886669497211727275498560629785060882568359375, -0.58107364288228224857846271333983168005943298339844,\ + 1., 2.03990003507541306504435851820744574069976806640625, 0.00017179031692307700847528739718228507626918144524) + +#define YCGCO_TO_SRGB(FROM_Y, FROM_CG, FROM_CO, TO_R, TO_G, TO_B)\ + TRANSFORM((TO_R), (TO_G), (TO_B), (FROM_Y), (FROM_CG), (FROM_CO),\ + 1., -1., 1.,\ + 1., 1., 0.,\ + 1., -1., -1.) + +#define CIEXYZ_TO_CIEXYY(FROM_X, FROM_Y, FROM_Z, TO_X, TO_Y, TO_YY)\ + do {\ + TYPE x__ = (FROM_X), y__ = (FROM_Y), z__ = (FROM_Z);\ + TYPE s__ = x__ + y__ + z__;\ + x__ = x__ / s__;\ + s__ = y__ / s__;\ + if (WASDIV0(x__) || WASDIV0(s__))\ + x__ = s__ = 0;\ + (TO_X) = x__;\ + (TO_Y) = s__;\ + (TO_YY) = y__;\ + } while (0) + +#define RGB_TO_CIEXYZ(FROM_R, FROM_G, FROM_B, TO_X, TO_Y, TO_Z, FROM_M)\ + XTRANSFORM((TO_X), (TO_Y), (TO_Z), (FROM_R), (FROM_G), (FROM_B),\ + (FROM_M)[0][0], (FROM_M)[0][1], (FROM_M)[0][2],\ + (FROM_M)[1][0], (FROM_M)[1][1], (FROM_M)[1][2],\ + (FROM_M)[2][0], (FROM_M)[2][1], (FROM_M)[2][2]) + +#define SRGB_TO_CIEXYZ(FROM_R, FROM_G, FROM_B, TO_X, TO_Y, TO_Z)\ + TRANSFORM((TO_X), (TO_Y), (TO_Z), (FROM_R), (FROM_G), (FROM_B),\ + 0.412457445582367576708548995157, 0.357575865245515878143578447634, 0.180437247826399665973085006954,\ + 0.212673370378408277403536885686, 0.715151730491031756287156895269, 0.072174899130559869164791564344,\ + 0.019333942761673460208893260415, 0.119191955081838593666354597644, 0.950302838552371742508739771438) + +#define CIEXYY_TO_CIEXYZ(FROM_X, FROM_Y, FROM_YY, TO_X, TO_Y, TO_Z)\ + do {\ + TYPE x__ = (FROM_X), y__ = (FROM_Y), Y__ = (FROM_YY);\ + TYPE Yy__ = Y__ / y__;\ + if (WASDIV0(Yy__)) {\ + (TO_X) = (TO_Y) = (TO_Z) = Y__;\ + } else {\ + (TO_X) = x__ * Yy__;\ + (TO_Y) = Y__;\ + (TO_Z) = (1 - x__ - y__) * Yy__;\ + }\ + } while (0) + +#define CIELAB_TO_CIEXYZ(FROM_L, FROM_A, FROM_B, TO_X, TO_Y, TO_Z)\ + do {\ + TYPE Y__ = ((FROM_L) + 16) / 116;\ + TYPE X__ = Y__ + (FROM_A) / 500;\ + TYPE Z__ = Y__ - (FROM_B) / 200;\ + (TO_X) = cielab_finv(X__) * D(0.95047);\ + (TO_Y) = cielab_finv(Y__);\ + (TO_Z) = cielab_finv(Z__) * D(1.08883);\ + } while (0) + +#define CIELUV_TO_CIEXYZ(FROM_L, FROM_U, FROM_V, TO_X, TO_Y, TO_Z, FROM_WHITE_X, FROM_WHITE_Y, FROM_WHITE_Z)\ + do {\ + TYPE L__ = (FROM_L), X__ = (FROM_WHITE_X), Y__ = (FROM_WHITE_Y), Z__ = (FROM_WHITE_Z);\ + TYPE L13__ = L__ * 13;\ + TYPE t__ = X__ + 15 * Y__ + 3 * Z__;\ + TYPE u__ = (FROM_U) / L13__ + 4 * X__ / t__;\ + TYPE v__ = (FROM_V) / L13__ + 9 * Y__ / t__;\ + if (L__ <= 8) {\ + Y__ *= L__ * 27 / 24389;\ + } else {\ + L__ = (L__ + 16) / 116;\ + Y__ *= L__ * L__ * L__;\ + }\ + (TO_X) = D(2.25) * Y__ * u__ / v__;\ + (TO_Y) = Y__;\ + (TO_Z) = Y__ * (3 / v__ - D(0.75) * u__ / v__ - 5);\ + } while (0) + +#define YIQ_TO_CIEXYZ(FROM_Y, FROM_I, FROM_Q, TO_X, TO_Y, TO_Z)\ + TRANSFORM((TO_X), (TO_Y), (TO_Z), (FROM_Y), (FROM_I), (FROM_Q),\ + 0.95047055865428309306963683411595411598682403564453,\ + 0.09738463974178063198294807989441324025392532348633,\ + 0.33223466706854809515903070860076695680618286132812,\ + 0.99999999999999988897769753748434595763683319091797,\ + -0.07112658301916767455974621725545148365199565887451,\ + -0.20786968876398304040264974901219829916954040527344,\ + 1.08882873639588373393394249433185905218124389648438,\ + -1.06592139332461721679123911599162966012954711914062,\ + 1.55474471255181900808395312196807935833930969238281) + +#define YDBDR_TO_CIEXYZ(FROM_Y, FROM_DB, FROM_DR, TO_X, TO_Y, TO_Z)\ + TRANSFORM((TO_X), (TO_Y), (TO_Z), (FROM_Y), (FROM_DB), (FROM_DR),\ + 0.95047055865428309306963683411595411598682403564453,\ + 0.07379612357298576119646327242662664502859115600586,\ + -0.12113653724874726136384595065464964136481285095215,\ + 0.99999999999999988897769753748434595763683319091797,\ + -0.04435684145428285540813106990754022262990474700928,\ + 0.07973534004202506575431641522300196811556816101074,\ + 1.08882873639588373393394249433185905218124389648438,\ + 0.61625657933494271123464613992837257683277130126953,\ + 0.02168821359337728266192257819966471288353204727173) + +#define YUV_TO_CIEXYZ(FROM_Y, FROM_U, FROM_V, TO_X, TO_Y, TO_Z)\ + TRANSFORM((TO_X), (TO_Y), (TO_Z), (FROM_Y), (FROM_U), (FROM_V),\ + 0.95047055865428309306963683411595411598682403564453,\ + 0.22648030324549334180161963558930438011884689331055,\ + 0.26274514929253273143316960158699657768011093139648,\ + 0.99999999999999988897769753748434595763683319091797,\ + -0.13613114642319409930415474718756740912795066833496,\ + -0.17294595255115238763288232348713790997862815856934,\ + 1.08882873639588373393394249433185905218124389648438,\ + 1.89129144197893928058817891724174842238426208496094,\ + -0.04704173528403532422714761196402832865715026855469) + +#define YPBPR_TO_CIEXYZ(FROM_Y, FROM_PB, FROM_PR, TO_X, TO_Y, TO_Z)\ + TRANSFORM((TO_X), (TO_Y), (TO_Z), (FROM_Y), (FROM_PB), (FROM_PR),\ + 0.95047055865428298204733437160030007362365722656250,\ + 0.14433968424876231217979238863335922360420227050781,\ + 0.30616461986760712399302519770571961998939514160156,\ + 0.99999999999999977795539507496869191527366638183594,\ + -0.00002022802471486617736928792510298080742359161377,\ + 0.00008771894888734421691367515450110659003257751465,\ + 1.08882873639588373393394249433185905218124389648438,\ + 0.93827031735982591165168287261622026562690734863281,\ + -0.01609699914324668607035206946420657914131879806519) + +#define YCGCO_TO_CIEXYZ(FROM_Y, FROM_CG, FROM_CO, TO_X, TO_Y, TO_Z)\ + TRANSFORM((TO_X), (TO_Y), (TO_Z), (FROM_Y), (FROM_CG), (FROM_CO),\ + 0.95047055865428309306963683411595411598682403564453,\ + -0.23531882816325136453805555447615915909409523010254,\ + 0.23202019775596791073546398820326430723071098327637,\ + 0.99999999999999988897769753748434595763683319091797,\ + 0.43030346098206362359661625305307097733020782470703,\ + 0.14049847124784842211653312915586866438388824462891,\ + 1.08882873639588373393394249433185905218124389648438,\ + -0.85044482623220662986796014592982828617095947265625,\ + -0.93096889579069830311652822274481877684593200683594) + +#define CIE1960UCS_TO_CIEXYZ(FROM_U, FROM_V, FROM_Y, TO_X, TO_Y, TO_Z)\ + do {\ + TYPE u__ = (FROM_U), v__ = (FROM_V), Y__ = (FROM_Y);\ + (TO_X) = D(1.5) * Y__ * u__ / v__;\ + (TO_Y) = Y__;\ + (TO_Z) = (4 * Y__ - Y__ * u__ - 10 * Y__ * v__) / (2 * v__);\ + } while (0) + +#define CIEXYZ_TO_CIELAB(FROM_X, FROM_Y, FROM_Z, TO_L, TO_A, TO_B)\ + do {\ + TYPE X__ = (FROM_X), Y__ = (FROM_Y), Z__ = (FROM_Z);\ + X__ /= D(0.95047);\ + Z__ /= D(1.08883);\ + Y__ = cielab_f(Y__);\ + (TO_L) = 116 * Y__ - 16;\ + (TO_A) = 500 * (cielab_f(X__) - Y__);\ + (TO_B) = 200 * (Y__ - cielab_f(Z__));\ + } while (0) + +#define CIEXYZ_TO_CIELUV(FROM_X, FROM_Y, FROM_Z, TO_L, TO_U, TO_V, TO_WHITE_X, TO_WHITE_Y, TO_WHITE_Z)\ + do {\ + TYPE WX__ = (TO_WHITE_X), WY__ = (TO_WHITE_Y), WZ__ = (TO_WHITE_Z);\ + TYPE X__ = (FROM_X), Y__ = (FROM_Y), Z__ = (FROM_Z);\ + TYPE L2__, L__, u__, v__, t__;\ + t__ = WX__ + 15 * WY__ + 3 * WZ__;\ + u__ = 4 * WX__ / t__;\ + v__ = 9 * WY__ / t__;\ + t__ = X__ + 15 * Y__ + 3 * Z__;\ + u__ = 4 * X__ / t__ - u__;\ + v__ = 9 * Y__ / t__ - v__;\ + L__ = Y__ / WY__;\ + L2__ = L__ * 24389;\ + L__ = L2__ <= 216 ? L2__ / 27 : xcbrt(L__) * 116 - 16;\ + (TO_L) = L__;\ + L__ *= 13;\ + (TO_U) = u__ * L__;\ + (TO_V) = v__ * L__;\ + } while (0) + +#define CIELCHUV_TO_CIELUV(FROM_L, FROM_C, FROM_H, TO_L, TO_U, TO_V)\ + do {\ + TYPE C__ = (FROM_C), h__ = (FROM_H);\ + (TO_L) = (FROM_L);\ + (TO_U) = C__ * xcos(h__);\ + (TO_V) = C__ * xsin(h__);\ + } while (0) + +#define CIELUV_TO_CIELCHUV(FROM_L, FROM_U, FROM_V, TO_L, TO_C, TO_H, TO_ONE_REVOLUTION)\ + do {\ + TYPE u__ = (FROM_U), v__ = (FROM_V), rev__ = (TO_ONE_REVOLUTION), h__;\ + (TO_L) = (FROM_L);\ + (TO_C) = xsqrt(u__ * u__ + v__ * v__);\ + h__ = xatan2(v__, u__) / PI2 * rev__;\ + if (!WASDIV0(h__) && (h__ < 0))\ + h__ += rev__;\ + (TO_H) = h__;\ + } while (0) + +#define SRGB_TO_YIQ(FROM_R, FROM_G, FROM_B, TO_Y, TO_I, TO_Q)\ + TRANSFORM((TO_Y), (TO_I), (TO_Q), (FROM_R), (FROM_G), (FROM_B),\ + 0.299, 0.587, 0.114,\ + 0.59571613491277464191853141528554260730743408203125, /* (0.877 cos 33°)(1 - 0.299) - (0.492 sin 33°)(-0.299) */\ + -0.27445283783925644716106262421817518770694732666016, /* (0.877 cos 33°)(-0.587) - (0.492 sin 33°)(-0.587) */\ + -0.32126329707351808373516632855171337723731994628906, /* (0.877 cos 33°)(-0.114) - (0.492 sin 33°)(1 - 0.114) */\ + 0.21145640212011795888713550084503367543220520019531, /* (0.877 sin 33°)(1 - 0.299) + (0.492 cos 33°)(-0.299) */\ + -0.52259104529161115593183239980135113000869750976562, /* (0.877 sin 33°)(-0.587) + (0.492 cos 33°)(-0.587) */\ + 0.31113464317149330806699936147197149693965911865234) /* (0.877 sin 33°)(-0.114) + (0.492 cos 33°)(1 - 0.114) */ + +#define CIEXYZ_TO_YIQ(FROM_X, FROM_Y, FROM_Z, TO_Y, TO_I, TO_Q)\ + TRANSFORM((TO_Y), (TO_I), (TO_Q), (FROM_X), (FROM_Y), (FROM_Z),\ + 0.40627729168038273499519164033699780702590942382812,\ + 0.61835674212166968910509012857801280915737152099609,\ + -0.00414330221353725880462093300593551248311996459961,\ + 2.17852787350219845308174626552499830722808837890625,\ + -1.36502666214454104753883711964590474963188171386719,\ + -0.64803574634025240541745915834326297044754028320312,\ + 1.20905577682138853923277110880007967352867126464844,\ + -1.36890364998339797431015085749095305800437927246094,\ + 0.20180559439597040016778350945969577878713607788086) + +#define SRGB_TO_YDBDR(FROM_R, FROM_G, FROM_B, TO_Y, TO_DB, TO_DR)\ + TRANSFORM((TO_Y), (TO_DB), (TO_DR), (FROM_R), (FROM_G), (FROM_B),\ + 0.299, 0.587, 0.114,\ + -0.450, -0.883, 1.333,\ + -1.333, 1.116, 0.217) + +#define YUV_TO_YDBDR(FROM_Y, FROM_U, FROM_V, TO_Y, TO_DB, TO_DR)\ + do {\ + (TO_Y) = (FROM_Y);\ + (TO_DB) = (FROM_U) * D(3.069);\ + (TO_DR) = (FROM_V) * D(-2.169);\ + } while (0) + +#define CIEXYZ_TO_YDBDR(FROM_X, FROM_Y, FROM_Z, TO_Y, TO_DB, TO_DR)\ + TRANSFORM((TO_Y), (TO_DB), (TO_DR), (FROM_X), (FROM_Y), (FROM_Z),\ + 0.40627729168038273499519164033699780702590942382812,\ + 0.61835674212166968910509012857801280915737152099609,\ + -0.00414330221353725880462093300593551248311996459961,\ + -0.52816561102614745237815441214479506015777587890625,\ + -1.23677481526212962315014465275453403592109680175781,\ + 1.59692761635924940222253098909277468919754028320312,\ + -5.38914174974103143966885909321717917919158935546875,\ + 4.09835630362728497999569299281574785709381103515625,\ + 0.94033545560642795013706063400604762136936187744141) + +#define YDBDR_TO_YUV(FROM_Y, FROM_DB, FROM_DR, TO_Y, TO_U, TO_V)\ + do {\ + (TO_Y) = (FROM_Y);\ + (TO_U) = (FROM_DB) / D(3.069);\ + (TO_V) = (FROM_DR) / D(-2.169);\ + } while (0) + +#define SRGB_TO_YUV(FROM_R, FROM_G, FROM_B, TO_Y, TO_U, TO_V)\ + TRANSFORM((TO_Y), (TO_U), (TO_V), (FROM_R), (FROM_G), (FROM_B),\ + 0.29899999999999998800959133404830936342477798461914,\ + 0.58699999999999996624922005139524117112159729003906,\ + 0.11400000000000000410782519111307919956743717193604,\ + -0.14662756598240470062854967636667424812912940979004,\ + -0.28771586836102963635752871596196200698614120483398,\ + 0.43434343434343436474165400795754976570606231689453,\ + 0.61456892577224520035628074765554629266262054443359,\ + -0.51452282157676354490405401520547457039356231689453,\ + -0.10004610419548178035231700278018251992762088775635) + +#define CIEXYZ_TO_YUV(FROM_X, FROM_Y, FROM_Z, TO_Y, TO_U, TO_V)\ + TRANSFORM((TO_Y), (TO_U), (TO_V), (FROM_X), (FROM_Y), (FROM_Z),\ + 0.40627729168038273499519164033699780702590942382812,\ + 0.61835674212166968910509012857801280915737152099609,\ + -0.00414330221353725880462093300593551248311996459961,\ + -0.17209697328971895746718701047939248383045196533203,\ + -0.40298951295605395239718404809536878019571304321289,\ + 0.52034135430408912093014350830344483256340026855469,\ + 2.48462044709130047692724474472925066947937011718750,\ + -1.88951420176453876997868519538315013051033020019531,\ + -0.43353409663735725798616726933687459677457809448242) + +#define SRGB_TO_YPBPR(FROM_R, FROM_G, FROM_B, TO_Y, TO_PB, TO_PR)\ + do {\ + TYPE R__ = (FROM_R), G__ = (FROM_G), B__ = (FROM_B);\ + (TO_Y) = R__ * D(0.2126) +\ + G__ * D(0.7152) +\ + B__ * D(0.0722);\ + (TO_PB) = B__ - to->Y;\ + (TO_PR) = R__ - to->Y;\ + } while (0) + +#define CIEXYZ_TO_YPBPR(FROM_X, FROM_Y, FROM_Z, TO_Y, TO_PB, TO_PR)\ + TRANSFORM((TO_Y), (TO_PB), (TO_PR), (FROM_X), (FROM_Y), (FROM_Z),\ + -0.00028314209073960778378920011277841695118695497513,\ + 1.00019821310075673892470149439759552478790283203125,\ + 0.00006512054470741990286342115723527967929840087891,\ + 0.05592664565509243568275365987574332393705844879150,\ + -1.20422439283671711685030913940863683819770812988281,\ + 1.05716144717799576113748116767965257167816162109375,\ + 3.24072939673847715269516811531502753496170043945312,\ + -2.53733297492083753610359053709544241428375244140625,\ + -0.49859531356743613805804216099204495549201965332031) + +#define SRGB_TO_YCGCO(FROM_R, FROM_G, FROM_B, TO_Y, TO_CG, TO_CO)\ + TRANSFORM((TO_Y), (TO_CG), (TO_CO), (FROM_R), (FROM_G), (FROM_B),\ + 0.25, 0.50, 0.25,\ + -0.25, 0.50, -0.25,\ + 0.50, 0., -0.50) + +#define CIEXYZ_TO_YCGCO(FROM_X, FROM_Y, FROM_Z, TO_Y, TO_CG, TO_CO)\ + TRANSFORM((TO_Y), (TO_CG), (TO_CO), (FROM_X), (FROM_Y), (FROM_Z),\ + 0.33938913643068269188063368346774950623512268066406,\ + 0.50271574450517486631895280879689380526542663574219,\ + 0.16045211478220866574417868832824751734733581542969,\ + -1.30865574267536244335019546269904822111129760742188,\ + 1.37329621528319534284889869013568386435508728027344,\ + -0.11889607256777862120955546743061859160661697387695,\ + 1.59240137554169236544510113162687048316001892089844,\ + -0.66655429104206020962664069884340278804302215576172,\ + -0.77787838037271606062006412685150280594825744628906) + +#define CIEUVW_TO_CIE1960UCS(FROM_U, FROM_V, FROM_W, TO_U, TO_V, TO_Y, FROM_U0, FROM_V0)\ + do {\ + TYPE U__ = (FROM_U), V__ = (FROM_V), W__ = (FROM_W), Y__;\ + Y__ = (W__ + 17) / 25;\ + Y__ *= Y__ * Y__;\ + W__ *= 13;\ + (TO_U) = U__ / W__ + (FROM_U0);\ + (TO_V) = V__ / W__ + (FROM_V0);\ + (TO_Y) = Y__;\ + } while (0) + +#define CIEXYZ_TO_CIE1960UCS(FROM_X, FROM_Y, FROM_Z, TO_U, TO_V, TO_Y)\ + do {\ + TYPE X__ = (FROM_X), Y__ = (FROM_Y), w__;\ + w__ = X__ + 15 * Y__ + 3 * (FROM_Z);\ + (TO_U) = 4 * X__ / w__;\ + (TO_V) = 6 * Y__ / w__;\ + (TO_Y) = Y__;\ + } while (0) + +#define CIEUVW_TO_CIEUVW(FROM_U, FROM_V, FROM_W, TO_U, TO_V, TO_W, FROM_U0, FROM_V0, TO_U0, TO_V0)\ + do {\ + TYPE U__ = (FROM_U), V__ = (FROM_V), W__ = (FROM_W);\ + TYPE w__ = W__ * 13;\ + U__ += w__ * ((FROM_U0) - (TO_U0));\ + V__ += w__ * ((FROM_V0) - (TO_V0));\ + (TO_U) = U__;\ + (TO_V) = V__;\ + (TO_W) = W__;\ + } while (0) + +#define CIE1960UCS_TO_CIEUVW(FROM_U, FROM_V, FROM_Y, TO_U, TO_V, TO_W, TO_U0, TO_V0)\ + do {\ + TYPE Y__ = 25 * xcbrt((FROM_Y)) - 17;\ + TYPE w__ = Y__ * 13;\ + to->U = w__ * ((FROM_U) - (TO_U0));\ + to->V = w__ * ((FROM_V) - (TO_V0));\ + to->W = Y__;\ + } while (0) + + + +static inline TYPE +srgb_encode(TYPE t) +{ + TYPE sign = 1; + if (t < 0) { + t = -t; + sign = -1; + } + t = t <= D(0.0031306684425217108) ? D(12.92) * t + : D(1.055) * xpow(t, 1 / D(2.4)) - D(0.055); + return t * sign; +} + +static inline TYPE +srgb_decode(TYPE t) +{ + TYPE sign = 1; + if (t < 0) { + t = -t; + sign = -1; + } + t = t <= D(0.0031306684425217108) * D(12.92) ? t / D(12.92) + : xpow((t + D(0.055)) / D(1.055), D(2.4)); + return t * sign; +} + +static inline TYPE +cielab_finv(TYPE t) +{ + return (t > D(6.) / D(29.)) ? t * t * t : (t - D(4.) / D(29.)) * 108 / 841; +} + +static inline TYPE +cielab_f(TYPE t) +{ + return (t > D(216.) / D(24389.)) ? xcbrt(t) : t * D(841.) / D(108.) + D(4.) / D(29.); +} diff --git a/convert-template.c b/convert-template.c index 223f840..99b1589 100644 --- a/convert-template.c +++ b/convert-template.c @@ -1,52 +1,5 @@ /* See LICENSE file for copyright and license details. */ -#if defined(__GNUC__) && !defined(__clang__) -# pragma GCC diagnostic ignored "-Wfloat-equal" -#elif defined(__clang__) -# pragma clang diagnostic ignored "-Wcomma" -# pragma clang diagnostic ignored "-Wfloat-equal" -# pragma clang diagnostic ignored "-Wvla" -# pragma clang diagnostic ignored "-Wtautological-compare" -# pragma clang diagnostic ignored "-Wcovered-switch-default" -# pragma clang diagnostic ignored "-Wfloat-conversion" -# pragma clang diagnostic ignored "-Wconversion" -# pragma clang diagnostic ignored "-Wdouble-promotion" -# pragma clang diagnostic ignored "-Wswitch-enum" -# pragma clang diagnostic ignored "-Wcast-align" -#endif - - -#define PI D(3.14159265358979323846) -#define PI2 (2 * D(3.14159265358979323846)) -#define WASDIV0(X) (xisinf(X) || xisnan(X)) - -#define REGULAR(S, T) ((T) <= (S)->TRANSITION ? (S)->SLOPE * (T) : (1 + (S)->OFFSET) * xpow((T), 1 / (S)->GAMMA) - (S)->OFFSET) -#define INVREGULAR(S, T) ((T) <= (S)->TRANSITIONINV ? (T) / (S)->SLOPE : xpow(((T) + (S)->OFFSET) / (1 + (S)->OFFSET), (S)->GAMMA)) - -#define TRANSFORM(X, Y, Z, A, B, C, R1C1, R1C2, R1C3, R2C1, R2C2, R2C3, R3C1, R3C2, R3C3)\ - do {\ - TYPE a__ = (A), b__ = (B), c__ = (C);\ - (X) = D(R1C1) * a__ + D(R1C2) * b__ + D(R1C3) * c__;\ - (Y) = D(R2C1) * a__ + D(R2C2) * b__ + D(R2C3) * c__;\ - (Z) = D(R3C1) * a__ + D(R3C2) * b__ + D(R3C3) * c__;\ - } while (0) - - - -#define SLOPE transfer.regular.slope -#define TRANSITIONINV transfer.regular.transitioninv -#define TRANSITION transfer.regular.transition -#define GAMMA transfer.regular.gamma -#define OFFSET transfer.regular.offset -#define TO_ENCODED_RED transfer.custom.to_encoded_red -#define TO_DECODED_RED transfer.custom.to_decoded_red -#define TO_ENCODED_GREEN transfer.custom.to_encoded_green -#define TO_DECODED_GREEN transfer.custom.to_decoded_green -#define TO_ENCODED_BLUE transfer.custom.to_encoded_blue -#define TO_DECODED_BLUE transfer.custom.to_decoded_blue - - - static void to_rgb(const libcolour_colour_t *restrict from, libcolour_rgb_t *restrict to); static void to_srgb(const libcolour_colour_t *restrict from, libcolour_srgb_t *restrict to); static void to_ciexyy(const libcolour_colour_t *restrict from, libcolour_ciexyy_t *restrict to); @@ -67,10 +20,7 @@ static void to_cieuvw(const libcolour_colour_t *restrict from, libcolour_cieuvw_ static void ciexyz_to_rgb(const libcolour_ciexyz_t *restrict from, libcolour_rgb_t *restrict to) { - TYPE X = from->X, Y = from->Y, Z = from->Z; - to->R = to->Minv[0][0] * X + to->Minv[0][1] * Y + to->Minv[0][2] * Z; - to->G = to->Minv[1][0] * X + to->Minv[1][1] * Y + to->Minv[1][2] * Z; - to->B = to->Minv[2][0] * X + to->Minv[2][1] * Y + to->Minv[2][2] * Z; + CIEXYZ_TO_RGB(from->X, from->Y, from->Z, to->R, to->G, to->B, to->Minv); } @@ -210,36 +160,7 @@ to_rgb(const libcolour_colour_t *restrict from, libcolour_rgb_t *restrict to) static void ciexyz_to_srgb(const libcolour_ciexyz_t *restrict from, libcolour_srgb_t *restrict to) { - TRANSFORM(to->R, to->G, to->B, from->X, from->Y, from->Z, - 3.240446254647737500675930277794, -1.537134761820080575134284117667, -0.498530193022728718155178739835, - -0.969266606244679751469561779231, 1.876011959788370209167851498933, 0.041556042214430065351304932619, - 0.055643503564352832235773149705, -0.204026179735960239147729566866, 1.057226567722703292062647051353); -} - -static inline TYPE -srgb_encode(TYPE t) -{ - TYPE sign = 1; - if (t < 0) { - t = -t; - sign = -1; - } - t = t <= D(0.0031306684425217108) ? D(12.92) * t - : D(1.055) * xpow(t, 1 / D(2.4)) - D(0.055); - return t * sign; -} - -static inline TYPE -srgb_decode(TYPE t) -{ - TYPE sign = 1; - if (t < 0) { - t = -t; - sign = -1; - } - t = t <= D(0.0031306684425217108) * D(12.92) ? t / D(12.92) - : xpow((t + D(0.055)) / D(1.055), D(2.4)); - return t * sign; + CIEXYZ_TO_SRGB(from->X, from->Y, from->Z, to->R, to->G, to->B); } static void @@ -261,50 +182,31 @@ srgb_to_srgb(const libcolour_srgb_t *restrict from, libcolour_srgb_t *restrict t static void yiq_to_srgb(const libcolour_yiq_t *restrict from, libcolour_srgb_t *restrict to) { - TRANSFORM(to->R, to->G, to->B, from->Y, from->I, from->Q, - 1, 0.95629483232089407263032398986979387700557708740234, 0.62102512544472865396727456754888407886028289794922, - 1, -0.27212147408397735492968649850809015333652496337891, -0.64738095351761570928061928498209454119205474853516, - 1, -1.10698990856712820018969978264067322015762329101562, 1.70461497549882934343656870623817667365074157714844); - /* ⎛1 1000 cos 33° / 877 1000 sin 33° / 877 ⎞ - ⎜1 9500 sin 33° / 24067 - 299000 * cos 33° / 514799 -9500 cos 33° / 24067 - 299000 * sin 33° / 514799⎟ - ⎝1 -250 sin 33° / 123 250 cos 33° / 123 ⎠ - */ + YIQ_TO_SRGB(from->Y, from->I, from->Q, to->R, to->G, to->B); } static void ydbdr_to_srgb(const libcolour_ydbdr_t *restrict from, libcolour_srgb_t *restrict to) { - TRANSFORM(to->R, to->G, to->B, from->Y, from->Db, from->Dr, - 1, 0.000092303716148, -0.525912630661865, - 1, -0.129132898890509, 0.267899328207599, - 1, 0.664679059978955, -0.000079202543533); + YDBDR_TO_SRGB(from->Y, from->Db, from->Dr, to->R, to->G, to->B); } static void ypbpr_to_srgb(const libcolour_ypbpr_t *restrict from, libcolour_srgb_t *restrict to) { - TYPE Y = from->Y, Pb = from->Pb, Pr = from->Pr; - to->R = Pr + Y; - to->B = Pb + Y; - to->G = (Y - to->R * D(0.2126) - to->B * D(0.0722)) / D(0.7152); + YPBPR_TO_SRGB(from->Y, from->Pb, from->Pr, to->R, to->G, to->B); } static void yuv_to_srgb(const libcolour_yuv_t *restrict from, libcolour_srgb_t *restrict to) { - TRANSFORM(to->R, to->G, to->B, from->Y, from->U, from->V, - 1, 0.00028328010485821202317155420580263580632163211703, 1.14070449590558520291949662350816652178764343261719, - 1, -0.39630886669497211727275498560629785060882568359375, -0.58107364288228224857846271333983168005943298339844, - 1, 2.03990003507541306504435851820744574069976806640625, 0.00017179031692307700847528739718228507626918144524); + YUV_TO_SRGB(from->Y, from->U, from->V, to->R, to->G, to->B); } static void ycgco_to_srgb(const libcolour_ycgco_t *restrict from, libcolour_srgb_t *restrict to) { - TYPE Y = from->Y, Cg = from->Cg, Co = from->Co; - to->R = Y - Cg + Co; - to->G = Y + Cg; - to->B = Y - Cg - Co; + YCGCO_TO_SRGB(from->Y, from->Cg, from->Co, to->R, to->G, to->B); } static void @@ -357,13 +259,7 @@ to_srgb(const libcolour_colour_t *restrict from, libcolour_srgb_t *restrict to) static void ciexyz_to_ciexyy(const libcolour_ciexyz_t *restrict from, libcolour_ciexyy_t *restrict to) { - TYPE X = from->X, Y = from->Y, Z = from->Z; - TYPE s = X + Y + Z; - to->x = X / s; - to->y = Y / s; - to->Y = Y; - if (WASDIV0(to->x) || WASDIV0(to->y)) - to->x = to->y = 0; + CIEXYZ_TO_CIEXYY(from->X, from->Y, from->Z, to->x, to->y, to->Y); } static void @@ -428,12 +324,11 @@ rgb_to_ciexyz(const libcolour_rgb_t *restrict from, libcolour_ciexyz_t *restrict } else { R = from->R, G = from->G, B = from->B; } - to->X = from->M[0][0] * R + from->M[0][1] * G + from->M[0][2] * B; - to->Y = from->M[1][0] * R + from->M[1][1] * G + from->M[1][2] * B; - to->Z = from->M[2][0] * R + from->M[2][1] * G + from->M[2][2] * B; + RGB_TO_CIEXYZ(R, G, B, to->X, to->Y, to->Z, from->M); } -static void srgb_to_ciexyz(const libcolour_srgb_t *restrict from, libcolour_ciexyz_t *restrict to) +static void +srgb_to_ciexyz(const libcolour_srgb_t *restrict from, libcolour_ciexyz_t *restrict to) { libcolour_srgb_t tmp; TYPE R, G, B; @@ -445,60 +340,26 @@ static void srgb_to_ciexyz(const libcolour_srgb_t *restrict from, libcolour_ciex } else { R = from->R, G = from->G, B = from->B; } - TRANSFORM(to->X, to->Y, to->Z, R, G, B, - 0.412457445582367576708548995157, 0.357575865245515878143578447634, 0.180437247826399665973085006954, - 0.212673370378408277403536885686, 0.715151730491031756287156895269, 0.072174899130559869164791564344, - 0.019333942761673460208893260415, 0.119191955081838593666354597644, 0.950302838552371742508739771438); + SRGB_TO_CIEXYZ(R, G, B, to->X, to->Y, to->Z); } static void ciexyy_to_ciexyz(const libcolour_ciexyy_t *restrict from, libcolour_ciexyz_t *restrict to) { - TYPE x = from->x, y = from->y, Y = from->Y; - TYPE Yy = Y / y; - if (WASDIV0(Yy)) { - to->X = to->Y = to->Z = Y; - } else { - to->X = x * Yy; - to->Y = Y; - to->Z = (1 - x - y) * Yy; - } -} - -static inline TYPE -cielab_finv(TYPE t) -{ - return (t > D(6.) / D(29.)) ? t * t * t : (t - D(4.) / D(29.)) * 108 / 841; + CIEXYY_TO_CIEXYZ(from->x, from->y, from->Y, to->X, to->Y, to->Z); } static void cielab_to_ciexyz(const libcolour_cielab_t *restrict from, libcolour_ciexyz_t *restrict to) { - TYPE L = from->L, a = from->a, b = from->b; - to->Y = (L + 16) / 116; - to->X = to->Y + a / 500; - to->Z = to->Y - b / 200; - to->X = cielab_finv(to->X) * D(0.95047); - to->Y = cielab_finv(to->Y); - to->Z = cielab_finv(to->Z) * D(1.08883); + CIELAB_TO_CIEXYZ(from->L, from->a, from->b, to->X, to->Y, to->Z); } static void cieluv_to_ciexyz(const libcolour_cieluv_t *restrict from, libcolour_ciexyz_t *restrict to) { - TYPE X = from->white.X, Y = from->white.Y, L = from->L, L13 = from->L * 13; - TYPE t = X + 15 * Y + 3 * from->white.Z; - TYPE u = from->u / L13 + 4 * X / t; - TYPE v = from->v / L13 + 9 * Y / t; - if (L <= 8) { - Y *= L * 27 / 24389; - } else { - L = (L + 16) / 116; - Y *= L * L * L; - } - to->X = D(2.25) * Y * u / v; - to->Y = Y; - to->Z = Y * (3 / v - D(0.75) * u / v - 5); + CIELUV_TO_CIEXYZ(from->L, from->u, from->v, to->X, to->Y, to->Z, + from->white.X, from->white.Y, from->white.Z); } static void @@ -514,85 +375,37 @@ cielchuv_to_ciexyz(const libcolour_cielchuv_t *restrict from, libcolour_ciexyz_t static void yiq_to_ciexyz(const libcolour_yiq_t *restrict from, libcolour_ciexyz_t *restrict to) { - TRANSFORM(to->X, to->Y, to->Z, from->Y, from->I, from->Q, - 0.95047055865428309306963683411595411598682403564453, - 0.09738463974178063198294807989441324025392532348633, - 0.33223466706854809515903070860076695680618286132812, - 0.99999999999999988897769753748434595763683319091797, - -0.07112658301916767455974621725545148365199565887451, - -0.20786968876398304040264974901219829916954040527344, - 1.08882873639588373393394249433185905218124389648438, - -1.06592139332461721679123911599162966012954711914062, - 1.55474471255181900808395312196807935833930969238281); + YIQ_TO_CIEXYZ(from->Y, from->I, from->Q, to->X, to->Y, to->Z); } static void ydbdr_to_ciexyz(const libcolour_ydbdr_t *restrict from, libcolour_ciexyz_t *restrict to) { - TRANSFORM(to->X, to->Y, to->Z, from->Y, from->Db, from->Dr, - 0.95047055865428309306963683411595411598682403564453, - 0.07379612357298576119646327242662664502859115600586, - -0.12113653724874726136384595065464964136481285095215, - 0.99999999999999988897769753748434595763683319091797, - -0.04435684145428285540813106990754022262990474700928, - 0.07973534004202506575431641522300196811556816101074, - 1.08882873639588373393394249433185905218124389648438, - 0.61625657933494271123464613992837257683277130126953, - 0.02168821359337728266192257819966471288353204727173); + YDBDR_TO_CIEXYZ(from->Y, from->Db, from->Dr, to->X, to->Y, to->Z); } static void yuv_to_ciexyz(const libcolour_yuv_t *restrict from, libcolour_ciexyz_t *restrict to) { - TRANSFORM(to->X, to->Y, to->Z, from->Y, from->U, from->V, - 0.95047055865428309306963683411595411598682403564453, - 0.22648030324549334180161963558930438011884689331055, - 0.26274514929253273143316960158699657768011093139648, - 0.99999999999999988897769753748434595763683319091797, - -0.13613114642319409930415474718756740912795066833496, - -0.17294595255115238763288232348713790997862815856934, - 1.08882873639588373393394249433185905218124389648438, - 1.89129144197893928058817891724174842238426208496094, - -0.04704173528403532422714761196402832865715026855469); + YUV_TO_CIEXYZ(from->Y, from->U, from->V, to->X, to->Y, to->Z); } static void ypbpr_to_ciexyz(const libcolour_ypbpr_t *restrict from, libcolour_ciexyz_t *restrict to) { - TRANSFORM(to->X, to->Y, to->Z, from->Y, from->Pb, from->Pr, - 0.95047055865428298204733437160030007362365722656250, - 0.14433968424876231217979238863335922360420227050781, - 0.30616461986760712399302519770571961998939514160156, - 0.99999999999999977795539507496869191527366638183594, - -0.00002022802471486617736928792510298080742359161377, - 0.00008771894888734421691367515450110659003257751465, - 1.08882873639588373393394249433185905218124389648438, - 0.93827031735982591165168287261622026562690734863281, - -0.01609699914324668607035206946420657914131879806519); + YPBPR_TO_CIEXYZ(from->Y, from->Pb, from->Pr, to->X, to->Y, to->Z); } static void ycgco_to_ciexyz(const libcolour_ycgco_t *restrict from, libcolour_ciexyz_t *restrict to) { - TRANSFORM(to->X, to->Y, to->Z, from->Y, from->Cg, from->Co, - 0.95047055865428309306963683411595411598682403564453, - -0.23531882816325136453805555447615915909409523010254, - 0.23202019775596791073546398820326430723071098327637, - 0.99999999999999988897769753748434595763683319091797, - 0.43030346098206362359661625305307097733020782470703, - 0.14049847124784842211653312915586866438388824462891, - 1.08882873639588373393394249433185905218124389648438, - -0.85044482623220662986796014592982828617095947265625, - -0.93096889579069830311652822274481877684593200683594); + YCGCO_TO_CIEXYZ(from->Y, from->Cg, from->Co, to->X, to->Y, to->Z); } static void cie1960ucs_to_ciexyz(const libcolour_cie1960ucs_t *restrict from, libcolour_ciexyz_t *restrict to) { - TYPE u = from->u, v = from->v, Y = from->Y; - to->X = D(1.5) * Y * u / v; - to->Y = Y; - to->Z = (4 * Y - Y * u - 10 * Y * v) / (2 * v); + CIE1960UCS_TO_CIEXYZ(from->u, from->v, from->Y, to->X, to->Y, to->Z); } static void @@ -666,22 +479,10 @@ static void to_ciexyz(const libcolour_colour_t *restrict from, libcolour_ciexyz_ } -static inline TYPE -cielab_f(TYPE t) -{ - return (t > D(216.) / D(24389.)) ? xcbrt(t) : t * D(841.) / D(108.) + D(4.) / D(29.); -} - static void ciexyz_to_cielab(const libcolour_ciexyz_t *restrict from, libcolour_cielab_t *restrict to) { - TYPE X = from->X, Y = from->Y, Z = from->Z; - X /= D(0.95047); - Z /= D(1.08883); - Y = cielab_f(Y); - to->L = 116 * Y - 16; - to->a = 500 * (cielab_f(X) - Y); - to->b = 200 * (Y - cielab_f(Z)); + CIEXYZ_TO_CIELAB(from->X, from->Y, from->Z, to->L, to->a, to->b); } static void @@ -713,20 +514,8 @@ to_cielab(const libcolour_colour_t *restrict from, libcolour_cielab_t *restrict static void ciexyz_to_cieluv(const libcolour_ciexyz_t *restrict from, libcolour_cieluv_t *restrict to) { - TYPE t, u, v, y, y2; - t = to->white.X + 15 * to->white.Y + 3 * to->white.Z; - u = 4 * to->white.X / t; - v = 9 * to->white.Y / t; - t = from->X + 15 * from->Y + 3 * from->Z; - u = 4 * from->X / t - u; - v = 9 * from->Y / t - v; - y = from->Y / to->white.Y; - y2 = y * 24389; - y = y2 <= 216 ? y2 / 27 : xcbrt(y) * 116 - 16; - to->L = y; - y *= 13; - to->u = u * y; - to->v = v * y; + CIEXYZ_TO_CIELUV(from->X, from->Y, from->Z, to->L, to->u, to->v, + to->white.X, to->white.Y, to->white.Z); } static void @@ -746,9 +535,7 @@ cielchuv_to_cieluv(const libcolour_cielchuv_t *restrict from, libcolour_cieluv_t } else { L = from->L, C = from->C, h = from->h * PI2 / from->one_revolution; } - to->L = L; - to->u = C * xcos(h); - to->v = C * xsin(h); + CIELCHUV_TO_CIELUV(L, C, h, to->L, to->u, to->v); } static void @@ -798,11 +585,7 @@ cieluv_to_cielchuv(const libcolour_cieluv_t *restrict from, libcolour_cielchuv_t } else { L = from->L, u = from->u, v = from->v; } - to->L = L; - to->C = xsqrt(u * u + v * v); - to->h = xatan2(v, u) / PI2 * to->one_revolution; - if (!WASDIV0(to->h) && (to->h < 0)) - to->h += to->one_revolution; + CIELUV_TO_CIELCHUV(L, u, v, to->L, to->C, to->h, to->one_revolution); } static void @@ -858,29 +641,14 @@ to_yiq(const libcolour_colour_t *restrict from, libcolour_yiq_t *restrict to) tmp.srgb.with_transfer = 0; to_srgb(from, &tmp.srgb); } - TRANSFORM(to->Y, to->I, to->Q, tmp.srgb.R, tmp.srgb.G, tmp.srgb.B, 0.299, 0.587, 0.114, - 0.59571613491277464191853141528554260730743408203125, /* (0.877 cos 33°)(1 - 0.299) - (0.492 sin 33°)(-0.299) */ - -0.27445283783925644716106262421817518770694732666016, /* (0.877 cos 33°)(-0.587) - (0.492 sin 33°)(-0.587) */ - -0.32126329707351808373516632855171337723731994628906, /* (0.877 cos 33°)(-0.114) - (0.492 sin 33°)(1 - 0.114) */ - 0.21145640212011795888713550084503367543220520019531, /* (0.877 sin 33°)(1 - 0.299) + (0.492 cos 33°)(-0.299) */ - -0.52259104529161115593183239980135113000869750976562, /* (0.877 sin 33°)(-0.587) + (0.492 cos 33°)(-0.587) */ - 0.31113464317149330806699936147197149693965911865234); /* (0.877 sin 33°)(-0.114) + (0.492 cos 33°)(1 - 0.114) */ + SRGB_TO_YIQ(tmp.srgb.R, tmp.srgb.G, tmp.srgb.B, to->Y, to->I, to->Q); break; default: tmp.model = LIBCOLOUR_CIEXYZ; to_ciexyz(from, &tmp.ciexyz); /* fall through */ case LIBCOLOUR_CIEXYZ: - TRANSFORM(to->Y, to->I, to->Q, tmp.ciexyz.X, tmp.ciexyz.Y, tmp.ciexyz.Z, - 0.40627729168038273499519164033699780702590942382812, - 0.61835674212166968910509012857801280915737152099609, - -0.00414330221353725880462093300593551248311996459961, - 2.17852787350219845308174626552499830722808837890625, - -1.36502666214454104753883711964590474963188171386719, - -0.64803574634025240541745915834326297044754028320312, - 1.20905577682138853923277110880007967352867126464844, - -1.36890364998339797431015085749095305800437927246094, - 0.20180559439597040016778350945969577878713607788086); + CIEXYZ_TO_YIQ(tmp.ciexyz.X, tmp.ciexyz.Y, tmp.ciexyz.Z, to->Y, to->I, to->Q); break; } } @@ -899,31 +667,17 @@ to_ydbdr(const libcolour_colour_t *restrict from, libcolour_ydbdr_t *restrict to tmp.srgb.with_transfer = 0; to_srgb(from, &tmp.srgb); } - TRANSFORM(to->Y, to->Db, to->Dr, tmp.srgb.R, tmp.srgb.G, tmp.srgb.B, - 0.299, 0.587, 0.114, - -0.450, -0.883, 1.333, - -1.333, 1.116, 0.217); + SRGB_TO_YDBDR(tmp.srgb.R, tmp.srgb.G, tmp.srgb.B, to->Y, to->Db, to->Dr); break; case LIBCOLOUR_YUV: - to->Y = tmp.yuv.Y; - to->Db = tmp.yuv.U * D(3.069); - to->Dr = -tmp.yuv.V * D(2.169); + YUV_TO_YDBDR(tmp.yuv.Y, tmp.yuv.U, tmp.yuv.V, to->Y, to->Db, to->Dr); break; default: tmp.model = LIBCOLOUR_CIEXYZ; to_ciexyz(from, &tmp.ciexyz); /* fall through */ case LIBCOLOUR_CIEXYZ: - TRANSFORM(to->Y, to->Db, to->Dr, tmp.ciexyz.X, tmp.ciexyz.Y, tmp.ciexyz.Z, - 0.40627729168038273499519164033699780702590942382812, - 0.61835674212166968910509012857801280915737152099609, - -0.00414330221353725880462093300593551248311996459961, - -0.52816561102614745237815441214479506015777587890625, - -1.23677481526212962315014465275453403592109680175781, - 1.59692761635924940222253098909277468919754028320312, - -5.38914174974103143966885909321717917919158935546875, - 4.09835630362728497999569299281574785709381103515625, - 0.94033545560642795013706063400604762136936187744141); + CIEXYZ_TO_YDBDR(tmp.ciexyz.X, tmp.ciexyz.Y, tmp.ciexyz.Z, to->Y, to->Db, to->Dr); break; } } @@ -938,41 +692,21 @@ to_yuv(const libcolour_colour_t *restrict from, libcolour_yuv_t *restrict to) *to = from->yuv; break; case LIBCOLOUR_YDBDR: - to->Y = from->ydbdr.Y; - to->U = from->ydbdr.Db / D(3.069); - to->V = from->ydbdr.Dr / D(-2.169); + YDBDR_TO_YUV(from->ydbdr.Y, from->ydbdr.Db, from->ydbdr.Dr, to->Y, to->U, to->V); break; case LIBCOLOUR_SRGB: if (tmp.srgb.with_transfer) { tmp.srgb.with_transfer = 0; to_srgb(from, &tmp.srgb); } - TRANSFORM(to->Y, to->U, to->V, tmp.srgb.R, tmp.srgb.G, tmp.srgb.B, - 0.29899999999999998800959133404830936342477798461914, - 0.58699999999999996624922005139524117112159729003906, - 0.11400000000000000410782519111307919956743717193604, - -0.14662756598240470062854967636667424812912940979004, - -0.28771586836102963635752871596196200698614120483398, - 0.43434343434343436474165400795754976570606231689453, - 0.61456892577224520035628074765554629266262054443359, - -0.51452282157676354490405401520547457039356231689453, - -0.10004610419548178035231700278018251992762088775635); + SRGB_TO_YUV(tmp.srgb.R, tmp.srgb.G, tmp.srgb.B, to->Y, to->U, to->V); break; default: tmp.model = LIBCOLOUR_CIEXYZ; to_ciexyz(from, &tmp.ciexyz); /* fall through */ case LIBCOLOUR_CIEXYZ: - TRANSFORM(to->Y, to->U, to->V, tmp.ciexyz.X, tmp.ciexyz.Y, tmp.ciexyz.Z, - 0.40627729168038273499519164033699780702590942382812, - 0.61835674212166968910509012857801280915737152099609, - -0.00414330221353725880462093300593551248311996459961, - -0.17209697328971895746718701047939248383045196533203, - -0.40298951295605395239718404809536878019571304321289, - 0.52034135430408912093014350830344483256340026855469, - 2.48462044709130047692724474472925066947937011718750, - -1.88951420176453876997868519538315013051033020019531, - -0.43353409663735725798616726933687459677457809448242); + CIEXYZ_TO_YUV(tmp.ciexyz.X, tmp.ciexyz.Y, tmp.ciexyz.Z, to->Y, to->U, to->V); break; } } @@ -991,27 +725,14 @@ to_ypbpr(const libcolour_colour_t *restrict from, libcolour_ypbpr_t *restrict to tmp.srgb.with_transfer = 0; to_srgb(from, &tmp.srgb); } - to->Y = tmp.srgb.R * D(0.2126) + - tmp.srgb.G * D(0.7152) + - tmp.srgb.B * D(0.0722); - to->Pb = tmp.srgb.B - to->Y; - to->Pr = tmp.srgb.R - to->Y; + SRGB_TO_YPBPR(tmp.srgb.R, tmp.srgb.G, tmp.srgb.B, to->Y, to->Pb, to->Pr); break; default: tmp.model = LIBCOLOUR_CIEXYZ; to_ciexyz(from, &tmp.ciexyz); /* fall through */ case LIBCOLOUR_CIEXYZ: - TRANSFORM(to->Y, to->Pb, to->Pr, tmp.ciexyz.X, tmp.ciexyz.Y, tmp.ciexyz.Z, - -0.00028314209073960778378920011277841695118695497513, - 1.00019821310075673892470149439759552478790283203125, - 0.00006512054470741990286342115723527967929840087891, - 0.05592664565509243568275365987574332393705844879150, - -1.20422439283671711685030913940863683819770812988281, - 1.05716144717799576113748116767965257167816162109375, - 3.24072939673847715269516811531502753496170043945312, - -2.53733297492083753610359053709544241428375244140625, - -0.49859531356743613805804216099204495549201965332031); + CIEXYZ_TO_YPBPR(tmp.ciexyz.X, tmp.ciexyz.Y, tmp.ciexyz.Z, to->Y, to->Pb, to->Pr); break; } } @@ -1030,25 +751,14 @@ to_ycgco(const libcolour_colour_t *restrict from, libcolour_ycgco_t *restrict to tmp.srgb.with_transfer = 0; to_srgb(from, &tmp.srgb); } - to->Y = tmp.srgb.R / 4 + tmp.srgb.G / 2 + tmp.srgb.B / 4; - to->Cg = -tmp.srgb.R / 4 + tmp.srgb.G / 2 - tmp.srgb.B / 4; - to->Co = tmp.srgb.R / 2 - tmp.srgb.B / 2; + SRGB_TO_YCGCO(tmp.srgb.R, tmp.srgb.G, tmp.srgb.B, to->Y, to->Cg, to->Co); break; default: tmp.model = LIBCOLOUR_CIEXYZ; to_ciexyz(from, &tmp.ciexyz); /* fall through */ case LIBCOLOUR_CIEXYZ: - TRANSFORM(to->Y, to->Cg, to->Co, tmp.ciexyz.X, tmp.ciexyz.Y, tmp.ciexyz.Z, - 0.33938913643068269188063368346774950623512268066406, - 0.50271574450517486631895280879689380526542663574219, - 0.16045211478220866574417868832824751734733581542969, - -1.30865574267536244335019546269904822111129760742188, - 1.37329621528319534284889869013568386435508728027344, - -0.11889607256777862120955546743061859160661697387695, - 1.59240137554169236544510113162687048316001892089844, - -0.66655429104206020962664069884340278804302215576172, - -0.77787838037271606062006412685150280594825744628906); + CIEXYZ_TO_YCGCO(tmp.ciexyz.X, tmp.ciexyz.Y, tmp.ciexyz.Z, to->Y, to->Cg, to->Co); break; } } @@ -1058,29 +768,20 @@ static void to_cie1960ucs(const libcolour_colour_t *restrict from, libcolour_cie1960ucs_t *restrict to) { libcolour_colour_t tmp = *from; - TYPE u, v, w, y; switch (from->model) { case LIBCOLOUR_CIE1960UCS: *to = from->cie1960ucs; return; case LIBCOLOUR_CIEUVW: - u = from->cieuvw.U, v = from->cieuvw.V, w = from->cieuvw.W; - y = (w + 17) / 25; - y *= y * y; - w *= 13; - to->u = u / w + from->cieuvw.u0; - to->v = v / w + from->cieuvw.v0; - to->Y = y; + CIEUVW_TO_CIE1960UCS(from->cieuvw.U, from->cieuvw.V, from->cieuvw.W, + to->u, to->v, to->Y, from->cieuvw.u0, from->cieuvw.v0); return; default: tmp.model = LIBCOLOUR_CIEXYZ; to_ciexyz(from, &tmp.ciexyz); /* fall through */ case LIBCOLOUR_CIEXYZ: - w = tmp.ciexyz.X + 15 * tmp.ciexyz.Y + 3 * tmp.ciexyz.Z; - to->u = 4 * tmp.ciexyz.X / w; - to->v = 6 * tmp.ciexyz.Y / w; - to->Y = tmp.ciexyz.Y; + CIEXYZ_TO_CIE1960UCS(tmp.ciexyz.X, tmp.ciexyz.Y, tmp.ciexyz.Z, to->u, to->v, to->Y); return; } } @@ -1089,27 +790,19 @@ to_cie1960ucs(const libcolour_colour_t *restrict from, libcolour_cie1960ucs_t *r static void to_cieuvw(const libcolour_colour_t *restrict from, libcolour_cieuvw_t *restrict to) { - TYPE U, V, W, w; libcolour_colour_t tmp = *from; switch (from->model) { case LIBCOLOUR_CIEUVW: - U = from->cieuvw.U, V = from->cieuvw.V, W = from->cieuvw.W; - w = W * 13; - U += w * (from->cieuvw.u0 - to->u0); - V += w * (from->cieuvw.v0 - to->v0); - to->U = U, to->V = V, to->W = W; + CIEUVW_TO_CIEUVW(from->cieuvw.U, from->cieuvw.V, from->cieuvw.W, to->U, to->V, to->W, + from->cieuvw.u0, from->cieuvw.v0, to->u0, to->v0); break; default: tmp.model = LIBCOLOUR_CIE1960UCS; to_cie1960ucs(from, &tmp.cie1960ucs); /* fall through */ case LIBCOLOUR_CIE1960UCS: - U = tmp.cie1960ucs.u, V = tmp.cie1960ucs.v, W = tmp.cie1960ucs.Y; - W = 25 * xcbrt(W) - 17; - w = W * 13; - to->U = w * (U - to->u0); - to->V = w * (V - to->v0); - to->W = W; + CIE1960UCS_TO_CIEUVW(tmp.cie1960ucs.u, tmp.cie1960ucs.v, tmp.cie1960ucs.Y, + to->U, to->V, to->W, to->u0, to->v0); break; } } diff --git a/double.c b/double.c index 77a2f70..9d37aed 100644 --- a/double.c +++ b/double.c @@ -81,6 +81,7 @@ #define libcolour_unmarshal libcolour_unmarshal_lf #define libcolour_convert_en_masse libcolour_convert_en_masse_lf +#include "common.h" #include "convert-template.c" #include "libcolour-template.c" #include "en_masse-template.c" diff --git a/en_masse-template.c b/en_masse-template.c index 8d1d779..9a9f17f 100644 --- a/en_masse-template.c +++ b/en_masse-template.c @@ -1,22 +1,5 @@ /* See LICENSE file for copyright and license details. */ -#if defined(__GNUC__) && !defined(__clang__) -# pragma GCC diagnostic ignored "-Wfloat-equal" -#elif defined(__clang__) -# pragma clang diagnostic ignored "-Wcomma" -# pragma clang diagnostic ignored "-Wfloat-equal" -# pragma clang diagnostic ignored "-Wvla" -# pragma clang diagnostic ignored "-Wtautological-compare" -# pragma clang diagnostic ignored "-Wcovered-switch-default" -# pragma clang diagnostic ignored "-Wfloat-conversion" -# pragma clang diagnostic ignored "-Wconversion" -# pragma clang diagnostic ignored "-Wdouble-promotion" -# pragma clang diagnostic ignored "-Wswitch-enum" -# pragma clang diagnostic ignored "-Wcast-align" -#endif - - - int libcolour_convert_en_masse(const libcolour_colour_t *from, const libcolour_colour_t *to, libcolour_convert_en_masse_mode_t mode, size_t n, ...) diff --git a/float.c b/float.c index 829003a..185423e 100644 --- a/float.c +++ b/float.c @@ -81,6 +81,7 @@ #define libcolour_unmarshal libcolour_unmarshal_f #define libcolour_convert_en_masse libcolour_convert_en_masse_f +#include "common.h" #include "convert-template.c" #include "libcolour-template.c" #include "en_masse-template.c" diff --git a/libcolour-template.c b/libcolour-template.c index f5455b0..9d0fb0b 100644 --- a/libcolour-template.c +++ b/libcolour-template.c @@ -1,64 +1,10 @@ /* See LICENSE file for copyright and license details. */ -#if defined(__GNUC__) && !defined(__clang__) -# pragma GCC diagnostic ignored "-Wfloat-equal" -#elif defined(__clang__) -# pragma clang diagnostic ignored "-Wcomma" -# pragma clang diagnostic ignored "-Wfloat-equal" -# pragma clang diagnostic ignored "-Wvla" -# pragma clang diagnostic ignored "-Wtautological-compare" -# pragma clang diagnostic ignored "-Wcovered-switch-default" -# pragma clang diagnostic ignored "-Wfloat-conversion" -# pragma clang diagnostic ignored "-Wconversion" -# pragma clang diagnostic ignored "-Wdouble-promotion" -# pragma clang diagnostic ignored "-Wswitch-enum" -# pragma clang diagnostic ignored "-Wcast-align" -#endif - - #define MARSHAL_VERSION 1 - -#define SLOPE transfer.regular.slope -#define TRANSITIONINV transfer.regular.transitioninv -#define TRANSITION transfer.regular.transition -#define GAMMA transfer.regular.gamma -#define OFFSET transfer.regular.offset -#define TO_ENCODED_RED transfer.custom.to_encoded_red -#define TO_DECODED_RED transfer.custom.to_decoded_red -#define TO_ENCODED_GREEN transfer.custom.to_encoded_green -#define TO_DECODED_GREEN transfer.custom.to_decoded_green -#define TO_ENCODED_BLUE transfer.custom.to_encoded_blue -#define TO_DECODED_BLUE transfer.custom.to_decoded_blue - - - -TYPE -libcolour_srgb_encode(TYPE t) -{ - TYPE sign = 1; - if (t < 0) { - t = -t; - sign = -1; - } - t = t <= D(0.0031306684425217108) ? D(12.92) * t : - D(1.055) * xpow(t, 1 / D(2.4)) - D(0.055); - return t * sign; -} - -TYPE -libcolour_srgb_decode(TYPE t) -{ - TYPE sign = 1; - if (t < 0) { - t = -t; - sign = -1; - } - t = t <= D(0.0031306684425217108) * D(12.92) ? t / D(12.92) : - xpow((t + D(0.055)) / D(1.055), D(2.4)); - return t * sign; -} +TYPE libcolour_srgb_encode(TYPE t) { return srgb_encode(t); } +TYPE libcolour_srgb_decode(TYPE t) { return srgb_decode(t); } int diff --git a/long-double.c b/long-double.c index b91da21..65954c3 100644 --- a/long-double.c +++ b/long-double.c @@ -81,6 +81,7 @@ #define libcolour_unmarshal libcolour_unmarshal_llf #define libcolour_convert_en_masse libcolour_convert_en_masse_llf +#include "common.h" #include "convert-template.c" #include "libcolour-template.c" #include "en_masse-template.c" -- cgit v1.2.3-70-g09d2