1 // Copyright 2014 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
7 #include "../../../include/fxge/fx_dib.h"
8 #include "../../../include/fxge/fx_ge.h"
9 #include "../../../include/fxcodec/fx_codec.h"
12 FX_BOOL ConvertBuffer(FXDIB_Format dest_format, FX_LPBYTE dest_buf, int dest_pitch, int width, int height,
13 const CFX_DIBSource* pSrcBitmap, int src_left, int src_top, FX_DWORD*& pal, void* pIccTransform);
14 void CmykDecode(FX_DWORD cmyk, int& c, int& m, int& y, int& k)
16 c = FXSYS_GetCValue(cmyk);
17 m = FXSYS_GetMValue(cmyk);
18 y = FXSYS_GetYValue(cmyk);
19 k = FXSYS_GetKValue(cmyk);
21 void ArgbDecode(FX_DWORD argb, int& a, int& r, int& g, int& b)
28 void ArgbDecode(FX_DWORD argb, int& a, FX_COLORREF& rgb)
31 rgb = FXSYS_RGB(FXARGB_R(argb), FXARGB_G(argb), FXARGB_B(argb));
33 FX_DWORD ArgbEncode(int a, FX_COLORREF rgb)
35 return FXARGB_MAKE(a, FXSYS_GetRValue(rgb), FXSYS_GetGValue(rgb), FXSYS_GetBValue(rgb));
37 CFX_DIBSource::CFX_DIBSource()
41 m_Width = m_Height = 0;
46 CFX_DIBSource::~CFX_DIBSource()
55 CFX_DIBitmap::CFX_DIBitmap()
61 #define _MAX_OOM_LIMIT_ 12000000
62 FX_BOOL CFX_DIBitmap::Create(int width, int height, FXDIB_Format format, FX_LPBYTE pBuffer, int pitch)
65 m_bpp = (FX_BYTE)format;
66 m_AlphaFlag = (FX_BYTE)(format >> 8);
67 m_Width = m_Height = m_Pitch = 0;
68 if (width <= 0 || height <= 0 || pitch < 0) {
71 if ((INT_MAX - 31) / width < (format & 0xff)) {
75 pitch = (width * (format & 0xff) + 31) / 32 * 4;
77 if ((1 << 30) / pitch < height) {
84 int size = pitch * height + 4;
85 int oomlimit = _MAX_OOM_LIMIT_;
86 if (oomlimit >= 0 && size >= oomlimit) {
87 m_pBuffer = FX_AllocNL(FX_BYTE, size);
89 m_pBuffer = FX_Alloc(FX_BYTE, size);
91 if (m_pBuffer == NULL) {
94 FXSYS_memset32(m_pBuffer, 0, sizeof (FX_BYTE) * size);
99 if (HasAlpha() && format != FXDIB_Argb) {
101 ret = BuildAlphaMask();
103 if (!m_bExtBuf && m_pBuffer) {
106 m_Width = m_Height = m_Pitch = 0;
113 FX_BOOL CFX_DIBitmap::Copy(const CFX_DIBSource* pSrc)
118 if (!Create(pSrc->GetWidth(), pSrc->GetHeight(), pSrc->GetFormat())) {
121 CopyPalette(pSrc->GetPalette());
122 CopyAlphaMask(pSrc->m_pAlphaMask);
123 for (int row = 0; row < pSrc->GetHeight(); row ++) {
124 FXSYS_memcpy32(m_pBuffer + row * m_Pitch, pSrc->GetScanline(row), m_Pitch);
128 CFX_DIBitmap::~CFX_DIBitmap()
130 if (m_pBuffer && !m_bExtBuf) {
135 void CFX_DIBitmap::TakeOver(CFX_DIBitmap* pSrcBitmap)
137 if (m_pBuffer && !m_bExtBuf) {
146 m_pBuffer = pSrcBitmap->m_pBuffer;
147 m_pPalette = pSrcBitmap->m_pPalette;
148 m_pAlphaMask = pSrcBitmap->m_pAlphaMask;
149 pSrcBitmap->m_pBuffer = NULL;
150 pSrcBitmap->m_pPalette = NULL;
151 pSrcBitmap->m_pAlphaMask = NULL;
152 m_bpp = pSrcBitmap->m_bpp;
153 m_bExtBuf = pSrcBitmap->m_bExtBuf;
154 m_AlphaFlag = pSrcBitmap->m_AlphaFlag;
155 m_Width = pSrcBitmap->m_Width;
156 m_Height = pSrcBitmap->m_Height;
157 m_Pitch = pSrcBitmap->m_Pitch;
159 CFX_DIBitmap* CFX_DIBSource::Clone(const FX_RECT* pClip) const
161 FX_RECT rect(0, 0, m_Width, m_Height);
163 rect.Intersect(*pClip);
164 if (rect.IsEmpty()) {
168 CFX_DIBitmap* pNewBitmap = FX_NEW CFX_DIBitmap;
172 if (!pNewBitmap->Create(rect.Width(), rect.Height(), GetFormat())) {
176 pNewBitmap->CopyPalette(m_pPalette);
177 pNewBitmap->CopyAlphaMask(m_pAlphaMask, pClip);
178 if (GetBPP() == 1 && rect.left % 8 != 0) {
179 int left_shift = rect.left % 32;
180 int right_shift = 32 - left_shift;
181 int dword_count = pNewBitmap->m_Pitch / 4;
182 for (int row = rect.top; row < rect.bottom; row ++) {
183 FX_DWORD* src_scan = (FX_DWORD*)GetScanline(row) + rect.left / 32;
184 FX_DWORD* dest_scan = (FX_DWORD*)pNewBitmap->GetScanline(row - rect.top);
185 for (int i = 0; i < dword_count; i ++) {
186 dest_scan[i] = (src_scan[i] << left_shift) | (src_scan[i + 1] >> right_shift);
190 int copy_len = (pNewBitmap->GetWidth() * pNewBitmap->GetBPP() + 7) / 8;
191 if (m_Pitch < (FX_DWORD)copy_len) {
194 for (int row = rect.top; row < rect.bottom; row ++) {
195 FX_LPCBYTE src_scan = GetScanline(row) + rect.left * m_bpp / 8;
196 FX_LPBYTE dest_scan = (FX_LPBYTE)pNewBitmap->GetScanline(row - rect.top);
197 FXSYS_memcpy32(dest_scan, src_scan, copy_len);
202 void CFX_DIBSource::BuildPalette()
208 m_pPalette = FX_Alloc(FX_DWORD, 2);
213 m_pPalette[0] = 0xff;
216 m_pPalette[0] = 0xff000000;
217 m_pPalette[1] = 0xffffffff;
219 } else if (GetBPP() == 8) {
220 m_pPalette = FX_Alloc(FX_DWORD, 256);
225 for (int i = 0; i < 256; i ++) {
226 m_pPalette[i] = 0xff - i;
229 for (int i = 0; i < 256; i ++) {
230 m_pPalette[i] = 0xff000000 | (i * 0x10101);
235 FX_BOOL CFX_DIBSource::BuildAlphaMask()
240 m_pAlphaMask = FX_NEW CFX_DIBitmap;
244 if (!m_pAlphaMask->Create(m_Width, m_Height, FXDIB_8bppMask)) {
249 FXSYS_memset8(m_pAlphaMask->GetBuffer(), 0xff, m_pAlphaMask->GetHeight()*m_pAlphaMask->GetPitch());
252 FX_DWORD CFX_DIBSource::GetPaletteEntry(int index) const
254 ASSERT((GetBPP() == 1 || GetBPP() == 8) && !IsAlphaMask());
256 return m_pPalette[index];
260 return index ? 0 : 0xff;
265 return index ? 0xffffffff : 0xff000000;
267 return index * 0x10101 | 0xff000000;
269 void CFX_DIBSource::SetPaletteEntry(int index, FX_DWORD color)
271 ASSERT((GetBPP() == 1 || GetBPP() == 8) && !IsAlphaMask());
272 if (m_pPalette == NULL) {
275 m_pPalette[index] = color;
277 int CFX_DIBSource::FindPalette(FX_DWORD color) const
279 ASSERT((GetBPP() == 1 || GetBPP() == 8) && !IsAlphaMask());
280 if (m_pPalette == NULL) {
283 return ((FX_BYTE)color == 0xff) ? 0 : 1;
285 return 0xff - (FX_BYTE)color;
288 return ((FX_BYTE)color == 0xff) ? 1 : 0;
290 return (FX_BYTE)color;
292 int palsize = (1 << GetBPP());
293 for (int i = 0; i < palsize; i ++)
294 if (m_pPalette[i] == color) {
299 void CFX_DIBitmap::Clear(FX_DWORD color)
301 if (m_pBuffer == NULL) {
304 switch (GetFormat()) {
306 FXSYS_memset8(m_pBuffer, (color & 0xff000000) ? 0xff : 0, m_Pitch * m_Height);
308 case FXDIB_1bppRgb: {
309 int index = FindPalette(color);
310 FXSYS_memset8(m_pBuffer, index ? 0xff : 0, m_Pitch * m_Height);
314 FXSYS_memset8(m_pBuffer, color >> 24, m_Pitch * m_Height);
316 case FXDIB_8bppRgb: {
317 int index = FindPalette(color);
318 FXSYS_memset8(m_pBuffer, index, m_Pitch * m_Height);
324 ArgbDecode(color, a, r, g, b);
325 if (r == g && g == b) {
326 FXSYS_memset8(m_pBuffer, r, m_Pitch * m_Height);
329 for (int col = 0; col < m_Width; col ++) {
330 m_pBuffer[byte_pos++] = b;
331 m_pBuffer[byte_pos++] = g;
332 m_pBuffer[byte_pos++] = r;
334 for (int row = 1; row < m_Height; row ++) {
335 FXSYS_memcpy32(m_pBuffer + row * m_Pitch, m_pBuffer, m_Pitch);
342 color = IsCmykImage() ? FXCMYK_TODIB(color) : FXARGB_TODIB(color);
343 for (int i = 0; i < m_Width; i ++) {
344 ((FX_DWORD*)m_pBuffer)[i] = color;
346 for (int row = 1; row < m_Height; row ++) {
347 FXSYS_memcpy32(m_pBuffer + row * m_Pitch, m_pBuffer, m_Pitch);
355 void CFX_DIBSource::GetOverlapRect(int& dest_left, int& dest_top, int& width, int& height,
356 int src_width, int src_height, int& src_left, int& src_top,
357 const CFX_ClipRgn* pClipRgn)
359 if (width == 0 || height == 0) {
362 ASSERT(width > 0 && height > 0);
363 if (dest_left > m_Width || dest_top > m_Height) {
368 int x_offset = dest_left - src_left;
369 int y_offset = dest_top - src_top;
370 FX_RECT src_rect(src_left, src_top, src_left + width, src_top + height);
371 FX_RECT src_bound(0, 0, src_width, src_height);
372 src_rect.Intersect(src_bound);
373 FX_RECT dest_rect(src_rect.left + x_offset, src_rect.top + y_offset,
374 src_rect.right + x_offset, src_rect.bottom + y_offset);
375 FX_RECT dest_bound(0, 0, m_Width, m_Height);
376 dest_rect.Intersect(dest_bound);
378 dest_rect.Intersect(pClipRgn->GetBox());
380 dest_left = dest_rect.left;
381 dest_top = dest_rect.top;
382 src_left = dest_left - x_offset;
383 src_top = dest_top - y_offset;
384 width = dest_rect.right - dest_rect.left;
385 height = dest_rect.bottom - dest_rect.top;
387 FX_BOOL CFX_DIBitmap::TransferBitmap(int dest_left, int dest_top, int width, int height,
388 const CFX_DIBSource* pSrcBitmap, int src_left, int src_top, void* pIccTransform)
390 if (m_pBuffer == NULL) {
393 GetOverlapRect(dest_left, dest_top, width, height, pSrcBitmap->GetWidth(), pSrcBitmap->GetHeight(), src_left, src_top, NULL);
394 if (width == 0 || height == 0) {
397 FXDIB_Format dest_format = GetFormat();
398 FXDIB_Format src_format = pSrcBitmap->GetFormat();
399 if (dest_format == src_format && pIccTransform == NULL) {
401 for (int row = 0; row < height; row ++) {
402 FX_LPBYTE dest_scan = m_pBuffer + (dest_top + row) * m_Pitch;
403 FX_LPCBYTE src_scan = pSrcBitmap->GetScanline(src_top + row);
404 for (int col = 0; col < width; col ++) {
405 if (src_scan[(src_left + col) / 8] & (1 << (7 - (src_left + col) % 8))) {
406 dest_scan[(dest_left + col) / 8] |= 1 << (7 - (dest_left + col) % 8);
408 dest_scan[(dest_left + col) / 8] &= ~(1 << (7 - (dest_left + col) % 8));
413 int Bpp = GetBPP() / 8;
414 for (int row = 0; row < height; row ++) {
415 FX_LPBYTE dest_scan = m_pBuffer + (dest_top + row) * m_Pitch + dest_left * Bpp;
416 FX_LPCBYTE src_scan = pSrcBitmap->GetScanline(src_top + row) + src_left * Bpp;
417 FXSYS_memcpy32(dest_scan, src_scan, width * Bpp);
425 dest_format = FXDIB_8bppMask;
427 FX_LPBYTE dest_buf = m_pBuffer + dest_top * m_Pitch + dest_left * GetBPP() / 8;
428 FX_DWORD* d_plt = NULL;
429 if(!ConvertBuffer(dest_format, dest_buf, m_Pitch, width, height, pSrcBitmap, src_left, src_top, d_plt, pIccTransform)) {
435 #ifndef _FPDFAPI_MINI_
436 FX_BOOL CFX_DIBitmap::TransferMask(int dest_left, int dest_top, int width, int height,
437 const CFX_DIBSource* pMask, FX_DWORD color, int src_left, int src_top, int alpha_flag, void* pIccTransform)
439 if (m_pBuffer == NULL) {
442 ASSERT(HasAlpha() && (m_bpp >= 24));
443 ASSERT(pMask->IsAlphaMask());
444 if (!HasAlpha() || !pMask->IsAlphaMask() || m_bpp < 24) {
447 GetOverlapRect(dest_left, dest_top, width, height, pMask->GetWidth(), pMask->GetHeight(), src_left, src_top, NULL);
448 if (width == 0 || height == 0) {
451 int src_pitch = pMask->GetPitch();
452 int src_bpp = pMask->GetBPP();
455 if (alpha_flag >> 8) {
456 alpha = alpha_flag & 0xff;
457 dst_color = FXCMYK_TODIB(color);
459 alpha = FXARGB_A(color);
460 dst_color = FXARGB_TODIB(color);
462 FX_LPBYTE color_p = (FX_LPBYTE)&dst_color;
463 if (pIccTransform && CFX_GEModule::Get()->GetCodecModule() && CFX_GEModule::Get()->GetCodecModule()->GetIccModule()) {
464 ICodec_IccModule* pIccModule = CFX_GEModule::Get()->GetCodecModule()->GetIccModule();
465 pIccModule->TranslateScanline(pIccTransform, color_p, color_p, 1);
467 if (alpha_flag >> 8 && !IsCmykImage())
468 AdobeCMYK_to_sRGB1(FXSYS_GetCValue(color), FXSYS_GetMValue(color), FXSYS_GetYValue(color), FXSYS_GetKValue(color),
469 color_p[2], color_p[1], color_p[0]);
470 else if (!(alpha_flag >> 8) && IsCmykImage()) {
475 color_p[3] = (FX_BYTE)alpha;
477 if (GetFormat() == FXDIB_Argb) {
478 for (int row = 0; row < height; row ++) {
479 FX_DWORD* dest_pos = (FX_DWORD*)(m_pBuffer + (dest_top + row) * m_Pitch + dest_left * 4);
480 FX_LPCBYTE src_scan = pMask->GetScanline(src_top + row);
482 for (int col = 0; col < width; col ++) {
483 int src_bitpos = src_left + col;
484 if (src_scan[src_bitpos / 8] & (1 << (7 - src_bitpos % 8))) {
485 *dest_pos = dst_color;
492 src_scan += src_left;
493 dst_color = FXARGB_TODIB(dst_color);
494 dst_color &= 0xffffff;
495 for (int col = 0; col < width; col ++) {
496 FXARGB_SETDIB(dest_pos++, dst_color | ((alpha * (*src_scan++) / 255) << 24));
501 int comps = m_bpp / 8;
502 for (int row = 0; row < height; row ++) {
503 FX_LPBYTE dest_color_pos = m_pBuffer + (dest_top + row) * m_Pitch + dest_left * comps;
504 FX_LPBYTE dest_alpha_pos = (FX_LPBYTE)m_pAlphaMask->GetScanline(dest_top + row) + dest_left;
505 FX_LPCBYTE src_scan = pMask->GetScanline(src_top + row);
507 for (int col = 0; col < width; col ++) {
508 int src_bitpos = src_left + col;
509 if (src_scan[src_bitpos / 8] & (1 << (7 - src_bitpos % 8))) {
510 FXSYS_memcpy32(dest_color_pos, color_p, comps);
511 *dest_alpha_pos = 0xff;
513 FXSYS_memset32(dest_color_pos, 0, comps);
516 dest_color_pos += comps;
520 src_scan += src_left;
521 for (int col = 0; col < width; col ++) {
522 FXSYS_memcpy32(dest_color_pos, color_p, comps);
523 dest_color_pos += comps;
524 *dest_alpha_pos++ = (alpha * (*src_scan++) / 255);
532 void CFX_DIBSource::CopyPalette(const FX_DWORD* pSrc, FX_DWORD size)
534 if (pSrc == NULL || GetBPP() > 8) {
540 FX_DWORD pal_size = 1 << GetBPP();
541 if (m_pPalette == NULL) {
542 m_pPalette = FX_Alloc(FX_DWORD, pal_size);
547 if (pal_size > size) {
550 FXSYS_memcpy32(m_pPalette, pSrc, pal_size * sizeof(FX_DWORD));
553 void CFX_DIBSource::GetPalette(FX_DWORD* pal, int alpha) const
555 ASSERT(GetBPP() <= 8 && !IsCmykImage());
557 pal[0] = ((m_pPalette ? m_pPalette[0] : 0xff000000) & 0xffffff) | (alpha << 24);
558 pal[1] = ((m_pPalette ? m_pPalette[1] : 0xffffffff) & 0xffffff) | (alpha << 24);
562 for (int i = 0; i < 256; i ++) {
563 pal[i] = (m_pPalette[i] & 0x00ffffff) | (alpha << 24);
566 for (int i = 0; i < 256; i ++) {
567 pal[i] = (i * 0x10101) | (alpha << 24);
571 CFX_DIBitmap* CFX_DIBSource::GetAlphaMask(const FX_RECT* pClip) const
573 ASSERT(GetFormat() == FXDIB_Argb);
574 FX_RECT rect(0, 0, m_Width, m_Height);
576 rect.Intersect(*pClip);
577 if (rect.IsEmpty()) {
581 CFX_DIBitmap* pMask = FX_NEW CFX_DIBitmap;
585 if (!pMask->Create(rect.Width(), rect.Height(), FXDIB_8bppMask)) {
589 for (int row = rect.top; row < rect.bottom; row ++) {
590 FX_LPCBYTE src_scan = GetScanline(row) + rect.left * 4 + 3;
591 FX_LPBYTE dest_scan = (FX_LPBYTE)pMask->GetScanline(row - rect.top);
592 for (int col = rect.left; col < rect.right; col ++) {
593 *dest_scan ++ = *src_scan;
599 FX_BOOL CFX_DIBSource::CopyAlphaMask(const CFX_DIBSource* pAlphaMask, const FX_RECT* pClip)
601 if (!HasAlpha() || GetFormat() == FXDIB_Argb) {
605 FX_RECT rect(0, 0, pAlphaMask->m_Width, pAlphaMask->m_Height);
607 rect.Intersect(*pClip);
608 if (rect.IsEmpty() || rect.Width() != m_Width || rect.Height() != m_Height) {
612 if (pAlphaMask->m_Width != m_Width || pAlphaMask->m_Height != m_Height) {
616 for (int row = 0; row < m_Height; row ++)
617 FXSYS_memcpy32((void*)m_pAlphaMask->GetScanline(row),
618 pAlphaMask->GetScanline(row + rect.top) + rect.left, m_pAlphaMask->m_Pitch);
620 m_pAlphaMask->Clear(0xff000000);
624 const int g_ChannelOffset[] = {0, 2, 1, 0, 0, 1, 2, 3, 3};
625 FX_BOOL CFX_DIBitmap::LoadChannel(FXDIB_Channel destChannel, const CFX_DIBSource* pSrcBitmap, FXDIB_Channel srcChannel)
627 if (m_pBuffer == NULL) {
630 CFX_DIBSource* pSrcClone = (CFX_DIBSource*)pSrcBitmap;
631 CFX_DIBitmap* pDst = this;
632 int destOffset, srcOffset;
633 if (srcChannel == FXDIB_Alpha) {
634 if (!pSrcBitmap->HasAlpha() && !pSrcBitmap->IsAlphaMask()) {
637 if (pSrcBitmap->GetBPP() == 1) {
638 pSrcClone = pSrcBitmap->CloneConvert(FXDIB_8bppMask);
639 if (pSrcClone == NULL) {
643 if(pSrcBitmap->GetFormat() == FXDIB_Argb) {
649 if (pSrcBitmap->IsAlphaMask()) {
652 if (pSrcBitmap->GetBPP() < 24) {
653 if (pSrcBitmap->IsCmykImage()) {
654 pSrcClone = pSrcBitmap->CloneConvert((FXDIB_Format)((pSrcBitmap->GetFormat() & 0xff00) | 0x20));
656 pSrcClone = pSrcBitmap->CloneConvert((FXDIB_Format)((pSrcBitmap->GetFormat() & 0xff00) | 0x18));
658 if (pSrcClone == NULL) {
662 srcOffset = g_ChannelOffset[srcChannel];
664 if (destChannel == FXDIB_Alpha) {
666 if(!ConvertFormat(FXDIB_8bppMask)) {
667 if (pSrcClone != pSrcBitmap) {
675 if(!ConvertFormat(IsCmykImage() ? FXDIB_Cmyka : FXDIB_Argb)) {
676 if (pSrcClone != pSrcBitmap) {
681 if (GetFormat() == FXDIB_Argb) {
687 if (pSrcClone != pSrcBitmap) {
694 if (!ConvertFormat(IsCmykImage() ? FXDIB_Cmyka : FXDIB_Argb)) {
695 if (pSrcClone != pSrcBitmap) {
701 #if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_
702 if (!ConvertFormat(IsCmykImage() ? FXDIB_Cmyk : FXDIB_Rgb32)) {
704 if (!ConvertFormat(IsCmykImage() ? FXDIB_Cmyk : FXDIB_Rgb)) {
706 if (pSrcClone != pSrcBitmap) {
712 destOffset = g_ChannelOffset[destChannel];
714 if (srcChannel == FXDIB_Alpha && pSrcClone->m_pAlphaMask) {
715 CFX_DIBitmap* pAlphaMask = pSrcClone->m_pAlphaMask;
716 if (pSrcClone->GetWidth() != m_Width || pSrcClone->GetHeight() != m_Height) {
718 pAlphaMask = pAlphaMask->StretchTo(m_Width, m_Height);
719 if (pAlphaMask == NULL) {
720 if (pSrcClone != pSrcBitmap) {
727 if (pSrcClone != pSrcBitmap) {
728 pSrcClone->m_pAlphaMask = NULL;
731 pSrcClone = pAlphaMask;
733 } else if (pSrcClone->GetWidth() != m_Width || pSrcClone->GetHeight() != m_Height) {
734 CFX_DIBitmap* pSrcMatched = pSrcClone->StretchTo(m_Width, m_Height);
735 if (pSrcClone != pSrcBitmap) {
738 if (pSrcMatched == NULL) {
741 pSrcClone = pSrcMatched;
743 if (destChannel == FXDIB_Alpha && m_pAlphaMask) {
747 int srcBytes = pSrcClone->GetBPP() / 8;
748 int destBytes = pDst->GetBPP() / 8;
749 for (int row = 0; row < m_Height; row ++) {
750 FX_LPBYTE dest_pos = (FX_LPBYTE)pDst->GetScanline(row) + destOffset;
751 FX_LPCBYTE src_pos = pSrcClone->GetScanline(row) + srcOffset;
752 for (int col = 0; col < m_Width; col ++) {
753 *dest_pos = *src_pos;
754 dest_pos += destBytes;
758 if (pSrcClone != pSrcBitmap && pSrcClone != pSrcBitmap->m_pAlphaMask) {
763 FX_BOOL CFX_DIBitmap::LoadChannel(FXDIB_Channel destChannel, int value)
765 if (m_pBuffer == NULL) {
769 if (destChannel == FXDIB_Alpha) {
771 if(!ConvertFormat(FXDIB_8bppMask)) {
777 if(!ConvertFormat(IsCmykImage() ? FXDIB_Cmyka : FXDIB_Argb)) {
780 if (GetFormat() == FXDIB_Argb) {
790 if (!ConvertFormat(IsCmykImage() ? FXDIB_Cmyka : FXDIB_Argb)) {
794 #if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_
795 if (!ConvertFormat(IsCmykImage() ? FXDIB_Cmyk : FXDIB_Rgb)) {
799 if (!ConvertFormat(IsCmykImage() ? FXDIB_Cmyk : FXDIB_Rgb32)) {
804 destOffset = g_ChannelOffset[destChannel];
806 int Bpp = GetBPP() / 8;
808 FXSYS_memset8(m_pBuffer, value, m_Height * m_Pitch);
811 if (destChannel == FXDIB_Alpha && m_pAlphaMask) {
812 FXSYS_memset8(m_pAlphaMask->GetBuffer(), value, m_pAlphaMask->GetHeight()*m_pAlphaMask->GetPitch());
815 for (int row = 0; row < m_Height; row ++) {
816 FX_LPBYTE scan_line = m_pBuffer + row * m_Pitch + destOffset;
817 for (int col = 0; col < m_Width; col ++) {
824 FX_BOOL CFX_DIBitmap::MultiplyAlpha(const CFX_DIBSource* pSrcBitmap)
826 if (m_pBuffer == NULL) {
829 ASSERT(pSrcBitmap->IsAlphaMask());
830 if (!pSrcBitmap->IsAlphaMask()) {
833 if (!IsAlphaMask() && !HasAlpha()) {
834 return LoadChannel(FXDIB_Alpha, pSrcBitmap, FXDIB_Alpha);
836 CFX_DIBitmap* pSrcClone = (CFX_DIBitmap*)pSrcBitmap;
837 if (pSrcBitmap->GetWidth() != m_Width || pSrcBitmap->GetHeight() != m_Height) {
838 pSrcClone = pSrcBitmap->StretchTo(m_Width, m_Height);
839 ASSERT(pSrcClone != NULL);
840 if (pSrcClone == NULL) {
845 if(!ConvertFormat(FXDIB_8bppMask)) {
846 if (pSrcClone != pSrcBitmap) {
851 for (int row = 0; row < m_Height; row ++) {
852 FX_LPBYTE dest_scan = m_pBuffer + m_Pitch * row;
853 FX_LPBYTE src_scan = pSrcClone->m_pBuffer + pSrcClone->m_Pitch * row;
854 if (pSrcClone->GetBPP() == 1) {
855 for (int col = 0; col < m_Width; col ++) {
856 if (!((1 << (7 - col % 8)) & src_scan[col / 8])) {
861 for (int col = 0; col < m_Width; col ++) {
862 *dest_scan = (*dest_scan) * src_scan[col] / 255;
868 if(GetFormat() == FXDIB_Argb) {
869 if (pSrcClone->GetBPP() == 1) {
870 if (pSrcClone != pSrcBitmap) {
875 for (int row = 0; row < m_Height; row ++) {
876 FX_LPBYTE dest_scan = m_pBuffer + m_Pitch * row + 3;
877 FX_LPBYTE src_scan = pSrcClone->m_pBuffer + pSrcClone->m_Pitch * row;
878 for (int col = 0; col < m_Width; col ++) {
879 *dest_scan = (*dest_scan) * src_scan[col] / 255;
884 m_pAlphaMask->MultiplyAlpha(pSrcClone);
887 if (pSrcClone != pSrcBitmap) {
892 FX_BOOL CFX_DIBitmap::GetGrayData(void* pIccTransform)
894 if (m_pBuffer == NULL) {
897 switch (GetFormat()) {
898 case FXDIB_1bppRgb: {
899 if (m_pPalette == NULL) {
903 for (int i = 0; i < 2; i ++) {
904 int r = (FX_BYTE)(m_pPalette[i] >> 16);
905 int g = (FX_BYTE)(m_pPalette[i] >> 8);
906 int b = (FX_BYTE)m_pPalette[i];
907 gray[i] = (FX_BYTE)FXRGB2GRAY(r, g, b);
909 CFX_DIBitmap* pMask = FX_NEW CFX_DIBitmap;
913 if (!pMask->Create(m_Width, m_Height, FXDIB_8bppMask)) {
917 FXSYS_memset8(pMask->GetBuffer(), gray[0], pMask->GetPitch() * m_Height);
918 for (int row = 0; row < m_Height; row ++) {
919 FX_LPBYTE src_pos = m_pBuffer + row * m_Pitch;
920 FX_LPBYTE dest_pos = (FX_LPBYTE)pMask->GetScanline(row);
921 for (int col = 0; col < m_Width; col ++) {
922 if (src_pos[col / 8] & (1 << (7 - col % 8))) {
932 case FXDIB_8bppRgb: {
933 if (m_pPalette == NULL) {
937 for (int i = 0; i < 256; i ++) {
938 int r = (FX_BYTE)(m_pPalette[i] >> 16);
939 int g = (FX_BYTE)(m_pPalette[i] >> 8);
940 int b = (FX_BYTE)m_pPalette[i];
941 gray[i] = (FX_BYTE)FXRGB2GRAY(r, g, b);
943 CFX_DIBitmap* pMask = FX_NEW CFX_DIBitmap;
947 if (!pMask->Create(m_Width, m_Height, FXDIB_8bppMask)) {
951 for (int row = 0; row < m_Height; row ++) {
952 FX_LPBYTE dest_pos = pMask->GetBuffer() + row * pMask->GetPitch();
953 FX_LPBYTE src_pos = m_pBuffer + row * m_Pitch;
954 for (int col = 0; col < m_Width; col ++) {
955 *dest_pos ++ = gray[*src_pos ++];
963 CFX_DIBitmap* pMask = FX_NEW CFX_DIBitmap;
967 if (!pMask->Create(m_Width, m_Height, FXDIB_8bppMask)) {
971 for (int row = 0; row < m_Height; row ++) {
972 FX_LPBYTE src_pos = m_pBuffer + row * m_Pitch;
973 FX_LPBYTE dest_pos = pMask->GetBuffer() + row * pMask->GetPitch();
974 for (int col = 0; col < m_Width; col ++) {
975 *dest_pos ++ = FXRGB2GRAY(src_pos[2], src_pos[1], *src_pos);
984 CFX_DIBitmap* pMask = FX_NEW CFX_DIBitmap;
988 if (!pMask->Create(m_Width, m_Height, FXDIB_8bppMask)) {
992 for (int row = 0; row < m_Height; row ++) {
993 FX_LPBYTE src_pos = m_pBuffer + row * m_Pitch;
994 FX_LPBYTE dest_pos = pMask->GetBuffer() + row * pMask->GetPitch();
995 for (int col = 0; col < m_Width; col ++) {
996 *dest_pos ++ = FXRGB2GRAY(src_pos[2], src_pos[1], *src_pos);
1009 FX_BOOL CFX_DIBitmap::MultiplyAlpha(int alpha)
1011 if (m_pBuffer == NULL) {
1014 switch (GetFormat()) {
1015 case FXDIB_1bppMask:
1016 if (!ConvertFormat(FXDIB_8bppMask)) {
1019 MultiplyAlpha(alpha);
1021 case FXDIB_8bppMask: {
1022 for (int row = 0; row < m_Height; row ++) {
1023 FX_LPBYTE scan_line = m_pBuffer + row * m_Pitch;
1024 for (int col = 0; col < m_Width; col ++) {
1025 scan_line[col] = scan_line[col] * alpha / 255;
1031 for (int row = 0; row < m_Height; row ++) {
1032 FX_LPBYTE scan_line = m_pBuffer + row * m_Pitch + 3;
1033 for (int col = 0; col < m_Width; col ++) {
1034 *scan_line = (*scan_line) * alpha / 255;
1042 m_pAlphaMask->MultiplyAlpha(alpha);
1043 } else if (IsCmykImage()) {
1044 if (!ConvertFormat((FXDIB_Format)(GetFormat() | 0x0200))) {
1047 m_pAlphaMask->MultiplyAlpha(alpha);
1049 if (!ConvertFormat(FXDIB_Argb)) {
1052 MultiplyAlpha(alpha);
1058 #if !defined(_FPDFAPI_MINI_) || defined(_FXCORE_FEATURE_ALL_)
1059 FX_DWORD CFX_DIBitmap::GetPixel(int x, int y) const
1061 if (m_pBuffer == NULL) {
1064 FX_LPBYTE pos = m_pBuffer + y * m_Pitch + x * GetBPP() / 8;
1065 switch (GetFormat()) {
1066 case FXDIB_1bppMask: {
1067 if ((*pos) & (1 << (7 - x % 8))) {
1072 case FXDIB_1bppRgb: {
1073 if ((*pos) & (1 << (7 - x % 8))) {
1074 return m_pPalette ? m_pPalette[1] : 0xffffffff;
1076 return m_pPalette ? m_pPalette[0] : 0xff000000;
1080 case FXDIB_8bppMask:
1081 return (*pos) << 24;
1083 return m_pPalette ? m_pPalette[*pos] : (0xff000000 | ((*pos) * 0x10101));
1087 return FXARGB_GETDIB(pos) | 0xff000000;
1089 return FXARGB_GETDIB(pos);
1096 void CFX_DIBitmap::SetPixel(int x, int y, FX_DWORD color)
1098 if (m_pBuffer == NULL) {
1101 if (x < 0 || x >= m_Width || y < 0 || y >= m_Height) {
1104 FX_LPBYTE pos = m_pBuffer + y * m_Pitch + x * GetBPP() / 8;
1105 switch (GetFormat()) {
1106 case FXDIB_1bppMask:
1108 *pos |= 1 << (7 - x % 8);
1110 *pos &= ~(1 << (7 - x % 8));
1115 if (color == m_pPalette[1]) {
1116 *pos |= 1 << (7 - x % 8);
1118 *pos &= ~(1 << (7 - x % 8));
1121 if (color == 0xffffffff) {
1122 *pos |= 1 << (7 - x % 8);
1124 *pos &= ~(1 << (7 - x % 8));
1128 case FXDIB_8bppMask:
1129 *pos = (FX_BYTE)(color >> 24);
1131 case FXDIB_8bppRgb: {
1133 for (int i = 0; i < 256; i ++) {
1134 if (m_pPalette[i] == color) {
1141 *pos = FXRGB2GRAY(FXARGB_R(color), FXARGB_G(color), FXARGB_B(color));
1147 int alpha = FXARGB_A(color);
1148 pos[0] = (FXARGB_B(color) * alpha + pos[0] * (255 - alpha)) / 255;
1149 pos[1] = (FXARGB_G(color) * alpha + pos[1] * (255 - alpha)) / 255;
1150 pos[2] = (FXARGB_R(color) * alpha + pos[2] * (255 - alpha)) / 255;
1154 pos[0] = FXARGB_B(color);
1155 pos[1] = FXARGB_G(color);
1156 pos[2] = FXARGB_R(color);
1160 FXARGB_SETDIB(pos, color);
1166 void CFX_DIBitmap::DownSampleScanline(int line, FX_LPBYTE dest_scan, int dest_bpp,
1167 int dest_width, FX_BOOL bFlipX, int clip_left, int clip_width) const
1169 if (m_pBuffer == NULL) {
1172 int src_Bpp = m_bpp / 8;
1173 FX_LPBYTE scanline = m_pBuffer + line * m_Pitch;
1175 for (int i = 0; i < clip_width; i ++) {
1176 FX_DWORD dest_x = clip_left + i;
1177 FX_DWORD src_x = dest_x * m_Width / dest_width;
1179 src_x = m_Width - src_x - 1;
1181 #ifdef FOXIT_CHROME_BUILD
1184 dest_scan[i] = (scanline[src_x / 8] & (1 << (7 - src_x % 8))) ? 255 : 0;
1186 } else if (src_Bpp == 1) {
1187 for (int i = 0; i < clip_width; i ++) {
1188 FX_DWORD dest_x = clip_left + i;
1189 FX_DWORD src_x = dest_x * m_Width / dest_width;
1191 src_x = m_Width - src_x - 1;
1193 #ifdef FOXIT_CHROME_BUILD
1198 if (!IsCmykImage()) {
1200 FX_ARGB argb = m_pPalette[scanline[src_x]];
1201 dest_scan[dest_pos] = FXARGB_B(argb);
1202 dest_scan[dest_pos + 1] = FXARGB_G(argb);
1203 dest_scan[dest_pos + 2] = FXARGB_R(argb);
1206 FX_CMYK cmyk = m_pPalette[scanline[src_x]];
1207 dest_scan[dest_pos] = FXSYS_GetCValue(cmyk);
1208 dest_scan[dest_pos + 1] = FXSYS_GetMValue(cmyk);
1209 dest_scan[dest_pos + 2] = FXSYS_GetYValue(cmyk);
1210 dest_scan[dest_pos + 3] = FXSYS_GetKValue(cmyk);
1213 dest_scan[dest_pos] = scanline[src_x];
1217 for (int i = 0; i < clip_width; i ++) {
1218 FX_DWORD dest_x = clip_left + i;
1219 FX_DWORD src_x = bFlipX ? (m_Width - dest_x * m_Width / dest_width - 1) * src_Bpp : (dest_x * m_Width / dest_width) * src_Bpp;
1220 #ifdef FOXIT_CHROME_BUILD
1221 src_x %= m_Width * src_Bpp;
1223 int dest_pos = i * src_Bpp;
1224 for (int b = 0; b < src_Bpp; b ++) {
1225 dest_scan[dest_pos + b] = scanline[src_x + b];
1230 FX_BOOL CFX_DIBitmap::ConvertColorScale(FX_DWORD forecolor, FX_DWORD backcolor)
1232 ASSERT(!IsAlphaMask());
1233 if (m_pBuffer == NULL || IsAlphaMask()) {
1236 int fc, fm, fy, fk, bc, bm, by, bk;
1237 int fr, fg, fb, br, bg, bb;
1238 FX_BOOL isCmykImage = IsCmykImage();
1240 fc = FXSYS_GetCValue(forecolor);
1241 fm = FXSYS_GetMValue(forecolor);
1242 fy = FXSYS_GetYValue(forecolor);
1243 fk = FXSYS_GetKValue(forecolor);
1244 bc = FXSYS_GetCValue(backcolor);
1245 bm = FXSYS_GetMValue(backcolor);
1246 by = FXSYS_GetYValue(backcolor);
1247 bk = FXSYS_GetKValue(backcolor);
1249 fr = FXSYS_GetRValue(forecolor);
1250 fg = FXSYS_GetGValue(forecolor);
1251 fb = FXSYS_GetBValue(forecolor);
1252 br = FXSYS_GetRValue(backcolor);
1253 bg = FXSYS_GetGValue(backcolor);
1254 bb = FXSYS_GetBValue(backcolor);
1258 if (forecolor == 0xff && backcolor == 0 && m_pPalette == NULL) {
1261 } else if (forecolor == 0 && backcolor == 0xffffff && m_pPalette == NULL) {
1264 if (m_pPalette == NULL) {
1267 int size = 1 << m_bpp;
1269 for (int i = 0; i < size; i ++) {
1271 AdobeCMYK_to_sRGB1(FXSYS_GetCValue(m_pPalette[i]), FXSYS_GetMValue(m_pPalette[i]), FXSYS_GetYValue(m_pPalette[i]), FXSYS_GetKValue(m_pPalette[i]),
1273 int gray = 255 - FXRGB2GRAY(r, g, b);
1274 m_pPalette[i] = CmykEncode(bc + (fc - bc) * gray / 255, bm + (fm - bm) * gray / 255,
1275 by + (fy - by) * gray / 255, bk + (fk - bk) * gray / 255);
1278 for (int i = 0; i < size; i ++) {
1279 int gray = FXRGB2GRAY(FXARGB_R(m_pPalette[i]), FXARGB_G(m_pPalette[i]), FXARGB_B(m_pPalette[i]));
1280 m_pPalette[i] = FXARGB_MAKE(0xff, br + (fr - br) * gray / 255, bg + (fg - bg) * gray / 255,
1281 bb + (fb - bb) * gray / 255);
1286 if (forecolor == 0xff && backcolor == 0x00) {
1287 for (int row = 0; row < m_Height; row ++) {
1288 FX_LPBYTE scanline = m_pBuffer + row * m_Pitch;
1289 for (int col = 0; col < m_Width; col ++) {
1291 AdobeCMYK_to_sRGB1(scanline[0], scanline[1], scanline[2], scanline[3],
1296 *scanline ++ = 255 - FXRGB2GRAY(r, g, b);
1301 } else if (forecolor == 0 && backcolor == 0xffffff) {
1302 for (int row = 0; row < m_Height; row ++) {
1303 FX_LPBYTE scanline = m_pBuffer + row * m_Pitch;
1304 int gap = m_bpp / 8 - 2;
1305 for (int col = 0; col < m_Width; col ++) {
1306 int gray = FXRGB2GRAY(scanline[2], scanline[1], scanline[0]);
1307 *scanline ++ = gray;
1308 *scanline ++ = gray;
1316 for (int row = 0; row < m_Height; row ++) {
1317 FX_LPBYTE scanline = m_pBuffer + row * m_Pitch;
1318 for (int col = 0; col < m_Width; col ++) {
1320 AdobeCMYK_to_sRGB1(scanline[0], scanline[1], scanline[2], scanline[3],
1322 int gray = 255 - FXRGB2GRAY(r, g, b);
1323 *scanline ++ = bc + (fc - bc) * gray / 255;
1324 *scanline ++ = bm + (fm - bm) * gray / 255;
1325 *scanline ++ = by + (fy - by) * gray / 255;
1326 *scanline ++ = bk + (fk - bk) * gray / 255;
1330 for (int row = 0; row < m_Height; row ++) {
1331 FX_LPBYTE scanline = m_pBuffer + row * m_Pitch;
1332 int gap = m_bpp / 8 - 2;
1333 for (int col = 0; col < m_Width; col ++) {
1334 int gray = FXRGB2GRAY(scanline[2], scanline[1], scanline[0]);
1335 *scanline ++ = bb + (fb - bb) * gray / 255;
1336 *scanline ++ = bg + (fg - bg) * gray / 255;
1337 *scanline = br + (fr - br) * gray / 255;
1343 FX_BOOL CFX_DIBitmap::DitherFS(const FX_DWORD* pPalette, int pal_size, const FX_RECT* pRect)
1345 if (m_pBuffer == NULL) {
1348 if (m_bpp != 8 && m_pPalette != NULL && m_AlphaFlag != 0) {
1351 if (m_Width < 4 && m_Height < 4) {
1354 FX_RECT rect(0, 0, m_Width, m_Height);
1356 rect.Intersect(*pRect);
1358 FX_BYTE translate[256];
1359 for (int i = 0; i < 256; i ++) {
1361 for (int j = 0; j < pal_size; j ++) {
1362 FX_BYTE entry = (FX_BYTE)pPalette[j];
1363 int err = (int)entry - i;
1364 if (err * err < err2) {
1366 translate[i] = entry;
1370 for (int row = rect.top; row < rect.bottom; row ++) {
1371 FX_LPBYTE scan = m_pBuffer + row * m_Pitch;
1372 FX_LPBYTE next_scan = m_pBuffer + (row + 1) * m_Pitch;
1373 for (int col = rect.left; col < rect.right; col ++) {
1374 int src_pixel = scan[col];
1375 int dest_pixel = translate[src_pixel];
1376 scan[col] = (FX_BYTE)dest_pixel;
1377 int error = -dest_pixel + src_pixel;
1378 if (col < rect.right - 1) {
1379 int src = scan[col + 1];
1380 src += error * 7 / 16;
1382 scan[col + 1] = 255;
1383 } else if (src < 0) {
1386 scan[col + 1] = src;
1389 if (col < rect.right - 1 && row < rect.bottom - 1) {
1390 int src = next_scan[col + 1];
1391 src += error * 1 / 16;
1393 next_scan[col + 1] = 255;
1394 } else if (src < 0) {
1395 next_scan[col + 1] = 0;
1397 next_scan[col + 1] = src;
1400 if (row < rect.bottom - 1) {
1401 int src = next_scan[col];
1402 src += error * 5 / 16;
1404 next_scan[col] = 255;
1405 } else if (src < 0) {
1408 next_scan[col] = src;
1411 if (col > rect.left && row < rect.bottom - 1) {
1412 int src = next_scan[col - 1];
1413 src += error * 3 / 16;
1415 next_scan[col - 1] = 255;
1416 } else if (src < 0) {
1417 next_scan[col - 1] = 0;
1419 next_scan[col - 1] = src;
1426 CFX_DIBitmap* CFX_DIBSource::FlipImage(FX_BOOL bXFlip, FX_BOOL bYFlip) const
1428 CFX_DIBitmap* pFlipped = FX_NEW CFX_DIBitmap;
1432 if (!pFlipped->Create(m_Width, m_Height, GetFormat())) {
1436 pFlipped->CopyPalette(m_pPalette);
1437 FX_LPBYTE pDestBuffer = pFlipped->GetBuffer();
1438 int Bpp = m_bpp / 8;
1439 for (int row = 0; row < m_Height; row ++) {
1440 FX_LPCBYTE src_scan = GetScanline(row);
1441 FX_LPBYTE dest_scan = pDestBuffer + m_Pitch * (bYFlip ? (m_Height - row - 1) : row);
1443 FXSYS_memcpy32(dest_scan, src_scan, m_Pitch);
1447 FXSYS_memset32(dest_scan, 0, m_Pitch);
1448 for (int col = 0; col < m_Width; col ++)
1449 if (src_scan[col / 8] & (1 << (7 - col % 8))) {
1450 int dest_col = m_Width - col - 1;
1451 dest_scan[dest_col / 8] |= (1 << (7 - dest_col % 8));
1454 dest_scan += (m_Width - 1) * Bpp;
1456 for (int col = 0; col < m_Width; col ++) {
1457 *dest_scan = *src_scan;
1461 } else if (Bpp == 3) {
1462 for (int col = 0; col < m_Width; col ++) {
1463 dest_scan[0] = src_scan[0];
1464 dest_scan[1] = src_scan[1];
1465 dest_scan[2] = src_scan[2];
1471 for (int col = 0; col < m_Width; col ++) {
1472 *(FX_DWORD*)dest_scan = *(FX_DWORD*)src_scan;
1480 pDestBuffer = pFlipped->m_pAlphaMask->GetBuffer();
1481 FX_DWORD dest_pitch = pFlipped->m_pAlphaMask->GetPitch();
1482 for (int row = 0; row < m_Height; row ++) {
1483 FX_LPCBYTE src_scan = m_pAlphaMask->GetScanline(row);
1484 FX_LPBYTE dest_scan = pDestBuffer + dest_pitch * (bYFlip ? (m_Height - row - 1) : row);
1486 FXSYS_memcpy32(dest_scan, src_scan, dest_pitch);
1489 dest_scan += (m_Width - 1);
1490 for (int col = 0; col < m_Width; col ++) {
1491 *dest_scan = *src_scan;
1499 CFX_DIBExtractor::CFX_DIBExtractor(const CFX_DIBSource* pSrc)
1502 if (pSrc->GetBuffer() == NULL) {
1503 m_pBitmap = pSrc->Clone();
1505 m_pBitmap = FX_NEW CFX_DIBitmap;
1509 if (!m_pBitmap->Create(pSrc->GetWidth(), pSrc->GetHeight(), pSrc->GetFormat(), pSrc->GetBuffer())) {
1514 m_pBitmap->CopyPalette(pSrc->GetPalette());
1515 m_pBitmap->CopyAlphaMask(pSrc->m_pAlphaMask);
1518 CFX_DIBExtractor::~CFX_DIBExtractor()
1524 CFX_FilteredDIB::CFX_FilteredDIB()
1529 CFX_FilteredDIB::~CFX_FilteredDIB()
1531 if (m_pSrc && m_bAutoDropSrc) {
1535 FX_Free(m_pScanline);
1538 void CFX_FilteredDIB::LoadSrc(const CFX_DIBSource* pSrc, FX_BOOL bAutoDropSrc)
1541 m_bAutoDropSrc = bAutoDropSrc;
1542 m_Width = pSrc->GetWidth();
1543 m_Height = pSrc->GetHeight();
1544 FXDIB_Format format = GetDestFormat();
1545 m_bpp = (FX_BYTE)format;
1546 m_AlphaFlag = (FX_BYTE)(format >> 8);
1547 m_Pitch = (m_Width * (format & 0xff) + 31) / 32 * 4;
1548 m_pPalette = GetDestPalette();
1549 m_pScanline = FX_Alloc(FX_BYTE, m_Pitch);
1551 FX_LPCBYTE CFX_FilteredDIB::GetScanline(int line) const
1553 TranslateScanline(m_pScanline, m_pSrc->GetScanline(line));
1556 void CFX_FilteredDIB::DownSampleScanline(int line, FX_LPBYTE dest_scan, int dest_bpp,
1557 int dest_width, FX_BOOL bFlipX, int clip_left, int clip_width) const
1559 m_pSrc->DownSampleScanline(line, dest_scan, dest_bpp, dest_width, bFlipX, clip_left, clip_width);
1560 TranslateDownSamples(dest_scan, dest_scan, clip_width, dest_bpp);
1562 CFX_ImageRenderer::CFX_ImageRenderer()
1565 m_pTransformer = NULL;
1566 m_bRgbByteOrder = FALSE;
1567 m_BlendType = FXDIB_BLEND_NORMAL;
1569 CFX_ImageRenderer::~CFX_ImageRenderer()
1571 if (m_pTransformer) {
1572 delete m_pTransformer;
1575 extern FX_RECT _FXDIB_SwapClipBox(FX_RECT& clip, int width, int height, FX_BOOL bFlipX, FX_BOOL bFlipY);
1576 FX_BOOL CFX_ImageRenderer::Start(CFX_DIBitmap* pDevice, const CFX_ClipRgn* pClipRgn,
1577 const CFX_DIBSource* pSource, int bitmap_alpha,
1578 FX_DWORD mask_color, const CFX_AffineMatrix* pMatrix,
1579 FX_DWORD dib_flags, FX_BOOL bRgbByteOrder,
1580 int alpha_flag, void* pIccTransform, int blend_type)
1582 m_Matrix = *pMatrix;
1583 CFX_FloatRect image_rect_f = m_Matrix.GetUnitRect();
1584 FX_RECT image_rect = image_rect_f.GetOutterRect();
1585 m_ClipBox = pClipRgn ? pClipRgn->GetBox() : FX_RECT(0, 0, pDevice->GetWidth(), pDevice->GetHeight());
1586 m_ClipBox.Intersect(image_rect);
1587 if (m_ClipBox.IsEmpty()) {
1590 m_pDevice = pDevice;
1591 m_pClipRgn = pClipRgn;
1592 m_MaskColor = mask_color;
1593 m_BitmapAlpha = bitmap_alpha;
1594 m_Matrix = *pMatrix;
1595 m_Flags = dib_flags;
1596 m_AlphaFlag = alpha_flag;
1597 m_pIccTransform = pIccTransform;
1598 m_bRgbByteOrder = bRgbByteOrder;
1599 m_BlendType = blend_type;
1601 if ((FXSYS_fabs(m_Matrix.b) >= 0.5f || m_Matrix.a == 0) ||
1602 (FXSYS_fabs(m_Matrix.c) >= 0.5f || m_Matrix.d == 0) ) {
1603 if (FXSYS_fabs(m_Matrix.a) < FXSYS_fabs(m_Matrix.b) / 20 && FXSYS_fabs(m_Matrix.d) < FXSYS_fabs(m_Matrix.c) / 20 &&
1604 FXSYS_fabs(m_Matrix.a) < 0.5f && FXSYS_fabs(m_Matrix.d) < 0.5f) {
1605 int dest_width = image_rect.Width();
1606 int dest_height = image_rect.Height();
1607 FX_RECT bitmap_clip = m_ClipBox;
1608 bitmap_clip.Offset(-image_rect.left, -image_rect.top);
1609 bitmap_clip = _FXDIB_SwapClipBox(bitmap_clip, dest_width, dest_height, m_Matrix.c > 0, m_Matrix.b < 0);
1610 m_Composer.Compose(pDevice, pClipRgn, bitmap_alpha, mask_color, m_ClipBox, TRUE,
1611 m_Matrix.c > 0, m_Matrix.b < 0, m_bRgbByteOrder, alpha_flag, pIccTransform, m_BlendType);
1612 if (!m_Stretcher.Start(&m_Composer, pSource, dest_height, dest_width, bitmap_clip, dib_flags)) {
1619 m_pTransformer = FX_NEW CFX_ImageTransformer;
1620 if (!m_pTransformer) {
1623 m_pTransformer->Start(pSource, &m_Matrix, dib_flags, &m_ClipBox);
1626 int dest_width = image_rect.Width();
1627 if (m_Matrix.a < 0) {
1628 dest_width = -dest_width;
1630 int dest_height = image_rect.Height();
1631 if (m_Matrix.d > 0) {
1632 dest_height = -dest_height;
1634 if (dest_width == 0 || dest_height == 0) {
1637 FX_RECT bitmap_clip = m_ClipBox;
1638 bitmap_clip.Offset(-image_rect.left, -image_rect.top);
1639 m_Composer.Compose(pDevice, pClipRgn, bitmap_alpha, mask_color,
1640 m_ClipBox, FALSE, FALSE, FALSE, m_bRgbByteOrder, alpha_flag, pIccTransform, m_BlendType);
1642 ret = m_Stretcher.Start(&m_Composer, pSource, dest_width, dest_height, bitmap_clip, dib_flags);
1645 FX_BOOL CFX_ImageRenderer::Continue(IFX_Pause* pPause)
1647 if (m_Status == 1) {
1648 return m_Stretcher.Continue(pPause);
1649 } else if (m_Status == 2) {
1650 if (m_pTransformer->Continue(pPause)) {
1653 CFX_DIBitmap* pBitmap = m_pTransformer->m_Storer.Detach();
1654 if (pBitmap == NULL) {
1657 if (pBitmap->GetBuffer() == NULL) {
1661 if (pBitmap->IsAlphaMask()) {
1662 if (m_BitmapAlpha != 255) {
1663 if (m_AlphaFlag >> 8) {
1664 m_AlphaFlag = (((FX_BYTE)((m_AlphaFlag & 0xff) * m_BitmapAlpha / 255)) | ((m_AlphaFlag >> 8) << 8));
1666 m_MaskColor = FXARGB_MUL_ALPHA(m_MaskColor, m_BitmapAlpha);
1669 m_pDevice->CompositeMask(m_pTransformer->m_ResultLeft, m_pTransformer->m_ResultTop,
1670 pBitmap->GetWidth(), pBitmap->GetHeight(), pBitmap, m_MaskColor,
1671 0, 0, m_BlendType, m_pClipRgn, m_bRgbByteOrder, m_AlphaFlag, m_pIccTransform);
1673 if (m_BitmapAlpha != 255) {
1674 pBitmap->MultiplyAlpha(m_BitmapAlpha);
1676 m_pDevice->CompositeBitmap(m_pTransformer->m_ResultLeft, m_pTransformer->m_ResultTop,
1677 pBitmap->GetWidth(), pBitmap->GetHeight(), pBitmap, 0, 0, m_BlendType, m_pClipRgn, m_bRgbByteOrder, m_pIccTransform);
1684 CFX_BitmapStorer::CFX_BitmapStorer()
1688 CFX_BitmapStorer::~CFX_BitmapStorer()
1694 CFX_DIBitmap* CFX_BitmapStorer::Detach()
1696 CFX_DIBitmap* pBitmap = m_pBitmap;
1700 void CFX_BitmapStorer::Replace(CFX_DIBitmap* pBitmap)
1705 m_pBitmap = pBitmap;
1707 void CFX_BitmapStorer::ComposeScanline(int line, FX_LPCBYTE scanline, FX_LPCBYTE scan_extra_alpha)
1709 FX_LPBYTE dest_buf = (FX_LPBYTE)m_pBitmap->GetScanline(line);
1710 FX_LPBYTE dest_alpha_buf = m_pBitmap->m_pAlphaMask ?
1711 (FX_LPBYTE)m_pBitmap->m_pAlphaMask->GetScanline(line) : NULL;
1713 FXSYS_memcpy32(dest_buf, scanline, m_pBitmap->GetPitch());
1715 if (dest_alpha_buf) {
1716 FXSYS_memcpy32(dest_alpha_buf, scan_extra_alpha, m_pBitmap->m_pAlphaMask->GetPitch());
1719 FX_BOOL CFX_BitmapStorer::SetInfo(int width, int height, FXDIB_Format src_format, FX_DWORD* pSrcPalette)
1721 m_pBitmap = FX_NEW CFX_DIBitmap;
1725 if (!m_pBitmap->Create(width, height, src_format)) {
1731 m_pBitmap->CopyPalette(pSrcPalette);