Krita Source Code Documentation
Loading...
Searching...
No Matches
MaskedImage Class Reference
+ Inheritance diagram for MaskedImage:

Public Member Functions

int channelCount (void) const
 
void clearMask (void)
 
bool containsMasked (int x, int y, int S)
 
KisSharedPtr< MaskedImagecopy (void)
 
int countMasked (void)
 
void DebugDump (const QString &name)
 
void downsample2x (void)
 
quint8 * getImagePixel (int x, int y)
 
QVector< float > getImagePixels (int x, int y) const
 
quint8 getImagePixelU8 (int x, int y, int chan) const
 
void initialize (KisPaintDeviceSP _imageDev, KisPaintDeviceSP _maskDev, QRect _maskRect)
 
bool isMasked (int x, int y)
 
 MaskedImage (KisPaintDeviceSP _imageDev, KisPaintDeviceSP _maskDev, QRect _maskRect)
 
void mixColors (std::vector< quint8 * > pixels, std::vector< float > w, float wsum, quint8 *dst)
 
void setImagePixels (int x, int y, QVector< float > &value)
 
void setMask (int x, int y, quint8 v)
 
QRect size ()
 
void toPaintDevice (KisPaintDeviceSP imageDev, QRect rect, KisSelectionSP selection)
 
void upscale (int newW, int newH)
 
- Public Member Functions inherited from KisShared
bool deref ()
 
bool ref ()
 
int refCount ()
 
QAtomicInt * sharedWeakReference ()
 

Public Attributes

std::function< float(const MaskedImage &, int, int, const MaskedImage &, int, int) distance )
 

Private Member Functions

void cacheImage (KisPaintDeviceSP imageDev, QRect rect)
 
void cacheMask (KisPaintDeviceSP maskDev, QRect rect)
 
 MaskedImage ()
 

Private Attributes

const KoColorSpacecs {nullptr}
 
const KoColorSpacecsMask {nullptr}
 
ImageData imageData
 
QRect imageSize
 
ImageData maskData
 
int nChannels {0}
 

Friends

template<typename T >
float distance_impl (const MaskedImage &my, int x, int y, const MaskedImage &other, int xo, int yo)
 

Additional Inherited Members

- Protected Member Functions inherited from KisShared
 KisShared ()
 
 ~KisShared ()
 

Detailed Description

Definition at line 199 of file kis_inpaint.cpp.

Constructor & Destructor Documentation

◆ MaskedImage() [1/2]

MaskedImage::MaskedImage ( )
inlineprivate

Definition at line 237 of file kis_inpaint.cpp.

237{}

◆ MaskedImage() [2/2]

MaskedImage::MaskedImage ( KisPaintDeviceSP _imageDev,
KisPaintDeviceSP _maskDev,
QRect _maskRect )
inline

Definition at line 292 of file kis_inpaint.cpp.

293 {
294 initialize(_imageDev, _maskDev, _maskRect);
295 }
void initialize(KisPaintDeviceSP _imageDev, KisPaintDeviceSP _maskDev, QRect _maskRect)

References initialize().

Member Function Documentation

◆ cacheImage()

void MaskedImage::cacheImage ( KisPaintDeviceSP imageDev,
QRect rect )
inlineprivate

Definition at line 215 of file kis_inpaint.cpp.

216 {
217 cs = imageDev->colorSpace();
219 imageData.Init(imageDev, rect);
220 imageSize = rect;
221 }
void Init(int _imageWidth, int _imageHeight, int _pixelSize)
const KoColorSpace * colorSpace() const
virtual quint32 channelCount() const =0
const KoColorSpace * cs
ImageData imageData

References KoColorSpace::channelCount(), KisPaintDevice::colorSpace(), cs, imageData, imageSize, ImageData::Init(), and nChannels.

◆ cacheMask()

void MaskedImage::cacheMask ( KisPaintDeviceSP maskDev,
QRect rect )
inlineprivate

Definition at line 224 of file kis_inpaint.cpp.

