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.
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
9 #include "../../../include/fpdfapi/fpdf_page.h"
10 #include "../../../include/fpdfapi/fpdf_module.h"
11 #include "../../../src/fxcrt/fx_safe_types.h"
12 #include "../../../third_party/base/numerics/safe_conversions_impl.h"
16 typedef enum {PSOP_ADD, PSOP_SUB, PSOP_MUL, PSOP_DIV, PSOP_IDIV, PSOP_MOD,
17 PSOP_NEG, PSOP_ABS, PSOP_CEILING, PSOP_FLOOR, PSOP_ROUND, PSOP_TRUNCATE,
18 PSOP_SQRT, PSOP_SIN, PSOP_COS, PSOP_ATAN, PSOP_EXP, PSOP_LN, PSOP_LOG,
19 PSOP_CVI, PSOP_CVR, PSOP_EQ, PSOP_NE, PSOP_GT, PSOP_GE, PSOP_LT, PSOP_LE,
20 PSOP_AND, PSOP_OR, PSOP_XOR, PSOP_NOT, PSOP_BITSHIFT, PSOP_TRUE, PSOP_FALSE,
21 PSOP_IF, PSOP_IFELSE, PSOP_POP, PSOP_EXCH, PSOP_DUP, PSOP_COPY,
22 PSOP_INDEX, PSOP_ROLL, PSOP_PROC, PSOP_CONST
28 FX_BOOL Parse(CPDF_SimpleParser& parser);
29 FX_BOOL Execute(CPDF_PSEngine* pEngine);
30 CFX_PtrArray m_Operators;
32 #define PSENGINE_STACKSIZE 100
38 FX_BOOL Parse(const FX_CHAR* string, int size);
41 return m_MainProc.Execute(this);
43 FX_BOOL DoOperator(PDF_PSOP op);
48 void Push(FX_FLOAT value);
51 Push((FX_FLOAT)value);
59 FX_FLOAT m_Stack[PSENGINE_STACKSIZE];
61 CPDF_PSProc m_MainProc;
63 CPDF_PSProc::~CPDF_PSProc()
65 int size = m_Operators.GetSize();
66 for (int i = 0; i < size; i ++) {
67 if (m_Operators[i] == (FX_LPVOID)PSOP_PROC) {
68 delete (CPDF_PSProc*)m_Operators[i + 1];
70 } else if (m_Operators[i] == (FX_LPVOID)PSOP_CONST) {
71 FX_Free((FX_FLOAT*)m_Operators[i + 1]);
76 #pragma optimize( "", off )
77 FX_BOOL CPDF_PSProc::Execute(CPDF_PSEngine* pEngine)
79 int size = m_Operators.GetSize();
80 for (int i = 0; i < size; i ++) {
81 PDF_PSOP op = (PDF_PSOP)(FX_UINTPTR)m_Operators[i];
82 if (op == PSOP_PROC) {
84 } else if (op == PSOP_CONST) {
85 pEngine->Push(*(FX_FLOAT*)m_Operators[i + 1]);
87 } else if (op == PSOP_IF) {
88 if (i < 2 || m_Operators[i - 2] != (FX_LPVOID)PSOP_PROC) {
91 if ((int)pEngine->Pop()) {
92 ((CPDF_PSProc*)m_Operators[i - 1])->Execute(pEngine);
94 } else if (op == PSOP_IFELSE) {
95 if (i < 4 || m_Operators[i - 2] != (FX_LPVOID)PSOP_PROC ||
96 m_Operators[i - 4] != (FX_LPVOID)PSOP_PROC) {
99 if ((int)pEngine->Pop()) {
100 ((CPDF_PSProc*)m_Operators[i - 3])->Execute(pEngine);
102 ((CPDF_PSProc*)m_Operators[i - 1])->Execute(pEngine);
105 pEngine->DoOperator(op);
110 #pragma optimize( "", on )
111 CPDF_PSEngine::CPDF_PSEngine()
115 CPDF_PSEngine::~CPDF_PSEngine()
118 void CPDF_PSEngine::Push(FX_FLOAT v)
120 if (m_StackCount == 100) {
123 m_Stack[m_StackCount++] = v;
125 FX_FLOAT CPDF_PSEngine::Pop()
127 if (m_StackCount == 0) {
130 return m_Stack[--m_StackCount];
132 const struct _PDF_PSOpName {
135 } _PDF_PSOpNames[] = {
136 {"add", PSOP_ADD}, {"sub", PSOP_SUB}, {"mul", PSOP_MUL}, {"div", PSOP_DIV},
137 {"idiv", PSOP_IDIV}, {"mod", PSOP_MOD}, {"neg", PSOP_NEG}, {"abs", PSOP_ABS},
138 {"ceiling", PSOP_CEILING}, {"floor", PSOP_FLOOR}, {"round", PSOP_ROUND},
139 {"truncate", PSOP_TRUNCATE}, {"sqrt", PSOP_SQRT}, {"sin", PSOP_SIN},
140 {"cos", PSOP_COS}, {"atan", PSOP_ATAN}, {"exp", PSOP_EXP}, {"ln", PSOP_LN},
141 {"log", PSOP_LOG}, {"cvi", PSOP_CVI}, {"cvr", PSOP_CVR}, {"eq", PSOP_EQ},
142 {"ne", PSOP_NE}, {"gt", PSOP_GT}, {"ge", PSOP_GE}, {"lt", PSOP_LT},
143 {"le", PSOP_LE}, {"and", PSOP_AND}, {"or", PSOP_OR}, {"xor", PSOP_XOR},
144 {"not", PSOP_NOT}, {"bitshift", PSOP_BITSHIFT}, {"true", PSOP_TRUE},
145 {"false", PSOP_FALSE}, {"if", PSOP_IF}, {"ifelse", PSOP_IFELSE},
146 {"pop", PSOP_POP}, {"exch", PSOP_EXCH}, {"dup", PSOP_DUP},
147 {"copy", PSOP_COPY}, {"index", PSOP_INDEX}, {"roll", PSOP_ROLL},
150 FX_BOOL CPDF_PSEngine::Parse(const FX_CHAR* string, int size)
152 CPDF_SimpleParser parser((FX_LPBYTE)string, size);
153 CFX_ByteStringC word = parser.GetWord();
154 if (word != FX_BSTRC("{")) {
157 return m_MainProc.Parse(parser);
159 FX_BOOL CPDF_PSProc::Parse(CPDF_SimpleParser& parser)
162 CFX_ByteStringC word = parser.GetWord();
163 if (word.IsEmpty()) {
166 if (word == FX_BSTRC("}")) {
169 if (word == FX_BSTRC("{")) {
170 CPDF_PSProc* pProc = new CPDF_PSProc;
171 m_Operators.Add((FX_LPVOID)PSOP_PROC);
172 m_Operators.Add(pProc);
173 if (!pProc->Parse(parser)) {
178 while (_PDF_PSOpNames[i].name) {
179 if (word == CFX_ByteStringC(_PDF_PSOpNames[i].name)) {
180 m_Operators.Add((FX_LPVOID)_PDF_PSOpNames[i].op);
185 if (_PDF_PSOpNames[i].name == NULL) {
186 FX_FLOAT* pd = FX_Alloc(FX_FLOAT, 1);
188 m_Operators.Add((FX_LPVOID)PSOP_CONST);
194 #define PI 3.1415926535897932384626433832795f
195 FX_BOOL CPDF_PSEngine::DoOperator(PDF_PSOP op)
236 Push((FX_FLOAT)FXSYS_fabs(d1));
240 Push((FX_FLOAT)FXSYS_ceil(d1));
244 Push((FX_FLOAT)FXSYS_floor(d1));
248 Push(FXSYS_round(d1));
256 Push((FX_FLOAT)FXSYS_sqrt(d1));
260 Push((FX_FLOAT)FXSYS_sin(d1 * PI / 180.0f));
264 Push((FX_FLOAT)FXSYS_cos(d1 * PI / 180.0f));
269 d1 = (FX_FLOAT)(FXSYS_atan2(d1, d2) * 180.0 / PI);
278 Push((FX_FLOAT)FXSYS_pow(d1, d2));
282 Push((FX_FLOAT)FXSYS_log(d1));
286 Push((FX_FLOAT)FXSYS_log10(d1));
297 Push((int)(d1 == d2));
302 Push((int)(d1 != d2));
307 Push((int)(d1 > d2));
312 Push((int)(d1 >= d2));
317 Push((int)(d1 < d2));
322 Push((int)(d1 <= d2));
343 case PSOP_BITSHIFT: {
344 int shift = (int)Pop();
375 if (n < 0 || n > PSENGINE_STACKSIZE || m_StackCount + n > PSENGINE_STACKSIZE || n > m_StackCount) {
378 for (int i = 0; i < n; i ++) {
379 m_Stack[m_StackCount + i] = m_Stack[m_StackCount + i - n];
386 if (n < 0 || n >= m_StackCount) {
389 Push(m_Stack[m_StackCount - n - 1]);
395 if (m_StackCount == 0) {
398 if (n < 0 || n > m_StackCount) {
402 for (int i = 0; i < -j; i ++) {
403 FX_FLOAT first = m_Stack[m_StackCount - n];
404 for (int ii = 0; ii < n - 1; ii ++) {
405 m_Stack[m_StackCount - n + ii] = m_Stack[m_StackCount - n + ii + 1];
407 m_Stack[m_StackCount - 1] = first;
410 for (int i = 0; i < j; i ++) {
411 FX_FLOAT last = m_Stack[m_StackCount - 1];
413 for (ii = 0; ii < n - 1; ii ++) {
414 m_Stack[m_StackCount - ii - 1] = m_Stack[m_StackCount - ii - 2];
416 m_Stack[m_StackCount - ii - 1] = last;
425 static FX_FLOAT PDF_Interpolate(FX_FLOAT x, FX_FLOAT xmin, FX_FLOAT xmax, FX_FLOAT ymin, FX_FLOAT ymax)
427 return ((x - xmin) * (ymax - ymin) / (xmax - xmin)) + ymin;
429 static FX_DWORD _GetBits32(FX_LPCBYTE pData, int bitpos, int nbits)
432 for (int i = 0; i < nbits; i ++)
433 if (pData[(bitpos + i) / 8] & (1 << (7 - (bitpos + i) % 8))) {
434 result |= 1 << (nbits - i - 1);
439 FX_FLOAT encode_max, encode_min;
443 FX_FLOAT decode_max, decode_min;
445 class CPDF_SampledFunc : public CPDF_Function
449 virtual ~CPDF_SampledFunc();
450 virtual FX_BOOL v_Init(CPDF_Object* pObj);
451 virtual FX_BOOL v_Call(FX_FLOAT* inputs, FX_FLOAT* results) const;
452 SampleEncodeInfo* m_pEncodeInfo;
453 SampleDecodeInfo* m_pDecodeInfo;
454 FX_DWORD m_nBitsPerSample, m_SampleMax;
455 CPDF_StreamAcc* m_pSampleStream;
457 CPDF_SampledFunc::CPDF_SampledFunc()
459 m_pSampleStream = NULL;
460 m_pEncodeInfo = NULL;
461 m_pDecodeInfo = NULL;
463 CPDF_SampledFunc::~CPDF_SampledFunc()
465 if (m_pSampleStream) {
466 delete m_pSampleStream;
469 FX_Free(m_pEncodeInfo);
472 FX_Free(m_pDecodeInfo);
475 FX_BOOL CPDF_SampledFunc::v_Init(CPDF_Object* pObj)
477 if (pObj->GetType() != PDFOBJ_STREAM) {
480 CPDF_Stream* pStream = (CPDF_Stream*)pObj;
481 CPDF_Dictionary* pDict = pStream->GetDict();
482 CPDF_Array* pSize = pDict->GetArray(FX_BSTRC("Size"));
483 CPDF_Array* pEncode = pDict->GetArray(FX_BSTRC("Encode"));
484 CPDF_Array* pDecode = pDict->GetArray(FX_BSTRC("Decode"));
485 m_nBitsPerSample = pDict->GetInteger(FX_BSTRC("BitsPerSample"));
486 m_SampleMax = 0xffffffff >> (32 - m_nBitsPerSample);
487 m_pSampleStream = new CPDF_StreamAcc;
488 m_pSampleStream->LoadAllData(pStream, FALSE);
489 m_pEncodeInfo = FX_Alloc(SampleEncodeInfo, m_nInputs);
491 FX_DWORD nTotalSamples = 1;
492 for (i = 0; i < m_nInputs; i ++) {
493 m_pEncodeInfo[i].sizes = pSize ? pSize->GetInteger(i) : 0;
494 if (!pSize && i == 0) {
495 m_pEncodeInfo[i].sizes = pDict->GetInteger(FX_BSTRC("Size"));
497 if (nTotalSamples > 0 && (FX_UINT32)(m_pEncodeInfo[i].sizes) > UINT_MAX / nTotalSamples) {
500 nTotalSamples *= m_pEncodeInfo[i].sizes;
502 m_pEncodeInfo[i].encode_min = pEncode->GetFloat(i * 2);
503 m_pEncodeInfo[i].encode_max = pEncode->GetFloat(i * 2 + 1);
505 m_pEncodeInfo[i].encode_min = 0;
506 if (m_pEncodeInfo[i].sizes == 1) {
507 m_pEncodeInfo[i].encode_max = 1;
509 m_pEncodeInfo[i].encode_max = (FX_FLOAT)m_pEncodeInfo[i].sizes - 1;
513 if (nTotalSamples > 0 && m_nBitsPerSample > UINT_MAX / nTotalSamples) {
516 nTotalSamples *= m_nBitsPerSample;
517 if (nTotalSamples > 0 && ((FX_UINT32)m_nOutputs) > UINT_MAX / nTotalSamples) {
520 nTotalSamples *= m_nOutputs;
521 if (nTotalSamples == 0 || m_pSampleStream->GetSize() * 8 < nTotalSamples) {
524 m_pDecodeInfo = FX_Alloc(SampleDecodeInfo, m_nOutputs);
525 for (i = 0; i < m_nOutputs; i ++) {
527 m_pDecodeInfo[i].decode_min = pDecode->GetFloat(2 * i);
528 m_pDecodeInfo[i].decode_max = pDecode->GetFloat(2 * i + 1);
530 m_pDecodeInfo[i].decode_min = m_pRanges[i * 2];
531 m_pDecodeInfo[i].decode_max = m_pRanges[i * 2 + 1];
536 FX_BOOL CPDF_SampledFunc::v_Call(FX_FLOAT* inputs, FX_FLOAT* results) const
539 CFX_FixedBufGrow<FX_FLOAT, 16> encoded_input_buf(m_nInputs);
540 FX_FLOAT* encoded_input = encoded_input_buf;
541 CFX_FixedBufGrow<int, 32> int_buf(m_nInputs * 2);
542 int* index = int_buf;
543 int* blocksize = index + m_nInputs;
544 for (int i = 0; i < m_nInputs; i ++) {
548 blocksize[i] = blocksize[i - 1] * m_pEncodeInfo[i - 1].sizes;
550 encoded_input[i] = PDF_Interpolate(inputs[i], m_pDomains[i * 2], m_pDomains[i * 2 + 1],
551 m_pEncodeInfo[i].encode_min, m_pEncodeInfo[i].encode_max);
552 index[i] = (int)encoded_input[i];
555 } else if (index[i] > m_pEncodeInfo[i].sizes - 1) {
556 index[i] = m_pEncodeInfo[i].sizes - 1;
558 pos += index[i] * blocksize[i];
560 FX_SAFE_INT32 bitpos = pos;
561 bitpos *= m_nBitsPerSample;
562 bitpos *= m_nOutputs;
563 if (!bitpos.IsValid()) {
566 FX_LPCBYTE pSampleData = m_pSampleStream->GetData();
567 if (pSampleData == NULL) {
570 FX_SAFE_INT32 bitpos1 = m_nOutputs - 1 > 0 ? m_nOutputs - 1 : 0;
571 bitpos1 *= m_nBitsPerSample;
572 bitpos1 += bitpos.ValueOrDie();
573 if (!bitpos1.IsValid()) {
576 for (int j = 0; j < m_nOutputs; j ++) {
577 FX_DWORD sample = _GetBits32(pSampleData, bitpos.ValueOrDie() + j * m_nBitsPerSample, m_nBitsPerSample);
578 FX_FLOAT encoded = (FX_FLOAT)sample;
579 for (int i = 0; i < m_nInputs; i ++) {
580 if (index[i] == m_pEncodeInfo[i].sizes - 1) {
582 encoded = encoded_input[i] * (FX_FLOAT)sample;
585 FX_SAFE_INT32 bitpos2 = blocksize[i];
587 bitpos2 *= m_nOutputs;
589 bitpos2 *= m_nBitsPerSample;
590 if (!bitpos2.IsValid()) {
593 FX_DWORD sample1 = _GetBits32(pSampleData, bitpos2.ValueOrDie(), m_nBitsPerSample);
594 encoded += (encoded_input[i] - index[i]) * ((FX_FLOAT)sample1 - (FX_FLOAT)sample);
597 results[j] = PDF_Interpolate(encoded, 0, (FX_FLOAT)m_SampleMax,
598 m_pDecodeInfo[j].decode_min, m_pDecodeInfo[j].decode_max);
602 class CPDF_PSFunc : public CPDF_Function
605 virtual FX_BOOL v_Init(CPDF_Object* pObj);
606 virtual FX_BOOL v_Call(FX_FLOAT* inputs, FX_FLOAT* results) const;
609 FX_BOOL CPDF_PSFunc::v_Init(CPDF_Object* pObj)
611 CPDF_Stream* pStream = (CPDF_Stream*)pObj;
613 acc.LoadAllData(pStream, FALSE);
614 return m_PS.Parse((const FX_CHAR*)acc.GetData(), acc.GetSize());
616 FX_BOOL CPDF_PSFunc::v_Call(FX_FLOAT* inputs, FX_FLOAT* results) const
618 CPDF_PSEngine& PS = (CPDF_PSEngine&)m_PS;
621 for (i = 0; i < m_nInputs; i ++) {
625 if (PS.GetStackSize() < m_nOutputs) {
628 for (i = 0; i < m_nOutputs; i ++) {
629 results[m_nOutputs - i - 1] = PS.Pop();
633 class CPDF_ExpIntFunc : public CPDF_Function
637 virtual ~CPDF_ExpIntFunc();
638 virtual FX_BOOL v_Init(CPDF_Object* pObj);
639 virtual FX_BOOL v_Call(FX_FLOAT* inputs, FX_FLOAT* results) const;
641 FX_FLOAT* m_pBeginValues;
642 FX_FLOAT* m_pEndValues;
645 CPDF_ExpIntFunc::CPDF_ExpIntFunc()
647 m_pBeginValues = NULL;
650 CPDF_ExpIntFunc::~CPDF_ExpIntFunc()
652 if (m_pBeginValues) {
653 FX_Free(m_pBeginValues);
656 FX_Free(m_pEndValues);
659 FX_BOOL CPDF_ExpIntFunc::v_Init(CPDF_Object* pObj)
661 CPDF_Dictionary* pDict = pObj->GetDict();
665 CPDF_Array* pArray0 = pDict->GetArray(FX_BSTRC("C0"));
666 if (m_nOutputs == 0) {
669 m_nOutputs = pArray0->GetCount();
672 CPDF_Array* pArray1 = pDict->GetArray(FX_BSTRC("C1"));
673 m_pBeginValues = FX_Alloc(FX_FLOAT, m_nOutputs * 2);
674 m_pEndValues = FX_Alloc(FX_FLOAT, m_nOutputs * 2);
675 for (int i = 0; i < m_nOutputs; i ++) {
676 m_pBeginValues[i] = pArray0 ? pArray0->GetFloat(i) : 0.0f;
677 m_pEndValues[i] = pArray1 ? pArray1->GetFloat(i) : 1.0f;
679 m_Exponent = pDict->GetFloat(FX_BSTRC("N"));
680 m_nOrigOutputs = m_nOutputs;
681 if (m_nOutputs && m_nInputs > INT_MAX / m_nOutputs) {
684 m_nOutputs *= m_nInputs;
687 FX_BOOL CPDF_ExpIntFunc::v_Call(FX_FLOAT* inputs, FX_FLOAT* results) const
689 for (int i = 0; i < m_nInputs; i ++)
690 for (int j = 0; j < m_nOrigOutputs; j ++) {
691 results[i * m_nOrigOutputs + j] = m_pBeginValues[j] + (FX_FLOAT)FXSYS_pow(inputs[i], m_Exponent) *
692 (m_pEndValues[j] - m_pBeginValues[j]);
696 class CPDF_StitchFunc : public CPDF_Function
700 virtual ~CPDF_StitchFunc();
701 virtual FX_BOOL v_Init(CPDF_Object* pObj);
702 virtual FX_BOOL v_Call(FX_FLOAT* inputs, FX_FLOAT* results) const;
704 CPDF_Function** m_pSubFunctions;
708 CPDF_StitchFunc::CPDF_StitchFunc()
711 m_pSubFunctions = NULL;
715 CPDF_StitchFunc::~CPDF_StitchFunc()
717 for (int i = 0; i < m_nSubs; i ++)
718 if (m_pSubFunctions[i]) {
719 delete m_pSubFunctions[i];
721 if (m_pSubFunctions) {
722 FX_Free(m_pSubFunctions);
731 FX_BOOL CPDF_StitchFunc::v_Init(CPDF_Object* pObj)
733 CPDF_Dictionary* pDict = pObj->GetDict();
737 CPDF_Array* pArray = pDict->GetArray(FX_BSTRC("Functions"));
738 if (pArray == NULL) {
741 m_nSubs = pArray->GetCount();
745 m_pSubFunctions = FX_Alloc(CPDF_Function*, m_nSubs);
748 for (i = 0; i < m_nSubs; i ++) {
749 CPDF_Object* pSub = pArray->GetElementValue(i);
753 m_pSubFunctions[i] = CPDF_Function::Load(pSub);
754 if (m_pSubFunctions[i] == NULL) {
757 if (m_pSubFunctions[i]->CountOutputs() > m_nOutputs) {
758 m_nOutputs = m_pSubFunctions[i]->CountOutputs();
761 m_pBounds = FX_Alloc(FX_FLOAT, m_nSubs + 1);
762 m_pBounds[0] = m_pDomains[0];
763 pArray = pDict->GetArray(FX_BSTRC("Bounds"));
764 if (pArray == NULL) {
767 for (i = 0; i < m_nSubs - 1; i ++) {
768 m_pBounds[i + 1] = pArray->GetFloat(i);
770 m_pBounds[m_nSubs] = m_pDomains[1];
771 m_pEncode = FX_Alloc(FX_FLOAT, m_nSubs * 2);
772 pArray = pDict->GetArray(FX_BSTRC("Encode"));
773 if (pArray == NULL) {
776 for (i = 0; i < m_nSubs * 2; i ++) {
777 m_pEncode[i] = pArray->GetFloat(i);
781 FX_BOOL CPDF_StitchFunc::v_Call(FX_FLOAT* inputs, FX_FLOAT* outputs) const
783 FX_FLOAT input = inputs[0];
785 for (i = 0; i < m_nSubs - 1; i ++)
786 if (input < m_pBounds[i + 1]) {
789 if (m_pSubFunctions[i] == NULL) {
792 input = PDF_Interpolate(input, m_pBounds[i], m_pBounds[i + 1], m_pEncode[i * 2], m_pEncode[i * 2 + 1]);
794 m_pSubFunctions[i]->Call(&input, m_nInputs, outputs, nresults);
797 CPDF_Function* CPDF_Function::Load(CPDF_Object* pFuncObj)
799 if (pFuncObj == NULL) {
802 CPDF_Function* pFunc = NULL;
804 if (pFuncObj->GetType() == PDFOBJ_STREAM) {
805 type = ((CPDF_Stream*)pFuncObj)->GetDict()->GetInteger(FX_BSTRC("FunctionType"));
806 } else if (pFuncObj->GetType() == PDFOBJ_DICTIONARY) {
807 type = ((CPDF_Dictionary*)pFuncObj)->GetInteger(FX_BSTRC("FunctionType"));
812 pFunc = new CPDF_SampledFunc;
813 } else if (type == 2) {
814 pFunc = new CPDF_ExpIntFunc;
815 } else if (type == 3) {
816 pFunc = new CPDF_StitchFunc;
817 } else if (type == 4) {
818 pFunc = new CPDF_PSFunc;
822 if (!pFunc->Init(pFuncObj)) {
828 CPDF_Function::CPDF_Function()
833 CPDF_Function::~CPDF_Function()
844 FX_BOOL CPDF_Function::Init(CPDF_Object* pObj)
846 CPDF_Dictionary* pDict;
847 if (pObj->GetType() == PDFOBJ_STREAM) {
848 pDict = ((CPDF_Stream*)pObj)->GetDict();
850 pDict = (CPDF_Dictionary*)pObj;
852 CPDF_Array* pDomains = pDict->GetArray(FX_BSTRC("Domain"));
853 if (pDomains == NULL) {
856 m_nInputs = pDomains->GetCount() / 2;
857 if (m_nInputs == 0) {
860 m_pDomains = FX_Alloc(FX_FLOAT, m_nInputs * 2);
861 for (int i = 0; i < m_nInputs * 2; i ++) {
862 m_pDomains[i] = pDomains->GetFloat(i);
864 CPDF_Array* pRanges = pDict->GetArray(FX_BSTRC("Range"));
867 m_nOutputs = pRanges->GetCount() / 2;
868 m_pRanges = FX_Alloc(FX_FLOAT, m_nOutputs * 2);
869 for (int i = 0; i < m_nOutputs * 2; i ++) {
870 m_pRanges[i] = pRanges->GetFloat(i);
873 FX_DWORD old_outputs = m_nOutputs;
874 FX_BOOL ret = v_Init(pObj);
875 if (m_pRanges && m_nOutputs > (int)old_outputs) {
876 m_pRanges = FX_Realloc(FX_FLOAT, m_pRanges, m_nOutputs * 2);
878 FXSYS_memset32(m_pRanges + (old_outputs * 2), 0, sizeof(FX_FLOAT) * (m_nOutputs - old_outputs) * 2);
883 FX_BOOL CPDF_Function::Call(FX_FLOAT* inputs, int ninputs, FX_FLOAT* results, int& nresults) const
885 if (m_nInputs != ninputs) {
888 nresults = m_nOutputs;
889 for (int i = 0; i < m_nInputs; i ++) {
890 if (inputs[i] < m_pDomains[i * 2]) {
891 inputs[i] = m_pDomains[i * 2];
892 } else if (inputs[i] > m_pDomains[i * 2 + 1]) {
893 inputs[i] = m_pDomains[i * 2] + 1;
896 v_Call(inputs, results);
898 for (int i = 0; i < m_nOutputs; i ++) {
899 if (results[i] < m_pRanges[i * 2]) {
900 results[i] = m_pRanges[i * 2];
901 } else if (results[i] > m_pRanges[i * 2 + 1]) {
902 results[i] = m_pRanges[i * 2 + 1];