package org.datavec.image.transform;

import org.datavec.image.data.ImageWritable;
import org.junit.Test;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.Buffer;
import java.util.List;
import java.util.Random;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

/**
 * Created by kepricon on 17. 5. 25.
 */
public class JsonYamlTest {
    @Test
    public void testJsonYamlImageTransformProcess() throws IOException {
        int seed = 12345;
        Random random = new Random(seed);

        //from org.bytedeco.javacpp.opencv_imgproc
        int COLOR_BGR2Luv = 50;
        int CV_BGR2GRAY = 6;


        ImageTransformProcess itp = new ImageTransformProcess.Builder()
                .colorConversionTransform(COLOR_BGR2Luv)
                .cropImageTransform(10)
                .equalizeHistTransform(CV_BGR2GRAY)
                .flipImageTransform(0)
                .resizeImageTransform(300, 300)
                .rotateImageTransform(30)
                .scaleImageTransform(3)
                .warpImageTransform((float)0.5)
//              .randomCropTransform(seed, 50, 50)
//              .filterImageTransform("noise=alls=20:allf=t+u,format=rgba", 100, 100, 4)
                .build();

        String asJson = itp.toJson();
        String asYaml = itp.toYaml();

//        System.out.println(asJson);
//        System.out.println("\n\n\n");
//        System.out.println(asYaml);

        ImageWritable img = TestImageTransform.makeRandomImage(0, 0, 3);
        ImageWritable imgJson = new ImageWritable(img.getFrame().clone());
        ImageWritable imgYaml = new ImageWritable(img.getFrame().clone());
        ImageWritable imgAll = new ImageWritable(img.getFrame().clone());

        ImageTransformProcess itpFromJson = ImageTransformProcess.fromJson(asJson);
        ImageTransformProcess itpFromYaml = ImageTransformProcess.fromYaml(asYaml);
//
        List<ImageTransform> transformList = itp.getTransformList();
        List<ImageTransform> transformListJson = itpFromJson.getTransformList();
        List<ImageTransform> transformListYaml = itpFromYaml.getTransformList();

        for (int i = 0; i < transformList.size(); i++) {
            ImageTransform it = transformList.get(i);
            ImageTransform itJson = transformListJson.get(i);
            ImageTransform itYaml = transformListYaml.get(i);

//            System.out.println(i + "\t" + it);

            img = it.transform(img);
            imgJson = itJson.transform(imgJson);
            imgYaml = itYaml.transform(imgYaml);

            if (it instanceof RandomCropTransform) {
                assertTrue(img.getFrame().imageHeight == imgJson.getFrame().imageHeight);
                assertTrue(img.getFrame().imageWidth == imgJson.getFrame().imageWidth);

                assertTrue(img.getFrame().imageHeight == imgYaml.getFrame().imageHeight);
                assertTrue(img.getFrame().imageWidth == imgYaml.getFrame().imageWidth);
            } else if (it instanceof FilterImageTransform) {
                assertEquals(img.getFrame().imageHeight, imgJson.getFrame().imageHeight);
                assertEquals(img.getFrame().imageWidth, imgJson.getFrame().imageWidth);
                assertEquals(img.getFrame().imageChannels, imgJson.getFrame().imageChannels);

                assertEquals(img.getFrame().imageHeight, imgYaml.getFrame().imageHeight);
                assertEquals(img.getFrame().imageWidth, imgYaml.getFrame().imageWidth);
                assertEquals(img.getFrame().imageChannels, imgYaml.getFrame().imageChannels);
            } else {
                assertEquals(img, imgJson);

                assertEquals(img, imgYaml);
            }
        }

        imgAll = itp.execute(imgAll);

        assertEquals(imgAll, img);
    }
}

@@ -132,60 +132,8 @@
 on;%0A
-import java.nio.ByteBuffer;%0Aimport java.nio.Buffer;%0A
 impo
@@ -1047,75 +1047,844 @@
 .5)%0A
-//              .randomCropTransform(seed, 50, 50)%0A//              
+%0A                // Note : since randomCropTransform use random value%0A                // the results from each case(json, yaml, ImageTransformProcess)%0A                // can be different%0A                // don't use the below line%0A                // if you uncomment it, you will get fail from below assertions%0A                //  .randomCropTransform(seed, 50, 50)%0A%0A                // Note : you will get %22java.lang.NoClassDefFoundError: Could not initialize class org.bytedeco.javacpp.avutil%22%0A                // it needs to add the below dependency%0A                // %3Cdependency%3E%0A                //     %3CgroupId%3Eorg.bytedeco.javacpp-presets%3C/groupId%3E%0A                //     %3CartifactId%3Effmpeg-platform%3C/artifactId%3E%0A                // %3C/dependency%3E%0A                // FFmpeg has license issues, be careful to use it%0A                //
 .fil
@@ -1952,16 +1952,17 @@
 100, 4)%0A
+%0A
         
@@ -2053,26 +2053,24 @@
 .toYaml();%0A%0A
-//
         Syst
@@ -2093,18 +2093,16 @@
 sJson);%0A
-//
         
@@ -2131,18 +2131,16 @@
 %5Cn%5Cn%22);%0A
-//
         
@@ -2634,18 +2634,16 @@
 sYaml);%0A
-//
 %0A       
@@ -3103,18 +3103,16 @@
 et(i);%0A%0A
-//
         

add extra explanations to the test code