225 {
226 Q_ASSERT(maskDev->colorSpace()->pixelSize() == 1);
227 csMask = maskDev->colorSpace();
228 maskData.Init(maskDev, rect);
229
230 //hard threshold for the initial mask
231 //may be optional. needs testing
232 std::for_each(maskData.data(), maskData.data() + maskData.num_bytes(), [](quint8 & v) {
233 v = (v > MASK_CLEAR) ? MASK_SET : MASK_CLEAR;
234 });
235 }
qreal v
quint8 * data(void) const
int num_bytes(void) const
virtual quint32 pixelSize() const =0
const KoColorSpace * csMask
ImageData maskData

References KisPaintDevice::colorSpace(), csMask, ImageView::data(), ImageData::Init(), maskData, ImageView::num_bytes(), KoColorSpace::pixelSize(), and v.

◆ channelCount()

int MaskedImage::channelCount ( void ) const
inline

Definition at line 472 of file kis_inpaint.cpp.

473 {
474 return cs->channelCount();
475 }

References KoColorSpace::channelCount(), and cs.

◆ clearMask()

void MaskedImage::clearMask ( void )
inline

Definition at line 262 of file kis_inpaint.cpp.

263 {
265 }
const quint8 MASK_CLEAR

References ImageView::data(), MASK_CLEAR, maskData, and ImageView::num_bytes().

◆ containsMasked()

bool MaskedImage::containsMasked ( int x,
int y,
int S )
inline

Definition at line 403 of file kis_inpaint.cpp.

404 {
405 for (int dy = -S; dy <= S; ++dy) {
406 int ys = y + dy;
407 if (ys < 0 || ys >= imageSize.height())
408 continue;
409
410 for (int dx = -S; dx <= S; ++dx) {
411 int xs = x + dx;
412 if (xs < 0 || xs >= imageSize.width())
413 continue;
414 if (isMasked(xs, ys))
415 return true;
416 }
417 }
418 return false;
419 }
Eigen::Matrix< double, 4, 2 > S
bool isMasked(int x, int y)

References imageSize, isMasked(), and S.

◆ copy()

KisSharedPtr< MaskedImage > MaskedImage::copy ( void )
inline

Definition at line 376 of file kis_inpaint.cpp.

377 {
379 clone->imageSize = this->imageSize;
380 clone->nChannels = this->nChannels;
381 clone->maskData = this->maskData;
382 clone->imageData = this->imageData;
383 clone->cs = this->cs;
384 clone->csMask = this->csMask;
385 clone->distance = this->distance;
386 return clone;
387 }
std::function< float(const MaskedImage &, int, int, const MaskedImage &, int, int) distance)

References cs, csMask, distance, imageData, imageSize, maskData, MaskedImage(), and nChannels.

◆ countMasked()

int MaskedImage::countMasked ( void )
inline

Definition at line 389 of file kis_inpaint.cpp.

390 {
391 int count = std::count_if(maskData.data(), maskData.data() + maskData.num_elements(), [](quint8 v) {
392 return v > MASK_CLEAR;
393 });
394 return count;
395 }
int num_elements(void) const

References ImageView::data(), maskData, ImageView::num_elements(), and v.

◆ DebugDump()

void MaskedImage::DebugDump ( const QString & name)
inline

Definition at line 256 of file kis_inpaint.cpp.

257 {
258 imageData.DebugDump(name + "_img");
259 maskData.DebugDump(name + "_mask");
260 }
void DebugDump(const QString &fnamePrefix)

References ImageView::DebugDump(), imageData, and maskData.

◆ downsample2x()

void MaskedImage::downsample2x ( void )
inline

Definition at line 297 of file kis_inpaint.cpp.

