KOffice – TDE office suite
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

kis_gray_colorspace.cc 31KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997
  1. /*
  2. * Copyright (c) 2002 Patrick Julien <freak@codepimps.org>
  3. * Copyright (c) 2004 Cyrille Berger
  4. * Copyright (c) 2004 Boudewijn Rempt <boud@valdyas.org>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. #include <limits.h>
  21. #include <stdlib.h>
  22. #include <config.h>
  23. #include LCMS_HEADER
  24. #include <tqimage.h>
  25. #include <tdelocale.h>
  26. #include <kdebug.h>
  27. #include <tdeglobal.h>
  28. #include "kis_abstract_colorspace.h"
  29. #include "kis_u8_base_colorspace.h"
  30. #include "kis_gray_colorspace.h"
  31. #include "kis_integer_maths.h"
  32. #define downscale(quantum) (quantum) //((unsigned char) ((quantum)/257UL))
  33. #define upscale(value) (value) // ((TQ_UINT8) (257UL*(value)))
  34. namespace {
  35. const TQ_INT32 MAX_CHANNEL_GRAYSCALE = 1;
  36. const TQ_INT32 MAX_CHANNEL_GRAYSCALEA = 2;
  37. }
  38. KisGrayColorSpace::KisGrayColorSpace(KisColorSpaceFactoryRegistry * parent, KisProfile *p) :
  39. KisU8BaseColorSpace(KisID("GRAYA", i18n("Grayscale")), TYPE_GRAYA_8, icSigGrayData, parent, p)
  40. {
  41. m_channels.push_back(new KisChannelInfo(i18n("Gray"), i18n("G"), 0, KisChannelInfo::COLOR, KisChannelInfo::UINT8));
  42. m_channels.push_back(new KisChannelInfo(i18n("Alpha"), i18n("A"), 1, KisChannelInfo::ALPHA, KisChannelInfo::UINT8));
  43. m_alphaPos = PIXEL_GRAY_ALPHA;
  44. init();
  45. }
  46. KisGrayColorSpace::~KisGrayColorSpace()
  47. {
  48. }
  49. void KisGrayColorSpace::setPixel(TQ_UINT8 *pixel, TQ_UINT8 gray, TQ_UINT8 alpha) const
  50. {
  51. pixel[PIXEL_GRAY] = gray;
  52. pixel[PIXEL_GRAY_ALPHA] = alpha;
  53. }
  54. void KisGrayColorSpace::getPixel(const TQ_UINT8 *pixel, TQ_UINT8 *gray, TQ_UINT8 *alpha) const
  55. {
  56. *gray = pixel[PIXEL_GRAY];
  57. *alpha = pixel[PIXEL_GRAY_ALPHA];
  58. }
  59. void KisGrayColorSpace::getAlpha(const TQ_UINT8 *pixel, TQ_UINT8 *alpha) const
  60. {
  61. *alpha = pixel[PIXEL_GRAY_ALPHA];
  62. }
  63. void KisGrayColorSpace::setAlpha(TQ_UINT8 *pixels, TQ_UINT8 alpha, TQ_INT32 nPixels) const
  64. {
  65. while (nPixels > 0) {
  66. pixels[PIXEL_GRAY_ALPHA] = alpha;
  67. --nPixels;
  68. pixels += MAX_CHANNEL_GRAYSCALEA;
  69. }
  70. }
  71. void KisGrayColorSpace::mixColors(const TQ_UINT8 **colors, const TQ_UINT8 *weights, TQ_UINT32 nColors, TQ_UINT8 *dst) const
  72. {
  73. TQ_UINT32 totalGray = 0, newAlpha = 0;
  74. while (nColors--)
  75. {
  76. TQ_UINT32 alpha = (*colors)[PIXEL_GRAY_ALPHA];
  77. TQ_UINT32 alphaTimesWeight = UINT8_MULT(alpha, *weights);
  78. totalGray += (*colors)[PIXEL_GRAY] * alphaTimesWeight;
  79. newAlpha += alphaTimesWeight;
  80. weights++;
  81. colors++;
  82. }
  83. Q_ASSERT(newAlpha <= 255);
  84. dst[PIXEL_GRAY_ALPHA] = newAlpha;
  85. if (newAlpha > 0) {
  86. totalGray = UINT8_DIVIDE(totalGray, newAlpha);
  87. }
  88. // Divide by 255.
  89. totalGray += 0x80;
  90. TQ_UINT32 dstGray = ((totalGray >> 8) + totalGray) >> 8;
  91. Q_ASSERT(dstGray <= 255);
  92. dst[PIXEL_GRAY] = dstGray;
  93. }
  94. void KisGrayColorSpace::convolveColors(TQ_UINT8** colors, TQ_INT32* kernelValues, KisChannelInfo::enumChannelFlags channelFlags, TQ_UINT8 *dst, TQ_INT32 factor, TQ_INT32 offset, TQ_INT32 nColors) const
  95. {
  96. TQ_INT32 totalGray = 0, totalAlpha = 0;
  97. while (nColors--)
  98. {
  99. TQ_INT32 weight = *kernelValues;
  100. if (weight != 0) {
  101. totalGray += (*colors)[PIXEL_GRAY] * weight;
  102. totalAlpha += (*colors)[PIXEL_GRAY_ALPHA] * weight;
  103. }
  104. colors++;
  105. kernelValues++;
  106. }
  107. if (channelFlags & KisChannelInfo::FLAG_COLOR) {
  108. dst[PIXEL_GRAY] = CLAMP((totalGray / factor) + offset, 0, TQ_UINT8_MAX);
  109. }
  110. if (channelFlags & KisChannelInfo::FLAG_ALPHA) {
  111. dst[PIXEL_GRAY_ALPHA] = CLAMP((totalAlpha/ factor) + offset, 0, TQ_UINT8_MAX);
  112. }
  113. }
  114. void KisGrayColorSpace::invertColor(TQ_UINT8 * src, TQ_INT32 nPixels)
  115. {
  116. TQ_UINT32 psize = pixelSize();
  117. while (nPixels--)
  118. {
  119. src[PIXEL_GRAY] = TQ_UINT8_MAX - src[PIXEL_GRAY];
  120. src += psize;
  121. }
  122. }
  123. void KisGrayColorSpace::darken(const TQ_UINT8 * src, TQ_UINT8 * dst, TQ_INT32 shade, bool compensate, double compensation, TQ_INT32 nPixels) const
  124. {
  125. TQ_UINT32 pSize = pixelSize();
  126. while (nPixels--) {
  127. if (compensate) {
  128. dst[PIXEL_GRAY] = (TQ_INT8) TQMIN(255,((src[PIXEL_GRAY] * shade) / (compensation * 255)));
  129. }
  130. else {
  131. dst[PIXEL_GRAY] = (TQ_INT8) TQMIN(255, (src[PIXEL_GRAY] * shade / 255));
  132. }
  133. dst += pSize;
  134. src += pSize;
  135. }
  136. }
  137. TQ_UINT8 KisGrayColorSpace::intensity8(const TQ_UINT8 * src) const
  138. {
  139. return src[PIXEL_GRAY];
  140. }
  141. TQValueVector<KisChannelInfo *> KisGrayColorSpace::channels() const
  142. {
  143. return m_channels;
  144. }
  145. TQ_UINT32 KisGrayColorSpace::nChannels() const
  146. {
  147. return MAX_CHANNEL_GRAYSCALEA;
  148. }
  149. TQ_UINT32 KisGrayColorSpace::nColorChannels() const
  150. {
  151. return MAX_CHANNEL_GRAYSCALE;
  152. }
  153. TQ_UINT32 KisGrayColorSpace::pixelSize() const
  154. {
  155. return MAX_CHANNEL_GRAYSCALEA;
  156. }
  157. void KisGrayColorSpace::bitBlt(TQ_UINT8 *dst,
  158. TQ_INT32 dstRowStride,
  159. const TQ_UINT8 *src,
  160. TQ_INT32 srcRowStride,
  161. const TQ_UINT8 *mask,
  162. TQ_INT32 maskRowStride,
  163. TQ_UINT8 opacity,
  164. TQ_INT32 rows,
  165. TQ_INT32 cols,
  166. const KisCompositeOp& op)
  167. {
  168. switch (op.op()) {
  169. case COMPOSITE_OVER:
  170. compositeOver(dst, dstRowStride, src, srcRowStride, mask, maskRowStride, rows, cols, opacity);
  171. break;
  172. case COMPOSITE_MULT:
  173. compositeMultiply(dst, dstRowStride, src, srcRowStride, mask, maskRowStride, rows, cols, opacity);
  174. break;
  175. case COMPOSITE_DIVIDE:
  176. compositeDivide(dst, dstRowStride, src, srcRowStride, mask, maskRowStride, rows, cols, opacity);
  177. break;
  178. case COMPOSITE_DARKEN:
  179. compositeDarken(dst, dstRowStride, src, srcRowStride, mask, maskRowStride, rows, cols, opacity);
  180. break;
  181. case COMPOSITE_LIGHTEN:
  182. compositeLighten(dst, dstRowStride, src, srcRowStride, mask, maskRowStride, rows, cols, opacity);
  183. break;
  184. case COMPOSITE_SCREEN:
  185. compositeScreen(dst, dstRowStride, src, srcRowStride, mask, maskRowStride, rows, cols, opacity);
  186. break;
  187. case COMPOSITE_OVERLAY:
  188. compositeOverlay(dst, dstRowStride, src, srcRowStride, mask, maskRowStride, rows, cols, opacity);
  189. break;
  190. case COMPOSITE_DODGE:
  191. compositeDodge(dst, dstRowStride, src, srcRowStride, mask, maskRowStride, rows, cols, opacity);
  192. break;
  193. case COMPOSITE_BURN:
  194. compositeBurn(dst, dstRowStride, src, srcRowStride, mask, maskRowStride, rows, cols, opacity);
  195. break;
  196. case COMPOSITE_ERASE:
  197. compositeErase(dst, dstRowStride, src, srcRowStride, mask, maskRowStride, rows, cols, opacity);
  198. break;
  199. case COMPOSITE_COPY:
  200. compositeCopy(dst, dstRowStride, src, srcRowStride, mask, maskRowStride, rows, cols, opacity);
  201. break;
  202. case COMPOSITE_CLEAR: {
  203. TQ_UINT8 *d;
  204. TQ_INT32 linesize;
  205. linesize = MAX_CHANNEL_GRAYSCALEA*sizeof(TQ_UINT8) * cols;
  206. d = dst;
  207. while (rows-- > 0) {
  208. memset(d, 0, linesize);
  209. d += dstRowStride;
  210. }
  211. }
  212. break;
  213. case COMPOSITE_ALPHA_DARKEN:
  214. compositeAlphaDarken(dst, dstRowStride, src, srcRowStride, mask, maskRowStride, rows, cols, opacity);
  215. break;
  216. default:
  217. break;
  218. }
  219. }
  220. KisCompositeOpList KisGrayColorSpace::userVisiblecompositeOps() const
  221. {
  222. KisCompositeOpList list;
  223. list.append(KisCompositeOp(COMPOSITE_OVER));
  224. list.append(KisCompositeOp(COMPOSITE_MULT));
  225. list.append(KisCompositeOp(COMPOSITE_BURN));
  226. list.append(KisCompositeOp(COMPOSITE_DODGE));
  227. list.append(KisCompositeOp(COMPOSITE_DIVIDE));
  228. list.append(KisCompositeOp(COMPOSITE_SCREEN));
  229. list.append(KisCompositeOp(COMPOSITE_OVERLAY));
  230. list.append(KisCompositeOp(COMPOSITE_DARKEN));
  231. list.append(KisCompositeOp(COMPOSITE_LIGHTEN));
  232. return list;
  233. }
  234. void KisGrayColorSpace::compositeOver(TQ_UINT8 *dstRowStart, TQ_INT32 dstRowStride, const TQ_UINT8 *srcRowStart, TQ_INT32 srcRowStride, const TQ_UINT8 *maskRowStart, TQ_INT32 maskRowStride, TQ_INT32 rows, TQ_INT32 numColumns, TQ_UINT8 opacity)
  235. {
  236. while (rows > 0) {
  237. const TQ_UINT8 *src = srcRowStart;
  238. TQ_UINT8 *dst = dstRowStart;
  239. TQ_INT32 columns = numColumns;
  240. const TQ_UINT8 *mask = maskRowStart;
  241. while (columns > 0) {
  242. TQ_UINT8 srcAlpha = src[PIXEL_GRAY_ALPHA];
  243. // apply the alphamask
  244. if(mask != 0)
  245. {
  246. if(*mask != OPACITY_OPAQUE)
  247. srcAlpha = UINT8_MULT(srcAlpha, *mask);
  248. mask++;
  249. }
  250. if (srcAlpha != OPACITY_TRANSPARENT) {
  251. if (opacity != OPACITY_OPAQUE) {
  252. srcAlpha = UINT8_MULT(srcAlpha, opacity);
  253. }
  254. if (srcAlpha == OPACITY_OPAQUE) {
  255. memcpy(dst, src, MAX_CHANNEL_GRAYSCALEA * sizeof(TQ_UINT8));
  256. } else {
  257. TQ_UINT8 dstAlpha = dst[PIXEL_GRAY_ALPHA];
  258. TQ_UINT8 srcBlend;
  259. if (dstAlpha == OPACITY_OPAQUE) {
  260. srcBlend = srcAlpha;
  261. } else {
  262. TQ_UINT8 newAlpha = dstAlpha + UINT8_MULT(OPACITY_OPAQUE - dstAlpha, srcAlpha);
  263. dst[PIXEL_GRAY_ALPHA] = newAlpha;
  264. if (newAlpha != 0) {
  265. srcBlend = UINT8_DIVIDE(srcAlpha, newAlpha);
  266. } else {
  267. srcBlend = srcAlpha;
  268. }
  269. }
  270. if (srcBlend == OPACITY_OPAQUE) {
  271. memcpy(dst, src, MAX_CHANNEL_GRAYSCALE * sizeof(TQ_UINT8));
  272. } else {
  273. dst[PIXEL_GRAY] = UINT8_BLEND(src[PIXEL_GRAY], dst[PIXEL_GRAY], srcBlend);
  274. }
  275. }
  276. }
  277. columns--;
  278. src += MAX_CHANNEL_GRAYSCALEA;
  279. dst += MAX_CHANNEL_GRAYSCALEA;
  280. }
  281. rows--;
  282. srcRowStart += srcRowStride;
  283. dstRowStart += dstRowStride;
  284. if(maskRowStart)
  285. maskRowStart += maskRowStride;
  286. }
  287. }
  288. void KisGrayColorSpace::compositeMultiply(TQ_UINT8 *dstRowStart, TQ_INT32 dstRowStride, const TQ_UINT8 *srcRowStart, TQ_INT32 srcRowStride, const TQ_UINT8 *maskRowStart, TQ_INT32 maskRowStride, TQ_INT32 rows, TQ_INT32 numColumns, TQ_UINT8 opacity)
  289. {
  290. while (rows > 0) {
  291. const TQ_UINT8 *src = srcRowStart;
  292. TQ_UINT8 *dst = dstRowStart;
  293. TQ_INT32 columns = numColumns;
  294. const TQ_UINT8 *mask = maskRowStart;
  295. while (columns > 0) {
  296. TQ_UINT8 srcAlpha = src[PIXEL_GRAY_ALPHA];
  297. TQ_UINT8 dstAlpha = dst[PIXEL_GRAY_ALPHA];
  298. srcAlpha = TQMIN(srcAlpha, dstAlpha);
  299. // apply the alphamask
  300. if(mask != 0)
  301. {
  302. if(*mask != OPACITY_OPAQUE)
  303. srcAlpha = UINT8_MULT(srcAlpha, *mask);
  304. mask++;
  305. }
  306. if (srcAlpha != OPACITY_TRANSPARENT) {
  307. if (opacity != OPACITY_OPAQUE) {
  308. srcAlpha = UINT8_MULT(src[PIXEL_GRAY_ALPHA], opacity);
  309. }
  310. TQ_UINT8 srcBlend;
  311. if (dstAlpha == OPACITY_OPAQUE) {
  312. srcBlend = srcAlpha;
  313. } else {
  314. TQ_UINT8 newAlpha = dstAlpha + UINT8_MULT(OPACITY_OPAQUE - dstAlpha, srcAlpha);
  315. dst[PIXEL_GRAY_ALPHA] = newAlpha;
  316. if (newAlpha != 0) {
  317. srcBlend = UINT8_DIVIDE(srcAlpha, newAlpha);
  318. } else {
  319. srcBlend = srcAlpha;
  320. }
  321. }
  322. TQ_UINT8 srcColor = src[PIXEL_GRAY];
  323. TQ_UINT8 dstColor = dst[PIXEL_GRAY];
  324. srcColor = UINT8_MULT(srcColor, dstColor);
  325. dst[PIXEL_GRAY] = UINT8_BLEND(srcColor, dstColor, srcBlend);
  326. }
  327. columns--;
  328. src += MAX_CHANNEL_GRAYSCALEA;
  329. dst += MAX_CHANNEL_GRAYSCALEA;
  330. }
  331. rows--;
  332. srcRowStart += srcRowStride;
  333. dstRowStart += dstRowStride;
  334. if(maskRowStart)
  335. maskRowStart += maskRowStride;
  336. }
  337. }
  338. void KisGrayColorSpace::compositeDivide(TQ_UINT8 *dstRowStart, TQ_INT32 dstRowStride, const TQ_UINT8 *srcRowStart, TQ_INT32 srcRowStride, const TQ_UINT8 *maskRowStart, TQ_INT32 maskRowStride, TQ_INT32 rows, TQ_INT32 numColumns, TQ_UINT8 opacity)
  339. {
  340. while (rows > 0) {
  341. const TQ_UINT8 *src = srcRowStart;
  342. TQ_UINT8 *dst = dstRowStart;
  343. TQ_INT32 columns = numColumns;
  344. const TQ_UINT8 *mask = maskRowStart;
  345. while (columns > 0) {
  346. TQ_UINT8 srcAlpha = src[PIXEL_GRAY_ALPHA];
  347. TQ_UINT8 dstAlpha = dst[PIXEL_GRAY_ALPHA];
  348. srcAlpha = TQMIN(srcAlpha, dstAlpha);
  349. // apply the alphamask
  350. if(mask != 0)
  351. {
  352. if(*mask != OPACITY_OPAQUE)
  353. srcAlpha = UINT8_MULT(srcAlpha, *mask);
  354. mask++;
  355. }
  356. if (srcAlpha != OPACITY_TRANSPARENT) {
  357. if (opacity != OPACITY_OPAQUE) {
  358. srcAlpha = UINT8_MULT(src[PIXEL_GRAY_ALPHA], opacity);
  359. }
  360. TQ_UINT8 srcBlend;
  361. if (dstAlpha == OPACITY_OPAQUE) {
  362. srcBlend = srcAlpha;
  363. } else {
  364. TQ_UINT8 newAlpha = dstAlpha + UINT8_MULT(OPACITY_OPAQUE - dstAlpha, srcAlpha);
  365. dst[PIXEL_GRAY_ALPHA] = newAlpha;
  366. if (newAlpha != 0) {
  367. srcBlend = UINT8_DIVIDE(srcAlpha, newAlpha);
  368. } else {
  369. srcBlend = srcAlpha;
  370. }
  371. }
  372. for (int channel = 0; channel < MAX_CHANNEL_GRAYSCALE; channel++) {
  373. TQ_UINT8 srcColor = src[channel];
  374. TQ_UINT8 dstColor = dst[channel];
  375. srcColor = TQMIN((dstColor * (UINT8_MAX + 1)) / (1 + srcColor), UINT8_MAX);
  376. TQ_UINT8 newColor = UINT8_BLEND(srcColor, dstColor, srcBlend);
  377. dst[channel] = newColor;
  378. }
  379. }
  380. columns--;
  381. src += MAX_CHANNEL_GRAYSCALEA;
  382. dst += MAX_CHANNEL_GRAYSCALEA;
  383. }
  384. rows--;
  385. srcRowStart += srcRowStride;
  386. dstRowStart += dstRowStride;
  387. if(maskRowStart)
  388. maskRowStart += maskRowStride;
  389. }
  390. }
  391. void KisGrayColorSpace::compositeScreen(TQ_UINT8 *dstRowStart, TQ_INT32 dstRowStride, const TQ_UINT8 *srcRowStart, TQ_INT32 srcRowStride, const TQ_UINT8 *maskRowStart, TQ_INT32 maskRowStride, TQ_INT32 rows, TQ_INT32 numColumns, TQ_UINT8 opacity)
  392. {
  393. while (rows > 0) {
  394. const TQ_UINT8 *src = srcRowStart;
  395. TQ_UINT8 *dst = dstRowStart;
  396. TQ_INT32 columns = numColumns;
  397. const TQ_UINT8 *mask = maskRowStart;
  398. while (columns > 0) {
  399. TQ_UINT8 srcAlpha = src[PIXEL_GRAY_ALPHA];
  400. TQ_UINT8 dstAlpha = dst[PIXEL_GRAY_ALPHA];
  401. srcAlpha = TQMIN(srcAlpha, dstAlpha);
  402. // apply the alphamask
  403. if(mask != 0)
  404. {
  405. if(*mask != OPACITY_OPAQUE)
  406. srcAlpha = UINT8_MULT(srcAlpha, *mask);
  407. mask++;
  408. }
  409. if (srcAlpha != OPACITY_TRANSPARENT) {
  410. if (opacity != OPACITY_OPAQUE) {
  411. srcAlpha = UINT8_MULT(src[PIXEL_GRAY_ALPHA], opacity);
  412. }
  413. TQ_UINT8 srcBlend;
  414. if (dstAlpha == OPACITY_OPAQUE) {
  415. srcBlend = srcAlpha;
  416. } else {
  417. TQ_UINT8 newAlpha = dstAlpha + UINT8_MULT(OPACITY_OPAQUE - dstAlpha, srcAlpha);
  418. dst[PIXEL_GRAY_ALPHA] = newAlpha;
  419. if (newAlpha != 0) {
  420. srcBlend = UINT8_DIVIDE(srcAlpha, newAlpha);
  421. } else {
  422. srcBlend = srcAlpha;
  423. }
  424. }
  425. for (int channel = 0; channel < MAX_CHANNEL_GRAYSCALE; channel++) {
  426. TQ_UINT8 srcColor = src[channel];
  427. TQ_UINT8 dstColor = dst[channel];
  428. srcColor = UINT8_MAX - UINT8_MULT(UINT8_MAX - dstColor, UINT8_MAX - srcColor);
  429. TQ_UINT8 newColor = UINT8_BLEND(srcColor, dstColor, srcBlend);
  430. dst[channel] = newColor;
  431. }
  432. }
  433. columns--;
  434. src += MAX_CHANNEL_GRAYSCALEA;
  435. dst += MAX_CHANNEL_GRAYSCALEA;
  436. }
  437. rows--;
  438. srcRowStart += srcRowStride;
  439. dstRowStart += dstRowStride;
  440. if(maskRowStart)
  441. maskRowStart += maskRowStride;
  442. }
  443. }
  444. void KisGrayColorSpace::compositeOverlay(TQ_UINT8 *dstRowStart, TQ_INT32 dstRowStride, const TQ_UINT8 *srcRowStart, TQ_INT32 srcRowStride, const TQ_UINT8 *maskRowStart, TQ_INT32 maskRowStride, TQ_INT32 rows, TQ_INT32 numColumns, TQ_UINT8 opacity)
  445. {
  446. while (rows > 0) {
  447. const TQ_UINT8 *src = srcRowStart;
  448. TQ_UINT8 *dst = dstRowStart;
  449. TQ_INT32 columns = numColumns;
  450. const TQ_UINT8 *mask = maskRowStart;
  451. while (columns > 0) {
  452. TQ_UINT8 srcAlpha = src[PIXEL_GRAY_ALPHA];
  453. TQ_UINT8 dstAlpha = dst[PIXEL_GRAY_ALPHA];
  454. srcAlpha = TQMIN(srcAlpha, dstAlpha);
  455. // apply the alphamask
  456. if(mask != 0)
  457. {
  458. if(*mask != OPACITY_OPAQUE)
  459. srcAlpha = UINT8_MULT(srcAlpha, *mask);
  460. mask++;
  461. }
  462. if (srcAlpha != OPACITY_TRANSPARENT) {
  463. if (opacity != OPACITY_OPAQUE) {
  464. srcAlpha = UINT8_MULT(src[PIXEL_GRAY_ALPHA], opacity);
  465. }
  466. TQ_UINT8 srcBlend;
  467. if (dstAlpha == OPACITY_OPAQUE) {
  468. srcBlend = srcAlpha;
  469. } else {
  470. TQ_UINT8 newAlpha = dstAlpha + UINT8_MULT(OPACITY_OPAQUE - dstAlpha, srcAlpha);
  471. dst[PIXEL_GRAY_ALPHA] = newAlpha;
  472. if (newAlpha != 0) {
  473. srcBlend = UINT8_DIVIDE(srcAlpha, newAlpha);
  474. } else {
  475. srcBlend = srcAlpha;
  476. }
  477. }
  478. for (int channel = 0; channel < MAX_CHANNEL_GRAYSCALE; channel++) {
  479. TQ_UINT8 srcColor = src[channel];
  480. TQ_UINT8 dstColor = dst[channel];
  481. srcColor = UINT8_MULT(dstColor, dstColor + UINT8_MULT(2 * srcColor, UINT8_MAX - dstColor));
  482. TQ_UINT8 newColor = UINT8_BLEND(srcColor, dstColor, srcBlend);
  483. dst[channel] = newColor;
  484. }
  485. }
  486. columns--;
  487. src += MAX_CHANNEL_GRAYSCALEA;
  488. dst += MAX_CHANNEL_GRAYSCALEA;
  489. }
  490. rows--;
  491. srcRowStart += srcRowStride;
  492. dstRowStart += dstRowStride;
  493. if(maskRowStart)
  494. maskRowStart += maskRowStride;
  495. }
  496. }
  497. void KisGrayColorSpace::compositeDodge(TQ_UINT8 *dstRowStart, TQ_INT32 dstRowStride, const TQ_UINT8 *srcRowStart, TQ_INT32 srcRowStride, const TQ_UINT8 *maskRowStart, TQ_INT32 maskRowStride, TQ_INT32 rows, TQ_INT32 numColumns, TQ_UINT8 opacity)
  498. {
  499. while (rows > 0) {
  500. const TQ_UINT8 *src = srcRowStart;
  501. TQ_UINT8 *dst = dstRowStart;
  502. TQ_INT32 columns = numColumns;
  503. const TQ_UINT8 *mask = maskRowStart;
  504. while (columns > 0) {
  505. TQ_UINT8 srcAlpha = src[PIXEL_GRAY_ALPHA];
  506. TQ_UINT8 dstAlpha = dst[PIXEL_GRAY_ALPHA];
  507. srcAlpha = TQMIN(srcAlpha, dstAlpha);
  508. // apply the alphamask
  509. if(mask != 0)
  510. {
  511. if(*mask != OPACITY_OPAQUE)
  512. srcAlpha = UINT8_MULT(srcAlpha, *mask);
  513. mask++;
  514. }
  515. if (srcAlpha != OPACITY_TRANSPARENT) {
  516. if (opacity != OPACITY_OPAQUE) {
  517. srcAlpha = UINT8_MULT(src[PIXEL_GRAY_ALPHA], opacity);
  518. }
  519. TQ_UINT8 srcBlend;
  520. if (dstAlpha == OPACITY_OPAQUE) {
  521. srcBlend = srcAlpha;
  522. } else {
  523. TQ_UINT8 newAlpha = dstAlpha + UINT8_MULT(OPACITY_OPAQUE - dstAlpha, srcAlpha);
  524. dst[PIXEL_GRAY_ALPHA] = newAlpha;
  525. if (newAlpha != 0) {
  526. srcBlend = UINT8_DIVIDE(srcAlpha, newAlpha);
  527. } else {
  528. srcBlend = srcAlpha;
  529. }
  530. }
  531. for (int channel = 0; channel < MAX_CHANNEL_GRAYSCALE; channel++) {
  532. TQ_UINT8 srcColor = src[channel];
  533. TQ_UINT8 dstColor = dst[channel];
  534. srcColor = TQMIN((dstColor * (UINT8_MAX + 1)) / (UINT8_MAX + 1 - srcColor), UINT8_MAX);
  535. TQ_UINT8 newColor = UINT8_BLEND(srcColor, dstColor, srcBlend);
  536. dst[channel] = newColor;
  537. }
  538. }
  539. columns--;
  540. src += MAX_CHANNEL_GRAYSCALEA;
  541. dst += MAX_CHANNEL_GRAYSCALEA;
  542. }
  543. rows--;
  544. srcRowStart += srcRowStride;
  545. dstRowStart += dstRowStride;
  546. if(maskRowStart)
  547. maskRowStart += maskRowStride;
  548. }
  549. }
  550. void KisGrayColorSpace::compositeBurn(TQ_UINT8 *dstRowStart, TQ_INT32 dstRowStride, const TQ_UINT8 *srcRowStart, TQ_INT32 srcRowStride, const TQ_UINT8 *maskRowStart, TQ_INT32 maskRowStride, TQ_INT32 rows, TQ_INT32 numColumns, TQ_UINT8 opacity)
  551. {
  552. while (rows > 0) {
  553. const TQ_UINT8 *src = srcRowStart;
  554. TQ_UINT8 *dst = dstRowStart;
  555. TQ_INT32 columns = numColumns;
  556. const TQ_UINT8 *mask = maskRowStart;
  557. while (columns > 0) {
  558. TQ_UINT8 srcAlpha = src[PIXEL_GRAY_ALPHA];
  559. TQ_UINT8 dstAlpha = dst[PIXEL_GRAY_ALPHA];
  560. srcAlpha = TQMIN(srcAlpha, dstAlpha);
  561. // apply the alphamask
  562. if(mask != 0)
  563. {
  564. if(*mask != OPACITY_OPAQUE)
  565. srcAlpha = UINT8_MULT(srcAlpha, *mask);
  566. mask++;
  567. }
  568. if (srcAlpha != OPACITY_TRANSPARENT) {
  569. if (opacity != OPACITY_OPAQUE) {
  570. srcAlpha = UINT8_MULT(src[PIXEL_GRAY_ALPHA], opacity);
  571. }
  572. TQ_UINT8 srcBlend;
  573. if (dstAlpha == OPACITY_OPAQUE) {
  574. srcBlend = srcAlpha;
  575. } else {
  576. TQ_UINT8 newAlpha = dstAlpha + UINT8_MULT(OPACITY_OPAQUE - dstAlpha, srcAlpha);
  577. dst[PIXEL_GRAY_ALPHA] = newAlpha;
  578. if (newAlpha != 0) {
  579. srcBlend = UINT8_DIVIDE(srcAlpha, newAlpha);
  580. } else {
  581. srcBlend = srcAlpha;
  582. }
  583. }
  584. for (int channel = 0; channel < MAX_CHANNEL_GRAYSCALE; channel++) {
  585. TQ_UINT8 srcColor = src[channel];
  586. TQ_UINT8 dstColor = dst[channel];
  587. srcColor = kMin(((UINT8_MAX - dstColor) * (UINT8_MAX + 1)) / (srcColor + 1), UINT8_MAX);
  588. srcColor = kClamp(UINT8_MAX - srcColor, 0u, UINT8_MAX);
  589. TQ_UINT8 newColor = UINT8_BLEND(srcColor, dstColor, srcBlend);
  590. dst[channel] = newColor;
  591. }
  592. }
  593. columns--;
  594. src += MAX_CHANNEL_GRAYSCALEA;
  595. dst += MAX_CHANNEL_GRAYSCALEA;
  596. }
  597. rows--;
  598. srcRowStart += srcRowStride;
  599. dstRowStart += dstRowStride;
  600. if(maskRowStart)
  601. maskRowStart += maskRowStride;
  602. }
  603. }
  604. void KisGrayColorSpace::compositeDarken(TQ_UINT8 *dstRowStart, TQ_INT32 dstRowStride, const TQ_UINT8 *srcRowStart, TQ_INT32 srcRowStride, const TQ_UINT8 *maskRowStart, TQ_INT32 maskRowStride, TQ_INT32 rows, TQ_INT32 numColumns, TQ_UINT8 opacity)
  605. {
  606. while (rows > 0) {
  607. const TQ_UINT8 *src = srcRowStart;
  608. TQ_UINT8 *dst = dstRowStart;
  609. TQ_INT32 columns = numColumns;
  610. const TQ_UINT8 *mask = maskRowStart;
  611. while (columns > 0) {
  612. TQ_UINT8 srcAlpha = src[PIXEL_GRAY_ALPHA];
  613. TQ_UINT8 dstAlpha = dst[PIXEL_GRAY_ALPHA];
  614. srcAlpha = TQMIN(srcAlpha, dstAlpha);
  615. // apply the alphamask
  616. if(mask != 0)
  617. {
  618. if(*mask != OPACITY_OPAQUE)
  619. srcAlpha = UINT8_MULT(srcAlpha, *mask);
  620. mask++;
  621. }
  622. if (srcAlpha != OPACITY_TRANSPARENT) {
  623. if (opacity != OPACITY_OPAQUE) {
  624. srcAlpha = UINT8_MULT(src[PIXEL_GRAY_ALPHA], opacity);
  625. }
  626. TQ_UINT8 srcBlend;
  627. if (dstAlpha == OPACITY_OPAQUE) {
  628. srcBlend = srcAlpha;
  629. } else {
  630. TQ_UINT8 newAlpha = dstAlpha + UINT8_MULT(OPACITY_OPAQUE - dstAlpha, srcAlpha);
  631. dst[PIXEL_GRAY_ALPHA] = newAlpha;
  632. if (newAlpha != 0) {
  633. srcBlend = UINT8_DIVIDE(srcAlpha, newAlpha);
  634. } else {
  635. srcBlend = srcAlpha;
  636. }
  637. }
  638. for (int channel = 0; channel < MAX_CHANNEL_GRAYSCALE; channel++) {
  639. TQ_UINT8 srcColor = src[channel];
  640. TQ_UINT8 dstColor = dst[channel];
  641. srcColor = TQMIN(srcColor, dstColor);
  642. TQ_UINT8 newColor = UINT8_BLEND(srcColor, dstColor, srcBlend);
  643. dst[channel] = newColor;
  644. }
  645. }
  646. columns--;
  647. src += MAX_CHANNEL_GRAYSCALEA;
  648. dst += MAX_CHANNEL_GRAYSCALEA;
  649. }
  650. rows--;
  651. srcRowStart += srcRowStride;
  652. dstRowStart += dstRowStride;
  653. if(maskRowStart)
  654. maskRowStart += maskRowStride;
  655. }
  656. }
  657. void KisGrayColorSpace::compositeLighten(TQ_UINT8 *dstRowStart, TQ_INT32 dstRowStride, const TQ_UINT8 *srcRowStart, TQ_INT32 srcRowStride, const TQ_UINT8 *maskRowStart, TQ_INT32 maskRowStride, TQ_INT32 rows, TQ_INT32 numColumns, TQ_UINT8 opacity)
  658. {
  659. while (rows > 0) {
  660. const TQ_UINT8 *src = srcRowStart;
  661. TQ_UINT8 *dst = dstRowStart;
  662. TQ_INT32 columns = numColumns;
  663. const TQ_UINT8 *mask = maskRowStart;
  664. while (columns > 0) {
  665. TQ_UINT8 srcAlpha = src[PIXEL_GRAY_ALPHA];
  666. TQ_UINT8 dstAlpha = dst[PIXEL_GRAY_ALPHA];
  667. srcAlpha = TQMIN(srcAlpha, dstAlpha);
  668. // apply the alphamask
  669. if(mask != 0)
  670. {
  671. if(*mask != OPACITY_OPAQUE)
  672. srcAlpha = UINT8_MULT(srcAlpha, *mask);
  673. mask++;
  674. }
  675. if (srcAlpha != OPACITY_TRANSPARENT) {
  676. if (opacity != OPACITY_OPAQUE) {
  677. srcAlpha = UINT8_MULT(src[PIXEL_GRAY_ALPHA], opacity);
  678. }
  679. TQ_UINT8 srcBlend;
  680. if (dstAlpha == OPACITY_OPAQUE) {
  681. srcBlend = srcAlpha;
  682. } else {
  683. TQ_UINT8 newAlpha = dstAlpha + UINT8_MULT(OPACITY_OPAQUE - dstAlpha, srcAlpha);
  684. dst[PIXEL_GRAY_ALPHA] = newAlpha;
  685. if (newAlpha != 0) {
  686. srcBlend = UINT8_DIVIDE(srcAlpha, newAlpha);
  687. } else {
  688. srcBlend = srcAlpha;
  689. }
  690. }
  691. for (int channel = 0; channel < MAX_CHANNEL_GRAYSCALE; channel++) {
  692. TQ_UINT8 srcColor = src[channel];
  693. TQ_UINT8 dstColor = dst[channel];
  694. srcColor = TQMAX(srcColor, dstColor);
  695. TQ_UINT8 newColor = UINT8_BLEND(srcColor, dstColor, srcBlend);
  696. dst[channel] = newColor;
  697. }
  698. }
  699. columns--;
  700. src += MAX_CHANNEL_GRAYSCALEA;
  701. dst += MAX_CHANNEL_GRAYSCALEA;
  702. }
  703. rows--;
  704. srcRowStart += srcRowStride;
  705. dstRowStart += dstRowStride;
  706. if(maskRowStart)
  707. maskRowStart += maskRowStride;
  708. }
  709. }
  710. void KisGrayColorSpace::compositeErase(TQ_UINT8 *dst,
  711. TQ_INT32 dstRowSize,
  712. const TQ_UINT8 *src,
  713. TQ_INT32 srcRowSize,
  714. const TQ_UINT8 *srcAlphaMask,
  715. TQ_INT32 maskRowStride,
  716. TQ_INT32 rows,
  717. TQ_INT32 cols,
  718. TQ_UINT8 /*opacity*/)
  719. {
  720. TQ_INT32 i;
  721. TQ_UINT8 srcAlpha;
  722. while (rows-- > 0)
  723. {
  724. const TQ_UINT8 *s = src;
  725. TQ_UINT8 *d = dst;
  726. const TQ_UINT8 *mask = srcAlphaMask;
  727. for (i = cols; i > 0; i--, s+=MAX_CHANNEL_GRAYSCALEA, d+=MAX_CHANNEL_GRAYSCALEA)
  728. {
  729. srcAlpha = s[PIXEL_GRAY_ALPHA];
  730. // apply the alphamask
  731. if(mask != 0)
  732. {
  733. if(*mask != OPACITY_OPAQUE)
  734. srcAlpha = UINT8_BLEND(srcAlpha, OPACITY_OPAQUE, *mask);
  735. mask++;
  736. }
  737. d[PIXEL_GRAY_ALPHA] = UINT8_MULT(srcAlpha, d[PIXEL_GRAY_ALPHA]);
  738. }
  739. dst += dstRowSize;
  740. if(srcAlphaMask)
  741. srcAlphaMask += maskRowStride;
  742. src += srcRowSize;
  743. }
  744. }
  745. void KisGrayColorSpace::compositeAlphaDarken(TQ_UINT8 *dstRowStart, TQ_INT32 dstRowStride,
  746. const TQ_UINT8 *srcRowStart, TQ_INT32 srcRowStride,
  747. const TQ_UINT8 *maskRowStart, TQ_INT32 maskRowStride,
  748. TQ_INT32 rows, TQ_INT32 numColumns, TQ_UINT8 opacity)
  749. {
  750. while (rows > 0) {
  751. const TQ_UINT8 *src = srcRowStart;
  752. TQ_UINT8 *dst = dstRowStart;
  753. const TQ_UINT8 *mask = maskRowStart;
  754. TQ_INT32 columns = numColumns;
  755. while (columns > 0) {
  756. TQ_UINT8 srcAlpha = src[PIXEL_GRAY_ALPHA];
  757. TQ_UINT8 dstAlpha = dst[PIXEL_GRAY_ALPHA];
  758. // apply the alphamask
  759. if(mask != 0)
  760. {
  761. if(*mask != OPACITY_OPAQUE)
  762. srcAlpha = UINT8_MULT(srcAlpha, *mask);
  763. mask++;
  764. }
  765. if (opacity != OPACITY_OPAQUE) {
  766. srcAlpha = UINT8_MULT(srcAlpha, opacity);
  767. }
  768. if (srcAlpha != OPACITY_TRANSPARENT && srcAlpha >= dstAlpha) {
  769. dst[PIXEL_GRAY_ALPHA] = srcAlpha;
  770. memcpy(dst, src, MAX_CHANNEL_GRAYSCALE * sizeof(TQ_UINT8));
  771. }
  772. columns--;
  773. src += MAX_CHANNEL_GRAYSCALEA;
  774. dst += MAX_CHANNEL_GRAYSCALEA;
  775. }
  776. rows--;
  777. srcRowStart += srcRowStride;
  778. dstRowStart += dstRowStride;
  779. if(maskRowStart)
  780. maskRowStart += maskRowStride;
  781. }
  782. }