This project has moved. For the latest updates, please go here.

Defective PSD files from portrait images

Feb 28, 2016 at 10:47 AM
Hello guys!

I'm trying to convert some images via the Magick.NET library to psd files. So far so good. Yesterday I tried to convert an image in portrait orientation (its height being larger than its width) and the resulting psd file was corrupted. First I thought it had something to do with the input image until I tried another image (also a portrait one) and another defective psd file was created. So I manually rotated both images to landscape and tried again only to find out that now everything works as expected. Using random images from different sources always yields the same result: Landscape image are save without error, but portrait ones will be corrupted.

The code used for the conversion is pretty straight forward:
using (MagickImageCollection collection = new MagickImageCollection())
using (MagickImage magickImage = new MagickImage("C:\portrait.jpg"))
{
    collection.Add(magickImage);
    collection.Write("C:\defective.psd");
}
I wouldn't open an issue because I'm still very new to Magick.NET and maybe I'm just doing something wrong or I'm understanding the mechanics wrong. So can anyone help me or confirm this is a bug?

Regards Lokscha
Coordinator
Feb 28, 2016 at 12:01 PM
Can you explain by what you mean by a corrupted PSD. Is it possible that this issue only occurs when your input image has no alpha channel. It does look like our ImageMagick code has an issue when there is only one image in your collection and it has an alpha channel..
Feb 28, 2016 at 2:37 PM
Okay, let me elaborate this a little bit further. The above code is just an example, which helps reproduce the effect.
In reality I add the image as a layer (not background-layer) to the MagickImageCollection with the following properties:
magickImage.Label = layerName;
magickImage.Format = MagickFormat.Psd;
magickImage.HasAlpha = true; // This leads to the image being threaded as a real layer (not background layer) in Photoshop.
magickImage.Compose = CompositeOperator.No; // This leads to the layer not being locked.
magickImage.CompressionMethod = CompressionMethod.RLE;
magickImage.Page = new MagickGeometry(x: 0, y: 0, width: magickImage.Width, height: magickImage.Height);
So I actually do force the use of an alpha channel, but it is not related to the error. I literally tried myriads of different combinations, like adding multiple layers (with or without including the merged layer at position zero (which, by the way, seems to be unnecessary when building an psd file with just one layer...)), changing their orientation back and forth, changing the sorting, the composition, the page-property and so on and so on. I have done this for the last two days to no avail. To reproduce the behaviour the code from the initial post suffices.

Defective means, that Photoshop wont even open the file. It says that it cannot read the layer data and asks if it should try and read the image as a whole, then resulting in another error stating, that this file is not compatible with this version of PS (which is CC2015 by the way). But when rotating such portrait images prior to saving (even with Magick.NET itself) this does not occur and the resulting psd file work like a charm.
Coordinator
Feb 28, 2016 at 4:02 PM
I don't own Photoshop so I cannot really test this issue.

What is the exact version of Magick.NET that you are using? And can you open the PSD files that are created with the following code?
using (MagickImage image = new MagickImage("xc:red", 50, 100))
{
  image.Alpha(AlphaOption.Opaque);
  image.Write("test1.psd");
}

using (MagickImageCollection images = new MagickImageCollection())
{
  MagickImage image = new MagickImage("xc:red", 50, 100);
  image.Alpha(AlphaOption.Opaque);
  images.Add(image);
  images.Write("test2.psd");
}
It should create the same file in both situations.
Feb 28, 2016 at 4:47 PM
I am using the newest one available which at the moment is Magick.NET 7.0.0.0102.
Prior I used version '7.0.0.0022' but due to some strange problems with TIFF images I switched to '7.0.0.0101' and yesterday because of the psd problem I updated again.

Using your provided code generates two defective psd files. When I switch width and heigth I see the expected red images. You do not need to know or even have Photoshop. Just generate the psd files and use FastStones MaxView image viewer (portable, windows only) to open them. MaxView does not show fancy error messages like PS does, but the images are clearly broken.
Coordinator
Feb 29, 2016 at 4:24 PM
I don't want to install that piece of software to test this. I asked someone who has Photshop CS and that could open the file without any problem. Which Quantum (Q8/Q16/Q16-HDRI) are you using? And can you share the file (with something like DropBox) that you created so that I can compare it to my file?
Feb 29, 2016 at 5:17 PM
I am using the Q8 - AnyCPU version of Magick.NET since I only convert images for web content. The requested files are here.
By the way, MaxView is portable so no installation required. But its okay, I wouldn't use any unknown software either. Oh, because I haven't said it yet: Thank you very much for your help. I am a little lost right now because of this.
Coordinator
Feb 29, 2016 at 7:12 PM
Thanks for the quick response. I can now investigate this much better. I am not sure when I will start on this because I will start my new job tomorrow and I don't know if I have the time/energy for it this week.
Feb 29, 2016 at 8:14 PM
Well take your time and good luck with the new job.
Coordinator
Mar 5, 2016 at 10:13 PM
I have good news and bad news. The good news is that the issue is fixed. The bad news is that I don't know why. Can you give it another try when I publish the next release of Magick.NET tomorrow?
Mar 6, 2016 at 4:06 PM
Well then I will just take the good news and I am looking forward to testing the new version. As for the bad news, believe me, sometime when you are in the middle of doing something completely different, the answer will come to you. That is an observation I made in the last couple of years trying to get my head into some strange problems.
Coordinator
Mar 6, 2016 at 5:29 PM
The new version has been published. Please let me know if it works for you now.
Mar 6, 2016 at 7:34 PM
So I just tried the new version and can confirm, that now portrait images can be saved as psd files. Thank you very much for your help in this matter.