298 {
299 int H = imageSize.height();
300 int W = imageSize.width();
301 int newW = W / 2, newH = H / 2;
302
303 KisPaintDeviceSP imageDev = new KisPaintDevice(cs);
305 imageDev->writeBytes(imageData.data(), 0, 0, W, H);
306 maskDev->writeBytes(maskData.data(), 0, 0, W, H);
307
308 ImageData newImage(newW, newH, cs->pixelSize());
309 ImageData newMask(newW, newH, 1);
310
311 KoDummyUpdaterHolder updaterHolder;
312 KisTransformWorker worker(imageDev, 1. / 2., 1. / 2., 0.0, 0.0, 0.0, 0.0, 0.0,
313 updaterHolder.updater(), KisFilterStrategyRegistry::instance()->value("Bicubic"));
314 worker.run();
315
316 KisTransformWorker workerMask(maskDev, 1. / 2., 1. / 2., 0.0, 0.0, 0.0, 0.0, 0.0,
317 updaterHolder.updater(), KisFilterStrategyRegistry::instance()->value("Bicubic"));
318 workerMask.run();
319
320 imageDev->readBytes(newImage.data(), 0, 0, newW, newH);
321 maskDev->readBytes(newMask.data(), 0, 0, newW, newH);
322 imageData = std::move(newImage);
323 maskData = std::move(newMask);
324
325 for (int i = 0; i < imageData.num_elements(); ++i) {
326 quint8* maskPix = maskData.data() + i * maskData.pixel_size();
327 if (*maskPix == MASK_SET) {
328 for (int k = 0; k < imageData.pixel_size(); k++)
329 *(imageData.data() + i * imageData.pixel_size() + k) = 0;
330 } else {
331 *maskPix = MASK_CLEAR;
332 }
333 }
334 imageSize = QRect(0, 0, newW, newH);
335 }
int pixel_size(void) const
static KisFilterStrategyRegistry * instance()
void readBytes(quint8 *data, qint32 x, qint32 y, qint32 w, qint32 h) const
void writeBytes(const quint8 *data, qint32 x, qint32 y, qint32 w, qint32 h)
A holder for an updater that does nothing.
Definition KoUpdater.h:116
KoUpdater * updater()
const T value(const QString &id) const
const quint8 MASK_SET

References cs, csMask, ImageView::data(), imageData, imageSize, KisFilterStrategyRegistry::instance(), MASK_CLEAR, MASK_SET, maskData, ImageView::num_elements(), ImageView::pixel_size(), KoColorSpace::pixelSize(), KisPaintDevice::readBytes(), KisTransformWorker::run(), KoDummyUpdaterHolder::updater(), KoGenericRegistry< T >::value(), and KisPaintDevice::writeBytes().

◆ getImagePixel()

quint8 * MaskedImage::getImagePixel ( int x,
int y )
inline

Definition at line 433 of file kis_inpaint.cpp.

434 {
435 return imageData(x, y);
436 }

References imageData.

◆ getImagePixels()

QVector< float > MaskedImage::getImagePixels ( int x,
int y ) const
inline

Definition at line 426 of file kis_inpaint.cpp.

427 {
430 return v;
431 }
virtual void normalisedChannelsValue(const quint8 *pixel, QVector< float > &channels) const =0

References KoColorSpace::channelCount(), cs, imageData, KoColorSpace::normalisedChannelsValue(), and v.

◆ getImagePixelU8()

quint8 MaskedImage::getImagePixelU8 ( int x,
int y,
int chan ) const
inline

Definition at line 421 of file kis_inpaint.cpp.

422 {
423 return cs->scaleToU8(imageData(x, y), chan);
424 }
virtual quint8 scaleToU8(const quint8 *srcPixel, qint32 channelPos) const =0

References cs, imageData, and KoColorSpace::scaleToU8().

◆ initialize()

void MaskedImage::initialize ( KisPaintDeviceSP _imageDev,
KisPaintDeviceSP _maskDev,
QRect _maskRect )
inline

Definition at line 267 of file kis_inpaint.cpp.

