Convert.c 44 KB

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