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_rgb_colorspace.cc 50KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501
  1. /*
  2. * Copyright (c) 2002 Patrick Julien <freak@codepimps.org>
  3. * Copyright (c) 2004 Boudewijn Rempt <boud@valdyas.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. */
  19. #include <config.h>
  20. #include <limits.h>
  21. #include <stdlib.h>
  22. #include LCMS_HEADER
  23. #include <tqimage.h>
  24. #include <tqcolor.h>
  25. #include <kdebug.h>
  26. #include <tdelocale.h>
  27. #include "kis_rgb_colorspace.h"
  28. #include "kis_u8_base_colorspace.h"
  29. #include "kis_color_conversions.h"
  30. #include "kis_integer_maths.h"
  31. #include "kis_colorspace_factory_registry.h"
  32. #include "composite.h"
  33. #define downscale(quantum) (quantum) //((unsigned char) ((quantum)/257UL))
  34. #define upscale(value) (value) // ((TQ_UINT8) (257UL*(value)))
  35. namespace {
  36. const TQ_INT32 MAX_CHANNEL_RGB = 3;
  37. const TQ_INT32 MAX_CHANNEL_RGBA = 4;
  38. }
  39. KisRgbColorSpace::KisRgbColorSpace(KisColorSpaceFactoryRegistry * parent, KisProfile *p) :
  40. KisU8BaseColorSpace(KisID("RGBA", i18n("RGB (8-bit integer/channel)")), TYPE_BGRA_8, icSigRgbData, parent, p)
  41. {
  42. m_channels.push_back(new KisChannelInfo(i18n("Red"), i18n("R"), 2, KisChannelInfo::COLOR, KisChannelInfo::UINT8, 1, TQColor(255,0,0)));
  43. m_channels.push_back(new KisChannelInfo(i18n("Green"), i18n("G"), 1, KisChannelInfo::COLOR, KisChannelInfo::UINT8, 1, TQColor(0,255,0)));
  44. m_channels.push_back(new KisChannelInfo(i18n("Blue"), i18n("B"), 0, KisChannelInfo::COLOR, KisChannelInfo::UINT8, 1, TQColor(0,0,255)));
  45. m_channels.push_back(new KisChannelInfo(i18n("Alpha"), i18n("A"), 3, KisChannelInfo::ALPHA, KisChannelInfo::UINT8));
  46. m_alphaPos = PIXEL_ALPHA;
  47. init();
  48. }
  49. KisRgbColorSpace::~KisRgbColorSpace()
  50. {
  51. }
  52. void KisRgbColorSpace::setPixel(TQ_UINT8 *pixel, TQ_UINT8 red, TQ_UINT8 green, TQ_UINT8 blue, TQ_UINT8 alpha) const
  53. {
  54. pixel[PIXEL_RED] = red;
  55. pixel[PIXEL_GREEN] = green;
  56. pixel[PIXEL_BLUE] = blue;
  57. pixel[PIXEL_ALPHA] = alpha;
  58. }
  59. void KisRgbColorSpace::getPixel(const TQ_UINT8 *pixel, TQ_UINT8 *red, TQ_UINT8 *green, TQ_UINT8 *blue, TQ_UINT8 *alpha) const
  60. {
  61. *red = pixel[PIXEL_RED];
  62. *green = pixel[PIXEL_GREEN];
  63. *blue = pixel[PIXEL_BLUE];
  64. *alpha = pixel[PIXEL_ALPHA];
  65. }
  66. void KisRgbColorSpace::mixColors(const TQ_UINT8 **colors, const TQ_UINT8 *weights, TQ_UINT32 nColors, TQ_UINT8 *dst) const
  67. {
  68. TQ_UINT32 totalRed = 0, totalGreen = 0, totalBlue = 0, totalAlpha = 0;
  69. while (nColors--)
  70. {
  71. TQ_UINT32 alpha = (*colors)[PIXEL_ALPHA];
  72. // although we only mult by weight and not by weight*256/255
  73. // we divide by the same amount later, so there is no need
  74. TQ_UINT32 alphaTimesWeight = alpha * *weights;
  75. totalRed += (*colors)[PIXEL_RED] * alphaTimesWeight;
  76. totalGreen += (*colors)[PIXEL_GREEN] * alphaTimesWeight;
  77. totalBlue += (*colors)[PIXEL_BLUE] * alphaTimesWeight;
  78. totalAlpha += alphaTimesWeight;
  79. weights++;
  80. colors++;
  81. }
  82. // note this is correct - if you look at the above calculation
  83. if (totalAlpha > 255*255) totalAlpha = 255*255;
  84. // Divide by 255.
  85. dst[PIXEL_ALPHA] =(((totalAlpha + 0x80)>>8)+totalAlpha + 0x80) >>8;
  86. if (totalAlpha > 0) {
  87. totalRed = totalRed / totalAlpha;
  88. totalGreen = totalGreen / totalAlpha;
  89. totalBlue = totalBlue / totalAlpha;
  90. } // else the values are already 0 too
  91. TQ_UINT32 dstRed = totalRed;
  92. //Q_ASSERT(dstRed <= 255);
  93. if (dstRed > 255) dstRed = 255;
  94. dst[PIXEL_RED] = dstRed;
  95. TQ_UINT32 dstGreen = totalGreen;
  96. //Q_ASSERT(dstGreen <= 255);
  97. if (dstGreen > 255) dstGreen = 255;
  98. dst[PIXEL_GREEN] = dstGreen;
  99. TQ_UINT32 dstBlue = totalBlue;
  100. //Q_ASSERT(dstBlue <= 255);
  101. if (dstBlue > 255) dstBlue = 255;
  102. dst[PIXEL_BLUE] = dstBlue;
  103. }
  104. void KisRgbColorSpace::convolveColors(TQ_UINT8** colors, TQ_INT32* kernelValues, KisChannelInfo::enumChannelFlags channelFlags, TQ_UINT8 *dst, TQ_INT32 factor, TQ_INT32 offset, TQ_INT32 nColors) const
  105. {
  106. TQ_INT64 totalRed = 0, totalGreen = 0, totalBlue = 0, totalAlpha = 0;
  107. TQ_INT32 totalWeight = 0, totalWeightTransparent = 0;
  108. while (nColors--)
  109. {
  110. TQ_INT32 weight = *kernelValues;
  111. if (weight != 0) {
  112. if((*colors)[PIXEL_ALPHA] == 0)
  113. {
  114. totalWeightTransparent += weight;
  115. } else {
  116. totalRed += (*colors)[PIXEL_RED] * weight;
  117. totalGreen += (*colors)[PIXEL_GREEN] * weight;
  118. totalBlue += (*colors)[PIXEL_BLUE] * weight;
  119. }
  120. totalAlpha += (*colors)[PIXEL_ALPHA] * weight;
  121. totalWeight += weight;
  122. }
  123. colors++;
  124. kernelValues++;
  125. }
  126. if(totalWeightTransparent == 0)
  127. {
  128. if (channelFlags & KisChannelInfo::FLAG_COLOR) {
  129. dst[PIXEL_RED] = CLAMP((totalRed / factor) + offset, 0, TQ_UINT8_MAX);
  130. dst[PIXEL_GREEN] = CLAMP((totalGreen / factor) + offset, 0, TQ_UINT8_MAX);
  131. dst[PIXEL_BLUE] = CLAMP((totalBlue / factor) + offset, 0, TQ_UINT8_MAX);
  132. }
  133. if (channelFlags & KisChannelInfo::FLAG_ALPHA) {
  134. dst[PIXEL_ALPHA] = CLAMP((totalAlpha/ factor) + offset, 0, TQ_UINT8_MAX);
  135. }
  136. } else if(totalWeightTransparent != totalWeight && (channelFlags & KisChannelInfo::FLAG_COLOR)) {
  137. if(totalWeight == factor)
  138. {
  139. TQ_INT64 a = ( totalWeight - totalWeightTransparent );
  140. dst[PIXEL_RED] = CLAMP((totalRed / a) + offset, 0, TQ_UINT8_MAX);
  141. dst[PIXEL_GREEN] = CLAMP((totalGreen / a) + offset, 0, TQ_UINT8_MAX);
  142. dst[PIXEL_BLUE] = CLAMP((totalBlue / a) + offset, 0, TQ_UINT8_MAX);
  143. } else {
  144. double a = totalWeight / ( factor * ( totalWeight - totalWeightTransparent ) ); // use double as it can saturate
  145. dst[PIXEL_RED] = CLAMP( (TQ_UINT8)(totalRed * a) + offset, 0, TQ_UINT8_MAX);
  146. dst[PIXEL_GREEN] = CLAMP( (TQ_UINT8)(totalGreen * a) + offset, 0, TQ_UINT8_MAX);
  147. dst[PIXEL_BLUE] = CLAMP( (TQ_UINT8)(totalBlue * a) + offset, 0, TQ_UINT8_MAX);
  148. }
  149. }
  150. if (channelFlags & KisChannelInfo::FLAG_ALPHA) {
  151. dst[PIXEL_ALPHA] = CLAMP((totalAlpha/ factor) + offset, 0, TQ_UINT8_MAX);
  152. }
  153. }
  154. void KisRgbColorSpace::invertColor(TQ_UINT8 * src, TQ_INT32 nPixels)
  155. {
  156. TQ_UINT32 psize = pixelSize();
  157. while (nPixels--)
  158. {
  159. src[PIXEL_RED] = TQ_UINT8_MAX - src[PIXEL_RED];
  160. src[PIXEL_GREEN] = TQ_UINT8_MAX - src[PIXEL_GREEN];
  161. src[PIXEL_BLUE] = TQ_UINT8_MAX - src[PIXEL_BLUE];
  162. src += psize;
  163. }
  164. }
  165. void KisRgbColorSpace::darken(const TQ_UINT8 * src, TQ_UINT8 * dst, TQ_INT32 shade, bool compensate, double compensation, TQ_INT32 nPixels) const
  166. {
  167. TQ_UINT32 pSize = pixelSize();
  168. while (nPixels--) {
  169. if (compensate) {
  170. dst[PIXEL_RED] = (TQ_INT8) TQMIN(255,((src[PIXEL_RED] * shade) / (compensation * 255)));
  171. dst[PIXEL_GREEN] = (TQ_INT8) TQMIN(255,((src[PIXEL_GREEN] * shade) / (compensation * 255)));
  172. dst[PIXEL_BLUE] = (TQ_INT8) TQMIN(255,((src[PIXEL_BLUE] * shade) / (compensation * 255)));
  173. }
  174. else {
  175. dst[PIXEL_RED] = (TQ_INT8) TQMIN(255, (src[PIXEL_RED] * shade / 255));
  176. dst[PIXEL_BLUE] = (TQ_INT8) TQMIN(255, (src[PIXEL_BLUE] * shade / 255));
  177. dst[PIXEL_GREEN] = (TQ_INT8) TQMIN(255, (src[PIXEL_GREEN] * shade / 255));
  178. }
  179. dst += pSize;
  180. src += pSize;
  181. }
  182. }
  183. TQ_UINT8 KisRgbColorSpace::intensity8(const TQ_UINT8 * src) const
  184. {
  185. return (TQ_UINT8)((src[PIXEL_RED] * 0.30 + src[PIXEL_GREEN] * 0.59 + src[PIXEL_BLUE] * 0.11) + 0.5);
  186. }
  187. TQValueVector<KisChannelInfo *> KisRgbColorSpace::channels() const
  188. {
  189. return m_channels;
  190. }
  191. TQ_UINT32 KisRgbColorSpace::nChannels() const
  192. {
  193. return MAX_CHANNEL_RGBA;
  194. }
  195. TQ_UINT32 KisRgbColorSpace::nColorChannels() const
  196. {
  197. return MAX_CHANNEL_RGB;
  198. }
  199. TQ_UINT32 KisRgbColorSpace::pixelSize() const
  200. {
  201. return MAX_CHANNEL_RGBA;
  202. }
  203. TQImage KisRgbColorSpace::convertToTQImage(const TQ_UINT8 *data, TQ_INT32 width, TQ_INT32 height,
  204. KisProfile * dstProfile,
  205. TQ_INT32 renderingIntent, float /*exposure*/)
  206. {
  207. Q_ASSERT(data);
  208. TQImage img = TQImage(const_cast<TQ_UINT8 *>(data), width, height, 32, 0, 0, TQImage::LittleEndian);
  209. img.setAlphaBuffer(true);
  210. // XXX: The previous version of this code used the quantum data directly
  211. // as an optimisation. We're introducing a copy overhead here which could
  212. // be factored out again if needed.
  213. img = img.copy();
  214. if (dstProfile != 0) {
  215. KisColorSpace *dstCS = m_parent->getColorSpace(KisID("RGBA",""), dstProfile->productName());
  216. convertPixelsTo(img.bits(),
  217. img.bits(), dstCS,
  218. width * height, renderingIntent);
  219. }
  220. return img;
  221. }
  222. void KisRgbColorSpace::compositeOver(TQ_UINT8 *dstRowStart, TQ_INT32 dstRowStride,
  223. const TQ_UINT8 *srcRowStart, TQ_INT32 srcRowStride,
  224. const TQ_UINT8 *maskRowStart, TQ_INT32 maskRowStride,
  225. TQ_INT32 rows, TQ_INT32 numColumns, TQ_UINT8 opacity)
  226. {
  227. while (rows > 0) {
  228. const TQ_UINT8 *src = srcRowStart;
  229. TQ_UINT8 *dst = dstRowStart;
  230. const TQ_UINT8 *mask = maskRowStart;
  231. TQ_INT32 columns = numColumns;
  232. while (columns > 0) {
  233. TQ_UINT8 srcAlpha = src[PIXEL_ALPHA];
  234. // apply the alphamask
  235. if(mask != 0)
  236. {
  237. if(*mask != OPACITY_OPAQUE)
  238. srcAlpha = UINT8_MULT(srcAlpha, *mask);
  239. mask++;
  240. }
  241. if (srcAlpha != OPACITY_TRANSPARENT) {
  242. if (opacity != OPACITY_OPAQUE) {
  243. srcAlpha = UINT8_MULT(srcAlpha, opacity);
  244. }
  245. if (srcAlpha == OPACITY_OPAQUE) {
  246. memcpy(dst, src, MAX_CHANNEL_RGBA * sizeof(TQ_UINT8));
  247. } else {
  248. TQ_UINT8 dstAlpha = dst[PIXEL_ALPHA];
  249. TQ_UINT8 srcBlend;
  250. if (dstAlpha == OPACITY_OPAQUE) {
  251. srcBlend = srcAlpha;
  252. } else {
  253. TQ_UINT8 newAlpha = dstAlpha + UINT8_MULT(OPACITY_OPAQUE - dstAlpha, srcAlpha);
  254. dst[PIXEL_ALPHA] = newAlpha;
  255. if (newAlpha != 0) {
  256. srcBlend = UINT8_DIVIDE(srcAlpha, newAlpha);
  257. } else {
  258. srcBlend = srcAlpha;
  259. }
  260. }
  261. if (srcBlend == OPACITY_OPAQUE) {
  262. memcpy(dst, src, MAX_CHANNEL_RGB * sizeof(TQ_UINT8));
  263. } else {
  264. dst[PIXEL_RED] = UINT8_BLEND(src[PIXEL_RED], dst[PIXEL_RED], srcBlend);
  265. dst[PIXEL_GREEN] = UINT8_BLEND(src[PIXEL_GREEN], dst[PIXEL_GREEN], srcBlend);
  266. dst[PIXEL_BLUE] = UINT8_BLEND(src[PIXEL_BLUE], dst[PIXEL_BLUE], srcBlend);
  267. }
  268. }
  269. }
  270. columns--;
  271. src += MAX_CHANNEL_RGBA;
  272. dst += MAX_CHANNEL_RGBA;
  273. }
  274. rows--;
  275. srcRowStart += srcRowStride;
  276. dstRowStart += dstRowStride;
  277. if(maskRowStart)
  278. maskRowStart += maskRowStride;
  279. }
  280. }
  281. void KisRgbColorSpace::compositeAlphaDarken(TQ_UINT8 *dstRowStart, TQ_INT32 dstRowStride,
  282. const TQ_UINT8 *srcRowStart, TQ_INT32 srcRowStride,
  283. const TQ_UINT8 *maskRowStart, TQ_INT32 maskRowStride,
  284. TQ_INT32 rows, TQ_INT32 numColumns, TQ_UINT8 opacity)
  285. {
  286. while (rows > 0) {
  287. const TQ_UINT8 *src = srcRowStart;
  288. TQ_UINT8 *dst = dstRowStart;
  289. const TQ_UINT8 *mask = maskRowStart;
  290. TQ_INT32 columns = numColumns;
  291. while (columns > 0) {
  292. TQ_UINT8 srcAlpha = src[PIXEL_ALPHA];
  293. TQ_UINT8 dstAlpha = dst[PIXEL_ALPHA];
  294. // apply the alphamask
  295. if(mask != 0)
  296. {
  297. if(*mask != OPACITY_OPAQUE)
  298. srcAlpha = UINT8_MULT(srcAlpha, *mask);
  299. mask++;
  300. }
  301. if (opacity != OPACITY_OPAQUE) {
  302. srcAlpha = UINT8_MULT(srcAlpha, opacity);
  303. }
  304. if (srcAlpha != OPACITY_TRANSPARENT && srcAlpha >= dstAlpha) {
  305. dst[PIXEL_ALPHA] = srcAlpha;
  306. memcpy(dst, src, MAX_CHANNEL_RGB * sizeof(TQ_UINT8));
  307. }
  308. columns--;
  309. src += MAX_CHANNEL_RGBA;
  310. dst += MAX_CHANNEL_RGBA;
  311. }
  312. rows--;
  313. srcRowStart += srcRowStride;
  314. dstRowStart += dstRowStride;
  315. if(maskRowStart)
  316. maskRowStart += maskRowStride;
  317. }
  318. }
  319. void KisRgbColorSpace::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)
  320. {
  321. while (rows > 0) {
  322. const TQ_UINT8 *src = srcRowStart;
  323. TQ_UINT8 *dst = dstRowStart;
  324. TQ_INT32 columns = numColumns;
  325. const TQ_UINT8 *mask = maskRowStart;
  326. while (columns > 0) {
  327. TQ_UINT8 srcAlpha = src[PIXEL_ALPHA];
  328. TQ_UINT8 dstAlpha = dst[PIXEL_ALPHA];
  329. srcAlpha = TQMIN(srcAlpha, dstAlpha);
  330. // apply the alphamask
  331. if(mask != 0)
  332. {
  333. if(*mask != OPACITY_OPAQUE)
  334. srcAlpha = UINT8_MULT(srcAlpha, *mask);
  335. mask++;
  336. }
  337. if (srcAlpha != OPACITY_TRANSPARENT) {
  338. if (opacity != OPACITY_OPAQUE) {
  339. srcAlpha = UINT8_MULT(src[PIXEL_ALPHA], opacity);
  340. }
  341. TQ_UINT8 srcBlend;
  342. if (dstAlpha == OPACITY_OPAQUE) {
  343. srcBlend = srcAlpha;
  344. } else {
  345. TQ_UINT8 newAlpha = dstAlpha + UINT8_MULT(OPACITY_OPAQUE - dstAlpha, srcAlpha);
  346. dst[PIXEL_ALPHA] = newAlpha;
  347. if (newAlpha != 0) {
  348. srcBlend = UINT8_DIVIDE(srcAlpha, newAlpha);
  349. } else {
  350. srcBlend = srcAlpha;
  351. }
  352. }
  353. TQ_UINT8 srcColor = src[PIXEL_RED];
  354. TQ_UINT8 dstColor = dst[PIXEL_RED];
  355. srcColor = UINT8_MULT(srcColor, dstColor);
  356. dst[PIXEL_RED] = UINT8_BLEND(srcColor, dstColor, srcBlend);
  357. srcColor = src[PIXEL_GREEN];
  358. dstColor = dst[PIXEL_GREEN];
  359. srcColor = UINT8_MULT(srcColor, dstColor);
  360. dst[PIXEL_GREEN] = UINT8_BLEND(srcColor, dstColor, srcBlend);
  361. srcColor = src[PIXEL_BLUE];
  362. dstColor = dst[PIXEL_BLUE];
  363. srcColor = UINT8_MULT(srcColor, dstColor);
  364. dst[PIXEL_BLUE] = UINT8_BLEND(srcColor, dstColor, srcBlend);
  365. }
  366. columns--;
  367. src += MAX_CHANNEL_RGBA;
  368. dst += MAX_CHANNEL_RGBA;
  369. }
  370. rows--;
  371. srcRowStart += srcRowStride;
  372. dstRowStart += dstRowStride;
  373. if(maskRowStart)
  374. maskRowStart += maskRowStride;
  375. }
  376. }
  377. void KisRgbColorSpace::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)
  378. {
  379. while (rows > 0) {
  380. const TQ_UINT8 *src = srcRowStart;
  381. TQ_UINT8 *dst = dstRowStart;
  382. TQ_INT32 columns = numColumns;
  383. const TQ_UINT8 *mask = maskRowStart;
  384. while (columns > 0) {
  385. TQ_UINT8 srcAlpha = src[PIXEL_ALPHA];
  386. TQ_UINT8 dstAlpha = dst[PIXEL_ALPHA];
  387. srcAlpha = TQMIN(srcAlpha, dstAlpha);
  388. // apply the alphamask
  389. if(mask != 0)
  390. {
  391. if(*mask != OPACITY_OPAQUE)
  392. srcAlpha = UINT8_MULT(srcAlpha, *mask);
  393. mask++;
  394. }
  395. if (srcAlpha != OPACITY_TRANSPARENT) {
  396. if (opacity != OPACITY_OPAQUE) {
  397. srcAlpha = UINT8_MULT(src[PIXEL_ALPHA], opacity);
  398. }
  399. TQ_UINT8 srcBlend;
  400. if (dstAlpha == OPACITY_OPAQUE) {
  401. srcBlend = srcAlpha;
  402. } else {
  403. TQ_UINT8 newAlpha = dstAlpha + UINT8_MULT(OPACITY_OPAQUE - dstAlpha, srcAlpha);
  404. dst[PIXEL_ALPHA] = newAlpha;
  405. if (newAlpha != 0) {
  406. srcBlend = UINT8_DIVIDE(srcAlpha, newAlpha);
  407. } else {
  408. srcBlend = srcAlpha;
  409. }
  410. }
  411. for (int channel = 0; channel < MAX_CHANNEL_RGB; channel++) {
  412. TQ_UINT8 srcColor = src[channel];
  413. TQ_UINT8 dstColor = dst[channel];
  414. srcColor = TQMIN((dstColor * (UINT8_MAX + 1u) + (srcColor / 2u)) / (1u + srcColor), UINT8_MAX);
  415. TQ_UINT8 newColor = UINT8_BLEND(srcColor, dstColor, srcBlend);
  416. dst[channel] = newColor;
  417. }
  418. }
  419. columns--;
  420. src += MAX_CHANNEL_RGBA;
  421. dst += MAX_CHANNEL_RGBA;
  422. }
  423. rows--;
  424. srcRowStart += srcRowStride;
  425. dstRowStart += dstRowStride;
  426. if(maskRowStart)
  427. maskRowStart += maskRowStride;
  428. }
  429. }
  430. void KisRgbColorSpace::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)
  431. {
  432. while (rows > 0) {
  433. const TQ_UINT8 *src = srcRowStart;
  434. TQ_UINT8 *dst = dstRowStart;
  435. TQ_INT32 columns = numColumns;
  436. const TQ_UINT8 *mask = maskRowStart;
  437. while (columns > 0) {
  438. TQ_UINT8 srcAlpha = src[PIXEL_ALPHA];
  439. TQ_UINT8 dstAlpha = dst[PIXEL_ALPHA];
  440. srcAlpha = TQMIN(srcAlpha, dstAlpha);
  441. // apply the alphamask
  442. if(mask != 0)
  443. {
  444. if(*mask != OPACITY_OPAQUE)
  445. srcAlpha = UINT8_MULT(srcAlpha, *mask);
  446. mask++;
  447. }
  448. if (srcAlpha != OPACITY_TRANSPARENT) {
  449. if (opacity != OPACITY_OPAQUE) {
  450. srcAlpha = UINT8_MULT(src[PIXEL_ALPHA], opacity);
  451. }
  452. TQ_UINT8 srcBlend;
  453. if (dstAlpha == OPACITY_OPAQUE) {
  454. srcBlend = srcAlpha;
  455. } else {
  456. TQ_UINT8 newAlpha = dstAlpha + UINT8_MULT(OPACITY_OPAQUE - dstAlpha, srcAlpha);
  457. dst[PIXEL_ALPHA] = newAlpha;
  458. if (newAlpha != 0) {
  459. srcBlend = UINT8_DIVIDE(srcAlpha, newAlpha);
  460. } else {
  461. srcBlend = srcAlpha;
  462. }
  463. }
  464. for (int channel = 0; channel < MAX_CHANNEL_RGB; channel++) {
  465. TQ_UINT8 srcColor = src[channel];
  466. TQ_UINT8 dstColor = dst[channel];
  467. srcColor = UINT8_MAX - UINT8_MULT(UINT8_MAX - dstColor, UINT8_MAX - srcColor);
  468. TQ_UINT8 newColor = UINT8_BLEND(srcColor, dstColor, srcBlend);
  469. dst[channel] = newColor;
  470. }
  471. }
  472. columns--;
  473. src += MAX_CHANNEL_RGBA;
  474. dst += MAX_CHANNEL_RGBA;
  475. }
  476. rows--;
  477. srcRowStart += srcRowStride;
  478. dstRowStart += dstRowStride;
  479. if(maskRowStart)
  480. maskRowStart += maskRowStride;
  481. }
  482. }
  483. void KisRgbColorSpace::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)
  484. {
  485. while (rows > 0) {
  486. const TQ_UINT8 *src = srcRowStart;
  487. TQ_UINT8 *dst = dstRowStart;
  488. TQ_INT32 columns = numColumns;
  489. const TQ_UINT8 *mask = maskRowStart;
  490. while (columns > 0) {
  491. TQ_UINT8 srcAlpha = src[PIXEL_ALPHA];
  492. TQ_UINT8 dstAlpha = dst[PIXEL_ALPHA];
  493. srcAlpha = TQMIN(srcAlpha, dstAlpha);
  494. // apply the alphamask
  495. if(mask != 0)
  496. {
  497. if(*mask != OPACITY_OPAQUE)
  498. srcAlpha = UINT8_MULT(srcAlpha, *mask);
  499. mask++;
  500. }
  501. if (srcAlpha != OPACITY_TRANSPARENT) {
  502. if (opacity != OPACITY_OPAQUE) {
  503. srcAlpha = UINT8_MULT(src[PIXEL_ALPHA], opacity);
  504. }
  505. TQ_UINT8 srcBlend;
  506. if (dstAlpha == OPACITY_OPAQUE) {
  507. srcBlend = srcAlpha;
  508. } else {
  509. TQ_UINT8 newAlpha = dstAlpha + UINT8_MULT(OPACITY_OPAQUE - dstAlpha, srcAlpha);
  510. dst[PIXEL_ALPHA] = newAlpha;
  511. if (newAlpha != 0) {
  512. srcBlend = UINT8_DIVIDE(srcAlpha, newAlpha);
  513. } else {
  514. srcBlend = srcAlpha;
  515. }
  516. }
  517. for (int channel = 0; channel < MAX_CHANNEL_RGB; channel++) {
  518. TQ_UINT8 srcColor = src[channel];
  519. TQ_UINT8 dstColor = dst[channel];
  520. srcColor = UINT8_MULT(dstColor, dstColor + UINT8_MULT(2 * srcColor, UINT8_MAX - dstColor));
  521. TQ_UINT8 newColor = UINT8_BLEND(srcColor, dstColor, srcBlend);
  522. dst[channel] = newColor;
  523. }
  524. }
  525. columns--;
  526. src += MAX_CHANNEL_RGBA;
  527. dst += MAX_CHANNEL_RGBA;
  528. }
  529. rows--;
  530. srcRowStart += srcRowStride;
  531. dstRowStart += dstRowStride;
  532. if(maskRowStart)
  533. maskRowStart += maskRowStride;
  534. }
  535. }
  536. void KisRgbColorSpace::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)
  537. {
  538. while (rows > 0) {
  539. const TQ_UINT8 *src = srcRowStart;
  540. TQ_UINT8 *dst = dstRowStart;
  541. TQ_INT32 columns = numColumns;
  542. const TQ_UINT8 *mask = maskRowStart;
  543. while (columns > 0) {
  544. TQ_UINT8 srcAlpha = src[PIXEL_ALPHA];
  545. TQ_UINT8 dstAlpha = dst[PIXEL_ALPHA];
  546. srcAlpha = TQMIN(srcAlpha, dstAlpha);
  547. // apply the alphamask
  548. if(mask != 0)
  549. {
  550. if(*mask != OPACITY_OPAQUE)
  551. srcAlpha = UINT8_MULT(srcAlpha, *mask);
  552. mask++;
  553. }
  554. if (srcAlpha != OPACITY_TRANSPARENT) {
  555. if (opacity != OPACITY_OPAQUE) {
  556. srcAlpha = UINT8_MULT(src[PIXEL_ALPHA], opacity);
  557. }
  558. TQ_UINT8 srcBlend;
  559. if (dstAlpha == OPACITY_OPAQUE) {
  560. srcBlend = srcAlpha;
  561. } else {
  562. TQ_UINT8 newAlpha = dstAlpha + UINT8_MULT(OPACITY_OPAQUE - dstAlpha, srcAlpha);
  563. dst[PIXEL_ALPHA] = newAlpha;
  564. if (newAlpha != 0) {
  565. srcBlend = UINT8_DIVIDE(srcAlpha, newAlpha);
  566. } else {
  567. srcBlend = srcAlpha;
  568. }
  569. }
  570. for (int channel = 0; channel < MAX_CHANNEL_RGB; channel++) {
  571. TQ_UINT8 srcColor = src[channel];
  572. TQ_UINT8 dstColor = dst[channel];
  573. srcColor = TQMIN((dstColor * (UINT8_MAX + 1)) / (UINT8_MAX + 1 - srcColor), UINT8_MAX);
  574. TQ_UINT8 newColor = UINT8_BLEND(srcColor, dstColor, srcBlend);
  575. dst[channel] = newColor;
  576. }
  577. }
  578. columns--;
  579. src += MAX_CHANNEL_RGBA;
  580. dst += MAX_CHANNEL_RGBA;
  581. }
  582. rows--;
  583. srcRowStart += srcRowStride;
  584. dstRowStart += dstRowStride;
  585. if(maskRowStart)
  586. maskRowStart += maskRowStride;
  587. }
  588. }
  589. void KisRgbColorSpace::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)
  590. {
  591. while (rows > 0) {
  592. const TQ_UINT8 *src = srcRowStart;
  593. TQ_UINT8 *dst = dstRowStart;
  594. TQ_INT32 columns = numColumns;
  595. const TQ_UINT8 *mask = maskRowStart;
  596. while (columns > 0) {
  597. TQ_UINT8 srcAlpha = src[PIXEL_ALPHA];
  598. TQ_UINT8 dstAlpha = dst[PIXEL_ALPHA];
  599. srcAlpha = TQMIN(srcAlpha, dstAlpha);
  600. // apply the alphamask
  601. if(mask != 0)
  602. {
  603. if(*mask != OPACITY_OPAQUE)
  604. srcAlpha = UINT8_MULT(srcAlpha, *mask);
  605. mask++;
  606. }
  607. if (srcAlpha != OPACITY_TRANSPARENT) {
  608. if (opacity != OPACITY_OPAQUE) {
  609. srcAlpha = UINT8_MULT(src[PIXEL_ALPHA], opacity);
  610. }
  611. TQ_UINT8 srcBlend;
  612. if (dstAlpha == OPACITY_OPAQUE) {
  613. srcBlend = srcAlpha;
  614. } else {
  615. TQ_UINT8 newAlpha = dstAlpha + UINT8_MULT(OPACITY_OPAQUE - dstAlpha, srcAlpha);
  616. dst[PIXEL_ALPHA] = newAlpha;
  617. if (newAlpha != 0) {
  618. srcBlend = UINT8_DIVIDE(srcAlpha, newAlpha);
  619. } else {
  620. srcBlend = srcAlpha;
  621. }
  622. }
  623. for (int channel = 0; channel < MAX_CHANNEL_RGB; channel++) {
  624. TQ_UINT8 srcColor = src[channel];
  625. TQ_UINT8 dstColor = dst[channel];
  626. srcColor = TQMIN(((UINT8_MAX - dstColor) * (UINT8_MAX + 1)) / (srcColor + 1), UINT8_MAX);
  627. if (UINT8_MAX - srcColor > UINT8_MAX) srcColor = UINT8_MAX;
  628. TQ_UINT8 newColor = UINT8_BLEND(srcColor, dstColor, srcBlend);
  629. dst[channel] = newColor;
  630. }
  631. }
  632. columns--;
  633. src += MAX_CHANNEL_RGBA;
  634. dst += MAX_CHANNEL_RGBA;
  635. }
  636. rows--;
  637. srcRowStart += srcRowStride;
  638. dstRowStart += dstRowStride;
  639. if(maskRowStart)
  640. maskRowStart += maskRowStride;
  641. }
  642. }
  643. void KisRgbColorSpace::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)
  644. {
  645. while (rows > 0) {
  646. const TQ_UINT8 *src = srcRowStart;
  647. TQ_UINT8 *dst = dstRowStart;
  648. TQ_INT32 columns = numColumns;
  649. const TQ_UINT8 *mask = maskRowStart;
  650. while (columns > 0) {
  651. TQ_UINT8 srcAlpha = src[PIXEL_ALPHA];
  652. TQ_UINT8 dstAlpha = dst[PIXEL_ALPHA];
  653. srcAlpha = TQMIN(srcAlpha, dstAlpha);
  654. // apply the alphamask
  655. if(mask != 0)
  656. {
  657. if(*mask != OPACITY_OPAQUE)
  658. srcAlpha = UINT8_MULT(srcAlpha, *mask);
  659. mask++;
  660. }
  661. if (srcAlpha != OPACITY_TRANSPARENT) {
  662. if (opacity != OPACITY_OPAQUE) {
  663. srcAlpha = UINT8_MULT(src[PIXEL_ALPHA], opacity);
  664. }
  665. TQ_UINT8 srcBlend;
  666. if (dstAlpha == OPACITY_OPAQUE) {
  667. srcBlend = srcAlpha;
  668. } else {
  669. TQ_UINT8 newAlpha = dstAlpha + UINT8_MULT(OPACITY_OPAQUE - dstAlpha, srcAlpha);
  670. dst[PIXEL_ALPHA] = newAlpha;
  671. if (newAlpha != 0) {
  672. srcBlend = UINT8_DIVIDE(srcAlpha, newAlpha);
  673. } else {
  674. srcBlend = srcAlpha;
  675. }
  676. }
  677. for (int channel = 0; channel < MAX_CHANNEL_RGB; channel++) {
  678. TQ_UINT8 srcColor = src[channel];
  679. TQ_UINT8 dstColor = dst[channel];
  680. srcColor = TQMIN(srcColor, dstColor);
  681. TQ_UINT8 newColor = UINT8_BLEND(srcColor, dstColor, srcBlend);
  682. dst[channel] = newColor;
  683. }
  684. }
  685. columns--;
  686. src += MAX_CHANNEL_RGBA;
  687. dst += MAX_CHANNEL_RGBA;
  688. }
  689. rows--;
  690. srcRowStart += srcRowStride;
  691. dstRowStart += dstRowStride;
  692. if(maskRowStart)
  693. maskRowStart += maskRowStride;
  694. }
  695. }
  696. void KisRgbColorSpace::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)
  697. {
  698. while (rows > 0) {
  699. const TQ_UINT8 *src = srcRowStart;
  700. TQ_UINT8 *dst = dstRowStart;
  701. TQ_INT32 columns = numColumns;
  702. const TQ_UINT8 *mask = maskRowStart;
  703. while (columns > 0) {
  704. TQ_UINT8 srcAlpha = src[PIXEL_ALPHA];
  705. TQ_UINT8 dstAlpha = dst[PIXEL_ALPHA];
  706. srcAlpha = TQMIN(srcAlpha, dstAlpha);
  707. // apply the alphamask
  708. if(mask != 0)
  709. {
  710. if(*mask != OPACITY_OPAQUE)
  711. srcAlpha = UINT8_MULT(srcAlpha, *mask);
  712. mask++;
  713. }
  714. if (srcAlpha != OPACITY_TRANSPARENT) {
  715. if (opacity != OPACITY_OPAQUE) {
  716. srcAlpha = UINT8_MULT(src[PIXEL_ALPHA], opacity);
  717. }
  718. TQ_UINT8 srcBlend;
  719. if (dstAlpha == OPACITY_OPAQUE) {
  720. srcBlend = srcAlpha;
  721. } else {
  722. TQ_UINT8 newAlpha = dstAlpha + UINT8_MULT(OPACITY_OPAQUE - dstAlpha, srcAlpha);
  723. dst[PIXEL_ALPHA] = newAlpha;
  724. if (newAlpha != 0) {
  725. srcBlend = UINT8_DIVIDE(srcAlpha, newAlpha);
  726. } else {
  727. srcBlend = srcAlpha;
  728. }
  729. }
  730. for (int channel = 0; channel < MAX_CHANNEL_RGB; channel++) {
  731. TQ_UINT8 srcColor = src[channel];
  732. TQ_UINT8 dstColor = dst[channel];
  733. srcColor = TQMAX(srcColor, dstColor);
  734. TQ_UINT8 newColor = UINT8_BLEND(srcColor, dstColor, srcBlend);
  735. dst[channel] = newColor;
  736. }
  737. }
  738. columns--;
  739. src += MAX_CHANNEL_RGBA;
  740. dst += MAX_CHANNEL_RGBA;
  741. }
  742. rows--;
  743. srcRowStart += srcRowStride;
  744. dstRowStart += dstRowStride;
  745. if(maskRowStart)
  746. maskRowStart += maskRowStride;
  747. }
  748. }
  749. void KisRgbColorSpace::compositeHue(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)
  750. {
  751. while (rows > 0) {
  752. const TQ_UINT8 *src = srcRowStart;
  753. TQ_UINT8 *dst = dstRowStart;
  754. TQ_INT32 columns = numColumns;
  755. const TQ_UINT8 *mask = maskRowStart;
  756. while (columns > 0) {
  757. TQ_UINT8 srcAlpha = src[PIXEL_ALPHA];
  758. TQ_UINT8 dstAlpha = dst[PIXEL_ALPHA];
  759. srcAlpha = TQMIN(srcAlpha, dstAlpha);
  760. // apply the alphamask
  761. if(mask != 0)
  762. {
  763. if(*mask != OPACITY_OPAQUE)
  764. srcAlpha = UINT8_MULT(srcAlpha, *mask);
  765. mask++;
  766. }
  767. if (srcAlpha != OPACITY_TRANSPARENT) {
  768. if (opacity != OPACITY_OPAQUE) {
  769. srcAlpha = UINT8_MULT(src[PIXEL_ALPHA], opacity);
  770. }
  771. TQ_UINT8 srcBlend;
  772. if (dstAlpha == OPACITY_OPAQUE) {
  773. srcBlend = srcAlpha;
  774. } else {
  775. TQ_UINT8 newAlpha = dstAlpha + UINT8_MULT(OPACITY_OPAQUE - dstAlpha, srcAlpha);
  776. dst[PIXEL_ALPHA] = newAlpha;
  777. if (newAlpha != 0) {
  778. srcBlend = UINT8_DIVIDE(srcAlpha, newAlpha);
  779. } else {
  780. srcBlend = srcAlpha;
  781. }
  782. }
  783. int dstRed = dst[PIXEL_RED];
  784. int dstGreen = dst[PIXEL_GREEN];
  785. int dstBlue = dst[PIXEL_BLUE];
  786. int srcHue;
  787. int srcSaturation;
  788. int srcValue;
  789. int dstHue;
  790. int dstSaturation;
  791. int dstValue;
  792. rgb_to_hsv(src[PIXEL_RED], src[PIXEL_GREEN], src[PIXEL_BLUE], &srcHue, &srcSaturation, &srcValue);
  793. rgb_to_hsv(dstRed, dstGreen, dstBlue, &dstHue, &dstSaturation, &dstValue);
  794. int srcRed;
  795. int srcGreen;
  796. int srcBlue;
  797. hsv_to_rgb(srcHue, dstSaturation, dstValue, &srcRed, &srcGreen, &srcBlue);
  798. dst[PIXEL_RED] = UINT8_BLEND(srcRed, dstRed, srcBlend);
  799. dst[PIXEL_GREEN] = UINT8_BLEND(srcGreen, dstGreen, srcBlend);
  800. dst[PIXEL_BLUE] = UINT8_BLEND(srcBlue, dstBlue, srcBlend);
  801. }
  802. columns--;
  803. src += MAX_CHANNEL_RGBA;
  804. dst += MAX_CHANNEL_RGBA;
  805. }
  806. rows--;
  807. srcRowStart += srcRowStride;
  808. dstRowStart += dstRowStride;
  809. if(maskRowStart)
  810. maskRowStart += maskRowStride;
  811. }
  812. }
  813. void KisRgbColorSpace::compositeSaturation(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)
  814. {
  815. while (rows > 0) {
  816. const TQ_UINT8 *src = srcRowStart;
  817. TQ_UINT8 *dst = dstRowStart;
  818. TQ_INT32 columns = numColumns;
  819. const TQ_UINT8 *mask = maskRowStart;
  820. while (columns > 0) {
  821. TQ_UINT8 srcAlpha = src[PIXEL_ALPHA];
  822. TQ_UINT8 dstAlpha = dst[PIXEL_ALPHA];
  823. srcAlpha = TQMIN(srcAlpha, dstAlpha);
  824. // apply the alphamask
  825. if(mask != 0)
  826. {
  827. if(*mask != OPACITY_OPAQUE)
  828. srcAlpha = UINT8_MULT(srcAlpha, *mask);
  829. mask++;
  830. }
  831. if (srcAlpha != OPACITY_TRANSPARENT) {
  832. if (opacity != OPACITY_OPAQUE) {
  833. srcAlpha = UINT8_MULT(src[PIXEL_ALPHA], opacity);
  834. }
  835. TQ_UINT8 srcBlend;
  836. if (dstAlpha == OPACITY_OPAQUE) {
  837. srcBlend = srcAlpha;
  838. } else {
  839. TQ_UINT8 newAlpha = dstAlpha + UINT8_MULT(OPACITY_OPAQUE - dstAlpha, srcAlpha);
  840. dst[PIXEL_ALPHA] = newAlpha;
  841. if (newAlpha != 0) {
  842. srcBlend = UINT8_DIVIDE(srcAlpha, newAlpha);
  843. } else {
  844. srcBlend = srcAlpha;
  845. }
  846. }
  847. int dstRed = dst[PIXEL_RED];
  848. int dstGreen = dst[PIXEL_GREEN];
  849. int dstBlue = dst[PIXEL_BLUE];
  850. int srcHue;
  851. int srcSaturation;
  852. int srcValue;
  853. int dstHue;
  854. int dstSaturation;
  855. int dstValue;
  856. rgb_to_hsv(src[PIXEL_RED], src[PIXEL_GREEN], src[PIXEL_BLUE], &srcHue, &srcSaturation, &srcValue);
  857. rgb_to_hsv(dstRed, dstGreen, dstBlue, &dstHue, &dstSaturation, &dstValue);
  858. int srcRed;
  859. int srcGreen;
  860. int srcBlue;
  861. hsv_to_rgb(dstHue, srcSaturation, dstValue, &srcRed, &srcGreen, &srcBlue);
  862. dst[PIXEL_RED] = UINT8_BLEND(srcRed, dstRed, srcBlend);
  863. dst[PIXEL_GREEN] = UINT8_BLEND(srcGreen, dstGreen, srcBlend);
  864. dst[PIXEL_BLUE] = UINT8_BLEND(srcBlue, dstBlue, srcBlend);
  865. }
  866. columns--;
  867. src += MAX_CHANNEL_RGBA;
  868. dst += MAX_CHANNEL_RGBA;
  869. }
  870. rows--;
  871. srcRowStart += srcRowStride;
  872. dstRowStart += dstRowStride;
  873. if(maskRowStart)
  874. maskRowStart += maskRowStride;
  875. }
  876. }
  877. void KisRgbColorSpace::compositeValue(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)
  878. {
  879. while (rows > 0) {
  880. const TQ_UINT8 *src = srcRowStart;
  881. TQ_UINT8 *dst = dstRowStart;
  882. TQ_INT32 columns = numColumns;
  883. const TQ_UINT8 *mask = maskRowStart;
  884. while (columns > 0) {
  885. TQ_UINT8 srcAlpha = src[PIXEL_ALPHA];
  886. TQ_UINT8 dstAlpha = dst[PIXEL_ALPHA];
  887. srcAlpha = TQMIN(srcAlpha, dstAlpha);
  888. // apply the alphamask
  889. if(mask != 0)
  890. {
  891. if(*mask != OPACITY_OPAQUE)
  892. srcAlpha = UINT8_MULT(srcAlpha, *mask);
  893. mask++;
  894. }
  895. if (srcAlpha != OPACITY_TRANSPARENT) {
  896. if (opacity != OPACITY_OPAQUE) {
  897. srcAlpha = UINT8_MULT(src[PIXEL_ALPHA], opacity);
  898. }
  899. TQ_UINT8 srcBlend;
  900. if (dstAlpha == OPACITY_OPAQUE) {
  901. srcBlend = srcAlpha;
  902. } else {
  903. TQ_UINT8 newAlpha = dstAlpha + UINT8_MULT(OPACITY_OPAQUE - dstAlpha, srcAlpha);
  904. dst[PIXEL_ALPHA] = newAlpha;
  905. if (newAlpha != 0) {
  906. srcBlend = UINT8_DIVIDE(srcAlpha, newAlpha);
  907. } else {
  908. srcBlend = srcAlpha;
  909. }
  910. }
  911. int dstRed = dst[PIXEL_RED];
  912. int dstGreen = dst[PIXEL_GREEN];
  913. int dstBlue = dst[PIXEL_BLUE];
  914. int srcHue;
  915. int srcSaturation;
  916. int srcValue;
  917. int dstHue;
  918. int dstSaturation;
  919. int dstValue;
  920. rgb_to_hsv(src[PIXEL_RED], src[PIXEL_GREEN], src[PIXEL_BLUE], &srcHue, &srcSaturation, &srcValue);
  921. rgb_to_hsv(dstRed, dstGreen, dstBlue, &dstHue, &dstSaturation, &dstValue);
  922. int srcRed;
  923. int srcGreen;
  924. int srcBlue;
  925. hsv_to_rgb(dstHue, dstSaturation, srcValue, &srcRed, &srcGreen, &srcBlue);
  926. dst[PIXEL_RED] = UINT8_BLEND(srcRed, dstRed, srcBlend);
  927. dst[PIXEL_GREEN] = UINT8_BLEND(srcGreen, dstGreen, srcBlend);
  928. dst[PIXEL_BLUE] = UINT8_BLEND(srcBlue, dstBlue, srcBlend);
  929. }
  930. columns--;
  931. src += MAX_CHANNEL_RGBA;
  932. dst += MAX_CHANNEL_RGBA;
  933. }
  934. rows--;
  935. srcRowStart += srcRowStride;
  936. dstRowStart += dstRowStride;
  937. if(maskRowStart)
  938. maskRowStart += maskRowStride;
  939. }
  940. }
  941. void KisRgbColorSpace::compositeColor(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)
  942. {
  943. while (rows > 0) {
  944. const TQ_UINT8 *src = srcRowStart;
  945. TQ_UINT8 *dst = dstRowStart;
  946. TQ_INT32 columns = numColumns;
  947. const TQ_UINT8 *mask = maskRowStart;
  948. while (columns > 0) {
  949. TQ_UINT8 srcAlpha = src[PIXEL_ALPHA];
  950. TQ_UINT8 dstAlpha = dst[PIXEL_ALPHA];
  951. srcAlpha = TQMIN(srcAlpha, dstAlpha);
  952. // apply the alphamask
  953. if(mask != 0)
  954. {
  955. if(*mask != OPACITY_OPAQUE)
  956. srcAlpha = UINT8_MULT(srcAlpha, *mask);
  957. mask++;
  958. }
  959. if (srcAlpha != OPACITY_TRANSPARENT) {
  960. if (opacity != OPACITY_OPAQUE) {
  961. srcAlpha = UINT8_MULT(src[PIXEL_ALPHA], opacity);
  962. }
  963. TQ_UINT8 srcBlend;
  964. if (dstAlpha == OPACITY_OPAQUE) {
  965. srcBlend = srcAlpha;
  966. } else {
  967. TQ_UINT8 newAlpha = dstAlpha + UINT8_MULT(OPACITY_OPAQUE - dstAlpha, srcAlpha);
  968. dst[PIXEL_ALPHA] = newAlpha;
  969. if (newAlpha != 0) {
  970. srcBlend = UINT8_DIVIDE(srcAlpha, newAlpha);
  971. } else {
  972. srcBlend = srcAlpha;
  973. }
  974. }
  975. int dstRed = dst[PIXEL_RED];
  976. int dstGreen = dst[PIXEL_GREEN];
  977. int dstBlue = dst[PIXEL_BLUE];
  978. int srcHue;
  979. int srcSaturation;
  980. int srcLightness;
  981. int dstHue;
  982. int dstSaturation;
  983. int dstLightness;
  984. rgb_to_hls(src[PIXEL_RED], src[PIXEL_GREEN], src[PIXEL_BLUE], &srcHue, &srcLightness, &srcSaturation);
  985. rgb_to_hls(dstRed, dstGreen, dstBlue, &dstHue, &dstLightness, &dstSaturation);
  986. TQ_UINT8 srcRed;
  987. TQ_UINT8 srcGreen;
  988. TQ_UINT8 srcBlue;
  989. hls_to_rgb(srcHue, dstLightness, srcSaturation, &srcRed, &srcGreen, &srcBlue);
  990. dst[PIXEL_RED] = UINT8_BLEND(srcRed, dstRed, srcBlend);
  991. dst[PIXEL_GREEN] = UINT8_BLEND(srcGreen, dstGreen, srcBlend);
  992. dst[PIXEL_BLUE] = UINT8_BLEND(srcBlue, dstBlue, srcBlend);
  993. }
  994. columns--;
  995. src += MAX_CHANNEL_RGBA;
  996. dst += MAX_CHANNEL_RGBA;
  997. }
  998. rows--;
  999. srcRowStart += srcRowStride;
  1000. dstRowStart += dstRowStride;
  1001. if(maskRowStart)
  1002. maskRowStart += maskRowStride;
  1003. }
  1004. }
  1005. void KisRgbColorSpace::compositeErase(TQ_UINT8 *dst,
  1006. TQ_INT32 dstRowSize,
  1007. const TQ_UINT8 *src,
  1008. TQ_INT32 srcRowSize,
  1009. const TQ_UINT8 *srcAlphaMask,
  1010. TQ_INT32 maskRowStride,
  1011. TQ_INT32 rows,
  1012. TQ_INT32 cols,
  1013. TQ_UINT8 /*opacity*/)
  1014. {
  1015. TQ_INT32 i;
  1016. TQ_UINT8 srcAlpha;
  1017. while (rows-- > 0)
  1018. {
  1019. const TQ_UINT8 *s = src;
  1020. TQ_UINT8 *d = dst;
  1021. const TQ_UINT8 *mask = srcAlphaMask;
  1022. for (i = cols; i > 0; i--, s+=MAX_CHANNEL_RGBA, d+=MAX_CHANNEL_RGBA)
  1023. {
  1024. srcAlpha = s[PIXEL_ALPHA];
  1025. // apply the alphamask
  1026. if(mask != 0)
  1027. {
  1028. if(*mask != OPACITY_OPAQUE)
  1029. srcAlpha = UINT8_BLEND(srcAlpha, OPACITY_OPAQUE, *mask);
  1030. mask++;
  1031. }
  1032. d[PIXEL_ALPHA] = UINT8_MULT(srcAlpha, d[PIXEL_ALPHA]);
  1033. }
  1034. dst += dstRowSize;
  1035. if(srcAlphaMask)
  1036. srcAlphaMask += maskRowStride;
  1037. src += srcRowSize;
  1038. }
  1039. }
  1040. void KisRgbColorSpace::bitBlt(TQ_UINT8 *dst,
  1041. TQ_INT32 dstRowStride,
  1042. const TQ_UINT8 *src,
  1043. TQ_INT32 srcRowStride,
  1044. const TQ_UINT8 *mask,
  1045. TQ_INT32 maskRowStride,
  1046. TQ_UINT8 opacity,
  1047. TQ_INT32 rows,
  1048. TQ_INT32 cols,
  1049. const KisCompositeOp& op)
  1050. {
  1051. switch (op.op()) {
  1052. case COMPOSITE_UNDEF:
  1053. // Undefined == no composition
  1054. break;
  1055. case COMPOSITE_OVER:
  1056. compositeOver(dst, dstRowStride, src, srcRowStride, mask, maskRowStride, rows, cols, opacity);
  1057. break;
  1058. case COMPOSITE_ALPHA_DARKEN:
  1059. compositeAlphaDarken(dst, dstRowStride, src, srcRowStride, mask, maskRowStride, rows, cols, opacity);
  1060. break;
  1061. case COMPOSITE_IN:
  1062. compositeIn(pixelSize(), dst, dstRowStride, src, srcRowStride, rows, cols, opacity);
  1063. break;
  1064. case COMPOSITE_OUT:
  1065. compositeOut(pixelSize(), dst, dstRowStride, src, srcRowStride, rows, cols, opacity);
  1066. break;
  1067. case COMPOSITE_ATOP:
  1068. compositeAtop(pixelSize(), dst, dstRowStride, src, srcRowStride, rows, cols, opacity);
  1069. break;
  1070. case COMPOSITE_XOR:
  1071. compositeXor(pixelSize(), dst, dstRowStride, src, srcRowStride, rows, cols, opacity);
  1072. break;
  1073. case COMPOSITE_PLUS:
  1074. compositePlus(pixelSize(), dst, dstRowStride, src, srcRowStride, rows, cols, opacity);
  1075. break;
  1076. case COMPOSITE_MINUS:
  1077. compositeMinus(pixelSize(), dst, dstRowStride, src, srcRowStride, rows, cols, opacity);
  1078. break;
  1079. case COMPOSITE_ADD:
  1080. compositeAdd(pixelSize(), dst, dstRowStride, src, srcRowStride, rows, cols, opacity);
  1081. break;
  1082. case COMPOSITE_SUBTRACT:
  1083. compositeSubtract(pixelSize(), dst, dstRowStride, src, srcRowStride, rows, cols, opacity);
  1084. break;
  1085. case COMPOSITE_DIFF:
  1086. compositeDiff(pixelSize(), dst, dstRowStride, src, srcRowStride, rows, cols, opacity);
  1087. break;
  1088. case COMPOSITE_MULT:
  1089. compositeMultiply(dst, dstRowStride, src, srcRowStride, mask, maskRowStride, rows, cols, opacity);
  1090. break;
  1091. case COMPOSITE_DIVIDE:
  1092. compositeDivide(dst, dstRowStride, src, srcRowStride, mask, maskRowStride, rows, cols, opacity);
  1093. break;
  1094. case COMPOSITE_BUMPMAP:
  1095. compositeBumpmap(pixelSize(), dst, dstRowStride, src, srcRowStride, rows, cols, opacity);
  1096. break;
  1097. case COMPOSITE_COPY:
  1098. compositeCopy(dst, dstRowStride, src, srcRowStride, mask, maskRowStride, rows, cols, opacity);
  1099. break;
  1100. case COMPOSITE_COPY_RED:
  1101. compositeCopyRed(pixelSize(), dst, dstRowStride, src, srcRowStride, rows, cols, opacity);
  1102. break;
  1103. case COMPOSITE_COPY_GREEN:
  1104. compositeCopyGreen(pixelSize(), dst, dstRowStride, src, srcRowStride, rows, cols, opacity);
  1105. break;
  1106. case COMPOSITE_COPY_BLUE:
  1107. compositeCopyBlue(pixelSize(), dst, dstRowStride, src, srcRowStride, rows, cols, opacity);
  1108. break;
  1109. case COMPOSITE_COPY_OPACITY:
  1110. compositeCopyOpacity(pixelSize(), dst, dstRowStride, src, srcRowStride, rows, cols, opacity);
  1111. break;
  1112. case COMPOSITE_CLEAR:
  1113. compositeClear(pixelSize(), dst, dstRowStride, src, srcRowStride, rows, cols, opacity);
  1114. break;
  1115. case COMPOSITE_DISSOLVE:
  1116. compositeDissolve(pixelSize(), dst, dstRowStride, src, srcRowStride, rows, cols, opacity);
  1117. break;
  1118. case COMPOSITE_DISPLACE:
  1119. compositeDisplace(pixelSize(), dst, dstRowStride, src, srcRowStride, rows, cols, opacity);
  1120. break;
  1121. #if 0
  1122. case COMPOSITE_MODULATE:
  1123. compositeModulate(pixelSize(), dst, dstRowStride, src, srcRowStride, rows, cols, opacity);
  1124. break;
  1125. case COMPOSITE_THRESHOLD:
  1126. compositeThreshold(pixelSize(), dst, dstRowStride, src, srcRowStride, rows, cols, opacity);
  1127. break;
  1128. #endif
  1129. case COMPOSITE_NO:
  1130. // No composition.
  1131. break;
  1132. case COMPOSITE_DARKEN:
  1133. compositeDarken(dst, dstRowStride, src, srcRowStride, mask, maskRowStride, rows, cols, opacity);
  1134. break;
  1135. case COMPOSITE_LIGHTEN:
  1136. compositeLighten(dst, dstRowStride, src, srcRowStride, mask, maskRowStride, rows, cols, opacity);
  1137. break;
  1138. case COMPOSITE_HUE:
  1139. compositeHue(dst, dstRowStride, src, srcRowStride, mask, maskRowStride, rows, cols, opacity);
  1140. break;
  1141. case COMPOSITE_SATURATION:
  1142. compositeSaturation(dst, dstRowStride, src, srcRowStride, mask, maskRowStride, rows, cols, opacity);
  1143. break;
  1144. case COMPOSITE_VALUE:
  1145. compositeValue(dst, dstRowStride, src, srcRowStride, mask, maskRowStride, rows, cols, opacity);
  1146. break;
  1147. case COMPOSITE_COLOR:
  1148. compositeColor(dst, dstRowStride, src, srcRowStride, mask, maskRowStride, rows, cols, opacity);
  1149. break;
  1150. case COMPOSITE_COLORIZE:
  1151. compositeColorize(pixelSize(), dst, dstRowStride, src, srcRowStride, rows, cols, opacity);
  1152. break;
  1153. case COMPOSITE_LUMINIZE:
  1154. compositeLuminize(pixelSize(), dst, dstRowStride, src, srcRowStride, rows, cols, opacity);
  1155. break;
  1156. case COMPOSITE_SCREEN:
  1157. compositeScreen(dst, dstRowStride, src, srcRowStride, mask, maskRowStride, rows, cols, opacity);
  1158. break;
  1159. case COMPOSITE_OVERLAY:
  1160. compositeOverlay(dst, dstRowStride, src, srcRowStride, mask, maskRowStride, rows, cols, opacity);
  1161. break;
  1162. case COMPOSITE_ERASE:
  1163. compositeErase(dst, dstRowStride, src, srcRowStride, mask, maskRowStride, rows, cols, opacity);
  1164. break;
  1165. case COMPOSITE_DODGE:
  1166. compositeDodge(dst, dstRowStride, src, srcRowStride, mask, maskRowStride, rows, cols, opacity);
  1167. break;
  1168. case COMPOSITE_BURN:
  1169. compositeBurn(dst, dstRowStride, src, srcRowStride, mask, maskRowStride, rows, cols, opacity);
  1170. break;
  1171. default:
  1172. break;
  1173. }
  1174. }
  1175. KisCompositeOpList KisRgbColorSpace::userVisiblecompositeOps() const
  1176. {
  1177. KisCompositeOpList list;
  1178. list.append(KisCompositeOp(COMPOSITE_OVER));
  1179. list.append(KisCompositeOp(COMPOSITE_ALPHA_DARKEN));
  1180. list.append(KisCompositeOp(COMPOSITE_MULT));
  1181. list.append(KisCompositeOp(COMPOSITE_BURN));
  1182. list.append(KisCompositeOp(COMPOSITE_DODGE));
  1183. list.append(KisCompositeOp(COMPOSITE_DIVIDE));
  1184. list.append(KisCompositeOp(COMPOSITE_SCREEN));
  1185. list.append(KisCompositeOp(COMPOSITE_OVERLAY));
  1186. list.append(KisCompositeOp(COMPOSITE_DARKEN));
  1187. list.append(KisCompositeOp(COMPOSITE_LIGHTEN));
  1188. list.append(KisCompositeOp(COMPOSITE_HUE));
  1189. list.append(KisCompositeOp(COMPOSITE_SATURATION));
  1190. list.append(KisCompositeOp(COMPOSITE_VALUE));
  1191. list.append(KisCompositeOp(COMPOSITE_COLOR));
  1192. list.append(KisCompositeOp(COMPOSITE_PLUS));
  1193. list.append(KisCompositeOp(COMPOSITE_MINUS));
  1194. list.append(KisCompositeOp(COMPOSITE_SUBTRACT));
  1195. list.append(KisCompositeOp(COMPOSITE_ADD));
  1196. return list;
  1197. }