import numpy as np
import scipy.misc

def mosaic(f,N,s=1.0):
    f = np.asarray(f)
    d,h,w = f.shape
    N = int(N)
    nLines = int(np.ceil(float(d)/N))
    nCells = int(nLines*N)
 
    # Add black slices to match the exact number of mosaic cells
    fullf = np.resize(f, (nCells,h,w))
    fullf[d:nCells,:,:] = 0

    Y,X = np.indices((nLines*h,N*w))
    Pts = np.array([
            (np.floor(Y/h)*N + np.floor(X/w)).ravel(),
            np.mod(Y,h).ravel(),
            np.mod(X,w).ravel() ]).astype(int).reshape((3,int(nLines*h),int(N*w)))
    g = fullf[Pts[0],Pts[1],Pts[2]]
    if (s != 1.0):
        #g = scipy.ndimage.interpolation.zoom(g,s,order=5)
        g = scipy.misc.imresize(g,s,interp='bilinear')
    return g

"""
Explanation: Function mosaic
Synopse
Create a 2D visualization of a 3D image.


g = mosaic(f,N,s)


g: Image. Mosaic of 2D images.


f: Image. 3D image.

N: Integer. Number of image columns in mosaic.
s: Float. Default: 1.0. Scale factor.

Description
This function puts the slices of a 3D image side-by-side on a mosaic (2D image). The objective is to provide basic 3D visualization.
End of explanation
"""


testing = (__name__ == "__main__")
if testing:
    ! jupyter nbconvert --to python mosaic.ipynb
    import sys
    import os
    import numpy as np
    ia898path = os.path.abspath('../../')
    if ia898path not in sys.path:
        sys.path.append(ia898path)
    import ia898.src as ia


"""
Explanation: Examples
End of explanation
"""


if testing:
    t = np.arange(60)
    t.shape = (5,3,4)

    print('Original 3D image:\n', t, '\n\n')

    for i in range(1,7):
        g = ia.mosaic(t,i)
        print('Mosaic with %d image column(s):\n' % i)
        print(g)
        print()

    for i in range(1,7):
        g = ia.mosaic(t,i,0.8)
        print('Mosaic with %d image column(s):\n' % i)
        print(g)
        print()

"""
Explanation: Example 1
End of explanation
"""


if testing:
    d,r,c = np.indices((5,3,4))
    t = ((r + c)%2) >0

    for i in range(1,7):
       g = ia.mosaic(t,i,0.8)
       print('Mosaic with %d image column(s):\n' % i)
       print(g)
       print()

"""
Explanation: Example 2
End of explanation
"""


