#-------------------------------------------------------------
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you 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.
#
#-------------------------------------------------------------

# This function has  the same functionality with img_translate but it handles multiple images at
# the same time. Each row of the input and output matrix represents a linearized image/matrix
# It translates the image and Optionally resizes the image (without scaling).
#
# .. code-block:: python
#
#   >>> import numpy as np
#   >>> from systemds.context import SystemDSContext
#   >>> from systemds.operator.algorithm import img_translate_linearized
#   >>> 
#   >>> with SystemDSContext() as sds:
#   ...     img = sds.from_numpy(
#   ...         np.array([[ 10., 20., 30.,
#   ...                     40., 50., 60.,
#   ...                     70., 80., 90. ]], dtype=np.float32)
#   ...     )
#   ...     result_img = img_translate_linearized(img, 1., 1., 3, 3, 255.0, 3, 3).compute()
#   ...     print(result_img.reshape(3, 3))
#   [[255. 255. 255.]
#    [255.  10.  20.]
#    [255.  40.  50.]]
#
#
# INPUT:
# ----------------------------------------------------------------------------------------------
# img_in                Input images as linearized 2D matrix with top left corner at [1, 1] (every row represents a linearized matrix/image)
# offset_x              The distance to move the image in x direction
# offset_y              The distance to move the image in y direction
# out_w                 Width of the output image
# out_h                 Height of the output image
# fill_value            The background of the image
# o_w                   Width of the original 2D images
# o_h                   Height of the original 2D images
# --------------------------------------------------------------------------------------------
#
# OUTPUT:
# --------------------------------------------------------------------------------------------
# img_out               Output matrix/image  (every row represents a linearized matrix/image)
# --------------------------------------------------------------------------------------------

m_img_translate_linearized = function(Matrix[Double] img_in, Double offset_x, 
Double offset_y,Integer out_w, Integer out_h, Double fill_value, Integer o_w, Integer o_h) 
return (Matrix[Double] img_out) {
  offset_x = round(offset_x)
  offset_y = round(offset_y)
  img_out = matrix(fill_value, rows=nrow(img_in), cols=out_w * out_h)
       parfor(y in 1:out_h,check=0) {
         for (x in 1:out_w) {
        src_x = x - offset_x
        src_y = y - offset_y
        if (src_x >= 1 & src_x <= o_w & src_y >= 1 & src_y <= o_h) {
          start_index = (src_y - 1) * o_w + src_x
          dest_index = (y - 1) * out_w + x
    
          if (start_index >= 1 & start_index <= ncol(img_in) & dest_index >= 1 & dest_index <= ncol(img_out)) {
            img_out[, dest_index] = img_in[, start_index]
          }  
        }
      }
    }
}


