Commit 751f9448a for imagemagick.org

commit 751f9448a8fe6a84894ab77d50f6f2190c4c9645
Author: Chi-Wei Lee <levi74108520963@gmail.com>
Date:   Tue Sep 29 23:29:49 2026 +0800

    Keep the directory when mogrify -format reads a compressed file (#8988)

    GetPathComponent() with BasePathSansCompressExtension kept the
    directory for an uncompressed file but returned only the base name for
    a file ending in .gz, .bz2, .Z, .svgz or .wmz. mogrify -format uses
    this path to name its output, so "mogrify -format bmp dir/image.ppm.gz"
    wrote image.bmp into the current directory instead of dir/image.bmp.

    Strip only the compression extension so the directory is kept.

diff --git a/MagickCore/utility.c b/MagickCore/utility.c
index 87ee3f0a1..26df67de7 100644
--- a/MagickCore/utility.c
+++ b/MagickCore/utility.c
@@ -1423,7 +1423,13 @@ MagickExport void GetPathComponent(const char *path,PathType type,
           (LocaleCompare(extension,"svgz") == 0) ||
           (LocaleCompare(extension,"wmz") == 0) ||
           (LocaleCompare(extension,"Z") == 0))
-        GetPathComponent(path,BasePath,component);
+        {
+          if (IsBasenameSeparator(*p) != MagickFalse)
+            p++;
+          q=strrchr(p,'.');
+          if ((q != (char *) NULL) && (q > p))
+            *q='\0';
+        }
       break;
     }
     case ExtensionPath:
diff --git a/Makefile.in b/Makefile.in
index 665fb6f44..24380b5dc 100644
--- a/Makefile.in
+++ b/Makefile.in
@@ -6272,6 +6272,7 @@ TESTS_TESTS = \
   tests/cli-colorspace.tap \
   tests/cli-exif.tap \
   tests/cli-heic.tap \
+  tests/cli-mogrify.tap \
   tests/cli-pcx.tap \
   tests/cli-pipe.tap \
   tests/cli-svg.tap \
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 062cec84b..5eb351726 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -41,6 +41,7 @@ TESTS_TESTS = \
   tests/cli-colorspace.tap \
   tests/cli-exif.tap \
   tests/cli-heic.tap \
+  tests/cli-mogrify.tap \
   tests/cli-pcx.tap \
   tests/cli-pipe.tap \
   tests/cli-svg.tap \
diff --git a/tests/cli-mogrify.tap b/tests/cli-mogrify.tap
new file mode 100755
index 000000000..abb1faf8f
--- /dev/null
+++ b/tests/cli-mogrify.tap
@@ -0,0 +1,40 @@
+#!/bin/sh
+#
+#  Copyright 1999 ImageMagick Studio LLC, a non-profit organization
+#  dedicated to making software imaging solutions freely available.
+#
+#  You may not use this file except in compliance with the License.  You may
+#  obtain a copy of the License at
+#
+#    https://imagemagick.org/license/
+#
+#  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.
+#
+#  Regression tests for mogrify.
+#
+. ./common.shi
+. ${srcdir}/tests/common.shi
+echo "1..2"
+
+dir=mogrify_format_dir
+rm -rf "$dir" mogrify_format_plain.bmp mogrify_format_gzip.bmp
+mkdir "$dir" || exit 1
+
+# -format writes the new file next to the input.
+${MAGICK} -size 2x2 xc:red "$dir/mogrify_format_plain.ppm" && \
+  ${MAGICK} mogrify -format bmp "$dir/mogrify_format_plain.ppm" && \
+  [ -f "$dir/mogrify_format_plain.bmp" ] && \
+  [ ! -f mogrify_format_plain.bmp ] && echo "ok" || echo "not ok"
+
+# The same holds when the input carries a compression extension.
+${MAGICK} -size 2x2 xc:red "$dir/mogrify_format_gzip.ppm.gz" && \
+  ${MAGICK} mogrify -format bmp "$dir/mogrify_format_gzip.ppm.gz" && \
+  [ -f "$dir/mogrify_format_gzip.bmp" ] && \
+  [ ! -f mogrify_format_gzip.bmp ] && echo "ok" || echo "not ok"
+
+rm -rf "$dir" mogrify_format_plain.bmp mogrify_format_gzip.bmp
+: