package com.cv4j.image.util;

import com.cv4j.core.datamodel.CV4JImage;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import edu.uthscsa.ric.volume.formats.jpeg.JPEGLosslessDecoderWrapper;

/**
 * Created by gloomy fish on 2017/7/8.
 */

public class ImageCodecs {

    public static CV4JImage read(String filePath) {
        CV4JImage image = null;
        if (filePath.endsWith(".jpg") || filePath.endsWith(".JPG")
                || filePath.endsWith(".JPEG") || filePath.endsWith(".jpeg")) {
            try {
                image = JPEGLosslessDecoderWrapper.readImage(getBytesFromFile(new File(filePath)));
            } catch (IOException ioe) {

            }
        } else if (filePath.endsWith(".png") || filePath.endsWith(".PNG")) {
            // TODO: zhigang
            PngReader pngr = new PngReader(new File(filePath));
            System.out.println(pngr.toString());
            int channels = pngr.imgInfo.channels;
            if (channels < 3 || pngr.imgInfo.bitDepth != 8)
                throw new RuntimeException("This method is for RGB8/RGBA8 images");

            ImageInfo info = pngr.getImgInfo();
            int width = info.cols;
            int height = info.rows;
            int dims = info.channels;
            int r = 0, g = 0, b = 0;
            int index = 0;
            int[] pixels = new int[width * height];
            for (int row = 0; row < height; row++) {
                ImageLineInt line = (ImageLineInt) pngr.readRow(row);
                int[] data = line.getScanline();
                for (int col = 0; col < width; col++) {
                    if (dims == 3) {
                        r = data[col*dims];
                        g = data[col*dims + 1];
                        b = data[col*dims + 2];
                        pixels[row * width + col] =
                                ((255 & 0xff) << 24) | ((r & 0xff) << 16) | ((g & 0xff) << 8) | (b & 0xff);
                    } else if (dims == 4) {
                        r = data[col*dims + 1];
                        g = data[col*dims + 2];
                        b = data[col*dims + 3];
                        pixels[row * width + col] =
                                ((255 & 0xff) << 24) | ((r & 0xff) << 16) | ((g & 0xff) << 8) | (b & 0xff);
                    }
                }
            }
            image = new CV4JImage(width, height, pixels);
        }
        return image;
    }

    private static byte[] getBytesFromFile(File file) {

        if (file == null) return null;

        byte[] ret = null;

        FileInputStream in = null;
        ByteArrayOutputStream out = null;
        try {
            in = new FileInputStream(file);
            out = new ByteArrayOutputStream(4096);
            byte[] b = new byte[4096];
            int n;
            while ((n = in.read(b)) != -1) {
                out.write(b, 0, n);
            }

            ret = out.toByteArray();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            IOUtils.closeQuietly(in);
            IOUtils.closeQuietly(out);
        }

        return ret;
    }
}

@@ -1,8 +1,619 @@
+/*%0A * Copyright (c) 2017-present, CV4J Contributors.%0A *%0A * Licensed under the Apache License, Version 2.0 (the %22License%22);%0A * you may not use this file except in compliance with the License.%0A * You may obtain a copy of the License at%0A *%0A * http://www.apache.org/licenses/LICENSE-2.0%0A *%0A * Unless required by applicable law or agreed to in writing, software%0A * distributed under the License is distributed on an %22AS IS%22 BASIS,%0A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A * See the License for the specific language governing permissions and%0A * limitations under the License.%0A */%0A
 package 
@@ -675,16 +675,57 @@
 4JImage;
+%0Aimport com.cv4j.exception.CV4JException;
 %0A%0Aimport
@@ -834,24 +834,129 @@
 Exception;%0A%0A
+import ar.com.hjg.pngj.ImageInfo;%0Aimport ar.com.hjg.pngj.ImageLineInt;%0Aimport ar.com.hjg.pngj.PngReader;%0A
 import edu.u
@@ -1019,56 +1019,8 @@
 r;%0A%0A
-/**%0A * Created by gloomy fish on 2017/7/8.%0A */%0A%0A
 publ
@@ -1119,32 +1119,73 @@
 e image = null;%0A
+        File file = new File(filePath);%0A%0A
         if (file
@@ -1410,34 +1410,20 @@
 romFile(
-new File(filePath)
+file
 ));%0A    
@@ -1554,37 +1554,8 @@
 ) %7B%0A
-            // TODO: zhigang%0A
     
@@ -1597,75 +1597,12 @@
 der(
-new File(filePath));%0A            System.out.println(pngr.toString()
+file
 );%0A 
@@ -1740,15 +1740,12 @@
 new 
-Runtime
+CV4J
 Exce
@@ -1830,20 +1830,15 @@
 ngr.
-getI
+i
 mgInfo
-()
 ;%0A  
@@ -2060,16 +2060,54 @@
 eight%5D;%0A
+            ImageLineInt line = null;%0A
         
@@ -2163,37 +2163,24 @@
             
-ImageLineInt 
 line = (Imag
@@ -3112,32 +3112,33 @@
 els);%0A        %7D%0A
+%0A
         return i

update ImageCodes