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