/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.pdfbox.filter;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.pdfbox.cos.COSDictionary;
import org.apache.pdfbox.cos.COSName;
import org.apache.pdfbox.filter.ccitt.CCITTFaxG31DDecodeInputStream;
import org.apache.pdfbox.filter.ccitt.FillOrderChangeInputStream;
import org.apache.pdfbox.filter.ccitt.TIFFFaxDecoder;
import org.apache.pdfbox.io.IOUtils;

/**
 * Decodes image data that has been encoded using either Group 3 or Group 4
 * CCITT facsimile (fax) encoding.
 *
 * @author Ben Litchfield
 * @author Marcel Kammer
 * @author Paul King
 */
final class CCITTFaxFilter extends Filter
{
    private static final Log LOG = LogFactory.getLog(CCITTFaxFilter.class);

    @Override
    public DecodeResult decode(InputStream encoded, OutputStream decoded,
                                         COSDictionary parameters, int index) throws IOException
    {
        DecodeResult result = new DecodeResult(new COSDictionary());
        result.getParameters().addAll(parameters);

        // get decode parameters
        COSDictionary decodeParms = getDecodeParams(parameters, index);

        // parse dimensions
        int cols = decodeParms.getInt(COSName.COLUMNS, 1728);
        int rows = decodeParms.getInt(COSName.ROWS, 0);
        int height = parameters.getInt(COSName.HEIGHT, COSName.H, 0);
        if (rows > 0 && height > 0)
        {
            // ensure that rows doesn't contain implausible data, see PDFBOX-771
            rows = Math.min(rows, height);
        }
        else
        {
            // at least one of the values has to have a valid value
            rows = Math.max(rows, height);
        }

        // decompress data
        int k = decodeParms.getInt(COSName.K, 0);
        boolean encodedByteAlign = decodeParms.getBoolean(COSName.ENCODED_BYTE_ALIGN, false);
        int arraySize = (cols + 7) / 8 * rows;
        TIFFFaxDecoder faxDecoder = new TIFFFaxDecoder(1, cols, rows);
        // TODO possible options??
        long tiffOptions = 0;
        byte[] compressed = IOUtils.toByteArray(encoded);
        byte[] decompressed = null;
        if (k == 0)
        {
            InputStream in = new CCITTFaxG31DDecodeInputStream(
                    new ByteArrayInputStream(compressed), cols, rows, encodedByteAlign);
            in = new FillOrderChangeInputStream(in); //Decorate to change fill order
            decompressed = IOUtils.toByteArray(in);
            in.close();
        }
        else if (k > 0)
        {
            decompressed = new byte[arraySize];
            faxDecoder.decode2D(decompressed, compressed, 0, rows, tiffOptions);
        }
        else if (k < 0)
        {
            decompressed = new byte[arraySize];
            faxDecoder.decodeT6(decompressed, compressed, 0, rows, tiffOptions, encodedByteAlign);
        }

        // invert bitmap
        boolean blackIsOne = decodeParms.getBoolean(COSName.BLACK_IS_1, false);
        if (!blackIsOne)
        {
            // Inverting the bitmap
            // Note the previous approach with starting from an IndexColorModel didn't work
            // reliably. In some cases the image wouldn't be painted for some reason.
            // So a safe but slower approach was taken.
            invertBitmap(decompressed);
        }

        // repair missing color space
        if (!parameters.containsKey(COSName.COLORSPACE))
        {
            result.getParameters().setName(COSName.COLORSPACE, COSName.DEVICEGRAY.getName());
        }

        decoded.write(decompressed);
        return new DecodeResult(parameters);
    }

    private void invertBitmap(byte[] bufferData)
    {
        for (int i = 0, c = bufferData.length; i < c; i++)
        {
            bufferData[i] = (byte) (~bufferData[i] & 0xFF);
        }
    }

    @Override
    protected void encode(InputStream input, OutputStream encoded, COSDictionary parameters)
            throws IOException
    {
        LOG.warn("CCITTFaxDecode.encode is not implemented yet, skipping this stream.");
    }
}

@@ -2843,79 +2843,8 @@
 ws;%0A
-        TIFFFaxDecoder faxDecoder = new TIFFFaxDecoder(1, cols, rows);%0A
     
@@ -2908,66 +2908,8 @@
  0;%0A
-        byte%5B%5D compressed = IOUtils.toByteArray(encoded);%0A
     
@@ -3037,65 +3037,15 @@
 eam(
-%0A                    new ByteArrayInputStream(compress
+encod
 ed
-)
 , co
@@ -3129,40 +3129,8 @@
 in);
- //Decorate to change fill order
 %0A   
@@ -3228,29 +3228,155 @@
 else
- if (k %3E 0)%0A        %7B
+%0A        %7B%0A            TIFFFaxDecoder faxDecoder = new TIFFFaxDecoder(1, cols, rows);%0A            byte%5B%5D compressed = IOUtils.toByteArray(encoded);
 %0A   
@@ -3412,32 +3412,73 @@
 yte%5BarraySize%5D;%0A
+            if (k %3E 0)%0A            %7B%0A    
             faxD
@@ -3542,34 +3542,42 @@
 tions);%0A        
-%7D%0A
+    %7D%0A    
         else if 
@@ -3576,20 +3576,13 @@
 else
- if (k %3C 0)%0A
+%0A    
     
@@ -3603,44 +3603,25 @@
     
-decompressed = new byte%5BarraySize%5D;%0A
+    // k %3C 0%0A    
     
@@ -3707,32 +3707,46 @@
 odedByteAlign);%0A
+            %7D%0A
         %7D%0A%0A     

remove double code; avoid uneeded storage when K = 0