268 {
269 cacheImage(_imageDev, _maskRect);
270 cacheMask(_maskDev, _maskRect);
271
272 //distance function is the only that needs to know the type
273 //For performance reasons we can't use functions provided by color space
274 KoID colorDepthId = _imageDev->colorSpace()->colorDepthId();
275
276 //Use RGB traits to assign actual pixel data types.
277 distance = &distance_impl<KoRgbU8Traits::channels_type>;
278
279 if( colorDepthId == Integer16BitsColorDepthID )
280 distance = &distance_impl<KoRgbU16Traits::channels_type>;
281#ifdef HAVE_OPENEXR
282 if( colorDepthId == Float16BitsColorDepthID )
283 distance = &distance_impl<KoRgbF16Traits::channels_type>;
284#endif
285 if( colorDepthId == Float32BitsColorDepthID )
286 distance = &distance_impl<KoRgbF32Traits::channels_type>;
287
288 if( colorDepthId == Float64BitsColorDepthID )
289 distance = &distance_impl<KoRgbF64Traits::channels_type>;
290 }
const KoID Float32BitsColorDepthID("F32", ki18n("32-bit float/channel"))
const KoID Float64BitsColorDepthID("F64", ki18n("64-bit float/channel"))
const KoID Float16BitsColorDepthID("F16", ki18n("16-bit float/channel"))
const KoID Integer16BitsColorDepthID("U16", ki18n("16-bit integer/channel"))
virtual KoID colorDepthId() const =0
Definition KoID.h:30
void cacheMask(KisPaintDeviceSP maskDev, QRect rect)
void cacheImage(KisPaintDeviceSP imageDev, QRect rect)

References cacheImage(), cacheMask(), KoColorSpace::colorDepthId(), KisPaintDevice::colorSpace(), distance, Float16BitsColorDepthID, Float32BitsColorDepthID, Float64BitsColorDepthID, and Integer16BitsColorDepthID.

◆ isMasked()

bool MaskedImage::isMasked ( int x,
int y )
inline

Definition at line 397 of file kis_inpaint.cpp.

398 {
399 return (*maskData(x, y) > MASK_CLEAR);
400 }

References MASK_CLEAR, and maskData.

◆ mixColors()

void MaskedImage::mixColors ( std::vector< quint8 * > pixels,
std::vector< float > w,
float wsum,
quint8 * dst )
inline

Definition at line 443 of file kis_inpaint.cpp.

444 {
445 const KoMixColorsOp* mixOp = cs->mixColorsOp();
446
447 size_t n = w.size();
448 assert(pixels.size() == n);
449 std::vector< qint16 > weights;
450 weights.clear();
451
452 float dif = 0;
453
454 float scale = 255 / (wsum + 0.001);
455
456 for (auto& v : w) {
457 //compensated summation to increase accuracy
458 float v1 = v * scale + dif;
459 float v2 = std::round(v1);
460 dif = v1 - v2;
461 weights.push_back(v2);
462 }
463
464 mixOp->mixColors(pixels.data(), weights.data(), n, dst);
465 }
KoMixColorsOp * mixColorsOp
virtual void mixColors(const quint8 *const *colors, const qint16 *weights, int nColors, quint8 *dst, int weightSum=255) const =0

References cs, KoMixColorsOp::mixColors(), KoColorSpace::mixColorsOp, and v.

◆ setImagePixels()

void MaskedImage::setImagePixels ( int x,
int y,
QVector< float > & value )
inline

Definition at line 438 of file kis_inpaint.cpp.

439 {
441 }
float value(const T *src, size_t ch)
virtual void fromNormalisedChannelsValue(quint8 *pixel, const QVector< float > &values) const =0

References cs, KoColorSpace::fromNormalisedChannelsValue(), imageData, and value().

◆ setMask()

void MaskedImage::setMask ( int x,
int y,
quint8 v )
inline

Definition at line 467 of file kis_inpaint.cpp.

468 {
469 *(maskData(x, y)) = v;
470 }

References maskData, and v.

◆ size()

QRect MaskedImage::size ( )
inline

Definition at line 371 of file kis_inpaint.cpp.

372 {
373 return imageSize;
374 }

References imageSize.

◆ toPaintDevice()

void MaskedImage::toPaintDevice ( KisPaintDeviceSP imageDev,
QRect rect,
KisSelectionSP selection )
inline

Definition at line 242 of file kis_inpaint.cpp.

243 {
244 if (!selection) {
245 imageData.saveToDevice(imageDev, rect);
246 } else {
247 KisPaintDeviceSP dev = new KisPaintDevice(imageDev->colorSpace());
248 dev->setDefaultBounds(imageDev->defaultBounds());
249
251
252 KisPainter::copyAreaOptimized(rect.topLeft(), dev, imageDev, rect, selection);
253 }
254 }
void saveToDevice(KisPaintDeviceSP outDev, QRect rect)
void setDefaultBounds(KisDefaultBoundsBaseSP bounds)
KisDefaultBoundsBaseSP defaultBounds() const
static void copyAreaOptimized(const QPoint &dstPt, KisPaintDeviceSP src, KisPaintDeviceSP dst, const QRect &originalSrcRect)

