<commit_before>package be.cytomine.formats

/*
 * Copyright (c) 2009-2017. Authors: see NOTICE file.
 *
 * Licensed under the GNU Lesser General Public License, Version 2.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.gnu.org/licenses/lgpl-2.1.txt
 *
 * 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.
 */
import be.cytomine.client.models.UploadedFile
import be.cytomine.formats.archive.ZipFormat
import be.cytomine.formats.heavyconvertable.CellSensVSIFormat
import be.cytomine.formats.heavyconvertable.ZeissCZIFormat
import be.cytomine.formats.lightconvertable.specialtiff.CZITIFFFormat
import be.cytomine.formats.lightconvertable.specialtiff.PhotoshopTIFFFormat
import be.cytomine.formats.lightconvertable.JPEGFormat
import be.cytomine.formats.lightconvertable.PNGFormat
import be.cytomine.formats.lightconvertable.specialtiff.PlanarTIFFFormat
import be.cytomine.formats.lightconvertable.BMPFormat
import be.cytomine.formats.lightconvertable.PGMFormat
import be.cytomine.formats.lightconvertable.specialtiff.BrokenTIFFFormat
import be.cytomine.formats.lightconvertable.specialtiff.HuronTIFFFormat
import be.cytomine.formats.supported.digitalpathology.*
import be.cytomine.formats.supported.JPEG2000Format
import be.cytomine.formats.supported.PhilipsTIFFFormat
import be.cytomine.formats.supported.PyramidalTIFFFormat
import be.cytomine.formats.supported.VentanaBIFFormat
import be.cytomine.formats.supported.VentanaTIFFFormat

import be.cytomine.image.ImageUtilsController
import grails.test.mixin.TestFor
import org.apache.commons.io.FileUtils
import utils.ProcUtils

/**
 * Created by stevben on 23/04/14.
 */

@TestFor(ImageUtilsController)
class CytomineFormatSpec {

    final static String IMAGES_REPOSITORY_PATH = '/data/cytominetest/images'

    private static String getFilenameForTest(def imageFilename, String imageRepository = IMAGES_REPOSITORY_PATH) {
        String source = createFullPathFromFilename(imageFilename, imageRepository)
        String target = ["/tmp", new Date().getTime(), imageFilename].join(File.separator)
        String targetDir = new File(target).getParent()
        def command = "mkdir -p $targetDir"
        ProcUtils.executeOnShell(command)
        command = "cp -r $source $target"
        ProcUtils.executeOnShell(command)
        assert(new File(target).exists())
        return target
    }

    private static String createFullPathFromFilename(def imageFilename, String imageRepository = IMAGES_REPOSITORY_PATH) {
        String uploadedFile = [imageRepository, imageFilename].join(File.separator)
        println uploadedFile
        assert new File(uploadedFile).exists()
        return uploadedFile
    }

    private static UploadedFile createUploadedFileFromImagePath(def imageFilename, def imageRepository = IMAGES_REPOSITORY_PATH) {
        File file = new File([imageRepository, imageFilename].join(File.separator))
        UploadedFile uploadedFile = null
        if (file.canRead()) {
            uploadedFile = new UploadedFile()
            uploadedFile.set("path", imageRepository)
            uploadedFile.set("filename", imageFilename) //for test only
        }

        assert(file != null)

        return uploadedFile
    }


    private static checkCorrectDetect(String uploadedFile, Class expectedClass){
        def imageFormats = FormatIdentifier.getImageFormats(uploadedFile)
        println imageFormats

        def format = imageFormats.find { expectedClass.isInstance(it.imageFormat) }

        assert(format != null)
    }


    /*********************** Test formats ***********************/
    // Common Format
    void "test jpegformat"() {
        def uploadedFile = createFullPathFromFilename("jpeg/test.jpeg")
        checkCorrectDetect(uploadedFile, JPEGFormat.class)
    }

    void "test pngformat"() {
        def uploadedFile = createFullPathFromFilename("png/test.png")
        checkCorrectDetect(uploadedFile, PNGFormat.class)
    }

    void "test pgmformat"() {
        def uploadedFile = createFullPathFromFilename("pgm/test.pgm")
        checkCorrectDetect(uploadedFile, PGMFormat.class)
    }

    void "test bmpformat"() {
        def uploadedFile = createFullPathFromFilename("bmp/test.bmp")
        checkCorrectDetect(uploadedFile, BMPFormat.class)
    }


