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

Create options/sources for JPEG/PNG support #407

Merged
merged 26 commits into from
Jan 20, 2024
Merged

Create options/sources for JPEG/PNG support #407

merged 26 commits into from
Jan 20, 2024

Conversation

luncliff
Copy link
Contributor

@luncliff luncliff commented Nov 4, 2023

Resolve #351

This provides opt-in support for using libpng and/or libjpeg. This is intended for platforms that lack WIC (i.e. WIndows Subsystem for Linux).

  • texassemble supports libPNG, libJPEG build
  • texconv supports libPNG, libJPEG build
  • texdiag supports libPNG, libJPEG build
  • Check matching between libPNG - DXGI_FORMAT
  • Check matching between libJPEG - DXGI_FORMAT
  • Create C style file I/O(FILE*) based implementation for the following functions

JPEG DirectXTexJPEG.h/cpp

  • GetMetadataFromJPEGFile
  • LoadFromJPEGFile
  • SaveToJPEGFile

PNG DirectXTexPNG.h/cpp

  • GetMetadataFromPNGFile
  • LoadFromPNGFile
  • SaveToPNGFile

References

Existing OpenEXR implementation

@luncliff luncliff marked this pull request as draft November 4, 2023 07:59
@luncliff luncliff marked this pull request as ready for review November 21, 2023 18:18
@luncliff
Copy link
Contributor Author

Still testing with my gallery images, but seems like the code can be reviewed for now...

If there is a coding convention, please let me know. I will read it and update the code.

@HENDRIX-ZT2
Copy link

It appears that the -srgb flag in texconv does not actually set the png to use srgb color space. From the code it appears that this PR does that -cool!

@JPeterMugaas
Copy link
Contributor

I have a few comments:

First, ArchLinux (https://archlinux.org/packages/extra/x86_64/libjpeg-turbo/) and the MSYS2 (https://www.msys2.org/) mingw-w64 distributions use libjpeg-turbo (https://libjpeg-turbo.virtualgl.org/) . That does NOT have the JCS_BG_RGB constant so you need to ifdef it with something such as #ifdef JCS_BG_RGB in your case statement at line 74 in DirectXTexJPEG.cpp.

Second, I get the following warning with GCC 13.2.0:

C:/msys64/home/jpmugaas/directxtex/Auxiliary/DirectXTexPNG.cpp: In function 'void DirectX::OnPNGWarning(png_structp, png_const_charp)':
C:/msys64/home/jpmugaas/directxtex/Auxiliary/DirectXTexPNG.cpp:33:5: warning: 'noreturn' function does return
   33 |     }
      |     ^

You should remove. the "[[noreturn]]" from line 30 in Auxiliary/DirectXTexPNG.cpp to fix this.

and

Third, pkg-config support was recently added so you need to add the following lines in CMakeLists at around line 390:

if(JPEG_FOUND)
  list(APPEND DIRECTXTEX_DEP_L "libjpeg")
endif()
if(PNG_FOUND)
  list(APPEND DIRECTXTEX_DEP_L "libpng")
endif()
if(ZLIB_FOUND)
  list(APPEND DIRECTXTEX_DEP_L "zlib")
endif()

This will help some users handle the direct dependencies.

@luncliff
Copy link
Contributor Author

Nice check @JPeterMugaas . You made what I need to complete this PR.

JCS_BG_RGB

JCS_BG_RGB is supported in TurboJPEG 9x

https://github.com/libjpeg-turbo/libjpeg-turbo/blob/ff9491d4e0a06542ae82367183e4548664705d80/jpeglib.h#L220-L229

But not in modern versions like 2.1.91 or 3.0.0

https://github.com/libjpeg-turbo/libjpeg-turbo/blob/6c610333497302c52ff36046f9ff72f0c3a6dc2e/jpeglib.h#L249-L274

