1. Find your way through the deepest dungeon in the 18th Mini Mapping Contest Poll.
    Dismiss Notice
  2. A brave new world lies beyond the seven seas. Join the 34th Modeling Contest today!
    Dismiss Notice
  3. Check out the Staff job openings thread.
    Dismiss Notice
Dismiss Notice
Hive 3 Remoosed BETA - NOW LIVE. Go check it out at BETA Hive Workshop! Post your feedback in this new forum BETA Feedback.
Dismiss Notice
60,000 passwords have been reset on July 8, 2019. If you cannot login, read this.

The evil that is ".blp".

Discussion in 'Programming' started by Dr Super Good, Sep 17, 2015.

  1. Dr Super Good

    Dr Super Good

    Spell Reviewer

    Joined:
    Jan 18, 2005
    Messages:
    26,189
    Resources:
    3
    Maps:
    1
    Spells:
    2
    Resources:
    3
    So I am working on a Java library containing service providers for the javax.imageio system to read/write (hopefully) ".blp" files. So far I have managed to get image (mipmap) extraction working for BLP1 files with content mode 0 (JPEG container). Although it currently does not load a buffered image of the contents, it does dump the file to disk for viewing. The files dumped appear valid which is good news.

    However the JPEG structure produced seems to be unusual and poorly support. It appears to be produced by some imaging library from Intel.

    For the high-resolution Gryphon Rider texture this is the dumped JPEG for mipmap level 0 (full size).
    [​IMG]
    What it appears like to you is questionable and probably heavily influenced by display calibration. Poor browser implementations might show you nonsense. The thumbnail is also likely to appear garbage as I doubt the quality of the imaging libraries used by the server. However what it should be is the correct texture including alpha with exception of the red and blue colour channels being swapped (part of the blp specification). Alpha should also be producing some visual indicator that may or may not be correct (depends on blp specification of the meaning of alpha).

    For comparison this is what GIMP sees it as after the colours are swapped. This image should not be subject to display calibration as GIMP appeared to use the intended profile to interpret the image. Notice that the alpha data appears to have been lost (it was loaded as a RGB image only).
    [​IMG]

    This is how it looks if a RGB dump is made with BLPLab tool which should be interpreting the file correctly. Again the colour channels have been swapped for you. Obviously alpha is lost but the detail contained in the alpha is not (unlike the GIMP processed image).
    [​IMG]

    The unprocessed JPEG dump is interpreted completely incorrectly by Paint Shop Pro. By the looks of the produced image it is not converting colour space correctly into RGB. This is likely the result of missing support or non-compliance with the JPEG standard. Hence why I used GIMP since it appears to be more compliant.

    The big question is what is going on with the alpha channel. The dumped unprocessed image is a "JPEG File Interchange Format (JFIF)" compliant image. Decent implementations of JPEG such as used by GIMP do load the RGB colours correctly from it however the alpha information is lost.

    I am still investigating the exact standards used by the dumped JPEG file, specifically how the alpha is stored. So far I suspect it uses a slightly different colour space from usual with it being either YCBCR or Y′CBCR (which ever of the two is not used by most JPEG files). The image is also generated by Intel imaging libraries according to meta data. It must also be using parts of the standard defined before 2004 for logical reasons.

    The documentation of what JPEG format blp supports is very poor. If it was fully understood it may be possible to generate smaller blp files (useful for mappers) or higher quality blp files (lossless) than previously possible.

    In any case feel free to mess around with the dumped image and see if you can make anything of it.
     

    Attached Files:

  2. GhostWolf

    GhostWolf

    Joined:
    Jul 29, 2007
    Messages:
    4,952
    Resources:
    2
    Tools:
    1
    Tutorials:
    1
    Resources:
    2
    It doesn't use anything fancy, it's just a normal 32bit BGRA format compressed as JPEG. It's non-standard, hence why software fails to load it properly. 4 channel JFIFs are only used by extensions as far as I know (Adobe and stuff like that), except for Blizzard of course.
     
  3. Ezekiel12

    Ezekiel12

    Joined:
    Mar 13, 2012
    Messages:
    1,061
    Resources:
    0
    Resources:
    0
    Do you know the following?


    Or Magos' specification:

     
  4. Dr Super Good

    Dr Super Good

    Spell Reviewer

    Joined:
    Jan 18, 2005
    Messages:
    26,189
    Resources:
    3
    Maps:
    1
    Spells:
    2
    Resources:
    3
    JFIF standard supports 4 channel RGBA under JPEG2000 compression according to some sources. However I am unsure when that was added to the standard.

    What is interesting is how in Windows 10 the image display is affected by colour profiles. What should be black is not black (incorrect gamma?). It also appears to see the alpha channel information as it shows true black where their is alpha but not true black where there is no alpha and it is meant to be true black.
    [​IMG]
    Next to that incorrect gamma and lack of forwarding alpha information it would appear to be interpreted correctly.

    Both of those I know about and have read. Unfortunately does not help with the JPEG compression much.

    EDIT:

    After some messing around I have come up with the following details.
    • The JPEG images are ABGR encoded. If they appear messed up (eg black being green) that is because 3 of the channels (BGR?) are being treated as YCBCR colours so are being wrongly converted to another colour space.
    • The images fail metadata validation checks by some readers such as the JAVA standard JPEG plugin. I am currently unsure of how to pull meta data down short of a custom JPEG plugin or one from another party (a huge pain...).
    • I think alpha is not pre-multiplied, however that might also be due some processing by the reader. If I set the destination image to use pre-multiplied alpha artefacts appear.
    • To read the colour values correctly you need to bypass the YCBCR conversion most JPEG reader pipelines apply. This can be done by specifying a different colour profile (what Blizzard must have done with the Intel JPEG library) or in the case of JAVA simply accessing the rasterizer of the image reader (which reason for existence is specifically for situations like this).
    • In JAVA one should be able to manipulate the rasterizers such that the R and B colour channel swap is performed as the BufferedImage (or returned Rasterizer) is produced from the JPEG reader Rasterizer. This should produce the output image with correct colour channels in a single step.

    EDIT:
    And here is the result (uncompressed PNG) of processing the image with the JAVA standard JPEG plugin.
    [​IMG]
    Colours should now be correct and alpha present. Not sure if the alpha is being correctly output though (although the data is there as one can see).
     

    Attached Files:

    Last edited: Sep 18, 2015
  5. Dr Super Good

    Dr Super Good

    Spell Reviewer

    Joined:
    Jan 18, 2005
    Messages:
    26,189
    Resources:
    3
    Maps:
    1
    Spells:
    2
    Resources:
    3
    I have the plugin now successfully reading JPEG BLP files (all mipmaps) for BLP1. I still have problems with the metadata extraction and do not have image parameters supported perfectly at the moment.
     
  6. GhostWolf

    GhostWolf

    Joined:
    Jul 29, 2007
    Messages:
    4,952
    Resources:
    2
    Tools:
    1
    Tutorials:
    1
    Resources:
    2
    Yes, because like I said, 4 channel JFIF is used by actual registered extensions, not any company making random changes.
    I don't think anyone in the world supports JPEG2000.

    I too had to skip the color space transformations done by the JS JPG decoder I found.

    Either way...good job? not sure why you are doing this, but whatever.