diff options
Diffstat (limited to '')
-rw-r--r-- | .gitignore | 2 | ||||
-rw-r--r-- | Makefile | 10 | ||||
-rw-r--r-- | TODO | 2 | ||||
-rw-r--r-- | conversions.h | 176 | ||||
-rw-r--r-- | double.c | 1 | ||||
-rw-r--r-- | en_masse-template.c | 154 | ||||
-rw-r--r-- | float.c | 1 | ||||
-rw-r--r-- | libcolour.h | 14 | ||||
-rw-r--r-- | long-double.c | 1 | ||||
-rwxr-xr-x | matrices.py | 184 | ||||
-rw-r--r-- | test.c | 1 |
11 files changed, 299 insertions, 247 deletions
@@ -9,5 +9,7 @@ *.so.* *.so *.a +/__pycache__/ /test /libcolour.7 +/conversion-matrices.h @@ -13,7 +13,8 @@ OBJ =\ HDR =\ libcolour.h\ common.h\ - conversions.h + conversions.h\ + conversion-matrices.h TEMPLATES =\ en_masse-template.c\ @@ -80,6 +81,11 @@ MAN3_SYMLINKS =\ all: libcolour.a libcolour.so.$(SO_VERSION) libcolour.7 test +conversion-matrices.h: matrices.py + printf '/* This file is generated! */\n' > conversion-matrices.h + ./matrices.py >> conversion-matrices.h + + float.o: float.c $(TEMPLATES) $(HDR) $(CC) $(CFLAGS) $(CPPFLAGS) -c -o $@ float.c @@ -179,6 +185,6 @@ check: test ./test clean: - -rm -- *.o *.a *.so *.so.* test + -rm -- *.o *.a *.so *.so.* test conversion-matrices.h .PHONY: all clean check install uninstall @@ -7,7 +7,7 @@ Colour models and colour spaces: TSL Y'UV Y'IQ - YES + Y'ES rgY RGK CIE DSH diff --git a/conversions.h b/conversions.h index 99261e9..494a666 100644 --- a/conversions.h +++ b/conversions.h @@ -12,25 +12,6 @@ (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\ - MATRIX( 3.240446254647737500675930277794, -1.537134761820080575134284117667, -0.498530193022728718155178739835,\ - -0.969266606244679751469561779231, 1.876011959788370209167851498933, 0.041556042214430065351304932619,\ - 0.055643503564352832235773149705, -0.204026179735960239147729566866, 1.057226567722703292062647051353) - -#define YIQ_TO_SRGB\ - MATRIX(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\ - MATRIX(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);\ @@ -41,16 +22,6 @@ (TO_G) = (y__ - r__ * D(0.2126) - b__ * D(0.0722)) / D(0.7152);\ } while (0) -#define YUV_TO_SRGB\ - MATRIX(1., 0.00028328010485821202317155420580263580632163211703, 1.14070449590558520291949662350816652178764343261719,\ - 1., -0.39630886669497211727275498560629785060882568359375, -0.58107364288228224857846271333983168005943298339844,\ - 1., 2.03990003507541306504435851820744574069976806640625, 0.00017179031692307700847528739718228507626918144524) - -#define YCGCO_TO_SRGB\ - MATRIX(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);\ @@ -69,11 +40,6 @@ (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\ - MATRIX(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);\ @@ -115,61 +81,6 @@ (TO_Z) = Y__ * (3 / v__ - D(0.75) * u__ / v__ - 5);\ } while (0) -#define YIQ_TO_CIEXYZ\ - MATRIX( 0.95047055865428309306963683411595411598682403564453,\ - 0.09738463974178063198294807989441324025392532348633,\ - 0.33223466706854809515903070860076695680618286132812,\ - 0.99999999999999988897769753748434595763683319091797,\ - -0.07112658301916767455974621725545148365199565887451,\ - -0.20786968876398304040264974901219829916954040527344,\ - 1.08882873639588373393394249433185905218124389648438,\ - -1.06592139332461721679123911599162966012954711914062,\ - 1.55474471255181900808395312196807935833930969238281) - -#define YDBDR_TO_CIEXYZ\ - MATRIX( 0.95047055865428309306963683411595411598682403564453,\ - 0.07379612357298576119646327242662664502859115600586,\ - -0.12113653724874726136384595065464964136481285095215,\ - 0.99999999999999988897769753748434595763683319091797,\ - -0.04435684145428285540813106990754022262990474700928,\ - 0.07973534004202506575431641522300196811556816101074,\ - 1.08882873639588373393394249433185905218124389648438,\ - 0.61625657933494271123464613992837257683277130126953,\ - 0.02168821359337728266192257819966471288353204727173) - -#define YUV_TO_CIEXYZ\ - MATRIX( 0.95047055865428309306963683411595411598682403564453,\ - 0.22648030324549334180161963558930438011884689331055,\ - 0.26274514929253273143316960158699657768011093139648,\ - 0.99999999999999988897769753748434595763683319091797,\ - -0.13613114642319409930415474718756740912795066833496,\ - -0.17294595255115238763288232348713790997862815856934,\ - 1.08882873639588373393394249433185905218124389648438,\ - 1.89129144197893928058817891724174842238426208496094,\ - -0.04704173528403532422714761196402832865715026855469) - -#define YPBPR_TO_CIEXYZ\ - MATRIX( 0.95047055865428298204733437160030007362365722656250,\ - 0.14433968424876231217979238863335922360420227050781,\ - 0.30616461986760712399302519770571961998939514160156,\ - 0.99999999999999977795539507496869191527366638183594,\ - -0.00002022802471486617736928792510298080742359161377,\ - 0.00008771894888734421691367515450110659003257751465,\ - 1.08882873639588373393394249433185905218124389648438,\ - 0.93827031735982591165168287261622026562690734863281,\ - -0.01609699914324668607035206946420657914131879806519) - -#define YCGCO_TO_CIEXYZ\ - MATRIX( 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);\ @@ -228,31 +139,6 @@ (TO_H) = h__;\ } while (0) -#define SRGB_TO_YIQ\ - MATRIX( 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\ - MATRIX( 0.40627729168038273499519164033699780702590942382812,\ - 0.61835674212166968910509012857801280915737152099609,\ - -0.00414330221353725880462093300593551248311996459961,\ - 2.17852787350219845308174626552499830722808837890625,\ - -1.36502666214454104753883711964590474963188171386719,\ - -0.64803574634025240541745915834326297044754028320312,\ - 1.20905577682138853923277110880007967352867126464844,\ - -1.36890364998339797431015085749095305800437927246094,\ - 0.20180559439597040016778350945969577878713607788086) - -#define SRGB_TO_YDBDR\ - MATRIX( 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 {\ TYPE U__ = (FROM_U), V__ = (FROM_V);\ @@ -261,17 +147,6 @@ (TO_DR) = V__ * D(-2.169);\ } while (0) -#define CIEXYZ_TO_YDBDR\ - MATRIX( 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 {\ TYPE DB__ = (FROM_DB), DR__ = (FROM_DR);\ @@ -280,28 +155,6 @@ (TO_V) = DR__ / D(-2.169);\ } while (0) -#define SRGB_TO_YUV\ - MATRIX( 0.29899999999999998800959133404830936342477798461914,\ - 0.58699999999999996624922005139524117112159729003906,\ - 0.11400000000000000410782519111307919956743717193604,\ - -0.14662756598240470062854967636667424812912940979004,\ - -0.28771586836102963635752871596196200698614120483398,\ - 0.43434343434343436474165400795754976570606231689453,\ - 0.61456892577224520035628074765554629266262054443359,\ - -0.51452282157676354490405401520547457039356231689453,\ - -0.10004610419548178035231700278018251992762088775635) - -#define CIEXYZ_TO_YUV\ - MATRIX( 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);\ @@ -313,33 +166,6 @@ (TO_PR) = R__ - Y__;\ } while (0) -#define CIEXYZ_TO_YPBPR\ - MATRIX(-0.00028314209073960778378920011277841695118695497513,\ - 1.00019821310075673892470149439759552478790283203125,\ - 0.00006512054470741990286342115723527967929840087891,\ - 0.05592664565509243568275365987574332393705844879150,\ - -1.20422439283671711685030913940863683819770812988281,\ - 1.05716144717799576113748116767965257167816162109375,\ - 3.24072939673847715269516811531502753496170043945312,\ - -2.53733297492083753610359053709544241428375244140625,\ - -0.49859531356743613805804216099204495549201965332031) - -#define SRGB_TO_YCGCO\ - MATRIX( 0.25, 0.50, 0.25,\ - -0.25, 0.50, -0.25,\ - 0.50, 0., -0.50) - -#define CIEXYZ_TO_YCGCO\ - MATRIX( 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__;\ @@ -380,3 +206,5 @@ (TO_V) = w__ * (V__ - v0__);\ (TO_W) = Y__;\ } while (0) + +#include "conversion-matrices.h" @@ -24,6 +24,7 @@ #define libcolour_ycgco_t libcolour_ycgco_lf_t #define libcolour_cie1960ucs_t libcolour_cie1960ucs_lf_t #define libcolour_cieuvw_t libcolour_cieuvw_lf_t +#define libcolour_yes_t libcolour_yes_lf_t #define TYPE double #define LIST_MODELS(X) LIBCOLOUR_LIST_MODELS(X, lf) diff --git a/en_masse-template.c b/en_masse-template.c index b539328..e131188 100644 --- a/en_masse-template.c +++ b/en_masse-template.c @@ -55,6 +55,27 @@ }\ } while (0) +#define DEFAULT_CONVERSION(FROM, TO)\ + case LIBCOLOUR_##FROM:\ + conversion_by_matrix(XARGUMENTS, FROM##_TO_##TO);\ + break + +#define DEFAULT_CONVERSION_FROM_SRGB(TO)\ + case LIBCOLOUR_SRGB:\ + LINEAR_SRGB;\ + conversion_by_matrix(XARGUMENTS, SRGB_TO_##TO);\ + break + +#define DEFAULT_CONV_0(FROM, TO)\ + case LIBCOLOUR_##FROM:\ + CONV_0(FROM##_TO_##TO);\ + break + +#define DEFAULT_CONV_N(FROM, TO, ...)\ + case LIBCOLOUR_##FROM:\ + CONV_N(FROM##_TO_##TO, __VA_ARGS__);\ + break + #define X(C, T, N) static void to_##N(const libcolour_colour_t *from, const T *to, XPARAMETERS); @@ -221,12 +242,16 @@ to_srgb(PARAMETERS(colour, srgb)) { libcolour_srgb_t tmp; switch (from->model) { + DEFAULT_CONVERSION(YCGCO, SRGB); + DEFAULT_CONVERSION(YDBDR, SRGB); + DEFAULT_CONVERSION(YES, SRGB); + DEFAULT_CONVERSION(YIQ, SRGB); + DEFAULT_CONVERSION(YUV, SRGB); + DEFAULT_CONV_0(YPBPR, SRGB); default: to_ciexyz(ARGUMENTS(from, NULL)); /* fall through */ - case LIBCOLOUR_CIEXYZ: - conversion_by_matrix(XARGUMENTS, CIEXYZ_TO_SRGB); - break; + DEFAULT_CONVERSION(CIEXYZ, SRGB); case LIBCOLOUR_SRGB: srgb_to_srgb: if (from->srgb.with_transfer != to->with_transfer) { @@ -241,21 +266,6 @@ to_srgb(PARAMETERS(colour, srgb)) } } return; - case LIBCOLOUR_YIQ: - conversion_by_matrix(XARGUMENTS, YIQ_TO_SRGB); - break; - case LIBCOLOUR_YDBDR: - conversion_by_matrix(XARGUMENTS, YDBDR_TO_SRGB); - break; - case LIBCOLOUR_YPBPR: - CONV_0(YPBPR_TO_SRGB); - break; - case LIBCOLOUR_YUV: - conversion_by_matrix(XARGUMENTS, YUV_TO_SRGB); - break; - case LIBCOLOUR_YCGCO: - conversion_by_matrix(XARGUMENTS, YCGCO_TO_SRGB); - break; } if (to->with_transfer) { tmp = *to; @@ -317,6 +327,12 @@ to_ciexyz(PARAMETERS(colour, ciexyz)) { libcolour_colour_t tmp; switch (from->model) { + DEFAULT_CONVERSION(YCGCO, CIEXYZ); + DEFAULT_CONVERSION(YDBDR, CIEXYZ); + DEFAULT_CONVERSION(YES, CIEXYZ); + DEFAULT_CONVERSION(YIQ, CIEXYZ); + DEFAULT_CONVERSION(YPBPR, CIEXYZ); + DEFAULT_CONVERSION(YUV, CIEXYZ); case LIBCOLOUR_RGB: if (from->rgb.with_transfer) { tmp.rgb = from->rgb; @@ -331,10 +347,7 @@ to_ciexyz(PARAMETERS(colour, ciexyz)) to_srgb(ARGUMENTS(from, &tmp.srgb)); from = (const void *)&tmp.srgb; /* fall through */ - case LIBCOLOUR_SRGB: - LINEAR_SRGB; - conversion_by_matrix(XARGUMENTS, SRGB_TO_CIEXYZ); - break; + DEFAULT_CONVERSION_FROM_SRGB(CIEXYZ); case LIBCOLOUR_CIEXYY: CONV_0(CIEXYY_TO_CIEXYZ); break; @@ -352,21 +365,6 @@ to_ciexyz(PARAMETERS(colour, ciexyz)) case LIBCOLOUR_CIELUV: CONV_N(CIELUV_TO_CIEXYZ, from->cieluv.white.X, from->cieluv.white.Y, from->cieluv.white.Z); break; - case LIBCOLOUR_YIQ: - conversion_by_matrix(XARGUMENTS, YIQ_TO_CIEXYZ); - break; - case LIBCOLOUR_YDBDR: - conversion_by_matrix(XARGUMENTS, YDBDR_TO_CIEXYZ); - break; - case LIBCOLOUR_YUV: - conversion_by_matrix(XARGUMENTS, YUV_TO_CIEXYZ); - break; - case LIBCOLOUR_YPBPR: - conversion_by_matrix(XARGUMENTS, YPBPR_TO_CIEXYZ); - break; - case LIBCOLOUR_YCGCO: - conversion_by_matrix(XARGUMENTS, YCGCO_TO_CIEXYZ); - break; case LIBCOLOUR_CIEUVW: tmp.cie1960ucs.model = LIBCOLOUR_CIE1960UCS; to_cie1960ucs(ARGUMENTS(from, &tmp.cie1960ucs)); @@ -477,18 +475,18 @@ static void to_yiq(PARAMETERS(colour, yiq)) { switch (from->model) { + DEFAULT_CONVERSION_FROM_SRGB(YIQ); + DEFAULT_CONVERSION(YCGCO, YIQ); + DEFAULT_CONVERSION(YDBDR, YIQ); + DEFAULT_CONVERSION(YES, YIQ); + DEFAULT_CONVERSION(YPBPR, YIQ); + DEFAULT_CONVERSION(YUV, YIQ); case LIBCOLOUR_YIQ: break; - case LIBCOLOUR_SRGB: - LINEAR_SRGB; - conversion_by_matrix(XARGUMENTS, SRGB_TO_YIQ); - break; default: to_ciexyz(ARGUMENTS(from, NULL)); /* fall through */ - case LIBCOLOUR_CIEXYZ: - conversion_by_matrix(XARGUMENTS, CIEXYZ_TO_YIQ); - break; + DEFAULT_CONVERSION(CIEXYZ, YIQ); } } @@ -497,21 +495,20 @@ static void to_ydbdr(PARAMETERS(colour, ydbdr)) { switch (from->model) { + DEFAULT_CONVERSION_FROM_SRGB(YDBDR); + DEFAULT_CONVERSION(YCGCO, YDBDR); + DEFAULT_CONVERSION(YES, YDBDR); + DEFAULT_CONVERSION(YIQ, YDBDR); + DEFAULT_CONVERSION(YPBPR, YDBDR); case LIBCOLOUR_YDBDR: break; - case LIBCOLOUR_SRGB: - LINEAR_SRGB; - conversion_by_matrix(XARGUMENTS, SRGB_TO_YDBDR); - break; case LIBCOLOUR_YUV: CONV_0(YUV_TO_YDBDR); break; default: to_ciexyz(ARGUMENTS(from, NULL)); /* fall through */ - case LIBCOLOUR_CIEXYZ: - conversion_by_matrix(XARGUMENTS, CIEXYZ_TO_YDBDR); - break; + DEFAULT_CONVERSION(CIEXYZ, YDBDR); } } @@ -520,21 +517,20 @@ static void to_yuv(PARAMETERS(colour, yuv)) { switch (from->model) { + DEFAULT_CONVERSION_FROM_SRGB(YUV); + DEFAULT_CONVERSION(YCGCO, YUV); + DEFAULT_CONVERSION(YES, YUV); + DEFAULT_CONVERSION(YIQ, YUV); + DEFAULT_CONVERSION(YPBPR, YUV); case LIBCOLOUR_YUV: break; case LIBCOLOUR_YDBDR: CONV_0(YDBDR_TO_YUV); break; - case LIBCOLOUR_SRGB: - LINEAR_SRGB; - conversion_by_matrix(XARGUMENTS, SRGB_TO_YUV); - break; default: to_ciexyz(ARGUMENTS(from, NULL)); /* fall through */ - case LIBCOLOUR_CIEXYZ: - conversion_by_matrix(XARGUMENTS, CIEXYZ_TO_YUV); - break; + DEFAULT_CONVERSION(CIEXYZ, YUV); } } @@ -543,6 +539,11 @@ static void to_ypbpr(PARAMETERS(colour, ypbpr)) { switch (from->model) { + DEFAULT_CONVERSION(YCGCO, YPBPR); + DEFAULT_CONVERSION(YDBDR, YPBPR); + DEFAULT_CONVERSION(YES, YPBPR); + DEFAULT_CONVERSION(YIQ, YPBPR); + DEFAULT_CONVERSION(YUV, YPBPR); case LIBCOLOUR_YPBPR: break; case LIBCOLOUR_SRGB: @@ -552,9 +553,7 @@ to_ypbpr(PARAMETERS(colour, ypbpr)) default: to_ciexyz(ARGUMENTS(from, NULL)); /* fall through */ - case LIBCOLOUR_CIEXYZ: - conversion_by_matrix(XARGUMENTS, CIEXYZ_TO_YPBPR); - break; + DEFAULT_CONVERSION(CIEXYZ, YPBPR); } } @@ -563,18 +562,18 @@ static void to_ycgco(PARAMETERS(colour, ycgco)) { switch (from->model) { + DEFAULT_CONVERSION_FROM_SRGB(YCGCO); + DEFAULT_CONVERSION(YDBDR, YCGCO); + DEFAULT_CONVERSION(YES, YCGCO); + DEFAULT_CONVERSION(YIQ, YCGCO); + DEFAULT_CONVERSION(YPBPR, YCGCO); + DEFAULT_CONVERSION(YUV, YCGCO); case LIBCOLOUR_YCGCO: break; - case LIBCOLOUR_SRGB: - LINEAR_SRGB; - conversion_by_matrix(XARGUMENTS, SRGB_TO_YCGCO); - break; default: to_ciexyz(ARGUMENTS(from, NULL)); /* fall through */ - case LIBCOLOUR_CIEXYZ: - conversion_by_matrix(XARGUMENTS, CIEXYZ_TO_YCGCO); - break; + DEFAULT_CONVERSION(CIEXYZ, YCGCO); } } @@ -614,6 +613,25 @@ to_cieuvw(PARAMETERS(colour, cieuvw)) } } +static void +to_yes(PARAMETERS(colour, yes)) +{ + switch (from->model) { + DEFAULT_CONVERSION_FROM_SRGB(YES); + DEFAULT_CONVERSION(YCGCO, YES); + DEFAULT_CONVERSION(YDBDR, YES); + DEFAULT_CONVERSION(YIQ, YES); + DEFAULT_CONVERSION(YPBPR, YES); + DEFAULT_CONVERSION(YUV, YES); + case LIBCOLOUR_YES: + break; + default: + to_ciexyz(ARGUMENTS(from, NULL)); + /* fall through */ + DEFAULT_CONVERSION(CIEXYZ, YES); + } +} + int libcolour_convert_en_masse(const libcolour_colour_t *from, const libcolour_colour_t *to, @@ -24,6 +24,7 @@ #define libcolour_ycgco_t libcolour_ycgco_f_t #define libcolour_cie1960ucs_t libcolour_cie1960ucs_f_t #define libcolour_cieuvw_t libcolour_cieuvw_f_t +#define libcolour_yes_t libcolour_yes_f_t #define TYPE float #define LIST_MODELS(X) LIBCOLOUR_LIST_MODELS(X, f) diff --git a/libcolour.h b/libcolour.h index 51dfaa8..8d7ae4f 100644 --- a/libcolour.h +++ b/libcolour.h @@ -162,7 +162,8 @@ _(LIBCOLOUR_YPBPR, libcolour_ypbpr_##RES##_t, ypbpr, __VA_ARGS__)\ _(LIBCOLOUR_YCGCO, libcolour_ycgco_##RES##_t, ycgco, __VA_ARGS__)\ _(LIBCOLOUR_CIE1960UCS, libcolour_cie1960ucs_##RES##_t, cie1960ucs, __VA_ARGS__)\ - _(LIBCOLOUR_CIEUVW, libcolour_cieuvw_##RES##_t, cieuvw, __VA_ARGS__) + _(LIBCOLOUR_CIEUVW, libcolour_cieuvw_##RES##_t, cieuvw, __VA_ARGS__)\ + _(LIBCOLOUR_YES, libcolour_yes_##RES##_t, yes, __VA_ARGS__) #define LIBCOLOUR_LIST_MODELS(_, RES)\ _(LIBCOLOUR_RGB, libcolour_rgb_##RES##_t, rgb)\ @@ -178,7 +179,8 @@ _(LIBCOLOUR_YPBPR, libcolour_ypbpr_##RES##_t, ypbpr)\ _(LIBCOLOUR_YCGCO, libcolour_ycgco_##RES##_t, ycgco)\ _(LIBCOLOUR_CIE1960UCS, libcolour_cie1960ucs_##RES##_t, cie1960ucs)\ - _(LIBCOLOUR_CIEUVW, libcolour_cieuvw_##RES##_t, cieuvw) + _(LIBCOLOUR_CIEUVW, libcolour_cieuvw_##RES##_t, cieuvw)\ + _(LIBCOLOUR_YES, libcolour_yes_##RES##_t, yes) #define LIBCOLOUR_RGB LIBCOLOUR_RGB @@ -195,6 +197,7 @@ #define LIBCOLOUR_YCGCO LIBCOLOUR_YCGCO #define LIBCOLOUR_CIE1960UCS LIBCOLOUR_CIE1960UCS #define LIBCOLOUR_CIEUVW LIBCOLOUR_CIEUVW +#define LIBCOLOUR_YES LIBCOLOUR_YES typedef enum libcolour_model { @@ -405,6 +408,13 @@ typedef struct libcolour_cielchuv_##RES {\ TYPE one_revolution;\ } libcolour_cielchuv_##RES##_t;\ \ +typedef struct libcolour_yes_##RES {\ + enum libcolour_model model;\ + TYPE Y;\ + TYPE E;\ + TYPE S;\ +} libcolour_yes_##RES##_t;\ +\ typedef struct libcolour_rgb_##RES {\ enum libcolour_model model;\ TYPE R;\ diff --git a/long-double.c b/long-double.c index 350ef87..dc46897 100644 --- a/long-double.c +++ b/long-double.c @@ -24,6 +24,7 @@ #define libcolour_ycgco_t libcolour_ycgco_llf_t #define libcolour_cie1960ucs_t libcolour_cie1960ucs_llf_t #define libcolour_cieuvw_t libcolour_cieuvw_llf_t +#define libcolour_yes_t libcolour_yes_llf_t #define TYPE long double #define LIST_MODELS(X) LIBCOLOUR_LIST_MODELS(X, llf) diff --git a/matrices.py b/matrices.py new file mode 100755 index 0000000..485374c --- /dev/null +++ b/matrices.py @@ -0,0 +1,184 @@ +#!/usr/bin/env python +# Both Python 2 and Python 3 will work + +import sys, math + +FORMAT = '%.50f' +DELIMITER = ', ' + +def _divrow(R, d): + return [c / d for c in R] + +def _subrow(A, B, m): + return [a - b * m for a, b in zip(A, B)] + +def transpose(M): + r, c = len(M), len(M[0]) + return [[M[y][x] for y in range(r)] for x in range(c)] + +def to_vector(M): + if len(M[0]) == 1: + return tuple(R[0] for R in M) + elif len(M) == 1: + return M[0] + raise Exception('Invalid arguments') + +def _multiply(A, B): + if isinstance(A[0], int) or isinstance(A[0], float): + A = [A] + if isinstance(B[0], int) or isinstance(B[0], float): + B = [B] + ar, ac, br, bc = len(A), len(A[0]), len(B), len(B[0]) + if ac != br: + if ac == bc: + B, br, bc = transpose(B), bc, br + elif ar == br: + A, ar, ac = transpose(A), ac, ar + else: + raise Exception('Invalid arguments') + return [[sum(A[r][i] * B[i][c] for i in range(ac)) for c in range(bc)] for r in range(ar)] + +def multiply(*Ms): + R, Ms = Ms[-1], list(Ms)[-2::-1] + for M in Ms: + R = _multiply(M, R) + return R + +def invert(M): + r, c = len(M), len(M[0]) + if r != c: + raise Exception('Invalid arguments') + I = [[1 if x == y else 0 for x in range(c)] for y in range(r)] + M = [MR + IR for MR, IR in zip(M, I)] + for r1 in range(r): + if M[r1][r1] == 0: + for r2 in range(r1 + 1, r): + if M[r2][r1] != 0: + break + if r2 == r: + raise Exception('Not invertable') + M[r1], M[r2] = M[r2], M[r1] + M[r1] = _divrow(M[r1], M[r1][r1]) + for r2 in range(r1 + 1, r): + M[r2] = _subrow(M[r2], M[r1], M[r2][r1]) + for r1 in reversed(range(r)): + for r2 in reversed(range(r1)): + M[r2] = _subrow(M[r2], M[r1], M[r2][r1]) + return [R[c:] for R in M] + +def str_matrix(M): + rfmt = '%s%s%s%s%s' % (FORMAT, DELIMITER, FORMAT, DELIMITER, FORMAT) + fmt = '%s\n%s\n%s' % (rfmt, rfmt, rfmt) + return fmt % tuple(M[0] + M[1] +M[2]) + +def print_matrix(M): + print(str_matrix(M)) + +def matrix(m11, m12, m13, m21, m22, m23, m31, m32, m33): + return [[m11, m12, m13], [m21, m22, m23], [m31, m32, m33]] + +def _cos(deg): + return math.cos(deg * math.pi / 180.) + +def _sin(deg): + return math.sin(deg * math.pi / 180.) + +def _yiq_i(k, a, b): + return 0.877 * _cos(33.) * (a - k) - (0.492 * _sin(33.)) * (b - k) + +def _yiq_q(k, a, b): + return 0.877 * _sin(33.) * (a - k) + (0.492 * _cos(33.)) * (b - k) + + +matrices = { + ('srgb', 'yes') : matrix(0.253, 0.684, 0.063, + 0.50, -0.50, 0., + 0.25, 0.25, -0.50), + + ('ycgco', 'srgb') : matrix(1., -1., 1., + 1., 1., 0., + 1., -1., -1.), + + ('srgb', 'yiq') : matrix(0.299, 0.587, 0.114, + _yiq_i(0.299, 1, 0), _yiq_i(0.587, 0, 0), _yiq_i(0.114, 0, 1), + _yiq_q(0.299, 1, 0), _yiq_q(0.587, 0, 0), _yiq_q(0.114, 0, 1)), + + ('srgb', 'ydbdr') : matrix( 0.299, 0.587, 0.114, + -0.450, -0.883, 1.333, + -1.333, 1.116, 0.217), + + ('srgb', 'ypbpr') : matrix( 0.2126, 0.7152, 0.0722, + -0.2126, -0.7152, 1 - 0.0722, + 1 - 0.2126, -0.7152, -0.0722), + + ('srgb', 'ciexyz') : matrix(0.412457445582367576708548995157, + 0.357575865245515878143578447634, + 0.180437247826399665973085006954, + 0.212673370378408277403536885686, + 0.715151730491031756287156895269, + 0.072174899130559869164791564344, + 0.019333942761673460208893260415, + 0.119191955081838593666354597644, + 0.950302838552371742508739771438), + + ('yuv', 'ydbdr') : matrix(1., 0., 0., + 0., 3.069, 0., + 0., 0., -2.169), +} + +if __name__ == '__main__': + models = set() + for (f, t) in list(matrices.keys()): + models.add(f) + models.add(t) + if (t, f) not in matrices: + matrices[(t, f)] = invert(matrices[(f, t)]) + + while True: + ms = list(matrices.keys()) + added = 0 + for m1 in models: + for m2 in models: + if m1 == m2: + continue + for m3 in models: + if m1 == m3 or m2 == m3: + continue + if (m1, m3) in ms: + continue + if (m1, m2) not in ms: + continue + if (m2, m3) not in ms: + continue + added += 1 + m = multiply(matrices[(m2, m3)], matrices[(m1, m2)]) + matrices[(m1, m3)] = m + if not added: + break + + for (f, t) in sorted(matrices.keys()): + M = multiply(matrices[(t, f)], matrices[(f, t)]) + for r in range(len(M)): + for c in range(len(M[r])): + x = M[r][c] + x -= 1 if r == c else 0 + x *= x + if x > 0.0001: + sys.stderr.write('(%s, %s)(%s, %s) != I\n' % (t, f, f, t)) + for r in M: + for c in r: + sys.stderr.write('\t%lg' % c) + sys.stderr.write('\n') + sys.exit(1) + + for (f, t) in sorted(matrices.keys()): + M = matrices[(f, t)] + macro = '%s_TO_%s' % (f.upper(), t.upper()) + print('') + print('#ifndef %s' % macro) + print('# define %s MATRIX(\\' % macro) + for row in M: + a = ', '.join([FORMAT] * len(row)) + b = ')' if row is M[-1] else ',\\' + print(('\t' + a + b) % tuple(row)) + print('#endif') @@ -724,6 +724,7 @@ main(int argc, char *argv[]) * YCgCo * CIE 1960 UCS * CIEUVW + * YES */ c1.srgb.with_transfer = 0; c1.srgb.R = 32 / 255., c1.srgb.G = 65 / 255., c1.srgb.B = 32 / 255.; |