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 imread(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
        }
        return image;
    }

    public static byte[] getBytesFromFile(File file) {
        byte[] ret = null;
        try {
            if (file == null) {
                // log.error("helper:the file is null!");
                return null;
            }
            FileInputStream in = new FileInputStream(file);
            ByteArrayOutputStream out = new ByteArrayOutputStream(4096);
            byte[] b = new byte[4096];
            int n;
            while ((n = in.read(b)) != -1) {
                out.write(b, 0, n);
            }
            in.close();
            out.close();
            ret = out.toByteArray();
        } catch (IOException e) {
            // log.error("helper:get bytes from file process error!");
            e.printStackTrace();
        }
        return ret;
    }
}

@@ -362,18 +362,16 @@
 4JImage 
-im
 read(Str
@@ -428,16 +428,17 @@
       if
+ 
 (filePat
@@ -455,18 +455,20 @@
 (%22.jpg%22)
+ 
 %7C%7C
+ 
 filePath
@@ -754,16 +754,17 @@
  else if
+ 
 (filePat
@@ -781,18 +781,20 @@
 (%22.png%22)
+ 
 %7C%7C
+ 
 filePath
@@ -883,29 +883,30 @@
     %7D%0A%0A    p
-ublic
+rivate
  static byte
@@ -965,24 +965,25 @@
 = null;%0A
+%0A
         
 try %7B%0A  
@@ -978,103 +978,122 @@
     
-try %7B%0A            if (file == null) %7B%0A                // log.error(%22helper:the 
+FileInputStream in = null;%0A        ByteArrayOutputStream out = null;%0A        try %7B%0A            if (
 file 
-is
+==
  null
-!%22);
+) %7B
 %0A   
@@ -1124,33 +1124,33 @@
 ;%0A            %7D%0A
- 
+%0A
            FileI
@@ -1140,39 +1140,24 @@
 %0A           
-FileInputStream
  in = new Fi
@@ -1189,38 +1189,16 @@
         
-ByteArrayOutputStream 
 out = ne
@@ -1385,56 +1385,8 @@
   %7D%0A
-            in.close();%0A            out.close();
 %0A   
@@ -1469,97 +1469,123 @@
     
-// log.error(%22helper:get bytes from file process error!%22);%0A            e.printStackTrace(
+e.printStackTrace();%0A        %7D finally %7B%0A            IOUtils.closeQuietly(in);%0A            IOUtils.closeQuietly(out
 );%0A 
@@ -1585,32 +1585,33 @@
 out);%0A        %7D%0A
+%0A
         return r

update ImageCodes