Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(474)

Side by Side Diff: core/fxge/dib/fx_dib_main.cpp

Issue 2572243002: Return unique_ptr from GetAlphaMask. (Closed)
Patch Set: Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 PDFium Authors. All rights reserved. 1 // Copyright 2014 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6 6
7 #include "core/fxge/fx_dib.h" 7 #include "core/fxge/fx_dib.h"
8 8
9 #include <limits.h> 9 #include <limits.h>
10 10
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after
121 m_pBuffer = nullptr; 121 m_pBuffer = nullptr;
122 m_Width = m_Height = m_Pitch = 0; 122 m_Width = m_Height = m_Pitch = 0;
123 return false; 123 return false;
124 } 124 }
125 } 125 }
126 } 126 }
127 return true; 127 return true;
128 } 128 }
129 129
130 bool CFX_DIBitmap::Copy(const CFX_DIBSource* pSrc) { 130 bool CFX_DIBitmap::Copy(const CFX_DIBSource* pSrc) {
131 if (m_pBuffer) { 131 if (m_pBuffer)
132 return false; 132 return false;
133 } 133
134 if (!Create(pSrc->GetWidth(), pSrc->GetHeight(), pSrc->GetFormat())) { 134 if (!Create(pSrc->GetWidth(), pSrc->GetHeight(), pSrc->GetFormat()))
135 return false; 135 return false;
136 } 136
137 CopyPalette(pSrc->GetPalette()); 137 SetPalette(pSrc->GetPalette());
138 CopyAlphaMask(pSrc->m_pAlphaMask); 138 SetAlphaMask(pSrc->m_pAlphaMask);
139 for (int row = 0; row < pSrc->GetHeight(); row++) { 139 for (int row = 0; row < pSrc->GetHeight(); row++)
140 FXSYS_memcpy(m_pBuffer + row * m_Pitch, pSrc->GetScanline(row), m_Pitch); 140 FXSYS_memcpy(m_pBuffer + row * m_Pitch, pSrc->GetScanline(row), m_Pitch);
141 } 141
142 return true; 142 return true;
143 } 143 }
144 144
145 CFX_DIBitmap::~CFX_DIBitmap() { 145 CFX_DIBitmap::~CFX_DIBitmap() {
146 if (!m_bExtBuf) 146 if (!m_bExtBuf)
147 FX_Free(m_pBuffer); 147 FX_Free(m_pBuffer);
148 148
149 m_pBuffer = nullptr; 149 m_pBuffer = nullptr;
150 } 150 }
151 151
(...skipping 27 matching lines...) Expand all
179 FX_RECT rect(0, 0, m_Width, m_Height); 179 FX_RECT rect(0, 0, m_Width, m_Height);
180 if (pClip) { 180 if (pClip) {
181 rect.Intersect(*pClip); 181 rect.Intersect(*pClip);
182 if (rect.IsEmpty()) 182 if (rect.IsEmpty())
183 return nullptr; 183 return nullptr;
184 } 184 }
185 auto pNewBitmap = pdfium::MakeUnique<CFX_DIBitmap>(); 185 auto pNewBitmap = pdfium::MakeUnique<CFX_DIBitmap>();
186 if (!pNewBitmap->Create(rect.Width(), rect.Height(), GetFormat())) 186 if (!pNewBitmap->Create(rect.Width(), rect.Height(), GetFormat()))
187 return nullptr; 187 return nullptr;
188 188
189 pNewBitmap->CopyPalette(m_pPalette.get()); 189 pNewBitmap->SetPalette(m_pPalette.get());
190 pNewBitmap->CopyAlphaMask(m_pAlphaMask, pClip); 190 pNewBitmap->SetAlphaMask(m_pAlphaMask, pClip);
191 if (GetBPP() == 1 && rect.left % 8 != 0) { 191 if (GetBPP() == 1 && rect.left % 8 != 0) {
192 int left_shift = rect.left % 32; 192 int left_shift = rect.left % 32;
193 int right_shift = 32 - left_shift; 193 int right_shift = 32 - left_shift;
194 int dword_count = pNewBitmap->m_Pitch / 4; 194 int dword_count = pNewBitmap->m_Pitch / 4;
195 for (int row = rect.top; row < rect.bottom; row++) { 195 for (int row = rect.top; row < rect.bottom; row++) {
196 uint32_t* src_scan = (uint32_t*)GetScanline(row) + rect.left / 32; 196 uint32_t* src_scan = (uint32_t*)GetScanline(row) + rect.left / 32;
197 uint32_t* dest_scan = (uint32_t*)pNewBitmap->GetScanline(row - rect.top); 197 uint32_t* dest_scan = (uint32_t*)pNewBitmap->GetScanline(row - rect.top);
198 for (int i = 0; i < dword_count; i++) { 198 for (int i = 0; i < dword_count; i++) {
199 dest_scan[i] = 199 dest_scan[i] =
200 (src_scan[i] << left_shift) | (src_scan[i + 1] >> right_shift); 200 (src_scan[i] << left_shift) | (src_scan[i + 1] >> right_shift);
(...skipping 364 matching lines...) Expand 10 before | Expand all | Expand 10 after
565 FXSYS_memcpy(dest_color_pos, color_p, comps); 565 FXSYS_memcpy(dest_color_pos, color_p, comps);
566 dest_color_pos += comps; 566 dest_color_pos += comps;
567 *dest_alpha_pos++ = (alpha * (*src_scan++) / 255); 567 *dest_alpha_pos++ = (alpha * (*src_scan++) / 255);
568 } 568 }
569 } 569 }
570 } 570 }
571 } 571 }
572 return true; 572 return true;
573 } 573 }
574 574
575 void CFX_DIBSource::CopyPalette(const uint32_t* pSrc) { 575 void CFX_DIBSource::SetPalette(const uint32_t* pSrc) {
576 static const uint32_t kPaletteSize = 256; 576 static const uint32_t kPaletteSize = 256;
577
578 if (!pSrc || GetBPP() > 8) { 577 if (!pSrc || GetBPP() > 8) {
579 m_pPalette.reset(); 578 m_pPalette.reset();
580 } else { 579 return;
581 uint32_t pal_size = 1 << GetBPP();
582 if (!m_pPalette)
583 m_pPalette.reset(FX_Alloc(uint32_t, pal_size));
584 pal_size = std::min(pal_size, kPaletteSize);
585 FXSYS_memcpy(m_pPalette.get(), pSrc, pal_size * sizeof(uint32_t));
586 } 580 }
581 uint32_t pal_size = 1 << GetBPP();
582 if (!m_pPalette)
583 m_pPalette.reset(FX_Alloc(uint32_t, pal_size));
584 pal_size = std::min(pal_size, kPaletteSize);
585 FXSYS_memcpy(m_pPalette.get(), pSrc, pal_size * sizeof(uint32_t));
587 } 586 }
588 587
589 void CFX_DIBSource::GetPalette(uint32_t* pal, int alpha) const { 588 void CFX_DIBSource::GetPalette(uint32_t* pal, int alpha) const {
590 ASSERT(GetBPP() <= 8 && !IsCmykImage()); 589 ASSERT(GetBPP() <= 8 && !IsCmykImage());
591 if (GetBPP() == 1) { 590 if (GetBPP() == 1) {
592 pal[0] = ((m_pPalette ? m_pPalette.get()[0] : 0xff000000) & 0xffffff) | 591 pal[0] = ((m_pPalette ? m_pPalette.get()[0] : 0xff000000) & 0xffffff) |
593 (alpha << 24); 592 (alpha << 24);
594 pal[1] = ((m_pPalette ? m_pPalette.get()[1] : 0xffffffff) & 0xffffff) | 593 pal[1] = ((m_pPalette ? m_pPalette.get()[1] : 0xffffffff) & 0xffffff) |
595 (alpha << 24); 594 (alpha << 24);
596 return; 595 return;
597 } 596 }
598 if (m_pPalette) { 597 if (m_pPalette) {
599 for (int i = 0; i < 256; i++) { 598 for (int i = 0; i < 256; i++) {
600 pal[i] = (m_pPalette.get()[i] & 0x00ffffff) | (alpha << 24); 599 pal[i] = (m_pPalette.get()[i] & 0x00ffffff) | (alpha << 24);
601 } 600 }
602 } else { 601 } else {
603 for (int i = 0; i < 256; i++) { 602 for (int i = 0; i < 256; i++) {
604 pal[i] = (i * 0x10101) | (alpha << 24); 603 pal[i] = (i * 0x10101) | (alpha << 24);
605 } 604 }
606 } 605 }
607 } 606 }
608 607
609 CFX_DIBitmap* CFX_DIBSource::GetAlphaMask(const FX_RECT* pClip) const { 608 std::unique_ptr<CFX_DIBitmap> CFX_DIBSource::CloneAlphaMask(
609 const FX_RECT* pClip) const {
610 ASSERT(GetFormat() == FXDIB_Argb); 610 ASSERT(GetFormat() == FXDIB_Argb);
611 FX_RECT rect(0, 0, m_Width, m_Height); 611 FX_RECT rect(0, 0, m_Width, m_Height);
612 if (pClip) { 612 if (pClip) {
613 rect.Intersect(*pClip); 613 rect.Intersect(*pClip);
614 if (rect.IsEmpty()) { 614 if (rect.IsEmpty())
615 return nullptr; 615 return nullptr;
616 }
617 } 616 }
618 CFX_DIBitmap* pMask = new CFX_DIBitmap; 617 auto pMask = pdfium::MakeUnique<CFX_DIBitmap>();
619 if (!pMask->Create(rect.Width(), rect.Height(), FXDIB_8bppMask)) { 618 if (!pMask->Create(rect.Width(), rect.Height(), FXDIB_8bppMask))
620 delete pMask;
621 return nullptr; 619 return nullptr;
622 } 620
623 for (int row = rect.top; row < rect.bottom; row++) { 621 for (int row = rect.top; row < rect.bottom; row++) {
624 const uint8_t* src_scan = GetScanline(row) + rect.left * 4 + 3; 622 const uint8_t* src_scan = GetScanline(row) + rect.left * 4 + 3;
625 uint8_t* dest_scan = (uint8_t*)pMask->GetScanline(row - rect.top); 623 uint8_t* dest_scan = (uint8_t*)pMask->GetScanline(row - rect.top);
dsinclair 2016/12/14 21:39:05 nit: static_cast
Tom Sepez 2016/12/14 22:05:05 Actually, both of these are const_casts. Done.
626 for (int col = rect.left; col < rect.right; col++) { 624 for (int col = rect.left; col < rect.right; col++) {
627 *dest_scan++ = *src_scan; 625 *dest_scan++ = *src_scan;
628 src_scan += 4; 626 src_scan += 4;
629 } 627 }
630 } 628 }
631 return pMask; 629 return pMask;
632 } 630 }
633 631
634 bool CFX_DIBSource::CopyAlphaMask(const CFX_DIBSource* pAlphaMask, 632 bool CFX_DIBSource::SetAlphaMask(const CFX_DIBSource* pAlphaMask,
635 const FX_RECT* pClip) { 633 const FX_RECT* pClip) {
636 if (!HasAlpha() || GetFormat() == FXDIB_Argb) { 634 if (!HasAlpha() || GetFormat() == FXDIB_Argb)
637 return false; 635 return false;
636
637 if (!pAlphaMask) {
638 m_pAlphaMask->Clear(0xff000000);
639 return true;
638 } 640 }
639 if (pAlphaMask) { 641 FX_RECT rect(0, 0, pAlphaMask->m_Width, pAlphaMask->m_Height);
640 FX_RECT rect(0, 0, pAlphaMask->m_Width, pAlphaMask->m_Height); 642 if (pClip) {
641 if (pClip) { 643 rect.Intersect(*pClip);
642 rect.Intersect(*pClip); 644 if (rect.IsEmpty() || rect.Width() != m_Width ||
643 if (rect.IsEmpty() || rect.Width() != m_Width || 645 rect.Height() != m_Height) {
644 rect.Height() != m_Height) { 646 return false;
645 return false;
646 }
647 } else {
648 if (pAlphaMask->m_Width != m_Width || pAlphaMask->m_Height != m_Height) {
649 return false;
650 }
651 } 647 }
652 for (int row = 0; row < m_Height; row++)
653 FXSYS_memcpy((void*)m_pAlphaMask->GetScanline(row),
654 pAlphaMask->GetScanline(row + rect.top) + rect.left,
655 m_pAlphaMask->m_Pitch);
656 } else { 648 } else {
657 m_pAlphaMask->Clear(0xff000000); 649 if (pAlphaMask->m_Width != m_Width || pAlphaMask->m_Height != m_Height)
650 return false;
658 } 651 }
652 for (int row = 0; row < m_Height; row++)
dsinclair 2016/12/14 21:39:05 nit: {}s
Tom Sepez 2016/12/14 22:05:05 much better. done.
653 FXSYS_memcpy((void*)m_pAlphaMask->GetScanline(row),
dsinclair 2016/12/14 21:39:05 static_cast?
654 pAlphaMask->GetScanline(row + rect.top) + rect.left,
655 m_pAlphaMask->m_Pitch);
659 return true; 656 return true;
660 } 657 }
661 658
662 const int g_ChannelOffset[] = {0, 2, 1, 0, 0, 1, 2, 3, 3}; 659 const int g_ChannelOffset[] = {0, 2, 1, 0, 0, 1, 2, 3, 3};
663 bool CFX_DIBitmap::LoadChannel(FXDIB_Channel destChannel, 660 bool CFX_DIBitmap::LoadChannel(FXDIB_Channel destChannel,
664 CFX_DIBSource* pSrcBitmap, 661 CFX_DIBSource* pSrcBitmap,
665 FXDIB_Channel srcChannel) { 662 FXDIB_Channel srcChannel) {
666 if (!m_pBuffer) 663 if (!m_pBuffer)
667 return false; 664 return false;
668 665
(...skipping 673 matching lines...) Expand 10 before | Expand all | Expand 10 after
1342 } 1339 }
1343 return true; 1340 return true;
1344 } 1341 }
1345 1342
1346 CFX_DIBitmap* CFX_DIBSource::FlipImage(bool bXFlip, bool bYFlip) const { 1343 CFX_DIBitmap* CFX_DIBSource::FlipImage(bool bXFlip, bool bYFlip) const {
1347 CFX_DIBitmap* pFlipped = new CFX_DIBitmap; 1344 CFX_DIBitmap* pFlipped = new CFX_DIBitmap;
1348 if (!pFlipped->Create(m_Width, m_Height, GetFormat())) { 1345 if (!pFlipped->Create(m_Width, m_Height, GetFormat())) {
1349 delete pFlipped; 1346 delete pFlipped;
1350 return nullptr; 1347 return nullptr;
1351 } 1348 }
1352 pFlipped->CopyPalette(m_pPalette.get()); 1349 pFlipped->SetPalette(m_pPalette.get());
1353 uint8_t* pDestBuffer = pFlipped->GetBuffer(); 1350 uint8_t* pDestBuffer = pFlipped->GetBuffer();
1354 int Bpp = m_bpp / 8; 1351 int Bpp = m_bpp / 8;
1355 for (int row = 0; row < m_Height; row++) { 1352 for (int row = 0; row < m_Height; row++) {
1356 const uint8_t* src_scan = GetScanline(row); 1353 const uint8_t* src_scan = GetScanline(row);
1357 uint8_t* dest_scan = 1354 uint8_t* dest_scan =
1358 pDestBuffer + m_Pitch * (bYFlip ? (m_Height - row - 1) : row); 1355 pDestBuffer + m_Pitch * (bYFlip ? (m_Height - row - 1) : row);
1359 if (!bXFlip) { 1356 if (!bXFlip) {
1360 FXSYS_memcpy(dest_scan, src_scan, m_Pitch); 1357 FXSYS_memcpy(dest_scan, src_scan, m_Pitch);
1361 continue; 1358 continue;
1362 } 1359 }
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
1416 } 1413 }
1417 1414
1418 CFX_DIBExtractor::CFX_DIBExtractor(const CFX_DIBSource* pSrc) { 1415 CFX_DIBExtractor::CFX_DIBExtractor(const CFX_DIBSource* pSrc) {
1419 if (pSrc->GetBuffer()) { 1416 if (pSrc->GetBuffer()) {
1420 m_pBitmap = pdfium::MakeUnique<CFX_DIBitmap>(); 1417 m_pBitmap = pdfium::MakeUnique<CFX_DIBitmap>();
1421 if (!m_pBitmap->Create(pSrc->GetWidth(), pSrc->GetHeight(), 1418 if (!m_pBitmap->Create(pSrc->GetWidth(), pSrc->GetHeight(),
1422 pSrc->GetFormat(), pSrc->GetBuffer())) { 1419 pSrc->GetFormat(), pSrc->GetBuffer())) {
1423 m_pBitmap.reset(); 1420 m_pBitmap.reset();
1424 return; 1421 return;
1425 } 1422 }
1426 m_pBitmap->CopyPalette(pSrc->GetPalette()); 1423 m_pBitmap->SetPalette(pSrc->GetPalette());
1427 m_pBitmap->CopyAlphaMask(pSrc->m_pAlphaMask); 1424 m_pBitmap->SetAlphaMask(pSrc->m_pAlphaMask);
1428 } else { 1425 } else {
1429 m_pBitmap = pSrc->Clone(); 1426 m_pBitmap = pSrc->Clone();
1430 } 1427 }
1431 } 1428 }
1432 1429
1433 CFX_DIBExtractor::~CFX_DIBExtractor() {} 1430 CFX_DIBExtractor::~CFX_DIBExtractor() {}
1434 1431
1435 CFX_FilteredDIB::CFX_FilteredDIB() : m_pSrc(nullptr) {} 1432 CFX_FilteredDIB::CFX_FilteredDIB() : m_pSrc(nullptr) {}
1436 1433
1437 CFX_FilteredDIB::~CFX_FilteredDIB() { 1434 CFX_FilteredDIB::~CFX_FilteredDIB() {
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after
1635 bool CFX_BitmapStorer::SetInfo(int width, 1632 bool CFX_BitmapStorer::SetInfo(int width,
1636 int height, 1633 int height,
1637 FXDIB_Format src_format, 1634 FXDIB_Format src_format,
1638 uint32_t* pSrcPalette) { 1635 uint32_t* pSrcPalette) {
1639 m_pBitmap = pdfium::MakeUnique<CFX_DIBitmap>(); 1636 m_pBitmap = pdfium::MakeUnique<CFX_DIBitmap>();
1640 if (!m_pBitmap->Create(width, height, src_format)) { 1637 if (!m_pBitmap->Create(width, height, src_format)) {
1641 m_pBitmap.reset(); 1638 m_pBitmap.reset();
1642 return false; 1639 return false;
1643 } 1640 }
1644 if (pSrcPalette) 1641 if (pSrcPalette)
1645 m_pBitmap->CopyPalette(pSrcPalette); 1642 m_pBitmap->SetPalette(pSrcPalette);
1646 return true; 1643 return true;
1647 } 1644 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698