#!/usr/bin/env python3
#
# NAME
#
#        med2image
#
# DESCRIPTION
#
#        'med2image' converts from medical image data files to
#        display-friendly formats (like png and jpg).
#
# HISTORY
#
# 23 February 2015
# o Initial design and coding.
#

# System imports
import os
import sys
sys.path.insert(1, os.path.join(os.path.dirname(__file__), '..'))
import argparse
from med2image import med2image

str_version = "1.0.4"
#import pdb; pdb.set_trace()

#define script arguments
parser = argparse.ArgumentParser(
    description="med2view converts an input medical image file to a more conventional graphical format.")
parser.add_argument("-i", "--inputFile",
                    help="input file",
                    dest='inputFile')
parser.add_argument("-o", "--outputFileStem",
                    help="output file",
                    default="output.jpg",
                    dest='outputFileStem')
parser.add_argument("-d", "--outputDir",
                    help="output image directory",
                    dest='outputDir',
                    default='.')
parser.add_argument("-t", "--outputFileType",
                    help="output image type",
                    dest='outputFileType',
                    default='')
parser.add_argument("-s", "--sliceToConvert",
                    help="slice to convert (for 3D data)",
                    dest='sliceToConvert',
                    default='-1')
parser.add_argument("-f", "--frameToConvert",
                    help="frame to convert (for 4D data)",
                    dest='frameToConvert',
                    default='-1')
parser.add_argument("--printElapsedTime",
                    help="print program run time",
                    dest='printElapsedTime',
                    action='store_true',
                    default=False)
parser.add_argument('-r', '--reslice',
                    help="save images along i,j,k directions -- 3D input only",
                    dest='reslice',
                    action='store_true',
                    default=False)
parser.add_argument('--showSlices',
                    help="show slices that are converted",
                    dest='showSlices',
                    action='store_true',
                    default=False)
parser.add_argument('--func',
                    help="apply transformation function before saving",
                    dest='func')
parser.add_argument("-x", "--man",
                    help="man",
                    dest='man',
                    action='store_true',
                    default=False)
parser.add_argument("-y", "--synopsis",
                    help="short synopsis",
                    dest='synopsis',
                    action='store_true',
                    default=False)
parser.add_argument('-v', '--version',
                    help    = 'if specified, print version number',
                    dest    = 'b_version',
                    action  = 'store_true',
                    default = False)

