-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
bitmap.cpp
1542 lines (1269 loc) · 37 KB
/
bitmap.cpp
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
/**************************************************************************
*
*
* Abstract:
*
* Contains bitmap base class implementation
*
**************************************************************************/
#include "precomp.hpp"
MtDefine(CBaseWGXBitmap, MILImaging, "CBaseWGXBitmap");
MtDefine(CWGXBitmapLock, MILImaging, "CWGXBitmapLock");
MtDefine(CWGXBitmapLockUnaligned, MILImaging, "CWGXBitmapLockUnaligned");
MtDefine(MUnalignedLockData, MILRawMemory, "MUnalignedLockData");
MtDefine(DirtyRectArray, CWGXBitmap, "CWGXBitmap::m_rgDirtyRects");
CBaseWGXBitmap::CBaseWGXBitmap()
{
m_rgDirtyRects = NULL;
m_cDirtyRects = 0;
m_fClearDirtyListOnNextAdd = true;
m_nUniquenessBeforeFirstDirtyRect = 0;
m_nUniquenessAtLastDirtyRect = 0;
}
CBaseWGXBitmap::~CBaseWGXBitmap()
{
WPFFree(ProcessHeap, m_rgDirtyRects);
}
STDMETHODIMP
CBaseWGXBitmap::HrFindInterface(
__in_ecount(1) REFIID riid,
__deref_out void **ppvObject
)
{
HRESULT hr = E_INVALIDARG;
if (ppvObject)
{
if (riid == IID_IMILResourceCache)
{
*ppvObject = static_cast<IMILResourceCache*>(this);
hr = S_OK;
}
else if (riid == IID_IWGXBitmap)
{
*ppvObject = static_cast<IWGXBitmap*>(this);
hr = S_OK;
}
else if (riid == IID_IWGXBitmapSource)
{
*ppvObject = static_cast<IWGXBitmapSource*>(this);
hr = S_OK;
}
else
{
hr = E_NOINTERFACE;
}
}
return hr;
}
//+------------------------------------------------------------------------
//
// Function: CWGXBitmap::AddDirtyRect
//
// Synopsis: Adds a new rectangle to the dirty list. Pass NULL to mark
// the entire bitmap as dirty. This function updates the
// uniqueness count so that the bitmap will be re-realized
// upon drawing.
//
//-------------------------------------------------------------------------
STDMETHODIMP
CBaseWGXBitmap::AddDirtyRect(
__in_ecount(1) const RECT *prcDirtyRect
)
{
HRESULT hr = S_OK;
AssertEntry(*this);
UINT uWidth, uHeight;
IFC(GetSize(&uWidth, &uHeight));
if ( (prcDirtyRect != NULL)
&& ( prcDirtyRect->left < 0
|| prcDirtyRect->top < 0
|| prcDirtyRect->right <= prcDirtyRect->left
|| (static_cast<UINT>(prcDirtyRect->right) > uWidth)
|| prcDirtyRect->bottom <= prcDirtyRect->top
|| (static_cast<UINT>(prcDirtyRect->bottom) > uHeight)
) )
{
IFC(E_INVALIDARG);
}
CMilRectU const *prcDirty = reinterpret_cast<CMilRectU const *>(prcDirtyRect);
// No need to remember dirty rects for a bitmap that is not
// cached yet.
if (CMILResourceCache::Count == 0)
{
Assert(m_cDirtyRects == 0);
goto Cleanup;
}
if (m_fClearDirtyListOnNextAdd ||
m_nUniquenessAtLastDirtyRect != GetUniqueCount())
{
m_cDirtyRects = 0;
m_fClearDirtyListOnNextAdd = FALSE;
}
//
// prcDirtyRect == NULL implies that the entire
// surface is dirty
//
if ((prcDirty == NULL) ||
((prcDirty->Width<UINT>() == uWidth) &&
(prcDirty->Height<UINT>() == uHeight)))
{
m_cDirtyRects = 0;
}
else
{
if (m_cDirtyRects == 0)
{
m_nUniquenessBeforeFirstDirtyRect = GetUniqueCount();
}
else
{
Assert(m_rgDirtyRects);
}
if ( (m_cDirtyRects > 0)
&& (m_rgDirtyRects[0].DoesContain(*prcDirty)))
{
// No dirty list change - new dirty rect is already included.
}
else if (m_cDirtyRects >= c_maxBitmapDirtyListSize)
{
// Collapse dirty list to a single large rect (including new rect)
while (m_cDirtyRects > 1)
{
m_rgDirtyRects[0].Union(m_rgDirtyRects[--m_cDirtyRects]);
}
m_rgDirtyRects[0].Union(*prcDirty);
Assert(m_cDirtyRects == 1);
}
else
{
if (!m_rgDirtyRects)
{
m_rgDirtyRects =
WPFAllocType(CMilRectU *,
ProcessHeap, Mt(DirtyRectArray),
c_maxBitmapDirtyListSize*sizeof(*m_rgDirtyRects));
IFCOOM(m_rgDirtyRects);
}
m_rgDirtyRects[m_cDirtyRects++] = *prcDirty;
}
}
Cleanup:
// Update the uniqueness count to ensure that
// the bitmap knows there is a change
UpdateUniqueCount();
if (SUCCEEDED(hr))
{
m_nUniquenessAtLastDirtyRect = GetUniqueCount();
}
else
{
// If we fail then we should update the entire bitmap. Clearing
// the dirty list will cause this to happen.
m_cDirtyRects = 0;
}
RRETURN(hr);
}
//+----------------------------------------------------------------------------
//
// Member:
// CBaseWGXBitmap::GetDirtyRects
//
// Synopsis:
// Gets a reference to the dirty rect array. Additionally any call will
// set call to clear out the list upon next dirty addition.
//
// Return:
// True means caller's cache (whatever the caller retains and is
// associated with uniqueness) is not completely invalid and dirty list is
// useful. Note caller's cache may be completely valid, which is
// indicated by *pcDirtyRects be set to 0.
//
// False means just the opposite. The caller cache is at least somewhat
// invalid. But since no dirty rect list can be generated that indicates
// what area are invalid, the caller's cache must be treated as completely
// invalid.
//
// Outputs:
// Out parameters are always set, even if false it returned.
//
// *pCachedUniqueness will always hold the bitmap's current uniqueness
// value.
//
// If false is returned or given cached uniqueness (*pCachedUniqueness)
// matches current uniqueness, then:
// 1. *pcDirtyRects will be 0 and
// 2. *prgDirtyRects will be NULL.
// Otherwise:
// 1. *pcDirtyRects will be a value from 1 to 5 and
// 2. *prgDirtyRects will be an array of *pcDirtyRects rects. The
// array may contain rectangles that overlap.
//
//-----------------------------------------------------------------------------
__success(true) STDMETHODIMP_(bool)
CBaseWGXBitmap::GetDirtyRects(
__deref_out_ecount(*pcDirtyRects) MilRectU const ** const prgDirtyRects, // disallow assignment to prgDirtyRects directly
__deref_out_range(0,5) UINT * const pcDirtyRects,
__inout_ecount(1) UINT * const pCachedUniqueness
)
{
// Note: the caller should assert the entry for the length of their
// use of this dirty list. This assert is just an extra protection
AssertEntry(*this);
Assert(prgDirtyRects);
Assert(pcDirtyRects);
bool fReturningValidDirtyList;
// Set indicator that dirty list should be cleared on next dirty add.
// Clearing is done so that the most recent caller of GetDirtyRects has the
// best chance of getting a positive result the next time it calls.
m_fClearDirtyListOnNextAdd = TRUE;
UINT nCurrentUnique = GetUniqueCount();
UINT nCachedUniqueness = *pCachedUniqueness;
// Always update cached uniqueness - this is the uniqueness that will be
// matched after applying dirty rects (or a full update if no dirty list is
// available.)
*pCachedUniqueness = nCurrentUnique;
//
// Check to see if the cache must be updated by this
// dirty rect list. Checks to make sure that the list is
// a. meaningful to the cache
// b. valid
// c. non-empty
//
if (nCachedUniqueness == m_nUniquenessBeforeFirstDirtyRect &&
m_nUniquenessAtLastDirtyRect == nCurrentUnique &&
m_cDirtyRects != 0)
{
Assert(m_rgDirtyRects);
*prgDirtyRects = m_rgDirtyRects;
*pcDirtyRects = m_cDirtyRects;
fReturningValidDirtyList = true;
}
else
{
// In this case the cache should update the entire rect
// if the uniqueness is different
*prgDirtyRects = NULL;
*pcDirtyRects = 0;
// If the uniqueness is different, then there is no available dirty
// list. When the same, the list is empty. Either way the same
// pointer and count is returned.
fReturningValidDirtyList = (nCachedUniqueness == nCurrentUnique);
}
return fReturningValidDirtyList;
}
STDMETHODIMP_(IWGXBitmap::SourceState::Enum)
CBaseWGXBitmap::SourceState() const
{
return IWGXBitmap::SourceState::FullSystemMemory;
}
STDMETHODIMP_(void)
CBaseWGXBitmap::GetUniquenessToken(
__out_ecount(1) UINT *puToken
) const
{
*puToken = GetUniqueCount();
}
CWGXBitmap::CWGXBitmap()
{
m_pPalette = NULL;
m_nWidth = 0;
m_nHeight = 0;
// Use a default value of 0 to show resolution hasn't been set. This is
// safer than DpiScale::DefaultPixelsPerInch(), which could cause rendering bugs, since we
// usually test at the same desktop DPI.
m_fltDpiX = 0.0f;
m_fltDpiY = 0.0f;
m_PixelFormat = MilPixelFormat::DontCare;
}
CWGXBitmap::~CWGXBitmap()
{
ReleaseInterfaceNoNULL(m_pPalette);
Verify(SUCCEEDED(m_lockState.CheckNoLocks()));
}
HRESULT CWGXBitmap::Init(
__in_ecount(1) IWGXBitmapSource *pISource
)
{
return E_FAIL;
}
HRESULT CWGXBitmap::CopyPixelsHelper(
__in MilPixelFormat::Enum pixelFormat,
__in UINT width,
__in UINT height,
__in UINT cbInputStride,
__in UINT cbInputBufferSize,
__in_bcount(cbInputBufferSize) BYTE *pbInputBuffer,
__in UINT cbOutputStride,
__in UINT cbOutputBufferSize,
__out_bcount(cbOutputBufferSize) BYTE *pbOutputBuffer
)
{
HRESULT hr = S_OK;
UINT cbCopyStride = 0;
// Check for input overflow
IFC(HrCheckBufferSize(
pixelFormat,
cbInputStride,
width,
height,
cbInputBufferSize));
// Check for output overflow
IFC(HrCheckBufferSize(
pixelFormat,
cbOutputStride,
width,
height,
cbOutputBufferSize));
IFC(HrCalcByteAlignedScanlineStride(width, pixelFormat, cbCopyStride));
if (cbCopyStride > cbOutputStride)
{
IFC(E_INVALIDARG);
}
// NOTE: we rely on the above Lock call to correctly align bit-packed
// pixel formats, so that when we Copy, we are copying left-aligned
// pixels.
for (UINT i=0; i<height; i++)
{
#pragma prefast(push)
#pragma prefast(disable:2015, "prefast can't correctly recognize the buffer size validation")
#pragma prefast(disable:12105, "prefast can't correctly recognize the buffer size validation")
GpMemcpy(pbOutputBuffer, pbInputBuffer, cbCopyStride);
#pragma prefast(pop)
pbOutputBuffer += cbOutputStride;
pbInputBuffer += cbInputStride;
}
Cleanup:
RRETURN(hr);
}
/**************************************************************************
*
* Function Description:
*
* Copy
*
* Copies pixel blocks into and out of the bitmap.
* This implementation provides Copy from Lock in the base class for bitmaps
* which already have the pixels loaded into memory. Such bitmaps may simply
* implement Lock and inherit this method.
*
* Arguments:
*
* prc - Rectangle in the surface space. NULL indicates the entire
* surface. Rectangles which exceed the surface bounds cause
* this routine to fail.
* pvPixels - Caller provided pixel buffer.
* cbStride - Stride for the caller pixel buffer.
*
* Created:
*
* 11/14/2001 asecchia
* Created it.
*
**************************************************************************/
HRESULT CWGXBitmap::CopyPixels(
__in_ecount_opt(1) const WICRect *prc,
__in UINT cbStride,
__in UINT cbBufferSize,
__out_ecount(cbBufferSize) BYTE *pbPixels
)
{
if (!pbPixels)
{
RRETURN(E_INVALIDARG);
}
IWGXBitmapLock *pILock = NULL;
WICRect rect;
HRESULT hr = S_OK;
if (prc == NULL)
{
rect.X = 0;
rect.Y = 0;
if (SUCCEEDED(hr))
{
hr = UIntToInt(m_nWidth, &rect.Width);
}
if (SUCCEEDED(hr))
{
hr = UIntToInt(m_nHeight, &rect.Height);
}
if (SUCCEEDED(hr))
{
prc = ▭
}
}
if (SUCCEEDED(hr))
{
RECT rcLock;
hr = HrCheckPixelRect(prc, &rcLock);
}
if (SUCCEEDED(hr))
{
MIL_THR(Lock(prc, MilBitmapLock::Read, &pILock));
}
UINT nLockWidth = 0;
UINT nLockHeight = 0;
if (SUCCEEDED(hr))
{
MIL_THR(HrCheckBufferSize(m_PixelFormat, cbStride, prc, cbBufferSize));
}
if (SUCCEEDED(hr))
{
MIL_THR(pILock->GetSize(&nLockWidth, &nLockHeight));
}
UINT cbLockStride = 0;
if (SUCCEEDED(hr))
{
MIL_THR(pILock->GetStride(&cbLockStride));
}
BYTE *pbSurface = NULL;
UINT cbSurface = 0;
if (SUCCEEDED(hr))
{
MIL_THR(pILock->GetDataPointer(&cbSurface, &pbSurface));
}
if (SUCCEEDED(hr))
{
// Copy the pixels from the surface into the caller-provided buffer.
MIL_THR(CopyPixelsHelper(
m_PixelFormat,
prc->Width,
nLockHeight,
cbLockStride,
cbSurface,
pbSurface,
cbStride,
cbBufferSize,
pbPixels));
}
ReleaseInterfaceNoNULL(pILock);
RRETURN(hr);
}
STDMETHODIMP CWGXBitmap::GetSize(
__out_ecount(1) UINT *pnWidth,
__out_ecount(1) UINT *pnHeight
)
{
if (!pnWidth || !pnHeight)
{
return E_INVALIDARG;
}
*pnWidth = m_nWidth;
*pnHeight = m_nHeight;
return S_OK;
}
STDMETHODIMP CWGXBitmap::GetPixelFormat(
__out_ecount(1) MilPixelFormat::Enum *pPixelFormat
)
{
*pPixelFormat = m_PixelFormat;
return S_OK;
}
STDMETHODIMP CWGXBitmap::CopyPalette(
__in_ecount(1) IWICPalette *pIPalette
)
{
HRESULT hr = S_OK;
if (m_pPalette == NULL)
{
IFC(WGXERR_PALETTEUNAVAILABLE);
}
IFC(pIPalette->InitializeFromPalette(m_pPalette));
Cleanup:
RRETURN(hr);
}
/**************************************************************************
*
* Function Description:
*
* GetResolution
*
* Often bitmaps have an implied resolution equal to the DPI of the screen
* Define this functionality on the base class so that it can be inherited
*
* Created:
*
* 12/23/2001 asecchia
* Created it.
*
**************************************************************************/
STDMETHODIMP CWGXBitmap::GetResolution(
__out_ecount(1) double *pDpiX,
__out_ecount(1) double *pDpiY
)
{
HRESULT hr = S_OK;
if (!pDpiX || !pDpiY)
{
IFC(E_INVALIDARG);
}
// Assume a default resolution on bitmaps.
*pDpiX = (double)m_fltDpiX;
*pDpiY = (double)m_fltDpiY;
Cleanup:
RRETURN(hr);
}
STDMETHODIMP CWGXBitmap::SetPalette(
__in_ecount(1) IWICPalette *pIPalette
)
{
HRESULT hr = S_OK;
IWICImagingFactory *pIWICFactory = NULL;
if (m_pPalette == NULL)
{
IFC(WICCreateImagingFactory_Proxy(WINCODEC_SDK_VERSION_WPF, &pIWICFactory));
IFC(pIWICFactory->CreatePalette(&m_pPalette));
}
IFC(m_pPalette->InitializeFromPalette(pIPalette));
// Ideally we'd check the original palette to see if anything really changed, but that's
// somewhat expensive
UpdateUniqueCount();
Cleanup:
if (FAILED(hr))
{
ReleaseInterface(m_pPalette);
}
ReleaseInterface(pIWICFactory);
RRETURN(hr);
}
STDMETHODIMP CWGXBitmap::Unlock(
__in_ecount(1) CWGXBitmapLock *pBitmapLock
)
{
DWORD dwFlags = pBitmapLock->GetFlags();
if (dwFlags & MilBitmapLock::Write)
{
m_lockState.UnlockWrite();
}
else if (dwFlags & MilBitmapLock::Read)
{
m_lockState.UnlockRead();
}
return S_OK;
}
STDMETHODIMP CWGXBitmap::SetResolution(
__in double dblDpiX,
__in double dblDpiY)
{
float fltDpiX = static_cast<float>(dblDpiX);
float fltDpiY = static_cast<float>(dblDpiY);
if (fltDpiX != m_fltDpiX || fltDpiY != m_fltDpiY)
{
m_fltDpiX = fltDpiX;
m_fltDpiY = fltDpiY;
UpdateUniqueCount();
}
return S_OK;
}
HRESULT CWGXBitmap::HrCheckPixelRect(
__in_ecount_opt(1) const WICRect *prcInput,
__out_ecount(1) RECT *prcOutput)
{
HRESULT hr = S_OK;
SetRect(prcOutput, 0, 0, m_nWidth, m_nHeight);
if (prcInput)
{
RECT rc;
rc.left = prcInput->X;
rc.top = prcInput->Y;
ULONG tmp1, tmp2, tmpResult;
IFC(LongToULong(prcInput->X, &tmp1));
IFC(LongToULong(prcInput->Width, &tmp2));
IFC(ULongAdd(tmp1, tmp2, &tmpResult));
IFC(ULongToLong(tmpResult, &(rc.right)));
IFC(LongToULong(prcInput->Y, &tmp1));
IFC(LongToULong(prcInput->Height, &tmp2));
IFC(ULongAdd(tmp1, tmp2, &tmpResult));
IFC(ULongToLong(tmpResult, &(rc.bottom)));
// If prc is specified, but it's empty (no area or negative
// width, height) then this is not a valid input.
// The prc must be fully contained within the image rectangle
// for it to be valid. We test this by intersecting the two
// rectangles and making sure the result is the same as prc.
// This works because we ensure prc is well formed and not empty.
IntersectRect(prcOutput, &rc, prcOutput);
if (IsRectEmpty(&rc) || !EqualRect(prcOutput, &rc))
{
RRETURN(E_INVALIDARG);
}
}
Cleanup:
RRETURN(hr);
}
HRESULT CWGXBitmap::HrLock(
__in_ecount(1) const RECT &rcLock,
__in MilPixelFormat::Enum pxlFormat,
__in UINT cbStride,
__in UINT cbBufferSize,
__in_bcount(cbBufferSize) VOID *pvPixels,
__in DWORD dwFlags,
__deref_out_ecount(1) IWGXBitmapLock **ppILock,
__in BOOL fLockOwnsPixelData
)
{
HRESULT hr = S_OK;
BOOL fUnlockNeeded = FALSE;
CWGXBitmapLock *pLock = NULL;
if (dwFlags & MilBitmapLock::Write)
{
IFC(m_lockState.LockWrite());
}
else if (dwFlags & MilBitmapLock::Read)
{
IFC(m_lockState.LockRead());
}
else
{
IFC(E_INVALIDARG);
}
fUnlockNeeded = TRUE;
pLock = new CWGXBitmapLock();
IFCOOM(pLock);
pLock->AddRef();
IFC(pLock->Init(
this,
rcLock.right - rcLock.left,
rcLock.bottom - rcLock.top,
pxlFormat,
cbStride,
cbBufferSize,
pvPixels,
dwFlags,
fLockOwnsPixelData
));
fUnlockNeeded = FALSE;
if (dwFlags & MilBitmapLock::Write)
{
IFC(AddDirtyRect(&rcLock));
}
*ppILock = pLock;
pLock = NULL;
Cleanup:
if (fUnlockNeeded)
{
if (dwFlags & MilBitmapLock::Write)
{
m_lockState.UnlockWrite();
}
else if (dwFlags & MilBitmapLock::Read)
{
m_lockState.UnlockRead();
}
}
// If we succeed in locking but fail afterwards, this will release the
// outstanding bitmap lock.
ReleaseInterface(pLock);
RRETURN(hr);
}
HRESULT CWGXBitmap::HrLockUnaligned(
__in_ecount(1) const RECT &rcLock,
MilPixelFormat::Enum pxlFormat,
__in_range(1, 7) UINT nBitsStart,
UINT nBitsTotal,
UINT cbStride,
UINT cbBufferSize,
__in_bcount(cbBufferSize) VOID *pvPixels,
DWORD dwFlags,
__deref_out_ecount(1) IWGXBitmapLock **ppILock
)
{
HRESULT hr = S_OK;
BOOL fUnlockNeeded = FALSE;
CWGXBitmapLockUnaligned *pLock = NULL;
if (dwFlags & MilBitmapLock::Write)
{
IFC(m_lockState.LockWrite());
}
else if (dwFlags & MilBitmapLock::Read)
{
IFC(m_lockState.LockRead());
}
else
{
IFC(E_INVALIDARG);
}
fUnlockNeeded = TRUE;
pLock = new CWGXBitmapLockUnaligned();
IFCOOM(pLock);
pLock->AddRef();
IFC(pLock->HrInit(
this,
rcLock.right - rcLock.left,
rcLock.bottom - rcLock.top,
pxlFormat,
cbStride,
cbBufferSize,
pvPixels,
dwFlags,
nBitsStart,
nBitsTotal
));
fUnlockNeeded = FALSE;
if (dwFlags & MilBitmapLock::Write)
{
IFC(AddDirtyRect(&rcLock));
}
*ppILock = pLock;
pLock = NULL;
Cleanup:
if (fUnlockNeeded)
{
if (dwFlags & MilBitmapLock::Write)
{
m_lockState.UnlockWrite();
}
else if (dwFlags & MilBitmapLock::Read)
{
m_lockState.UnlockRead();
}
}
// If we succeed in locking but fail afterwards, this will release the
// outstanding bitmap lock.
ReleaseInterface(pLock);
RRETURN(hr);
}
HRESULT CWGXBitmap::HrInitDataFromBitmapSource(
__in_ecount(1) IWGXBitmapSource *pISource,
__in_ecount_opt(1) WICRect * srcRect)
{
HRESULT hr = S_OK;
IWICImagingFactory *pIWICFactory = NULL;
if (SUCCEEDED(hr))
{
MIL_THR(pISource->GetSize(&m_nWidth, &m_nHeight));
if (srcRect != NULL)
{
Assert ((srcRect->Width > 0) && (srcRect->Height > 0) &&
(srcRect->X >= 0) && (srcRect->Y >= 0) &&
(static_cast<UINT>(srcRect->X + srcRect->Width) <= m_nWidth) &&
(static_cast<UINT>(srcRect->Y + srcRect->Height) <= m_nHeight));
m_nWidth = srcRect->Width;
m_nHeight = srcRect->Height;
}
}
if (SUCCEEDED(hr))
{
MIL_THR(pISource->GetPixelFormat(&m_PixelFormat));
}
if (SUCCEEDED(hr))
{
DWORD dwPixelFormatSize = GetPixelFormatSize(m_PixelFormat);
if (dwPixelFormatSize == 0)
{
MIL_THR(WINCODEC_ERR_UNKNOWNIMAGEFORMAT);
}
else if (m_nWidth >= (UINT)(INT_MAX / dwPixelFormatSize))
{
MIL_THR(WINCODEC_ERR_VALUEOVERFLOW);
}
}
// Check whether the bitmap size overflows.
if (SUCCEEDED(hr))
{
UINT cbStride;
// This won't overflow because we checked for that above.
MIL_THR(HrCalcDWordAlignedScanlineStride(m_nWidth, m_PixelFormat, cbStride));
if (SUCCEEDED(hr))
{
if (m_nHeight != 0 &&
(cbStride >= (UINT)(INT_MAX / m_nHeight)))
{
MIL_THR(WINCODEC_ERR_VALUEOVERFLOW);
}
}
}
if (SUCCEEDED(hr))
{
double dblDpiX = 0.0;
double dblDpiY = 0.0;
MIL_THR(pISource->GetResolution(&dblDpiX, &dblDpiY));
m_fltDpiX = static_cast<FLOAT>(dblDpiX);
m_fltDpiY = static_cast<FLOAT>(dblDpiY);
}
if (SUCCEEDED(hr))
{
if (IsIndexedPixelFormat(m_PixelFormat))
{
if (m_pPalette == NULL)
{
MIL_THR(WICCreateImagingFactory_Proxy(WINCODEC_SDK_VERSION_WPF, &pIWICFactory));
if (SUCCEEDED(hr))
{
MIL_THR(pIWICFactory->CreatePalette(&m_pPalette));
}
}
if (SUCCEEDED(hr))
{
MIL_THR(pISource->CopyPalette(m_pPalette));
}
}
}
if (FAILED(hr))
ReleaseInterface(m_pPalette);
ReleaseInterface(pIWICFactory);
RRETURN(hr);
}
HRESULT CreateBitmapFromSourceRect(
__in_ecount(1) IWGXBitmapSource *pISource,
__in UINT x,
__in UINT y,
__in UINT width,
__in UINT height,
__in BOOL fCopySource,
__deref_out_ecount(1) IWGXBitmap **ppBitmap
)
{
HRESULT hr = S_OK;
CSystemMemoryBitmap *pTempBitmap = new CSystemMemoryBitmap();
if (NULL == pTempBitmap)
{
MIL_THR(E_OUTOFMEMORY);
}
else
{
pTempBitmap->AddRef();
}
if (SUCCEEDED(hr))
{
WICRect rc = {x, y, width, height };
MIL_THR(pTempBitmap->Init(pISource, &rc, fCopySource));
}