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

2474: Fix FreeImage texture loader bugs, add test of image contents #2475

Merged
merged 8 commits into from
Dec 7, 2018

Conversation

ericwa
Copy link
Collaborator

@ericwa ericwa commented Dec 1, 2018

  • Fixes a bug loading the image from Custom textures sometimes render oddly  #2474 (I think it was due to not calling FreeImage_ConvertTo24Bits() on 32-bit input images, but the std::memcpy() was also wrong.)
  • Don't crash on invalid png inputs
  • Support transparency (rendeded as alpha masked)
  • Tests for the above

Fixes #2474

The main one was not calling FreeImage_ConvertTo24Bits() on 32-bit
input images, but the std::memcpy() was also wrong.

Fixes #2474
@ghost ghost assigned ericwa Dec 1, 2018
@ghost ghost added the Status:In Progress label Dec 1, 2018
@ericwa ericwa requested a review from kduske December 1, 2018 20:26
Copy link
Collaborator

@kduske kduske left a comment

Choose a reason for hiding this comment

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

Please add some documentation or extract functions, otherwise looks good.

mipImage = image;
}

const auto bytesPerPixel = FreeImage_GetLine(mipImage) / FreeImage_GetWidth(mipImage);
Copy link
Collaborator

Choose a reason for hiding this comment

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

What is going on here? Please document, or even better, extract a function?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The bytesPerPixel calculation is from the FreeImage documentation, I'll add a comment and reference the part of the docs I'm following here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

or do you mean these for loops below in general? Yeah, I can extract this into a function. It's copying out the contents of the FIBITMAP as a sequence of bytes ordered RGBA, with no padding between the rows. (Internally in the FBITMAP they're stored in an order that is given by the FI_RGBA_* macros, it might not be RGBA, and there is padding between the rows so you can't just memcpy the whole image).

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ah, ok. But also I'm wondering why we create mipmaps here when we already have code in Texture that will instruct OpenGL to generate them. Is it not better to leave it to GL?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah, we could just leave it to OpenGL to create the mipmaps, that would remove the outer loop. This code is also slower because it scales each mipmap from the full size image, instead of scaling it from the next larger mipmap which is what you should do, afaik.

Is it possible to change that without some refactoring to all of the texture loaders? Right now it looks like you pass in the number of mip levels you want to the constructor FreeImageTextureReader::FreeImageTextureReader. Maybe we should leave that for later if it requires refactoring all the loaders?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We could use that but from my reading of docs/bugs, it doesn't change the byte order, so you still have to check the FI_RGBA_* macros.
https://sourceforge.net/p/freeimage/bugs/172/

Looking at FreeImage.h and FI_RGBA_RED etc., the byte order is either BGRA or RGBA so we could just check for the order and map it to the correct OpenGL enum.

Copy link
Collaborator

Choose a reason for hiding this comment

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

FreeImageTextureReader is constructed in TextureLoader::createTextureReader with a default of 4 mip levels. I think that's fine?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Mapping the image format to the correct GL enum seems like the way to go. Less code means less bugs, and also faster texture loading.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Agreed, updating it now. Also I see how to skip generating the mips in FreeImageTextureReader.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Oh, right, there's more code to generate mips in there. I think that can just be removed. We should leave it to OpenGL to generate them, it'll probably be faster and more correct that way.

@ericwa ericwa merged commit f854bf7 into master Dec 7, 2018
@ghost ghost removed the Status:In Progress label Dec 7, 2018
@ericwa ericwa deleted the feature/2474 branch December 7, 2018 02:34
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