OLD | NEW |
1 // Copyright 2014 PDFium Authors. All rights reserved. | 1 // Copyright 2014 PDFium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
6 | 6 |
7 #include "core/fpdfapi/fpdf_page/pageint.h" | 7 #include "core/fpdfapi/fpdf_page/pageint.h" |
8 | 8 |
9 #include <limits.h> | 9 #include <limits.h> |
10 | 10 |
(...skipping 473 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
484 return ((x - xmin) * (ymax - ymin) / (xmax - xmin)) + ymin; | 484 return ((x - xmin) * (ymax - ymin) / (xmax - xmin)) + ymin; |
485 } | 485 } |
486 static uint32_t _GetBits32(const uint8_t* pData, int bitpos, int nbits) { | 486 static uint32_t _GetBits32(const uint8_t* pData, int bitpos, int nbits) { |
487 int result = 0; | 487 int result = 0; |
488 for (int i = 0; i < nbits; i++) | 488 for (int i = 0; i < nbits; i++) |
489 if (pData[(bitpos + i) / 8] & (1 << (7 - (bitpos + i) % 8))) { | 489 if (pData[(bitpos + i) / 8] & (1 << (7 - (bitpos + i) % 8))) { |
490 result |= 1 << (nbits - i - 1); | 490 result |= 1 << (nbits - i - 1); |
491 } | 491 } |
492 return result; | 492 return result; |
493 } | 493 } |
494 typedef struct { | |
495 FX_FLOAT encode_max, encode_min; | |
496 int sizes; | |
497 } SampleEncodeInfo; | |
498 typedef struct { FX_FLOAT decode_max, decode_min; } SampleDecodeInfo; | |
499 | 494 |
500 class CPDF_SampledFunc : public CPDF_Function { | 495 class CPDF_PSFunc : public CPDF_Function { |
501 public: | 496 public: |
502 CPDF_SampledFunc(); | 497 CPDF_PSFunc() : CPDF_Function(Type::kType4PostScript) {} |
503 ~CPDF_SampledFunc() override; | |
504 | |
505 // CPDF_Function | 498 // CPDF_Function |
506 FX_BOOL v_Init(CPDF_Object* pObj) override; | 499 FX_BOOL v_Init(CPDF_Object* pObj) override; |
507 FX_BOOL v_Call(FX_FLOAT* inputs, FX_FLOAT* results) const override; | 500 FX_BOOL v_Call(FX_FLOAT* inputs, FX_FLOAT* results) const override; |
508 | 501 |
509 SampleEncodeInfo* m_pEncodeInfo; | 502 private: |
510 SampleDecodeInfo* m_pDecodeInfo; | 503 CPDF_PSEngine m_PS; |
511 uint32_t m_nBitsPerSample; | |
512 uint32_t m_SampleMax; | |
513 CPDF_StreamAcc* m_pSampleStream; | |
514 }; | 504 }; |
515 | 505 |
| 506 FX_BOOL CPDF_PSFunc::v_Init(CPDF_Object* pObj) { |
| 507 CPDF_StreamAcc acc; |
| 508 acc.LoadAllData(pObj->AsStream(), FALSE); |
| 509 return m_PS.Parse(reinterpret_cast<const FX_CHAR*>(acc.GetData()), |
| 510 acc.GetSize()); |
| 511 } |
| 512 |
| 513 FX_BOOL CPDF_PSFunc::v_Call(FX_FLOAT* inputs, FX_FLOAT* results) const { |
| 514 CPDF_PSEngine& PS = const_cast<CPDF_PSEngine&>(m_PS); |
| 515 PS.Reset(); |
| 516 for (uint32_t i = 0; i < m_nInputs; i++) |
| 517 PS.Push(inputs[i]); |
| 518 PS.Execute(); |
| 519 if (PS.GetStackSize() < m_nOutputs) |
| 520 return FALSE; |
| 521 for (uint32_t i = 0; i < m_nOutputs; i++) |
| 522 results[m_nOutputs - i - 1] = PS.Pop(); |
| 523 return TRUE; |
| 524 } |
| 525 |
| 526 } // namespace |
| 527 |
516 CPDF_SampledFunc::CPDF_SampledFunc() : CPDF_Function(Type::kType0Sampled) { | 528 CPDF_SampledFunc::CPDF_SampledFunc() : CPDF_Function(Type::kType0Sampled) { |
517 m_pSampleStream = NULL; | 529 m_pSampleStream = NULL; |
518 m_pEncodeInfo = NULL; | 530 m_pEncodeInfo = NULL; |
519 m_pDecodeInfo = NULL; | 531 m_pDecodeInfo = NULL; |
520 } | 532 } |
521 | 533 |
522 CPDF_SampledFunc::~CPDF_SampledFunc() { | 534 CPDF_SampledFunc::~CPDF_SampledFunc() { |
523 delete m_pSampleStream; | 535 delete m_pSampleStream; |
524 FX_Free(m_pEncodeInfo); | 536 FX_Free(m_pEncodeInfo); |
525 FX_Free(m_pDecodeInfo); | 537 FX_Free(m_pDecodeInfo); |
526 } | 538 } |
| 539 |
527 FX_BOOL CPDF_SampledFunc::v_Init(CPDF_Object* pObj) { | 540 FX_BOOL CPDF_SampledFunc::v_Init(CPDF_Object* pObj) { |
528 CPDF_Stream* pStream = pObj->AsStream(); | 541 CPDF_Stream* pStream = pObj->AsStream(); |
529 if (!pStream) | 542 if (!pStream) |
530 return false; | 543 return false; |
531 | 544 |
532 CPDF_Dictionary* pDict = pStream->GetDict(); | 545 CPDF_Dictionary* pDict = pStream->GetDict(); |
533 CPDF_Array* pSize = pDict->GetArrayBy("Size"); | 546 CPDF_Array* pSize = pDict->GetArrayBy("Size"); |
534 CPDF_Array* pEncode = pDict->GetArrayBy("Encode"); | 547 CPDF_Array* pEncode = pDict->GetArrayBy("Encode"); |
535 CPDF_Array* pDecode = pDict->GetArrayBy("Decode"); | 548 CPDF_Array* pDecode = pDict->GetArrayBy("Decode"); |
536 m_nBitsPerSample = pDict->GetIntegerBy("BitsPerSample"); | 549 m_nBitsPerSample = pDict->GetIntegerBy("BitsPerSample"); |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
572 if (pDecode) { | 585 if (pDecode) { |
573 m_pDecodeInfo[i].decode_min = pDecode->GetFloatAt(2 * i); | 586 m_pDecodeInfo[i].decode_min = pDecode->GetFloatAt(2 * i); |
574 m_pDecodeInfo[i].decode_max = pDecode->GetFloatAt(2 * i + 1); | 587 m_pDecodeInfo[i].decode_max = pDecode->GetFloatAt(2 * i + 1); |
575 } else { | 588 } else { |
576 m_pDecodeInfo[i].decode_min = m_pRanges[i * 2]; | 589 m_pDecodeInfo[i].decode_min = m_pRanges[i * 2]; |
577 m_pDecodeInfo[i].decode_max = m_pRanges[i * 2 + 1]; | 590 m_pDecodeInfo[i].decode_max = m_pRanges[i * 2 + 1]; |
578 } | 591 } |
579 } | 592 } |
580 return TRUE; | 593 return TRUE; |
581 } | 594 } |
| 595 |
582 FX_BOOL CPDF_SampledFunc::v_Call(FX_FLOAT* inputs, FX_FLOAT* results) const { | 596 FX_BOOL CPDF_SampledFunc::v_Call(FX_FLOAT* inputs, FX_FLOAT* results) const { |
583 int pos = 0; | 597 int pos = 0; |
584 CFX_FixedBufGrow<FX_FLOAT, 16> encoded_input_buf(m_nInputs); | 598 CFX_FixedBufGrow<FX_FLOAT, 16> encoded_input_buf(m_nInputs); |
585 FX_FLOAT* encoded_input = encoded_input_buf; | 599 FX_FLOAT* encoded_input = encoded_input_buf; |
586 CFX_FixedBufGrow<int, 32> int_buf(m_nInputs * 2); | 600 CFX_FixedBufGrow<uint32_t, 32> int_buf(m_nInputs * 2); |
587 int* index = int_buf; | 601 uint32_t* index = int_buf; |
588 int* blocksize = index + m_nInputs; | 602 uint32_t* blocksize = index + m_nInputs; |
589 for (uint32_t i = 0; i < m_nInputs; i++) { | 603 for (uint32_t i = 0; i < m_nInputs; i++) { |
590 if (i == 0) | 604 if (i == 0) |
591 blocksize[i] = 1; | 605 blocksize[i] = 1; |
592 else | 606 else |
593 blocksize[i] = blocksize[i - 1] * m_pEncodeInfo[i - 1].sizes; | 607 blocksize[i] = blocksize[i - 1] * m_pEncodeInfo[i - 1].sizes; |
594 encoded_input[i] = PDF_Interpolate( | 608 encoded_input[i] = PDF_Interpolate( |
595 inputs[i], m_pDomains[i * 2], m_pDomains[i * 2 + 1], | 609 inputs[i], m_pDomains[i * 2], m_pDomains[i * 2 + 1], |
596 m_pEncodeInfo[i].encode_min, m_pEncodeInfo[i].encode_max); | 610 m_pEncodeInfo[i].encode_min, m_pEncodeInfo[i].encode_max); |
597 index[i] = (int)encoded_input[i]; | 611 index[i] = std::min((uint32_t)std::max(0.f, encoded_input[i]), |
598 if (index[i] < 0) | 612 m_pEncodeInfo[i].sizes - 1); |
599 index[i] = 0; | |
600 else if (index[i] > m_pEncodeInfo[i].sizes - 1) | |
601 index[i] = m_pEncodeInfo[i].sizes - 1; | |
602 pos += index[i] * blocksize[i]; | 613 pos += index[i] * blocksize[i]; |
603 } | 614 } |
604 FX_SAFE_INT32 bits_to_output = m_nOutputs; | 615 FX_SAFE_INT32 bits_to_output = m_nOutputs; |
605 bits_to_output *= m_nBitsPerSample; | 616 bits_to_output *= m_nBitsPerSample; |
606 if (!bits_to_output.IsValid()) { | 617 if (!bits_to_output.IsValid()) { |
607 return FALSE; | 618 return FALSE; |
608 } | 619 } |
609 FX_SAFE_INT32 bitpos = pos; | 620 FX_SAFE_INT32 bitpos = pos; |
610 bitpos *= bits_to_output.ValueOrDie(); | 621 bitpos *= bits_to_output.ValueOrDie(); |
611 if (!bitpos.IsValid()) { | 622 if (!bitpos.IsValid()) { |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
643 ((FX_FLOAT)sample1 - (FX_FLOAT)sample); | 654 ((FX_FLOAT)sample1 - (FX_FLOAT)sample); |
644 } | 655 } |
645 } | 656 } |
646 results[j] = PDF_Interpolate(encoded, 0, (FX_FLOAT)m_SampleMax, | 657 results[j] = PDF_Interpolate(encoded, 0, (FX_FLOAT)m_SampleMax, |
647 m_pDecodeInfo[j].decode_min, | 658 m_pDecodeInfo[j].decode_min, |
648 m_pDecodeInfo[j].decode_max); | 659 m_pDecodeInfo[j].decode_max); |
649 } | 660 } |
650 return TRUE; | 661 return TRUE; |
651 } | 662 } |
652 | 663 |
653 class CPDF_PSFunc : public CPDF_Function { | |
654 public: | |
655 // CPDF_Function | |
656 CPDF_PSFunc() : CPDF_Function(Type::kType4PostScript) {} | |
657 FX_BOOL v_Init(CPDF_Object* pObj) override; | |
658 FX_BOOL v_Call(FX_FLOAT* inputs, FX_FLOAT* results) const override; | |
659 | |
660 CPDF_PSEngine m_PS; | |
661 }; | |
662 | |
663 FX_BOOL CPDF_PSFunc::v_Init(CPDF_Object* pObj) { | |
664 CPDF_Stream* pStream = pObj->AsStream(); | |
665 CPDF_StreamAcc acc; | |
666 acc.LoadAllData(pStream, FALSE); | |
667 return m_PS.Parse((const FX_CHAR*)acc.GetData(), acc.GetSize()); | |
668 } | |
669 FX_BOOL CPDF_PSFunc::v_Call(FX_FLOAT* inputs, FX_FLOAT* results) const { | |
670 CPDF_PSEngine& PS = (CPDF_PSEngine&)m_PS; | |
671 PS.Reset(); | |
672 for (uint32_t i = 0; i < m_nInputs; i++) | |
673 PS.Push(inputs[i]); | |
674 PS.Execute(); | |
675 if (PS.GetStackSize() < m_nOutputs) | |
676 return FALSE; | |
677 for (uint32_t i = 0; i < m_nOutputs; i++) | |
678 results[m_nOutputs - i - 1] = PS.Pop(); | |
679 return TRUE; | |
680 } | |
681 | |
682 } // namespace | |
683 | |
684 CPDF_ExpIntFunc::CPDF_ExpIntFunc() | 664 CPDF_ExpIntFunc::CPDF_ExpIntFunc() |
685 : CPDF_Function(Type::kType2ExpotentialInterpolation) { | 665 : CPDF_Function(Type::kType2ExpotentialInterpolation) { |
686 m_pBeginValues = NULL; | 666 m_pBeginValues = NULL; |
687 m_pEndValues = NULL; | 667 m_pEndValues = NULL; |
688 } | 668 } |
689 | 669 |
690 CPDF_ExpIntFunc::~CPDF_ExpIntFunc() { | 670 CPDF_ExpIntFunc::~CPDF_ExpIntFunc() { |
691 FX_Free(m_pBeginValues); | 671 FX_Free(m_pBeginValues); |
692 FX_Free(m_pEndValues); | 672 FX_Free(m_pEndValues); |
693 } | 673 } |
(...skipping 211 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
905 if (m_pRanges) { | 885 if (m_pRanges) { |
906 for (uint32_t i = 0; i < m_nOutputs; i++) { | 886 for (uint32_t i = 0; i < m_nOutputs; i++) { |
907 if (results[i] < m_pRanges[i * 2]) | 887 if (results[i] < m_pRanges[i * 2]) |
908 results[i] = m_pRanges[i * 2]; | 888 results[i] = m_pRanges[i * 2]; |
909 else if (results[i] > m_pRanges[i * 2 + 1]) | 889 else if (results[i] > m_pRanges[i * 2 + 1]) |
910 results[i] = m_pRanges[i * 2 + 1]; | 890 results[i] = m_pRanges[i * 2 + 1]; |
911 } | 891 } |
912 } | 892 } |
913 return TRUE; | 893 return TRUE; |
914 } | 894 } |
OLD | NEW |