def synopsis(ab_shortOnly=False):
    scriptName = os.path.basename(sys.argv[0])
    shortSynopsis = '''
    NAME

	    med2image.py - convert medical images to jpg/png/etc.

    SYNOPSIS

            %s                                   \\
                     -i|--input <inputFile>                \\
                    [-d|--outputDir <outputDir>]           \\
                     -o|--output <outputFileStem>          \\
                    [-t|--outputFileType <outputFileType>] \\
                    [-s|--sliceToConvert <sliceToConvert>] \\
                    [-f|--frameToConvert <frameToConvert>] \\
                    [--showSlices]                         \\
                    [--func {invertIntensities}]            \\
                    [--reslice]                            \\
                    [-x|--man]				   \\
		    [-y|--synopsis]

    BRIEF EXAMPLE

	    med2image.py -i slice.dcm -o slice.jpg

    ''' % scriptName

    description = '''
    DESCRIPTION

        `%s' converts input medical image formatted data to a more
        display friendly format, such as jpg or png.

        Currently understands NIfTI and DICOM input formats.

    ARGS

        -i|--inputFile <inputFile>
        Input file to convert. Typically a DICOM file or a nifti volume.

        [-d|--outputDir <outputDir>]
        The directory to contain the converted output image files.

        -o|--outputFileStem <outputFileStem>
        The output file stem to store conversion. If this is specified
        with an extension, this extension will be used to specify the
        output file type.

        SPECIAL CASES:
        For DICOM data, the <outputFileStem> can be set to the value of
        an internal DICOM tag. The tag is specified by preceding the tag
        name with a percent character '%%', so 

            -o %%ProtocolName

        will use the DICOM 'ProtocolName' to name the output file. Note
        that special characters (like spaces) in the DICOM value are 
        replaced by underscores '_'.

        Multiple tags can be specified, for example

            -o %%PatientName%%PatientID%%ProtocolName

        and the output filename will have each DICOM tag string as 
        specified in order, connected with dashes.

        A special %%inputFile is available to specify the input file that
        was read (without extension).

        [-t|--outputFileType <outputFileType>]
        The output file type. If different to <outputFileStem> extension,
        will override extension in favour of <outputFileType>.

        [-s|--sliceToConvert <sliceToConvert>]
        In the case of volume files, the slice (z) index to convert. Ignored
        for 2D input data. If a '-1' is sent, then convert *all* the slices.
        If an 'm' is specified, only convert the middle slice in an input
        volume.

        [-f|--frameToConvert <sliceToConvert>]
        In the case of 4D volume files, the volume (V) containing the
        slice (z) index to convert. Ignored for 3D input data. If a '-1' is
        sent, then convert *all* the frames. If an 'm' is specified, only
        convert the middle frame in the 4D input stack.

        [--showSlices]
        If specified, render/show image slices as they are created.
        
        [--func {invertIntensities}]
        Apply the specified transformation function before saving.

        [--reslice]
        For 3D data only. Assuming [i,j,k] coordinates, the default is to save
        along the 'k' direction. By passing a --reslice image data in the 'i' and
        'j' directions are also saved. Furthermore, the <outputDir> is subdivided into
        'slice' (k), 'row' (i), and 'col' (j) subdirectories.

        [-x|--man]
        Show full help.

        [-y|--synopsis]
        Show brief help.

    EXAMPLES

    NIfTI

    o Convert each slice in a NIfTI volume 'vol.nii' to a jpg called
      'image-sliceXXX.jpg' and store results in a directory called 'out':

    		med2image -i vol.nii -d out -o image.jpg -s -1

    o Convert only the middle slice in an input volume and store in current
      directory:

    		med2image -i vol.nii -o image.jpg -s m

    o Convert a specific slice, i.e. slice 20

    		med2image -i vol.nii -o image.jpg -s 20

    DICOM

    o Simply convert a DICOM file called 'slice.dcm' to a jpg called 'slice.jpg':

    		med2image -i slice.dcm -o slice.jpg

    o Convert all DICOMs in a directory. Note that is assumes all DICOM files
      in the directory containing the passed file belong to the same series.
      Conversion will fail if multiple series are interspersed in the same dir.

            med2image -i slice.dcm -o slice.jpg -s -1

    GITHUB

        o See https://github.com/FNNDSC/med2image for more help and source.


    ''' % (scriptName)
    if ab_shortOnly:
        return shortSynopsis
    else:
        return shortSynopsis + description


# parse passed arguments
args = parser.parse_args()

if args.b_version:
    print("Version: %s" % str_version)
    sys.exit(1)

if args.man or args.synopsis:
    if args.man:
        str_help = synopsis(False)
    else:
        str_help = synopsis(True)
    print(str_help)
    sys.exit(1)

str_outputFileStem, str_outputFileExtension = os.path.splitext(args.outputFileStem)
if len(str_outputFileExtension):
    str_outputFileExtension = str_outputFileExtension.split('.')[1]
try:
    str_inputFileStem, str_inputFileExtension = os.path.splitext(args.inputFile)
except:
    print(synopsis(False))
    sys.exit(1)

if not len(args.outputFileType) and len(str_outputFileExtension):
    args.outputFileType = str_outputFileExtension

if len(str_outputFileExtension):
    args.outputFileStem = str_outputFileStem

b_niftiExt = (str_inputFileExtension == '.nii' or
              str_inputFileExtension == '.gz')
b_dicomExt = str_inputFileExtension == '.dcm'
if b_niftiExt:
    C_convert = med2image.med2image_nii(
        inputFile=args.inputFile,
        outputDir=args.outputDir,
        outputFileStem=args.outputFileStem,
        outputFileType=args.outputFileType,
        sliceToConvert=args.sliceToConvert,
        frameToConvert=args.frameToConvert,
        showSlices=args.showSlices,
        reslice=args.reslice
    )

    print('sliceToConvert:', args.sliceToConvert)

if b_dicomExt:
    C_convert = med2image.med2image_dcm(
        inputFile=args.inputFile,
        outputDir=args.outputDir,
        outputFileStem=args.outputFileStem,
        outputFileType=args.outputFileType,
        sliceToConvert=args.sliceToConvert,
        reslice=args.reslice
    )

if args.func:
    C_convert.func = args.func

# And now run it!
med2image.misc.tic()
C_convert.run()
if args.printElapsedTime: print("Elapsed time = %f seconds" % med2image.misc.toc())
sys.exit(0)

