/*
 * 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.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;

/**
 * Decompresses data encoded using a DCT (discrete cosine transform)
 * technique based on the JPEG standard.
 *
 * @author John Hewson
 */
final class DCTFilter extends Filter
{
    private static final Log LOG = LogFactory.getLog(DCTFilter.class);

    @Override
    public final DecodeResult decode(InputStream encoded, OutputStream decoded,
                                         COSDictionary parameters, int index) throws IOException
    {
    	// Already ready, just read it back out
    	byte[] buffer = new byte[1024];
        int bytesRead;
        while ((bytesRead = encoded.read(buffer)) != -1)
        {
            decoded.write(buffer, 0, bytesRead);
        }
        
        return new DecodeResult(parameters);
    }
    
    // Anything past here probably won't be needed

    // reads the APP14 Adobe transform tag
//    private Integer getAdobeTransform(IIOMetadata metadata)
//    {
//        Element tree = (Element)metadata.getAsTree("javax_imageio_jpeg_image_1.0");
//        Element markerSequence = (Element)tree.getElementsByTagName("markerSequence").item(0);
//
//        if (markerSequence.getElementsByTagName("app14Adobe") != null)
//        {
//            Element adobe = (Element)markerSequence.getElementsByTagName("app14Adobe").item(0);
//            return Integer.parseInt(adobe.getAttribute("transform"));
//        }
//        return 0; // Unknown
//    }TODO

    // converts YCCK image to CMYK. YCCK is an equivalent encoding for
    // CMYK data, so no color management code is needed here, nor does the
    // PDF color space have to be consulted
//    private WritableRaster fromYCCKtoCMYK(Raster raster) throws IOException
//    {
//        WritableRaster writableRaster = raster.createCompatibleWritableRaster();
//
//        int[] value = new int[4];
//        for (int y = 0, height = raster.getHeight(); y < height; y++)
//        {
//            for (int x = 0, width = raster.getWidth(); x < width; x++)
//            {
//                raster.getPixel(x, y, value);
//
//                // 4-channels 0..255
//                float Y = value[0];
//                float Cb = value[1];
//                float Cr = value[2];
//                float K = value[3];
//
//                // YCCK to RGB, see http://software.intel.com/en-us/node/442744
//                int r = clamp(Y + 1.402f * Cr - 179.456f);
//                int g = clamp(Y - 0.34414f * Cb - 0.71414f * Cr + 135.45984f);
//                int b = clamp(Y + 1.772f * Cb - 226.816f);
//
//                // naive RGB to CMYK
//                int cyan = 255 - r;
//                int magenta = 255 - g;
//                int yellow = 255 - b;
//
//                // update new raster
//                value[0] = cyan;
//                value[1] = magenta;
//                value[2] = yellow;
//                value[3] = (int)K;
//                writableRaster.setPixel(x, y, value);
//            }
//        }
//        return writableRaster;
//    }TODO

    // converts from BGR to RGB
//    private WritableRaster fromBGRtoRGB(Raster raster) throws IOException
//    {
//        WritableRaster writableRaster = raster.createCompatibleWritableRaster();
//
//        int width = raster.getWidth();
//        int height = raster.getHeight();
//        int w3 = width * 3;
//        int[] tab = new int[w3];
//        //BEWARE: handling the full image at a time is slower than one line at a time        
//        for (int y = 0; y < height; y++)
//        {
//            raster.getPixels(0, y, width, 1, tab);
//            for (int off = 0; off < w3; off += 3)
//            {
//                int tmp = tab[off];
//                tab[off] = tab[off + 2];
//                tab[off + 2] = tmp;
//            }
//            writableRaster.setPixels(0, y, width, 1, tab);
//        }
//        return writableRaster;
//    } TODO

    // clamps value to 0-255 range
    private int clamp(float value)
    {
        return (int)((value < 0) ? 0 : ((value > 255) ? 255 : value));
    }

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

@@ -1070,16 +1070,54 @@
 ionary;%0D
+%0Aimport org.apache.pdfbox.io.IOUtils;%0D
 %0A%0D%0A/**%0D%0A
@@ -1623,16 +1623,18 @@
 ck out%0D%0A
+//
     %09byt
@@ -1663,16 +1663,18 @@
 1024%5D;%0D%0A
+//
         
@@ -1689,16 +1689,18 @@
 sRead;%0D%0A
+//
         
@@ -1749,16 +1749,18 @@
 != -1)%0D%0A
+//
         
@@ -1758,24 +1758,26 @@
 /        %7B%0D%0A
+//
             
@@ -1810,24 +1810,26 @@
 ytesRead);%0D%0A
+//
         %7D%0D%0A 
@@ -1833,24 +1833,75 @@
 %0D%0A        %0D%0A
+        IOUtils.copy(encoded, decoded);%0D%0A        %0D%0A
         retu

Use IOUtils for copying streams