#!/usr/bin/env python

# Fonctionne sur Gimp 2.10.24 portable 64/32 bit Win

import os, sys, subprocess, time
from gimpfu import *
from os.path import expanduser

def mosaic_command_line (TheImage, TheDrawable,HALF_TILE, GAUSS, EDGE_DETECTION, RAND_SIZE, MAX_ANGLE, GAP_CHAIN_SPACING, MAKE_CONVEX, ZoomSVG, Temporisation):
    pdb.gimp_context_push()
    ExportType = 1
    Tempo = abs(Temporisation)
    ZoomSVG = ZoomSVG+1
    ZoomSVGT = str(ZoomSVG)
    CRLF = "\r\n"
    USER = expanduser("~")
    TestEnvironment = False

    if os.path.exists(USER+"\\anaconda3\\Yobeatz_mosaic_Gimp_2_10\\mosaic_gimp_svg.py"):
        Environment = 0 # Anaconda 
        TestEnvironment = True
    elif os.path.exists(USER+"\\miniconda3\\Yobeatz_mosaic_Gimp_2_10\\mosaic_gimp_svg.py"):
        Environment = 1 # Miniconda
        TestEnvironment = True

    if TestEnvironment == True:
        if Environment == 0:
            # Anaconda
            La = "cd  %USERPROFILE%\\anaconda3\\Yobeatz_mosaic_Gimp_2_10"+CRLF
            Lb = "CALL \"%USERPROFILE%\\anaconda3\\Scripts\\activate.bat\""+CRLF
            Lc = "%CONDA_PYTHON_EXE% \"mosaic_gimp_svg.py\" %1 %2 %3 %4 %5 %6 %7 %8 %9"+CRLF
            Anaconda = USER+"\\anaconda3\\Yobeatz_mosaic_Gimp_2_10"
            Bat_File = USER+"\\anaconda3\\Yobeatz_mosaic_Gimp_2_10\\mosaic.bat"
            ImagePNG = USER+"\\anaconda3\\Yobeatz_mosaic_Gimp_2_10\\mosaic_input.png"
            ImageSVG = USER+"\\anaconda3\\Yobeatz_mosaic_Gimp_2_10\\mosaic_output.svg"
            RSVG_convert_exe = USER+"\\anaconda3\\Yobeatz_mosaic_Gimp_2_10\\rsvg-convert\\rsvg-convert.exe"
            RSVG_convert_svg_in = USER+"\\anaconda3\\Yobeatz_mosaic_Gimp_2_10\\rsvg-convert\\mosaic_output.svg"
            RSVG_convert_svg_out = USER+"\\anaconda3\\Yobeatz_mosaic_Gimp_2_10\\rsvg-convert\\mosaic_output_zoom.svg"
            RSVG_convert_dir = USER+"\\anaconda3\\Yobeatz_mosaic_Gimp_2_10\\rsvg-convert"
        elif Environment == 1:
            # MiniConda
            La = "cd  %USERPROFILE%\\miniconda3\\Yobeatz_mosaic_Gimp_2_10"+CRLF
            Lb = "CALL \"%USERPROFILE%\\miniconda3\\Scripts\\activate.bat\""+CRLF
            Lc = "%CONDA_PYTHON_EXE% \"mosaic_gimp_svg.py\" %1 %2 %3 %4 %5 %6 %7 %8 %9"+CRLF
            Anaconda = USER+"\\miniconda3\\Yobeatz_mosaic_Gimp_2_10"
            Bat_File = USER+"\\miniconda3\\Yobeatz_mosaic_Gimp_2_10\\mosaic.bat"
            ImagePNG = USER+"\\miniconda3\\Yobeatz_mosaic_Gimp_2_10\\mosaic_input.png"
            ImageSVG = USER+"\\miniconda3\\Yobeatz_mosaic_Gimp_2_10\\mosaic_output.svg"
            RSVG_convert_exe = USER+"\\miniconda3\\Yobeatz_mosaic_Gimp_2_10\\rsvg-convert\\rsvg-convert.exe"
            RSVG_convert_svg_in = USER+"\\miniconda3\\Yobeatz_mosaic_Gimp_2_10\\rsvg-convert\\mosaic_output.svg"
            RSVG_convert_svg_out = USER+"\\miniconda3\\Yobeatz_mosaic_Gimp_2_10\\rsvg-convert\\mosaic_output_zoom.svg"
            RSVG_convert_dir = USER+"\\miniconda3\\Yobeatz_mosaic_Gimp_2_10\\rsvg-convert"

        Ld = "exit()"+CRLF
        Le = "conda deactivate"+CRLF
        Lf = "exit"+CRLF
        FileAw = open(Bat_File, "w")
        FileAw.write(La+Lb+Lc+Ld+Le+Lf)
        FileAw.close()

        # gimp_samj_mosaic_1.txt half_tile 4...30 => half size of mosaic tile
        # gimp_samj_mosaic_2.txt GAUSS  0...8 => blurs image before edge detection (check "edges" image for a good value)
        # gimp_samj_mosaic_3.txt EDGE_DETECTION  HED or DiBlasi
        # gimp_samj_mosaic_4.txt RAND_SIZE  portion of tile size which is added or removed randomly during construction
        # gimp_samj_mosaic_5.txt MAX_ANGLE  30...75 => max construction angle for tiles along roundings
        # gimp_samj_mosaic_6.txt GAP_CHAIN_SPACING  0.4 to 1.0 => spacing of gap filler chains
        # gimp_samj_mosaic_7.txt MAKE_CONVEX  default is True => break concave into more realistic polygons

        FilePa = open(Anaconda+"\\gimp_samj_mosaic_1.txt", "w")
        FilePa.write(str(HALF_TILE))
        FilePa.close()

        FilePb = open(Anaconda+"\\gimp_samj_mosaic_2.txt", "w")
        FilePb.write(str(GAUSS))
        FilePb.close()

        FilePc = open(Anaconda+"\\gimp_samj_mosaic_3.txt", "w")
        FilePc.write(str(EDGE_DETECTION))
        FilePc.close()

        FilePd = open(Anaconda+"\\gimp_samj_mosaic_4.txt", "w")
        FilePd.write(str(RAND_SIZE))
        FilePd.close()

        FilePe = open(Anaconda+"\\gimp_samj_mosaic_5.txt", "w")
        FilePe.write(str(MAX_ANGLE))
        FilePe.close()

        FilePf = open(Anaconda+"\\gimp_samj_mosaic_6.txt", "w")
        FilePf.write(str(GAP_CHAIN_SPACING))
        FilePf.close()

        FilePg = open(Anaconda+"\\gimp_samj_mosaic_7.txt", "w")
        FilePg.write(str(MAKE_CONVEX))
        FilePg.close()

        time.sleep(Tempo)

        if ExportType == 0:
            # jpeg export
            pdb.file_jpeg_save(TheImage, TheDrawable, "mosaic_input.jpg", "mosaic_input.jpg", 1, 0, 0, 0, "Mosaic command line from GIMP 2.10 portable.", 0, 0, 0, 0)
            time.sleep(Tempo)
            os.system(Bat_File)
        elif ExportType == 1:
            # png export
            pdb.file_png_save(TheImage, TheDrawable, ImagePNG, "mosaic_input.png", 0, 9, 1, 0, 0, 1, 1)
            time.sleep(Tempo+1)
            StartBat = "start /WAIT /B /D "+Anaconda+" "+Bat_File
            os.system(StartBat)
            time.sleep(Tempo)
            RSVG_convert_copy = "copy "+ImageSVG+" "+RSVG_convert_svg_in
            os.system(RSVG_convert_copy)
            time.sleep(Tempo)
            RSVG_convert_CL = "start /WAIT /B /D "+RSVG_convert_dir+" rsvg-convert.exe -a -z "+ZoomSVGT+" -f  svg mosaic_output.svg -o mosaic_output_zoom.svg"
            os.system(RSVG_convert_CL)
            time.sleep(Tempo)
            newimage = pdb.file_svg_load(RSVG_convert_svg_out, "mosaic_output_zoom.svg", 90, 0, 0, 2)

            display_newimage = pdb.gimp_display_new(newimage)

            os.system("erase "+ImagePNG)
            os.system("erase "+ImageSVG)
            os.system("erase "+RSVG_convert_svg_in)
            os.system("erase "+Bat_File)
            os.system("erase "+Anaconda+"\\gimp_samj_mosaic_?.txt")
    else:
        pdb.gimp_message("Install Anaconda or Miniconda in User Directory :"+CRLF+"https://docs.conda.io/projects/conda/en/latest/user-guide/install/index.html"+CRLF+"https://docs.conda.io/en/latest/miniconda.html"+CRLF+CRLF+"and Gimp plug-in :"+CRLF+"https://samjcreations.blogspot.com/2021/05/creer-des-mosaiques-romaines-avec.html")

    pdb.gimp_context_pop()

