FX_BOOL CPDF_ExpIntFunc::v_Init(CPDF_Object* pObj) { CPDF_Dictionary* pDict = pObj->GetDict(); if (!pDict) { return FALSE; } CPDF_Array* pArray0 = pDict->GetArrayBy("C0"); if (m_nOutputs == 0) { m_nOutputs = 1; if (pArray0) { m_nOutputs = pArray0->GetCount(); } } CPDF_Array* pArray1 = pDict->GetArrayBy("C1"); m_pBeginValues = FX_Alloc2D(FX_FLOAT, m_nOutputs, 2); m_pEndValues = FX_Alloc2D(FX_FLOAT, m_nOutputs, 2); for (uint32_t i = 0; i < m_nOutputs; i++) { m_pBeginValues[i] = pArray0 ? pArray0->GetFloatAt(i) : 0.0f; m_pEndValues[i] = pArray1 ? pArray1->GetFloatAt(i) : 1.0f; } m_Exponent = pDict->GetFloatBy("N"); m_nOrigOutputs = m_nOutputs; if (m_nOutputs && m_nInputs > INT_MAX / m_nOutputs) { return FALSE; } m_nOutputs *= m_nInputs; return TRUE; }
void CPDF_ICCBasedCS::TranslateImageLine(uint8_t* pDestBuf, const uint8_t* pSrcBuf, int pixels, int image_width, int image_height, FX_BOOL bTransMask) const { if (m_pProfile->m_bsRGB) { ReverseRGB(pDestBuf, pSrcBuf, pixels); } else if (m_pProfile->m_pTransform) { int nMaxColors = 1; for (int i = 0; i < m_nComponents; i++) { nMaxColors *= 52; } if (m_nComponents > 3 || image_width * image_height < nMaxColors * 3 / 2) { CPDF_ModuleMgr::Get()->GetIccModule()->TranslateScanline( m_pProfile->m_pTransform, pDestBuf, pSrcBuf, pixels); } else { if (m_pCache == NULL) { ((CPDF_ICCBasedCS*)this)->m_pCache = FX_Alloc2D(uint8_t, nMaxColors, 3); uint8_t* temp_src = FX_Alloc2D(uint8_t, nMaxColors, m_nComponents); uint8_t* pSrc = temp_src; for (int i = 0; i < nMaxColors; i++) { FX_DWORD color = i; FX_DWORD order = nMaxColors / 52; for (int c = 0; c < m_nComponents; c++) { *pSrc++ = (uint8_t)(color / order * 5); color %= order; order /= 52; } } CPDF_ModuleMgr::Get()->GetIccModule()->TranslateScanline( m_pProfile->m_pTransform, m_pCache, temp_src, nMaxColors); FX_Free(temp_src); } for (int i = 0; i < pixels; i++) { int index = 0; for (int c = 0; c < m_nComponents; c++) { index = index * 52 + (*pSrcBuf) / 5; pSrcBuf++; } index *= 3; *pDestBuf++ = m_pCache[index]; *pDestBuf++ = m_pCache[index + 1]; *pDestBuf++ = m_pCache[index + 2]; } } } else if (m_pAlterCS) { m_pAlterCS->TranslateImageLine(pDestBuf, pSrcBuf, pixels, image_width, image_height); } }
FX_DWORD _DecodeAllScanlines(ICodec_ScanlineDecoder* pDecoder, uint8_t*& dest_buf, FX_DWORD& dest_size) { if (pDecoder == NULL) { return (FX_DWORD) - 1; } int ncomps = pDecoder->CountComps(); int bpc = pDecoder->GetBPC(); int width = pDecoder->GetWidth(); int height = pDecoder->GetHeight(); int pitch = (width * ncomps * bpc + 7) / 8; if (height == 0 || pitch > (1 << 30) / height) { delete pDecoder; return -1; } dest_buf = FX_Alloc2D(uint8_t, pitch, height); dest_size = pitch * height; // Safe since checked alloc returned. for (int row = 0; row < height; row ++) { uint8_t* pLine = pDecoder->GetScanline(row); if (pLine == NULL) { break; } FXSYS_memcpy(dest_buf + row * pitch, pLine, pitch); } FX_DWORD srcoff = pDecoder->GetSrcOffset(); delete pDecoder; return srcoff; }
void CPDF_PageRenderCache::CacheOptimization(int32_t dwLimitCacheSize) { if (m_nCacheSize <= (FX_DWORD)dwLimitCacheSize) { return; } int nCount = m_ImageCaches.GetCount(); CACHEINFO* pCACHEINFO = (CACHEINFO*)FX_Alloc2D(uint8_t, sizeof(CACHEINFO), nCount); FX_POSITION pos = m_ImageCaches.GetStartPosition(); int i = 0; while (pos) { void* key; void* value; m_ImageCaches.GetNextAssoc(pos, key, value); pCACHEINFO[i].time = ((CPDF_ImageCache*)value)->GetTimeCount(); pCACHEINFO[i++].pStream = ((CPDF_ImageCache*)value)->GetStream(); } FXSYS_qsort(pCACHEINFO, nCount, sizeof(CACHEINFO), compare); FX_DWORD nTimeCount = m_nTimeCount; if (nTimeCount + 1 < nTimeCount) { for (i = 0; i < nCount; i++) { ((CPDF_ImageCache*)(m_ImageCaches[pCACHEINFO[i].pStream])) ->m_dwTimeCount = i; } m_nTimeCount = nCount; } i = 0; while (nCount > 15) { ClearImageCache(pCACHEINFO[i++].pStream); nCount--; } while (m_nCacheSize > (FX_DWORD)dwLimitCacheSize) { ClearImageCache(pCACHEINFO[i++].pStream); } FX_Free(pCACHEINFO); }
void CBC_CommonBitMatrix::Init(int32_t dimension) { m_width = dimension; m_height = dimension; int32_t rowSize = (m_height + 31) >> 5; m_rowSize = rowSize; m_bits = FX_Alloc2D(int32_t, m_rowSize, m_height); FXSYS_memset(m_bits, 0, m_rowSize * m_height * sizeof(int32_t)); }
void CBC_CommonBitMatrix::Init(int32_t width, int32_t height) { m_width = width; m_height = height; int32_t rowSize = (width + 31) >> 5; m_rowSize = rowSize; m_bits = FX_Alloc2D(int32_t, m_rowSize, m_height); FXSYS_memset(m_bits, 0, m_rowSize * m_height * sizeof(int32_t)); }
uint8_t* CBC_PDF417Writer::Encode(const CFX_WideString& contents, int32_t& outWidth, int32_t& outHeight, int32_t& e) { CBC_PDF417 encoder; int32_t col = (m_Width / m_ModuleWidth - 69) / 17; int32_t row = m_Height / (m_ModuleWidth * 20); if (row >= 3 && row <= 90 && col >= 1 && col <= 30) { encoder.setDimensions(col, col, row, row); } else if (col >= 1 && col <= 30) { encoder.setDimensions(col, col, 90, 3); } else if (row >= 3 && row <= 90) { encoder.setDimensions(30, 1, row, row); } encoder.generateBarcodeLogic(contents, m_iCorrectLevel, e); BC_EXCEPTION_CHECK_ReturnValue(e, nullptr); int32_t lineThickness = 2; int32_t aspectRatio = 4; CBC_BarcodeMatrix* barcodeMatrix = encoder.getBarcodeMatrix(); CFX_ByteArray originalScale; originalScale.Copy(barcodeMatrix->getScaledMatrix( lineThickness, aspectRatio * lineThickness)); int32_t width = outWidth; int32_t height = outHeight; outWidth = barcodeMatrix->getWidth(); outHeight = barcodeMatrix->getHeight(); bool rotated = false; if ((height > width) ^ (outWidth < outHeight)) { rotateArray(originalScale, outHeight, outWidth); rotated = true; int32_t temp = outHeight; outHeight = outWidth; outWidth = temp; } int32_t scaleX = width / outWidth; int32_t scaleY = height / outHeight; int32_t scale; if (scaleX < scaleY) { scale = scaleX; } else { scale = scaleY; } if (scale > 1) { originalScale.RemoveAll(); originalScale.Copy(barcodeMatrix->getScaledMatrix( scale * lineThickness, scale * aspectRatio * lineThickness)); if (rotated) { rotateArray(originalScale, outHeight, outWidth); int32_t temp = outHeight; outHeight = outWidth; outWidth = temp; } } uint8_t* result = FX_Alloc2D(uint8_t, outHeight, outWidth); FXSYS_memcpy(result, originalScale.GetData(), outHeight * outWidth); return result; }
FX_BOOL CPDF_Function::Init(CPDF_Object* pObj) { CPDF_Dictionary* pDict; if (pObj->GetType() == PDFOBJ_STREAM) { pDict = ((CPDF_Stream*)pObj)->GetDict(); } else { pDict = (CPDF_Dictionary*)pObj; } CPDF_Array* pDomains = pDict->GetArray(FX_BSTRC("Domain")); if (pDomains == NULL) { return FALSE; } m_nInputs = pDomains->GetCount() / 2; if (m_nInputs == 0) { return FALSE; } m_pDomains = FX_Alloc2D(FX_FLOAT, m_nInputs, 2); for (int i = 0; i < m_nInputs * 2; i++) { m_pDomains[i] = pDomains->GetFloat(i); } CPDF_Array* pRanges = pDict->GetArray(FX_BSTRC("Range")); m_nOutputs = 0; if (pRanges) { m_nOutputs = pRanges->GetCount() / 2; m_pRanges = FX_Alloc2D(FX_FLOAT, m_nOutputs, 2); for (int i = 0; i < m_nOutputs * 2; i++) { m_pRanges[i] = pRanges->GetFloat(i); } } FX_DWORD old_outputs = m_nOutputs; if (!v_Init(pObj)) { return FALSE; } if (m_pRanges && m_nOutputs > (int)old_outputs) { m_pRanges = FX_Realloc(FX_FLOAT, m_pRanges, m_nOutputs * 2); if (m_pRanges) { FXSYS_memset(m_pRanges + (old_outputs * 2), 0, sizeof(FX_FLOAT) * (m_nOutputs - old_outputs) * 2); } } return TRUE; }
FX_BOOL CPDF_StitchFunc::v_Init(CPDF_Object* pObj) { CPDF_Dictionary* pDict = pObj->GetDict(); if (!pDict) { return FALSE; } if (m_nInputs != kRequiredNumInputs) { return FALSE; } CPDF_Array* pArray = pDict->GetArrayBy("Functions"); if (!pArray) { return FALSE; } uint32_t nSubs = pArray->GetCount(); if (nSubs == 0) return FALSE; m_nOutputs = 0; for (uint32_t i = 0; i < nSubs; i++) { CPDF_Object* pSub = pArray->GetDirectObjectAt(i); if (pSub == pObj) return FALSE; std::unique_ptr<CPDF_Function> pFunc(CPDF_Function::Load(pSub)); if (!pFunc) return FALSE; // Check that the input dimensionality is 1, and that all output // dimensionalities are the same. if (pFunc->CountInputs() != kRequiredNumInputs) return FALSE; if (pFunc->CountOutputs() != m_nOutputs) { if (m_nOutputs) return FALSE; m_nOutputs = pFunc->CountOutputs(); } m_pSubFunctions.push_back(std::move(pFunc)); } m_pBounds = FX_Alloc(FX_FLOAT, nSubs + 1); m_pBounds[0] = m_pDomains[0]; pArray = pDict->GetArrayBy("Bounds"); if (!pArray) return FALSE; for (uint32_t i = 0; i < nSubs - 1; i++) m_pBounds[i + 1] = pArray->GetFloatAt(i); m_pBounds[nSubs] = m_pDomains[1]; m_pEncode = FX_Alloc2D(FX_FLOAT, nSubs, 2); pArray = pDict->GetArrayBy("Encode"); if (!pArray) return FALSE; for (uint32_t i = 0; i < nSubs * 2; i++) m_pEncode[i] = pArray->GetFloatAt(i); return TRUE; }
uint32_t* CJBig2_GSIDProc::decode_Arith(CJBig2_ArithDecoder* pArithDecoder, JBig2ArithCtx* gbContext, IFX_Pause* pPause) { std::unique_ptr<CJBig2_GRDProc> pGRD(new CJBig2_GRDProc()); pGRD->MMR = GSMMR; pGRD->GBW = GSW; pGRD->GBH = GSH; pGRD->GBTEMPLATE = GSTEMPLATE; pGRD->TPGDON = 0; pGRD->USESKIP = GSUSESKIP; pGRD->SKIP = GSKIP; if (GSTEMPLATE <= 1) { pGRD->GBAT[0] = 3; } else { pGRD->GBAT[0] = 2; } pGRD->GBAT[1] = -1; if (pGRD->GBTEMPLATE == 0) { pGRD->GBAT[2] = -3; pGRD->GBAT[3] = -1; pGRD->GBAT[4] = 2; pGRD->GBAT[5] = -2; pGRD->GBAT[6] = -2; pGRD->GBAT[7] = -2; } std::vector<std::unique_ptr<CJBig2_Image>> GSPLANES(GSBPP); for (int32_t i = GSBPP - 1; i >= 0; --i) { CJBig2_Image* pImage = nullptr; FXCODEC_STATUS status = pGRD->Start_decode_Arith(&pImage, pArithDecoder, gbContext, nullptr); while (status == FXCODEC_STATUS_DECODE_TOBECONTINUE) status = pGRD->Continue_decode(pPause); if (!pImage) return nullptr; GSPLANES[i].reset(pImage); if (i < GSBPP - 1) pImage->composeFrom(0, 0, GSPLANES[i + 1].get(), JBIG2_COMPOSE_XOR); } std::unique_ptr<uint32_t, FxFreeDeleter> GSVALS( FX_Alloc2D(uint32_t, GSW, GSH)); JBIG2_memset(GSVALS.get(), 0, sizeof(uint32_t) * GSW * GSH); for (uint32_t y = 0; y < GSH; ++y) { for (uint32_t x = 0; x < GSW; ++x) { for (int32_t i = 0; i < GSBPP; ++i) GSVALS.get()[y * GSW + x] |= GSPLANES[i]->getPixel(x, y) << i; } } return GSVALS.release(); }
FX_BOOL CPDF_StitchFunc::v_Init(CPDF_Object* pObj) { CPDF_Dictionary* pDict = pObj->GetDict(); if (pDict == NULL) { return FALSE; } CPDF_Array* pArray = pDict->GetArray(FX_BSTRC("Functions")); if (pArray == NULL) { return FALSE; } m_nSubs = pArray->GetCount(); if (m_nSubs == 0) { return FALSE; } m_pSubFunctions = FX_Alloc(CPDF_Function*, m_nSubs); m_nOutputs = 0; int i; for (i = 0; i < m_nSubs; i++) { CPDF_Object* pSub = pArray->GetElementValue(i); if (pSub == pObj) { return FALSE; } m_pSubFunctions[i] = CPDF_Function::Load(pSub); if (m_pSubFunctions[i] == NULL) { return FALSE; } if (m_pSubFunctions[i]->CountOutputs() > m_nOutputs) { m_nOutputs = m_pSubFunctions[i]->CountOutputs(); } } m_pBounds = FX_Alloc(FX_FLOAT, m_nSubs + 1); m_pBounds[0] = m_pDomains[0]; pArray = pDict->GetArray(FX_BSTRC("Bounds")); if (pArray == NULL) { return FALSE; } for (i = 0; i < m_nSubs - 1; i++) { m_pBounds[i + 1] = pArray->GetFloat(i); } m_pBounds[m_nSubs] = m_pDomains[1]; m_pEncode = FX_Alloc2D(FX_FLOAT, m_nSubs, 2); pArray = pDict->GetArray(FX_BSTRC("Encode")); if (pArray == NULL) { return FALSE; } for (i = 0; i < m_nSubs * 2; i++) { m_pEncode[i] = pArray->GetFloat(i); } return TRUE; }
FX_BOOL CPDF_Function::Init(CPDF_Object* pObj) { CPDF_Stream* pStream = pObj->AsStream(); CPDF_Dictionary* pDict = pStream ? pStream->GetDict() : pObj->AsDictionary(); CPDF_Array* pDomains = pDict->GetArrayBy("Domain"); if (!pDomains) return FALSE; m_nInputs = pDomains->GetCount() / 2; if (m_nInputs == 0) return FALSE; m_pDomains = FX_Alloc2D(FX_FLOAT, m_nInputs, 2); for (uint32_t i = 0; i < m_nInputs * 2; i++) { m_pDomains[i] = pDomains->GetFloatAt(i); } CPDF_Array* pRanges = pDict->GetArrayBy("Range"); m_nOutputs = 0; if (pRanges) { m_nOutputs = pRanges->GetCount() / 2; m_pRanges = FX_Alloc2D(FX_FLOAT, m_nOutputs, 2); for (uint32_t i = 0; i < m_nOutputs * 2; i++) m_pRanges[i] = pRanges->GetFloatAt(i); } uint32_t old_outputs = m_nOutputs; if (!v_Init(pObj)) return FALSE; if (m_pRanges && m_nOutputs > old_outputs) { m_pRanges = FX_Realloc(FX_FLOAT, m_pRanges, m_nOutputs * 2); if (m_pRanges) { FXSYS_memset(m_pRanges + (old_outputs * 2), 0, sizeof(FX_FLOAT) * (m_nOutputs - old_outputs) * 2); } } return TRUE; }
void* CFX_BaseSegmentedArray::Add() { if (m_DataSize % m_SegmentSize) { return GetAt(m_DataSize++); } void* pSegment = FX_Alloc2D(uint8_t, m_UnitSize, m_SegmentSize); if (!m_pIndex) { m_pIndex = pSegment; m_DataSize++; return pSegment; } if (m_IndexDepth == 0) { void** pIndex = FX_Alloc(void*, m_IndexSize); pIndex[0] = m_pIndex; pIndex[1] = pSegment; m_pIndex = pIndex; m_DataSize++; m_IndexDepth++; return pSegment; }
uint32_t* CJBig2_GSIDProc::decode_MMR(CJBig2_BitStream* pStream, IFX_Pause* pPause) { std::unique_ptr<CJBig2_GRDProc> pGRD(new CJBig2_GRDProc()); pGRD->MMR = GSMMR; pGRD->GBW = GSW; pGRD->GBH = GSH; std::unique_ptr<CJBig2_Image*> GSPLANES(FX_Alloc(CJBig2_Image*, GSBPP)); JBIG2_memset(GSPLANES.get(), 0, sizeof(CJBig2_Image*) * GSBPP); pGRD->Start_decode_MMR(&GSPLANES.get()[GSBPP - 1], pStream, nullptr); if (!GSPLANES.get()[GSBPP - 1]) return nullptr; pStream->alignByte(); pStream->offset(3); int32_t J = GSBPP - 2; while (J >= 0) { pGRD->Start_decode_MMR(&GSPLANES.get()[J], pStream, nullptr); if (!GSPLANES.get()[J]) { for (int32_t K = GSBPP - 1; K > J; --K) delete GSPLANES.get()[K]; return nullptr; } pStream->alignByte(); pStream->offset(3); GSPLANES.get()[J]->composeFrom(0, 0, GSPLANES.get()[J + 1], JBIG2_COMPOSE_XOR); J = J - 1; } std::unique_ptr<uint32_t> GSVALS(FX_Alloc2D(uint32_t, GSW, GSH)); JBIG2_memset(GSVALS.get(), 0, sizeof(uint32_t) * GSW * GSH); for (uint32_t y = 0; y < GSH; ++y) { for (uint32_t x = 0; x < GSW; ++x) { for (J = 0; J < GSBPP; ++J) { GSVALS.get()[y * GSW + x] |= GSPLANES.get()[J]->getPixel(x, y) << J; } } } for (J = 0; J < GSBPP; ++J) { delete GSPLANES.get()[J]; } return GSVALS.release(); }
static void DrawLatticeGouraudShading(CFX_DIBitmap* pBitmap, CFX_Matrix* pObject2Bitmap, CPDF_Stream* pShadingStream, CPDF_Function** pFuncs, int nFuncs, CPDF_ColorSpace* pCS, int alpha) { ASSERT(pBitmap->GetFormat() == FXDIB_Argb); int row_verts = pShadingStream->GetDict()->GetInteger("VerticesPerRow"); if (row_verts < 2) return; CPDF_MeshStream stream; if (!stream.Load(pShadingStream, pFuncs, nFuncs, pCS)) return; CPDF_MeshVertex* vertex = FX_Alloc2D(CPDF_MeshVertex, row_verts, 2); if (!stream.GetVertexRow(vertex, row_verts, pObject2Bitmap)) { FX_Free(vertex); return; } int last_index = 0; while (1) { CPDF_MeshVertex* last_row = vertex + last_index * row_verts; CPDF_MeshVertex* this_row = vertex + (1 - last_index) * row_verts; if (!stream.GetVertexRow(this_row, row_verts, pObject2Bitmap)) { FX_Free(vertex); return; } CPDF_MeshVertex triangle[3]; for (int i = 1; i < row_verts; i++) { triangle[0] = last_row[i]; triangle[1] = this_row[i - 1]; triangle[2] = last_row[i - 1]; DrawGouraud(pBitmap, alpha, triangle); triangle[2] = this_row[i]; DrawGouraud(pBitmap, alpha, triangle); } last_index = 1 - last_index; } FX_Free(vertex); }
uint8_t* CBC_DataMatrixWriter::Encode(const CFX_WideString& contents, int32_t& outWidth, int32_t& outHeight, int32_t& e) { if (outWidth < 0 || outHeight < 0) { e = BCExceptionHeightAndWidthMustBeAtLeast1; return nullptr; } CBC_SymbolShapeHint::SymbolShapeHint shape = CBC_SymbolShapeHint::FORCE_SQUARE; CBC_Dimension* minSize = nullptr; CBC_Dimension* maxSize = nullptr; CFX_WideString ecLevel; CFX_WideString encoded = CBC_HighLevelEncoder::encodeHighLevel( contents, ecLevel, shape, minSize, maxSize, e); if (e != BCExceptionNO) return nullptr; CBC_SymbolInfo* symbolInfo = CBC_SymbolInfo::lookup( encoded.GetLength(), shape, minSize, maxSize, true, e); if (e != BCExceptionNO) return nullptr; CFX_WideString codewords = CBC_ErrorCorrection::encodeECC200(encoded, symbolInfo, e); if (e != BCExceptionNO) return nullptr; CBC_DefaultPlacement* placement = new CBC_DefaultPlacement(codewords, symbolInfo->getSymbolDataWidth(e), symbolInfo->getSymbolDataHeight(e)); if (e != BCExceptionNO) return nullptr; placement->place(); CBC_CommonByteMatrix* bytematrix = encodeLowLevel(placement, symbolInfo, e); if (e != BCExceptionNO) return nullptr; outWidth = bytematrix->GetWidth(); outHeight = bytematrix->GetHeight(); uint8_t* result = FX_Alloc2D(uint8_t, outWidth, outHeight); FXSYS_memcpy(result, bytematrix->GetArray(), outWidth * outHeight); delete bytematrix; delete placement; return result; }
uint8_t* CFX_BaseDiscreteArray::AddSpaceTo(int32_t index) { ASSERT(index > -1); FX_BASEDISCRETEARRAYDATA* pData = (FX_BASEDISCRETEARRAYDATA*)m_pData; int32_t& iChunkCount = pData->iChunkCount; int32_t iChunkSize = pData->iChunkSize; uint8_t* pChunk = nullptr; int32_t iChunk = index / iChunkSize; if (iChunk < iChunkCount) { pChunk = pData->ChunkBuffer.GetAt(iChunk); } if (!pChunk) { pChunk = FX_Alloc2D(uint8_t, iChunkSize, pData->iBlockSize); FXSYS_memset(pChunk, 0, iChunkSize * pData->iBlockSize); pData->ChunkBuffer.SetAtGrow(iChunk, pChunk); if (iChunkCount <= iChunk) { iChunkCount = iChunk + 1; } } return pChunk + (index % iChunkSize) * pData->iBlockSize; }
uint8_t* CBC_QRCodeWriter::Encode(const CFX_WideString& contents, int32_t ecLevel, int32_t& outWidth, int32_t& outHeight, int32_t& e) { CBC_QRCoderErrorCorrectionLevel* ec = NULL; switch (ecLevel) { case 0: ec = CBC_QRCoderErrorCorrectionLevel::L; break; case 1: ec = CBC_QRCoderErrorCorrectionLevel::M; break; case 2: ec = CBC_QRCoderErrorCorrectionLevel::Q; break; case 3: ec = CBC_QRCoderErrorCorrectionLevel::H; break; default: { e = BCExceptionUnSupportEclevel; BC_EXCEPTION_CHECK_ReturnValue(e, NULL); } } CBC_QRCoder qr; if (m_iVersion > 0 && m_iVersion < 41) { CFX_ByteString byteStr = contents.UTF8Encode(); CBC_QRCoderEncoder::Encode(byteStr, ec, &qr, e, m_iVersion); } else { CBC_QRCoderEncoder::Encode(contents, ec, &qr, e); } BC_EXCEPTION_CHECK_ReturnValue(e, NULL); outWidth = qr.GetMatrixWidth(); outHeight = qr.GetMatrixWidth(); uint8_t* result = FX_Alloc2D(uint8_t, outWidth, outHeight); FXSYS_memcpy(result, qr.GetMatrix()->GetArray(), outWidth * outHeight); return result; }
FX_BOOL CPDF_IndexedCS::v_Load(CPDF_Document* pDoc, CPDF_Array* pArray) { if (pArray->GetCount() < 4) { return FALSE; } CPDF_Object* pBaseObj = pArray->GetElementValue(1); if (pBaseObj == m_pArray) { return FALSE; } CPDF_DocPageData* pDocPageData = pDoc->GetPageData(); m_pBaseCS = pDocPageData->GetColorSpace(pBaseObj, NULL); if (!m_pBaseCS) { return FALSE; } m_pCountedBaseCS = pDocPageData->FindColorSpacePtr(m_pBaseCS->GetArray()); m_nBaseComponents = m_pBaseCS->CountComponents(); m_pCompMinMax = FX_Alloc2D(FX_FLOAT, m_nBaseComponents, 2); FX_FLOAT defvalue; for (int i = 0; i < m_nBaseComponents; i++) { m_pBaseCS->GetDefaultValue(i, defvalue, m_pCompMinMax[i * 2], m_pCompMinMax[i * 2 + 1]); m_pCompMinMax[i * 2 + 1] -= m_pCompMinMax[i * 2]; } m_MaxIndex = pArray->GetInteger(2); CPDF_Object* pTableObj = pArray->GetElementValue(3); if (!pTableObj) return FALSE; if (CPDF_String* pString = pTableObj->AsString()) { m_Table = pString->GetString(); } else if (CPDF_Stream* pStream = pTableObj->AsStream()) { CPDF_StreamAcc acc; acc.LoadAllData(pStream, FALSE); m_Table = CFX_ByteStringC(acc.GetData(), acc.GetSize()); } return TRUE; }
void CPDF_Image::SetImage(const CFX_DIBitmap* pBitmap, int32_t iCompress, IFX_FileWrite* pFileWrite, IFX_FileRead* pFileRead, const CFX_DIBitmap* pMask, const CPDF_ImageSetParam* pParam) { int32_t BitmapWidth = pBitmap->GetWidth(); int32_t BitmapHeight = pBitmap->GetHeight(); if (BitmapWidth < 1 || BitmapHeight < 1) { return; } uint8_t* src_buf = pBitmap->GetBuffer(); int32_t src_pitch = pBitmap->GetPitch(); int32_t bpp = pBitmap->GetBPP(); FX_BOOL bUseMatte = pParam && pParam->pMatteColor && (pBitmap->GetFormat() == FXDIB_Argb); CPDF_Dictionary* pDict = new CPDF_Dictionary; pDict->SetAtName("Type", "XObject"); pDict->SetAtName("Subtype", "Image"); pDict->SetAtInteger("Width", BitmapWidth); pDict->SetAtInteger("Height", BitmapHeight); uint8_t* dest_buf = NULL; FX_STRSIZE dest_pitch = 0, dest_size = 0, opType = -1; if (bpp == 1) { int32_t reset_a = 0, reset_r = 0, reset_g = 0, reset_b = 0; int32_t set_a = 0, set_r = 0, set_g = 0, set_b = 0; if (!pBitmap->IsAlphaMask()) { ArgbDecode(pBitmap->GetPaletteArgb(0), reset_a, reset_r, reset_g, reset_b); ArgbDecode(pBitmap->GetPaletteArgb(1), set_a, set_r, set_g, set_b); } if (set_a == 0 || reset_a == 0) { pDict->SetAt("ImageMask", new CPDF_Boolean(TRUE)); if (reset_a == 0) { CPDF_Array* pArray = new CPDF_Array; pArray->AddInteger(1); pArray->AddInteger(0); pDict->SetAt("Decode", pArray); } } else { CPDF_Array* pCS = new CPDF_Array; pCS->AddName("Indexed"); pCS->AddName("DeviceRGB"); pCS->AddInteger(1); CFX_ByteString ct; FX_CHAR* pBuf = ct.GetBuffer(6); pBuf[0] = (FX_CHAR)reset_r; pBuf[1] = (FX_CHAR)reset_g; pBuf[2] = (FX_CHAR)reset_b; pBuf[3] = (FX_CHAR)set_r; pBuf[4] = (FX_CHAR)set_g; pBuf[5] = (FX_CHAR)set_b; ct.ReleaseBuffer(6); pCS->Add(new CPDF_String(ct, TRUE)); pDict->SetAt("ColorSpace", pCS); } pDict->SetAtInteger("BitsPerComponent", 1); dest_pitch = (BitmapWidth + 7) / 8; if ((iCompress & 0x03) == PDF_IMAGE_NO_COMPRESS) { opType = 1; } else { opType = 0; } } else if (bpp == 8) { int32_t iPalette = pBitmap->GetPaletteSize(); if (iPalette > 0) { CPDF_Array* pCS = new CPDF_Array; m_pDocument->AddIndirectObject(pCS); pCS->AddName("Indexed"); pCS->AddName("DeviceRGB"); pCS->AddInteger(iPalette - 1); uint8_t* pColorTable = FX_Alloc2D(uint8_t, iPalette, 3); uint8_t* ptr = pColorTable; for (int32_t i = 0; i < iPalette; i++) { FX_DWORD argb = pBitmap->GetPaletteArgb(i); ptr[0] = (uint8_t)(argb >> 16); ptr[1] = (uint8_t)(argb >> 8); ptr[2] = (uint8_t)argb; ptr += 3; } CPDF_Stream* pCTS = new CPDF_Stream(pColorTable, iPalette * 3, new CPDF_Dictionary); m_pDocument->AddIndirectObject(pCTS); pCS->AddReference(m_pDocument, pCTS); pDict->SetAtReference("ColorSpace", m_pDocument, pCS); } else {
static void* my_alloc_func (void* opaque, unsigned int items, unsigned int size) { return FX_Alloc2D(uint8_t, items, size); }
static void _JpegEncode(const CFX_DIBSource* pSource, FX_LPBYTE& dest_buf, FX_STRSIZE& dest_size, int quality, FX_LPCBYTE icc_buf, FX_DWORD icc_length) { struct jpeg_error_mgr jerr; jerr.error_exit = _error_do_nothing; jerr.emit_message = _error_do_nothing1; jerr.output_message = _error_do_nothing; jerr.format_message = _error_do_nothing2; jerr.reset_error_mgr = _error_do_nothing; struct jpeg_compress_struct cinfo; memset(&cinfo, 0, sizeof(cinfo)); cinfo.err = &jerr; jpeg_create_compress(&cinfo); int Bpp = pSource->GetBPP() / 8; FX_DWORD nComponents = Bpp >= 3 ? (pSource->IsCmykImage() ? 4 : 3) : 1; FX_DWORD pitch = pSource->GetPitch(); FX_DWORD width = pdfium::base::checked_cast<FX_DWORD>(pSource->GetWidth()); FX_DWORD height = pdfium::base::checked_cast<FX_DWORD>(pSource->GetHeight()); FX_SAFE_DWORD safe_buf_len = width; safe_buf_len *= height; safe_buf_len *= nComponents; safe_buf_len += 1024; if (icc_length) { safe_buf_len += 255 * 18; safe_buf_len += icc_length; } FX_DWORD dest_buf_length = 0; if (!safe_buf_len.IsValid()) { dest_buf = nullptr; } else { dest_buf_length = safe_buf_len.ValueOrDie(); dest_buf = FX_TryAlloc(FX_BYTE, dest_buf_length); const int MIN_TRY_BUF_LEN = 1024; while (!dest_buf && dest_buf_length > MIN_TRY_BUF_LEN) { dest_buf_length >>= 1; dest_buf = FX_TryAlloc(FX_BYTE, dest_buf_length); } } if (!dest_buf) { FX_OutOfMemoryTerminate(); } struct jpeg_destination_mgr dest; dest.init_destination = _dest_do_nothing; dest.term_destination = _dest_do_nothing; dest.empty_output_buffer = _dest_empty; dest.next_output_byte = dest_buf; dest.free_in_buffer = dest_buf_length; cinfo.dest = &dest; cinfo.image_width = width; cinfo.image_height = height; cinfo.input_components = nComponents; if (nComponents == 1) { cinfo.in_color_space = JCS_GRAYSCALE; } else if (nComponents == 3) { cinfo.in_color_space = JCS_RGB; } else { cinfo.in_color_space = JCS_CMYK; } FX_LPBYTE line_buf = NULL; if (nComponents > 1) { line_buf = FX_Alloc2D(FX_BYTE, width, nComponents); } jpeg_set_defaults(&cinfo); if(quality != 75) { jpeg_set_quality(&cinfo, quality, TRUE); } jpeg_start_compress(&cinfo, TRUE); _JpegEmbedIccProfile(&cinfo, icc_buf, icc_length); JSAMPROW row_pointer[1]; JDIMENSION row; while (cinfo.next_scanline < cinfo.image_height) { FX_LPCBYTE src_scan = pSource->GetScanline(cinfo.next_scanline); if (nComponents > 1) { FX_LPBYTE dest_scan = line_buf; if (nComponents == 3) { for (int i = 0; i < width; i ++) { dest_scan[0] = src_scan[2]; dest_scan[1] = src_scan[1]; dest_scan[2] = src_scan[0]; dest_scan += 3; src_scan += Bpp; } } else { for (int i = 0; i < pitch; i ++) { *dest_scan++ = ~*src_scan++; } } row_pointer[0] = line_buf; } else { row_pointer[0] = (FX_LPBYTE)src_scan; } row = cinfo.next_scanline; jpeg_write_scanlines(&cinfo, row_pointer, 1); if (cinfo.next_scanline == row) { dest_buf = FX_Realloc(FX_BYTE, dest_buf, dest_buf_length + JPEG_BLOCK_SIZE); dest.next_output_byte = dest_buf + dest_buf_length - dest.free_in_buffer; dest_buf_length += JPEG_BLOCK_SIZE; dest.free_in_buffer += JPEG_BLOCK_SIZE; } } jpeg_finish_compress(&cinfo); jpeg_destroy_compress(&cinfo); if (line_buf) { FX_Free(line_buf); } dest_size = dest_buf_length - (FX_STRSIZE)dest.free_in_buffer; }
FX_BOOL CPDF_ICCBasedCS::v_Load(CPDF_Document* pDoc, CPDF_Array* pArray) { CPDF_Stream* pStream = pArray->GetStream(1); if (pStream == NULL) { return FALSE; } m_pProfile = pDoc->LoadIccProfile(pStream); if (!m_pProfile) { return FALSE; } m_nComponents = m_pProfile->GetComponents(); //Try using the nComponents from ICC profile CPDF_Dictionary* pDict = pStream->GetDict(); if (m_pProfile->m_pTransform == NULL) { // No valid ICC profile or using sRGB CPDF_Object* pAlterCSObj = pDict ? pDict->GetElementValue(FX_BSTRC("Alternate")) : NULL; if (pAlterCSObj) { CPDF_ColorSpace* pAlterCS = CPDF_ColorSpace::Load(pDoc, pAlterCSObj); if (pAlterCS) { if (m_nComponents == 0) { // NO valid ICC profile if (pAlterCS->CountComponents() > 0) { // Use Alternative colorspace m_nComponents = pAlterCS->CountComponents(); m_pAlterCS = pAlterCS; m_bOwn = TRUE; } else { // No valid alternative colorspace pAlterCS->ReleaseCS(); int32_t nDictComponents = pDict ? pDict->GetInteger(FX_BSTRC("N")) : 0; if (nDictComponents != 1 && nDictComponents != 3 && nDictComponents != 4) { return FALSE; } m_nComponents = nDictComponents; } } else { // Using sRGB if (pAlterCS->CountComponents() != m_nComponents) { pAlterCS->ReleaseCS(); } else { m_pAlterCS = pAlterCS; m_bOwn = TRUE; } } } } if (!m_pAlterCS) { if (m_nComponents == 1) { m_pAlterCS = GetStockCS(PDFCS_DEVICEGRAY); } else if (m_nComponents == 3) { m_pAlterCS = GetStockCS(PDFCS_DEVICERGB); } else if (m_nComponents == 4) { m_pAlterCS = GetStockCS(PDFCS_DEVICECMYK); } } } CPDF_Array* pRanges = pDict->GetArray(FX_BSTRC("Range")); m_pRanges = FX_Alloc2D(FX_FLOAT, m_nComponents, 2); for (int i = 0; i < m_nComponents * 2; i ++) { if (pRanges) { m_pRanges[i] = pRanges->GetNumber(i); } else if (i % 2) { m_pRanges[i] = 1.0f; } else { m_pRanges[i] = 0; } } return TRUE; }
void CBC_CommonByteMatrix::Init() { m_bytes = FX_Alloc2D(uint8_t, m_height, m_width); FXSYS_memset(m_bytes, 0xff, m_height * m_width); }