Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correctly read JPEG compressed BLP images #4685

Merged
merged 12 commits into from Feb 19, 2022
Merged

Correctly read JPEG compressed BLP images #4685

merged 12 commits into from Feb 19, 2022

Conversation

Meithal
Copy link
Contributor

@Meithal Meithal commented Jun 12, 2020

Fixes #4684

Changes proposed in this pull request:

  • At this moment trying to open a BLP1 file is a black square
  • This is because BLP1 are a 4channel files in the BGRA format and BLP1 tries to open it in RGB format
  • This open the BLP1 file in the correct format, + counter basic protection where picture is made completely transparent because war3 engine ignores alpha channel

@Meithal
Copy link
Contributor Author

Meithal commented Jun 12, 2020

Also this can break some code, if you want to save the picture in JPG, you will have to convert it to RGB mode before.

@radarhere radarhere changed the title Handles BLP1 format Handle BLP1 format Jun 12, 2020
@hugovk
Copy link
Member

hugovk commented Jun 27, 2020

Also this can break some code, if you want to save the picture in JPG, you will have to convert it to RGB mode before.

Please could you give some more info/examples of what this could break?

Also please can you add tests?

@Meithal
Copy link
Contributor Author

Meithal commented Jun 28, 2020

Hello, a test would be having a reference blp file and comparing if the extracted file matches the original one? How is it possible to get a blp file without using blizzard tools, do we need to be able to generate a blp with pillow in first place? #3176

For what can break, it means that previously this is possible

with Image.open("file.blp") as pil:
    pil.save("file.jpg")

Now you have to write

with Image.open("file.blp") as pil:
    conv = pil.convert("RGB")
    conv.save("file.jpg")

@hugovk
Copy link
Member

hugovk commented Jun 30, 2020

You can see the existing BLP tests in https://github.com/python-pillow/Pillow/blob/master/Tests/test_file_blp.py

They test an opened BLP image is as expected by comparing against a PNG version.

The BLP tests and files were added in #3007, and see the discussion there, that the files must be able to be shared under the Pillow licence, so it's most likely we cannot include files copyright someone else (unless specific permission is given).

@hugovk
Copy link
Member

hugovk commented Jun 28, 2021

@Meithal Hi! This PR is now a year old, did you get a chance to look into adding tests?

@Meithal
Copy link
Contributor Author

Meithal commented Jun 29, 2021

I'll try to tackle it this weekend.

@radarhere radarhere changed the title Handle BLP1 format Added support for BLP1 format Jan 17, 2022
@radarhere radarhere changed the title Added support for BLP1 format Correctly read JPEG compressed BLP images Jan 18, 2022
@radarhere
Copy link
Member

I've removed the alpha channel, which I think you weren't sure about in the first place. I did this for simplicity, and because there wasn't a test image demonstrating the need.

@Meithal to be clear, is the image that you've included here one that can be included under our license and distributed with Pillow? There was a long discussion about this in #3007

@Meithal
Copy link
Contributor Author

Meithal commented Feb 15, 2022

I'm not sure, it should have the same license than the other ones, it is generated automatically by blizzard worldedit and included as is in a map archive.

I have been trying to find an example of blp1 file with a transparent component to include it in the test, and cover those paths but got lost in a rabbit hole doing that (I was looping through thousands of existing archives), I think your patches removed the alpha support, it may still be useful to not do so, maybe add an extra function that keeps it so older code doesn't break?

@radarhere
Copy link
Member

I'm not sure, it should have the same license than the other ones, it is generated automatically by blizzard worldedit and included as is in a map archive.

I found that we already have a BLP1 image in our repository, so I've pushed a commit to switch to that instead.

I think your patches removed the alpha support, it may still be useful to not do so, maybe add an extra function that keeps it so older code doesn't break?

This PR exists because no BLP1 images were being read correctly, yes? Not just ones with alpha, but any of them. So surely all older code is broken, and this PR only makes things better?

@hugovk hugovk merged commit 3d33987 into python-pillow:main Feb 19, 2022
@radarhere
Copy link
Member

You may be interested to know that #6069 currently adds support for alpha in BLP1 when _blp_compression == 1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BLP1 files are not handled
3 participants