register(
    "mosaic_command_line",
    "Mosaic command line",
    "Drawable to Mosaic from GIMP 2.10 portable.",
    "samj",
    "samj GNU/GPL",
    "20210507",
    "Mosaic command line...",
    "RGB*",
    [
        (PF_IMAGE, "image", "TheImage", None),
        (PF_DRAWABLE, "drawable", "TheDrawable", None),
        (PF_INT, "half_tile", "HALF__TILE 4...30 | 12 ", 12),
        (PF_INT, "gauss", "GAUSS 0...8 | 3", 3),
        (PF_OPTION,"edge_detection", "EDGE__DETECTION | HED", 0, ["HED","DiBlasi"]),
        (PF_FLOAT, "rand_size", "RAND__SIZE 0...10 | 0.3", 0.3),
        (PF_INT, "max_angle", "MAX__ANGLE 30...75 | 40", 40),
        (PF_FLOAT, "gap_chain_spacing", "GAP__CHAIN__SPACING 0.4...1.0 | 0.5", 0.5),
        (PF_OPTION,"make_convex", "MAKE__CONVEX", 1, ["No","More realistic polygons"]),
        (PF_OPTION,"zoom", "Zoom Import SVG", 1, ["1","2","3","4","5"]),
        (PF_INT, "temporisation", "Temporisation - Delay 1s...5s", 1),
    ],
    [],
    mosaic_command_line,
    menu="<Image>/Filters"
)

main()