https://github.com/libjpeg-turbo/libjpeg-turbo/blob/6c87537f60941f3c265c339fe60d1e31d2a42ccf/jpeglib.h#L249-L274

I will try detection with LIBJPEG_TURBO_VERSION_NUMBER in TurboJPEG family. Not sure it will be easy work.

@JPeterMugaas
Copy link
Contributor

JPeterMugaas commented Dec 26, 2023

I have the modern 3.0.1 version of jpeg-turbo. The code I have in Auxiliary/DirectXTexJPEG.cpp is:

#ifdef JCS_BG_RGB
            case JCS_BG_RGB:
#endif
            case JCS_RGB:

I think that is the cleanest solution to the issue with LibJPEG versions.

@JPeterMugaas
Copy link
Contributor

I have a question about ZLib as a dependency. Is ZLIB a DIRECT dependency where this library links directly to zlib or is it an INDIRECT dependency where this library links to libpng and libpng links to zlib?

I ask because we do NOT want to list INDIRECT dependencies in our pkg-config file because those can change between library versions (see: #426 ) and how the library was built (this library is a good example of that). The pkg-config program solves INDIRECT dependencies provided everybody writes their pkg-config metadata files CORRECTLY.

For the curious, https://people.freedesktop.org/~dbn/pkg-config-guide.html and its FAQ section at https://people.freedesktop.org/~dbn/pkg-config-guide.html#faq provide excellent information about the pkg-config program and associated metadata files.

@luncliff
Copy link
Contributor Author

luncliff commented Dec 27, 2023

Understood. I will check if it works in both Mozilla JPEG and IJG JPEG.
#407 (comment)

In short, I wanted to be simple for ZLIB::ZLIB.
On second thought, https://cmake.org/cmake/help/latest/module/CMakeFindDependencyMacro.html will make a more understandable call stack of CMake modules/scripts. I will try it.
#407 (comment)

@JPeterMugaas
Copy link
Contributor

I wonder if find_package(ZLIB REQUIRED) and ZLIB::ZLIB are necessary at all. You just would need to directly link to libpng with something like target_link_libraries(${PROJECT_NAME} PUBLIC PNG::PNG). libpng.pc lists zlib as a private dependency and I have seen .pc files that list libpng but not zlib.

@luncliff
Copy link
Contributor Author

luncliff commented Dec 27, 2023 via email

@JPeterMugaas
Copy link
Contributor

In the meantime, I suggest that you change CMakeLists.txt, around line 393 line to:

if(ENABLE_LIBPNG_SUPPORT AND PNG_FOUND)
  list(APPEND DIRECTXTEX_DEP_L "libpng")
endif()

@luncliff
Copy link
Contributor Author

luncliff commented Dec 27, 2023 via email

@JPeterMugaas
Copy link
Contributor

JPeterMugaas commented Dec 27, 2023

No, the CMakeLists.txt code I added was just for local testing so I didn't check it into a branch. You can add it to your own CMakeLists.txt file.

@luncliff
Copy link
Contributor Author

I see. Fixing build warnings of texdiag can be done in another PR. It's out of this PR's purpose.

@JPeterMugaas
Copy link
Contributor

My appologies for that posting mistake. Enough said.

@walbourn walbourn self-assigned this Dec 27, 2023
@walbourn walbourn added enhancement wsl Related to Windows Subsystem for Linux support labels Dec 27, 2023
@luncliff luncliff requested a review from walbourn December 28, 2023 01:59
@walbourn
Copy link
Member

walbourn commented Jan 7, 2024

Thanks for the hard work on this. I'll work though it and do some formatting changes and start working on test coverge.

@walbourn
Copy link
Member

walbourn commented Jan 7, 2024

My test media is hosted in https://github.com/walbourn/directxtexmedia. In my initial test of this code I built using the VCPKG jpeglib-turbo and libpng ports.

for %1 in (\directxtexmedia\*jpg) do texdiag info %1

and

for %1 in (\directxtexmedia\*png) do texdiag info %1

I hit a number of failures. Maybe you can take a look?

  • fishingboat.jpg and image.127839287370267572.jpg both crash in the jpeglib-turbo library
  • Glee_Newscorp_rev.jpg exists with E_FAIL
  • BASN0G16.PNG crashes

@walbourn
Copy link
Member

walbourn commented Jan 7, 2024

I've pushed code review changes back and rebased for the latest main. You should delete your working branch and repull.

@walbourn
Copy link
Member

walbourn commented Jan 7, 2024

/azp run DirectXTex-GitHub-CMake,DirectXTex-GitHub-CMake-Dev17,DirectXTex-GitHub-MinGW,DirectXTex-GitHub-CMake-Xbox,DirectXTex-GitHub-CMake-Xbox-Dev17,DirectXTex-GitHub-WSL,DirectXTex-GitHub-WSL-11

Copy link

Azure Pipelines successfully started running 3 pipeline(s).

@luncliff
Copy link
Contributor Author

luncliff commented Jan 8, 2024

#407 (comment)

Short note for the files.

  • fishingboat.jpg was JCS_GRAYSCALE. I will update the output format handling
    • image.127839287370267572.jpg same error
  • Glee_Newscorp_rev.jpg contains CMYK. I will check proper DXGI_FORMAT with WIC
  • BASN0G16.PNG fails to release memory for zlib operation

No much idea about the zlib memory issue. I'm going to debug it in Windows together

@walbourn
Copy link
Member

@walbourn
Copy link
Member

walbourn commented Jan 19, 2024

@luncliff Looks like the only failure I'm getting now is:

Glee_Newscorp_rev.jpg

If you don't want to support CMYK that's fine, but it would ideally return HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED) instead of E_FAIL.

