Home | History | Annotate | Download | only in dib
      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.
      4 
      5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
      6 
      7 #include "../../../include/fxge/fx_dib.h"
      8 #include "dib_int.h"
      9 int SDP_Table[513] = {
     10     256, 256, 256, 256, 256, 256, 256, 256, 256, 255, 255, 255, 255, 255, 255, 254, 254, 254, 254,
     11     253, 253, 253, 252, 252, 252, 251, 251, 251, 250, 250, 249, 249, 249, 248, 248, 247, 247, 246,
     12     246, 245, 244, 244, 243, 243, 242, 242, 241, 240, 240, 239, 238, 238, 237, 236, 236, 235, 234,
     13     233, 233, 232, 231, 230, 230, 229, 228, 227, 226, 226, 225, 224, 223, 222, 221, 220, 219, 218,
     14     218, 217, 216, 215, 214, 213, 212, 211, 210, 209, 208, 207, 206, 205, 204, 203, 202, 201, 200,
     15     199, 198, 196, 195, 194, 193, 192, 191, 190, 189, 188, 186, 185, 184, 183, 182, 181, 179, 178,
     16     177, 176, 175, 173, 172, 171, 170, 169, 167, 166, 165, 164, 162, 161, 160, 159, 157, 156, 155,
     17     154, 152, 151, 150, 149, 147, 146, 145, 143, 142, 141, 140, 138, 137, 136, 134, 133, 132, 130,
     18     129, 128, 126, 125, 124, 122, 121, 120, 119, 117, 116, 115, 113, 112, 111, 109, 108, 107, 105,
     19     104, 103, 101, 100, 99, 97, 96, 95, 93, 92, 91, 89, 88, 87, 85, 84, 83, 81, 80, 79, 77, 76, 75,
     20     73, 72, 71, 69, 68, 67, 66, 64, 63, 62, 60, 59, 58, 57, 55, 54, 53, 52, 50, 49, 48, 47, 45, 44,
     21     43, 42, 40, 39, 38, 37, 36, 34, 33, 32, 31, 30, 28, 27, 26, 25, 24, 23, 21, 20, 19, 18, 17, 16,
     22     15, 14, 13, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 0, -1, -2, -3, -4, -5, -6, -7, -7, -8, -9, -10,
     23     -11, -12, -12, -13, -14, -15, -15, -16, -17, -17, -18, -19, -19, -20, -21, -21, -22, -22, -23, -24,
     24     -24, -25, -25, -26, -26, -27, -27, -27, -28, -28, -29, -29, -30, -30, -30, -31, -31, -31, -32, -32,
     25     -32, -33, -33, -33, -33, -34, -34, -34, -34, -35, -35, -35, -35, -35, -36, -36, -36, -36, -36, -36,
     26     -36, -36, -36, -37, -37, -37, -37, -37, -37, -37, -37, -37, -37, -37, -37, -37, -37, -37, -37, -37,
     27     -37, -37, -37, -37, -36, -36, -36, -36, -36, -36, -36, -36, -36, -35, -35, -35, -35, -35, -35, -34,
     28     -34, -34, -34, -34, -33, -33, -33, -33, -33, -32, -32, -32, -32, -31, -31, -31, -31, -30, -30, -30,
     29     -30, -29, -29, -29, -29, -28, -28, -28, -27, -27, -27, -27, -26, -26, -26, -25, -25, -25, -24, -24,
     30     -24, -23, -23, -23, -22, -22, -22, -22, -21, -21, -21, -20, -20, -20, -19, -19, -19, -18, -18, -18,
     31     -17, -17, -17, -16, -16, -16, -15, -15, -15, -14, -14, -14, -13, -13, -13, -12, -12, -12, -11, -11,
     32     -11, -10, -10, -10, -9, -9, -9, -9, -8, -8, -8, -7, -7, -7, -7, -6, -6, -6, -6, -5, -5, -5, -5, -4,
     33     -4, -4, -4, -3, -3, -3, -3, -3, -2, -2, -2, -2, -2, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0,
     34     0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
     35 };
     36 class CFX_BilinearMatrix : public CPDF_FixedMatrix
     37 {
     38 public:
     39     CFX_BilinearMatrix(const CFX_AffineMatrix& src, int bits): CPDF_FixedMatrix(src, bits)
     40     {}
     41     inline void	Transform(int x, int y, int& x1, int& y1, int&res_x, int&res_y)
     42     {
     43         x1 = a * x + c * y + e + base / 2;
     44         y1 = b * x + d * y + f + base / 2;
     45         res_x = x1 % base;
     46         res_y = y1 % base;
     47         if (res_x < 0 && res_x > -base) {
     48             res_x = base + res_x;
     49         }
     50         if (res_y < 0 && res_x > -base) {
     51             res_y = base + res_y;
     52         }
     53         x1 /= base;
     54         y1 /= base;
     55     }
     56 };
     57 CFX_DIBitmap* CFX_DIBSource::SwapXY(FX_BOOL bXFlip, FX_BOOL bYFlip, const FX_RECT* pDestClip) const
     58 {
     59     FX_RECT dest_clip(0, 0, m_Height, m_Width);
     60     if (pDestClip) {
     61         dest_clip.Intersect(*pDestClip);
     62     }
     63     if (dest_clip.IsEmpty()) {
     64         return NULL;
     65     }
     66     CFX_DIBitmap* pTransBitmap = FX_NEW CFX_DIBitmap;
     67     if (!pTransBitmap) {
     68         return NULL;
     69     }
     70     int result_height = dest_clip.Height(), result_width = dest_clip.Width();
     71     if (!pTransBitmap->Create(result_width, result_height, GetFormat())) {
     72         delete pTransBitmap;
     73         return NULL;
     74     }
     75     pTransBitmap->CopyPalette(m_pPalette);
     76     int src_pitch = m_Pitch;
     77     int dest_pitch = pTransBitmap->GetPitch();
     78     FX_LPBYTE dest_buf = pTransBitmap->GetBuffer();
     79     int row_start = bXFlip ? m_Height - dest_clip.right : dest_clip.left;
     80     int row_end = bXFlip ? m_Height - dest_clip.left : dest_clip.right;
     81     int col_start = bYFlip ? m_Width - dest_clip.bottom : dest_clip.top;
     82     int col_end = bYFlip ? m_Width - dest_clip.top : dest_clip.bottom;
     83     if (GetBPP() == 1) {
     84         FXSYS_memset8(dest_buf, 0xff, dest_pitch * result_height);
     85         for (int row = row_start; row < row_end; row ++) {
     86             FX_LPCBYTE src_scan = GetScanline(row);
     87             int dest_col = (bXFlip ? dest_clip.right - (row - row_start) - 1 : row) - dest_clip.left;
     88             FX_LPBYTE dest_scan = dest_buf;
     89             if (bYFlip) {
     90                 dest_scan += (result_height - 1) * dest_pitch;
     91             }
     92             int dest_step = bYFlip ? -dest_pitch : dest_pitch;
     93             for (int col = col_start; col < col_end; col ++) {
     94                 if (!(src_scan[col / 8] & (1 << (7 - col % 8)))) {
     95                     dest_scan[dest_col / 8] &= ~(1 << (7 - dest_col % 8));
     96                 }
     97                 dest_scan += dest_step;
     98             }
     99         }
    100     } else {
    101         int nBytes = GetBPP() / 8;
    102         int dest_step = bYFlip ? -dest_pitch : dest_pitch;
    103         if (nBytes == 3) {
    104             dest_step -= 2;
    105         }
    106         for (int row = row_start; row < row_end; row ++) {
    107             int dest_col = (bXFlip ? dest_clip.right - (row - row_start) - 1 : row) - dest_clip.left;
    108             FX_LPBYTE dest_scan = dest_buf + dest_col * nBytes;
    109             if (bYFlip) {
    110                 dest_scan += (result_height - 1) * dest_pitch;
    111             }
    112             if (nBytes == 4) {
    113                 FX_DWORD* src_scan = (FX_DWORD*)GetScanline(row) + col_start;
    114                 for (int col = col_start; col < col_end; col ++) {
    115                     *(FX_DWORD*)dest_scan = *src_scan++;
    116                     dest_scan += dest_step;
    117                 }
    118             } else {
    119                 FX_LPCBYTE src_scan = GetScanline(row) + col_start * nBytes;
    120                 if (nBytes == 1)
    121                     for (int col = col_start; col < col_end; col ++) {
    122                         *dest_scan = *src_scan++;
    123                         dest_scan += dest_step;
    124                     }
    125                 else
    126                     for (int col = col_start; col < col_end; col ++) {
    127                         *dest_scan++ = *src_scan++;
    128                         *dest_scan++ = *src_scan++;
    129                         *dest_scan = *src_scan++;
    130                         dest_scan += dest_step;
    131                     }
    132             }
    133         }
    134     }
    135     if (m_pAlphaMask) {
    136         src_pitch = m_pAlphaMask->m_Pitch;
    137         dest_pitch = pTransBitmap->m_pAlphaMask->GetPitch();
    138         dest_buf = pTransBitmap->m_pAlphaMask->GetBuffer();
    139         int dest_step = bYFlip ? -dest_pitch : dest_pitch;
    140         for (int row = row_start; row < row_end; row ++) {
    141             int dest_col = (bXFlip ? dest_clip.right - (row - row_start) - 1 : row) - dest_clip.left;
    142             FX_LPBYTE dest_scan = dest_buf + dest_col;
    143             if (bYFlip) {
    144                 dest_scan += (result_height - 1) * dest_pitch;
    145             }
    146             FX_LPCBYTE src_scan = m_pAlphaMask->GetScanline(row) + col_start;
    147             for (int col = col_start; col < col_end; col ++) {
    148                 *dest_scan = *src_scan++;
    149                 dest_scan += dest_step;
    150             }
    151         }
    152     }
    153     return pTransBitmap;
    154 }
    155 #define FIX16_005 0.05f
    156 FX_RECT _FXDIB_SwapClipBox(FX_RECT& clip, int width, int height, FX_BOOL bFlipX, FX_BOOL bFlipY)
    157 {
    158     FX_RECT rect;
    159     if (bFlipY) {
    160         rect.left = height - clip.top;
    161         rect.right = height - clip.bottom;
    162     } else {
    163         rect.left = clip.top;
    164         rect.right = clip.bottom;
    165     }
    166     if (bFlipX) {
    167         rect.top = width - clip.left;
    168         rect.bottom = width - clip.right;
    169     } else {
    170         rect.top = clip.left;
    171         rect.bottom = clip.right;
    172     }
    173     rect.Normalize();
    174     return rect;
    175 }
    176 CFX_DIBitmap* CFX_DIBSource::TransformTo(const CFX_AffineMatrix* pDestMatrix, int& result_left, int& result_top,
    177         FX_DWORD flags, const FX_RECT* pDestClip) const
    178 {
    179     CFX_ImageTransformer transformer;
    180     transformer.Start(this, pDestMatrix, flags, pDestClip);
    181     transformer.Continue(NULL);
    182     result_left = transformer.m_ResultLeft;
    183     result_top = transformer.m_ResultTop;
    184     CFX_DIBitmap* pTransformed = transformer.m_Storer.Detach();
    185     return pTransformed;
    186 }
    187 CFX_DIBitmap* CFX_DIBSource::StretchTo(int dest_width, int dest_height, FX_DWORD flags, const FX_RECT* pClip) const
    188 {
    189     FX_RECT clip_rect(0, 0, FXSYS_abs(dest_width), FXSYS_abs(dest_height));
    190     if (pClip) {
    191         clip_rect.Intersect(*pClip);
    192     }
    193     if (clip_rect.IsEmpty()) {
    194         return NULL;
    195     }
    196     if (dest_width == m_Width && dest_height == m_Height) {
    197         return Clone(&clip_rect);
    198     }
    199     CFX_ImageStretcher stretcher;
    200     CFX_BitmapStorer storer;
    201     if (stretcher.Start(&storer, this, dest_width, dest_height, clip_rect, flags)) {
    202         stretcher.Continue(NULL);
    203     }
    204     return storer.Detach();
    205 }
    206 CFX_ImageTransformer::CFX_ImageTransformer()
    207 {
    208     m_Status = 0;
    209     m_pMatrix = NULL;
    210 }
    211 CFX_ImageTransformer::~CFX_ImageTransformer()
    212 {
    213 }
    214 FX_BOOL CFX_ImageTransformer::Start(const CFX_DIBSource* pSrc, const CFX_AffineMatrix* pDestMatrix, int flags, const FX_RECT* pDestClip)
    215 {
    216     m_pMatrix = (CFX_AffineMatrix*)pDestMatrix;
    217     CFX_FloatRect unit_rect = pDestMatrix->GetUnitRect();
    218     FX_RECT result_rect = unit_rect.GetClosestRect();
    219     FX_RECT result_clip = result_rect;
    220     if (pDestClip) {
    221         result_clip.Intersect(*pDestClip);
    222     }
    223     if (result_clip.IsEmpty()) {
    224         return FALSE;
    225     }
    226     m_ResultLeft = result_clip.left;
    227     m_ResultTop = result_clip.top;
    228     m_ResultWidth = result_clip.Width();
    229     m_ResultHeight = result_clip.Height();
    230     m_Flags = flags;
    231     if (FXSYS_fabs(pDestMatrix->a) < FXSYS_fabs(pDestMatrix->b) / 20 &&
    232             FXSYS_fabs(pDestMatrix->d) < FXSYS_fabs(pDestMatrix->c) / 20 &&
    233             FXSYS_fabs(pDestMatrix->a) < 0.5f && FXSYS_fabs(pDestMatrix->d) < 0.5f) {
    234         int dest_width = result_rect.Width();
    235         int dest_height = result_rect.Height();
    236         result_clip.Offset(-result_rect.left, -result_rect.top);
    237         result_clip = _FXDIB_SwapClipBox(result_clip, dest_width, dest_height, pDestMatrix->c > 0, pDestMatrix->b < 0);
    238         m_Stretcher.Start(&m_Storer, pSrc, dest_height, dest_width, result_clip, flags);
    239         m_Status = 1;
    240         return TRUE;
    241     }
    242     if (FXSYS_fabs(pDestMatrix->b) < FIX16_005 && FXSYS_fabs(pDestMatrix->c) < FIX16_005) {
    243         int dest_width = pDestMatrix->a > 0 ? (int)FXSYS_ceil(pDestMatrix->a) : (int)FXSYS_floor(pDestMatrix->a);
    244         int dest_height = pDestMatrix->d > 0 ? (int) - FXSYS_ceil(pDestMatrix->d) : (int) - FXSYS_floor(pDestMatrix->d);
    245         result_clip.Offset(-result_rect.left, -result_rect.top);
    246         m_Stretcher.Start(&m_Storer, pSrc, dest_width, dest_height, result_clip, flags);
    247         m_Status = 2;
    248         return TRUE;
    249     }
    250     int stretch_width = (int)FXSYS_ceil(FXSYS_sqrt2(pDestMatrix->a, pDestMatrix->b));
    251     int stretch_height = (int)FXSYS_ceil(FXSYS_sqrt2(pDestMatrix->c, pDestMatrix->d));
    252     CFX_AffineMatrix stretch2dest(1.0f, 0.0f, 0.0f, -1.0f, 0.0f, (FX_FLOAT)(stretch_height));
    253     stretch2dest.Concat(pDestMatrix->a / stretch_width, pDestMatrix->b / stretch_width,
    254                         pDestMatrix->c / stretch_height, pDestMatrix->d / stretch_height, pDestMatrix->e, pDestMatrix->f);
    255     m_dest2stretch.SetReverse(stretch2dest);
    256     CFX_FloatRect clip_rect_f(result_clip);
    257     clip_rect_f.Transform(&m_dest2stretch);
    258     m_StretchClip = clip_rect_f.GetOutterRect();
    259     m_StretchClip.Intersect(0, 0, stretch_width, stretch_height);
    260     m_Stretcher.Start(&m_Storer, pSrc, stretch_width, stretch_height, m_StretchClip, flags);
    261     m_Status = 3;
    262     return TRUE;
    263 }
    264 FX_BYTE _bilinear_interpol(FX_LPCBYTE buf, int row_offset_l, int row_offset_r,
    265                            int src_col_l, int src_col_r, int res_x, int res_y,
    266                            int bpp, int c_offset)
    267 {
    268     int i_resx = 255 - res_x;
    269     int col_bpp_l = src_col_l * bpp;
    270     int col_bpp_r = src_col_r * bpp;
    271     FX_LPCBYTE buf_u = buf + row_offset_l + c_offset;
    272     FX_LPCBYTE buf_d = buf + row_offset_r + c_offset;
    273     FX_LPCBYTE src_pos0 = buf_u + col_bpp_l;
    274     FX_LPCBYTE src_pos1 = buf_u + col_bpp_r;
    275     FX_LPCBYTE src_pos2 = buf_d + col_bpp_l;
    276     FX_LPCBYTE src_pos3 = buf_d + col_bpp_r;
    277     FX_BYTE r_pos_0 = (*src_pos0 * i_resx + *src_pos1 * res_x) >> 8;
    278     FX_BYTE r_pos_1 = (*src_pos2 * i_resx + *src_pos3 * res_x) >> 8;
    279     return (r_pos_0 * (255 - res_y) + r_pos_1 * res_y) >> 8;
    280 }
    281 FX_BYTE _bicubic_interpol(FX_LPCBYTE buf, int pitch, int pos_pixel[], int u_w[], int v_w[], int res_x, int res_y,
    282                           int bpp, int c_offset)
    283 {
    284     int s_result = 0;
    285     for (int i = 0; i < 4; i ++) {
    286         int a_result = 0;
    287         for (int j = 0; j < 4; j ++) {
    288             a_result += u_w[j] * (*(FX_BYTE*)(buf + pos_pixel[i + 4] * pitch + pos_pixel[j] * bpp + c_offset));
    289         }
    290         s_result += a_result * v_w[i];
    291     }
    292     s_result >>= 16;
    293     return (FX_BYTE)(s_result < 0 ? 0 : s_result > 255 ? 255 : s_result);
    294 }
    295 void _bicubic_get_pos_weight(int pos_pixel[], int u_w[], int v_w[], int src_col_l, int src_row_l,
    296                              int res_x, int res_y, int stretch_width, int stretch_height)
    297 {
    298     pos_pixel[0] = src_col_l - 1;
    299     pos_pixel[1] = src_col_l;
    300     pos_pixel[2] = src_col_l + 1;
    301     pos_pixel[3] = src_col_l + 2;
    302     pos_pixel[4] = src_row_l - 1;
    303     pos_pixel[5] = src_row_l;
    304     pos_pixel[6] = src_row_l + 1;
    305     pos_pixel[7] = src_row_l + 2;
    306     for (int i = 0 ; i < 4; i ++) {
    307         if (pos_pixel[i] < 0) {
    308             pos_pixel[i] = 0;
    309         }
    310         if (pos_pixel[i] >= stretch_width) {
    311             pos_pixel[i] = stretch_width - 1;
    312         }
    313         if (pos_pixel[i + 4] < 0) {
    314             pos_pixel[i + 4] = 0;
    315         }
    316         if (pos_pixel[i + 4] >= stretch_height) {
    317             pos_pixel[i + 4] = stretch_height - 1;
    318         }
    319     }
    320     u_w[0] = SDP_Table[256 + res_x];
    321     u_w[1] = SDP_Table[res_x];
    322     u_w[2] = SDP_Table[256 - res_x];
    323     u_w[3] = SDP_Table[512 - res_x];
    324     v_w[0] = SDP_Table[256 + res_y];
    325     v_w[1] = SDP_Table[res_y];
    326     v_w[2] = SDP_Table[256 - res_y];
    327     v_w[3] = SDP_Table[512 - res_y];
    328 }
    329 FXDIB_Format _GetTransformedFormat(const CFX_DIBSource* pDrc)
    330 {
    331     FXDIB_Format format = pDrc->GetFormat();
    332     if (pDrc->IsAlphaMask()) {
    333         format = FXDIB_8bppMask;
    334     } else if (format >= 1025) {
    335         format = FXDIB_Cmyka;
    336     } else if (format <= 32 || format == FXDIB_Argb) {
    337         format = FXDIB_Argb;
    338     } else {
    339         format = FXDIB_Rgba;
    340     }
    341     return format;
    342 }
    343 FX_BOOL CFX_ImageTransformer::Continue(IFX_Pause* pPause)
    344 {
    345     if (m_Status == 1) {
    346         if (m_Stretcher.Continue(pPause)) {
    347             return TRUE;
    348         }
    349         if (m_Storer.GetBitmap()) {
    350             m_Storer.Replace(m_Storer.GetBitmap()->SwapXY(m_pMatrix->c > 0, m_pMatrix->b < 0));
    351         }
    352         return FALSE;
    353     } else if (m_Status == 2) {
    354         return m_Stretcher.Continue(pPause);
    355     } else if (m_Status != 3) {
    356         return FALSE;
    357     }
    358     if (m_Stretcher.Continue(pPause)) {
    359         return TRUE;
    360     }
    361     int stretch_width = m_StretchClip.Width();
    362     int stretch_height = m_StretchClip.Height();
    363     if (m_Storer.GetBitmap() == NULL) {
    364         return FALSE;
    365     }
    366     FX_LPCBYTE stretch_buf = m_Storer.GetBitmap()->GetBuffer();
    367     FX_LPCBYTE stretch_buf_mask = NULL;
    368     if (m_Storer.GetBitmap()->m_pAlphaMask) {
    369         stretch_buf_mask = m_Storer.GetBitmap()->m_pAlphaMask->GetBuffer();
    370     }
    371     int stretch_pitch = m_Storer.GetBitmap()->GetPitch();
    372     CFX_DIBitmap* pTransformed = FX_NEW CFX_DIBitmap;
    373     if (!pTransformed) {
    374         return FALSE;
    375     }
    376     FXDIB_Format transformF = _GetTransformedFormat(m_Stretcher.m_pSource);
    377     if (!pTransformed->Create(m_ResultWidth, m_ResultHeight, transformF)) {
    378         delete pTransformed;
    379         return FALSE;
    380     }
    381     pTransformed->Clear(0);
    382     if (pTransformed->m_pAlphaMask) {
    383         pTransformed->m_pAlphaMask->Clear(0);
    384     }
    385     CFX_AffineMatrix result2stretch(1.0f, 0.0f, 0.0f, 1.0f, (FX_FLOAT)(m_ResultLeft), (FX_FLOAT)(m_ResultTop));
    386     result2stretch.Concat(m_dest2stretch);
    387     result2stretch.TranslateI(-m_StretchClip.left, -m_StretchClip.top);
    388     if (stretch_buf_mask == NULL && pTransformed->m_pAlphaMask) {
    389         pTransformed->m_pAlphaMask->Clear(0xff000000);
    390     } else if (pTransformed->m_pAlphaMask) {
    391         int stretch_pitch_mask = m_Storer.GetBitmap()->m_pAlphaMask->GetPitch();
    392         if (!(m_Flags & FXDIB_DOWNSAMPLE) && !(m_Flags & FXDIB_BICUBIC_INTERPOL)) {
    393             CFX_BilinearMatrix result2stretch_fix(result2stretch, 8);
    394             for (int row = 0; row < m_ResultHeight; row ++) {
    395                 FX_BYTE* dest_pos_mask = (FX_BYTE*)pTransformed->m_pAlphaMask->GetScanline(row);
    396                 for (int col = 0; col < m_ResultWidth; col ++) {
    397                     int src_col_l, src_row_l, res_x, res_y;
    398                     result2stretch_fix.Transform(col, row, src_col_l, src_row_l, res_x, res_y);
    399                     if (src_col_l >= 0 && src_col_l <= stretch_width && src_row_l >= 0 && src_row_l <= stretch_height) {
    400                         if (src_col_l == stretch_width) {
    401                             src_col_l--;
    402                         }
    403                         if (src_row_l == stretch_height) {
    404                             src_row_l--;
    405                         }
    406                         int src_col_r = src_col_l + 1;
    407                         int src_row_r = src_row_l + 1;
    408                         if (src_col_r == stretch_width) {
    409                             src_col_r--;
    410                         }
    411                         if (src_row_r == stretch_height) {
    412                             src_row_r--;
    413                         }
    414                         int row_offset_l = src_row_l * stretch_pitch_mask;
    415                         int row_offset_r = src_row_r * stretch_pitch_mask;
    416                         *dest_pos_mask = _bilinear_interpol(stretch_buf_mask, row_offset_l, row_offset_r, src_col_l, src_col_r, res_x, res_y, 1, 0);
    417                     }
    418                     dest_pos_mask++;
    419                 }
    420             }
    421         } else if (m_Flags & FXDIB_BICUBIC_INTERPOL) {
    422             CFX_BilinearMatrix result2stretch_fix(result2stretch, 8);
    423             int pos_pixel[8];
    424             for (int row = 0; row < m_ResultHeight; row ++) {
    425                 FX_BYTE* dest_pos_mask = (FX_BYTE*)pTransformed->m_pAlphaMask->GetScanline(row);
    426                 for (int col = 0; col < m_ResultWidth; col ++) {
    427                     int src_col_l, src_row_l, res_x, res_y;
    428                     result2stretch_fix.Transform(col, row, src_col_l, src_row_l, res_x, res_y);
    429                     if (src_col_l >= 0 && src_col_l <= stretch_width && src_row_l >= 0 && src_row_l <= stretch_height) {
    430                         int u_w[4], v_w[4];
    431                         if (src_col_l == stretch_width) {
    432                             src_col_l--;
    433                         }
    434                         if (src_row_l == stretch_height) {
    435                             src_row_l--;
    436                         }
    437                         _bicubic_get_pos_weight(pos_pixel, u_w, v_w, src_col_l, src_row_l, res_x, res_y, stretch_width, stretch_height);
    438                         *dest_pos_mask = _bicubic_interpol(stretch_buf_mask, stretch_pitch_mask, pos_pixel, u_w, v_w, res_x, res_y, 1, 0);
    439                     }
    440                     dest_pos_mask++;
    441                 }
    442             }
    443         } else {
    444             CPDF_FixedMatrix result2stretch_fix(result2stretch, 8);
    445             for (int row = 0; row < m_ResultHeight; row ++) {
    446                 FX_BYTE* dest_pos_mask = (FX_BYTE*)pTransformed->m_pAlphaMask->GetScanline(row);
    447                 for (int col = 0; col < m_ResultWidth; col ++) {
    448                     int src_col, src_row;
    449                     result2stretch_fix.Transform(col, row, src_col, src_row);
    450                     if (src_col >= 0 && src_col <= stretch_width && src_row >= 0 && src_row <= stretch_height) {
    451                         if (src_col == stretch_width) {
    452                             src_col --;
    453                         }
    454                         if (src_row == stretch_height) {
    455                             src_row --;
    456                         }
    457                         *dest_pos_mask = stretch_buf_mask[src_row * stretch_pitch_mask + src_col];
    458                     }
    459                     dest_pos_mask++;
    460                 }
    461             }
    462         }
    463     }
    464     if (m_Storer.GetBitmap()->IsAlphaMask()) {
    465         if (!(m_Flags & FXDIB_DOWNSAMPLE) && !(m_Flags & FXDIB_BICUBIC_INTERPOL)) {
    466             CFX_BilinearMatrix result2stretch_fix(result2stretch, 8);
    467             for (int row = 0; row < m_ResultHeight; row ++) {
    468                 FX_LPBYTE dest_scan = (FX_LPBYTE)pTransformed->GetScanline(row);
    469                 for (int col = 0; col < m_ResultWidth; col ++) {
    470                     int src_col_l, src_row_l, res_x, res_y;
    471                     result2stretch_fix.Transform(col, row, src_col_l, src_row_l, res_x, res_y);
    472                     if (src_col_l >= 0 && src_col_l <= stretch_width && src_row_l >= 0 && src_row_l <= stretch_height) {
    473                         if (src_col_l == stretch_width) {
    474                             src_col_l--;
    475                         }
    476                         if (src_row_l == stretch_height) {
    477                             src_row_l--;
    478                         }
    479                         int src_col_r = src_col_l + 1;
    480                         int src_row_r = src_row_l + 1;
    481                         if (src_col_r == stretch_width) {
    482                             src_col_r--;
    483                         }
    484                         if (src_row_r == stretch_height) {
    485                             src_row_r--;
    486                         }
    487                         int row_offset_l = src_row_l * stretch_pitch;
    488                         int row_offset_r = src_row_r * stretch_pitch;
    489                         *dest_scan = _bilinear_interpol(stretch_buf, row_offset_l, row_offset_r, src_col_l, src_col_r, res_x, res_y, 1, 0);
    490                     }
    491                     dest_scan ++;
    492                 }
    493             }
    494         } else if (m_Flags & FXDIB_BICUBIC_INTERPOL) {
    495             CFX_BilinearMatrix result2stretch_fix(result2stretch, 8);
    496             int pos_pixel[8];
    497             for (int row = 0; row < m_ResultHeight; row ++) {
    498                 FX_LPBYTE dest_scan = (FX_LPBYTE)pTransformed->GetScanline(row);
    499                 for (int col = 0; col < m_ResultWidth; col ++) {
    500                     int src_col_l, src_row_l, res_x, res_y;
    501                     result2stretch_fix.Transform(col, row, src_col_l, src_row_l, res_x, res_y);
    502                     if (src_col_l >= 0 && src_col_l <= stretch_width && src_row_l >= 0 && src_row_l <= stretch_height) {
    503                         int u_w[4], v_w[4];
    504                         if (src_col_l == stretch_width) {
    505                             src_col_l--;
    506                         }
    507                         if (src_row_l == stretch_height) {
    508                             src_row_l--;
    509                         }
    510                         _bicubic_get_pos_weight(pos_pixel, u_w, v_w, src_col_l, src_row_l, res_x, res_y, stretch_width, stretch_height);
    511                         *dest_scan = _bicubic_interpol(stretch_buf, stretch_pitch, pos_pixel, u_w, v_w, res_x, res_y, 1, 0);
    512                     }
    513                     dest_scan ++;
    514                 }
    515             }
    516         } else {
    517             CPDF_FixedMatrix result2stretch_fix(result2stretch, 8);
    518             for (int row = 0; row < m_ResultHeight; row ++) {
    519                 FX_LPBYTE dest_scan = (FX_LPBYTE)pTransformed->GetScanline(row);
    520                 for (int col = 0; col < m_ResultWidth; col ++) {
    521                     int src_col, src_row;
    522                     result2stretch_fix.Transform(col, row, src_col, src_row);
    523                     if (src_col >= 0 && src_col <= stretch_width && src_row >= 0 && src_row <= stretch_height) {
    524                         if (src_col == stretch_width) {
    525                             src_col --;
    526                         }
    527                         if (src_row == stretch_height) {
    528                             src_row --;
    529                         }
    530                         FX_LPCBYTE src_pixel = stretch_buf + stretch_pitch * src_row + src_col;
    531                         *dest_scan = *src_pixel;
    532                     }
    533                     dest_scan ++;
    534                 }
    535             }
    536         }
    537     } else {
    538         int Bpp = m_Storer.GetBitmap()->GetBPP() / 8;
    539         int destBpp = pTransformed->GetBPP() / 8;
    540         if (Bpp == 1) {
    541             FX_BOOL bHasAlpha = m_Storer.GetBitmap()->HasAlpha();
    542             FX_DWORD argb[256];
    543             FX_ARGB* pPal = m_Storer.GetBitmap()->GetPalette();
    544             if (pPal) {
    545                 for (int i = 0; i < 256; i ++) {
    546                     argb[i] = pPal[i];
    547                 }
    548             } else {
    549                 if (m_Storer.GetBitmap()->IsCmykImage())
    550                     for (int i = 0; i < 256; i ++) {
    551                         argb[i] = 255 - i;
    552                     }
    553                 else
    554                     for (int i = 0; i < 256; i ++) {
    555                         argb[i] = 0xff000000 | (i * 0x010101);
    556                     }
    557             }
    558             if (!(m_Flags & FXDIB_DOWNSAMPLE) && !(m_Flags & FXDIB_BICUBIC_INTERPOL)) {
    559                 CFX_BilinearMatrix result2stretch_fix(result2stretch, 8);
    560                 for (int row = 0; row < m_ResultHeight; row ++) {
    561                     FX_BYTE* dest_pos = (FX_BYTE*)pTransformed->GetScanline(row);
    562                     for (int col = 0; col < m_ResultWidth; col ++) {
    563                         int src_col_l, src_row_l, res_x, res_y;
    564                         result2stretch_fix.Transform(col, row, src_col_l, src_row_l, res_x, res_y);
    565                         if (src_col_l >= 0 && src_col_l <= stretch_width && src_row_l >= 0 && src_row_l <= stretch_height) {
    566                             if (src_col_l == stretch_width) {
    567                                 src_col_l--;
    568                             }
    569                             if (src_row_l == stretch_height) {
    570                                 src_row_l--;
    571                             }
    572                             int src_col_r = src_col_l + 1;
    573                             int src_row_r = src_row_l + 1;
    574                             if (src_col_r == stretch_width) {
    575                                 src_col_r--;
    576                             }
    577                             if (src_row_r == stretch_height) {
    578                                 src_row_r--;
    579                             }
    580                             int row_offset_l = src_row_l * stretch_pitch;
    581                             int row_offset_r = src_row_r * stretch_pitch;
    582                             FX_DWORD r_bgra_cmyk = argb[_bilinear_interpol(stretch_buf, row_offset_l, row_offset_r, src_col_l, src_col_r, res_x, res_y, 1, 0)];
    583                             if (transformF == FXDIB_Rgba) {
    584                                 dest_pos[0] = (FX_BYTE)(r_bgra_cmyk >> 24);
    585                                 dest_pos[1] = (FX_BYTE)(r_bgra_cmyk >> 16);
    586                                 dest_pos[2] = (FX_BYTE)(r_bgra_cmyk >> 8);
    587                             } else {
    588                                 *(FX_DWORD*)dest_pos = r_bgra_cmyk;
    589                             }
    590                         }
    591                         dest_pos += destBpp;
    592                     }
    593                 }
    594             } else if (m_Flags & FXDIB_BICUBIC_INTERPOL) {
    595                 CFX_BilinearMatrix result2stretch_fix(result2stretch, 8);
    596                 int pos_pixel[8];
    597                 for (int row = 0; row < m_ResultHeight; row ++) {
    598                     FX_BYTE* dest_pos = (FX_BYTE*)pTransformed->GetScanline(row);
    599                     for (int col = 0; col < m_ResultWidth; col ++) {
    600                         int src_col_l, src_row_l, res_x, res_y;
    601                         result2stretch_fix.Transform(col, row, src_col_l, src_row_l, res_x, res_y);
    602                         if (src_col_l >= 0 && src_col_l <= stretch_width && src_row_l >= 0 && src_row_l <= stretch_height) {
    603                             int u_w[4], v_w[4];
    604                             if (src_col_l == stretch_width) {
    605                                 src_col_l--;
    606                             }
    607                             if (src_row_l == stretch_height) {
    608                                 src_row_l--;
    609                             }
    610                             _bicubic_get_pos_weight(pos_pixel, u_w, v_w, src_col_l, src_row_l, res_x, res_y, stretch_width, stretch_height);
    611                             FX_DWORD r_bgra_cmyk = argb[_bicubic_interpol(stretch_buf, stretch_pitch, pos_pixel, u_w, v_w, res_x, res_y, 1, 0)];
    612                             if (transformF == FXDIB_Rgba) {
    613                                 dest_pos[0] = (FX_BYTE)(r_bgra_cmyk >> 24);
    614                                 dest_pos[1] = (FX_BYTE)(r_bgra_cmyk >> 16);
    615                                 dest_pos[2] = (FX_BYTE)(r_bgra_cmyk >> 8);
    616                             } else {
    617                                 *(FX_DWORD*)dest_pos = r_bgra_cmyk;
    618                             }
    619                         }
    620                         dest_pos += destBpp;
    621                     }
    622                 }
    623             } else {
    624                 CPDF_FixedMatrix result2stretch_fix(result2stretch, 8);
    625                 for (int row = 0; row < m_ResultHeight; row ++) {
    626                     FX_BYTE* dest_pos = (FX_BYTE*)pTransformed->GetScanline(row);
    627                     for (int col = 0; col < m_ResultWidth; col ++) {
    628                         int src_col, src_row;
    629                         result2stretch_fix.Transform(col, row, src_col, src_row);
    630                         if (src_col >= 0 && src_col <= stretch_width && src_row >= 0 && src_row <= stretch_height) {
    631                             if (src_col == stretch_width) {
    632                                 src_col --;
    633                             }
    634                             if (src_row == stretch_height) {
    635                                 src_row --;
    636                             }
    637                             FX_DWORD r_bgra_cmyk = argb[stretch_buf[src_row * stretch_pitch + src_col]];
    638                             if (transformF == FXDIB_Rgba) {
    639                                 dest_pos[0] = (FX_BYTE)(r_bgra_cmyk >> 24);
    640                                 dest_pos[1] = (FX_BYTE)(r_bgra_cmyk >> 16);
    641                                 dest_pos[2] = (FX_BYTE)(r_bgra_cmyk >> 8);
    642                             } else {
    643                                 *(FX_DWORD*)dest_pos = r_bgra_cmyk;
    644                             }
    645                         }
    646                         dest_pos += destBpp;
    647                     }
    648                 }
    649             }
    650         } else {
    651             FX_BOOL bHasAlpha = m_Storer.GetBitmap()->HasAlpha();
    652             int destBpp = pTransformed->GetBPP() / 8;
    653             if (!(m_Flags & FXDIB_DOWNSAMPLE) && !(m_Flags & FXDIB_BICUBIC_INTERPOL)) {
    654                 CFX_BilinearMatrix result2stretch_fix(result2stretch, 8);
    655                 for (int row = 0; row < m_ResultHeight; row ++) {
    656                     FX_BYTE* dest_pos = (FX_BYTE*)pTransformed->GetScanline(row);
    657                     for (int col = 0; col < m_ResultWidth; col ++) {
    658                         int src_col_l, src_row_l, res_x, res_y, r_pos_k_r = 0;
    659                         result2stretch_fix.Transform(col, row, src_col_l, src_row_l, res_x, res_y);
    660                         if (src_col_l >= 0 && src_col_l <= stretch_width && src_row_l >= 0 && src_row_l <= stretch_height) {
    661                             if (src_col_l == stretch_width) {
    662                                 src_col_l--;
    663                             }
    664                             if (src_row_l == stretch_height) {
    665                                 src_row_l--;
    666                             }
    667                             int src_col_r = src_col_l + 1;
    668                             int src_row_r = src_row_l + 1;
    669                             if (src_col_r == stretch_width) {
    670                                 src_col_r--;
    671                             }
    672                             if (src_row_r == stretch_height) {
    673                                 src_row_r--;
    674                             }
    675                             int row_offset_l = src_row_l * stretch_pitch;
    676                             int row_offset_r = src_row_r * stretch_pitch;
    677                             FX_BYTE r_pos_red_y_r   = _bilinear_interpol(stretch_buf, row_offset_l, row_offset_r, src_col_l, src_col_r, res_x, res_y, Bpp, 2);
    678                             FX_BYTE r_pos_green_m_r = _bilinear_interpol(stretch_buf, row_offset_l, row_offset_r, src_col_l, src_col_r, res_x, res_y, Bpp, 1);
    679                             FX_BYTE r_pos_blue_c_r  = _bilinear_interpol(stretch_buf, row_offset_l, row_offset_r, src_col_l, src_col_r, res_x, res_y, Bpp, 0);
    680                             if (bHasAlpha) {
    681                                 if (transformF != FXDIB_Argb) {
    682                                     if (transformF == FXDIB_Rgba) {
    683                                         dest_pos[0] = r_pos_blue_c_r;
    684                                         dest_pos[1] = r_pos_green_m_r;
    685                                         dest_pos[2] = r_pos_red_y_r;
    686                                     } else {
    687                                         r_pos_k_r = _bilinear_interpol(stretch_buf, row_offset_l, row_offset_r, src_col_l, src_col_r, res_x, res_y, Bpp, 3);
    688                                         *(FX_DWORD*)dest_pos = FXCMYK_TODIB(CmykEncode(r_pos_blue_c_r, r_pos_green_m_r, r_pos_red_y_r, r_pos_k_r));
    689                                     }
    690                                 } else {
    691                                     FX_BYTE r_pos_a_r = _bilinear_interpol(stretch_buf, row_offset_l, row_offset_r, src_col_l, src_col_r, res_x, res_y, Bpp, 3);
    692                                     *(FX_DWORD*)dest_pos = FXARGB_TODIB(FXARGB_MAKE(r_pos_a_r, r_pos_red_y_r, r_pos_green_m_r, r_pos_blue_c_r));
    693                                 }
    694                             } else {
    695                                 r_pos_k_r = 0xff;
    696                                 if (transformF == FXDIB_Cmyka) {
    697                                     r_pos_k_r = _bilinear_interpol(stretch_buf, row_offset_l, row_offset_r, src_col_l, src_col_r, res_x, res_y, Bpp, 3);
    698                                     *(FX_DWORD*)dest_pos = FXCMYK_TODIB(CmykEncode(r_pos_blue_c_r, r_pos_green_m_r, r_pos_red_y_r, r_pos_k_r));
    699                                 } else {
    700                                     *(FX_DWORD*)dest_pos = FXARGB_TODIB(FXARGB_MAKE(r_pos_k_r, r_pos_red_y_r, r_pos_green_m_r, r_pos_blue_c_r));
    701                                 }
    702                             }
    703                         }
    704                         dest_pos += destBpp;
    705                     }
    706                 }
    707             } else if (m_Flags & FXDIB_BICUBIC_INTERPOL) {
    708                 CFX_BilinearMatrix result2stretch_fix(result2stretch, 8);
    709                 int pos_pixel[8];
    710                 for (int row = 0; row < m_ResultHeight; row ++) {
    711                     FX_BYTE* dest_pos = (FX_BYTE*)pTransformed->GetScanline(row);
    712                     for (int col = 0; col < m_ResultWidth; col ++) {
    713                         int src_col_l, src_row_l, res_x, res_y, r_pos_k_r = 0;
    714                         result2stretch_fix.Transform(col, row, src_col_l, src_row_l, res_x, res_y);
    715                         if (src_col_l >= 0 && src_col_l <= stretch_width && src_row_l >= 0 && src_row_l <= stretch_height) {
    716                             int u_w[4], v_w[4];
    717                             if (src_col_l == stretch_width) {
    718                                 src_col_l--;
    719                             }
    720                             if (src_row_l == stretch_height) {
    721                                 src_row_l--;
    722                             }
    723                             _bicubic_get_pos_weight(pos_pixel, u_w, v_w, src_col_l, src_row_l, res_x, res_y, stretch_width, stretch_height);
    724                             FX_BYTE r_pos_red_y_r   = _bicubic_interpol(stretch_buf, stretch_pitch, pos_pixel, u_w, v_w, res_x, res_y, Bpp, 2);
    725                             FX_BYTE r_pos_green_m_r = _bicubic_interpol(stretch_buf, stretch_pitch, pos_pixel, u_w, v_w, res_x, res_y, Bpp, 1);
    726                             FX_BYTE r_pos_blue_c_r  = _bicubic_interpol(stretch_buf, stretch_pitch, pos_pixel, u_w, v_w, res_x, res_y, Bpp, 0);
    727                             if (bHasAlpha) {
    728                                 if (transformF != FXDIB_Argb) {
    729                                     if (transformF == FXDIB_Rgba) {
    730                                         dest_pos[0] = r_pos_blue_c_r;
    731                                         dest_pos[1] = r_pos_green_m_r;
    732                                         dest_pos[2] = r_pos_red_y_r;
    733                                     } else {
    734                                         r_pos_k_r = _bicubic_interpol(stretch_buf, stretch_pitch, pos_pixel, u_w, v_w, res_x, res_y, Bpp, 3);
    735                                         *(FX_DWORD*)dest_pos = FXCMYK_TODIB(CmykEncode(r_pos_blue_c_r, r_pos_green_m_r, r_pos_red_y_r, r_pos_k_r));
    736                                     }
    737                                 } else {
    738                                     FX_BYTE r_pos_a_r = _bicubic_interpol(stretch_buf, stretch_pitch, pos_pixel, u_w, v_w, res_x, res_y, Bpp, 3);
    739                                     *(FX_DWORD*)dest_pos = FXARGB_TODIB(FXARGB_MAKE(r_pos_a_r, r_pos_red_y_r, r_pos_green_m_r, r_pos_blue_c_r));
    740                                 }
    741                             } else {
    742                                 r_pos_k_r = 0xff;
    743                                 if (transformF == FXDIB_Cmyka) {
    744                                     r_pos_k_r = _bicubic_interpol(stretch_buf, stretch_pitch, pos_pixel, u_w, v_w, res_x, res_y, Bpp, 3);
    745                                     *(FX_DWORD*)dest_pos = FXCMYK_TODIB(CmykEncode(r_pos_blue_c_r, r_pos_green_m_r, r_pos_red_y_r, r_pos_k_r));
    746                                 } else {
    747                                     *(FX_DWORD*)dest_pos = FXARGB_TODIB(FXARGB_MAKE(r_pos_k_r, r_pos_red_y_r, r_pos_green_m_r, r_pos_blue_c_r));
    748                                 }
    749                             }
    750                         }
    751                         dest_pos += destBpp;
    752                     }
    753                 }
    754             } else {
    755                 CPDF_FixedMatrix result2stretch_fix(result2stretch, 8);
    756                 for (int row = 0; row < m_ResultHeight; row ++) {
    757                     FX_BYTE* dest_pos = (FX_BYTE*)pTransformed->GetScanline(row);
    758                     for (int col = 0; col < m_ResultWidth; col ++) {
    759                         int src_col, src_row;
    760                         result2stretch_fix.Transform(col, row, src_col, src_row);
    761                         if (src_col >= 0 && src_col <= stretch_width && src_row >= 0 && src_row <= stretch_height) {
    762                             if (src_col == stretch_width) {
    763                                 src_col --;
    764                             }
    765                             if (src_row == stretch_height) {
    766                                 src_row --;
    767                             }
    768                             FX_LPCBYTE src_pos = stretch_buf + src_row * stretch_pitch + src_col * Bpp;
    769                             if (bHasAlpha) {
    770                                 if (transformF != FXDIB_Argb) {
    771                                     if (transformF == FXDIB_Rgba) {
    772                                         dest_pos[0] = src_pos[0];
    773                                         dest_pos[1] = src_pos[1];
    774                                         dest_pos[2] = src_pos[2];
    775                                     } else {
    776                                         *(FX_DWORD*)dest_pos = FXCMYK_TODIB(CmykEncode(src_pos[0], src_pos[1], src_pos[2], src_pos[3]));
    777                                     }
    778                                 } else {
    779                                     *(FX_DWORD*)dest_pos = FXARGB_TODIB(FXARGB_MAKE(src_pos[3], src_pos[2], src_pos[1], src_pos[0]));
    780                                 }
    781                             } else {
    782                                 if (transformF == FXDIB_Cmyka) {
    783                                     *(FX_DWORD*)dest_pos = FXCMYK_TODIB(CmykEncode(src_pos[0], src_pos[1], src_pos[2], src_pos[3]));
    784                                 } else {
    785                                     *(FX_DWORD*)dest_pos = FXARGB_TODIB(FXARGB_MAKE(0xff, src_pos[2], src_pos[1], src_pos[0]));
    786                                 }
    787                             }
    788                         }
    789                         dest_pos += destBpp;
    790                     }
    791                 }
    792             }
    793         }
    794     }
    795     m_Storer.Replace(pTransformed);
    796     return FALSE;
    797 }
    798