Convert.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737
  1. /*
  2. * The Python Imaging Library
  3. * $Id$
  4. *
  5. * convert images
  6. *
  7. * history:
  8. * 1995-06-15 fl created
  9. * 1995-11-28 fl added some "RGBA" and "CMYK" conversions
  10. * 1996-04-22 fl added "1" conversions (same as "L")
  11. * 1996-05-05 fl added palette conversions (hack)
  12. * 1996-07-23 fl fixed "1" conversions to zero/non-zero convention
  13. * 1996-11-01 fl fixed "P" to "L" and "RGB" to "1" conversions
  14. * 1996-12-29 fl set alpha byte in RGB converters
  15. * 1997-05-12 fl added ImagingConvert2
  16. * 1997-05-30 fl added floating point support
  17. * 1997-08-27 fl added "P" to "1" and "P" to "F" conversions
  18. * 1998-01-11 fl added integer support
  19. * 1998-07-01 fl added "YCbCr" support
  20. * 1998-07-02 fl added "RGBX" conversions (sort of)
  21. * 1998-07-04 fl added floyd-steinberg dithering
  22. * 1998-07-12 fl changed "YCrCb" to "YCbCr" (!)
  23. * 1998-12-29 fl added basic "I;16" and "I;16B" conversions
  24. * 1999-02-03 fl added "RGBa", and "BGR" conversions (experimental)
  25. * 2003-09-26 fl added "LA" and "PA" conversions (experimental)
  26. * 2005-05-05 fl fixed "P" to "1" threshold
  27. * 2005-12-08 fl fixed palette memory leak in topalette
  28. *
  29. * Copyright (c) 1997-2005 by Secret Labs AB.
  30. * Copyright (c) 1995-1997 by Fredrik Lundh.
  31. *
  32. * See the README file for details on usage and redistribution.
  33. */
  34. #include "Imaging.h"
  35. #define MAX(a, b) (a)>(b) ? (a) : (b)
  36. #define MIN(a, b) (a)<(b) ? (a) : (b)
  37. #define CLIP16(v) ((v) <= -32768 ? -32768 : (v) >= 32767 ? 32767 : (v))
  38. /* ITU-R Recommendation 601-2 (assuming nonlinear RGB) */
  39. #define L(rgb)\
  40. ((INT32) (rgb)[0]*299 + (INT32) (rgb)[1]*587 + (INT32) (rgb)[2]*114)
  41. #define L24(rgb)\
  42. ((rgb)[0]*19595 + (rgb)[1]*38470 + (rgb)[2]*7471)
  43. /* ------------------- */
  44. /* 1 (bit) conversions */
  45. /* ------------------- */
  46. static void
  47. bit2l(UINT8* out, const UINT8* in, int xsize)
  48. {
  49. int x;
  50. for (x = 0; x < xsize; x++)
  51. *out++ = (*in++ != 0) ? 255 : 0;
  52. }
  53. static void
  54. bit2rgb(UINT8* out, const UINT8* in, int xsize)
  55. {
  56. int x;
  57. for (x = 0; x < xsize; x++) {
  58. UINT8 v = (*in++ != 0) ? 255 : 0;
  59. *out++ = v;
  60. *out++ = v;
  61. *out++ = v;
  62. *out++ = 255;
  63. }
  64. }
  65. static void
  66. bit2cmyk(UINT8* out, const UINT8* in, int xsize)
  67. {
  68. int x;
  69. for (x = 0; x < xsize; x++) {
  70. *out++ = 0;
  71. *out++ = 0;
  72. *out++ = 0;
  73. *out++ = (*in++ != 0) ? 0 : 255;
  74. }
  75. }
  76. static void
  77. bit2ycbcr(UINT8* out, const UINT8* in, int xsize)
  78. {
  79. int x;
  80. for (x = 0; x < xsize; x++) {
  81. *out++ = (*in++ != 0) ? 255 : 0;
  82. *out++ = 128;
  83. *out++ = 128;
  84. *out++ = 255;
  85. }
  86. }
  87. static void
  88. bit2hsv(UINT8* out, const UINT8* in, int xsize)
  89. {
  90. int x;
  91. for (x = 0; x < xsize; x++, out += 4) {
  92. UINT8 v = (*in++ != 0) ? 255 : 0;
  93. out[0] = 0;
  94. out[1] = 0;
  95. out[2] = v;
  96. out[3] = 255;
  97. }
  98. }
  99. /* ----------------- */
  100. /* RGB/L conversions */
  101. /* ----------------- */
  102. static void
  103. l2bit(UINT8* out, const UINT8* in, int xsize)
  104. {
  105. int x;
  106. for (x = 0; x < xsize; x++)
  107. *out++ = (*in++ >= 128) ? 255 : 0;
  108. }
  109. static void
  110. lA2la(UINT8* out, const UINT8* in, int xsize)
  111. {
  112. int x;
  113. unsigned int alpha, pixel, tmp;
  114. for (x = 0; x < xsize; x++, in += 4) {
  115. alpha = in[3];
  116. pixel = MULDIV255(in[0], alpha, tmp);
  117. *out++ = (UINT8) pixel;
  118. *out++ = (UINT8) pixel;
  119. *out++ = (UINT8) pixel;
  120. *out++ = (UINT8) alpha;
  121. }
  122. }
  123. /* RGBa -> RGBA conversion to remove premultiplication
  124. Needed for correct transforms/resizing on RGBA images */
  125. static void
  126. la2lA(UINT8* out, const UINT8* in, int xsize)
  127. {
  128. int x;
  129. unsigned int alpha, pixel;
  130. for (x = 0; x < xsize; x++, in+=4) {
  131. alpha = in[3];
  132. if (alpha == 255 || alpha == 0) {
  133. pixel = in[0];
  134. } else {
  135. pixel = CLIP8((255 * in[0]) / alpha);
  136. }
  137. *out++ = (UINT8) pixel;
  138. *out++ = (UINT8) pixel;
  139. *out++ = (UINT8) pixel;
  140. *out++ = (UINT8) alpha;
  141. }
  142. }
  143. static void
  144. l2la(UINT8* out, const UINT8* in, int xsize)
  145. {
  146. int x;
  147. for (x = 0; x < xsize; x++) {
  148. UINT8 v = *in++;
  149. *out++ = v;
  150. *out++ = v;
  151. *out++ = v;
  152. *out++ = 255;
  153. }
  154. }
  155. static void
  156. l2rgb(UINT8* out, const UINT8* in, int xsize)
  157. {
  158. int x;
  159. for (x = 0; x < xsize; x++) {
  160. UINT8 v = *in++;
  161. *out++ = v;
  162. *out++ = v;
  163. *out++ = v;
  164. *out++ = 255;
  165. }
  166. }
  167. static void
  168. l2hsv(UINT8* out, const UINT8* in, int xsize)
  169. {
  170. int x;
  171. for (x = 0; x < xsize; x++, out += 4) {
  172. UINT8 v = *in++;
  173. out[0] = 0;
  174. out[1] = 0;
  175. out[2] = v;
  176. out[3] = 255;
  177. }
  178. }
  179. static void
  180. la2l(UINT8* out, const UINT8* in, int xsize)
  181. {
  182. int x;
  183. for (x = 0; x < xsize; x++, in += 4)
  184. *out++ = in[0];
  185. }
  186. static void
  187. la2rgb(UINT8* out, const UINT8* in, int xsize)
  188. {
  189. int x;
  190. for (x = 0; x < xsize; x++, in += 4) {
  191. UINT8 v = in[0];
  192. *out++ = v;
  193. *out++ = v;
  194. *out++ = v;
  195. *out++ = in[3];
  196. }
  197. }
  198. static void
  199. la2hsv(UINT8* out, const UINT8* in, int xsize)
  200. {
  201. int x;
  202. for (x = 0; x < xsize; x++, in += 4, out += 4) {
  203. UINT8 v = in[0];
  204. out[0] = 0;
  205. out[1] = 0;
  206. out[2] = v;
  207. out[3] = in[3];
  208. }
  209. }
  210. static void
  211. rgb2bit(UINT8* out, const UINT8* in, int xsize)
  212. {
  213. int x;
  214. for (x = 0; x < xsize; x++, in += 4)
  215. /* ITU-R Recommendation 601-2 (assuming nonlinear RGB) */
  216. *out++ = (L(in) >= 128000) ? 255 : 0;
  217. }
  218. static void
  219. rgb2l(UINT8* out, const UINT8* in, int xsize)
  220. {
  221. int x;
  222. for (x = 0; x < xsize; x++, in += 4)
  223. /* ITU-R Recommendation 601-2 (assuming nonlinear RGB) */
  224. *out++ = L24(in) >> 16;
  225. }
  226. static void
  227. rgb2la(UINT8* out, const UINT8* in, int xsize)
  228. {
  229. int x;
  230. for (x = 0; x < xsize; x++, in += 4, out += 4) {
  231. /* ITU-R Recommendation 601-2 (assuming nonlinear RGB) */
  232. out[0] = out[1] = out[2] = L24(in) >> 16;
  233. out[3] = 255;
  234. }
  235. }
  236. static void
  237. rgb2i(UINT8* out_, const UINT8* in, int xsize)
  238. {
  239. int x;
  240. for (x = 0; x < xsize; x++, in += 4, out_ += 4) {
  241. INT32 v = L24(in) >> 16;
  242. memcpy(out_, &v, sizeof(v));
  243. }
  244. }
  245. static void
  246. rgb2f(UINT8* out_, const UINT8* in, int xsize)
  247. {
  248. int x;
  249. for (x = 0; x < xsize; x++, in += 4, out_ += 4) {
  250. FLOAT32 v = (float) L(in) / 1000.0F;
  251. memcpy(out_, &v, sizeof(v));
  252. }
  253. }
  254. static void
  255. rgb2bgr15(UINT8* out_, const UINT8* in, int xsize)
  256. {
  257. int x;
  258. for (x = 0; x < xsize; x++, in += 4, out_ += 2) {
  259. UINT16 v =
  260. ((((UINT16)in[0])<<7)&0x7c00) +
  261. ((((UINT16)in[1])<<2)&0x03e0) +
  262. ((((UINT16)in[2])>>3)&0x001f);
  263. memcpy(out_, &v, sizeof(v));
  264. }
  265. }
  266. static void
  267. rgb2bgr16(UINT8* out_, const UINT8* in, int xsize)
  268. {
  269. int x;
  270. for (x = 0; x < xsize; x++, in += 4, out_ += 2) {
  271. UINT16 v =
  272. ((((UINT16)in[0])<<8)&0xf800) +
  273. ((((UINT16)in[1])<<3)&0x07e0) +
  274. ((((UINT16)in[2])>>3)&0x001f);
  275. memcpy(out_, &v, sizeof(v));
  276. }
  277. }
  278. static void
  279. rgb2bgr24(UINT8* out, const UINT8* in, int xsize)
  280. {
  281. int x;
  282. for (x = 0; x < xsize; x++, in += 4) {
  283. *out++ = in[2];
  284. *out++ = in[1];
  285. *out++ = in[0];
  286. }
  287. }
  288. static void
  289. rgb2hsv_row(UINT8* out, const UINT8* in)
  290. { // following colorsys.py
  291. float h,s,rc,gc,bc,cr;
  292. UINT8 maxc,minc;
  293. UINT8 r, g, b;
  294. UINT8 uh,us,uv;
  295. r = in[0];
  296. g = in[1];
  297. b = in[2];
  298. maxc = MAX(r,MAX(g,b));
  299. minc = MIN(r,MIN(g,b));
  300. uv = maxc;
  301. if (minc == maxc){
  302. uh = 0;
  303. us = 0;
  304. } else {
  305. cr = (float)(maxc-minc);
  306. s = cr/(float)maxc;
  307. rc = ((float)(maxc-r))/cr;
  308. gc = ((float)(maxc-g))/cr;
  309. bc = ((float)(maxc-b))/cr;
  310. if (r == maxc) {
  311. h = bc-gc;
  312. } else if (g == maxc) {
  313. h = 2.0 + rc-bc;
  314. } else {
  315. h = 4.0 + gc-rc;
  316. }
  317. // incorrect hue happens if h/6 is negative.
  318. h = fmod((h/6.0 + 1.0), 1.0);
  319. uh = (UINT8)CLIP8((int)(h*255.0));
  320. us = (UINT8)CLIP8((int)(s*255.0));
  321. }
  322. out[0] = uh;
  323. out[1] = us;
  324. out[2] = uv;
  325. }
  326. static void
  327. rgb2hsv(UINT8* out, const UINT8* in, int xsize)
  328. {
  329. int x;
  330. for (x = 0; x < xsize; x++, in += 4, out += 4) {
  331. rgb2hsv_row(out, in);
  332. out[3] = in[3];
  333. }
  334. }
  335. static void
  336. hsv2rgb(UINT8* out, const UINT8* in, int xsize)
  337. { // following colorsys.py
  338. int p,q,t;
  339. UINT8 up,uq,ut;
  340. int i, x;
  341. float f, fs;
  342. UINT8 h,s,v;
  343. for (x = 0; x < xsize; x++, in += 4) {
  344. h = in[0];
  345. s = in[1];
  346. v = in[2];
  347. if (s==0){
  348. *out++ = v;
  349. *out++ = v;
  350. *out++ = v;
  351. } else {
  352. i = floor((float)h * 6.0 / 255.0); // 0 - 6
  353. f = (float)h * 6.0 / 255.0 - (float)i; // 0-1 : remainder.
  354. fs = ((float)s)/255.0;
  355. p = round((float)v * (1.0-fs));
  356. q = round((float)v * (1.0-fs*f));
  357. t = round((float)v * (1.0-fs*(1.0-f)));
  358. up = (UINT8)CLIP8(p);
  359. uq = (UINT8)CLIP8(q);
  360. ut = (UINT8)CLIP8(t);
  361. switch (i%6) {
  362. case 0:
  363. *out++ = v;
  364. *out++ = ut;
  365. *out++ = up;
  366. break;
  367. case 1:
  368. *out++ = uq;
  369. *out++ = v;
  370. *out++ = up;
  371. break;
  372. case 2:
  373. *out++ = up;
  374. *out++ = v;
  375. *out++ = ut;
  376. break;
  377. case 3:
  378. *out++ = up;
  379. *out++ = uq;
  380. *out++ = v;
  381. break;
  382. case 4:
  383. *out++ = ut;
  384. *out++ = up;
  385. *out++ = v;
  386. break;
  387. case 5:
  388. *out++ = v;
  389. *out++ = up;
  390. *out++ = uq;
  391. break;
  392. }
  393. }
  394. *out++ = in[3];
  395. }
  396. }
  397. /* ---------------- */
  398. /* RGBA conversions */
  399. /* ---------------- */
  400. static void
  401. rgb2rgba(UINT8* out, const UINT8* in, int xsize)
  402. {
  403. int x;
  404. for (x = 0; x < xsize; x++) {
  405. *out++ = *in++;
  406. *out++ = *in++;
  407. *out++ = *in++;
  408. *out++ = 255; in++;
  409. }
  410. }
  411. static void
  412. rgba2la(UINT8* out, const UINT8* in, int xsize)
  413. {
  414. int x;
  415. for (x = 0; x < xsize; x++, in += 4, out += 4) {
  416. /* ITU-R Recommendation 601-2 (assuming nonlinear RGB) */
  417. out[0] = out[1] = out[2] = L24(in) >> 16;
  418. out[3] = in[3];
  419. }
  420. }
  421. static void
  422. rgba2rgb(UINT8* out, const UINT8* in, int xsize)
  423. {
  424. int x;
  425. for (x = 0; x < xsize; x++) {
  426. *out++ = *in++;
  427. *out++ = *in++;
  428. *out++ = *in++;
  429. *out++ = 255; in++;
  430. }
  431. }
  432. static void
  433. rgbA2rgba(UINT8* out, const UINT8* in, int xsize)
  434. {
  435. int x;
  436. unsigned int alpha, tmp;
  437. for (x = 0; x < xsize; x++) {
  438. alpha = in[3];
  439. *out++ = MULDIV255(*in++, alpha, tmp);
  440. *out++ = MULDIV255(*in++, alpha, tmp);
  441. *out++ = MULDIV255(*in++, alpha, tmp);
  442. *out++ = *in++;
  443. }
  444. }
  445. /* RGBa -> RGBA conversion to remove premultiplication
  446. Needed for correct transforms/resizing on RGBA images */
  447. static void
  448. rgba2rgbA(UINT8* out, const UINT8* in, int xsize)
  449. {
  450. int x;
  451. unsigned int alpha;
  452. for (x = 0; x < xsize; x++, in+=4) {
  453. alpha = in[3];
  454. if (alpha == 255 || alpha == 0) {
  455. *out++ = in[0];
  456. *out++ = in[1];
  457. *out++ = in[2];
  458. } else {
  459. *out++ = CLIP8((255 * in[0]) / alpha);
  460. *out++ = CLIP8((255 * in[1]) / alpha);
  461. *out++ = CLIP8((255 * in[2]) / alpha);
  462. }
  463. *out++ = in[3];
  464. }
  465. }
  466. /*
  467. * Conversion of RGB + single transparent color to RGBA,
  468. * where any pixel that matches the color will have the
  469. * alpha channel set to 0
  470. */
  471. static void
  472. rgbT2rgba(UINT8* out, int xsize, int r, int g, int b)
  473. {
  474. #ifdef WORDS_BIGENDIAN
  475. UINT32 trns = ((r & 0xff)<<24) | ((g & 0xff)<<16) | ((b & 0xff)<<8) | 0xff;
  476. UINT32 repl = trns & 0xffffff00;
  477. #else
  478. UINT32 trns = (0xff <<24) | ((b & 0xff)<<16) | ((g & 0xff)<<8) | (r & 0xff);
  479. UINT32 repl = trns & 0x00ffffff;
  480. #endif
  481. int i;
  482. for (i=0; i < xsize; i++ ,out += sizeof(trns)) {
  483. UINT32 v;
  484. memcpy(&v, out, sizeof(v));
  485. if (v==trns) {
  486. memcpy(out, &repl, sizeof(repl));
  487. }
  488. }
  489. }
  490. /* ---------------- */
  491. /* CMYK conversions */
  492. /* ---------------- */
  493. static void
  494. l2cmyk(UINT8* out, const UINT8* in, int xsize)
  495. {
  496. int x;
  497. for (x = 0; x < xsize; x++) {
  498. *out++ = 0;
  499. *out++ = 0;
  500. *out++ = 0;
  501. *out++ = ~(*in++);
  502. }
  503. }
  504. static void
  505. la2cmyk(UINT8* out, const UINT8* in, int xsize)
  506. {
  507. int x;
  508. for (x = 0; x < xsize; x++, in += 4) {
  509. *out++ = 0;
  510. *out++ = 0;
  511. *out++ = 0;
  512. *out++ = ~(in[0]);
  513. }
  514. }
  515. static void
  516. rgb2cmyk(UINT8* out, const UINT8* in, int xsize)
  517. {
  518. int x;
  519. for (x = 0; x < xsize; x++) {
  520. /* Note: no undercolour removal */
  521. *out++ = ~(*in++);
  522. *out++ = ~(*in++);
  523. *out++ = ~(*in++);
  524. *out++ = 0; in++;
  525. }
  526. }
  527. static void
  528. cmyk2rgb(UINT8* out, const UINT8* in, int xsize)
  529. {
  530. int x, nk, tmp;
  531. for (x = 0; x < xsize; x++) {
  532. nk = 255 - in[3];
  533. out[0] = CLIP8(nk - MULDIV255(in[0], nk, tmp));
  534. out[1] = CLIP8(nk - MULDIV255(in[1], nk, tmp));
  535. out[2] = CLIP8(nk - MULDIV255(in[2], nk, tmp));
  536. out[3] = 255;
  537. out += 4;
  538. in += 4;
  539. }
  540. }
  541. static void
  542. cmyk2hsv(UINT8* out, const UINT8* in, int xsize)
  543. {
  544. int x, nk, tmp;
  545. for (x = 0; x < xsize; x++) {
  546. nk = 255 - in[3];
  547. out[0] = CLIP8(nk - MULDIV255(in[0], nk, tmp));
  548. out[1] = CLIP8(nk - MULDIV255(in[1], nk, tmp));
  549. out[2] = CLIP8(nk - MULDIV255(in[2], nk, tmp));
  550. rgb2hsv_row(out, out);
  551. out[3] = 255;
  552. out += 4;
  553. in += 4;
  554. }
  555. }
  556. /* ------------- */
  557. /* I conversions */
  558. /* ------------- */
  559. static void
  560. bit2i(UINT8* out_, const UINT8* in, int xsize)
  561. {
  562. int x;
  563. for (x = 0; x < xsize; x++, out_ += 4) {
  564. INT32 v = (*in++ != 0) ? 255 : 0;
  565. memcpy(out_, &v, sizeof(v));
  566. }
  567. }
  568. static void
  569. l2i(UINT8* out_, const UINT8* in, int xsize)
  570. {
  571. int x;
  572. for (x = 0; x < xsize; x++, out_ += 4) {
  573. INT32 v = *in++;
  574. memcpy(out_, &v, sizeof(v));
  575. }
  576. }
  577. static void
  578. i2l(UINT8* out, const UINT8* in_, int xsize)
  579. {
  580. int x;
  581. for (x = 0; x < xsize; x++, out++, in_ += 4) {
  582. INT32 v;
  583. memcpy(&v, in_, sizeof(v));
  584. if (v <= 0)
  585. *out = 0;
  586. else if (v >= 255)
  587. *out = 255;
  588. else
  589. *out = (UINT8) v;
  590. }
  591. }
  592. static void
  593. i2f(UINT8* out_, const UINT8* in_, int xsize)
  594. {
  595. int x;
  596. for (x = 0; x < xsize; x++, in_ += 4, out_ += 4) {
  597. INT32 i;
  598. FLOAT32 f;
  599. memcpy(&i, in_, sizeof(i));
  600. f = i;
  601. memcpy(out_, &f, sizeof(f));
  602. }
  603. }
  604. static void
  605. i2rgb(UINT8* out, const UINT8* in_, int xsize)
  606. {
  607. int x;
  608. INT32* in = (INT32*) in_;
  609. for (x = 0; x < xsize; x++, in++, out+=4) {
  610. if (*in <= 0)
  611. out[0] = out[1] = out[2] = 0;
  612. else if (*in >= 255)
  613. out[0] = out[1] = out[2] = 255;
  614. else
  615. out[0] = out[1] = out[2] = (UINT8) *in;
  616. out[3] = 255;
  617. }
  618. }
  619. static void
  620. i2hsv(UINT8* out, const UINT8* in_, int xsize)
  621. {
  622. int x;
  623. INT32* in = (INT32*) in_;
  624. for (x = 0; x < xsize; x++, in++, out+=4) {
  625. out[0] = 0;
  626. out[1] = 0;
  627. if (*in <= 0) {
  628. out[2] = 0;
  629. } else if (*in >= 255) {
  630. out[2] = 255;
  631. } else {
  632. out[2] = (UINT8) *in;
  633. }
  634. out[3] = 255;
  635. }
  636. }
  637. /* ------------- */
  638. /* F conversions */
  639. /* ------------- */
  640. static void
  641. bit2f(UINT8* out_, const UINT8* in, int xsize)
  642. {
  643. int x;
  644. for (x = 0; x < xsize; x++, out_ += 4) {
  645. FLOAT32 f = (*in++ != 0) ? 255.0F : 0.0F;
  646. memcpy(out_, &f, sizeof(f));
  647. }
  648. }
  649. static void
  650. l2f(UINT8* out_, const UINT8* in, int xsize)
  651. {
  652. int x;
  653. for (x = 0; x < xsize; x++, out_ += 4) {
  654. FLOAT32 f = (FLOAT32) *in++;
  655. memcpy(out_, &f, sizeof(f));
  656. }
  657. }
  658. static void
  659. f2l(UINT8* out, const UINT8* in_, int xsize)
  660. {
  661. int x;
  662. for (x = 0; x < xsize; x++, out++, in_ += 4) {
  663. FLOAT32 v;
  664. memcpy(&v, in_, sizeof(v));
  665. if (v <= 0.0)
  666. *out = 0;
  667. else if (v >= 255.0)
  668. *out = 255;
  669. else
  670. *out = (UINT8) v;
  671. }
  672. }
  673. static void
  674. f2i(UINT8* out_, const UINT8* in_, int xsize)
  675. {
  676. int x;
  677. for (x = 0; x < xsize; x++, in_ += 4, out_ += 4) {
  678. FLOAT32 f;
  679. INT32 i;
  680. memcpy(&f, in_, sizeof(f));
  681. i = f;
  682. memcpy(out_, &i, sizeof(i));
  683. }
  684. }
  685. /* ----------------- */
  686. /* YCbCr conversions */
  687. /* ----------------- */
  688. /* See ConvertYCbCr.c for RGB/YCbCr tables */
  689. static void
  690. l2ycbcr(UINT8* out, const UINT8* in, int xsize)
  691. {
  692. int x;
  693. for (x = 0; x < xsize; x++) {
  694. *out++ = *in++;
  695. *out++ = 128;
  696. *out++ = 128;
  697. *out++ = 255;
  698. }
  699. }
  700. static void
  701. la2ycbcr(UINT8* out, const UINT8* in, int xsize)
  702. {
  703. int x;
  704. for (x = 0; x < xsize; x++, in += 4) {
  705. *out++ = in[0];
  706. *out++ = 128;
  707. *out++ = 128;
  708. *out++ = 255;
  709. }
  710. }
  711. static void
  712. ycbcr2l(UINT8* out, const UINT8* in, int xsize)
  713. {
  714. int x;
  715. for (x = 0; x < xsize; x++, in += 4)
  716. *out++ = in[0];
  717. }
  718. static void
  719. ycbcr2la(UINT8* out, const UINT8* in, int xsize)
  720. {
  721. int x;
  722. for (x = 0; x < xsize; x++, in += 4, out += 4) {
  723. out[0] = out[1] = out[2] = in[0];
  724. out[3] = 255;
  725. }
  726. }
  727. /* ------------------------- */
  728. /* I;16 (16-bit) conversions */
  729. /* ------------------------- */
  730. static void
  731. I_I16L(UINT8* out, const UINT8* in_, int xsize)
  732. {
  733. int x, v;
  734. for (x = 0; x < xsize; x++, in_ += 4) {
  735. INT32 i;
  736. memcpy(&i, in_, sizeof(i));
  737. v = CLIP16(i);
  738. *out++ = (UINT8) v;
  739. *out++ = (UINT8) (v >> 8);
  740. }
  741. }
  742. static void
  743. I_I16B(UINT8* out, const UINT8* in_, int xsize)
  744. {
  745. int x, v;
  746. for (x = 0; x < xsize; x++, in_ += 4) {
  747. INT32 i;
  748. memcpy(&i, in_, sizeof(i));
  749. v = CLIP16(i);
  750. *out++ = (UINT8) (v >> 8);
  751. *out++ = (UINT8) v;
  752. }
  753. }
  754. static void
  755. I16L_I(UINT8* out_, const UINT8* in, int xsize)
  756. {
  757. int x;
  758. for (x = 0; x < xsize; x++, in += 2, out_ += 4) {
  759. INT32 v = in[0] + ((int) in[1] << 8);
  760. memcpy(out_, &v, sizeof(v));
  761. }
  762. }
  763. static void
  764. I16B_I(UINT8* out_, const UINT8* in, int xsize)
  765. {
  766. int x;
  767. for (x = 0; x < xsize; x++, in += 2, out_ += 4) {
  768. INT32 v = in[1] + ((int) in[0] << 8);
  769. memcpy(out_, &v, sizeof(v));
  770. }
  771. }
  772. static void
  773. I16L_F(UINT8* out_, const UINT8* in, int xsize)
  774. {
  775. int x;
  776. for (x = 0; x < xsize; x++, in += 2, out_ += 4) {
  777. FLOAT32 v = in[0] + ((int) in[1] << 8);
  778. memcpy(out_, &v, sizeof(v));
  779. }
  780. }
  781. static void
  782. I16B_F(UINT8* out_, const UINT8* in, int xsize)
  783. {
  784. int x;
  785. for (x = 0; x < xsize; x++, in += 2, out_ += 4) {
  786. FLOAT32 v = in[1] + ((int) in[0] << 8);
  787. memcpy(out_, &v, sizeof(v));
  788. }
  789. }
  790. static void
  791. L_I16L(UINT8* out, const UINT8* in, int xsize)
  792. {
  793. int x;
  794. for (x = 0; x < xsize; x++, in++) {
  795. *out++ = *in;
  796. *out++ = 0;
  797. }
  798. }
  799. static void
  800. L_I16B(UINT8* out, const UINT8* in, int xsize)
  801. {
  802. int x;
  803. for (x = 0; x < xsize; x++, in++) {
  804. *out++ = 0;
  805. *out++ = *in;
  806. }
  807. }
  808. static void
  809. I16L_L(UINT8* out, const UINT8* in, int xsize)
  810. {
  811. int x;
  812. for (x = 0; x < xsize; x++, in += 2)
  813. if (in[1] != 0)
  814. *out++ = 255;
  815. else
  816. *out++ = in[0];
  817. }
  818. static void
  819. I16B_L(UINT8* out, const UINT8* in, int xsize)
  820. {
  821. int x;
  822. for (x = 0; x < xsize; x++, in += 2)
  823. if (in[0] != 0)
  824. *out++ = 255;
  825. else
  826. *out++ = in[1];
  827. }
  828. static struct {
  829. const char* from;
  830. const char* to;
  831. ImagingShuffler convert;
  832. } converters[] = {
  833. { "1", "L", bit2l },
  834. { "1", "I", bit2i },
  835. { "1", "F", bit2f },
  836. { "1", "RGB", bit2rgb },
  837. { "1", "RGBA", bit2rgb },
  838. { "1", "RGBX", bit2rgb },
  839. { "1", "CMYK", bit2cmyk },
  840. { "1", "YCbCr", bit2ycbcr },
  841. { "1", "HSV", bit2hsv },
  842. { "L", "1", l2bit },
  843. { "L", "LA", l2la },
  844. { "L", "I", l2i },
  845. { "L", "F", l2f },
  846. { "L", "RGB", l2rgb },
  847. { "L", "RGBA", l2rgb },
  848. { "L", "RGBX", l2rgb },
  849. { "L", "CMYK", l2cmyk },
  850. { "L", "YCbCr", l2ycbcr },
  851. { "L", "HSV", l2hsv },
  852. { "LA", "L", la2l },
  853. { "LA", "La", lA2la },
  854. { "LA", "RGB", la2rgb },
  855. { "LA", "RGBA", la2rgb },
  856. { "LA", "RGBX", la2rgb },
  857. { "LA", "CMYK", la2cmyk },
  858. { "LA", "YCbCr", la2ycbcr },
  859. { "LA", "HSV", la2hsv },
  860. { "La", "LA", la2lA },
  861. { "I", "L", i2l },
  862. { "I", "F", i2f },
  863. { "I", "RGB", i2rgb },
  864. { "I", "RGBA", i2rgb },
  865. { "I", "RGBX", i2rgb },
  866. { "I", "HSV", i2hsv },
  867. { "F", "L", f2l },
  868. { "F", "I", f2i },
  869. { "RGB", "1", rgb2bit },
  870. { "RGB", "L", rgb2l },
  871. { "RGB", "LA", rgb2la },
  872. { "RGB", "I", rgb2i },
  873. { "RGB", "F", rgb2f },
  874. { "RGB", "BGR;15", rgb2bgr15 },
  875. { "RGB", "BGR;16", rgb2bgr16 },
  876. { "RGB", "BGR;24", rgb2bgr24 },
  877. { "RGB", "RGBA", rgb2rgba },
  878. { "RGB", "RGBX", rgb2rgba },
  879. { "RGB", "CMYK", rgb2cmyk },
  880. { "RGB", "YCbCr", ImagingConvertRGB2YCbCr },
  881. { "RGB", "HSV", rgb2hsv },
  882. { "RGBA", "1", rgb2bit },
  883. { "RGBA", "L", rgb2l },
  884. { "RGBA", "LA", rgba2la },
  885. { "RGBA", "I", rgb2i },
  886. { "RGBA", "F", rgb2f },
  887. { "RGBA", "RGB", rgba2rgb },
  888. { "RGBA", "RGBa", rgbA2rgba },
  889. { "RGBA", "RGBX", rgb2rgba },
  890. { "RGBA", "CMYK", rgb2cmyk },
  891. { "RGBA", "YCbCr", ImagingConvertRGB2YCbCr },
  892. { "RGBA", "HSV", rgb2hsv },
  893. { "RGBa", "RGBA", rgba2rgbA },
  894. { "RGBX", "1", rgb2bit },
  895. { "RGBX", "L", rgb2l },
  896. { "RGBX", "LA", rgb2la },
  897. { "RGBX", "I", rgb2i },
  898. { "RGBX", "F", rgb2f },
  899. { "RGBX", "RGB", rgba2rgb },
  900. { "RGBX", "CMYK", rgb2cmyk },
  901. { "RGBX", "YCbCr", ImagingConvertRGB2YCbCr },
  902. { "RGBX", "HSV", rgb2hsv },
  903. { "CMYK", "RGB", cmyk2rgb },
  904. { "CMYK", "RGBA", cmyk2rgb },
  905. { "CMYK", "RGBX", cmyk2rgb },
  906. { "CMYK", "HSV", cmyk2hsv },
  907. { "YCbCr", "L", ycbcr2l },
  908. { "YCbCr", "LA", ycbcr2la },
  909. { "YCbCr", "RGB", ImagingConvertYCbCr2RGB },
  910. { "HSV", "RGB", hsv2rgb },
  911. { "I", "I;16", I_I16L },
  912. { "I;16", "I", I16L_I },
  913. { "L", "I;16", L_I16L },
  914. { "I;16", "L", I16L_L },
  915. { "I", "I;16L", I_I16L },
  916. { "I;16L", "I", I16L_I },
  917. { "I", "I;16B", I_I16B },
  918. { "I;16B", "I", I16B_I },
  919. { "L", "I;16L", L_I16L },
  920. { "I;16L", "L", I16L_L },
  921. { "L", "I;16B", L_I16B },
  922. { "I;16B", "L", I16B_L },
  923. { "I;16", "F", I16L_F },
  924. { "I;16L", "F", I16L_F },
  925. { "I;16B", "F", I16B_F },
  926. { NULL }
  927. };
  928. /* FIXME: translate indexed versions to pointer versions below this line */
  929. /* ------------------- */
  930. /* Palette conversions */
  931. /* ------------------- */
  932. static void
  933. p2bit(UINT8* out, const UINT8* in, int xsize, const UINT8* palette)
  934. {
  935. int x;
  936. /* FIXME: precalculate greyscale palette? */
  937. for (x = 0; x < xsize; x++)
  938. *out++ = (L(&palette[in[x]*4]) >= 128000) ? 255 : 0;
  939. }
  940. static void
  941. pa2bit(UINT8* out, const UINT8* in, int xsize, const UINT8* palette)
  942. {
  943. int x;
  944. /* FIXME: precalculate greyscale palette? */
  945. for (x = 0; x < xsize; x++, in += 4)
  946. *out++ = (L(&palette[in[0]*4]) >= 128000) ? 255 : 0;
  947. }
  948. static void
  949. p2l(UINT8* out, const UINT8* in, int xsize, const UINT8* palette)
  950. {
  951. int x;
  952. /* FIXME: precalculate greyscale palette? */
  953. for (x = 0; x < xsize; x++)
  954. *out++ = L(&palette[in[x]*4]) / 1000;
  955. }
  956. static void
  957. pa2l(UINT8* out, const UINT8* in, int xsize, const UINT8* palette)
  958. {
  959. int x;
  960. /* FIXME: precalculate greyscale palette? */
  961. for (x = 0; x < xsize; x++, in += 4)
  962. *out++ = L(&palette[in[0]*4]) / 1000;
  963. }
  964. static void
  965. p2pa(UINT8* out, const UINT8* in, int xsize, const UINT8* palette)
  966. {
  967. int x;
  968. for (x = 0; x < xsize; x++, in++) {
  969. const UINT8* rgba = &palette[in[0]];
  970. *out++ = in[0];
  971. *out++ = in[0];
  972. *out++ = in[0];
  973. *out++ = rgba[3];
  974. }
  975. }
  976. static void
  977. p2la(UINT8* out, const UINT8* in, int xsize, const UINT8* palette)
  978. {
  979. int x;
  980. /* FIXME: precalculate greyscale palette? */
  981. for (x = 0; x < xsize; x++, out+=4) {
  982. const UINT8* rgba = &palette[*in++ * 4];
  983. out[0] = out[1] = out[2] = L(rgba) / 1000;
  984. out[3] = rgba[3];
  985. }
  986. }
  987. static void
  988. pa2la(UINT8* out, const UINT8* in, int xsize, const UINT8* palette)
  989. {
  990. int x;
  991. /* FIXME: precalculate greyscale palette? */
  992. for (x = 0; x < xsize; x++, in += 4, out += 4) {
  993. out[0] = out[1] = out[2] = L(&palette[in[0]*4]) / 1000;
  994. out[3] = in[3];
  995. }
  996. }
  997. static void
  998. p2i(UINT8* out_, const UINT8* in, int xsize, const UINT8* palette)
  999. {
  1000. int x;
  1001. for (x = 0; x < xsize; x++, out_ += 4) {
  1002. INT32 v = L(&palette[in[x]*4]) / 1000;
  1003. memcpy(out_, &v, sizeof(v));
  1004. }
  1005. }
  1006. static void
  1007. pa2i(UINT8* out_, const UINT8* in, int xsize, const UINT8* palette)
  1008. {
  1009. int x;
  1010. INT32* out = (INT32*) out_;
  1011. for (x = 0; x < xsize; x++, in += 4)
  1012. *out++ = L(&palette[in[0]*4]) / 1000;
  1013. }
  1014. static void
  1015. p2f(UINT8* out_, const UINT8* in, int xsize, const UINT8* palette)
  1016. {
  1017. int x;
  1018. for (x = 0; x < xsize; x++, out_ += 4) {
  1019. FLOAT32 v = L(&palette[in[x]*4]) / 1000.0F;
  1020. memcpy(out_, &v, sizeof(v));
  1021. }
  1022. }
  1023. static void
  1024. pa2f(UINT8* out_, const UINT8* in, int xsize, const UINT8* palette)
  1025. {
  1026. int x;
  1027. FLOAT32* out = (FLOAT32*) out_;
  1028. for (x = 0; x < xsize; x++, in += 4)
  1029. *out++ = (float) L(&palette[in[0]*4]) / 1000.0F;
  1030. }
  1031. static void
  1032. p2rgb(UINT8* out, const UINT8* in, int xsize, const UINT8* palette)
  1033. {
  1034. int x;
  1035. for (x = 0; x < xsize; x++) {
  1036. const UINT8* rgb = &palette[*in++ * 4];
  1037. *out++ = rgb[0];
  1038. *out++ = rgb[1];
  1039. *out++ = rgb[2];
  1040. *out++ = 255;
  1041. }
  1042. }
  1043. static void
  1044. pa2rgb(UINT8* out, const UINT8* in, int xsize, const UINT8* palette)
  1045. {
  1046. int x;
  1047. for (x = 0; x < xsize; x++, in += 4) {
  1048. const UINT8* rgb = &palette[in[0] * 4];
  1049. *out++ = rgb[0];
  1050. *out++ = rgb[1];
  1051. *out++ = rgb[2];
  1052. *out++ = 255;
  1053. }
  1054. }
  1055. static void
  1056. p2hsv(UINT8* out, const UINT8* in, int xsize, const UINT8* palette)
  1057. {
  1058. int x;
  1059. for (x = 0; x < xsize; x++, out += 4) {
  1060. const UINT8* rgb = &palette[*in++ * 4];
  1061. rgb2hsv_row(out, rgb);
  1062. out[3] = 255;
  1063. }
  1064. }
  1065. static void
  1066. pa2hsv(UINT8* out, const UINT8* in, int xsize, const UINT8* palette)
  1067. {
  1068. int x;
  1069. for (x = 0; x < xsize; x++, in += 4, out += 4) {
  1070. const UINT8* rgb = &palette[in[0] * 4];
  1071. rgb2hsv_row(out, rgb);
  1072. out[3] = 255;
  1073. }
  1074. }
  1075. static void
  1076. p2rgba(UINT8* out, const UINT8* in, int xsize, const UINT8* palette)
  1077. {
  1078. int x;
  1079. for (x = 0; x < xsize; x++) {
  1080. const UINT8* rgba = &palette[*in++ * 4];
  1081. *out++ = rgba[0];
  1082. *out++ = rgba[1];
  1083. *out++ = rgba[2];
  1084. *out++ = rgba[3];
  1085. }
  1086. }
  1087. static void
  1088. pa2rgba(UINT8* out, const UINT8* in, int xsize, const UINT8* palette)
  1089. {
  1090. int x;
  1091. for (x = 0; x < xsize; x++, in += 4) {
  1092. const UINT8* rgb = &palette[in[0] * 4];
  1093. *out++ = rgb[0];
  1094. *out++ = rgb[1];
  1095. *out++ = rgb[2];
  1096. *out++ = in[3];
  1097. }
  1098. }
  1099. static void
  1100. p2cmyk(UINT8* out, const UINT8* in, int xsize, const UINT8* palette)
  1101. {
  1102. p2rgb(out, in, xsize, palette);
  1103. rgb2cmyk(out, out, xsize);
  1104. }
  1105. static void
  1106. pa2cmyk(UINT8* out, const UINT8* in, int xsize, const UINT8* palette)
  1107. {
  1108. pa2rgb(out, in, xsize, palette);
  1109. rgb2cmyk(out, out, xsize);
  1110. }
  1111. static void
  1112. p2ycbcr(UINT8* out, const UINT8* in, int xsize, const UINT8* palette)
  1113. {
  1114. p2rgb(out, in, xsize, palette);
  1115. ImagingConvertRGB2YCbCr(out, out, xsize);
  1116. }
  1117. static void
  1118. pa2ycbcr(UINT8* out, const UINT8* in, int xsize, const UINT8* palette)
  1119. {
  1120. pa2rgb(out, in, xsize, palette);
  1121. ImagingConvertRGB2YCbCr(out, out, xsize);
  1122. }
  1123. static Imaging
  1124. frompalette(Imaging imOut, Imaging imIn, const char *mode)
  1125. {
  1126. ImagingSectionCookie cookie;
  1127. int alpha;
  1128. int y;
  1129. void (*convert)(UINT8*, const UINT8*, int, const UINT8*);
  1130. /* Map palette image to L, RGB, RGBA, or CMYK */
  1131. if (!imIn->palette)
  1132. return (Imaging) ImagingError_ValueError("no palette");
  1133. alpha = !strcmp(imIn->mode, "PA");
  1134. if (strcmp(mode, "1") == 0)
  1135. convert = alpha ? pa2bit : p2bit;
  1136. else if (strcmp(mode, "L") == 0)
  1137. convert = alpha ? pa2l : p2l;
  1138. else if (strcmp(mode, "LA") == 0)
  1139. convert = alpha ? pa2la : p2la;
  1140. else if (strcmp(mode, "PA") == 0)
  1141. convert = p2pa;
  1142. else if (strcmp(mode, "I") == 0)
  1143. convert = alpha ? pa2i : p2i;
  1144. else if (strcmp(mode, "F") == 0)
  1145. convert = alpha ? pa2f : p2f;
  1146. else if (strcmp(mode, "RGB") == 0)
  1147. convert = alpha ? pa2rgb : p2rgb;
  1148. else if (strcmp(mode, "RGBA") == 0)
  1149. convert = alpha ? pa2rgba : p2rgba;
  1150. else if (strcmp(mode, "RGBX") == 0)
  1151. convert = alpha ? pa2rgba : p2rgba;
  1152. else if (strcmp(mode, "CMYK") == 0)
  1153. convert = alpha ? pa2cmyk : p2cmyk;
  1154. else if (strcmp(mode, "YCbCr") == 0)
  1155. convert = alpha ? pa2ycbcr : p2ycbcr;
  1156. else if (strcmp(mode, "HSV") == 0)
  1157. convert = alpha ? pa2hsv : p2hsv;
  1158. else
  1159. return (Imaging) ImagingError_ValueError("conversion not supported");
  1160. imOut = ImagingNew2Dirty(mode, imOut, imIn);
  1161. if (!imOut)
  1162. return NULL;
  1163. ImagingSectionEnter(&cookie);
  1164. for (y = 0; y < imIn->ysize; y++)
  1165. (*convert)((UINT8*) imOut->image[y], (UINT8*) imIn->image[y],
  1166. imIn->xsize, imIn->palette->palette);
  1167. ImagingSectionLeave(&cookie);
  1168. return imOut;
  1169. }
  1170. #if defined(_MSC_VER)
  1171. #pragma optimize("", off)
  1172. #endif
  1173. static Imaging
  1174. topalette(Imaging imOut, Imaging imIn, const char *mode, ImagingPalette inpalette, int dither)
  1175. {
  1176. ImagingSectionCookie cookie;
  1177. int alpha;
  1178. int x, y;
  1179. ImagingPalette palette = inpalette;;
  1180. /* Map L or RGB/RGBX/RGBA to palette image */
  1181. if (strcmp(imIn->mode, "L") != 0 && strncmp(imIn->mode, "RGB", 3) != 0)
  1182. return (Imaging) ImagingError_ValueError("conversion not supported");
  1183. alpha = !strcmp(mode, "PA");
  1184. if (palette == NULL) {
  1185. /* FIXME: make user configurable */
  1186. if (imIn->bands == 1)
  1187. palette = ImagingPaletteNew("RGB"); /* Initialised to grey ramp */
  1188. else
  1189. palette = ImagingPaletteNewBrowser(); /* Standard colour cube */
  1190. }
  1191. if (!palette)
  1192. return (Imaging) ImagingError_ValueError("no palette");
  1193. imOut = ImagingNew2Dirty(mode, imOut, imIn);
  1194. if (!imOut) {
  1195. if (palette != inpalette)
  1196. ImagingPaletteDelete(palette);
  1197. return NULL;
  1198. }
  1199. ImagingPaletteDelete(imOut->palette);
  1200. imOut->palette = ImagingPaletteDuplicate(palette);
  1201. if (imIn->bands == 1) {
  1202. /* greyscale image */
  1203. /* Greyscale palette: copy data as is */
  1204. ImagingSectionEnter(&cookie);
  1205. for (y = 0; y < imIn->ysize; y++) {
  1206. if (alpha) {
  1207. l2la((UINT8*) imOut->image[y], (UINT8*) imIn->image[y], imIn->xsize);
  1208. } else {
  1209. memcpy(imOut->image[y], imIn->image[y], imIn->linesize);
  1210. }
  1211. }
  1212. ImagingSectionLeave(&cookie);
  1213. } else {
  1214. /* colour image */
  1215. /* Create mapping cache */
  1216. if (ImagingPaletteCachePrepare(palette) < 0) {
  1217. ImagingDelete(imOut);
  1218. if (palette != inpalette)
  1219. ImagingPaletteDelete(palette);
  1220. return NULL;
  1221. }
  1222. if (dither) {
  1223. /* floyd-steinberg dither */
  1224. int* errors;
  1225. errors = calloc(imIn->xsize + 1, sizeof(int) * 3);
  1226. if (!errors) {
  1227. ImagingDelete(imOut);
  1228. return ImagingError_MemoryError();
  1229. }
  1230. /* Map each pixel to the nearest palette entry */
  1231. ImagingSectionEnter(&cookie);
  1232. for (y = 0; y < imIn->ysize; y++) {
  1233. int r, r0, r1, r2;
  1234. int g, g0, g1, g2;
  1235. int b, b0, b1, b2;
  1236. UINT8* in = (UINT8*) imIn->image[y];
  1237. UINT8* out = alpha ? (UINT8*) imOut->image32[y] : imOut->image8[y];
  1238. int* e = errors;
  1239. r = r0 = r1 = 0;
  1240. g = g0 = g1 = 0;
  1241. b = b0 = b1 = b2 = 0;
  1242. for (x = 0; x < imIn->xsize; x++, in += 4) {
  1243. int d2;
  1244. INT16* cache;
  1245. r = CLIP8(in[0] + (r + e[3+0])/16);
  1246. g = CLIP8(in[1] + (g + e[3+1])/16);
  1247. b = CLIP8(in[2] + (b + e[3+2])/16);
  1248. /* get closest colour */
  1249. cache = &ImagingPaletteCache(palette, r, g, b);
  1250. if (cache[0] == 0x100)
  1251. ImagingPaletteCacheUpdate(palette, r, g, b);
  1252. if (alpha) {
  1253. out[x*4] = out[x*4+1] = out[x*4+2] = (UINT8) cache[0];
  1254. out[x*4+3] = 255;
  1255. } else {
  1256. out[x] = (UINT8) cache[0];
  1257. }
  1258. r -= (int) palette->palette[cache[0]*4];
  1259. g -= (int) palette->palette[cache[0]*4+1];
  1260. b -= (int) palette->palette[cache[0]*4+2];
  1261. /* propagate errors (don't ask ;-) */
  1262. r2 = r; d2 = r + r; r += d2; e[0] = r + r0;
  1263. r += d2; r0 = r + r1; r1 = r2; r += d2;
  1264. g2 = g; d2 = g + g; g += d2; e[1] = g + g0;
  1265. g += d2; g0 = g + g1; g1 = g2; g += d2;
  1266. b2 = b; d2 = b + b; b += d2; e[2] = b + b0;
  1267. b += d2; b0 = b + b1; b1 = b2; b += d2;
  1268. e += 3;
  1269. }
  1270. e[0] = b0;
  1271. e[1] = b1;
  1272. e[2] = b2;
  1273. }
  1274. ImagingSectionLeave(&cookie);
  1275. free(errors);
  1276. } else {
  1277. /* closest colour */
  1278. ImagingSectionEnter(&cookie);
  1279. for (y = 0; y < imIn->ysize; y++) {
  1280. int r, g, b;
  1281. UINT8* in = (UINT8*) imIn->image[y];
  1282. UINT8* out = alpha ? (UINT8*) imOut->image32[y] : imOut->image8[y];
  1283. for (x = 0; x < imIn->xsize; x++, in += 4) {
  1284. INT16* cache;
  1285. r = in[0]; g = in[1]; b = in[2];
  1286. /* get closest colour */
  1287. cache = &ImagingPaletteCache(palette, r, g, b);
  1288. if (cache[0] == 0x100)
  1289. ImagingPaletteCacheUpdate(palette, r, g, b);
  1290. if (alpha) {
  1291. out[x*4] = out[x*4+1] = out[x*4+2] = (UINT8) cache[0];
  1292. out[x*4+3] = 255;
  1293. } else {
  1294. out[x] = (UINT8) cache[0];
  1295. }
  1296. }
  1297. }
  1298. ImagingSectionLeave(&cookie);
  1299. }
  1300. if (inpalette != palette)
  1301. ImagingPaletteCacheDelete(palette);
  1302. }
  1303. if (inpalette != palette)
  1304. ImagingPaletteDelete(palette);
  1305. return imOut;
  1306. }
  1307. static Imaging
  1308. tobilevel(Imaging imOut, Imaging imIn, int dither)
  1309. {
  1310. ImagingSectionCookie cookie;
  1311. int x, y;
  1312. int* errors;
  1313. /* Map L or RGB to dithered 1 image */
  1314. if (strcmp(imIn->mode, "L") != 0 && strcmp(imIn->mode, "RGB") != 0)
  1315. return (Imaging) ImagingError_ValueError("conversion not supported");
  1316. imOut = ImagingNew2Dirty("1", imOut, imIn);
  1317. if (!imOut)
  1318. return NULL;
  1319. errors = calloc(imIn->xsize + 1, sizeof(int));
  1320. if (!errors) {
  1321. ImagingDelete(imOut);
  1322. return ImagingError_MemoryError();
  1323. }
  1324. if (imIn->bands == 1) {
  1325. /* map each pixel to black or white, using error diffusion */
  1326. ImagingSectionEnter(&cookie);
  1327. for (y = 0; y < imIn->ysize; y++) {
  1328. int l, l0, l1, l2, d2;
  1329. UINT8* in = (UINT8*) imIn->image[y];
  1330. UINT8* out = imOut->image8[y];
  1331. l = l0 = l1 = 0;
  1332. for (x = 0; x < imIn->xsize; x++) {
  1333. /* pick closest colour */
  1334. l = CLIP8(in[x] + (l + errors[x+1])/16);
  1335. out[x] = (l > 128) ? 255 : 0;
  1336. /* propagate errors */
  1337. l -= (int) out[x];
  1338. l2 = l; d2 = l + l; l += d2; errors[x] = l + l0;
  1339. l += d2; l0 = l + l1; l1 = l2; l += d2;
  1340. }
  1341. errors[x] = l0;
  1342. }
  1343. ImagingSectionLeave(&cookie);
  1344. } else {
  1345. /* map each pixel to black or white, using error diffusion */
  1346. ImagingSectionEnter(&cookie);
  1347. for (y = 0; y < imIn->ysize; y++) {
  1348. int l, l0, l1, l2, d2;
  1349. UINT8* in = (UINT8*) imIn->image[y];
  1350. UINT8* out = imOut->image8[y];
  1351. l = l0 = l1 = 0;
  1352. for (x = 0; x < imIn->xsize; x++, in += 4) {
  1353. /* pick closest colour */
  1354. l = CLIP8(L(in)/1000 + (l + errors[x+1])/16);
  1355. out[x] = (l > 128) ? 255 : 0;
  1356. /* propagate errors */
  1357. l -= (int) out[x];
  1358. l2 = l; d2 = l + l; l += d2; errors[x] = l + l0;
  1359. l += d2; l0 = l + l1; l1 = l2; l += d2;
  1360. }
  1361. errors[x] = l0;
  1362. }
  1363. ImagingSectionLeave(&cookie);
  1364. }
  1365. free(errors);
  1366. return imOut;
  1367. }
  1368. #if defined(_MSC_VER)
  1369. #pragma optimize("", on)
  1370. #endif
  1371. static Imaging
  1372. convert(Imaging imOut, Imaging imIn, const char *mode,
  1373. ImagingPalette palette, int dither)
  1374. {
  1375. ImagingSectionCookie cookie;
  1376. ImagingShuffler convert;
  1377. int y;
  1378. if (!imIn)
  1379. return (Imaging) ImagingError_ModeError();
  1380. if (!mode) {
  1381. /* Map palette image to full depth */
  1382. if (!imIn->palette)
  1383. return (Imaging) ImagingError_ModeError();
  1384. mode = imIn->palette->mode;
  1385. } else
  1386. /* Same mode? */
  1387. if (!strcmp(imIn->mode, mode))
  1388. return ImagingCopy2(imOut, imIn);
  1389. /* test for special conversions */
  1390. if (strcmp(imIn->mode, "P") == 0 || strcmp(imIn->mode, "PA") == 0)
  1391. return frompalette(imOut, imIn, mode);
  1392. if (strcmp(mode, "P") == 0 || strcmp(mode, "PA") == 0)
  1393. return topalette(imOut, imIn, mode, palette, dither);
  1394. if (dither && strcmp(mode, "1") == 0)
  1395. return tobilevel(imOut, imIn, dither);
  1396. /* standard conversion machinery */
  1397. convert = NULL;
  1398. for (y = 0; converters[y].from; y++)
  1399. if (!strcmp(imIn->mode, converters[y].from) &&
  1400. !strcmp(mode, converters[y].to)) {
  1401. convert = converters[y].convert;
  1402. break;
  1403. }
  1404. if (!convert)
  1405. #ifdef notdef
  1406. return (Imaging) ImagingError_ValueError("conversion not supported");
  1407. #else
  1408. {
  1409. static char buf[256];
  1410. /* FIXME: may overflow if mode is too large */
  1411. sprintf(buf, "conversion from %s to %s not supported", imIn->mode, mode);
  1412. return (Imaging) ImagingError_ValueError(buf);
  1413. }
  1414. #endif
  1415. imOut = ImagingNew2Dirty(mode, imOut, imIn);
  1416. if (!imOut)
  1417. return NULL;
  1418. ImagingSectionEnter(&cookie);
  1419. for (y = 0; y < imIn->ysize; y++)
  1420. (*convert)((UINT8*) imOut->image[y], (UINT8*) imIn->image[y],
  1421. imIn->xsize);
  1422. ImagingSectionLeave(&cookie);
  1423. return imOut;
  1424. }
  1425. Imaging
  1426. ImagingConvert(Imaging imIn, const char *mode,
  1427. ImagingPalette palette, int dither)
  1428. {
  1429. return convert(NULL, imIn, mode, palette, dither);
  1430. }
  1431. Imaging
  1432. ImagingConvert2(Imaging imOut, Imaging imIn)
  1433. {
  1434. return convert(imOut, imIn, imOut->mode, NULL, 0);
  1435. }
  1436. Imaging
  1437. ImagingConvertTransparent(Imaging imIn, const char *mode,
  1438. int r, int g, int b)
  1439. {
  1440. ImagingSectionCookie cookie;
  1441. ImagingShuffler convert;
  1442. Imaging imOut = NULL;
  1443. int y;
  1444. if (!imIn){
  1445. return (Imaging) ImagingError_ModeError();
  1446. }
  1447. if (!((strcmp(imIn->mode, "RGB") == 0 ||
  1448. strcmp(imIn->mode, "1") == 0 ||
  1449. strcmp(imIn->mode, "I") == 0 ||
  1450. strcmp(imIn->mode, "L") == 0)
  1451. && strcmp(mode, "RGBA") == 0))
  1452. #ifdef notdef
  1453. {
  1454. return (Imaging) ImagingError_ValueError("conversion not supported");
  1455. }
  1456. #else
  1457. {
  1458. static char buf[256];
  1459. /* FIXME: may overflow if mode is too large */
  1460. sprintf(buf, "conversion from %s to %s not supported in convert_transparent", imIn->mode, mode);
  1461. return (Imaging) ImagingError_ValueError(buf);
  1462. }
  1463. #endif
  1464. if (strcmp(imIn->mode, "RGB") == 0) {
  1465. convert = rgb2rgba;
  1466. } else {
  1467. if (strcmp(imIn->mode, "1") == 0) {
  1468. convert = bit2rgb;
  1469. } else if (strcmp(imIn->mode, "I") == 0) {
  1470. convert = i2rgb;
  1471. } else {
  1472. convert = l2rgb;
  1473. }
  1474. g = b = r;
  1475. }
  1476. imOut = ImagingNew2Dirty(mode, imOut, imIn);
  1477. if (!imOut){
  1478. return NULL;
  1479. }
  1480. ImagingSectionEnter(&cookie);
  1481. for (y = 0; y < imIn->ysize; y++) {
  1482. (*convert)((UINT8*) imOut->image[y], (UINT8*) imIn->image[y],
  1483. imIn->xsize);
  1484. rgbT2rgba((UINT8*) imOut->image[y], imIn->xsize, r, g, b);
  1485. }
  1486. ImagingSectionLeave(&cookie);
  1487. return imOut;
  1488. }
  1489. Imaging
  1490. ImagingConvertInPlace(Imaging imIn, const char* mode)
  1491. {
  1492. ImagingSectionCookie cookie;
  1493. ImagingShuffler convert;
  1494. int y;
  1495. /* limited support for inplace conversion */
  1496. if (strcmp(imIn->mode, "L") == 0 && strcmp(mode, "1") == 0)
  1497. convert = l2bit;
  1498. else if (strcmp(imIn->mode, "1") == 0 && strcmp(mode, "L") == 0)
  1499. convert = bit2l;
  1500. else
  1501. return ImagingError_ModeError();
  1502. ImagingSectionEnter(&cookie);
  1503. for (y = 0; y < imIn->ysize; y++)
  1504. (*convert)((UINT8*) imIn->image[y], (UINT8*) imIn->image[y],
  1505. imIn->xsize);
  1506. ImagingSectionLeave(&cookie);
  1507. return imIn;
  1508. }