Ejemplo n.º 1
0
FX_BOOL CFX_ImageTransformer::Continue(IFX_Pause* pPause) {
  if (m_Status == 1) {
    if (m_Stretcher->Continue(pPause))
      return TRUE;

    if (m_Storer.GetBitmap()) {
      std::unique_ptr<CFX_DIBitmap> swapped(
          m_Storer.GetBitmap()->SwapXY(m_pMatrix->c > 0, m_pMatrix->b < 0));
      m_Storer.Replace(std::move(swapped));
    }
    return FALSE;
  }

  if (m_Status == 2)
    return m_Stretcher->Continue(pPause);

  if (m_Status != 3)
    return FALSE;

  if (m_Stretcher->Continue(pPause))
    return TRUE;

  int stretch_width = m_StretchClip.Width();
  int stretch_height = m_StretchClip.Height();
  if (!m_Storer.GetBitmap())
    return FALSE;

  const uint8_t* stretch_buf = m_Storer.GetBitmap()->GetBuffer();
  const uint8_t* stretch_buf_mask = nullptr;
  if (m_Storer.GetBitmap()->m_pAlphaMask)
    stretch_buf_mask = m_Storer.GetBitmap()->m_pAlphaMask->GetBuffer();

  int stretch_pitch = m_Storer.GetBitmap()->GetPitch();
  std::unique_ptr<CFX_DIBitmap> pTransformed(new CFX_DIBitmap);
  FXDIB_Format transformF = GetTransformedFormat(m_Stretcher->source());
  if (!pTransformed->Create(m_result.Width(), m_result.Height(), transformF))
    return FALSE;

  pTransformed->Clear(0);
  if (pTransformed->m_pAlphaMask)
    pTransformed->m_pAlphaMask->Clear(0);

  CFX_Matrix result2stretch(1.0f, 0.0f, 0.0f, 1.0f, (FX_FLOAT)(m_result.left),
                            (FX_FLOAT)(m_result.top));
  result2stretch.Concat(m_dest2stretch);
  result2stretch.TranslateI(-m_StretchClip.left, -m_StretchClip.top);
  if (!stretch_buf_mask && pTransformed->m_pAlphaMask) {
    pTransformed->m_pAlphaMask->Clear(0xff000000);
  } else if (pTransformed->m_pAlphaMask) {
    int stretch_pitch_mask = m_Storer.GetBitmap()->m_pAlphaMask->GetPitch();
    if (!(m_Flags & FXDIB_DOWNSAMPLE) && !(m_Flags & FXDIB_BICUBIC_INTERPOL)) {
      CFX_BilinearMatrix result2stretch_fix(result2stretch, 8);
      for (int row = 0; row < m_result.Height(); row++) {
        uint8_t* dest_pos_mask =
            (uint8_t*)pTransformed->m_pAlphaMask->GetScanline(row);
        for (int col = 0; col < m_result.Width(); col++) {
          int src_col_l, src_row_l, res_x, res_y;
          result2stretch_fix.Transform(col, row, src_col_l, src_row_l, res_x,
                                       res_y);
          if (src_col_l >= 0 && src_col_l <= stretch_width && src_row_l >= 0 &&
              src_row_l <= stretch_height) {
            if (src_col_l == stretch_width) {
              src_col_l--;
            }
            if (src_row_l == stretch_height) {
              src_row_l--;
            }
            int src_col_r = src_col_l + 1;
            int src_row_r = src_row_l + 1;
            if (src_col_r == stretch_width) {
              src_col_r--;
            }
            if (src_row_r == stretch_height) {
              src_row_r--;
            }
            int row_offset_l = src_row_l * stretch_pitch_mask;
            int row_offset_r = src_row_r * stretch_pitch_mask;
            *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);
          }
          dest_pos_mask++;
        }
      }
    } else if (m_Flags & FXDIB_BICUBIC_INTERPOL) {
      CFX_BilinearMatrix result2stretch_fix(result2stretch, 8);
      for (int row = 0; row < m_result.Height(); row++) {
        uint8_t* dest_pos_mask =
            (uint8_t*)pTransformed->m_pAlphaMask->GetScanline(row);
        for (int col = 0; col < m_result.Width(); col++) {
          int src_col_l, src_row_l, res_x, res_y;
          result2stretch_fix.Transform(col, row, src_col_l, src_row_l, res_x,
                                       res_y);
          if (src_col_l >= 0 && src_col_l <= stretch_width && src_row_l >= 0 &&
              src_row_l <= stretch_height) {
            int pos_pixel[8];
            int u_w[4], v_w[4];
            if (src_col_l == stretch_width) {
              src_col_l--;
            }
            if (src_row_l == stretch_height) {
              src_row_l--;
            }
            bicubic_get_pos_weight(pos_pixel, u_w, v_w, src_col_l, src_row_l,
                                   res_x, res_y, stretch_width, stretch_height);
            *dest_pos_mask =
                bicubic_interpol(stretch_buf_mask, stretch_pitch_mask,
                                 pos_pixel, u_w, v_w, res_x, res_y, 1, 0);
          }
          dest_pos_mask++;
        }
      }
    } else {
      CPDF_FixedMatrix result2stretch_fix(result2stretch, 8);
      for (int row = 0; row < m_result.Height(); row++) {
        uint8_t* dest_pos_mask =
            (uint8_t*)pTransformed->m_pAlphaMask->GetScanline(row);
        for (int col = 0; col < m_result.Width(); col++) {
          int src_col, src_row;
          result2stretch_fix.Transform(col, row, src_col, src_row);
          if (src_col >= 0 && src_col <= stretch_width && src_row >= 0 &&
              src_row <= stretch_height) {
            if (src_col == stretch_width) {
              src_col--;
            }
            if (src_row == stretch_height) {
              src_row--;
            }
            *dest_pos_mask =
                stretch_buf_mask[src_row * stretch_pitch_mask + src_col];
          }
          dest_pos_mask++;
        }
      }
    }
  }
  if (m_Storer.GetBitmap()->IsAlphaMask()) {
    if (!(m_Flags & FXDIB_DOWNSAMPLE) && !(m_Flags & FXDIB_BICUBIC_INTERPOL)) {
      CFX_BilinearMatrix result2stretch_fix(result2stretch, 8);
      for (int row = 0; row < m_result.Height(); row++) {
        uint8_t* dest_scan = (uint8_t*)pTransformed->GetScanline(row);
        for (int col = 0; col < m_result.Width(); col++) {
          int src_col_l, src_row_l, res_x, res_y;
          result2stretch_fix.Transform(col, row, src_col_l, src_row_l, res_x,
                                       res_y);
          if (src_col_l >= 0 && src_col_l <= stretch_width && src_row_l >= 0 &&
              src_row_l <= stretch_height) {
            if (src_col_l == stretch_width) {
              src_col_l--;
            }
            if (src_row_l == stretch_height) {
              src_row_l--;
            }
            int src_col_r = src_col_l + 1;
            int src_row_r = src_row_l + 1;
            if (src_col_r == stretch_width) {
              src_col_r--;
            }
            if (src_row_r == stretch_height) {
              src_row_r--;
            }
            int row_offset_l = src_row_l * stretch_pitch;
            int row_offset_r = src_row_r * stretch_pitch;
            *dest_scan =
                bilinear_interpol(stretch_buf, row_offset_l, row_offset_r,
                                  src_col_l, src_col_r, res_x, res_y, 1, 0);
          }
          dest_scan++;
        }
      }
    } else if (m_Flags & FXDIB_BICUBIC_INTERPOL) {
      CFX_BilinearMatrix result2stretch_fix(result2stretch, 8);
      for (int row = 0; row < m_result.Height(); row++) {
        uint8_t* dest_scan = (uint8_t*)pTransformed->GetScanline(row);
        for (int col = 0; col < m_result.Width(); col++) {
          int src_col_l, src_row_l, res_x, res_y;
          result2stretch_fix.Transform(col, row, src_col_l, src_row_l, res_x,
                                       res_y);
          if (src_col_l >= 0 && src_col_l <= stretch_width && src_row_l >= 0 &&
              src_row_l <= stretch_height) {
            int pos_pixel[8];
            int u_w[4], v_w[4];
            if (src_col_l == stretch_width) {
              src_col_l--;
            }
            if (src_row_l == stretch_height) {
              src_row_l--;
            }
            bicubic_get_pos_weight(pos_pixel, u_w, v_w, src_col_l, src_row_l,
                                   res_x, res_y, stretch_width, stretch_height);
            *dest_scan = bicubic_interpol(stretch_buf, stretch_pitch, pos_pixel,
                                          u_w, v_w, res_x, res_y, 1, 0);
          }
          dest_scan++;
        }
      }
    } else {
      CPDF_FixedMatrix result2stretch_fix(result2stretch, 8);
      for (int row = 0; row < m_result.Height(); row++) {
        uint8_t* dest_scan = (uint8_t*)pTransformed->GetScanline(row);
        for (int col = 0; col < m_result.Width(); col++) {
          int src_col, src_row;
          result2stretch_fix.Transform(col, row, src_col, src_row);
          if (src_col >= 0 && src_col <= stretch_width && src_row >= 0 &&
              src_row <= stretch_height) {
            if (src_col == stretch_width) {
              src_col--;
            }
            if (src_row == stretch_height) {
              src_row--;
            }
            const uint8_t* src_pixel =
                stretch_buf + stretch_pitch * src_row + src_col;
            *dest_scan = *src_pixel;
          }
          dest_scan++;
        }
      }
    }
  } else {
    int Bpp = m_Storer.GetBitmap()->GetBPP() / 8;
    if (Bpp == 1) {
      uint32_t argb[256];
      FX_ARGB* pPal = m_Storer.GetBitmap()->GetPalette();
      if (pPal) {
        for (int i = 0; i < 256; i++) {
          argb[i] = pPal[i];
        }
      } else {
        if (m_Storer.GetBitmap()->IsCmykImage()) {
          for (int i = 0; i < 256; i++) {
            argb[i] = 255 - i;
          }
        } else {
          for (int i = 0; i < 256; i++) {
            argb[i] = 0xff000000 | (i * 0x010101);
          }
        }
      }
      int destBpp = pTransformed->GetBPP() / 8;
      if (!(m_Flags & FXDIB_DOWNSAMPLE) &&
          !(m_Flags & FXDIB_BICUBIC_INTERPOL)) {
        CFX_BilinearMatrix result2stretch_fix(result2stretch, 8);
        for (int row = 0; row < m_result.Height(); row++) {
          uint8_t* dest_pos = (uint8_t*)pTransformed->GetScanline(row);
          for (int col = 0; col < m_result.Width(); col++) {
            int src_col_l, src_row_l, res_x, res_y;
            result2stretch_fix.Transform(col, row, src_col_l, src_row_l, res_x,
                                         res_y);
            if (src_col_l >= 0 && src_col_l <= stretch_width &&
                src_row_l >= 0 && src_row_l <= stretch_height) {
              if (src_col_l == stretch_width) {
                src_col_l--;
              }
              if (src_row_l == stretch_height) {
                src_row_l--;
              }
              int src_col_r = src_col_l + 1;
              int src_row_r = src_row_l + 1;
              if (src_col_r == stretch_width) {
                src_col_r--;
              }
              if (src_row_r == stretch_height) {
                src_row_r--;
              }
              int row_offset_l = src_row_l * stretch_pitch;
              int row_offset_r = src_row_r * stretch_pitch;
              uint32_t 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)];
              if (transformF == FXDIB_Rgba) {
                dest_pos[0] = (uint8_t)(r_bgra_cmyk >> 24);
                dest_pos[1] = (uint8_t)(r_bgra_cmyk >> 16);
                dest_pos[2] = (uint8_t)(r_bgra_cmyk >> 8);
              } else {
                *(uint32_t*)dest_pos = r_bgra_cmyk;
              }
            }
            dest_pos += destBpp;
          }
        }
FX_BOOL CFX_ImageTransformer::Continue(IFX_Pause* pPause)
{
    if (m_Status == 1) {
        if (m_Stretcher.Continue(pPause)) {
            return TRUE;
        }
        if (m_Storer.GetBitmap()) {
            m_Storer.Replace(m_Storer.GetBitmap()->SwapXY(m_pMatrix->c > 0, m_pMatrix->b < 0));
        }
        return FALSE;
    } else if (m_Status == 2) {
        return m_Stretcher.Continue(pPause);
    } else if (m_Status != 3) {
        return FALSE;
    }
    if (m_Stretcher.Continue(pPause)) {
        return TRUE;
    }
    int stretch_width = m_StretchClip.Width();
    int stretch_height = m_StretchClip.Height();
    if (m_Storer.GetBitmap() == NULL) {
        return FALSE;
    }
    FX_LPCBYTE stretch_buf = m_Storer.GetBitmap()->GetBuffer();
    FX_LPCBYTE stretch_buf_mask = NULL;
    if (m_Storer.GetBitmap()->m_pAlphaMask) {
        stretch_buf_mask = m_Storer.GetBitmap()->m_pAlphaMask->GetBuffer();
    }
    int stretch_pitch = m_Storer.GetBitmap()->GetPitch();
    CFX_DIBitmap* pTransformed = FX_NEW CFX_DIBitmap;
    if (!pTransformed) {
        return FALSE;
    }
    FXDIB_Format transformF = _GetTransformedFormat(m_Stretcher.m_pSource);
    if (!pTransformed->Create(m_ResultWidth, m_ResultHeight, transformF)) {
        delete pTransformed;
        return FALSE;
    }
    pTransformed->Clear(0);
    if (pTransformed->m_pAlphaMask) {
        pTransformed->m_pAlphaMask->Clear(0);
    }
    CFX_AffineMatrix result2stretch(1.0f, 0.0f, 0.0f, 1.0f, (FX_FLOAT)(m_ResultLeft), (FX_FLOAT)(m_ResultTop));
    result2stretch.Concat(m_dest2stretch);
    result2stretch.TranslateI(-m_StretchClip.left, -m_StretchClip.top);
    if (stretch_buf_mask == NULL && pTransformed->m_pAlphaMask) {
        pTransformed->m_pAlphaMask->Clear(0xff000000);
    } else if (pTransformed->m_pAlphaMask) {
        int stretch_pitch_mask = m_Storer.GetBitmap()->m_pAlphaMask->GetPitch();
        if (!(m_Flags & FXDIB_DOWNSAMPLE) && !(m_Flags & FXDIB_BICUBIC_INTERPOL)) {
            CFX_BilinearMatrix result2stretch_fix(result2stretch, 8);
            for (int row = 0; row < m_ResultHeight; row ++) {
                FX_BYTE* dest_pos_mask = (FX_BYTE*)pTransformed->m_pAlphaMask->GetScanline(row);
                for (int col = 0; col < m_ResultWidth; col ++) {
                    int src_col_l, src_row_l, res_x, res_y;
                    result2stretch_fix.Transform(col, row, src_col_l, src_row_l, res_x, res_y);
                    if (src_col_l >= 0 && src_col_l <= stretch_width && src_row_l >= 0 && src_row_l <= stretch_height) {
                        if (src_col_l == stretch_width) {
                            src_col_l--;
                        }
                        if (src_row_l == stretch_height) {
                            src_row_l--;
                        }
                        int src_col_r = src_col_l + 1;
                        int src_row_r = src_row_l + 1;
                        if (src_col_r == stretch_width) {
                            src_col_r--;
                        }
                        if (src_row_r == stretch_height) {
                            src_row_r--;
                        }
                        int row_offset_l = src_row_l * stretch_pitch_mask;
                        int row_offset_r = src_row_r * stretch_pitch_mask;
                        *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);
                    }
                    dest_pos_mask++;
                }
            }
        } else if (m_Flags & FXDIB_BICUBIC_INTERPOL) {
            CFX_BilinearMatrix result2stretch_fix(result2stretch, 8);
            int pos_pixel[8];
            for (int row = 0; row < m_ResultHeight; row ++) {
                FX_BYTE* dest_pos_mask = (FX_BYTE*)pTransformed->m_pAlphaMask->GetScanline(row);
                for (int col = 0; col < m_ResultWidth; col ++) {
                    int src_col_l, src_row_l, res_x, res_y;
                    result2stretch_fix.Transform(col, row, src_col_l, src_row_l, res_x, res_y);
                    if (src_col_l >= 0 && src_col_l <= stretch_width && src_row_l >= 0 && src_row_l <= stretch_height) {
                        int u_w[4], v_w[4];
                        if (src_col_l == stretch_width) {
                            src_col_l--;
                        }
                        if (src_row_l == stretch_height) {
                            src_row_l--;
                        }
                        _bicubic_get_pos_weight(pos_pixel, u_w, v_w, src_col_l, src_row_l, res_x, res_y, stretch_width, stretch_height);
                        *dest_pos_mask = _bicubic_interpol(stretch_buf_mask, stretch_pitch_mask, pos_pixel, u_w, v_w, res_x, res_y, 1, 0);
                    }
                    dest_pos_mask++;
                }
            }
        } else {
            CPDF_FixedMatrix result2stretch_fix(result2stretch, 8);
            for (int row = 0; row < m_ResultHeight; row ++) {
                FX_BYTE* dest_pos_mask = (FX_BYTE*)pTransformed->m_pAlphaMask->GetScanline(row);
                for (int col = 0; col < m_ResultWidth; col ++) {
                    int src_col, src_row;
                    result2stretch_fix.Transform(col, row, src_col, src_row);
                    if (src_col >= 0 && src_col <= stretch_width && src_row >= 0 && src_row <= stretch_height) {
                        if (src_col == stretch_width) {
                            src_col --;
                        }
                        if (src_row == stretch_height) {
                            src_row --;
                        }
                        *dest_pos_mask = stretch_buf_mask[src_row * stretch_pitch_mask + src_col];
                    }
                    dest_pos_mask++;
                }
            }
        }
    }
    if (m_Storer.GetBitmap()->IsAlphaMask()) {
        if (!(m_Flags & FXDIB_DOWNSAMPLE) && !(m_Flags & FXDIB_BICUBIC_INTERPOL)) {
            CFX_BilinearMatrix result2stretch_fix(result2stretch, 8);
            for (int row = 0; row < m_ResultHeight; row ++) {
                FX_LPBYTE dest_scan = (FX_LPBYTE)pTransformed->GetScanline(row);
                for (int col = 0; col < m_ResultWidth; col ++) {
                    int src_col_l, src_row_l, res_x, res_y;
                    result2stretch_fix.Transform(col, row, src_col_l, src_row_l, res_x, res_y);
                    if (src_col_l >= 0 && src_col_l <= stretch_width && src_row_l >= 0 && src_row_l <= stretch_height) {
                        if (src_col_l == stretch_width) {
                            src_col_l--;
                        }
                        if (src_row_l == stretch_height) {
                            src_row_l--;
                        }
                        int src_col_r = src_col_l + 1;
                        int src_row_r = src_row_l + 1;
                        if (src_col_r == stretch_width) {
                            src_col_r--;
                        }
                        if (src_row_r == stretch_height) {
                            src_row_r--;
                        }
                        int row_offset_l = src_row_l * stretch_pitch;
                        int row_offset_r = src_row_r * stretch_pitch;
                        *dest_scan = _bilinear_interpol(stretch_buf, row_offset_l, row_offset_r, src_col_l, src_col_r, res_x, res_y, 1, 0);
                    }
                    dest_scan ++;
                }
            }
        } else if (m_Flags & FXDIB_BICUBIC_INTERPOL) {
            CFX_BilinearMatrix result2stretch_fix(result2stretch, 8);
            int pos_pixel[8];
            for (int row = 0; row < m_ResultHeight; row ++) {
                FX_LPBYTE dest_scan = (FX_LPBYTE)pTransformed->GetScanline(row);
                for (int col = 0; col < m_ResultWidth; col ++) {
                    int src_col_l, src_row_l, res_x, res_y;
                    result2stretch_fix.Transform(col, row, src_col_l, src_row_l, res_x, res_y);
                    if (src_col_l >= 0 && src_col_l <= stretch_width && src_row_l >= 0 && src_row_l <= stretch_height) {
                        int u_w[4], v_w[4];
                        if (src_col_l == stretch_width) {
                            src_col_l--;
                        }
                        if (src_row_l == stretch_height) {
                            src_row_l--;
                        }
                        _bicubic_get_pos_weight(pos_pixel, u_w, v_w, src_col_l, src_row_l, res_x, res_y, stretch_width, stretch_height);
                        *dest_scan = _bicubic_interpol(stretch_buf, stretch_pitch, pos_pixel, u_w, v_w, res_x, res_y, 1, 0);
                    }
                    dest_scan ++;
                }
            }
        } else {
            CPDF_FixedMatrix result2stretch_fix(result2stretch, 8);
            for (int row = 0; row < m_ResultHeight; row ++) {
                FX_LPBYTE dest_scan = (FX_LPBYTE)pTransformed->GetScanline(row);
                for (int col = 0; col < m_ResultWidth; col ++) {
                    int src_col, src_row;
                    result2stretch_fix.Transform(col, row, src_col, src_row);
                    if (src_col >= 0 && src_col <= stretch_width && src_row >= 0 && src_row <= stretch_height) {
                        if (src_col == stretch_width) {
                            src_col --;
                        }
                        if (src_row == stretch_height) {
                            src_row --;
                        }
                        FX_LPCBYTE src_pixel = stretch_buf + stretch_pitch * src_row + src_col;
                        *dest_scan = *src_pixel;
                    }
                    dest_scan ++;
                }
            }
        }
    } else {
        int Bpp = m_Storer.GetBitmap()->GetBPP() / 8;
        int destBpp = pTransformed->GetBPP() / 8;
        if (Bpp == 1) {
            FX_BOOL bHasAlpha = m_Storer.GetBitmap()->HasAlpha();
            FX_DWORD argb[256];
            FX_ARGB* pPal = m_Storer.GetBitmap()->GetPalette();
            if (pPal) {
                for (int i = 0; i < 256; i ++) {
                    argb[i] = pPal[i];
                }
            } else {
                if (m_Storer.GetBitmap()->IsCmykImage())
                    for (int i = 0; i < 256; i ++) {
                        argb[i] = 255 - i;
                    }
                else
                    for (int i = 0; i < 256; i ++) {
                        argb[i] = 0xff000000 | (i * 0x010101);
                    }
            }
            if (!(m_Flags & FXDIB_DOWNSAMPLE) && !(m_Flags & FXDIB_BICUBIC_INTERPOL)) {
                CFX_BilinearMatrix result2stretch_fix(result2stretch, 8);
                for (int row = 0; row < m_ResultHeight; row ++) {
                    FX_BYTE* dest_pos = (FX_BYTE*)pTransformed->GetScanline(row);
                    for (int col = 0; col < m_ResultWidth; col ++) {
                        int src_col_l, src_row_l, res_x, res_y;
                        result2stretch_fix.Transform(col, row, src_col_l, src_row_l, res_x, res_y);
                        if (src_col_l >= 0 && src_col_l <= stretch_width && src_row_l >= 0 && src_row_l <= stretch_height) {
                            if (src_col_l == stretch_width) {
                                src_col_l--;
                            }
                            if (src_row_l == stretch_height) {
                                src_row_l--;
                            }
                            int src_col_r = src_col_l + 1;
                            int src_row_r = src_row_l + 1;
                            if (src_col_r == stretch_width) {
                                src_col_r--;
                            }
                            if (src_row_r == stretch_height) {
                                src_row_r--;
                            }
                            int row_offset_l = src_row_l * stretch_pitch;
                            int row_offset_r = src_row_r * stretch_pitch;
                            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)];
                            if (transformF == FXDIB_Rgba) {
                                dest_pos[0] = (FX_BYTE)(r_bgra_cmyk >> 24);
                                dest_pos[1] = (FX_BYTE)(r_bgra_cmyk >> 16);
                                dest_pos[2] = (FX_BYTE)(r_bgra_cmyk >> 8);
                            } else {
                                *(FX_DWORD*)dest_pos = r_bgra_cmyk;
                            }
                        }
                        dest_pos += destBpp;
                    }
                }