testdlopennote.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #include <SDL3/SDL.h>
  2. #include <SDL3/SDL_main.h>
  3. #ifdef SDL_PLATFORM_WINDOWS
  4. #define PNG_SHARED_LIBRARY "libpng16-16.dll"
  5. #elif defined(SDL_PLATFORM_APPLE)
  6. #define PNG_SHARED_LIBRARY "libpng16.16.dylib"
  7. #else
  8. #define PNG_SHARED_LIBRARY "libpng16.so.16"
  9. #endif
  10. SDL_ELF_NOTE_DLOPEN(
  11. "png",
  12. "Support for loading PNG images using libpng",
  13. SDL_ELF_NOTE_DLOPEN_PRIORITY_RECOMMENDED,
  14. PNG_SHARED_LIBRARY
  15. );
  16. typedef int png_sig_cmp_fn(const unsigned char *sig, size_t start, size_t num_to_check);
  17. static struct {
  18. SDL_SharedObject *library;
  19. png_sig_cmp_fn *png_sig_cmp;
  20. } libpng16;
  21. static bool libpng_init(void)
  22. {
  23. libpng16.library = SDL_LoadObject(PNG_SHARED_LIBRARY);
  24. if (!libpng16.library) {
  25. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to load libpng library \"" PNG_SHARED_LIBRARY "\"");
  26. return false;
  27. }
  28. libpng16.png_sig_cmp = (png_sig_cmp_fn *)SDL_LoadFunction(libpng16.library, "png_sig_cmp");
  29. return libpng16.png_sig_cmp != NULL;
  30. }
  31. static void libpng_quit(void)
  32. {
  33. SDL_UnloadObject(libpng16.library);
  34. SDL_zero(libpng16);
  35. }
  36. static bool is_png(const char *path)
  37. {
  38. unsigned char header[8];
  39. size_t count;
  40. bool result;
  41. SDL_IOStream *io = SDL_IOFromFile(path, "rb");
  42. if (io == NULL) {
  43. return false;
  44. }
  45. count = SDL_ReadIO(io, header, sizeof(header));
  46. result = libpng16.png_sig_cmp(header, 0, count) == 0;
  47. SDL_CloseIO(io);
  48. return result;
  49. }
  50. int main(int argc, char *argv[])
  51. {
  52. int i;
  53. /* Enable standard application logging */
  54. SDL_SetLogPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  55. if (argc < 2) {
  56. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Usage: %s IMAGE [IMAGE [IMAGE ... ]]", argv[0]);
  57. return 1;
  58. }
  59. if (!libpng_init()) {
  60. return 1;
  61. }
  62. for (i = 1; i < argc; i++) {
  63. SDL_Log("\"%s\" is a png: %s", argv[i], is_png(argv[i]) ? "YES" : "NO");
  64. }
  65. libpng_quit();
  66. return 0;
  67. }