    // TIFF Format
    /*void "test tiffplanarformat"() {
        def uploadedFile = createFullPathFromFilename("tiff/planar.tif")
        checkCorrectDetect(uploadedFile, PlanarTIFFFormat.class)
    }

    void "test tiffphotoshopformat"() {
        def uploadedFile = createFullPathFromFilename("tiff/photoshop.tif")
        checkCorrectDetect(uploadedFile, PhotoshopTIFFFormat.class)
    }

    void "test ventanatiffformat"() {
        def uploadedFile = createFullPathFromFilename("tiff/ventana.tif")
        checkCorrectDetect(uploadedFile, VentanaTIFFFormat.class)
    }

    void "test huronTIFFformat"() {
        def uploadedFile = createFullPathFromFilename("tiff/huron.tif")
        checkCorrectDetect(uploadedFile, HuronTIFFFormat.class)
    }

    void "test brokenTIFFformat"() {
        def uploadedFile = createFullPathFromFilename("tiff/broken.tif")
        checkCorrectDetect(uploadedFile, BrokenTIFFFormat.class)
    }

    void "test pyrTIFFformat"() {
        def uploadedFile = createFullPathFromFilename("tiff/pyramidal.tif")
        checkCorrectDetect(uploadedFile, PyramidalTIFFFormat.class)
    }

    void "test philipsTIFFFormat"() {
        def uploadedFile = createFullPathFromFilename("tiff/philips.tif")
        checkCorrectDetect(uploadedFile, PhilipsTIFFFormat.class)
    }

    void "test CZITIFFDetect"() {
        def uploadedFile = createFullPathFromFilename("tiff/czi.tif")
        checkCorrectDetect(uploadedFile, CZITIFFFormat.class)
    }*/


    // Single File Format
    void "test ndpiformat"() {
        def uploadedFile = createFullPathFromFilename("ndpi/test.ndpi")
        checkCorrectDetect(uploadedFile, HamamatsuNDPIFormat.class)
    }

    void "test aperioSVSFormat"() {
        def uploadedFile = createFullPathFromFilename("svs/test.svs")
        checkCorrectDetect(uploadedFile, AperioSVSFormat.class)
    }

    /*void "test ventanabifformat"() {
        def uploadedFile = createFullPathFromFilename("bif/test.bif")
        checkCorrectDetect(uploadedFile, VentanaBIFFormat.class)
    }*/

    void "test J2Kformat"() {
        def uploadedFile = createFullPathFromFilename("j2k/test.jp2")
        checkCorrectDetect(uploadedFile, JPEG2000Format.class)
    }

    /*void "test leicaSCNFormat"() {
        def uploadedFile = createFullPathFromFilename("scn/test.scn")
        checkCorrectDetect(uploadedFile, LeicaSCNFormat.class)
    }

    // CZI Format
    void "test CZIFormat"() {
        def uploadedFile = createFullPathFromFilename("czi/test.czi")
        checkCorrectDetect(uploadedFile, ZeissCZIFormat.class)<commit_msg>Fix Jpeg 2000<commit_after>package be.cytomine.formats

/*
 * Copyright (c) 2009-2017. Authors: see NOTICE file.
 *
 * Licensed under the GNU Lesser General Public License, Version 2.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.gnu.org/licenses/lgpl-2.1.txt
 *
 * 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.
 */
import be.cytomine.client.models.UploadedFile
import be.cytomine.formats.archive.ZipFormat
import be.cytomine.formats.heavyconvertable.CellSensVSIFormat
import be.cytomine.formats.heavyconvertable.ZeissCZIFormat
import be.cytomine.formats.lightconvertable.specialtiff.CZITIFFFormat
import be.cytomine.formats.lightconvertable.specialtiff.PhotoshopTIFFFormat
import be.cytomine.formats.lightconvertable.JPEGFormat
import be.cytomine.formats.lightconvertable.PNGFormat
import be.cytomine.formats.lightconvertable.specialtiff.PlanarTIFFFormat
import be.cytomine.formats.lightconvertable.BMPFormat
import be.cytomine.formats.lightconvertable.PGMFormat
import be.cytomine.formats.lightconvertable.specialtiff.BrokenTIFFFormat
import be.cytomine.formats.lightconvertable.specialtiff.HuronTIFFFormat
import be.cytomine.formats.supported.digitalpathology.*
import be.cytomine.formats.supported.JPEG2000Format
import be.cytomine.formats.supported.PhilipsTIFFFormat
import be.cytomine.formats.supported.PyramidalTIFFFormat
import be.cytomine.formats.supported.VentanaBIFFormat
import be.cytomine.formats.supported.VentanaTIFFFormat

import be.cytomine.image.ImageUtilsController
import grails.test.mixin.TestFor
import grails.util.Holders
import org.apache.commons.io.FileUtils
import utils.ProcUtils

/**
 * Created by stevben on 23/04/14.
 */

@TestFor(ImageUtilsController)
class CytomineFormatSpec {

    final static String IMAGES_REPOSITORY_PATH = '/data/cytominetest/images'

    private static String getFilenameForTest(def imageFilename, String imageRepository = IMAGES_REPOSITORY_PATH) {
        String source = createFullPathFromFilename(imageFilename, imageRepository)
        String target = ["/tmp", new Date().getTime(), imageFilename].join(File.separator)
        String targetDir = new File(target).getParent()
        def command = "mkdir -p $targetDir"
        ProcUtils.executeOnShell(command)
        command = "cp -r $source $target"
        ProcUtils.executeOnShell(command)
        assert(new File(target).exists())
        return target
    }

    private static String createFullPathFromFilename(def imageFilename, String imageRepository = IMAGES_REPOSITORY_PATH) {
        String uploadedFile = [imageRepository, imageFilename].join(File.separator)
        println uploadedFile
        assert new File(uploadedFile).exists()
        return uploadedFile
    }

