color-fast.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736
  1. // Copyright (C) 2004-2024 Artifex Software, Inc.
  2. //
  3. // This file is part of MuPDF.
  4. //
  5. // MuPDF is free software: you can redistribute it and/or modify it under the
  6. // terms of the GNU Affero General Public License as published by the Free
  7. // Software Foundation, either version 3 of the License, or (at your option)
  8. // any later version.
  9. //
  10. // MuPDF is distributed in the hope that it will be useful, but WITHOUT ANY
  11. // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  12. // FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
  13. // details.
  14. //
  15. // You should have received a copy of the GNU Affero General Public License
  16. // along with MuPDF. If not, see <https://www.gnu.org/licenses/agpl-3.0.en.html>
  17. //
  18. // Alternative licensing terms are available from the licensor.
  19. // For commercial licensing, see <https://www.artifex.com/> or contact
  20. // Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco,
  21. // CA 94129, USA, for further information.
  22. #include "mupdf/fitz.h"
  23. #include "color-imp.h"
  24. #include <math.h>
  25. /* Fast color transforms */
  26. static void gray_to_gray(fz_context *ctx, fz_color_converter *cc, const float *gray, float *xyz)
  27. {
  28. xyz[0] = gray[0];
  29. }
  30. static void gray_to_rgb(fz_context *ctx, fz_color_converter *cc, const float *gray, float *rgb)
  31. {
  32. rgb[0] = gray[0];
  33. rgb[1] = gray[0];
  34. rgb[2] = gray[0];
  35. }
  36. static void rgb_to_gray(fz_context *ctx, fz_color_converter *cc, const float *rgb, float *gray)
  37. {
  38. gray[0] = rgb[0] * 0.3f + rgb[1] * 0.59f + rgb[2] * 0.11f;
  39. }
  40. static void bgr_to_gray(fz_context *ctx, fz_color_converter *cc, const float *bgr, float *gray)
  41. {
  42. gray[0] = bgr[0] * 0.11f + bgr[1] * 0.59f + bgr[2] * 0.3f;
  43. }
  44. static void rgb_to_rgb(fz_context *ctx, fz_color_converter *cc, const float *rgb, float *xyz)
  45. {
  46. xyz[0] = rgb[0];
  47. xyz[1] = rgb[1];
  48. xyz[2] = rgb[2];
  49. }
  50. static void rgb_to_bgr(fz_context *ctx, fz_color_converter *cc, const float *rgb, float *bgr)
  51. {
  52. bgr[0] = rgb[2];
  53. bgr[1] = rgb[1];
  54. bgr[2] = rgb[0];
  55. }
  56. static void cmyk_to_cmyk(fz_context *ctx, fz_color_converter *cc, const float *cmyk, float *xyz)
  57. {
  58. xyz[0] = cmyk[0];
  59. xyz[1] = cmyk[1];
  60. xyz[2] = cmyk[2];
  61. xyz[3] = cmyk[3];
  62. }
  63. static void gray_to_cmyk(fz_context *ctx, fz_color_converter *cc, const float *gray, float *cmyk)
  64. {
  65. cmyk[0] = 0;
  66. cmyk[1] = 0;
  67. cmyk[2] = 0;
  68. cmyk[3] = 1 - gray[0];
  69. }
  70. static void cmyk_to_gray(fz_context *ctx, fz_color_converter *cc, const float *cmyk, float *gray)
  71. {
  72. float c = cmyk[0] * 0.3f;
  73. float m = cmyk[1] * 0.59f;
  74. float y = cmyk[2] * 0.11f;
  75. gray[0] = 1 - fz_min(c + m + y + cmyk[3], 1);
  76. }
  77. static void rgb_to_cmyk(fz_context *ctx, fz_color_converter *cc, const float *rgb, float *cmyk)
  78. {
  79. float c, m, y, k;
  80. c = 1 - rgb[0];
  81. m = 1 - rgb[1];
  82. y = 1 - rgb[2];
  83. k = fz_min(c, fz_min(m, y));
  84. cmyk[0] = c - k;
  85. cmyk[1] = m - k;
  86. cmyk[2] = y - k;
  87. cmyk[3] = k;
  88. }
  89. static void bgr_to_cmyk(fz_context *ctx, fz_color_converter *cc, const float *bgr, float *cmyk)
  90. {
  91. float c, m, y, k;
  92. c = 1 - bgr[2];
  93. m = 1 - bgr[1];
  94. y = 1 - bgr[0];
  95. k = fz_min(c, fz_min(m, y));
  96. cmyk[0] = c - k;
  97. cmyk[1] = m - k;
  98. cmyk[2] = y - k;
  99. cmyk[3] = k;
  100. }
  101. static void cmyk_to_rgb(fz_context *ctx, fz_color_converter *cc, const float *cmyk, float *rgb)
  102. {
  103. rgb[0] = 1 - fz_min(1, cmyk[0] + cmyk[3]);
  104. rgb[1] = 1 - fz_min(1, cmyk[1] + cmyk[3]);
  105. rgb[2] = 1 - fz_min(1, cmyk[2] + cmyk[3]);
  106. }
  107. static void cmyk_to_bgr(fz_context *ctx, fz_color_converter *cc, const float *cmyk, float *bgr)
  108. {
  109. bgr[0] = 1 - fz_min(cmyk[2] + cmyk[3], 1);
  110. bgr[1] = 1 - fz_min(cmyk[1] + cmyk[3], 1);
  111. bgr[2] = 1 - fz_min(cmyk[0] + cmyk[3], 1);
  112. }
  113. static inline float fung(float x)
  114. {
  115. if (x >= 6.0f / 29.0f)
  116. return x * x * x;
  117. return (108.0f / 841.0f) * (x - (4.0f / 29.0f));
  118. }
  119. static void lab_to_rgb(fz_context *ctx, fz_color_converter *cc, const float *lab, float *rgb)
  120. {
  121. /* input is in range (0..100, -128..127, -128..127) not (0..1, 0..1, 0..1) */
  122. float lstar, astar, bstar, l, m, n, x, y, z, r, g, b;
  123. lstar = lab[0];
  124. astar = lab[1];
  125. bstar = lab[2];
  126. m = (lstar + 16) / 116;
  127. l = m + astar / 500;
  128. n = m - bstar / 200;
  129. x = fung(l);
  130. y = fung(m);
  131. z = fung(n);
  132. r = (3.240449f * x + -1.537136f * y + -0.498531f * z) * 0.830026f;
  133. g = (-0.969265f * x + 1.876011f * y + 0.041556f * z) * 1.05452f;
  134. b = (0.055643f * x + -0.204026f * y + 1.057229f * z) * 1.1003f;
  135. rgb[0] = sqrtf(fz_clamp(r, 0, 1));
  136. rgb[1] = sqrtf(fz_clamp(g, 0, 1));
  137. rgb[2] = sqrtf(fz_clamp(b, 0, 1));
  138. }
  139. static void lab_to_gray(fz_context *ctx, fz_color_converter *cc, const float *lab, float *gray)
  140. {
  141. gray[0] = lab[0] / 100;
  142. }
  143. static void lab_to_bgr(fz_context *ctx, fz_color_converter *cc, const float *lab, float *bgr)
  144. {
  145. float rgb[3];
  146. lab_to_rgb(ctx, cc, lab, rgb);
  147. rgb_to_bgr(ctx, cc, rgb, bgr);
  148. }
  149. static void lab_to_cmyk(fz_context *ctx, fz_color_converter *cc, const float *lab, float *cmyk)
  150. {
  151. float rgb[3];
  152. lab_to_rgb(ctx, cc, lab, rgb);
  153. rgb_to_cmyk(ctx, cc, rgb, cmyk);
  154. }
  155. fz_color_convert_fn *
  156. fz_lookup_fast_color_converter(fz_context *ctx, fz_colorspace *ss, fz_colorspace *ds)
  157. {
  158. int stype = ss->type;
  159. int dtype = ds->type;
  160. if (stype == FZ_COLORSPACE_GRAY)
  161. {
  162. if (dtype == FZ_COLORSPACE_GRAY) return gray_to_gray;
  163. if (dtype == FZ_COLORSPACE_RGB) return gray_to_rgb;
  164. if (dtype == FZ_COLORSPACE_BGR) return gray_to_rgb;
  165. if (dtype == FZ_COLORSPACE_CMYK) return gray_to_cmyk;
  166. }
  167. else if (stype == FZ_COLORSPACE_RGB)
  168. {
  169. if (dtype == FZ_COLORSPACE_GRAY) return rgb_to_gray;
  170. if (dtype == FZ_COLORSPACE_RGB) return rgb_to_rgb;
  171. if (dtype == FZ_COLORSPACE_BGR) return rgb_to_bgr;
  172. if (dtype == FZ_COLORSPACE_CMYK) return rgb_to_cmyk;
  173. }
  174. else if (stype == FZ_COLORSPACE_BGR)
  175. {
  176. if (dtype == FZ_COLORSPACE_GRAY) return bgr_to_gray;
  177. if (dtype == FZ_COLORSPACE_RGB) return rgb_to_bgr;
  178. if (dtype == FZ_COLORSPACE_BGR) return rgb_to_rgb;
  179. if (dtype == FZ_COLORSPACE_CMYK) return bgr_to_cmyk;
  180. }
  181. else if (stype == FZ_COLORSPACE_CMYK)
  182. {
  183. if (dtype == FZ_COLORSPACE_GRAY) return cmyk_to_gray;
  184. if (dtype == FZ_COLORSPACE_RGB) return cmyk_to_rgb;
  185. if (dtype == FZ_COLORSPACE_BGR) return cmyk_to_bgr;
  186. if (dtype == FZ_COLORSPACE_CMYK) return cmyk_to_cmyk;
  187. }
  188. else if (stype == FZ_COLORSPACE_LAB)
  189. {
  190. if (dtype == FZ_COLORSPACE_GRAY) return lab_to_gray;
  191. if (dtype == FZ_COLORSPACE_RGB) return lab_to_rgb;
  192. if (dtype == FZ_COLORSPACE_BGR) return lab_to_bgr;
  193. if (dtype == FZ_COLORSPACE_CMYK) return lab_to_cmyk;
  194. }
  195. fz_throw(ctx, FZ_ERROR_ARGUMENT, "cannot find color converter");
  196. }
  197. /* Fast pixmap color conversions */
  198. static void fast_gray_to_rgb(fz_context *ctx, const fz_pixmap *src, fz_pixmap *dst, int copy_spots)
  199. {
  200. unsigned char *s = src->samples;
  201. unsigned char *d = dst->samples;
  202. size_t w = src->w;
  203. int h = src->h;
  204. int sn = src->n;
  205. int ss = src->s;
  206. int sa = src->alpha;
  207. int dn = dst->n;
  208. int ds = dst->s;
  209. int da = dst->alpha;
  210. ptrdiff_t d_line_inc = dst->stride - w * dn;
  211. ptrdiff_t s_line_inc = src->stride - w * sn;
  212. /* If copying spots, they must match, and we can never drop alpha (but we can invent it) */
  213. if (copy_spots && ss != ds)
  214. fz_throw(ctx, FZ_ERROR_ARGUMENT, "incompatible number of spots when converting pixmap");
  215. if (!da && sa)
  216. fz_throw(ctx, FZ_ERROR_ARGUMENT, "cannot drop alpha when converting pixmap");
  217. if ((int)w < 0 || h < 0)
  218. return;
  219. if (d_line_inc == 0 && s_line_inc == 0)
  220. {
  221. w *= h;
  222. h = 1;
  223. }
  224. if (ss == 0 && ds == 0)
  225. {
  226. /* Common, no spots case */
  227. if (da)
  228. {
  229. if (sa)
  230. {
  231. while (h--)
  232. {
  233. size_t ww = w;
  234. while (ww--)
  235. {
  236. d[0] = s[0];
  237. d[1] = s[0];
  238. d[2] = s[0];
  239. d[3] = s[1];
  240. s += 2;
  241. d += 4;
  242. }
  243. d += d_line_inc;
  244. s += s_line_inc;
  245. }
  246. }
  247. else
  248. {
  249. while (h--)
  250. {
  251. size_t ww = w;
  252. while (ww--)
  253. {
  254. d[0] = s[0];
  255. d[1] = s[0];
  256. d[2] = s[0];
  257. d[3] = 255;
  258. s++;
  259. d += 4;
  260. }
  261. d += d_line_inc;
  262. s += s_line_inc;
  263. }
  264. }
  265. }
  266. else
  267. {
  268. while (h--)
  269. {
  270. size_t ww = w;
  271. while (ww--)
  272. {
  273. d[0] = s[0];
  274. d[1] = s[0];
  275. d[2] = s[0];
  276. s++;
  277. d += 3;
  278. }
  279. d += d_line_inc;
  280. s += s_line_inc;
  281. }
  282. }
  283. }
  284. else if (copy_spots)
  285. {
  286. /* Slower, spots capable version */
  287. int i;
  288. while (h--)
  289. {
  290. size_t ww = w;
  291. while (ww--)
  292. {
  293. d[0] = s[0];
  294. d[1] = s[0];
  295. d[2] = s[0];
  296. s += 1;
  297. d += 3;
  298. for (i=ss; i > 0; i--)
  299. *d++ = *s++;
  300. if (da)
  301. *d++ = sa ? *s++ : 255;
  302. }
  303. d += d_line_inc;
  304. s += s_line_inc;
  305. }
  306. }
  307. else
  308. {
  309. while (h--)
  310. {
  311. size_t ww = w;
  312. while (ww--)
  313. {
  314. d[0] = s[0];
  315. d[1] = s[0];
  316. d[2] = s[0];
  317. s += sn;
  318. d += dn;
  319. if (da)
  320. d[-1] = sa ? s[-1] : 255;
  321. }
  322. d += d_line_inc;
  323. s += s_line_inc;
  324. }
  325. }
  326. }
  327. static void fast_gray_to_cmyk(fz_context *ctx, const fz_pixmap *src, fz_pixmap *dst, int copy_spots)
  328. {
  329. unsigned char *s = src->samples;
  330. unsigned char *d = dst->samples;
  331. size_t w = src->w;
  332. int h = src->h;
  333. int sn = src->n;
  334. int ss = src->s;
  335. int sa = src->alpha;
  336. int dn = dst->n;
  337. int ds = dst->s;
  338. int da = dst->alpha;
  339. ptrdiff_t d_line_inc = dst->stride - w * dn;
  340. ptrdiff_t s_line_inc = src->stride - w * sn;
  341. int k, g;
  342. int a = 255;
  343. int i;
  344. if (copy_spots && ss != ds)
  345. fz_throw(ctx, FZ_ERROR_ARGUMENT, "incompatible number of spots when converting pixmap");
  346. if ((int)w < 0 || h < 0)
  347. fz_throw(ctx, FZ_ERROR_LIMIT, "integer overflow");
  348. while (h--)
  349. {
  350. size_t ww = w;
  351. while (ww--)
  352. {
  353. g = s[0];
  354. if (sa)
  355. {
  356. a = s[1+ss];
  357. g = fz_div255(g, a);
  358. }
  359. k = 255 - g;
  360. if (da)
  361. {
  362. *d++ = 0;
  363. *d++ = 0;
  364. *d++ = 0;
  365. *d++ = fz_mul255(k, a);
  366. }
  367. else
  368. {
  369. *d++ = 0;
  370. *d++ = 0;
  371. *d++ = 0;
  372. *d++ = k;
  373. }
  374. if (copy_spots)
  375. {
  376. s += 1;
  377. for (i=ss; i > 0; --i)
  378. *d++ = *s++;
  379. s += sa;
  380. }
  381. else
  382. {
  383. s += 1 + ss + sa;
  384. d += ds;
  385. }
  386. if (da)
  387. {
  388. *d++ = a;
  389. }
  390. }
  391. d += d_line_inc;
  392. s += s_line_inc;
  393. }
  394. }
  395. static void fast_rgb_to_gray(fz_context *ctx, const fz_pixmap *src, fz_pixmap *dst, int copy_spots)
  396. {
  397. unsigned char *s = src->samples;
  398. unsigned char *d = dst->samples;
  399. size_t w = src->w;
  400. int h = src->h;
  401. int sn = src->n;
  402. int ss = src->s;
  403. int sa = src->alpha;
  404. int dn = dst->n;
  405. int ds = dst->s;
  406. int da = dst->alpha;
  407. ptrdiff_t d_line_inc = dst->stride - w * dn;
  408. ptrdiff_t s_line_inc = src->stride - w * sn;
  409. /* If copying spots, they must match, and we can never drop alpha (but we can invent it) */
  410. if (copy_spots && ss != ds)
  411. fz_throw(ctx, FZ_ERROR_ARGUMENT, "incompatible number of spots when converting pixmap");
  412. if (!da && sa)
  413. fz_throw(ctx, FZ_ERROR_ARGUMENT, "cannot drop alpha when converting pixmap");
  414. if ((int)w < 0 || h < 0)
  415. return;
  416. if (d_line_inc == 0 && s_line_inc == 0)
  417. {
  418. w *= h;
  419. h = 1;
  420. }
  421. if (ss == 0 && ds == 0)
  422. {
  423. /* Common, no spots case */
  424. if (da)
  425. {
  426. if (sa)
  427. {
  428. while (h--)
  429. {
  430. size_t ww = w;
  431. while (ww--)
  432. {
  433. d[0] = ((s[0]+1) * 77 + (s[1]+1) * 150 + (s[2]+1) * 28) >> 8;
  434. d[1] = s[3];
  435. s += 4;
  436. d += 2;
  437. }
  438. d += d_line_inc;
  439. s += s_line_inc;
  440. }
  441. }
  442. else
  443. {
  444. while (h--)
  445. {
  446. size_t ww = w;
  447. while (ww--)
  448. {
  449. d[0] = ((s[0]+1) * 77 + (s[1]+1) * 150 + (s[2]+1) * 28) >> 8;
  450. d[1] = 255;
  451. s += 3;
  452. d += 2;
  453. }
  454. d += d_line_inc;
  455. s += s_line_inc;
  456. }
  457. }
  458. }
  459. else
  460. {
  461. while (h--)
  462. {
  463. size_t ww = w;
  464. while (ww--)
  465. {
  466. d[0] = ((s[0]+1) * 77 + (s[1]+1) * 150 + (s[2]+1) * 28) >> 8;
  467. s += 3;
  468. d++;
  469. }
  470. d += d_line_inc;
  471. s += s_line_inc;
  472. }
  473. }
  474. }
  475. else if (copy_spots)
  476. {
  477. /* Slower, spots capable version */
  478. int i;
  479. while (h--)
  480. {
  481. size_t ww = w;
  482. while (ww--)
  483. {
  484. d[0] = ((s[0]+1) * 77 + (s[1]+1) * 150 + (s[2]+1) * 28) >> 8;
  485. s += 3;
  486. d++;
  487. for (i=ss; i > 0; i--)
  488. *d++ = *s++;
  489. if (da)
  490. *d++ = sa ? *s++ : 255;
  491. }
  492. d += d_line_inc;
  493. s += s_line_inc;
  494. }
  495. }
  496. else
  497. {
  498. while (h--)
  499. {
  500. size_t ww = w;
  501. while (ww--)
  502. {
  503. d[0] = ((s[0]+1) * 77 + (s[1]+1) * 150 + (s[2]+1) * 28) >> 8;
  504. s += sn;
  505. d += dn;
  506. if (da)
  507. d[-1] = sa ? s[-1] : 255;
  508. }
  509. d += d_line_inc;
  510. s += s_line_inc;
  511. }
  512. }
  513. }
  514. static void fast_bgr_to_gray(fz_context *ctx, const fz_pixmap *src, fz_pixmap *dst, int copy_spots)
  515. {
  516. unsigned char *s = src->samples;
  517. unsigned char *d = dst->samples;
  518. size_t w = src->w;
  519. int h = src->h;
  520. int sn = src->n;
  521. int ss = src->s;
  522. int sa = src->alpha;
  523. int dn = dst->n;
  524. int ds = dst->s;
  525. int da = dst->alpha;
  526. ptrdiff_t d_line_inc = dst->stride - w * dn;
  527. ptrdiff_t s_line_inc = src->stride - w * sn;
  528. /* If copying spots, they must match, and we can never drop alpha (but we can invent it) */
  529. if (copy_spots && ss != ds)
  530. fz_throw(ctx, FZ_ERROR_ARGUMENT, "incompatible number of spots when converting pixmap");
  531. if (!da && sa)
  532. fz_throw(ctx, FZ_ERROR_ARGUMENT, "cannot drop alpha when converting pixmap");
  533. if ((int)w < 0 || h < 0)
  534. return;
  535. if (d_line_inc == 0 && s_line_inc == 0)
  536. {
  537. w *= h;
  538. h = 1;
  539. }
  540. if (ss == 0 && ds == 0)
  541. {
  542. /* Common, no spots case */
  543. if (da)
  544. {
  545. if (sa)
  546. {
  547. while (h--)
  548. {
  549. size_t ww = w;
  550. while (ww--)
  551. {
  552. d[0] = ((s[0]+1) * 28 + (s[1]+1) * 150 + (s[2]+1) * 77) >> 8;
  553. d[1] = s[3];
  554. s += 4;
  555. d += 2;
  556. }
  557. d += d_line_inc;
  558. s += s_line_inc;
  559. }
  560. }
  561. else
  562. {
  563. while (h--)
  564. {
  565. size_t ww = w;
  566. while (ww--)
  567. {
  568. d[0] = ((s[0]+1) * 28 + (s[1]+1) * 150 + (s[2]+1) * 77) >> 8;
  569. d[1] = 255;
  570. s += 3;
  571. d += 2;
  572. }
  573. d += d_line_inc;
  574. s += s_line_inc;
  575. }
  576. }
  577. }
  578. else
  579. {
  580. int si = 3 + src->alpha;
  581. while (h--)
  582. {
  583. size_t ww = w;
  584. while (ww--)
  585. {
  586. d[0] = ((s[0]+1) * 28 + (s[1]+1) * 150 + (s[2]+1) * 77) >> 8;
  587. s += si;
  588. d++;
  589. }
  590. d += d_line_inc;
  591. s += s_line_inc;
  592. }
  593. }
  594. }
  595. else if (copy_spots)
  596. {
  597. /* Slower, spots capable version */
  598. while (h--)
  599. {
  600. int i;
  601. size_t ww = w;
  602. while (ww--)
  603. {
  604. d[0] = ((s[0]+1) * 28 + (s[1]+1) * 150 + (s[2]+1) * 77) >> 8;
  605. s += 3;
  606. d++;
  607. for (i=ss; i > 0; i--)
  608. *d++ = *s++;
  609. if (da)
  610. *d++ = sa ? *s++ : 255;
  611. }
  612. d += d_line_inc;
  613. s += s_line_inc;
  614. }
  615. }
  616. else
  617. {
  618. /* Slower, spots capable version */
  619. while (h--)
  620. {
  621. size_t ww = w;
  622. while (ww--)
  623. {
  624. d[0] = ((s[0]+1) * 28 + (s[1]+1) * 150 + (s[2]+1) * 77) >> 8;
  625. s += sn;
  626. d += dn;
  627. if (da)
  628. d[-1] = sa ? s[-1] : 255;
  629. }
  630. d += d_line_inc;
  631. s += s_line_inc;
  632. }
  633. }
  634. }
  635. static void fast_rgb_to_cmyk(fz_context *ctx, const fz_pixmap *src, fz_pixmap *dst, int copy_spots)
  636. {
  637. unsigned char *s = src->samples;
  638. unsigned char *d = dst->samples;
  639. size_t w = src->w;
  640. int h = src->h;
  641. int sn = src->n;
  642. int ss = src->s;
  643. int sa = src->alpha;
  644. int dn = dst->n;
  645. int ds = dst->s;
  646. int da = dst->alpha;
  647. ptrdiff_t d_line_inc = dst->stride - w * dn;
  648. ptrdiff_t s_line_inc = src->stride - w * sn;
  649. int c, m, y, k, r, g, b;
  650. int a = 255;
  651. int i;
  652. if (copy_spots && ss != ds)
  653. fz_throw(ctx, FZ_ERROR_ARGUMENT, "incompatible number of spots when converting pixmap");
  654. if ((int)w < 0 || h < 0)
  655. fz_throw(ctx, FZ_ERROR_LIMIT, "integer overflow");
  656. while (h--)
  657. {
  658. size_t ww = w;
  659. while (ww--)
  660. {
  661. r = s[0];
  662. g = s[1];
  663. b = s[2];
  664. if (sa)
  665. {
  666. a = s[3+ss];
  667. r = fz_div255(r, a);
  668. g = fz_div255(g, a);
  669. b = fz_div255(b, a);
  670. }
  671. c = 255 - r;
  672. m = 255 - g;
  673. y = 255 - b;
  674. k = fz_mini(c, fz_mini(m, y));
  675. c = c - k;
  676. m = m - k;
  677. y = y - k;
  678. if (da)
  679. {
  680. *d++ = fz_mul255(c, a);
  681. *d++ = fz_mul255(m, a);
  682. *d++ = fz_mul255(y, a);
  683. *d++ = fz_mul255(k, a);
  684. }
  685. else
  686. {
  687. *d++ = c;
  688. *d++ = m;
  689. *d++ = y;
  690. *d++ = k;
  691. }
  692. if (copy_spots)
  693. {
  694. s += 3;
  695. for (i=ss; i > 0; --i)
  696. *d++ = *s++;
  697. s += sa;
  698. }
  699. else
  700. {
  701. s += 3 + ss + sa;
  702. d += ds;
  703. }
  704. if (da)
  705. {
  706. *d++ = a;
  707. }
  708. }
  709. d += d_line_inc;
  710. s += s_line_inc;
  711. }
  712. }
  713. static void fast_bgr_to_cmyk(fz_context *ctx, const fz_pixmap *src, fz_pixmap *dst, int copy_spots)
  714. {
  715. unsigned char *s = src->samples;
  716. unsigned char *d = dst->samples;
  717. size_t w = src->w;
  718. int h = src->h;
  719. int sn = src->n;
  720. int ss = src->s;
  721. int sa = src->alpha;
  722. int dn = dst->n;
  723. int ds = dst->s;
  724. int da = dst->alpha;
  725. ptrdiff_t d_line_inc = dst->stride - w * dn;
  726. ptrdiff_t s_line_inc = src->stride - w * sn;
  727. int c, m, y, k, r, g, b;
  728. int a = 255;
  729. int i;
  730. if (copy_spots && ss != ds)
  731. fz_throw(ctx, FZ_ERROR_ARGUMENT, "incompatible number of spots when converting pixmap");
  732. if ((int)w < 0 || h < 0)
  733. fz_throw(ctx, FZ_ERROR_LIMIT, "integer overflow");
  734. while (h--)
  735. {
  736. size_t ww = w;
  737. while (ww--)
  738. {
  739. b = s[0];
  740. g = s[1];
  741. r = s[2];
  742. if (sa)
  743. {
  744. a = s[3+ss];
  745. r = fz_div255(r, a);
  746. g = fz_div255(g, a);
  747. b = fz_div255(b, a);
  748. }
  749. c = 255 - r;
  750. m = 255 - g;
  751. y = 255 - b;
  752. k = fz_mini(c, fz_mini(m, y));
  753. c = c - k;
  754. m = m - k;
  755. y = y - k;
  756. if (da)
  757. {
  758. *d++ = fz_mul255(c, a);
  759. *d++ = fz_mul255(m, a);
  760. *d++ = fz_mul255(y, a);
  761. *d++ = fz_mul255(k, a);
  762. }
  763. else
  764. {
  765. *d++ = c;
  766. *d++ = m;
  767. *d++ = y;
  768. *d++ = k;
  769. }
  770. if (copy_spots)
  771. {
  772. s += 3;
  773. for (i=ss; i > 0; --i)
  774. *d++ = *s++;
  775. s += sa;
  776. }
  777. else
  778. {
  779. s += 3 + ss + sa;
  780. d += ds;
  781. }
  782. if (da)
  783. {
  784. *d++ = a;
  785. }
  786. }
  787. d += d_line_inc;
  788. s += s_line_inc;
  789. }
  790. }
  791. static void fast_cmyk_to_gray(fz_context *ctx, const fz_pixmap *src, fz_pixmap *dst, int copy_spots)
  792. {
  793. unsigned char *s = src->samples;
  794. unsigned char *d = dst->samples;
  795. size_t w = src->w;
  796. int h = src->h;
  797. int sn = src->n;
  798. int ss = src->s;
  799. int sa = src->alpha;
  800. int dn = dst->n;
  801. int ds = dst->s;
  802. int da = dst->alpha;
  803. ptrdiff_t d_line_inc = dst->stride - w * dn;
  804. ptrdiff_t s_line_inc = src->stride - w * sn;
  805. int c, m, y, k, g;
  806. int a = 255;
  807. int i;
  808. if (copy_spots && ss != ds)
  809. fz_throw(ctx, FZ_ERROR_ARGUMENT, "incompatible number of spots when converting pixmap");
  810. if ((int)w < 0 || h < 0)
  811. fz_throw(ctx, FZ_ERROR_LIMIT, "integer overflow");
  812. while (h--)
  813. {
  814. size_t ww = w;
  815. while (ww--)
  816. {
  817. c = s[0];
  818. m = s[1];
  819. y = s[2];
  820. k = s[3];
  821. if (sa)
  822. {
  823. a = s[4+ss];
  824. c = fz_div255(c, a);
  825. m = fz_div255(m, a);
  826. y = fz_div255(y, a);
  827. k = fz_div255(k, a);
  828. }
  829. g = 255 - fz_mini(c + m + y + k, 255);
  830. if (da)
  831. {
  832. *d++ = fz_mul255(g, a);
  833. }
  834. else
  835. {
  836. *d++ = g;
  837. }
  838. if (copy_spots)
  839. {
  840. s += 4;
  841. for (i=ss; i > 0; --i)
  842. *d++ = *s++;
  843. s += sa;
  844. }
  845. else
  846. {
  847. s += 4 + ss + sa;
  848. d += ds;
  849. }
  850. if (da)
  851. {
  852. *d++ = a;
  853. }
  854. }
  855. d += d_line_inc;
  856. s += s_line_inc;
  857. }
  858. }
  859. static void fast_cmyk_to_rgb(fz_context *ctx, const fz_pixmap *src, fz_pixmap *dst, int copy_spots)
  860. {
  861. unsigned char *s = src->samples;
  862. unsigned char *d = dst->samples;
  863. size_t w = src->w;
  864. int h = src->h;
  865. int sn = src->n;
  866. int ss = src->s;
  867. int sa = src->alpha;
  868. int dn = dst->n;
  869. int ds = dst->s;
  870. int da = dst->alpha;
  871. ptrdiff_t d_line_inc = dst->stride - w * dn;
  872. ptrdiff_t s_line_inc = src->stride - w * sn;
  873. int c, m, y, k, r, g, b;
  874. int a = 255;
  875. int i;
  876. if (copy_spots && ss != ds)
  877. fz_throw(ctx, FZ_ERROR_ARGUMENT, "incompatible number of spots when converting pixmap");
  878. if ((int)w < 0 || h < 0)
  879. fz_throw(ctx, FZ_ERROR_LIMIT, "integer overflow");
  880. while (h--)
  881. {
  882. size_t ww = w;
  883. while (ww--)
  884. {
  885. c = s[0];
  886. m = s[1];
  887. y = s[2];
  888. k = s[3];
  889. if (sa)
  890. {
  891. a = s[4+ss];
  892. c = fz_div255(c, a);
  893. m = fz_div255(m, a);
  894. y = fz_div255(y, a);
  895. k = fz_div255(k, a);
  896. }
  897. r = 255 - fz_mini(c + k, 255);
  898. g = 255 - fz_mini(m + k, 255);
  899. b = 255 - fz_mini(y + k, 255);
  900. if (da)
  901. {
  902. *d++ = fz_mul255(r, a);
  903. *d++ = fz_mul255(g, a);
  904. *d++ = fz_mul255(b, a);
  905. }
  906. else
  907. {
  908. *d++ = r;
  909. *d++ = g;
  910. *d++ = b;
  911. }
  912. if (copy_spots)
  913. {
  914. s += 4;
  915. for (i=ss; i > 0; --i)
  916. *d++ = *s++;
  917. s += sa;
  918. }
  919. else
  920. {
  921. s += 4 + ss + sa;
  922. d += ds;
  923. }
  924. if (da)
  925. {
  926. *d++ = a;
  927. }
  928. }
  929. d += d_line_inc;
  930. s += s_line_inc;
  931. }
  932. }
  933. static void fast_cmyk_to_bgr(fz_context *ctx, const fz_pixmap *src, fz_pixmap *dst, int copy_spots)
  934. {
  935. unsigned char *s = src->samples;
  936. unsigned char *d = dst->samples;
  937. size_t w = src->w;
  938. int h = src->h;
  939. int sn = src->n;
  940. int ss = src->s;
  941. int sa = src->alpha;
  942. int dn = dst->n;
  943. int ds = dst->s;
  944. int da = dst->alpha;
  945. ptrdiff_t d_line_inc = dst->stride - w * dn;
  946. ptrdiff_t s_line_inc = src->stride - w * sn;
  947. int c, m, y, k, r, g, b;
  948. int a = 255;
  949. int i;
  950. if (copy_spots && ss != ds)
  951. fz_throw(ctx, FZ_ERROR_ARGUMENT, "incompatible number of spots when converting pixmap");
  952. if ((int)w < 0 || h < 0)
  953. fz_throw(ctx, FZ_ERROR_LIMIT, "integer overflow");
  954. while (h--)
  955. {
  956. size_t ww = w;
  957. while (ww--)
  958. {
  959. c = s[0];
  960. m = s[1];
  961. y = s[2];
  962. k = s[3];
  963. if (sa)
  964. {
  965. a = s[4+ss];
  966. c = fz_div255(c, a);
  967. m = fz_div255(m, a);
  968. y = fz_div255(y, a);
  969. k = fz_div255(k, a);
  970. }
  971. r = 255 - fz_mini(c + k, 255);
  972. g = 255 - fz_mini(m + k, 255);
  973. b = 255 - fz_mini(y + k, 255);
  974. if (da)
  975. {
  976. *d++ = fz_mul255(b, a);
  977. *d++ = fz_mul255(g, a);
  978. *d++ = fz_mul255(r, a);
  979. }
  980. else
  981. {
  982. *d++ = b;
  983. *d++ = g;
  984. *d++ = r;
  985. }
  986. if (copy_spots)
  987. {
  988. s += 4;
  989. for (i=ss; i > 0; --i)
  990. *d++ = *s++;
  991. s += sa;
  992. }
  993. else
  994. {
  995. s += 4 + ss + sa;
  996. d += ds;
  997. }
  998. if (da)
  999. {
  1000. *d++ = a;
  1001. }
  1002. }
  1003. d += d_line_inc;
  1004. s += s_line_inc;
  1005. }
  1006. }
  1007. static void fast_rgb_to_bgr(fz_context *ctx, const fz_pixmap *src, fz_pixmap *dst, int copy_spots)
  1008. {
  1009. unsigned char *s = src->samples;
  1010. unsigned char *d = dst->samples;
  1011. size_t w = src->w;
  1012. int h = src->h;
  1013. int sn = src->n;
  1014. int ss = src->s;
  1015. int sa = src->alpha;
  1016. int dn = dst->n;
  1017. int ds = dst->s;
  1018. int da = dst->alpha;
  1019. ptrdiff_t d_line_inc = dst->stride - w * dn;
  1020. ptrdiff_t s_line_inc = src->stride - w * sn;
  1021. /* If copying spots, they must match, and we can never drop alpha (but we can invent it) */
  1022. if (copy_spots && ss != ds)
  1023. fz_throw(ctx, FZ_ERROR_ARGUMENT, "incompatible number of spots when converting pixmap");
  1024. if (!da && sa)
  1025. fz_throw(ctx, FZ_ERROR_ARGUMENT, "cannot drop alpha when converting pixmap");
  1026. if ((int)w < 0 || h < 0)
  1027. return;
  1028. if (d_line_inc == 0 && s_line_inc == 0)
  1029. {
  1030. w *= h;
  1031. h = 1;
  1032. }
  1033. if (ss == 0 && ds == 0)
  1034. {
  1035. /* Common, no spots case */
  1036. if (da)
  1037. {
  1038. if (sa)
  1039. {
  1040. while (h--)
  1041. {
  1042. size_t ww = w;
  1043. while (ww--)
  1044. {
  1045. d[0] = s[2];
  1046. d[1] = s[1];
  1047. d[2] = s[0];
  1048. d[3] = s[3];
  1049. s += 4;
  1050. d += 4;
  1051. }
  1052. }
  1053. }
  1054. else
  1055. {
  1056. while (h--)
  1057. {
  1058. size_t ww = w;
  1059. while (ww--)
  1060. {
  1061. d[0] = s[2];
  1062. d[1] = s[1];
  1063. d[2] = s[0];
  1064. d[3] = 255;
  1065. s += 3;
  1066. d += 4;
  1067. }
  1068. }
  1069. }
  1070. }
  1071. else
  1072. {
  1073. while (h--)
  1074. {
  1075. size_t ww = w;
  1076. while (ww--)
  1077. {
  1078. d[0] = s[2];
  1079. d[1] = s[1];
  1080. d[2] = s[0];
  1081. s += 3;
  1082. d += 3;
  1083. }
  1084. }
  1085. }
  1086. }
  1087. else if (copy_spots)
  1088. {
  1089. /* Slower, spots capable version */
  1090. while (h--)
  1091. {
  1092. int i;
  1093. size_t ww = w;
  1094. while (ww--)
  1095. {
  1096. d[0] = s[2];
  1097. d[1] = s[1];
  1098. d[2] = s[0];
  1099. s += 3;
  1100. d += 3;
  1101. for (i=ss; i > 0; i--)
  1102. *d++ = *s++;
  1103. if (da)
  1104. *d++ = sa ? *s++ : 255;
  1105. }
  1106. d += d_line_inc;
  1107. s += s_line_inc;
  1108. }
  1109. }
  1110. else
  1111. {
  1112. while (h--)
  1113. {
  1114. size_t ww = w;
  1115. while (ww--)
  1116. {
  1117. d[0] = s[2];
  1118. d[1] = s[1];
  1119. d[2] = s[0];
  1120. s += sn;
  1121. d += dn;
  1122. if (da)
  1123. d[-1] = sa ? s[-1] : 255;
  1124. }
  1125. d += d_line_inc;
  1126. s += s_line_inc;
  1127. }
  1128. }
  1129. }
  1130. static void fast_gray_to_gray(fz_context *ctx, const fz_pixmap *src, fz_pixmap *dst, int copy_spots)
  1131. {
  1132. unsigned char *s = src->samples;
  1133. unsigned char *d = dst->samples;
  1134. size_t w = src->w;
  1135. int h = src->h;
  1136. int sn = src->n;
  1137. int ss = src->s;
  1138. int sa = src->alpha;
  1139. int dn = dst->n;
  1140. int ds = dst->s;
  1141. int da = dst->alpha;
  1142. ptrdiff_t d_line_inc = dst->stride - w * dn;
  1143. ptrdiff_t s_line_inc = src->stride - w * sn;
  1144. if ((int)w < 0 || h < 0)
  1145. return;
  1146. /* Alpha-only pixmaps count as device_gray with no alpha. */
  1147. if (sn == 1 && sa)
  1148. sa = 0;
  1149. if (dn == 1 && da)
  1150. da = 0;
  1151. /* If copying spots, they must match, and we can never drop alpha (but we can invent it) */
  1152. if (copy_spots && ss != ds)
  1153. fz_throw(ctx, FZ_ERROR_ARGUMENT, "incompatible number of spots when converting pixmap");
  1154. if (!da && sa)
  1155. fz_throw(ctx, FZ_ERROR_ARGUMENT, "cannot drop alpha when converting pixmap");
  1156. if (d_line_inc == 0 && s_line_inc == 0)
  1157. {
  1158. w *= h;
  1159. h = 1;
  1160. }
  1161. if (ss == 0 && ds == 0)
  1162. {
  1163. /* Common, no spots case */
  1164. if (da)
  1165. {
  1166. if (sa)
  1167. {
  1168. while (h--)
  1169. {
  1170. size_t ww = w;
  1171. while (ww--)
  1172. {
  1173. d[0] = s[0];
  1174. d[1] = s[1];
  1175. s += 2;
  1176. d += 2;
  1177. }
  1178. }
  1179. }
  1180. else
  1181. {
  1182. while (h--)
  1183. {
  1184. size_t ww = w;
  1185. while (ww--)
  1186. {
  1187. d[0] = s[0];
  1188. d[1] = 255;
  1189. s += 1;
  1190. d += 2;
  1191. }
  1192. }
  1193. }
  1194. }
  1195. else
  1196. {
  1197. while (h--)
  1198. {
  1199. size_t ww = w;
  1200. while (ww--)
  1201. {
  1202. d[0] = s[0];
  1203. s += 1;
  1204. d += 1;
  1205. }
  1206. }
  1207. }
  1208. }
  1209. else if (copy_spots)
  1210. {
  1211. /* Slower, spots capable version */
  1212. while (h--)
  1213. {
  1214. int i;
  1215. size_t ww = w;
  1216. while (ww--)
  1217. {
  1218. d[0] = s[0];
  1219. s += 1;
  1220. d += 1;
  1221. for (i=ss; i > 0; i--)
  1222. *d++ = *s++;
  1223. if (da)
  1224. *d++ = sa ? *s++ : 255;
  1225. }
  1226. d += d_line_inc;
  1227. s += s_line_inc;
  1228. }
  1229. }
  1230. else
  1231. {
  1232. while (h--)
  1233. {
  1234. size_t ww = w;
  1235. while (ww--)
  1236. {
  1237. d[0] = s[0];
  1238. s += sn;
  1239. d += dn;
  1240. if (da)
  1241. d[-1] = sa ? s[-1] : 255;
  1242. }
  1243. d += d_line_inc;
  1244. s += s_line_inc;
  1245. }
  1246. }
  1247. }
  1248. static void fast_rgb_to_rgb(fz_context *ctx, const fz_pixmap *src, fz_pixmap *dst, int copy_spots)
  1249. {
  1250. unsigned char *s = src->samples;
  1251. unsigned char *d = dst->samples;
  1252. size_t w = src->w;
  1253. int h = src->h;
  1254. int sn = src->n;
  1255. int ss = src->s;
  1256. int sa = src->alpha;
  1257. int dn = dst->n;
  1258. int ds = dst->s;
  1259. int da = dst->alpha;
  1260. ptrdiff_t d_line_inc = dst->stride - w * dn;
  1261. ptrdiff_t s_line_inc = src->stride - w * sn;
  1262. /* If copying spots, they must match, and we can never drop alpha (but we can invent it) */
  1263. if (copy_spots && ss != ds)
  1264. fz_throw(ctx, FZ_ERROR_ARGUMENT, "incompatible number of spots when converting pixmap");
  1265. if (!da && sa)
  1266. fz_throw(ctx, FZ_ERROR_ARGUMENT, "cannot drop alpha when converting pixmap");
  1267. if ((int)w < 0 || h < 0)
  1268. return;
  1269. if (d_line_inc == 0 && s_line_inc == 0)
  1270. {
  1271. w *= h;
  1272. h = 1;
  1273. }
  1274. if (ss == 0 && ds == 0)
  1275. {
  1276. /* Common, no spots case */
  1277. if (da)
  1278. {
  1279. if (sa)
  1280. {
  1281. while (h--)
  1282. {
  1283. size_t ww = w;
  1284. while (ww--)
  1285. {
  1286. d[0] = s[0];
  1287. d[1] = s[1];
  1288. d[2] = s[2];
  1289. d[3] = s[3];
  1290. s += 4;
  1291. d += 4;
  1292. }
  1293. }
  1294. }
  1295. else
  1296. {
  1297. while (h--)
  1298. {
  1299. size_t ww = w;
  1300. while (ww--)
  1301. {
  1302. d[0] = s[0];
  1303. d[1] = s[1];
  1304. d[2] = s[2];
  1305. d[3] = 255;
  1306. s += 3;
  1307. d += 4;
  1308. }
  1309. }
  1310. }
  1311. }
  1312. else
  1313. {
  1314. while (h--)
  1315. {
  1316. size_t ww = w;
  1317. while (ww--)
  1318. {
  1319. d[0] = s[0];
  1320. d[1] = s[1];
  1321. d[2] = s[2];
  1322. s += 3;
  1323. d += 3;
  1324. }
  1325. }
  1326. }
  1327. }
  1328. else if (copy_spots)
  1329. {
  1330. /* Slower, spots capable version */
  1331. while (h--)
  1332. {
  1333. int i;
  1334. size_t ww = w;
  1335. while (ww--)
  1336. {
  1337. d[0] = s[0];
  1338. d[1] = s[1];
  1339. d[2] = s[2];
  1340. s += 3;
  1341. d += 3;
  1342. for (i=ss; i > 0; i--)
  1343. *d++ = *s++;
  1344. if (da)
  1345. *d++ = sa ? *s++ : 255;
  1346. }
  1347. d += d_line_inc;
  1348. s += s_line_inc;
  1349. }
  1350. }
  1351. else
  1352. {
  1353. while (h--)
  1354. {
  1355. size_t ww = w;
  1356. while (ww--)
  1357. {
  1358. d[0] = s[0];
  1359. d[1] = s[1];
  1360. d[2] = s[2];
  1361. s += sn;
  1362. d += dn;
  1363. if (da)
  1364. d[-1] = sa ? s[-1] : 255;
  1365. }
  1366. d += d_line_inc;
  1367. s += s_line_inc;
  1368. }
  1369. }
  1370. }
  1371. static void fast_cmyk_to_cmyk(fz_context *ctx, const fz_pixmap *src, fz_pixmap *dst, int copy_spots)
  1372. {
  1373. unsigned char *s = src->samples;
  1374. unsigned char *d = dst->samples;
  1375. size_t w = src->w;
  1376. int h = src->h;
  1377. int sn = src->n;
  1378. int ss = src->s;
  1379. int sa = src->alpha;
  1380. int dn = dst->n;
  1381. int ds = dst->s;
  1382. int da = dst->alpha;
  1383. ptrdiff_t d_line_inc = dst->stride - w * dn;
  1384. ptrdiff_t s_line_inc = src->stride - w * sn;
  1385. /* If copying spots, they must match, and we can never drop alpha (but we can invent it) */
  1386. if (copy_spots && ss != ds)
  1387. fz_throw(ctx, FZ_ERROR_ARGUMENT, "incompatible number of spots when converting pixmap");
  1388. if (!da && sa)
  1389. fz_throw(ctx, FZ_ERROR_ARGUMENT, "cannot drop alpha when converting pixmap");
  1390. if ((int)w < 0 || h < 0)
  1391. return;
  1392. if (d_line_inc == 0 && s_line_inc == 0)
  1393. {
  1394. w *= h;
  1395. h = 1;
  1396. }
  1397. if (ss == 0 && ds == 0)
  1398. {
  1399. /* Common, no spots case */
  1400. if (da)
  1401. {
  1402. if (sa)
  1403. {
  1404. while (h--)
  1405. {
  1406. size_t ww = w;
  1407. while (ww--)
  1408. {
  1409. d[0] = s[0];
  1410. d[1] = s[1];
  1411. d[2] = s[2];
  1412. d[3] = s[3];
  1413. d[4] = s[4];
  1414. s += 5;
  1415. d += 5;
  1416. }
  1417. }
  1418. }
  1419. else
  1420. {
  1421. while (h--)
  1422. {
  1423. size_t ww = w;
  1424. while (ww--)
  1425. {
  1426. d[0] = s[0];
  1427. d[1] = s[1];
  1428. d[2] = s[2];
  1429. d[3] = s[3];
  1430. d[4] = 255;
  1431. s += 4;
  1432. d += 5;
  1433. }
  1434. }
  1435. }
  1436. }
  1437. else
  1438. {
  1439. while (h--)
  1440. {
  1441. size_t ww = w;
  1442. while (ww--)
  1443. {
  1444. d[0] = s[0];
  1445. d[1] = s[1];
  1446. d[2] = s[2];
  1447. d[3] = s[3];
  1448. s += 4;
  1449. d += 4;
  1450. }
  1451. }
  1452. }
  1453. }
  1454. else if (copy_spots)
  1455. {
  1456. /* Slower, spots capable version */
  1457. while (h--)
  1458. {
  1459. int i;
  1460. size_t ww = w;
  1461. while (ww--)
  1462. {
  1463. d[0] = s[0];
  1464. d[1] = s[1];
  1465. d[2] = s[2];
  1466. d[3] = s[3];
  1467. s += 4;
  1468. d += 4;
  1469. for (i=ss; i > 0; i--)
  1470. *d++ = *s++;
  1471. if (da)
  1472. *d++ = sa ? *s++ : 255;
  1473. }
  1474. d += d_line_inc;
  1475. s += s_line_inc;
  1476. }
  1477. }
  1478. else
  1479. {
  1480. while (h--)
  1481. {
  1482. size_t ww = w;
  1483. while (ww--)
  1484. {
  1485. d[0] = s[0];
  1486. d[1] = s[1];
  1487. d[2] = s[2];
  1488. d[3] = s[3];
  1489. s += sn;
  1490. d += dn;
  1491. if (da)
  1492. d[-1] = sa ? s[-1] : 255;
  1493. }
  1494. d += d_line_inc;
  1495. s += s_line_inc;
  1496. }
  1497. }
  1498. }
  1499. void
  1500. fz_fast_any_to_alpha(fz_context *ctx, const fz_pixmap *src, fz_pixmap *dst, int copy_spots)
  1501. {
  1502. if (!src->alpha)
  1503. fz_clear_pixmap_with_value(ctx, dst, 255);
  1504. else
  1505. {
  1506. unsigned char *s = src->samples;
  1507. unsigned char *d = dst->samples;
  1508. size_t w = src->w;
  1509. int h = src->h;
  1510. int n = src->n;
  1511. ptrdiff_t d_line_inc = dst->stride - w * dst->n;
  1512. ptrdiff_t s_line_inc = src->stride - w * src->n;
  1513. if ((int)w < 0 || h < 0)
  1514. return;
  1515. if (d_line_inc == 0 && s_line_inc == 0)
  1516. {
  1517. w *= h;
  1518. h = 1;
  1519. }
  1520. s += n-1;
  1521. while (h--)
  1522. {
  1523. size_t ww = w;
  1524. while (ww--)
  1525. {
  1526. *d++ = *s;
  1527. s += n;
  1528. }
  1529. d += d_line_inc;
  1530. s += s_line_inc;
  1531. }
  1532. }
  1533. }
  1534. void
  1535. fz_convert_fast_pixmap_samples(fz_context *ctx, const fz_pixmap *src, fz_pixmap *dst, int copy_spots)
  1536. {
  1537. fz_colorspace *ss = src->colorspace;
  1538. fz_colorspace *ds = dst->colorspace;
  1539. int dtype = ds ? ds->type : FZ_COLORSPACE_GRAY;
  1540. int stype = ss ? ss->type : FZ_COLORSPACE_GRAY;
  1541. if (!ds)
  1542. {
  1543. fz_fast_any_to_alpha(ctx, src, dst, copy_spots);
  1544. }
  1545. else if (stype == FZ_COLORSPACE_GRAY)
  1546. {
  1547. if (dtype == FZ_COLORSPACE_GRAY)
  1548. fast_gray_to_gray(ctx, src, dst, copy_spots);
  1549. else if (dtype == FZ_COLORSPACE_RGB)
  1550. fast_gray_to_rgb(ctx, src, dst, copy_spots);
  1551. else if (dtype == FZ_COLORSPACE_BGR)
  1552. fast_gray_to_rgb(ctx, src, dst, copy_spots);
  1553. else if (dtype == FZ_COLORSPACE_CMYK)
  1554. fast_gray_to_cmyk(ctx, src, dst, copy_spots);
  1555. else
  1556. goto slow;
  1557. }
  1558. else if (stype == FZ_COLORSPACE_RGB)
  1559. {
  1560. if (dtype == FZ_COLORSPACE_GRAY)
  1561. fast_rgb_to_gray(ctx, src, dst, copy_spots);
  1562. else if (dtype == FZ_COLORSPACE_RGB)
  1563. fast_rgb_to_rgb(ctx, src, dst, copy_spots);
  1564. else if (dtype == FZ_COLORSPACE_BGR)
  1565. fast_rgb_to_bgr(ctx, src, dst, copy_spots);
  1566. else if (dtype == FZ_COLORSPACE_CMYK)
  1567. fast_rgb_to_cmyk(ctx, src, dst, copy_spots);
  1568. else
  1569. goto slow;
  1570. }
  1571. else if (stype == FZ_COLORSPACE_BGR)
  1572. {
  1573. if (dtype == FZ_COLORSPACE_GRAY)
  1574. fast_bgr_to_gray(ctx, src, dst, copy_spots);
  1575. else if (dtype == FZ_COLORSPACE_RGB)
  1576. fast_rgb_to_bgr(ctx, src, dst, copy_spots);
  1577. else if (dtype == FZ_COLORSPACE_BGR)
  1578. fast_rgb_to_rgb(ctx, src, dst, copy_spots);
  1579. else if (dtype == FZ_COLORSPACE_CMYK)
  1580. fast_bgr_to_cmyk(ctx, src, dst, copy_spots);
  1581. else
  1582. goto slow;
  1583. }
  1584. else if (stype == FZ_COLORSPACE_CMYK)
  1585. {
  1586. if (dtype == FZ_COLORSPACE_GRAY)
  1587. fast_cmyk_to_gray(ctx, src, dst, copy_spots);
  1588. else if (dtype == FZ_COLORSPACE_RGB)
  1589. fast_cmyk_to_rgb(ctx, src, dst, copy_spots);
  1590. else if (dtype == FZ_COLORSPACE_BGR)
  1591. fast_cmyk_to_bgr(ctx, src, dst, copy_spots);
  1592. else if (dtype == FZ_COLORSPACE_CMYK)
  1593. fast_cmyk_to_cmyk(ctx, src, dst, copy_spots);
  1594. else
  1595. goto slow;
  1596. }
  1597. else
  1598. {
  1599. goto slow;
  1600. }
  1601. return;
  1602. slow:
  1603. fz_convert_slow_pixmap_samples(ctx, src, dst, NULL, fz_default_color_params, copy_spots);
  1604. }