forked from NatronGitHub/Natron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Image.h
1076 lines (885 loc) · 37.7 KB
/
Image.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* ***** BEGIN LICENSE BLOCK *****
* This file is part of Natron <https://natrongithub.github.io/>,
* (C) 2018-2021 The Natron developers
* (C) 2013-2018 INRIA and Alexandre Gauthier-Foichat
*
* Natron is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Natron is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Natron. If not, see <http://www.gnu.org/licenses/gpl-2.0.html>
* ***** END LICENSE BLOCK ***** */
#ifndef NATRON_ENGINE_IMAGE_H
#define NATRON_ENGINE_IMAGE_H
// ***** BEGIN PYTHON BLOCK *****
// from <https://docs.python.org/3/c-api/intro.html#include-files>:
// "Since Python may define some pre-processor definitions which affect the standard headers on some systems, you must include Python.h before any standard headers are included."
#include <Python.h>
// ***** END PYTHON BLOCK *****
#include "Global/Macros.h"
#include <list>
#include <map>
#include <algorithm> // min, max
#include <bitset>
#include "Global/GlobalDefines.h"
CLANG_DIAG_OFF(deprecated)
#include <QtCore/QHash>
CLANG_DIAG_ON(deprecated)
#include <QtCore/QReadWriteLock>
#include "Engine/ImageKey.h"
#include "Engine/ImagePlaneDesc.h"
#include "Engine/ImageParams.h"
#include "Engine/CacheEntry.h"
#include "Engine/OutputSchedulerThread.h"
#include "Engine/RectD.h"
#include "Engine/ViewIdx.h"
#include "Engine/EngineFwd.h"
NATRON_NAMESPACE_ENTER
class GenericAccess
{
public:
GenericAccess() {}
virtual ~GenericAccess()
{
}
};
class Bitmap
{
public:
Bitmap(const RectI & bounds)
: _bounds(bounds)
, _map( bounds.area() )
, _dirtyZone()
, _dirtyZoneSet(false)
{
//Do not assert !rod.isNull() : An empty image can be created for entries that correspond to
// "identities" images (i.e: images that are just a link to another image). See EffectInstance :
// "!!!Note that if isIdentity is true it will allocate an empty image object with 0 bytes of data."
//assert(!rod.isNull());
std::fill(_map.begin(), _map.end(), 0);
}
Bitmap()
: _bounds()
, _map()
, _dirtyZone()
, _dirtyZoneSet(false)
{
}
void initialize(const RectI & bounds)
{
_bounds = bounds;
_map.resize( _bounds.area() );
std::fill(_map.begin(), _map.end(), 0);
}
~Bitmap()
{
}
void setTo1()
{
std::fill(_map.begin(), _map.end(), 1);
}
const RectI & getBounds() const
{
return _bounds;
}
#if NATRON_ENABLE_TRIMAP
void minimalNonMarkedRects_trimap(const RectI & roi, std::list<RectI>& ret, bool* isBeingRenderedElsewhere) const;
RectI minimalNonMarkedBbox_trimap(const RectI & roi, bool* isBeingRenderedElsewhere) const;
#endif
void minimalNonMarkedRects(const RectI & roi, std::list<RectI>& ret) const;
RectI minimalNonMarkedBbox(const RectI & roi) const;
// returns true if the roi only contains 0s
bool isNonMarked(const RectI & roi) const;
///Fill with 1 the roi
void markForRendered(const RectI & roi) { markFor(roi, 1); }
#if NATRON_ENABLE_TRIMAP
///Fill with 2 the roi
void markForRendering(const RectI & roi);
#endif
void clear(const RectI& roi) { markFor(roi, 0); }
void swap(Bitmap& other);
const char* getBitmap() const
{
return &_map.front();
}
char* getBitmap()
{
return &_map.front();
}
const char* getBitmapAt(int x, int y) const;
char* getBitmapAt(int x, int y);
void copyRowPortion(int x1, int x2, int y, const Bitmap& other);
void copyBitmapPortion(const RectI& roi, const Bitmap& other);
void setDirtyZone(const RectI& zone)
{
_dirtyZone = zone;
_dirtyZoneSet = true;
}
private:
void markFor(const RectI & roi, char value);
private:
RectI _bounds;
std::vector<char> _map;
/**
* This represents the zone that has potentially something to render. In minimalNonMarkedRects
* we intersect the region of interest with the dirty zone. This is useful to optimize the bitmap checking
* when we are sure multiple threads are not using the image and we have a very small RoI to render.
* For now it's only used for the rotopaint while painting.
**/
RectI _dirtyZone;
bool _dirtyZoneSet;
};
class Image
: public CacheEntryHelper<unsigned char, ImageKey, ImageParams>, public BufferableObject
{
public:
Image(const ImageKey & key,
const ImageParamsPtr & params,
const CacheAPI* cache);
/*This constructor can be used to allocate a local Image. The deallocation should
then be handled by the user. Note that no view number is passed in parameter
as it is not needed.*/
Image(const ImagePlaneDesc& components,
const RectD & regionOfDefinition, //!< rod in canonical coordinates
const RectI & bounds, //!< bounds in pixel coordinates
unsigned int mipMapLevel,
double par,
ImageBitDepthEnum bitdepth,
ImagePremultiplicationEnum premult,
ImageFieldingOrderEnum fielding,
bool useBitmap = false,
StorageModeEnum storage = eStorageModeRAM,
U32 textureTarget = GL_TEXTURE_2D);
//Same as above but parameters are in the ImageParams object
Image(const ImageKey & key,
const ImageParamsPtr& params);
virtual ~Image();
bool usesBitMap() const { return _useBitmap; }
StorageModeEnum getStorageMode() const
{
return _params->getStorageInfo().mode;
}
virtual void onMemoryAllocated(bool diskRestoration) OVERRIDE FINAL;
static ImageKey makeKey(const CacheEntryHolder* holder,
U64 nodeHashKey,
bool frameVaryingOrAnimated,
double time,
ViewIdx view,
bool draftMode,
bool fullScaleWithDownscaleInputs);
static ImageParamsPtr makeParams(const RectD & rod, // the image rod in canonical coordinates
const double par,
unsigned int mipMapLevel,
bool isRoDProjectFormat,
const ImagePlaneDesc& components,
ImageBitDepthEnum bitdepth,
ImagePremultiplicationEnum premult,
ImageFieldingOrderEnum fielding,
StorageModeEnum storage = eStorageModeRAM,
U32 textureTarget = GL_TEXTURE_2D);
static ImageParamsPtr makeParams(const RectD & rod, // the image rod in canonical coordinates
const RectI& bounds,
const double par,
unsigned int mipMapLevel,
bool isRoDProjectFormat,
const ImagePlaneDesc& components,
ImageBitDepthEnum bitdepth,
ImagePremultiplicationEnum premult,
ImageFieldingOrderEnum fielding,
StorageModeEnum storage = eStorageModeRAM,
U32 textureTarget = GL_TEXTURE_2D);
// ImageParamsPtr getParams() const WARN_UNUSED_RETURN;
/**
* @brief Resizes this image so it contains newBounds, copying all the content of the current bounds of the image into
* a new buffer. This is not thread-safe and should be called only while under an ImageLocker
**/
bool ensureBounds(const RectI& newBounds, bool fillWithBlackAndTransparent = false, bool setBitmapTo1 = false);
/**
* @brief Same as ensureBounds() except that if a resize is needed, it will do the resize in the output image instead to avoid taking the
* write lock from this image.
**/
bool copyAndResizeIfNeeded(const RectI& newBounds, bool fillWithBlackAndTransparent, bool setBitmapTo1, ImagePtr* output);
static void applyTextureMapping(const RectI& bounds, const RectI& roi);
private:
static void resizeInternal(const Image* srcImg,
const RectI& srcBounds,
const RectI& merge,
bool fillWithBlackAndTransparent,
bool setBitmapTo1,
bool createInCache,
ImagePtr* outputImage);
public:
/**
* @brief Returns the region of definition of the image in canonical coordinates. It doesn't have any
* scale applied to it. In order to return the true pixel data window you must call getBounds()
* WARNING: this is NOT the same definition as in OpenFX, where the Image RoD is always in pixels.
**/
const RectD & getRoD() const
{
return _rod;
};
/**
* @brief Do not use this. This is used only to circumvent a situation where 2 images of the same hash could have a different RoD
* to prevent an assert from triggering.
**/
void setRoD(const RectD& rod);
/**
* @brief Returns the bounds where data is in the image.
* This is equivalent to calling getRoD().mipMapLevel(getMipMapLevel());
* but slightly faster since it is stored as a member of the image.
**/
RectI getBounds() const
{
QReadLocker k(&_entryLock);
return _bounds;
};
virtual size_t size() const OVERRIDE FINAL
{
std::size_t dt = dataSize();
bool got = _entryLock.tryLockForRead();
dt += _bitmap.getBounds().area();
if (got) {
_entryLock.unlock();
}
return dt;
}
///Overridden from BufferableObject
virtual std::size_t sizeInRAM() const OVERRIDE FINAL
{
return size();
}
unsigned int getMipMapLevel() const
{
return this->_params->getMipMapLevel();
}
double getScale() const
{
return getScaleFromMipMapLevel( getMipMapLevel() );
}
unsigned int getComponentsCount() const;
const ImagePlaneDesc& getComponents() const
{
return this->_params->getComponents();
}
void setBitmapDirtyZone(const RectI& zone);
/**
* @brief This function returns true if the components 'from' have enough components to
* convert to the 'to' components.
* e.g: RGBA to RGB would return true , the opposite would return false.
**/
static bool hasEnoughDataToConvert(ImagePlaneDescEnum from, ImagePlaneDescEnum to);
static std::string getFormatString(const ImagePlaneDesc& comps, ImageBitDepthEnum depth);
static std::string getDepthString(ImageBitDepthEnum depth);
static bool isBitDepthConversionLossy(ImageBitDepthEnum from, ImageBitDepthEnum to);
ImageBitDepthEnum getBitDepth() const
{
return this->_bitDepth;
}
ImageFieldingOrderEnum getFieldingOrder() const;
ImagePremultiplicationEnum getPremultiplication() const;
double getPixelAspectRatio() const;
/**
* @brief Same as getElementsCount(getComponents()) * getBounds().width()
**/
unsigned int getRowElements() const;
/**
* @brief Lock the image for reading, while this object is living, the image buffer can't be written to.
* You must ensure that the image will live as long as this object lives otherwise the pointer will be invalidated.
* You may no longer use the pointer returned by pixelAt once this object dies.
**/
class ReadAccess
: public GenericAccess
{
const Image* img;
public:
ReadAccess(const Image* img)
: GenericAccess()
, img(img)
{
if (img) {
img->lockForRead();
}
}
ReadAccess(const ReadAccess& other)
: GenericAccess()
, img(other.img)
{
//This is a recursive lock so it doesn't matter if we take it twice
if (img) {
img->lockForRead();
}
}
virtual ~ReadAccess()
{
if (img) {
img->unlock();
}
}
/**
* @brief Access pixels. The pointer must be cast to the appropriate type afterwards.
**/
const unsigned char* pixelAt(int x,
int y) const
{
assert(img);
return img->pixelAt(x, y);
}
const char* bitmapAt(int x,
int y) const
{
assert(img);
return img->getBitmapAt(x, y);
}
};
typedef boost::shared_ptr<ReadAccess> ReadAccessPtr;
/**
* @brief Lock the image for writing, while this object is living, the image buffer can't be read.
* You must ensure that the image will live as long as this object lives otherwise the pointer will be invalidated.
* You may no longer use the pointer returned by pixelAt once this object dies.
**/
class WriteAccess
: public GenericAccess
{
Image* img;
public:
WriteAccess(Image* img)
: GenericAccess()
, img(img)
{
img->lockForWrite();
}
WriteAccess(const WriteAccess& other)
: GenericAccess()
, img(other.img)
{
//This is a recursive lock so it doesn't matter if we take it twice
img->lockForWrite();
}
virtual ~WriteAccess()
{
img->unlock();
}
/**
* @brief Access pixels. The pointer must be cast to the appropriate type afterwards.
**/
unsigned char* pixelAt(int x,
int y)
{
return img->pixelAt(x, y);
}
char* bitmapAt(int x,
int y) const
{
assert(img);
return img->getBitmapAt(x, y);
}
};
typedef boost::shared_ptr<WriteAccess> WriteAccessPtr;
ReadAccess getReadRights() const
{
return ReadAccess(this);
}
WriteAccess getWriteRights()
{
return WriteAccess(this);
}
static unsigned char* pixelAtStatic(int x, int y, const RectI& bounds, int nComps, int dataSizeOf, unsigned char* buf);
private:
friend class ReadAccess;
friend class WriteAccess;
/**
* These are private accessors to the buffer. They may only exclusively called while under the lock
* of an image.
**/
const char* getBitmapAt(int x,
int y) const
{
return this->_bitmap.getBitmapAt(x, y);
}
char* getBitmapAt(int x,
int y)
{
return this->_bitmap.getBitmapAt(x, y);
}
/**
* @brief Access pixels. The pointer must be cast to the appropriate type afterwards.
**/
unsigned char* pixelAt(int x, int y);
const unsigned char* pixelAt(int x, int y) const;
/**
* @brief Locks the image for read/write access.
* There can be a deadlock situation in the following situation:
* The lock for read is taken when a plugin attempts to
* fetch an image from a source clip. But if the plug-in
* fetches twice the very same image (likely if this is a tracker on the last frame for example) then it will deadlock
* if a clip is not asking for the exact same region because it is likely there is something left to render.
*
* We detect such calls and ensure that there are no deadlock.
*
*
* NB: We cannot also rely on a mutex based implementation since it can lead to a deadlock in the following situation:
* Thread A requests image at time T and locks it
* Thread B requests image at time T+1 and locks it
* Thread A requests image at time T+1 and hangs
* Thread B requests image at time T and hangs
**/
void lockForRead() const
{
_entryLock.lockForRead();
}
void lockForWrite() const
{
_entryLock.lockForWrite();
}
void unlock() const
{
_entryLock.unlock();
}
template <typename SRCPIX, typename DSTPIX, int srcMaxValue, int dstMaxValue>
static void convertToFormatInternal_sameComps(const RectI & renderWindow,
const Image & srcImg,
Image & dstImg,
ViewerColorSpaceEnum srcColorSpace,
ViewerColorSpaceEnum dstColorSpace,
bool copyBitmap);
template <typename SRCPIX, typename DSTPIX, int srcMaxValue, int dstMaxValue, int srcNComps, int dstNComps>
static void convertToFormatInternal(const RectI & renderWindow,
const Image & srcImg,
Image & dstImg,
ViewerColorSpaceEnum srcColorSpace,
ViewerColorSpaceEnum dstColorSpace,
int channelForAlpha,
bool useAlpha0,
bool copyBitmap,
bool requiresUnpremult);
template <typename SRCPIX, typename DSTPIX, int srcMaxValue, int dstMaxValue, int srcNComps, int dstNComps,
bool requiresUnpremult>
static void convertToFormatInternalForUnpremult(const RectI & renderWindow,
const Image & srcImg,
Image & dstImg,
ViewerColorSpaceEnum srcColorSpace,
ViewerColorSpaceEnum dstColorSpace,
bool useAlpha0,
bool copyBitmap,
int channelForAlpha);
template <typename SRCPIX, typename DSTPIX, int srcMaxValue, int dstMaxValue, int srcNComps, int dstNComps,
bool requiresUnpremult, bool useColorspaces>
static void convertToFormatInternalForColorSpace(const RectI & renderWindow,
const Image & srcImg,
Image & dstImg,
bool copyBitmap,
bool useAlpha0,
ViewerColorSpaceEnum srcColorSpace,
ViewerColorSpaceEnum dstColorSpace,
int channelForAlpha);
template <typename SRCPIX, typename DSTPIX, int srcMaxValue, int dstMaxValue>
static void convertToFormatInternalForDepth(const RectI & renderWindow,
const Image & srcImg,
Image & dstImg,
ViewerColorSpaceEnum srcColorSpace,
ViewerColorSpaceEnum dstColorSpace,
int channelForAlpha,
bool useAlpha0,
bool copyBitmap,
bool requiresUnpremult);
public:
/**
* @brief Returns a list of portions of image that are not yet rendered within the
* region of interest given. This internally uses the bitmap to know what portion
* are already rendered in the image. It aims to return the minimal
* area to render. Since this problem is quite hard to solve,the different portions
* of image returned may contain already rendered pixels.
*
* Note that if the RoI is larger than the bounds of the image, the out of bounds portions
* will be added to the resulting list of rectangles.
**/
#if NATRON_ENABLE_TRIMAP
void getRestToRender_trimap(const RectI & regionOfInterest,
std::list<RectI>& ret,
bool* isBeingRenderedElsewhere) const
{
if (!_useBitmap) {
return;
}
QReadLocker locker(&_entryLock);
_bitmap.minimalNonMarkedRects_trimap(regionOfInterest, ret, isBeingRenderedElsewhere);
}
#endif
void getRestToRender(const RectI & regionOfInterest,
std::list<RectI>& ret) const
{
if (!_useBitmap) {
return;
}
QReadLocker locker(&_entryLock);
_bitmap.minimalNonMarkedRects(regionOfInterest, ret);
}
#if NATRON_ENABLE_TRIMAP
RectI getMinimalRect_trimap(const RectI & regionOfInterest,
bool* isBeingRenderedElsewhere) const
{
if (!_useBitmap) {
return regionOfInterest;
}
QReadLocker locker(&_entryLock);
return _bitmap.minimalNonMarkedBbox_trimap(regionOfInterest, isBeingRenderedElsewhere);
}
#endif
RectI getMinimalRect(const RectI & regionOfInterest) const
{
if (!_useBitmap) {
return regionOfInterest;
}
QReadLocker locker(&_entryLock);
return _bitmap.minimalNonMarkedBbox(regionOfInterest);
}
#if NATRON_ENABLE_TRIMAP
RectI getMinimalRectAndMarkForRendering_trimap(const RectI & regionOfInterest,
bool* isBeingRenderedElsewhere)
{
if (!_useBitmap) {
return regionOfInterest;
}
RectI ret;
{
QReadLocker locker(&_entryLock);
ret = _bitmap.minimalNonMarkedBbox_trimap(regionOfInterest, isBeingRenderedElsewhere);
}
markForRendering(ret);
return ret;
}
#endif
void markForRendered(const RectI & roi)
{
if (!_useBitmap) {
return;
}
QWriteLocker locker(&_entryLock);
RectI intersection;
_bounds.intersect(roi, &intersection);
_bitmap.markForRendered(intersection);
}
#if NATRON_ENABLE_TRIMAP
///Fill with 2 the roi
void markForRendering(const RectI & roi)
{
if (!_useBitmap) {
return;
}
QWriteLocker locker(&_entryLock);
RectI intersection;
_bounds.intersect(roi, &intersection);
_bitmap.markForRendering(intersection);
}
#endif
void clearBitmap(const RectI& roi)
{
if (!_useBitmap) {
return;
}
QWriteLocker locker(&_entryLock);
RectI intersection;
_bounds.intersect(roi, &intersection);
_bitmap.clear(intersection);
}
#ifdef DEBUG
void printUnrenderedPixels(const RectI& roi) const;
#endif
/**
* @brief Fills the image with the given colour. If the image components
* are not RGBA it will ignore the unsupported components.
* For example if the image comps is eImageComponentAlpha, then only the alpha value 'a' will
* be used.
**/
void fill( const RectI & roi, float r, float g, float b, float a, const OSGLContextPtr& glContext = OSGLContextPtr() );
void fillZero( const RectI& roi, const OSGLContextPtr& glContext = OSGLContextPtr() );
void fillBoundsZero( const OSGLContextPtr& glContext = OSGLContextPtr() );
/**
* @brief Same as fill(const RectI&,float,float,float,float) but fills the R,G and B
* components with the same value.
**/
void fill( const RectI & rect,
float colorValue = 0.f,
float alphaValue = 1.f,
const OSGLContextPtr& glContext = OSGLContextPtr() )
{
fill(rect, colorValue, colorValue, colorValue, alphaValue, glContext);
}
/**
* @brief Copies the content of the portion defined by roi of the other image pixels into this image.
* The internal bitmap will be copied as well
**/
void pasteFrom( const Image & src, const RectI & srcRoi, bool copyBitmap = true, const OSGLContextPtr& glContext = OSGLContextPtr() );
/**
* @brief Downscales a portion of this image into output.
* This function will adjust roi to the largest enclosed rectangle for the
* given mipmap level,
* and then computes the mipmap of the given level of that rectangle.
**/
void downscaleMipMap(const RectD& rod,
const RectI & roi,
unsigned int fromLevel, unsigned int toLevel,
bool copyBitMap,
Image* output) const;
/**
* @brief Upscales a portion of this image into output.
* If the upscaled roi does not fit into output's bounds, it is cropped first.
**/
void upscaleMipMap(const RectI & roi, unsigned int fromLevel, unsigned int toLevel, Image* output) const;
static double getScaleFromMipMapLevel(unsigned int level);
static unsigned int getLevelFromScale(double s);
/**
* @brief This function can be used to do the following conversion:
* 1) RGBA to RGB
* 2) RGBA to alpha
* 3) RGB to RGBA
* 4) RGB to alpha
*
* Also this function converts to the output bit depth.
*
* This function only works for images with the same region of definition and mipmaplevel.
*
*
* @param renderWindow The rectangle to convert
*
* @param srcColorSpace Input data will be taken to be in this color-space
*
* @param dstColorSpace Output data will be converted to this color-space.
*
* @param channelForAlpha is used in cases 2) and 4) to determine from which channel we should
* fill the alpha. If it is -1 it indicates you want to clear the mask.
*
* @param copyBitMap The bitmap will also be copied.
*
* @param requiresUnpremult If true, if a component conversion from RGBA to RGB happens
* the RGB channels will be divided by the alpha channel when copied to the output image.
*
* Note that this function is mainly used for the following conversion:
* RGBA --> Alpha
* or bit depth conversion
* Implementation should tend to optimize these cases.
**/
void convertToFormat(const RectI & renderWindow,
ViewerColorSpaceEnum srcColorSpace,
ViewerColorSpaceEnum dstColorSpace,
int channelForAlpha,
bool copyBitMap,
bool requiresUnpremult,
Image* dstImg) const;
void convertToFormatAlpha0(const RectI & renderWindow,
ViewerColorSpaceEnum srcColorSpace,
ViewerColorSpaceEnum dstColorSpace,
int channelForAlpha,
bool copyBitMap,
bool requiresUnpremult,
Image* dstImg) const;
private:
void convertToFormatCommon(const RectI & renderWindow,
ViewerColorSpaceEnum srcColorSpace,
ViewerColorSpaceEnum dstColorSpace,
int channelForAlpha,
bool useAlpha0,
bool copyBitMap,
bool requiresUnpremult,
Image* dstImg) const;
template <typename PIX, bool doPremult>
void premultInternal(const RectI& roi);
template <bool doPremult>
void premultForDepth(const RectI& roi);
public:
/**
* @brief Premultiply the image by its alpha channel on the given RoI.
* Currently there is no implementation for OpenGL textures.
**/
void premultImage(const RectI& roi);
/**
* @brief Unpremultiply the image by its alpha channel on the given RoI.
* Currently there is no implementation for OpenGL textures.
**/
void unpremultImage(const RectI& roi);
bool canCallCopyUnProcessedChannels(std::bitset<4> processChannels) const;
/**
* @brief Given the channels to process, this function copies from the originalImage the channels
* that are not marked to true in processChannels.
**/
void copyUnProcessedChannels( const RectI& roi,
ImagePremultiplicationEnum outputPremult,
ImagePremultiplicationEnum originalImagePremult,
std::bitset<4> processChannels,
const ImagePtr& originalImage,
bool ignorePremult,
const OSGLContextPtr& glContext = OSGLContextPtr() );
/**
* @brief Mask the image by the given mask and also disolves it to the originalImg with the given mix.
**/
void applyMaskMix( const RectI& roi,
const Image* maskImg,
const Image* originalImg,
bool masked,
bool maskInvert,
float mix,
const OSGLContextPtr& glContext = OSGLContextPtr() );
/**
* @brief Eeturns true if image contains NaNs or infinite values, and fix them.
* Currently, no OpenGL implementation is provided.
*/
bool checkForNaNs(const RectI& roi) WARN_UNUSED_RETURN;
void copyBitmapRowPortion(int x1, int x2, int y, const Image& other);
void copyBitmapPortion(const RectI& roi, const Image& other);
template <typename PIX>
static PIX clamp(PIX x, PIX minval, PIX maxval);
template<typename PIX>
static PIX clampIfInt(float v);
template <typename SRCPIX, typename DSTPIX>
static DSTPIX convertPixelDepth(SRCPIX pix);
private:
template<int srcNComps, int dstNComps, typename PIX, int maxValue, bool masked, bool maskInvert>
void applyMaskMixForMaskInvert(const RectI& roi,
const Image* maskImg,
const Image* originalImg,
float mix);
template<int srcNComps, int dstNComps, typename PIX, int maxValue, bool masked>
void applyMaskMixForMasked(const RectI& roi,
const Image* maskImg,
const Image* originalImg,
bool maskInvert,
float mix);
template<int srcNComps, int dstNComps, typename PIX, int maxValue>
void applyMaskMixForDepth(const RectI& roi,
const Image* maskImg,
const Image* originalImg,
bool masked,
bool maskInvert,
float mix);
template<int srcNComps, int dstNComps>
void applyMaskMixForDstComponents(const RectI& roi,
const Image* maskImg,
const Image* originalImg,
bool masked,
bool maskInvert,
float mix);
template<int srcNComps>
void applyMaskMixForSrcComponents(const RectI& roi,
const Image* maskImg,
const Image* originalImg,
bool masked,
bool maskInvert,
float mix);
template <typename PIX, int maxValue, int srcNComps, int dstNComps, bool doR, bool doG, bool doB, bool doA, bool premult, bool originalPremult, bool ignorePremult>
void copyUnProcessedChannelsForPremult(std::bitset<4> processChannels,
const RectI& roi,
const ImagePtr& originalImage);
template <typename PIX, int maxValue, int srcNComps, int dstNComps, bool ignorePremult>
void copyUnProcessedChannelsForPremult(bool premult, bool originalPremult,
std::bitset<4> processChannels,
const RectI& roi,
const ImagePtr& originalImage);
template <typename PIX, int maxValue, int srcNComps, int dstNComps, bool doR, bool doG, bool doB, bool doA>
void copyUnProcessedChannelsForChannels(const std::bitset<4> processChannels,
const bool premult,
const RectI& roi,
const ImagePtr& originalImage,
const bool originalPremult,
const bool ignorePremult);
template <typename PIX, int maxValue, int srcNComps, int dstNComps>
void copyUnProcessedChannelsForChannels(const std::bitset<4> processChannels,
const bool premult,
const RectI& roi,
const ImagePtr& originalImage,
const bool originalPremult,
const bool ignorePremult);
template <typename PIX, int maxValue, int srcNComps, int dstNComps>
void copyUnProcessedChannelsForComponents(bool premult,
const RectI& roi,
const std::bitset<4> processChannels,
const ImagePtr& originalImage,
const bool originalPremult,
const bool ignorePremult);
template <typename PIX, int maxValue>
void copyUnProcessedChannelsForDepth(bool premult,
const RectI& roi,
std::bitset<4> processChannels,
const ImagePtr& originalImage,
bool originalPremult,
bool ignorePremult);
/**
* @brief Given the output buffer,the region of interest and the mip map level, this
* function computes the mip map of this image in the given roi.
* If roi is NOT a power of 2, then it will be rounded to the closest power of 2.
**/
void buildMipMapLevel(const RectD& dstRoD, const RectI & roiCanonical, unsigned int level, bool copyBitMap,
Image* output) const;
/**
* @brief Halve the given roi of this image into output.
* If the RoI bounds are odd, the largest enclosing RoI with even bounds will be considered.
**/
void halveRoI(const RectI & roi, bool copyBitMap,
Image* output) const;