    private static UploadedFile createUploadedFileFromImagePath(def imageFilename, def imageRepository = IMAGES_REPOSITORY_PATH) {
        File file = new File([imageRepository, imageFilename].join(File.separator))
        UploadedFile uploadedFile = null
        if (file.canRead()) {
            uploadedFile = new UploadedFile()
            uploadedFile.set("path", imageRepository)
            uploadedFile.set("filename", imageFilename) //for test only
        }

        assert(file != null)

        return uploadedFile
    }


    private static checkCorrectDetect(String uploadedFile, Class expectedClass){
        def imageFormats = FormatIdentifier.getImageFormats(uploadedFile)
        println imageFormats

        def format = imageFormats.find { expectedClass.isInstance(it.imageFormat) }

        assert(format != null)
    }


    /*********************** Test formats ***********************/
    // Common Format
    void "test jpegformat"() {
        def uploadedFile = createFullPathFromFilename("jpeg/test.jpeg")
        checkCorrectDetect(uploadedFile, JPEGFormat.class)
    }

    void "test pngformat"() {
        def uploadedFile = createFullPathFromFilename("png/test.png")
        checkCorrectDetect(uploadedFile, PNGFormat.class)
    }

    void "test pgmformat"() {
        def uploadedFile = createFullPathFromFilename("pgm/test.pgm")
        checkCorrectDetect(uploadedFile, PGMFormat.class)
    }

    void "test bmpformat"() {
        def uploadedFile = createFullPathFromFilename("bmp/test.bmp")
        checkCorrectDetect(uploadedFile, BMPFormat.class)
    }


    // TIFF Format
    /*void "test tiffplanarformat"() {
        def uploadedFile = createFullPathFromFilename("tiff/planar.tif")
        checkCorrectDetect(uploadedFile, PlanarTIFFFormat.class)
    }

    void "test tiffphotoshopformat"() {
        def uploadedFile = createFullPathFromFilename("tiff/photoshop.tif")
        checkCorrectDetect(uploadedFile, PhotoshopTIFFFormat.class)
    }

    void "test ventanatiffformat"() {
        def uploadedFile = createFullPathFromFilename("tiff/ventana.tif")
        checkCorrectDetect(uploadedFile, VentanaTIFFFormat.class)
    }

    void "test huronTIFFformat"() {
        def uploadedFile = createFullPathFromFilename("tiff/huron.tif")
        checkCorrectDetect(uploadedFile, HuronTIFFFormat.class)
    }

    void "test brokenTIFFformat"() {
        def uploadedFile = createFullPathFromFilename("tiff/broken.tif")
        checkCorrectDetect(uploadedFile, BrokenTIFFFormat.class)
    }

    void "test pyrTIFFformat"() {
        def uploadedFile = createFullPathFromFilename("tiff/pyramidal.tif")
        checkCorrectDetect(uploadedFile, PyramidalTIFFFormat.class)
    }

    void "test philipsTIFFFormat"() {
        def uploadedFile = createFullPathFromFilename("tiff/philips.tif")
        checkCorrectDetect(uploadedFile, PhilipsTIFFFormat.class)
    }

    void "test CZITIFFDetect"() {
        def uploadedFile = createFullPathFromFilename("tiff/czi.tif")
        checkCorrectDetect(uploadedFile, CZITIFFFormat.class)
    }*/


    // Single File Format
    void "test ndpiformat"() {
        def uploadedFile = createFullPathFromFilename("ndpi/test.ndpi")
        checkCorrectDetect(uploadedFile, HamamatsuNDPIFormat.class)
    }

    void "test aperioSVSFormat"() {
        def uploadedFile = createFullPathFromFilename("svs/test.svs")
        checkCorrectDetect(uploadedFile, AperioSVSFormat.class)
    }

    /*void "test ventanabifformat"() {
        def uploadedFile = createFullPathFromFilename("bif/test.bif")
        checkCorrectDetect(uploadedFile, VentanaBIFFormat.class)
    }*/

    void "test J2Kformat"() {
        if(!Holders.config.cytomine.Jpeg2000Enabled) return;
        def uploadedFile = createFullPathFromFilename("j2k/test.jp2")
        checkCorrectDetect(uploadedFile, JPEG2000Format.class)
    }

    /*void "test leicaSCNFormat"() {
        def uploadedFile = createFullPathFromFilename("scn/test.scn")
        checkCorrectDetect(uploadedFile, LeicaSCNFormat.class)
    }

    // CZI Format
    void "test CZIFormat"() {
        def uploadedFile = createFullPathFromFilename("czi/test.czi")
        checkCorrectDetect(uploadedFile, ZeissCZIFormat.class)
{'fraction_non_alphanumeric': 0.09412007711374278, 'fraction_numerical': 0.004544202698980997, 'mean_word_length': 3.8448965977318212, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4820126', 'n_tokens_mistral': 4516, 'n_tokens_neox': 4217, 'n_words': 825}