[ 
https://issues.apache.org/jira/browse/PDFBOX-2042?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13979838#comment-13979838
 ] 

Tilman Hausherr commented on PDFBOX-2042:
-----------------------------------------

Thanks for the test.

Ouch!!! "private COSArray getRangeArray(int n)" does something sometimes _and_ 
returns something, and has two bugs. What I think was intended was to extend 
the range array with default values when needed. However the default values are 
the wrong ones (should be 0 1 according to the spec, not -100 100, this is for 
LAB), and the array extension isn't done because of an off-by-one mistake. I 
have committed a fix in rev 1589767 for the trunk and rev 1589769 for the 1.8 
branch.

Before:
{code}
    private COSArray getRangeArray(int n)
    {
        COSArray rangeArray = 
(COSArray)stream.getStream().getDictionaryObject(COSName.RANGE);
        if(rangeArray == null)
        {
            rangeArray = new COSArray();
            stream.getStream().setItem(COSName.RANGE, rangeArray);
            while(rangeArray.size() < n*2)
            {
                rangeArray.add(new COSFloat(-100));
                rangeArray.add(new COSFloat(100));
            }
        }
        return rangeArray;
    }
{code}

After:

{code}
    /**
     * Get the range array, create and fill it with default values (0, 1) if
     * needed so that it has enough value pairs for the position.
     *
     * @param pos The zero-based position that should exist after this call is
     * completed.
     * @return A valid range array.
     */
    private COSArray getRangeArray(int pos)
    {
        //TODO per "clean code", a method should either 
        // return something or modify something, but not both.
        COSArray rangeArray = 
(COSArray)stream.getStream().getDictionaryObject(COSName.RANGE);
        if(rangeArray == null)
        {
            rangeArray = new COSArray();
            stream.getStream().setItem(COSName.RANGE, rangeArray);
        }
        // extend range array with default values if needed
        while (rangeArray.size() < (pos + 1) * 2)
        {
            rangeArray.add(new COSFloat(0));
            rangeArray.add(new COSFloat(1));
        }        
        return rangeArray;
    }
{code}

I will try to create a better fix later this week that returns default values 
if the array doesn't exist or is too small, and creates a correctly sized array 
for writing operations. This will have the advantage that PDF files don't get 
longer, i.e. don't have unneeded default range arrays. (This fix creates a 
default range array)

Btw this bug also resulted in an exception in TestExtractText.

The fixed libs will appear within a few hours here:
https://repository.apache.org/content/groups/snapshots/org/apache/pdfbox/pdfbox/1.8.5-SNAPSHOT/

> ColorSpace with empty Range array
> ---------------------------------
>
>                 Key: PDFBOX-2042
>                 URL: https://issues.apache.org/jira/browse/PDFBOX-2042
>             Project: PDFBox
>          Issue Type: Bug
>          Components: PDModel
>    Affects Versions: 1.8.4, 1.8.5, 2.0.0
>            Reporter: Juraj Lonc
>            Assignee: Tilman Hausherr
>             Fix For: 1.8.5, 2.0.0
>
>         Attachments: ModifyTest.java, pdfbox18.pdf, pdfbox20.pdf
>
>
> I have PDF document where I am modifying PDPage content stream.
> Saved document is invalid (Adobe reader complains about it).
> I have narrowed it down to ColorSpace. 
> Original document has colorspace:
> /ColorSpace <<
> /Cs6 [/ICCBased <<
> /Alternate /DeviceRGB
> /Filter /FlateDecode
> /Length 2597
> /N 3
> >>]>>
> Modified document has colorspace:
> /ColorSpace <<
> /Cs6 [/ICCBased <<
> /Alternate /DeviceRGB
> /Filter /FlateDecode
> /Length 2597
> /N 3
> /Range []
> >>]>>
> When I manually remove "/Range []" from PDF then Adobe reader opens it 
> without an error.
> Obviously that range is added by calling PDICCBased.getRangeArray(0) 
> somewhere.



--
This message was sent by Atlassian JIRA
(v6.2#6252)

Reply via email to