import sys
from matplotlib import image as mpimg
import numpy as np
import os

DIPHA_CONST = 8067171840
DIPHA_IMAGE_TYPE_CONST = 1
DIM = 3

input_dir = os.path.join(os.getcwd(), sys.argv[1])
dipha_output_filename = sys.argv[2]
vert_filename = sys.argv[3]

input_filenames = [name
                   for name in os.listdir(input_dir)
                   if (os.path.isfile(input_dir + '/' + name)) and (name != ".DS_Store")]
input_filenames.sort()


image = mpimg.imread(os.path.join(input_dir, input_filenames[0]))
nx, ny = image.shape
del image
nz = len(input_filenames)

print(nx, ny, nz)
#sys.exit()
im_cube = np.zeros([nx, ny, nz])

i = 0
for name in input_filenames:
    sys.stdout.flush()
    print(i, name)
    fileName = input_dir + "/" + name
    im_cube[:, :, i] = mpimg.imread(fileName)
    i = i + 1


print('writing dipha output...')
with open(dipha_output_filename, 'wb') as output_file:
    # this is needed to verify you are giving dipha a dipha file
    np.int64(DIPHA_CONST).tofile(output_file)
    # this tells dipha that we are giving an image as input
    np.int64(DIPHA_IMAGE_TYPE_CONST).tofile(output_file)
    # number of points
    np.int64(nx * ny * nz).tofile(output_file)
    # dimension
    np.int64(DIM).tofile(output_file)
    # pixels in each dimension
    np.int64(nx).tofile(output_file)
    np.int64(ny).tofile(output_file)
    np.int64(nz).tofile(output_file)
    # pixel values
    for k in range(nz):
        sys.stdout.flush()
        print('dipha - working on image', k)
        for j in range(ny):
            for i in range(nx):
                val = int(-im_cube[i, j, k]*255)
                '''
                if val != 0 and val != -1:
                    print('val check:', val)
                '''
                np.float64(val).tofile(output_file)
    output_file.close()

print('writing vert file')
with open(vert_filename, 'w') as vert_file:
    for k in range(nz):
        sys.stdout.flush()
        print('verts - working on image', k)
        for j in range(ny):
            for i in range(nx):
                vert_file.write(str(i) + ' ' + str(j) + ' ' + str(k) + ' ' + str(int(-im_cube[i, j, k] * 255)) + '\n')
    vert_file.close()

print(nx, ny, nz)

[((27, 10, 27, 32), 'numpy.zeros', 'np.zeros', ({(27, 19, 27, 31): '[nx, ny, nz]'}, {}), '([nx, ny, nz])', True, 'import numpy as np\n'), ((10, 25, 10, 36), 'os.getcwd', 'os.getcwd', ({}, {}), '()', False, 'import os\n'), ((20, 21, 20, 64), 'os.path.join', 'os.path.join', ({(20, 34, 20, 43): 'input_dir', (20, 45, 20, 63): 'input_filenames[0]'}, {}), '(input_dir, input_filenames[0])', False, 'import os\n'), ((31, 4, 31, 22), 'sys.stdout.flush', 'sys.stdout.flush', ({}, {}), '()', False, 'import sys\n'), ((34, 23, 34, 45), 'matplotlib.image.imread', 'mpimg.imread', ({(34, 36, 34, 44): 'fileName'}, {}), '(fileName)', True, 'from matplotlib import image as mpimg\n'), ((15, 31, 15, 52), 'os.listdir', 'os.listdir', ({(15, 42, 15, 51): 'input_dir'}, {}), '(input_dir)', False, 'import os\n'), ((54, 8, 54, 26), 'sys.stdout.flush', 'sys.stdout.flush', ({}, {}), '()', False, 'import sys\n'), ((69, 8, 69, 26), 'sys.stdout.flush', 'sys.stdout.flush', ({}, {}), '()', False, 'import sys\n'), ((16, 23, 16, 61), 'os.path.isfile', 'os.path.isfile', ({(16, 38, 16, 60): "(input_dir + '/' + name)"}, {}), "(input_dir + '/' + name)", False, 'import os\n'), ((41, 4, 41, 25), 'numpy.int64', 'np.int64', ({(41, 13, 41, 24): 'DIPHA_CONST'}, {}), '(DIPHA_CONST)', True, 'import numpy as np\n'), ((43, 4, 43, 36), 'numpy.int64', 'np.int64', ({(43, 13, 43, 35): 'DIPHA_IMAGE_TYPE_CONST'}, {}), '(DIPHA_IMAGE_TYPE_CONST)', True, 'import numpy as np\n'), ((45, 4, 45, 26), 'numpy.int64', 'np.int64', ({(45, 13, 45, 25): '(nx * ny * nz)'}, {}), '(nx * ny * nz)', True, 'import numpy as np\n'), ((47, 4, 47, 17), 'numpy.int64', 'np.int64', ({(47, 13, 47, 16): 'DIM'}, {}), '(DIM)', True, 'import numpy as np\n'), ((49, 4, 49, 16), 'numpy.int64', 'np.int64', ({(49, 13, 49, 15): 'nx'}, {}), '(nx)', True, 'import numpy as np\n'), ((50, 4, 50, 16), 'numpy.int64', 'np.int64', ({(50, 13, 50, 15): 'ny'}, {}), '(ny)', True, 'import numpy as np\n'), ((51, 4, 51, 16), 'numpy.int64', 'np.int64', ({(51, 13, 51, 15): 'nz'}, {}), '(nz)', True, 'import numpy as np\n'), ((63, 16, 63, 31), 'numpy.float64', 'np.float64', ({(63, 27, 63, 30): 'val'}, {}), '(val)', True, 'import numpy as np\n')]