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_TryAlloc(FX_BYTE, size);
88 if (m_pBuffer == NULL) {
92 m_pBuffer = FX_Alloc(FX_BYTE, size);
98 if (HasAlpha() && format != FXDIB_Argb) {
100 ret = BuildAlphaMask();
102 if (!m_bExtBuf && m_pBuffer) {
105 m_Width = m_Height = m_Pitch = 0;
112 FX_BOOL CFX_DIBitmap::Copy(const CFX_DIBSource* pSrc)
117 if (!Create(pSrc->GetWidth(), pSrc->GetHeight(), pSrc->GetFormat())) {
120 CopyPalette(pSrc->GetPalette());
121 CopyAlphaMask(pSrc->m_pAlphaMask);
122 for (int row = 0; row < pSrc->GetHeight(); row ++) {
123 FXSYS_memcpy32(m_pBuffer + row * m_Pitch, pSrc->GetScanline(row), m_Pitch);
127 CFX_DIBitmap::~CFX_DIBitmap()
129 if (m_pBuffer && !m_bExtBuf) {
134 void CFX_DIBitmap::TakeOver(CFX_DIBitmap* pSrcBitmap)
136 if (m_pBuffer && !m_bExtBuf) {
145 m_pBuffer = pSrcBitmap->m_pBuffer;
146 m_pPalette = pSrcBitmap->m_pPalette;
147 m_pAlphaMask = pSrcBitmap->m_pAlphaMask;
148 pSrcBitmap->m_pBuffer = NULL;
149 pSrcBitmap->m_pPalette = NULL;
150 pSrcBitmap->m_pAlphaMask = NULL;
151 m_bpp = pSrcBitmap->m_bpp;
152 m_bExtBuf = pSrcBitmap->m_bExtBuf;
153 m_AlphaFlag = pSrcBitmap->m_AlphaFlag;
154 m_Width = pSrcBitmap->m_Width;
155 m_Height = pSrcBitmap->m_Height;
156 m_Pitch = pSrcBitmap->m_Pitch;
158 CFX_DIBitmap* CFX_DIBSource::Clone(const FX_RECT* pClip) const
160 FX_RECT rect(0, 0, m_Width, m_Height);
162 rect.Intersect(*pClip);
163 if (rect.IsEmpty()) {
167 CFX_DIBitmap* pNewBitmap = FX_NEW CFX_DIBitmap;
171 if (!pNewBitmap->Create(rect.Width(), rect.Height(), GetFormat())) {
175 pNewBitmap->CopyPalette(m_pPalette);
176 pNewBitmap->CopyAlphaMask(m_pAlphaMask, pClip);
177 if (GetBPP() == 1 && rect.left % 8 != 0) {
178 int left_shift = rect.left % 32;
179 int right_shift = 32 - left_shift;
180 int dword_count = pNewBitmap->m_Pitch / 4;
181 for (int row = rect.top; row < rect.bottom; row ++) {
182 FX_DWORD* src_scan = (FX_DWORD*)GetScanline(row) + rect.left / 32;
183 FX_DWORD* dest_scan = (FX_DWORD*)pNewBitmap->GetScanline(row - rect.top);
184 for (int i = 0; i < dword_count; i ++) {
185 dest_scan[i] = (src_scan[i] << left_shift) | (src_scan[i + 1] >> right_shift);
189 int copy_len = (pNewBitmap->GetWidth() * pNewBitmap->GetBPP() + 7) / 8;
190 if (m_Pitch < (FX_DWORD)copy_len) {
193 for (int row = rect.top; row < rect.bottom; row ++) {
194 FX_LPCBYTE src_scan = GetScanline(row) + rect.left * m_bpp / 8;
195 FX_LPBYTE dest_scan = (FX_LPBYTE)pNewBitmap->GetScanline(row - rect.top);
196 FXSYS_memcpy32(dest_scan, src_scan, copy_len);
201 void CFX_DIBSource::BuildPalette()
207 m_pPalette = FX_Alloc(FX_DWORD, 2);
209 m_pPalette[0] = 0xff;
212 m_pPalette[0] = 0xff000000;
213 m_pPalette[1] = 0xffffffff;
215 } else if (GetBPP() == 8) {
216 m_pPalette = FX_Alloc(FX_DWORD, 256);
218 for (int i = 0; i < 256; i ++) {
219 m_pPalette[i] = 0xff - i;
222 for (int i = 0; i < 256; i ++) {
223 m_pPalette[i] = 0xff000000 | (i * 0x10101);
228 FX_BOOL CFX_DIBSource::BuildAlphaMask()
233 m_pAlphaMask = FX_NEW CFX_DIBitmap;
237 if (!m_pAlphaMask->Create(m_Width, m_Height, FXDIB_8bppMask)) {
242 FXSYS_memset8(m_pAlphaMask->GetBuffer(), 0xff, m_pAlphaMask->GetHeight()*m_pAlphaMask->GetPitch());
245 FX_DWORD CFX_DIBSource::GetPaletteEntry(int index) const
247 ASSERT((GetBPP() == 1 || GetBPP() == 8) && !IsAlphaMask());
249 return m_pPalette[index];
253 return index ? 0 : 0xff;
258 return index ? 0xffffffff : 0xff000000;
260 return index * 0x10101 | 0xff000000;
262 void CFX_DIBSource::SetPaletteEntry(int index, FX_DWORD color)
264 ASSERT((GetBPP() == 1 || GetBPP() == 8) && !IsAlphaMask());
265 if (m_pPalette == NULL) {
268 m_pPalette[index] = color;
270 int CFX_DIBSource::FindPalette(FX_DWORD color) const
272 ASSERT((GetBPP() == 1 || GetBPP() == 8) && !IsAlphaMask());
273 if (m_pPalette == NULL) {
276 return ((FX_BYTE)color == 0xff) ? 0 : 1;
278 return 0xff - (FX_BYTE)color;
281 return ((FX_BYTE)color == 0xff) ? 1 : 0;
283 return (FX_BYTE)color;
285 int palsize = (1 << GetBPP());
286 for (int i = 0; i < palsize; i ++)
287 if (m_pPalette[i] == color) {
292 void CFX_DIBitmap::Clear(FX_DWORD color)
294 if (m_pBuffer == NULL) {
297 switch (GetFormat()) {
299 FXSYS_memset8(m_pBuffer, (color & 0xff000000) ? 0xff : 0, m_Pitch * m_Height);
301 case FXDIB_1bppRgb: {
302 int index = FindPalette(color);
303 FXSYS_memset8(m_pBuffer, index ? 0xff : 0, m_Pitch * m_Height);
307 FXSYS_memset8(m_pBuffer, color >> 24, m_Pitch * m_Height);
309 case FXDIB_8bppRgb: {
310 int index = FindPalette(color);
311 FXSYS_memset8(m_pBuffer, index, m_Pitch * m_Height);
317 ArgbDecode(color, a, r, g, b);
318 if (r == g && g == b) {
319 FXSYS_memset8(m_pBuffer, r, m_Pitch * m_Height);
322 for (int col = 0; col < m_Width; col ++) {
323 m_pBuffer[byte_pos++] = b;
324 m_pBuffer[byte_pos++] = g;
325 m_pBuffer[byte_pos++] = r;
327 for (int row = 1; row < m_Height; row ++) {
328 FXSYS_memcpy32(m_pBuffer + row * m_Pitch, m_pBuffer, m_Pitch);
335 color = IsCmykImage() ? FXCMYK_TODIB(color) : FXARGB_TODIB(color);
336 for (int i = 0; i < m_Width; i ++) {
337 ((FX_DWORD*)m_pBuffer)[i] = color;
339 for (int row = 1; row < m_Height; row ++) {
340 FXSYS_memcpy32(m_pBuffer + row * m_Pitch, m_pBuffer, m_Pitch);
348 void CFX_DIBSource::GetOverlapRect(int& dest_left, int& dest_top, int& width, int& height,
349 int src_width, int src_height, int& src_left, int& src_top,
350 const CFX_ClipRgn* pClipRgn)
352 if (width == 0 || height == 0) {
355 ASSERT(width > 0 && height > 0);
356 if (dest_left > m_Width || dest_top > m_Height) {
361 int x_offset = dest_left - src_left;
362 int y_offset = dest_top - src_top;
363 FX_RECT src_rect(src_left, src_top, src_left + width, src_top + height);
364 FX_RECT src_bound(0, 0, src_width, src_height);
365 src_rect.Intersect(src_bound);
366 FX_RECT dest_rect(src_rect.left + x_offset, src_rect.top + y_offset,
367 src_rect.right + x_offset, src_rect.bottom + y_offset);
368 FX_RECT dest_bound(0, 0, m_Width, m_Height);
369 dest_rect.Intersect(dest_bound);
371 dest_rect.Intersect(pClipRgn->GetBox());
373 dest_left = dest_rect.left;
374 dest_top = dest_rect.top;
375 src_left = dest_left - x_offset;
376 src_top = dest_top - y_offset;
377 width = dest_rect.right - dest_rect.left;
378 height = dest_rect.bottom - dest_rect.top;
380 FX_BOOL CFX_DIBitmap::TransferBitmap(int dest_left, int dest_top, int width, int height,
381 const CFX_DIBSource* pSrcBitmap, int src_left, int src_top, void* pIccTransform)
383 if (m_pBuffer == NULL) {
386 GetOverlapRect(dest_left, dest_top, width, height, pSrcBitmap->GetWidth(), pSrcBitmap->GetHeight(), src_left, src_top, NULL);
387 if (width == 0 || height == 0) {
390 FXDIB_Format dest_format = GetFormat();
391 FXDIB_Format src_format = pSrcBitmap->GetFormat();
392 if (dest_format == src_format && pIccTransform == NULL) {
394 for (int row = 0; row < height; row ++) {
395 FX_LPBYTE dest_scan = m_pBuffer + (dest_top + row) * m_Pitch;
396 FX_LPCBYTE src_scan = pSrcBitmap->GetScanline(src_top + row);
397 for (int col = 0; col < width; col ++) {
398 if (src_scan[(src_left + col) / 8] & (1 << (7 - (src_left + col) % 8))) {
399 dest_scan[(dest_left + col) / 8] |= 1 << (7 - (dest_left + col) % 8);
401 dest_scan[(dest_left + col) / 8] &= ~(1 << (7 - (dest_left + col) % 8));
406 int Bpp = GetBPP() / 8;
407 for (int row = 0; row < height; row ++) {
408 FX_LPBYTE dest_scan = m_pBuffer + (dest_top + row) * m_Pitch + dest_left * Bpp;
409 FX_LPCBYTE src_scan = pSrcBitmap->GetScanline(src_top + row) + src_left * Bpp;
410 FXSYS_memcpy32(dest_scan, src_scan, width * Bpp);
418 dest_format = FXDIB_8bppMask;
420 FX_LPBYTE dest_buf = m_pBuffer + dest_top * m_Pitch + dest_left * GetBPP() / 8;
421 FX_DWORD* d_plt = NULL;
422 if(!ConvertBuffer(dest_format, dest_buf, m_Pitch, width, height, pSrcBitmap, src_left, src_top, d_plt, pIccTransform)) {
428 FX_BOOL CFX_DIBitmap::TransferMask(int dest_left, int dest_top, int width, int height,
429 const CFX_DIBSource* pMask, FX_DWORD color, int src_left, int src_top, int alpha_flag, void* pIccTransform)
431 if (m_pBuffer == NULL) {
434 ASSERT(HasAlpha() && (m_bpp >= 24));
435 ASSERT(pMask->IsAlphaMask());
436 if (!HasAlpha() || !pMask->IsAlphaMask() || m_bpp < 24) {
439 GetOverlapRect(dest_left, dest_top, width, height, pMask->GetWidth(), pMask->GetHeight(), src_left, src_top, NULL);
440 if (width == 0 || height == 0) {
443 int src_bpp = pMask->GetBPP();
446 if (alpha_flag >> 8) {
447 alpha = alpha_flag & 0xff;
448 dst_color = FXCMYK_TODIB(color);
450 alpha = FXARGB_A(color);
451 dst_color = FXARGB_TODIB(color);
453 FX_LPBYTE color_p = (FX_LPBYTE)&dst_color;
454 if (pIccTransform && CFX_GEModule::Get()->GetCodecModule() && CFX_GEModule::Get()->GetCodecModule()->GetIccModule()) {
455 ICodec_IccModule* pIccModule = CFX_GEModule::Get()->GetCodecModule()->GetIccModule();
456 pIccModule->TranslateScanline(pIccTransform, color_p, color_p, 1);
458 if (alpha_flag >> 8 && !IsCmykImage())
459 AdobeCMYK_to_sRGB1(FXSYS_GetCValue(color), FXSYS_GetMValue(color), FXSYS_GetYValue(color), FXSYS_GetKValue(color),
460 color_p[2], color_p[1], color_p[0]);
461 else if (!(alpha_flag >> 8) && IsCmykImage()) {
466 color_p[3] = (FX_BYTE)alpha;
468 if (GetFormat() == FXDIB_Argb) {
469 for (int row = 0; row < height; row ++) {
470 FX_DWORD* dest_pos = (FX_DWORD*)(m_pBuffer + (dest_top + row) * m_Pitch + dest_left * 4);
471 FX_LPCBYTE src_scan = pMask->GetScanline(src_top + row);
473 for (int col = 0; col < width; col ++) {
474 int src_bitpos = src_left + col;
475 if (src_scan[src_bitpos / 8] & (1 << (7 - src_bitpos % 8))) {
476 *dest_pos = dst_color;
483 src_scan += src_left;
484 dst_color = FXARGB_TODIB(dst_color);
485 dst_color &= 0xffffff;
486 for (int col = 0; col < width; col ++) {
487 FXARGB_SETDIB(dest_pos++, dst_color | ((alpha * (*src_scan++) / 255) << 24));
492 int comps = m_bpp / 8;
493 for (int row = 0; row < height; row ++) {
494 FX_LPBYTE dest_color_pos = m_pBuffer + (dest_top + row) * m_Pitch + dest_left * comps;
495 FX_LPBYTE dest_alpha_pos = (FX_LPBYTE)m_pAlphaMask->GetScanline(dest_top + row) + dest_left;
496 FX_LPCBYTE src_scan = pMask->GetScanline(src_top + row);
498 for (int col = 0; col < width; col ++) {
499 int src_bitpos = src_left + col;
500 if (src_scan[src_bitpos / 8] & (1 << (7 - src_bitpos % 8))) {
501 FXSYS_memcpy32(dest_color_pos, color_p, comps);
502 *dest_alpha_pos = 0xff;
504 FXSYS_memset32(dest_color_pos, 0, comps);
507 dest_color_pos += comps;
511 src_scan += src_left;
512 for (int col = 0; col < width; col ++) {
513 FXSYS_memcpy32(dest_color_pos, color_p, comps);
514 dest_color_pos += comps;
515 *dest_alpha_pos++ = (alpha * (*src_scan++) / 255);
522 void CFX_DIBSource::CopyPalette(const FX_DWORD* pSrc, FX_DWORD size)
524 if (pSrc == NULL || GetBPP() > 8) {
530 FX_DWORD pal_size = 1 << GetBPP();
531 if (m_pPalette == NULL) {
532 m_pPalette = FX_Alloc(FX_DWORD, pal_size);
534 if (pal_size > size) {
537 FXSYS_memcpy32(m_pPalette, pSrc, pal_size * sizeof(FX_DWORD));
540 void CFX_DIBSource::GetPalette(FX_DWORD* pal, int alpha) const
542 ASSERT(GetBPP() <= 8 && !IsCmykImage());
544 pal[0] = ((m_pPalette ? m_pPalette[0] : 0xff000000) & 0xffffff) | (alpha << 24);
545 pal[1] = ((m_pPalette ? m_pPalette[1] : 0xffffffff) & 0xffffff) | (alpha << 24);
549 for (int i = 0; i < 256; i ++) {
550 pal[i] = (m_pPalette[i] & 0x00ffffff) | (alpha << 24);
553 for (int i = 0; i < 256; i ++) {
554 pal[i] = (i * 0x10101) | (alpha << 24);
558 CFX_DIBitmap* CFX_DIBSource::GetAlphaMask(const FX_RECT* pClip) const
560 ASSERT(GetFormat() == FXDIB_Argb);
561 FX_RECT rect(0, 0, m_Width, m_Height);
563 rect.Intersect(*pClip);
564 if (rect.IsEmpty()) {
568 CFX_DIBitmap* pMask = FX_NEW CFX_DIBitmap;
572 if (!pMask->Create(rect.Width(), rect.Height(), FXDIB_8bppMask)) {
576 for (int row = rect.top; row < rect.bottom; row ++) {
577 FX_LPCBYTE src_scan = GetScanline(row) + rect.left * 4 + 3;
578 FX_LPBYTE dest_scan = (FX_LPBYTE)pMask->GetScanline(row - rect.top);
579 for (int col = rect.left; col < rect.right; col ++) {
580 *dest_scan ++ = *src_scan;
586 FX_BOOL CFX_DIBSource::CopyAlphaMask(const CFX_DIBSource* pAlphaMask, const FX_RECT* pClip)
588 if (!HasAlpha() || GetFormat() == FXDIB_Argb) {
592 FX_RECT rect(0, 0, pAlphaMask->m_Width, pAlphaMask->m_Height);
594 rect.Intersect(*pClip);
595 if (rect.IsEmpty() || rect.Width() != m_Width || rect.Height() != m_Height) {
599 if (pAlphaMask->m_Width != m_Width || pAlphaMask->m_Height != m_Height) {
603 for (int row = 0; row < m_Height; row ++)
604 FXSYS_memcpy32((void*)m_pAlphaMask->GetScanline(row),
605 pAlphaMask->GetScanline(row + rect.top) + rect.left, m_pAlphaMask->m_Pitch);
607 m_pAlphaMask->Clear(0xff000000);
611 const int g_ChannelOffset[] = {0, 2, 1, 0, 0, 1, 2, 3, 3};
612 FX_BOOL CFX_DIBitmap::LoadChannel(FXDIB_Channel destChannel, const CFX_DIBSource* pSrcBitmap, FXDIB_Channel srcChannel)
614 if (m_pBuffer == NULL) {
617 CFX_DIBSource* pSrcClone = (CFX_DIBSource*)pSrcBitmap;
618 CFX_DIBitmap* pDst = this;
619 int destOffset, srcOffset;
620 if (srcChannel == FXDIB_Alpha) {
621 if (!pSrcBitmap->HasAlpha() && !pSrcBitmap->IsAlphaMask()) {
624 if (pSrcBitmap->GetBPP() == 1) {
625 pSrcClone = pSrcBitmap->CloneConvert(FXDIB_8bppMask);
626 if (pSrcClone == NULL) {
630 if(pSrcBitmap->GetFormat() == FXDIB_Argb) {
636 if (pSrcBitmap->IsAlphaMask()) {
639 if (pSrcBitmap->GetBPP() < 24) {
640 if (pSrcBitmap->IsCmykImage()) {
641 pSrcClone = pSrcBitmap->CloneConvert((FXDIB_Format)((pSrcBitmap->GetFormat() & 0xff00) | 0x20));
643 pSrcClone = pSrcBitmap->CloneConvert((FXDIB_Format)((pSrcBitmap->GetFormat() & 0xff00) | 0x18));
645 if (pSrcClone == NULL) {
649 srcOffset = g_ChannelOffset[srcChannel];
651 if (destChannel == FXDIB_Alpha) {
653 if(!ConvertFormat(FXDIB_8bppMask)) {
654 if (pSrcClone != pSrcBitmap) {
662 if(!ConvertFormat(IsCmykImage() ? FXDIB_Cmyka : FXDIB_Argb)) {
663 if (pSrcClone != pSrcBitmap) {
668 if (GetFormat() == FXDIB_Argb) {
674 if (pSrcClone != pSrcBitmap) {
681 if (!ConvertFormat(IsCmykImage() ? FXDIB_Cmyka : FXDIB_Argb)) {
682 if (pSrcClone != pSrcBitmap) {
688 #if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_
689 if (!ConvertFormat(IsCmykImage() ? FXDIB_Cmyk : FXDIB_Rgb32)) {
691 if (!ConvertFormat(IsCmykImage() ? FXDIB_Cmyk : FXDIB_Rgb)) {
693 if (pSrcClone != pSrcBitmap) {
699 destOffset = g_ChannelOffset[destChannel];
701 if (srcChannel == FXDIB_Alpha && pSrcClone->m_pAlphaMask) {
702 CFX_DIBitmap* pAlphaMask = pSrcClone->m_pAlphaMask;
703 if (pSrcClone->GetWidth() != m_Width || pSrcClone->GetHeight() != m_Height) {
705 pAlphaMask = pAlphaMask->StretchTo(m_Width, m_Height);
706 if (pAlphaMask == NULL) {
707 if (pSrcClone != pSrcBitmap) {
714 if (pSrcClone != pSrcBitmap) {
715 pSrcClone->m_pAlphaMask = NULL;
718 pSrcClone = pAlphaMask;
720 } else if (pSrcClone->GetWidth() != m_Width || pSrcClone->GetHeight() != m_Height) {
721 CFX_DIBitmap* pSrcMatched = pSrcClone->StretchTo(m_Width, m_Height);
722 if (pSrcClone != pSrcBitmap) {
725 if (pSrcMatched == NULL) {
728 pSrcClone = pSrcMatched;
730 if (destChannel == FXDIB_Alpha && m_pAlphaMask) {
734 int srcBytes = pSrcClone->GetBPP() / 8;
735 int destBytes = pDst->GetBPP() / 8;
736 for (int row = 0; row < m_Height; row ++) {
737 FX_LPBYTE dest_pos = (FX_LPBYTE)pDst->GetScanline(row) + destOffset;
738 FX_LPCBYTE src_pos = pSrcClone->GetScanline(row) + srcOffset;
739 for (int col = 0; col < m_Width; col ++) {
740 *dest_pos = *src_pos;
741 dest_pos += destBytes;
745 if (pSrcClone != pSrcBitmap && pSrcClone != pSrcBitmap->m_pAlphaMask) {
750 FX_BOOL CFX_DIBitmap::LoadChannel(FXDIB_Channel destChannel, int value)
752 if (m_pBuffer == NULL) {
756 if (destChannel == FXDIB_Alpha) {
758 if(!ConvertFormat(FXDIB_8bppMask)) {
764 if(!ConvertFormat(IsCmykImage() ? FXDIB_Cmyka : FXDIB_Argb)) {
767 if (GetFormat() == FXDIB_Argb) {
777 if (!ConvertFormat(IsCmykImage() ? FXDIB_Cmyka : FXDIB_Argb)) {
781 #if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_
782 if (!ConvertFormat(IsCmykImage() ? FXDIB_Cmyk : FXDIB_Rgb)) {
786 if (!ConvertFormat(IsCmykImage() ? FXDIB_Cmyk : FXDIB_Rgb32)) {
791 destOffset = g_ChannelOffset[destChannel];
793 int Bpp = GetBPP() / 8;
795 FXSYS_memset8(m_pBuffer, value, m_Height * m_Pitch);
798 if (destChannel == FXDIB_Alpha && m_pAlphaMask) {
799 FXSYS_memset8(m_pAlphaMask->GetBuffer(), value, m_pAlphaMask->GetHeight()*m_pAlphaMask->GetPitch());
802 for (int row = 0; row < m_Height; row ++) {
803 FX_LPBYTE scan_line = m_pBuffer + row * m_Pitch + destOffset;
804 for (int col = 0; col < m_Width; col ++) {
811 FX_BOOL CFX_DIBitmap::MultiplyAlpha(const CFX_DIBSource* pSrcBitmap)
813 if (m_pBuffer == NULL) {
816 ASSERT(pSrcBitmap->IsAlphaMask());
817 if (!pSrcBitmap->IsAlphaMask()) {
820 if (!IsAlphaMask() && !HasAlpha()) {
821 return LoadChannel(FXDIB_Alpha, pSrcBitmap, FXDIB_Alpha);
823 CFX_DIBitmap* pSrcClone = (CFX_DIBitmap*)pSrcBitmap;
824 if (pSrcBitmap->GetWidth() != m_Width || pSrcBitmap->GetHeight() != m_Height) {
825 pSrcClone = pSrcBitmap->StretchTo(m_Width, m_Height);
826 ASSERT(pSrcClone != NULL);
827 if (pSrcClone == NULL) {
832 if(!ConvertFormat(FXDIB_8bppMask)) {
833 if (pSrcClone != pSrcBitmap) {
838 for (int row = 0; row < m_Height; row ++) {
839 FX_LPBYTE dest_scan = m_pBuffer + m_Pitch * row;
840 FX_LPBYTE src_scan = pSrcClone->m_pBuffer + pSrcClone->m_Pitch * row;
841 if (pSrcClone->GetBPP() == 1) {
842 for (int col = 0; col < m_Width; col ++) {
843 if (!((1 << (7 - col % 8)) & src_scan[col / 8])) {
848 for (int col = 0; col < m_Width; col ++) {
849 *dest_scan = (*dest_scan) * src_scan[col] / 255;
855 if(GetFormat() == FXDIB_Argb) {
856 if (pSrcClone->GetBPP() == 1) {
857 if (pSrcClone != pSrcBitmap) {
862 for (int row = 0; row < m_Height; row ++) {
863 FX_LPBYTE dest_scan = m_pBuffer + m_Pitch * row + 3;
864 FX_LPBYTE src_scan = pSrcClone->m_pBuffer + pSrcClone->m_Pitch * row;
865 for (int col = 0; col < m_Width; col ++) {
866 *dest_scan = (*dest_scan) * src_scan[col] / 255;
871 m_pAlphaMask->MultiplyAlpha(pSrcClone);
874 if (pSrcClone != pSrcBitmap) {
879 FX_BOOL CFX_DIBitmap::GetGrayData(void* pIccTransform)
881 if (m_pBuffer == NULL) {
884 switch (GetFormat()) {
885 case FXDIB_1bppRgb: {
886 if (m_pPalette == NULL) {
890 for (int i = 0; i < 2; i ++) {
891 int r = (FX_BYTE)(m_pPalette[i] >> 16);
892 int g = (FX_BYTE)(m_pPalette[i] >> 8);
893 int b = (FX_BYTE)m_pPalette[i];
894 gray[i] = (FX_BYTE)FXRGB2GRAY(r, g, b);
896 CFX_DIBitmap* pMask = FX_NEW CFX_DIBitmap;
900 if (!pMask->Create(m_Width, m_Height, FXDIB_8bppMask)) {
904 FXSYS_memset8(pMask->GetBuffer(), gray[0], pMask->GetPitch() * m_Height);
905 for (int row = 0; row < m_Height; row ++) {
906 FX_LPBYTE src_pos = m_pBuffer + row * m_Pitch;
907 FX_LPBYTE dest_pos = (FX_LPBYTE)pMask->GetScanline(row);
908 for (int col = 0; col < m_Width; col ++) {
909 if (src_pos[col / 8] & (1 << (7 - col % 8))) {
919 case FXDIB_8bppRgb: {
920 if (m_pPalette == NULL) {
924 for (int i = 0; i < 256; i ++) {
925 int r = (FX_BYTE)(m_pPalette[i] >> 16);
926 int g = (FX_BYTE)(m_pPalette[i] >> 8);
927 int b = (FX_BYTE)m_pPalette[i];
928 gray[i] = (FX_BYTE)FXRGB2GRAY(r, g, b);
930 CFX_DIBitmap* pMask = FX_NEW CFX_DIBitmap;
934 if (!pMask->Create(m_Width, m_Height, FXDIB_8bppMask)) {
938 for (int row = 0; row < m_Height; row ++) {
939 FX_LPBYTE dest_pos = pMask->GetBuffer() + row * pMask->GetPitch();
940 FX_LPBYTE src_pos = m_pBuffer + row * m_Pitch;
941 for (int col = 0; col < m_Width; col ++) {
942 *dest_pos ++ = gray[*src_pos ++];
950 CFX_DIBitmap* pMask = FX_NEW CFX_DIBitmap;
954 if (!pMask->Create(m_Width, m_Height, FXDIB_8bppMask)) {
958 for (int row = 0; row < m_Height; row ++) {
959 FX_LPBYTE src_pos = m_pBuffer + row * m_Pitch;
960 FX_LPBYTE dest_pos = pMask->GetBuffer() + row * pMask->GetPitch();
961 for (int col = 0; col < m_Width; col ++) {
962 *dest_pos ++ = FXRGB2GRAY(src_pos[2], src_pos[1], *src_pos);
971 CFX_DIBitmap* pMask = FX_NEW CFX_DIBitmap;
975 if (!pMask->Create(m_Width, m_Height, FXDIB_8bppMask)) {
979 for (int row = 0; row < m_Height; row ++) {
980 FX_LPBYTE src_pos = m_pBuffer + row * m_Pitch;
981 FX_LPBYTE dest_pos = pMask->GetBuffer() + row * pMask->GetPitch();
982 for (int col = 0; col < m_Width; col ++) {
983 *dest_pos ++ = FXRGB2GRAY(src_pos[2], src_pos[1], *src_pos);
996 FX_BOOL CFX_DIBitmap::MultiplyAlpha(int alpha)
998 if (m_pBuffer == NULL) {
1001 switch (GetFormat()) {
1002 case FXDIB_1bppMask:
1003 if (!ConvertFormat(FXDIB_8bppMask)) {
1006 MultiplyAlpha(alpha);
1008 case FXDIB_8bppMask: {
1009 for (int row = 0; row < m_Height; row ++) {
1010 FX_LPBYTE scan_line = m_pBuffer + row * m_Pitch;
1011 for (int col = 0; col < m_Width; col ++) {
1012 scan_line[col] = scan_line[col] * alpha / 255;
1018 for (int row = 0; row < m_Height; row ++) {
1019 FX_LPBYTE scan_line = m_pBuffer + row * m_Pitch + 3;
1020 for (int col = 0; col < m_Width; col ++) {
1021 *scan_line = (*scan_line) * alpha / 255;
1029 m_pAlphaMask->MultiplyAlpha(alpha);
1030 } else if (IsCmykImage()) {
1031 if (!ConvertFormat((FXDIB_Format)(GetFormat() | 0x0200))) {
1034 m_pAlphaMask->MultiplyAlpha(alpha);
1036 if (!ConvertFormat(FXDIB_Argb)) {
1039 MultiplyAlpha(alpha);
1045 FX_DWORD CFX_DIBitmap::GetPixel(int x, int y) const
1047 if (m_pBuffer == NULL) {
1050 FX_LPBYTE pos = m_pBuffer + y * m_Pitch + x * GetBPP() / 8;
1051 switch (GetFormat()) {
1052 case FXDIB_1bppMask: {
1053 if ((*pos) & (1 << (7 - x % 8))) {
1058 case FXDIB_1bppRgb: {
1059 if ((*pos) & (1 << (7 - x % 8))) {
1060 return m_pPalette ? m_pPalette[1] : 0xffffffff;
1062 return m_pPalette ? m_pPalette[0] : 0xff000000;
1066 case FXDIB_8bppMask:
1067 return (*pos) << 24;
1069 return m_pPalette ? m_pPalette[*pos] : (0xff000000 | ((*pos) * 0x10101));
1073 return FXARGB_GETDIB(pos) | 0xff000000;
1075 return FXARGB_GETDIB(pos);
1081 void CFX_DIBitmap::SetPixel(int x, int y, FX_DWORD color)
1083 if (m_pBuffer == NULL) {
1086 if (x < 0 || x >= m_Width || y < 0 || y >= m_Height) {
1089 FX_LPBYTE pos = m_pBuffer + y * m_Pitch + x * GetBPP() / 8;
1090 switch (GetFormat()) {
1091 case FXDIB_1bppMask:
1093 *pos |= 1 << (7 - x % 8);
1095 *pos &= ~(1 << (7 - x % 8));
1100 if (color == m_pPalette[1]) {
1101 *pos |= 1 << (7 - x % 8);
1103 *pos &= ~(1 << (7 - x % 8));
1106 if (color == 0xffffffff) {
1107 *pos |= 1 << (7 - x % 8);
1109 *pos &= ~(1 << (7 - x % 8));
1113 case FXDIB_8bppMask:
1114 *pos = (FX_BYTE)(color >> 24);
1116 case FXDIB_8bppRgb: {
1118 for (int i = 0; i < 256; i ++) {
1119 if (m_pPalette[i] == color) {
1126 *pos = FXRGB2GRAY(FXARGB_R(color), FXARGB_G(color), FXARGB_B(color));
1132 int alpha = FXARGB_A(color);
1133 pos[0] = (FXARGB_B(color) * alpha + pos[0] * (255 - alpha)) / 255;
1134 pos[1] = (FXARGB_G(color) * alpha + pos[1] * (255 - alpha)) / 255;
1135 pos[2] = (FXARGB_R(color) * alpha + pos[2] * (255 - alpha)) / 255;
1139 pos[0] = FXARGB_B(color);
1140 pos[1] = FXARGB_G(color);
1141 pos[2] = FXARGB_R(color);
1145 FXARGB_SETDIB(pos, color);
1151 void CFX_DIBitmap::DownSampleScanline(int line, FX_LPBYTE dest_scan, int dest_bpp,
1152 int dest_width, FX_BOOL bFlipX, int clip_left, int clip_width) const
1154 if (m_pBuffer == NULL) {
1157 int src_Bpp = m_bpp / 8;
1158 FX_LPBYTE scanline = m_pBuffer + line * m_Pitch;
1160 for (int i = 0; i < clip_width; i ++) {
1161 FX_DWORD dest_x = clip_left + i;
1162 FX_DWORD src_x = dest_x * m_Width / dest_width;
1164 src_x = m_Width - src_x - 1;
1167 dest_scan[i] = (scanline[src_x / 8] & (1 << (7 - src_x % 8))) ? 255 : 0;
1169 } else if (src_Bpp == 1) {
1170 for (int i = 0; i < clip_width; i ++) {
1171 FX_DWORD dest_x = clip_left + i;
1172 FX_DWORD src_x = dest_x * m_Width / dest_width;
1174 src_x = m_Width - src_x - 1;
1179 if (!IsCmykImage()) {
1181 FX_ARGB argb = m_pPalette[scanline[src_x]];
1182 dest_scan[dest_pos] = FXARGB_B(argb);
1183 dest_scan[dest_pos + 1] = FXARGB_G(argb);
1184 dest_scan[dest_pos + 2] = FXARGB_R(argb);
1187 FX_CMYK cmyk = m_pPalette[scanline[src_x]];
1188 dest_scan[dest_pos] = FXSYS_GetCValue(cmyk);
1189 dest_scan[dest_pos + 1] = FXSYS_GetMValue(cmyk);
1190 dest_scan[dest_pos + 2] = FXSYS_GetYValue(cmyk);
1191 dest_scan[dest_pos + 3] = FXSYS_GetKValue(cmyk);
1194 dest_scan[dest_pos] = scanline[src_x];
1198 for (int i = 0; i < clip_width; i ++) {
1199 FX_DWORD dest_x = clip_left + i;
1200 FX_DWORD src_x = bFlipX ? (m_Width - dest_x * m_Width / dest_width - 1) * src_Bpp : (dest_x * m_Width / dest_width) * src_Bpp;
1201 src_x %= m_Width * src_Bpp;
1202 int dest_pos = i * src_Bpp;
1203 for (int b = 0; b < src_Bpp; b ++) {
1204 dest_scan[dest_pos + b] = scanline[src_x + b];
1209 FX_BOOL CFX_DIBitmap::ConvertColorScale(FX_DWORD forecolor, FX_DWORD backcolor)
1211 ASSERT(!IsAlphaMask());
1212 if (m_pBuffer == NULL || IsAlphaMask()) {
1215 int fc, fm, fy, fk, bc, bm, by, bk;
1216 int fr, fg, fb, br, bg, bb;
1217 FX_BOOL isCmykImage = IsCmykImage();
1219 fc = FXSYS_GetCValue(forecolor);
1220 fm = FXSYS_GetMValue(forecolor);
1221 fy = FXSYS_GetYValue(forecolor);
1222 fk = FXSYS_GetKValue(forecolor);
1223 bc = FXSYS_GetCValue(backcolor);
1224 bm = FXSYS_GetMValue(backcolor);
1225 by = FXSYS_GetYValue(backcolor);
1226 bk = FXSYS_GetKValue(backcolor);
1228 fr = FXSYS_GetRValue(forecolor);
1229 fg = FXSYS_GetGValue(forecolor);
1230 fb = FXSYS_GetBValue(forecolor);
1231 br = FXSYS_GetRValue(backcolor);
1232 bg = FXSYS_GetGValue(backcolor);
1233 bb = FXSYS_GetBValue(backcolor);
1237 if (forecolor == 0xff && backcolor == 0 && m_pPalette == NULL) {
1240 } else if (forecolor == 0 && backcolor == 0xffffff && m_pPalette == NULL) {
1243 if (m_pPalette == NULL) {
1246 int size = 1 << m_bpp;
1248 for (int i = 0; i < size; i ++) {
1250 AdobeCMYK_to_sRGB1(FXSYS_GetCValue(m_pPalette[i]), FXSYS_GetMValue(m_pPalette[i]), FXSYS_GetYValue(m_pPalette[i]), FXSYS_GetKValue(m_pPalette[i]),
1252 int gray = 255 - FXRGB2GRAY(r, g, b);
1253 m_pPalette[i] = CmykEncode(bc + (fc - bc) * gray / 255, bm + (fm - bm) * gray / 255,
1254 by + (fy - by) * gray / 255, bk + (fk - bk) * gray / 255);
1257 for (int i = 0; i < size; i ++) {
1258 int gray = FXRGB2GRAY(FXARGB_R(m_pPalette[i]), FXARGB_G(m_pPalette[i]), FXARGB_B(m_pPalette[i]));
1259 m_pPalette[i] = FXARGB_MAKE(0xff, br + (fr - br) * gray / 255, bg + (fg - bg) * gray / 255,
1260 bb + (fb - bb) * gray / 255);
1265 if (forecolor == 0xff && backcolor == 0x00) {
1266 for (int row = 0; row < m_Height; row ++) {
1267 FX_LPBYTE scanline = m_pBuffer + row * m_Pitch;
1268 for (int col = 0; col < m_Width; col ++) {
1270 AdobeCMYK_to_sRGB1(scanline[0], scanline[1], scanline[2], scanline[3],
1275 *scanline ++ = 255 - FXRGB2GRAY(r, g, b);
1280 } else if (forecolor == 0 && backcolor == 0xffffff) {
1281 for (int row = 0; row < m_Height; row ++) {
1282 FX_LPBYTE scanline = m_pBuffer + row * m_Pitch;
1283 int gap = m_bpp / 8 - 2;
1284 for (int col = 0; col < m_Width; col ++) {
1285 int gray = FXRGB2GRAY(scanline[2], scanline[1], scanline[0]);
1286 *scanline ++ = gray;
1287 *scanline ++ = gray;
1295 for (int row = 0; row < m_Height; row ++) {
1296 FX_LPBYTE scanline = m_pBuffer + row * m_Pitch;
1297 for (int col = 0; col < m_Width; col ++) {
1299 AdobeCMYK_to_sRGB1(scanline[0], scanline[1], scanline[2], scanline[3],
1301 int gray = 255 - FXRGB2GRAY(r, g, b);
1302 *scanline ++ = bc + (fc - bc) * gray / 255;
1303 *scanline ++ = bm + (fm - bm) * gray / 255;
1304 *scanline ++ = by + (fy - by) * gray / 255;
1305 *scanline ++ = bk + (fk - bk) * gray / 255;
1309 for (int row = 0; row < m_Height; row ++) {
1310 FX_LPBYTE scanline = m_pBuffer + row * m_Pitch;
1311 int gap = m_bpp / 8 - 2;
1312 for (int col = 0; col < m_Width; col ++) {
1313 int gray = FXRGB2GRAY(scanline[2], scanline[1], scanline[0]);
1314 *scanline ++ = bb + (fb - bb) * gray / 255;
1315 *scanline ++ = bg + (fg - bg) * gray / 255;
1316 *scanline = br + (fr - br) * gray / 255;
1323 FX_BOOL CFX_DIBitmap::DitherFS(const FX_DWORD* pPalette, int pal_size, const FX_RECT* pRect)
1325 if (m_pBuffer == NULL) {
1328 if (m_bpp != 8 && m_pPalette != NULL && m_AlphaFlag != 0) {
1331 if (m_Width < 4 && m_Height < 4) {
1334 FX_RECT rect(0, 0, m_Width, m_Height);
1336 rect.Intersect(*pRect);
1338 FX_BYTE translate[256];
1339 for (int i = 0; i < 256; i ++) {
1341 for (int j = 0; j < pal_size; j ++) {
1342 FX_BYTE entry = (FX_BYTE)pPalette[j];
1343 int err = (int)entry - i;
1344 if (err * err < err2) {
1346 translate[i] = entry;
1350 for (int row = rect.top; row < rect.bottom; row ++) {
1351 FX_LPBYTE scan = m_pBuffer + row * m_Pitch;
1352 FX_LPBYTE next_scan = m_pBuffer + (row + 1) * m_Pitch;
1353 for (int col = rect.left; col < rect.right; col ++) {
1354 int src_pixel = scan[col];
1355 int dest_pixel = translate[src_pixel];
1356 scan[col] = (FX_BYTE)dest_pixel;
1357 int error = -dest_pixel + src_pixel;
1358 if (col < rect.right - 1) {
1359 int src = scan[col + 1];
1360 src += error * 7 / 16;
1362 scan[col + 1] = 255;
1363 } else if (src < 0) {
1366 scan[col + 1] = src;
1369 if (col < rect.right - 1 && row < rect.bottom - 1) {
1370 int src = next_scan[col + 1];
1371 src += error * 1 / 16;
1373 next_scan[col + 1] = 255;
1374 } else if (src < 0) {
1375 next_scan[col + 1] = 0;
1377 next_scan[col + 1] = src;
1380 if (row < rect.bottom - 1) {
1381 int src = next_scan[col];
1382 src += error * 5 / 16;
1384 next_scan[col] = 255;
1385 } else if (src < 0) {
1388 next_scan[col] = src;
1391 if (col > rect.left && row < rect.bottom - 1) {
1392 int src = next_scan[col - 1];
1393 src += error * 3 / 16;
1395 next_scan[col - 1] = 255;
1396 } else if (src < 0) {
1397 next_scan[col - 1] = 0;
1399 next_scan[col - 1] = src;
1406 CFX_DIBitmap* CFX_DIBSource::FlipImage(FX_BOOL bXFlip, FX_BOOL bYFlip) const
1408 CFX_DIBitmap* pFlipped = FX_NEW CFX_DIBitmap;
1412 if (!pFlipped->Create(m_Width, m_Height, GetFormat())) {
1416 pFlipped->CopyPalette(m_pPalette);
1417 FX_LPBYTE pDestBuffer = pFlipped->GetBuffer();
1418 int Bpp = m_bpp / 8;
1419 for (int row = 0; row < m_Height; row ++) {
1420 FX_LPCBYTE src_scan = GetScanline(row);
1421 FX_LPBYTE dest_scan = pDestBuffer + m_Pitch * (bYFlip ? (m_Height - row - 1) : row);
1423 FXSYS_memcpy32(dest_scan, src_scan, m_Pitch);
1427 FXSYS_memset32(dest_scan, 0, m_Pitch);
1428 for (int col = 0; col < m_Width; col ++)
1429 if (src_scan[col / 8] & (1 << (7 - col % 8))) {
1430 int dest_col = m_Width - col - 1;
1431 dest_scan[dest_col / 8] |= (1 << (7 - dest_col % 8));
1434 dest_scan += (m_Width - 1) * Bpp;
1436 for (int col = 0; col < m_Width; col ++) {
1437 *dest_scan = *src_scan;
1441 } else if (Bpp == 3) {
1442 for (int col = 0; col < m_Width; col ++) {
1443 dest_scan[0] = src_scan[0];
1444 dest_scan[1] = src_scan[1];
1445 dest_scan[2] = src_scan[2];
1451 for (int col = 0; col < m_Width; col ++) {
1452 *(FX_DWORD*)dest_scan = *(FX_DWORD*)src_scan;
1460 pDestBuffer = pFlipped->m_pAlphaMask->GetBuffer();
1461 FX_DWORD dest_pitch = pFlipped->m_pAlphaMask->GetPitch();
1462 for (int row = 0; row < m_Height; row ++) {
1463 FX_LPCBYTE src_scan = m_pAlphaMask->GetScanline(row);
1464 FX_LPBYTE dest_scan = pDestBuffer + dest_pitch * (bYFlip ? (m_Height - row - 1) : row);
1466 FXSYS_memcpy32(dest_scan, src_scan, dest_pitch);
1469 dest_scan += (m_Width - 1);
1470 for (int col = 0; col < m_Width; col ++) {
1471 *dest_scan = *src_scan;
1479 CFX_DIBExtractor::CFX_DIBExtractor(const CFX_DIBSource* pSrc)
1482 if (pSrc->GetBuffer() == NULL) {
1483 m_pBitmap = pSrc->Clone();
1485 m_pBitmap = FX_NEW CFX_DIBitmap;
1489 if (!m_pBitmap->Create(pSrc->GetWidth(), pSrc->GetHeight(), pSrc->GetFormat(), pSrc->GetBuffer())) {
1494 m_pBitmap->CopyPalette(pSrc->GetPalette());
1495 m_pBitmap->CopyAlphaMask(pSrc->m_pAlphaMask);
1498 CFX_DIBExtractor::~CFX_DIBExtractor()
1504 CFX_FilteredDIB::CFX_FilteredDIB()
1509 CFX_FilteredDIB::~CFX_FilteredDIB()
1511 if (m_pSrc && m_bAutoDropSrc) {
1515 FX_Free(m_pScanline);
1518 void CFX_FilteredDIB::LoadSrc(const CFX_DIBSource* pSrc, FX_BOOL bAutoDropSrc)
1521 m_bAutoDropSrc = bAutoDropSrc;
1522 m_Width = pSrc->GetWidth();
1523 m_Height = pSrc->GetHeight();
1524 FXDIB_Format format = GetDestFormat();
1525 m_bpp = (FX_BYTE)format;
1526 m_AlphaFlag = (FX_BYTE)(format >> 8);
1527 m_Pitch = (m_Width * (format & 0xff) + 31) / 32 * 4;
1528 m_pPalette = GetDestPalette();
1529 m_pScanline = FX_Alloc(FX_BYTE, m_Pitch);
1531 FX_LPCBYTE CFX_FilteredDIB::GetScanline(int line) const
1533 TranslateScanline(m_pScanline, m_pSrc->GetScanline(line));
1536 void CFX_FilteredDIB::DownSampleScanline(int line, FX_LPBYTE dest_scan, int dest_bpp,
1537 int dest_width, FX_BOOL bFlipX, int clip_left, int clip_width) const
1539 m_pSrc->DownSampleScanline(line, dest_scan, dest_bpp, dest_width, bFlipX, clip_left, clip_width);
1540 TranslateDownSamples(dest_scan, dest_scan, clip_width, dest_bpp);
1542 CFX_ImageRenderer::CFX_ImageRenderer()
1545 m_pTransformer = NULL;
1546 m_bRgbByteOrder = FALSE;
1547 m_BlendType = FXDIB_BLEND_NORMAL;
1549 CFX_ImageRenderer::~CFX_ImageRenderer()
1551 if (m_pTransformer) {
1552 delete m_pTransformer;
1555 extern FX_RECT _FXDIB_SwapClipBox(FX_RECT& clip, int width, int height, FX_BOOL bFlipX, FX_BOOL bFlipY);
1556 FX_BOOL CFX_ImageRenderer::Start(CFX_DIBitmap* pDevice, const CFX_ClipRgn* pClipRgn,
1557 const CFX_DIBSource* pSource, int bitmap_alpha,
1558 FX_DWORD mask_color, const CFX_AffineMatrix* pMatrix,
1559 FX_DWORD dib_flags, FX_BOOL bRgbByteOrder,
1560 int alpha_flag, void* pIccTransform, int blend_type)
1562 m_Matrix = *pMatrix;
1563 CFX_FloatRect image_rect_f = m_Matrix.GetUnitRect();
1564 FX_RECT image_rect = image_rect_f.GetOutterRect();
1565 m_ClipBox = pClipRgn ? pClipRgn->GetBox() : FX_RECT(0, 0, pDevice->GetWidth(), pDevice->GetHeight());
1566 m_ClipBox.Intersect(image_rect);
1567 if (m_ClipBox.IsEmpty()) {
1570 m_pDevice = pDevice;
1571 m_pClipRgn = pClipRgn;
1572 m_MaskColor = mask_color;
1573 m_BitmapAlpha = bitmap_alpha;
1574 m_Matrix = *pMatrix;
1575 m_Flags = dib_flags;
1576 m_AlphaFlag = alpha_flag;
1577 m_pIccTransform = pIccTransform;
1578 m_bRgbByteOrder = bRgbByteOrder;
1579 m_BlendType = blend_type;
1581 if ((FXSYS_fabs(m_Matrix.b) >= 0.5f || m_Matrix.a == 0) ||
1582 (FXSYS_fabs(m_Matrix.c) >= 0.5f || m_Matrix.d == 0) ) {
1583 if (FXSYS_fabs(m_Matrix.a) < FXSYS_fabs(m_Matrix.b) / 20 && FXSYS_fabs(m_Matrix.d) < FXSYS_fabs(m_Matrix.c) / 20 &&
1584 FXSYS_fabs(m_Matrix.a) < 0.5f && FXSYS_fabs(m_Matrix.d) < 0.5f) {
1585 int dest_width = image_rect.Width();
1586 int dest_height = image_rect.Height();
1587 FX_RECT bitmap_clip = m_ClipBox;
1588 bitmap_clip.Offset(-image_rect.left, -image_rect.top);
1589 bitmap_clip = _FXDIB_SwapClipBox(bitmap_clip, dest_width, dest_height, m_Matrix.c > 0, m_Matrix.b < 0);
1590 m_Composer.Compose(pDevice, pClipRgn, bitmap_alpha, mask_color, m_ClipBox, TRUE,
1591 m_Matrix.c > 0, m_Matrix.b < 0, m_bRgbByteOrder, alpha_flag, pIccTransform, m_BlendType);
1592 if (!m_Stretcher.Start(&m_Composer, pSource, dest_height, dest_width, bitmap_clip, dib_flags)) {
1599 m_pTransformer = FX_NEW CFX_ImageTransformer;
1600 if (!m_pTransformer) {
1603 m_pTransformer->Start(pSource, &m_Matrix, dib_flags, &m_ClipBox);
1606 int dest_width = image_rect.Width();
1607 if (m_Matrix.a < 0) {
1608 dest_width = -dest_width;
1610 int dest_height = image_rect.Height();
1611 if (m_Matrix.d > 0) {
1612 dest_height = -dest_height;
1614 if (dest_width == 0 || dest_height == 0) {
1617 FX_RECT bitmap_clip = m_ClipBox;
1618 bitmap_clip.Offset(-image_rect.left, -image_rect.top);
1619 m_Composer.Compose(pDevice, pClipRgn, bitmap_alpha, mask_color,
1620 m_ClipBox, FALSE, FALSE, FALSE, m_bRgbByteOrder, alpha_flag, pIccTransform, m_BlendType);
1622 ret = m_Stretcher.Start(&m_Composer, pSource, dest_width, dest_height, bitmap_clip, dib_flags);
1625 FX_BOOL CFX_ImageRenderer::Continue(IFX_Pause* pPause)
1627 if (m_Status == 1) {
1628 return m_Stretcher.Continue(pPause);
1629 } else if (m_Status == 2) {
1630 if (m_pTransformer->Continue(pPause)) {
1633 CFX_DIBitmap* pBitmap = m_pTransformer->m_Storer.Detach();
1634 if (pBitmap == NULL) {
1637 if (pBitmap->GetBuffer() == NULL) {
1641 if (pBitmap->IsAlphaMask()) {
1642 if (m_BitmapAlpha != 255) {
1643 if (m_AlphaFlag >> 8) {
1644 m_AlphaFlag = (((FX_BYTE)((m_AlphaFlag & 0xff) * m_BitmapAlpha / 255)) | ((m_AlphaFlag >> 8) << 8));
1646 m_MaskColor = FXARGB_MUL_ALPHA(m_MaskColor, m_BitmapAlpha);
1649 m_pDevice->CompositeMask(m_pTransformer->m_ResultLeft, m_pTransformer->m_ResultTop,
1650 pBitmap->GetWidth(), pBitmap->GetHeight(), pBitmap, m_MaskColor,
1651 0, 0, m_BlendType, m_pClipRgn, m_bRgbByteOrder, m_AlphaFlag, m_pIccTransform);
1653 if (m_BitmapAlpha != 255) {
1654 pBitmap->MultiplyAlpha(m_BitmapAlpha);
1656 m_pDevice->CompositeBitmap(m_pTransformer->m_ResultLeft, m_pTransformer->m_ResultTop,
1657 pBitmap->GetWidth(), pBitmap->GetHeight(), pBitmap, 0, 0, m_BlendType, m_pClipRgn, m_bRgbByteOrder, m_pIccTransform);
1664 CFX_BitmapStorer::CFX_BitmapStorer()
1668 CFX_BitmapStorer::~CFX_BitmapStorer()
1674 CFX_DIBitmap* CFX_BitmapStorer::Detach()
1676 CFX_DIBitmap* pBitmap = m_pBitmap;
1680 void CFX_BitmapStorer::Replace(CFX_DIBitmap* pBitmap)
1685 m_pBitmap = pBitmap;
1687 void CFX_BitmapStorer::ComposeScanline(int line, FX_LPCBYTE scanline, FX_LPCBYTE scan_extra_alpha)
1689 FX_LPBYTE dest_buf = (FX_LPBYTE)m_pBitmap->GetScanline(line);
1690 FX_LPBYTE dest_alpha_buf = m_pBitmap->m_pAlphaMask ?
1691 (FX_LPBYTE)m_pBitmap->m_pAlphaMask->GetScanline(line) : NULL;
1693 FXSYS_memcpy32(dest_buf, scanline, m_pBitmap->GetPitch());
1695 if (dest_alpha_buf) {
1696 FXSYS_memcpy32(dest_alpha_buf, scan_extra_alpha, m_pBitmap->m_pAlphaMask->GetPitch());
1699 FX_BOOL CFX_BitmapStorer::SetInfo(int width, int height, FXDIB_Format src_format, FX_DWORD* pSrcPalette)
1701 m_pBitmap = FX_NEW CFX_DIBitmap;
1705 if (!m_pBitmap->Create(width, height, src_format)) {
1711 m_pBitmap->CopyPalette(pSrcPalette);