Menu ▾ ▴

#50 Refactored PSD image data loading

closed
None
5
2012-10-31
2010-07-15
No

Suppled is a refactored PSD loader, basically rewriting the ReadImageData function.

New features:

  • From 1/2 to 1/3 the memory usage - no extra full-image-size buffer allocations!
  • Improved loading speed - image data is loaded using line cache.
  • Improved CMYK to RGB conversion - done as integer, not as float.
  • Added support for flags, and implemented header only loading. (FIF_LOAD_NOPIXELS)
  • Added support for CMYK and LAB loading. (new PSD_CMYK and PSD_LAB flags, LAB should be considered a stub)
  • Correctly handles variable channel count and a stub to load channels beyond first alpha (as extra channels).
  • More then 200 lines of code reduction

Also:
Made FreeImage_Invert work with 16bit

Discussion

  • Mihail Naydenov

    Mihail Naydenov - 2010-07-15

    Improved psd loader and Invert to support 16bit

     
  • Hervé Drolon

    Hervé Drolon - 2010-07-20

    Hi Mihail,

    My first comments after a quick look at your code :

    1) why do you rename PSDP_ #define to PSDP_ #define ?

    2) I don't understand the need for the assignTri template function. This is more a problem than a gain in readability, as the RGBTRIPLE and RGBQUAD structures are handled differently on big-endian and little-endian OS : thus, 8-bit images must be handled differently than 16-bit images. Currently, the FIRGB16 and FIRGBA16 structures have a fixed memory ordering, but this may change as soon as graphic cards will be able to display this kind of buffer ... So we must process each case (8- and 16-bit) with a specific function.

    3) Why did you removed the comments about the conversion rules in psdParser::ReadImageData (ex psdParser::ProcessBuffer) ? It looks like the parser have now less capabilities (not sure about this) ...

    4) Do you have a test set that covers all possible output formats supported by Photoshop ?

    5) Did you test the plugin behavior with a big endian system (I see many uses of FREEIMAGE_BIGENDIAN #define) ?

    Lastly, I will commit the new FreeImage_Invert soon.

    Hervé

     
  • Mihail Naydenov

    Mihail Naydenov - 2010-07-20

    Hi

    1) Just because they clash with newly defined PSD_CMYK and PSD_LAB flags, and these I think need to be consistent with the others *_CMYK flags

    2) Huum I hope Im not missing or misunderstanding something these should be handled correctly. It might not be obvious assignTri is template function with specialization for BYTE...WORDs is handled in a generic way, but for now there are no alternatives ...and even if such variation emerge will easily be handled by a specialization for WORDs.
    template<class t="">
    static inline
    void assignTri(T r, T g, T b, T* out) // used for WORD for now
    {
    out[0] = r;
    out[1] = g;
    out[2] = b;
    }</class>

    template<>
    inline
    void assignTri<byte>(BYTE r, BYTE g, BYTE b, BYTE* out)
    {
    out[FI_RGBA_RED] = r;
    out[FI_RGBA_GREEN] = g;
    out[FI_RGBA_BLUE] = b;
    } </byte>

    3) Actually there was very little conversion at all... most of the code was copy-buffer variations for channel count and image type. The new code should handle all old cases plus it will load RGBA+ images which were assert(false) before, also RGBAF are loaded as well now. After loading, conversion is done for the color modes, in the most part reusing the old code.
    (The only deviation is that Gray+A images are not expanded to rgba. This is because I intend to handle these as gray + extra channel. A fix can be provided, though)

    4) I will upload a testcase tomorrow.

    5) PSD specification states: "All data is stored in big endian byte order." So most of these are if-not-def bigendian for windows to swap WORDs and pixel values when reading...bot no, I have not tested it on bigendian os-es...

     
  • Mihail Naydenov

    Mihail Naydenov - 2010-07-21

    (second) Improved psd loader + test and Invert 16bit

     
  • Mihail Naydenov

    Mihail Naydenov - 2010-07-21

    I have made some fixes to the code and created a test case. These are now attached.

    Here is link for a set of test images:
    http://www.megafileupload.com/en/file/252726/PSDtestImages-zip.html

     
  • Mihail Naydenov

    Mihail Naydenov - 2010-07-26

    The file is already pretty bad_alloc aware, there was one omission, however:

    File (second) Improved psd loader line 1147:

    BYTE* rle_line_start = new BYTE[largestRLELine];

    must be changed to:

            BYTE* rle_line_start = new (std::nothrow) BYTE[largestRLELine];
            if(!rle_line_start) {
                SAFE_DELETE_ARRAY(line_start);
                throw std::bad_alloc();
            }
    

    Note, we handle all exceptions (both our (const char *text) and standard (const std::exception& e)) in Load().

     
  • Mihail Naydenov

    Mihail Naydenov - 2010-07-26

    Oops, I forgot the dib itself. Here the correct code:

            BYTE* rle_line_start = new (std::nothrow) BYTE[largestRLELine];
            if(!rle_line_start) {
                FreeImage_Unload(bitmap);
                SAFE_DELETE_ARRAY(line_start);
                throw std::bad_alloc();
            }
    
     
  • Hervé Drolon

    Hervé Drolon - 2010-07-26

    Thanks for the patch :)
    I've integrated your patch to the CVS, together with corrections for VS 2008 warnings, and correction of the following error :

    inside
    FIBITMAP* psdParser::ReadImageData(FreeImageIO *io, fi_handle handle),
    inside
    case PSDP_COMPRESSION_RLE: // RLE compression
    this line
    WORD rleLineSizeList[nChannels][nHeight];
    => impossible with VS C++ (you cannot declare a dynamic array like this).
    use instead :
    WORD *rleLineSizeList = new (std::nothrow) WORD[nChannels*nHeight];

    With this error fix, all seems OK with this new parser.

    Now I just have to update the FreeImage documentation :)

    Hervé

     
  • Hervé Drolon

    Hervé Drolon - 2010-07-27

    regression and crash

     
  • Hervé Drolon

    Hervé Drolon - 2010-07-27

    Mihail,

    I've found one regression in the new PSD parser (see attached '6channels.psd' file).
    The main problem is that this file cause a crash of the library ...
    Can you have a look at it, using the CVS version of the parser ?

     
  • Mihail Naydenov

    Mihail Naydenov - 2010-07-27

    in the rle decompress part the line

    memcpy(line, rle_line, len);

    must be replaced with:

    memcpy(line, rle_line, line + len > line_end ? line_end - line : len); //< assert we dont write beyound eol

    same should be done with memset also.

    Apparently one of the lines had one byte bigger size in the rle size table, than the actual image. I dont know what causes this, may be "anti-compression", and can think of better way to handle this.

     
  • Mihail Naydenov

    Mihail Naydenov - 2010-07-28

    posting after midnight I forgot line_end is not defined.

    Before the for ch loop

    BYTE*const line_end = line_start + lineSize;

     
  • Hervé Drolon

    Hervé Drolon - 2010-07-28

    This doesn't work better :(
    The crash is now with memset ...

     
  • Mihail Naydenov

    Mihail Naydenov - 2010-07-29

    regression fix

     
  • Mihail Naydenov

    Mihail Naydenov - 2010-07-29

    Submitted the whole file. I have tested it with 6channels.psd - should work.

     
  • Hervé Drolon

    Hervé Drolon - 2010-07-29

    This time, all is OK on my side :)
    Changes commited to the CVS.

     
  • SourceForge Robot

    This Tracker item was closed automatically by the system. It was
    previously set to a Pending status, and the original submitter
    did not respond within 14 days (the time period specified by
    the administrator of this Tracker).

     

Anonymous
Anonymous

Add attachments
Cancel