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

always convert image to RGB before checking RGB colors #2

Merged
merged 1 commit into from Jun 20, 2017

Conversation

illwieckz
Copy link
Member

Previously, Sloth was assuming the given addition texture was an RGB one. By the way, some PNG crusher use index PNG abilities to losslessly save space when possible. Because of that, Sloth crashes.

This code is faulty, it crashes on if not rgb[0] == rgb[1] == rgb[2]:

if shader["addition"]:
	img = Image.open(shader["abspath"]+os.path.sep+shader["addition"]+shader["ext"]["addition"], "r")

	gray = ( img.mode in ("L", "LA") )

	# check for RGB images with no actual non-gray color
	if not gray:
		colors = img.getcolors(maxcolors = 256)
		if colors:
			gray = True
			for _, rgb in colors:
				if not rgb[0] == rgb[1] == rgb[2]:
					gray = False
					break

	shader["meta"]["additionGrayscale"] = gray

Here is some experiments I did:

In [1]: from PIL import Image
In [2]: img = Image.open("…/tex-pk02_src.dpkdir/textures/shared_pk02_src/wall02c_a.png")
In [7]: _, rgb = img.getcolors(maxcolors = 256)[0]
In [8]: rgb
Out[8]: 0
In [9]: rgb[0]
TypeError                                 Traceback (most recent call last)
----> 1 rgb[0]

TypeError: 'int' object is not subscriptable

In [13]: img = Image.open("…/tex-pk02_source.pk3dir/textures/shared_pk02_src/wall02c_a.tga")
In [14]: img.getcolors()[0]
Out[14]: (44, (255, 255, 255))
In [15]: img = Image.open("…/tex-pk02_src.dpkdir/textures/shared_pk02_src/wall02c_a.png")
In [16]: img.getcolors()[0]
Out[16]: (23, 0)
In [17]: img = img.convert("RGB")
In [18]: img.getcolors()[0]
Out[18]: (44, (255, 255, 255))
In [19]: img = Image.open("…/tex-pk02_src.dpkdir/textures/shared_pk02_src/wall02c_a.png").convert("RGB")
In [20]: img.getcolors()[0]
Out[20]: (44, (255, 255, 255))
In [24]: _, rgb = img.getcolors()[0]
In [25]: rgb
Out[25]: (255, 255, 255)

The fix is easy:

if shader["addition"]:
-	img = Image.open(shader["abspath"]+os.path.sep+shader["addition"]+shader["ext"]["addition"], "r")
+	img = Image.open(shader["abspath"]+os.path.sep+shader["addition"]+shader["ext"]["addition"], "r").convert("RGB")

	gray = ( img.mode in ("L", "LA") )

	# check for RGB images with no actual non-gray color
	if not gray:
		colors = img.getcolors(maxcolors = 256)
		if colors:
			gray = True
			for _, rgb in colors:
				if not rgb[0] == rgb[1] == rgb[2]:
					gray = False
					break

	shader["meta"]["additionGrayscale"] = gray

Copy link
Member

@Viech Viech left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it make more sense to convert the image after the gray = ( img.mode in ("L", "LA") ) check? I didn't test, but from a distance it looks like that check can never succeed if the image was converted before.

@illwieckz
Copy link
Member Author

fixed

@illwieckz
Copy link
Member Author

for documentation purpose, that was how looked like the error before that fix:

Traceback (most recent call last):
  File "…/sloth.py", line 1108, in <module>
    sg.generateSet(path, setname = a.root, cutextension = a.strip)
  File "…/sloth.py", line 648, in generateSet
    self.__analyzeMaps(shader)
  File "…/sloth.py", line 432, in __analyzeMaps
    if not rgb[0] == rgb[1] == rgb[2]:
TypeError: 'int' object is not subscriptable

Copy link
Member

@Viech Viech left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I didn't pay any more attention to this since my last comment. The change looks good with the new order of operations.

@illwieckz
Copy link
Member Author

Thanks for the review. It looks like I can't merge it myself, can you do it?

@illwieckz illwieckz merged commit 79be17f into DaemonEngine:master Jun 20, 2017
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.

None yet

2 participants