| // Copyright 2014 PDFium Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
| |
| #include "public/fpdf_edit.h" |
| |
| #include "core/fpdfapi/cpdf_modulemgr.h" |
| #include "core/fpdfapi/page/cpdf_image.h" |
| #include "core/fpdfapi/page/cpdf_imageobject.h" |
| #include "core/fpdfapi/page/cpdf_pageobject.h" |
| #include "core/fpdfapi/parser/cpdf_array.h" |
| #include "core/fpdfapi/parser/cpdf_name.h" |
| #include "fpdfsdk/fsdk_define.h" |
| #include "third_party/base/ptr_util.h" |
| |
| namespace { |
| |
| bool LoadJpegHelper(FPDF_PAGE* pages, |
| int nCount, |
| FPDF_PAGEOBJECT image_object, |
| FPDF_FILEACCESS* fileAccess, |
| bool inlineJpeg) { |
| if (!image_object || !fileAccess) |
| return false; |
| |
| CFX_RetainPtr<IFX_SeekableReadStream> pFile = |
| MakeSeekableReadStream(fileAccess); |
| CPDF_ImageObject* pImgObj = static_cast<CPDF_ImageObject*>(image_object); |
| |
| if (pages) { |
| for (int index = 0; index < nCount; index++) { |
| CPDF_Page* pPage = CPDFPageFromFPDFPage(pages[index]); |
| if (pPage) |
| pImgObj->GetImage()->ResetCache(pPage, nullptr); |
| } |
| } |
| |
| if (inlineJpeg) |
| pImgObj->GetImage()->SetJpegImageInline(pFile); |
| else |
| pImgObj->GetImage()->SetJpegImage(pFile); |
| pImgObj->SetDirty(true); |
| return true; |
| } |
| |
| } // namespace |
| |
| FPDF_EXPORT FPDF_PAGEOBJECT FPDF_CALLCONV |
| FPDFPageObj_NewImageObj(FPDF_DOCUMENT document) { |
| CPDF_Document* pDoc = CPDFDocumentFromFPDFDocument(document); |
| if (!pDoc) |
| return nullptr; |
| |
| auto pImageObj = pdfium::MakeUnique<CPDF_ImageObject>(); |
| pImageObj->SetImage(pdfium::MakeRetain<CPDF_Image>(pDoc)); |
| return pImageObj.release(); |
| } |
| |
| FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV |
| FPDFImageObj_LoadJpegFile(FPDF_PAGE* pages, |
| int nCount, |
| FPDF_PAGEOBJECT image_object, |
| FPDF_FILEACCESS* fileAccess) { |
| return LoadJpegHelper(pages, nCount, image_object, fileAccess, false); |
| } |
| |
| FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV |
| FPDFImageObj_LoadJpegFileInline(FPDF_PAGE* pages, |
| int nCount, |
| FPDF_PAGEOBJECT image_object, |
| FPDF_FILEACCESS* fileAccess) { |
| return LoadJpegHelper(pages, nCount, image_object, fileAccess, true); |
| } |
| |
| FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV |
| FPDFImageObj_SetMatrix(FPDF_PAGEOBJECT image_object, |
| double a, |
| double b, |
| double c, |
| double d, |
| double e, |
| double f) { |
| if (!image_object) |
| return false; |
| |
| CPDF_ImageObject* pImgObj = static_cast<CPDF_ImageObject*>(image_object); |
| pImgObj->set_matrix(CFX_Matrix(static_cast<float>(a), static_cast<float>(b), |
| static_cast<float>(c), static_cast<float>(d), |
| static_cast<float>(e), static_cast<float>(f))); |
| pImgObj->CalcBoundingBox(); |
| pImgObj->SetDirty(true); |
| return true; |
| } |
| |
| FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV |
| FPDFImageObj_SetBitmap(FPDF_PAGE* pages, |
| int nCount, |
| FPDF_PAGEOBJECT image_object, |
| FPDF_BITMAP bitmap) { |
| if (!image_object || !bitmap || !pages) |
| return false; |
| |
| CPDF_ImageObject* pImgObj = static_cast<CPDF_ImageObject*>(image_object); |
| for (int index = 0; index < nCount; index++) { |
| CPDF_Page* pPage = CPDFPageFromFPDFPage(pages[index]); |
| if (pPage) |
| pImgObj->GetImage()->ResetCache(pPage, nullptr); |
| } |
| CFX_RetainPtr<CFX_DIBitmap> holder(CFXBitmapFromFPDFBitmap(bitmap)); |
| pImgObj->GetImage()->SetImage(holder); |
| pImgObj->CalcBoundingBox(); |
| pImgObj->SetDirty(true); |
| return true; |
| } |
| |
| FPDF_EXPORT FPDF_BITMAP FPDF_CALLCONV |
| FPDFImageObj_GetBitmap(FPDF_PAGEOBJECT image_object) { |
| CPDF_PageObject* pObj = CPDFPageObjectFromFPDFPageObject(image_object); |
| if (!pObj || !pObj->IsImage()) |
| return nullptr; |
| |
| CFX_RetainPtr<CPDF_Image> pImg = pObj->AsImage()->GetImage(); |
| if (!pImg) |
| return nullptr; |
| |
| CFX_RetainPtr<CFX_DIBSource> pSource = pImg->LoadDIBSource(); |
| if (!pSource) |
| return nullptr; |
| |
| CFX_RetainPtr<CFX_DIBitmap> pBitmap; |
| // If the source image has a representation of 1 bit per pixel, then convert |
| // it to a grayscale bitmap having 1 byte per pixel, since bitmaps have no |
| // concept of bits. Otherwise, convert the source image to a bitmap directly, |
| // retaining its color representation. |
| if (pSource->GetBPP() == 1) |
| pBitmap = pSource->CloneConvert(FXDIB_8bppRgb); |
| else |
| pBitmap = pSource->Clone(nullptr); |
| |
| return pBitmap.Leak(); |
| } |
| |
| FPDF_EXPORT unsigned long FPDF_CALLCONV |
| FPDFImageObj_GetImageDataDecoded(FPDF_PAGEOBJECT image_object, |
| void* buffer, |
| unsigned long buflen) { |
| CPDF_PageObject* pObj = CPDFPageObjectFromFPDFPageObject(image_object); |
| if (!pObj || !pObj->IsImage()) |
| return 0; |
| |
| CFX_RetainPtr<CPDF_Image> pImg = pObj->AsImage()->GetImage(); |
| if (!pImg) |
| return 0; |
| |
| CPDF_Stream* pImgStream = pImg->GetStream(); |
| if (!pImgStream) |
| return 0; |
| |
| return DecodeStreamMaybeCopyAndReturnLength(pImgStream, buffer, buflen); |
| } |
| |
| FPDF_EXPORT unsigned long FPDF_CALLCONV |
| FPDFImageObj_GetImageDataRaw(FPDF_PAGEOBJECT image_object, |
| void* buffer, |
| unsigned long buflen) { |
| CPDF_PageObject* pObj = CPDFPageObjectFromFPDFPageObject(image_object); |
| if (!pObj || !pObj->IsImage()) |
| return 0; |
| |
| CFX_RetainPtr<CPDF_Image> pImg = pObj->AsImage()->GetImage(); |
| if (!pImg) |
| return 0; |
| |
| CPDF_Stream* pImgStream = pImg->GetStream(); |
| if (!pImgStream) |
| return 0; |
| |
| uint32_t len = pImgStream->GetRawSize(); |
| if (buffer && buflen >= len) |
| memcpy(buffer, pImgStream->GetRawData(), len); |
| |
| return len; |
| } |
| |
| FPDF_EXPORT int FPDF_CALLCONV |
| FPDFImageObj_GetImageFilterCount(FPDF_PAGEOBJECT image_object) { |
| CPDF_PageObject* pObj = CPDFPageObjectFromFPDFPageObject(image_object); |
| if (!pObj || !pObj->IsImage()) |
| return 0; |
| |
| CFX_RetainPtr<CPDF_Image> pImg = pObj->AsImage()->GetImage(); |
| if (!pImg) |
| return 0; |
| |
| CPDF_Dictionary* pDict = pImg->GetDict(); |
| CPDF_Object* pFilter = pDict ? pDict->GetDirectObjectFor("Filter") : nullptr; |
| if (!pFilter) |
| return 0; |
| |
| if (pFilter->IsArray()) |
| return pFilter->AsArray()->GetCount(); |
| if (pFilter->IsName()) |
| return 1; |
| |
| return 0; |
| } |
| |
| FPDF_EXPORT unsigned long FPDF_CALLCONV |
| FPDFImageObj_GetImageFilter(FPDF_PAGEOBJECT image_object, |
| int index, |
| void* buffer, |
| unsigned long buflen) { |
| if (index < 0 || index >= FPDFImageObj_GetImageFilterCount(image_object)) |
| return 0; |
| |
| CPDF_PageObject* pObj = CPDFPageObjectFromFPDFPageObject(image_object); |
| CPDF_Object* pFilter = |
| pObj->AsImage()->GetImage()->GetDict()->GetDirectObjectFor("Filter"); |
| CFX_WideString wsFilters; |
| if (pFilter->IsName()) |
| wsFilters = pFilter->AsName()->GetUnicodeText(); |
| else |
| wsFilters = pFilter->AsArray()->GetUnicodeTextAt(index); |
| |
| return Utf16EncodeMaybeCopyAndReturnLength(wsFilters, buffer, buflen); |
| } |