# Copyright 2019 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Image util ops."""

import tensorflow as tf


def get_ndims(image):
    return image.get_shape().ndims or tf.rank(image)


def to_4D_image(image):
    """Convert 2/3/4D image to 4D image.

    Args:
      image: 2/3/4D tensor.

    Returns:
      4D tensor with the same type.
    """
    with tf.control_dependencies(
        [
            tf.debugging.assert_rank_in(
                image, [2, 3, 4], message="`image` must be 2/3/4D tensor"
            )
        ]
    ):
        ndims = image.get_shape().ndims
        if ndims is None:
            return _dynamic_to_4D_image(image)
        elif ndims == 2:
            return image[None, :, :, None]
        elif ndims == 3:
            return image[None, :, :, :]
        else:
            return image


def _dynamic_to_4D_image(image):
    shape = tf.shape(image)
    original_rank = tf.rank(image)
    # 4D image => [N, H, W, C] or [N, C, H, W]
    # 3D image => [1, H, W, C] or [1, C, H, W]
    # 2D image => [1, H, W, 1]
    left_pad = tf.cast(tf.less_equal(original_rank, 3), dtype=tf.int32)
    right_pad = tf.cast(tf.equal(original_rank, 2), dtype=tf.int32)
    new_shape = tf.concat(
        [
            tf.ones(shape=left_pad, dtype=tf.int32),
            shape,
            tf.ones(shape=right_pad, dtype=tf.int32),
        ],
        axis=0,
    )
    return tf.reshape(image, new_shape)


def from_4D_image(image, ndims):
    """Convert back to an image with `ndims` rank.

    Args:
      image: 4D tensor.
      ndims: The original rank of the image.

    Returns:
      `ndims`-D tensor with the same type.
    """
    with tf.control_dependencies(
        [tf.debugging.assert_rank(image, 4, message="`image` must be 4D tensor")]
    ):
        if isinstance(ndims, tf.Tensor):
            return _dynamic_from_4D_image(image, ndims)
        elif ndims == 2:
            return tf.squeeze(image, [0, 3])
        elif ndims == 3:
            return tf.squeeze(image, [0])
        else:
            return image


def _dynamic_from_4D_image(image, original_rank):
    shape = tf.shape(image)
    # 4D image <= [N, H, W, C] or [N, C, H, W]
    # 3D image <= [1, H, W, C] or [1, C, H, W]
    # 2D image <= [1, H, W, 1]
    begin = tf.cast(tf.less_equal(original_rank, 3), dtype=tf.int32)
    end = 4 - tf.cast(tf.equal(original_rank, 2), dtype=tf.int32)
    new_shape = shape[begin:end]
    return tf.reshape(image, new_shape)

[(52, 'tensorflow.shape', 'tf.shape', True, 'import tensorflow as tf\n'), (53, 'tensorflow.rank', 'tf.rank', True, 'import tensorflow as tf\n'), (67, 'tensorflow.reshape', 'tf.reshape', True, 'import tensorflow as tf\n'), (94, 'tensorflow.shape', 'tf.shape', True, 'import tensorflow as tf\n'), (101, 'tensorflow.reshape', 'tf.reshape', True, 'import tensorflow as tf\n'), (21, 'tensorflow.rank', 'tf.rank', True, 'import tensorflow as tf\n'), (57, 'tensorflow.less_equal', 'tf.less_equal', True, 'import tensorflow as tf\n'), (58, 'tensorflow.equal', 'tf.equal', True, 'import tensorflow as tf\n'), (98, 'tensorflow.less_equal', 'tf.less_equal', True, 'import tensorflow as tf\n'), (61, 'tensorflow.ones', 'tf.ones', True, 'import tensorflow as tf\n'), (63, 'tensorflow.ones', 'tf.ones', True, 'import tensorflow as tf\n'), (99, 'tensorflow.equal', 'tf.equal', True, 'import tensorflow as tf\n'), (35, 'tensorflow.debugging.assert_rank_in', 'tf.debugging.assert_rank_in', True, 'import tensorflow as tf\n'), (81, 'tensorflow.debugging.assert_rank', 'tf.debugging.assert_rank', True, 'import tensorflow as tf\n'), (86, 'tensorflow.squeeze', 'tf.squeeze', True, 'import tensorflow as tf\n'), (88, 'tensorflow.squeeze', 'tf.squeeze', True, 'import tensorflow as tf\n')]