References KisPaintDevice::colorSpace(), KisPainter::copyAreaOptimized(), KisPaintDevice::defaultBounds(), imageData, ImageView::saveToDevice(), and KisPaintDevice::setDefaultBounds().

◆ upscale()

void MaskedImage::upscale ( int newW,
int newH )
inline

Definition at line 337 of file kis_inpaint.cpp.

338 {
339 int H = imageSize.height();
340 int W = imageSize.width();
341
342 ImageData newImage(newW, newH, cs->pixelSize());
343 ImageData newMask(newW, newH, 1);
344
345 QVector<float> colors(nChannels, 0.f);
347
348 for (int y = 0; y < newH; ++y) {
349 for (int x = 0; x < newW; ++x) {
350
351 // original pixel
352 int xs = (x * W) / newW;
353 int ys = (y * H) / newH;
354
355 // copy to new image
356 if (!isMasked(xs, ys)) {
357 std::copy(imageData(xs, ys), imageData(xs, ys) + imageData.pixel_size(), newImage(x, y));
358 *newMask(x, y) = MASK_CLEAR;
359 } else {
360 std::fill(newImage(x, y), newImage(x, y) + newImage.pixel_size(), 0);
361 *newMask(x, y) = MASK_SET;
362 }
363 }
364 }
365
366 imageData = std::move(newImage);
367 maskData = std::move(newMask);
368 imageSize = QRect(0, 0, newW, newH);
369 }

References cs, imageData, imageSize, isMasked(), MASK_CLEAR, MASK_SET, maskData, nChannels, ImageView::pixel_size(), KoColorSpace::pixelSize(), and v.

Friends And Related Symbol Documentation

◆ distance_impl

template<typename T >
float distance_impl ( const MaskedImage & my,
int x,
int y,
const MaskedImage & other,
int xo,
int yo )
friend

Definition at line 481 of file kis_inpaint.cpp.

482{
483 float dsq = 0;
484 quint32 nchannels = my.channelCount();
485 T *v1 = reinterpret_cast<T*>(my.imageData(x, y));
486 T *v2 = reinterpret_cast<T*>(other.imageData(xo, yo));
487
488 for (quint32 chan = 0; chan < nchannels; chan++) {
489 //It's very important not to lose precision in the next line
490 float v = ((float)(*(v1 + chan)) - (float)(*(v2 + chan)));
491 dsq += v * v;
492 }
493
494 // in HDR color spaces the value of the channel may become bigger than the unitValue
495 return qMin((float)(nchannels * MAX_DIST), dsq / (pow2((float)KoColorSpaceMathsTraits<T>::unitValue) / MAX_DIST ));
496}
int channelCount(void) const
T pow2(const T &x)
Definition kis_global.h:166
const int MAX_DIST

Member Data Documentation

◆ cs

const KoColorSpace* MaskedImage::cs {nullptr}
private

Definition at line 208 of file kis_inpaint.cpp.

208{nullptr};

◆ csMask

const KoColorSpace* MaskedImage::csMask {nullptr}
private

Definition at line 209 of file kis_inpaint.cpp.

209{nullptr};

◆ distance

std::function< float(const MaskedImage&, int, int, const MaskedImage& , int , int ) MaskedImage::distance)

Definition at line 240 of file kis_inpaint.cpp.

◆ imageData

ImageData MaskedImage::imageData
private

Definition at line 212 of file kis_inpaint.cpp.

◆ imageSize

QRect MaskedImage::imageSize
private

Definition at line 205 of file kis_inpaint.cpp.

◆ maskData

ImageData MaskedImage::maskData
private

Definition at line 211 of file kis_inpaint.cpp.

◆ nChannels

int MaskedImage::nChannels {0}
private

Definition at line 206 of file kis_inpaint.cpp.

206{0};

The documentation for this class was generated from the following file: