# IntQuantCl is originally a function of julia mzMl imzML with the adition 
# of altering the scale of the colors according to colorlevel.
function IntQuantCl( slice , colorLevel)
    # Compute scale factor for amplitude discretization
    lower  = minimum( slice )
    scale  = colorLevel / maximum( slice )
    dim    = size( slice )
    image  = zeros( UInt8, dim[1], dim[2] )
    for i in 1:length( slice )
        image[i] = convert( UInt8, floor( slice[i] * scale + 0.5 ) )
    end
    return image
end

# SaveBitmap originally a function of the mzML imzML library in julia,
# This function dinamically adjust the color palete adjusting to the ammount of colors
# available in pixmap
function SaveBitmapCl( name, pixMap::Array{UInt8,2}, colorTable::Array{UInt32,1} )
    # Get image dimensions
    dim = size( pixMap )
    if length( dim ) != 2
      return 0
    end
    # Normalize pixel values to get a more accurate reading of the image
    minVal = minimum(pixMap)
    maxVal = maximum(pixMap)
    pixMap = round.(UInt8, 255 * (pixMap .- minVal) ./ (maxVal - minVal))
    # Compute row padding
    padding = ( 4 - dim[1] & 0x3 ) & 0x3
    # Compute file dimensions. Header = 14 + 40 + ( 256 * 4 ) = 1078
    offset   = 1078
    imgBytes = dim[2] * ( dim[1] + padding )
    # Create file
    stream = open( name, "w" )
    # Save file header
    write( stream, UInt16( 0x4D42 ) )
    write( stream, UInt32[ offset + imgBytes, 0 , offset ] )
    # Save info header
    write( stream, UInt32[ 40, dim[1], dim[2], 0x80001, 0 ] )
    write( stream, UInt32[ imgBytes, 0, 0, 256, 0 ] )
    # Save color table
    write( stream, colorTable )
    if length( colorTable ) < 256
        fixTable = zeros( UInt32, 256 - length( colorTable ) )
        write( stream, fixTable )
    end
    # Save image pixels
    if padding == 0
        for i = 1:dim[2]
            write( stream, pixMap[:,i] )
        end
    else
        zeroPad = zeros( UInt8, padding )
        for i in 1:dim[2]
            write( stream, pixMap[:,i] )
            write( stream, zeroPad )
        end
    end
    # Close file
    close( stream )
end

# SaveBitmap originally a function of the mzML imzML library in julia,
# now has an adjustment for NaN values in case they exist to mantain data integrity
function GetMzSliceJl(imzML, mass, tolerance)
    # Alloc space for slice
    width = maximum(imzML[1, :])
    height = maximum(imzML[2, :])
    image = fill(0.0, width, height)

    for i in 1:size(imzML)[2]
        index = julia_mzML_imzML.FindMass(imzML[3, i], mass, tolerance)
        if index != 0
            image[imzML[1, i], imzML[2, i]] = imzML[4, i][index]
        end
    end
    # Adjustment for NaN values with 0
    replace!(image, NaN => 0.0)
    return image
end
