PSD file processed incorrectly?

Post any defects you find in the released or beta versions of the ImageMagick software here. Include the ImageMagick version, OS, and any command-line required to reproduce the problem. Got a patch for a bug? Post it here.
Post Reply
AUTOMATIC
Posts: 6
Joined: 2012-12-23T08:04:07-07:00
Authentication code: 6789

PSD file processed incorrectly?

Post by AUTOMATIC »

Imagemagick handles some PSD files incorrectly.

I uploaded one: http://www.speedyshare.com/gmmhE/N071CUSB-A00.psd

It's just transparent background with black and red text on it, and Photoshop displays it as such.

But when I convert it with imagemagick:

convert N071CUSB_A00.psd -layers merge N071CUSB_A00.png

I end up with just black rectangle (with red text on it). So all transparency is gone. Setting backround to any color does not change anything. Creating Magick::Image from this file results in same black rectangle (with red bits).

Version: ImageMagick 6.8.0-8 2012-12-05 Q8 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2012 ImageMagick Studio LLC
Features: OpenMP
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: PSD file processed incorrectly?

Post by magick »

Your image has a layer and a precombined image. You want the precombined image with this command:
  • convert 'N071CUSB_A00.psd[1]' N071CUSB_A00.png
N071CUSB_A00.png has a transparent background, as expected.
AUTOMATIC
Posts: 6
Joined: 2012-12-23T08:04:07-07:00
Authentication code: 6789

Re: PSD file processed incorrectly?

Post by AUTOMATIC »

But the problem is, I just want an image as it appears in Photoshop. Some other image would have something else in layer [1], whatever it is. What I uploaded is but one example.

And, additionally, I can not do this from Magick++'s Blob. I have no way of specifying this [1]: subImage/subRange do nothing, readImages applied to blob returns only one image (even though when I dump the blob to disk and then read it with readImages, it returns two)...

So far I indeed tried to work around this by dumping each file I access to to temp directory and loading it with [1] suffix. But it's a horrible solution. I'm working on a shell extension for displaying thumbnails for some files, and those archives I'm handling contain icons in PSD format. Accessing hard drive every time I want to display one (and people would have hundreds) is just wrong.
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: PSD file processed incorrectly?

Post by magick »

The precombined image is always the last image in the sequence of a PSD image file. The STL interface must be used to operate on image sequences or images (e.g. of format GIF, PSD, TIFF, MIFF, Postscript, & MNG) which are comprised of multiple image frames. Individual frames of a multi-frame image may be requested by adding array-style notation to the end of the file name (e.g. "animation.gif[1]" retrieves the second frame of a PSD image. See http://www.imagemagick.org/api/Image.php.
AUTOMATIC
Posts: 6
Joined: 2012-12-23T08:04:07-07:00
Authentication code: 6789

Re: PSD file processed incorrectly?

Post by AUTOMATIC »

Like I wrote before, STL interface is not helping:
readImages applied to blob returns only one image (even though when I dump the blob to disk and then read it with readImages, it returns two)...
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: PSD file processed incorrectly?

Post by magick »

It works for us, here is a code segment:
  • Blob blob(static_cast<const unsigned char*>(blobData), blobLen);
    delete [] blobData;

    list<Image> first;
    readImages( &first, blob ); // read all images into a STL list container
    list<Image>::iterator last = first.end(); // point to the PSD pre-combined image
    writeImages( --last, first.end(), "N071CUSB_A00.png", true ); // only write the pre-combined image to PNG
AUTOMATIC
Posts: 6
Joined: 2012-12-23T08:04:07-07:00
Authentication code: 6789

Re: PSD file processed incorrectly?

Post by AUTOMATIC »

writeImages() sort of works, but there is no way to retrieve the Magick::Image from list<Image> first. first.size() returns 1. There is only one image inside (again, even though writeImages() writes two to disk), and it's the one without transparency...
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: PSD file processed incorrectly?

Post by magick »

We get two images and the expected results, we'll post the entire codeset:

Code: Select all

#include <Magick++.h>
#include <string>
#include <iostream>
#include <fstream>

#if defined(MISSING_STD_IOS_BINARY)
#  define IOS_IN_BINARY ios::in

#else
#  define IOS_IN_BINARY ios::in | ios::binary
#endif

using namespace std;
using namespace Magick;

int main( int /*argc*/, char ** argv)
{
  InitializeMagick(*argv);
  try
  {
    string
      testimage;
    
    // Read raw data from file into BLOB
    testimage = "N071CUSB_A00.psd";
    ifstream in( testimage.c_str(), ios::in | IOS_IN_BINARY );
    if ( !in )
      {
        cout << "Failed to open file " << testimage << " for input!" << endl;
        exit(1);
      }
    unsigned char* blobData = new unsigned char[100000];
    char* c=reinterpret_cast<char *>(blobData);
    size_t blobLen=0;
    while( (blobLen< 100000) && in.get(*c) ) {
      c++;
      blobLen++;
    }
    if ((!in.eof()) || (blobLen == 0))
      {
        cout << "Failed to read file " << testimage << " for input!" << endl;
        exit(1);
      }
    in.close();

    // Construct Magick++ Blob
    Blob blob(static_cast<const unsigned char*>(blobData), blobLen);
    delete [] blobData;

    list<Image> first;
    readImages( &first, blob );
    list<Image>::iterator last = first.end();
    writeImages( --last, first.end(), "N071CUSB_A00.png", true );
  }
  catch( Exception &error_ )
  {
    cout << "Caught exception: " << error_.what() << endl;
    return 1;
  }
  catch( exception &error_ )
  {
    cout << "Caught exception: " << error_.what() << endl;
    return 1;
  }
  return 0;
}
AUTOMATIC
Posts: 6
Joined: 2012-12-23T08:04:07-07:00
Authentication code: 6789

Re: PSD file processed incorrectly?

Post by AUTOMATIC »

This code works for me to, see, but I do not want to write images to disk. I want to keep working with them, in memory, without touching disk.

For this purpose I want to retrieve the second picture from the first list in that code, presumably by writing:

Code: Select all

    list<Image> first;
    readImages( &first, blob );
    list<Image>::iterator last = first.end();
    --last;
    Image icon=*last;
And then icon should contain that second image (with transparency).

But what actually happens is that icon is still a black rectangle without transparency. writeImages() does work as intended, for me too, but getting two pictures from it is impossible: the list first has only one element, only one picture.

So if you add

Code: Select all

icon.write("a.png")
To the code I posted, you'll get a black rectangle, not nice transparent picture.
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: PSD file processed incorrectly?

Post by magick »

We're using ImageMagick 6.8.1-3 and we get expected results (transparent background) for a.png:
  • ...
    list<Image> first;
    readImages( &first, blob );
    list<Image>::iterator last = first.end();
    last--;
    Image icon=*last;
    icon.write("a.png");
AUTOMATIC
Posts: 6
Joined: 2012-12-23T08:04:07-07:00
Authentication code: 6789

Re: PSD file processed incorrectly?

Post by AUTOMATIC »

I guess It was a bug in my build.

I re-downloaded the library and rebuilt it, and voila, it now works as it should.

Thank you!
Post Reply