### ImageMagick version

7.1.1-13

### Operating system

Linux

### Operating system, version and so on

Alpine 3.18.5

### Description

When upgrading ImageMagick from version 7.1.0-16 to 7.1.1-13, our command to compress HEIC image and set colorspace to gray doesn't work anymore. It produces a corrupted file. Moreover, the process time is 6 times slower than before.

Before :

```bash
$ cat /etc/os-release

NAME="Alpine Linux"
ID=alpine
VERSION_ID=3.15.6
PRETTY_NAME="Alpine Linux v3.15"
HOME_URL="https://alpinelinux.org/"
BUG_REPORT_URL="https://bugs.alpinelinux.org/"

$ magick --version

Version: ImageMagick 7.1.0-16 Q16-HDRI x86_64 2021-11-21 https://imagemagick.org
Copyright: (C) 1999-2021 ImageMagick Studio LLC
License: https://imagemagick.org/script/license.php
Features: Cipher DPC HDRI Modules
Delegates (built-in): bzlib cairo fontconfig freetype gslib heic jng jpeg lcms ltdl lzma png ps rsvg tiff webp x xml zlib
Compiler: gcc (10.3)

$ time magick convert -quality 50 -colorspace Gray image.heic new1.heic

real    0m2.156s
user    0m23.128s
sys     0m0.251s
```

After :

```bash
$ cat /etc/os-release

NAME="Alpine Linux"
ID=alpine
VERSION_ID=3.18.5
PRETTY_NAME="Alpine Linux v3.18"
HOME_URL="https://alpinelinux.org/"
BUG_REPORT_URL="https://gitlab.alpinelinux.org/alpine/aports/-/issues"

$ magick --version

Version: ImageMagick 7.1.1-13 Q16-HDRI x86_64 21276 https://imagemagick.org
Copyright: (C) 1999 ImageMagick Studio LLC
License: https://imagemagick.org/script/license.php
Features: Cipher DPC HDRI Modules
Delegates (built-in): bzlib cairo fontconfig freetype gslib heic jng jpeg jxl lcms ltdl lzma png ps raw rsvg tiff webp x xml zlib
Compiler: gcc (12.2)

$ time magick convert -quality 50 -colorspace Gray image.heic new2.heic

real    0m11.927s
user    0m12.242s
sys     0m0.151s
```

The original image (`image.heic`) and the generated files (before : `new1.heic`, after : `new2.heic`) are attached below in a zip file.

### Steps to Reproduce

```bash
magick convert -quality 50 -colorspace Gray image.heic new.heic
```

### Images

[images.zip](https://github.com/ImageMagick/ImageMagick/files/13557942/images.zip)

-colorspace Gray on HEIC image produces corrupted file
This is a GitHub Issue
repo:ImageMagick
owner:ImageMagick
Title : -colorspace Gray on HEIC image produces corrupted file
Issue date:

--- start body ---
### ImageMagick version

7.1.1-13

### Operating system

Linux

### Operating system, version and so on

Alpine 3.18.5

### Description

When upgrading ImageMagick from version 7.1.0-16 to 7.1.1-13, our command to compress HEIC image and set colorspace to gray doesn't work anymore. It produces a corrupted file. Moreover, the process time is 6 times slower than before.

Before :

```bash
$ cat /etc/os-release

NAME="Alpine Linux"
ID=alpine
VERSION_ID=3.15.6
PRETTY_NAME="Alpine Linux v3.15"
HOME_URL="https://alpinelinux.org/"
BUG_REPORT_URL="https://bugs.alpinelinux.org/"

$ magick --version

Version: ImageMagick 7.1.0-16 Q16-HDRI x86_64 2021-11-21 https://imagemagick.org
Copyright: (C) 1999-2021 ImageMagick Studio LLC
License: https://imagemagick.org/script/license.php
Features: Cipher DPC HDRI Modules
Delegates (built-in): bzlib cairo fontconfig freetype gslib heic jng jpeg lcms ltdl lzma png ps rsvg tiff webp x xml zlib
Compiler: gcc (10.3)

$ time magick convert -quality 50 -colorspace Gray image.heic new1.heic

real    0m2.156s
user    0m23.128s
sys     0m0.251s
```

After :

```bash
$ cat /etc/os-release

NAME="Alpine Linux"
ID=alpine
VERSION_ID=3.18.5
PRETTY_NAME="Alpine Linux v3.18"
HOME_URL="https://alpinelinux.org/"
BUG_REPORT_URL="https://gitlab.alpinelinux.org/alpine/aports/-/issues"

$ magick --version

Version: ImageMagick 7.1.1-13 Q16-HDRI x86_64 21276 https://imagemagick.org
Copyright: (C) 1999 ImageMagick Studio LLC
License: https://imagemagick.org/script/license.php
Features: Cipher DPC HDRI Modules
Delegates (built-in): bzlib cairo fontconfig freetype gslib heic jng jpeg jxl lcms ltdl lzma png ps raw rsvg tiff webp x xml zlib
Compiler: gcc (12.2)

$ time magick convert -quality 50 -colorspace Gray image.heic new2.heic

real    0m11.927s
user    0m12.242s
sys     0m0.151s
```

The original image (`image.heic`) and the generated files (before : `new1.heic`, after : `new2.heic`) are attached below in a zip file.

### Steps to Reproduce

```bash
magick convert -quality 50 -colorspace Gray image.heic new.heic
```

### Images

[images.zip](https://github.com/ImageMagick/ImageMagick/files/13557942/images.zip)

--- end body ---

The issue describes a regression in ImageMagick version 7.1.1-13 where the command to convert HEIC images to grayscale results in corrupted output files. This indicates a potential vulnerability in the handling of HEIC files, as it affects the integrity of the output and may lead to data loss or unexpected behavior when processing images.