OLD | NEW |
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 Loading... |
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 Loading... |
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 Loading... |
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 = |
| 624 const_cast<uint8_t*>(pMask->GetScanline(row - rect.top)); |
626 for (int col = rect.left; col < rect.right; col++) { | 625 for (int col = rect.left; col < rect.right; col++) { |
627 *dest_scan++ = *src_scan; | 626 *dest_scan++ = *src_scan; |
628 src_scan += 4; | 627 src_scan += 4; |
629 } | 628 } |
630 } | 629 } |
631 return pMask; | 630 return pMask; |
632 } | 631 } |
633 | 632 |
634 bool CFX_DIBSource::CopyAlphaMask(const CFX_DIBSource* pAlphaMask, | 633 bool CFX_DIBSource::SetAlphaMask(const CFX_DIBSource* pAlphaMask, |
635 const FX_RECT* pClip) { | 634 const FX_RECT* pClip) { |
636 if (!HasAlpha() || GetFormat() == FXDIB_Argb) { | 635 if (!HasAlpha() || GetFormat() == FXDIB_Argb) |
637 return false; | 636 return false; |
| 637 |
| 638 if (!pAlphaMask) { |
| 639 m_pAlphaMask->Clear(0xff000000); |
| 640 return true; |
638 } | 641 } |
639 if (pAlphaMask) { | 642 FX_RECT rect(0, 0, pAlphaMask->m_Width, pAlphaMask->m_Height); |
640 FX_RECT rect(0, 0, pAlphaMask->m_Width, pAlphaMask->m_Height); | 643 if (pClip) { |
641 if (pClip) { | 644 rect.Intersect(*pClip); |
642 rect.Intersect(*pClip); | 645 if (rect.IsEmpty() || rect.Width() != m_Width || |
643 if (rect.IsEmpty() || rect.Width() != m_Width || | 646 rect.Height() != m_Height) { |
644 rect.Height() != m_Height) { | 647 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 } | 648 } |
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 { | 649 } else { |
657 m_pAlphaMask->Clear(0xff000000); | 650 if (pAlphaMask->m_Width != m_Width || pAlphaMask->m_Height != m_Height) |
| 651 return false; |
| 652 } |
| 653 for (int row = 0; row < m_Height; row++) { |
| 654 FXSYS_memcpy(const_cast<uint8_t*>(m_pAlphaMask->GetScanline(row)), |
| 655 pAlphaMask->GetScanline(row + rect.top) + rect.left, |
| 656 m_pAlphaMask->m_Pitch); |
658 } | 657 } |
659 return true; | 658 return true; |
660 } | 659 } |
661 | 660 |
662 const int g_ChannelOffset[] = {0, 2, 1, 0, 0, 1, 2, 3, 3}; | 661 const int g_ChannelOffset[] = {0, 2, 1, 0, 0, 1, 2, 3, 3}; |
663 bool CFX_DIBitmap::LoadChannel(FXDIB_Channel destChannel, | 662 bool CFX_DIBitmap::LoadChannel(FXDIB_Channel destChannel, |
664 CFX_DIBSource* pSrcBitmap, | 663 CFX_DIBSource* pSrcBitmap, |
665 FXDIB_Channel srcChannel) { | 664 FXDIB_Channel srcChannel) { |
666 if (!m_pBuffer) | 665 if (!m_pBuffer) |
667 return false; | 666 return false; |
(...skipping 674 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1342 } | 1341 } |
1343 return true; | 1342 return true; |
1344 } | 1343 } |
1345 | 1344 |
1346 CFX_DIBitmap* CFX_DIBSource::FlipImage(bool bXFlip, bool bYFlip) const { | 1345 CFX_DIBitmap* CFX_DIBSource::FlipImage(bool bXFlip, bool bYFlip) const { |
1347 CFX_DIBitmap* pFlipped = new CFX_DIBitmap; | 1346 CFX_DIBitmap* pFlipped = new CFX_DIBitmap; |
1348 if (!pFlipped->Create(m_Width, m_Height, GetFormat())) { | 1347 if (!pFlipped->Create(m_Width, m_Height, GetFormat())) { |
1349 delete pFlipped; | 1348 delete pFlipped; |
1350 return nullptr; | 1349 return nullptr; |
1351 } | 1350 } |
1352 pFlipped->CopyPalette(m_pPalette.get()); | 1351 pFlipped->SetPalette(m_pPalette.get()); |
1353 uint8_t* pDestBuffer = pFlipped->GetBuffer(); | 1352 uint8_t* pDestBuffer = pFlipped->GetBuffer(); |
1354 int Bpp = m_bpp / 8; | 1353 int Bpp = m_bpp / 8; |
1355 for (int row = 0; row < m_Height; row++) { | 1354 for (int row = 0; row < m_Height; row++) { |
1356 const uint8_t* src_scan = GetScanline(row); | 1355 const uint8_t* src_scan = GetScanline(row); |
1357 uint8_t* dest_scan = | 1356 uint8_t* dest_scan = |
1358 pDestBuffer + m_Pitch * (bYFlip ? (m_Height - row - 1) : row); | 1357 pDestBuffer + m_Pitch * (bYFlip ? (m_Height - row - 1) : row); |
1359 if (!bXFlip) { | 1358 if (!bXFlip) { |
1360 FXSYS_memcpy(dest_scan, src_scan, m_Pitch); | 1359 FXSYS_memcpy(dest_scan, src_scan, m_Pitch); |
1361 continue; | 1360 continue; |
1362 } | 1361 } |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1416 } | 1415 } |
1417 | 1416 |
1418 CFX_DIBExtractor::CFX_DIBExtractor(const CFX_DIBSource* pSrc) { | 1417 CFX_DIBExtractor::CFX_DIBExtractor(const CFX_DIBSource* pSrc) { |
1419 if (pSrc->GetBuffer()) { | 1418 if (pSrc->GetBuffer()) { |
1420 m_pBitmap = pdfium::MakeUnique<CFX_DIBitmap>(); | 1419 m_pBitmap = pdfium::MakeUnique<CFX_DIBitmap>(); |
1421 if (!m_pBitmap->Create(pSrc->GetWidth(), pSrc->GetHeight(), | 1420 if (!m_pBitmap->Create(pSrc->GetWidth(), pSrc->GetHeight(), |
1422 pSrc->GetFormat(), pSrc->GetBuffer())) { | 1421 pSrc->GetFormat(), pSrc->GetBuffer())) { |
1423 m_pBitmap.reset(); | 1422 m_pBitmap.reset(); |
1424 return; | 1423 return; |
1425 } | 1424 } |
1426 m_pBitmap->CopyPalette(pSrc->GetPalette()); | 1425 m_pBitmap->SetPalette(pSrc->GetPalette()); |
1427 m_pBitmap->CopyAlphaMask(pSrc->m_pAlphaMask); | 1426 m_pBitmap->SetAlphaMask(pSrc->m_pAlphaMask); |
1428 } else { | 1427 } else { |
1429 m_pBitmap = pSrc->Clone(); | 1428 m_pBitmap = pSrc->Clone(); |
1430 } | 1429 } |
1431 } | 1430 } |
1432 | 1431 |
1433 CFX_DIBExtractor::~CFX_DIBExtractor() {} | 1432 CFX_DIBExtractor::~CFX_DIBExtractor() {} |
1434 | 1433 |
1435 CFX_FilteredDIB::CFX_FilteredDIB() : m_pSrc(nullptr) {} | 1434 CFX_FilteredDIB::CFX_FilteredDIB() : m_pSrc(nullptr) {} |
1436 | 1435 |
1437 CFX_FilteredDIB::~CFX_FilteredDIB() { | 1436 CFX_FilteredDIB::~CFX_FilteredDIB() { |
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1635 bool CFX_BitmapStorer::SetInfo(int width, | 1634 bool CFX_BitmapStorer::SetInfo(int width, |
1636 int height, | 1635 int height, |
1637 FXDIB_Format src_format, | 1636 FXDIB_Format src_format, |
1638 uint32_t* pSrcPalette) { | 1637 uint32_t* pSrcPalette) { |
1639 m_pBitmap = pdfium::MakeUnique<CFX_DIBitmap>(); | 1638 m_pBitmap = pdfium::MakeUnique<CFX_DIBitmap>(); |
1640 if (!m_pBitmap->Create(width, height, src_format)) { | 1639 if (!m_pBitmap->Create(width, height, src_format)) { |
1641 m_pBitmap.reset(); | 1640 m_pBitmap.reset(); |
1642 return false; | 1641 return false; |
1643 } | 1642 } |
1644 if (pSrcPalette) | 1643 if (pSrcPalette) |
1645 m_pBitmap->CopyPalette(pSrcPalette); | 1644 m_pBitmap->SetPalette(pSrcPalette); |
1646 return true; | 1645 return true; |
1647 } | 1646 } |
OLD | NEW |