package edu.tamu.tcat.analytics.image.integral.datatrax;

import java.awt.color.ColorSpace;
import java.awt.image.BufferedImage;
import java.awt.image.ColorConvertOp;
import java.awt.image.Raster;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.Callable;

import edu.tamu.tcat.analytics.datatrax.Transformer;
import edu.tamu.tcat.analytics.datatrax.TransformerConfigurationException;
import edu.tamu.tcat.analytics.datatrax.TransformerContext;
import edu.tamu.tcat.analytics.image.integral.IntegralImage;
import edu.tamu.tcat.analytics.image.integral.IntegralImageImpl;

public class BufferedImageAdapter implements Transformer
{
   
   public final static String EXTENSION_ID = "tcat.dia.images.adapters.buffered.integral"; 
   public final static String IMAGE_PIN = "image"; 

   public BufferedImageAdapter()
   {
   }

   @Override
   public void configure(Map<String, Object> data) throws TransformerConfigurationException
   {
      // no-op
   }

   @Override
   public Map<String, Object> getConfiguration()
   {
      return new HashMap<>();
   }
   
   public IntegralImage adapt(BufferedImage image)
   {
      Objects.requireNonNull(image, "Source image was null");
      
      Raster data = image.getData();
      if (data.getNumBands() > 1)
      {
         // convert to grayscale.
         ColorSpace colorSpace = ColorSpace.getInstance(ColorSpace.CS_GRAY);
         ColorConvertOp op = new ColorConvertOp(colorSpace, null);
         image = op.filter(image, null);
      }
      
      return IntegralImageImpl.create(data);
   }

   @Override
   public Callable<IntegralImage> create(TransformerContext ctx)
   {
      final BufferedImage image = (BufferedImage)ctx.getValue("image");
      return new Callable<IntegralImage>()
      {

         @Override
         public IntegralImage call() throws Exception
         {
            return adapt(image);
         }
      };
   }
}

@@ -1228,24 +1228,474 @@
 l%22);%0A      %0A
+      // HACK prevent 'java.awt.color.CMMException: LCMS error 13: Couldn't link the profiles' exceptions%0A      //      See: http://stackoverflow.com/questions/26535842/multithreaded-jpeg-image-processing-in-java%0A      try %7B%0A         Class.forName(%22java.awt.color.ICC_ColorSpace%22);%0A         Class.forName(%22sun.java2d.cmm.lcms.LCMS%22);%0A      %7D catch (Exception ex) %7B%0A         throw new IllegalStateException(%22Cannot load color models%22);%0A      %7D%0A      %0A
       Raster

Fix error on loding file formats and color profile information