Copy link
Member

@walbourn walbourn left a comment

Choose a reason for hiding this comment

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

Looks great. Thanks for the contribution.

What do you want to do about CMYK? Leave it as is?

@walbourn
Copy link
Member

/azp run DirectXTex-GitHub-CMake,DirectXTex-GitHub-CMake-Dev17,DirectXTex-GitHub-MinGW,DirectXTex-GitHub-CMake-Xbox,DirectXTex-GitHub-CMake-Xbox-Dev17,DirectXTex-GitHub-WSL,DirectXTex-GitHub-WSL-11

Copy link

Azure Pipelines successfully started running 3 pipeline(s).

@walbourn
Copy link
Member

/azp run DirectXTex-GitHub-CMake,DirectXTex-GitHub-CMake-Dev17,DirectXTex-GitHub-MinGW,DirectXTex-GitHub-CMake-Xbox,DirectXTex-GitHub-CMake-Xbox-Dev17,DirectXTex-GitHub-WSL,DirectXTex-GitHub-WSL-11

Copy link

Azure Pipelines successfully started running 3 pipeline(s).

@luncliff
Copy link
Contributor Author

If you don't want to support CMYK that's fine, but it would ideally return HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED) instead of E_FAIL.

+1 for HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED). I will update now

@walbourn
Copy link
Member

/azp run DirectXTex-GitHub-CMake,DirectXTex-GitHub-CMake-Dev17,DirectXTex-GitHub-MinGW,DirectXTex-GitHub-CMake-Xbox,DirectXTex-GitHub-CMake-Xbox-Dev17,DirectXTex-GitHub-WSL,DirectXTex-GitHub-WSL-11

Copy link

Azure Pipelines successfully started running 3 pipeline(s).

Copy link
Member

@walbourn walbourn left a comment

Choose a reason for hiding this comment

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

Thanks for all the work!

@walbourn walbourn merged commit fae11ce into microsoft:main Jan 20, 2024
6 checks passed
@luncliff luncliff deleted the support/jpeg-png branch January 21, 2024 01:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement wsl Related to Windows Subsystem for Linux support
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement PNG and JPEG file I/O for Linux
4 participants