Baunsgaard commented on code in PR #1903:
URL: https://github.com/apache/systemds/pull/1903#discussion_r1325649041


##########
scripts/builtin/img_mirror_linearized.dml:
##########
@@ -0,0 +1,82 @@
+#-------------------------------------------------------------
+#
+# 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_mirror but it handles 
multiple images at
+# the same time. Each row of the input and output matrix represents a 
linearized image/matrix
+# It flips an image on the X (horizontal) or Y (vertical) axis.
+# INPUT:
+# 
-----------------------------------------------------------------------------------------
+# img_matrix           Input matrix/image (every row represents a linearized 
matrix/image)
+# horizontal_axis      flip either in X or Y axis
+# original_rows        number of rows in the original 2-D images 
+# original_cols        number of cols in the original 2-D images 
+# 
-----------------------------------------------------------------------------------------
+#
+# OUTPUT:
+# 
-----------------------------------------------------------------------------------------
+# R            Output matrix/image  (every row represents a linearized 
matrix/image)
+# 
-----------------------------------------------------------------------------------------
+
+m_img_mirror_linearized = function(matrix[double] img_matrix, Boolean 
horizontal_axis, 
+Integer original_rows, Integer original_cols) return (matrix[double] R) {
+  n = ncol(img_matrix);
+  R = matrix(0, rows=nrow(img_matrix), cols=n);
+  rows = original_rows;
+  cols = original_cols;
+
+  if (horizontal_axis) {
+    for (i in seq(1, (rows %/% 2) * cols, cols)) {
+      start = i;
+      end = i + cols - 1;
+      mirrorStart = (n - end) + 1;
+      mirrorEnd = (n - start) + 1;
+      R[, start:end] = img_matrix[, mirrorStart:mirrorEnd];
+      R[, mirrorStart:mirrorEnd] = img_matrix[, start:end];
+    }
+    if (rows %% 2 == 1) {
+      midStart = ((rows %/% 2)) * cols + 1;
+      midEnd = midStart + cols - 1;
+      R[, midStart:midEnd] = img_matrix[, midStart:midEnd];
+    }
+  } 
+  else {
+    for (i in 1:nrow(img_matrix)) {
+      offset = 1;
+      while (offset <= n) {
+        # Get the sub-matrix
+        end = min(n, offset + cols - 1);
+        # Reverse the sub-matrix columns
+        reversed_sub_matrix = matrix(0, rows=1, cols=cols);
+        idx = 1;
+        for (j in offset:end) {
+          reversed_sub_matrix[1, cols - idx + 1] = img_matrix[i, j];

Review Comment:
   this line here is very inefficient. 
   in general it should be used as a last solution to assign individual cells 
in a matrix. 
   
   I suggest you think about this for a wile and see if you can find a better 
way where you slice entire columns or rows.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to