Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

145 lines
4.9KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For this technical preview, this file is not subject to commercial licensing.
  8. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  9. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  10. DISCLAIMED.
  11. ==============================================================================
  12. */
  13. namespace juce
  14. {
  15. extern Image JUCE_API getIconFromApplication (const String&, int);
  16. static Image getIconFromIcnsFile (const File& icnsFile, const int size)
  17. {
  18. FileInputStream stream (icnsFile);
  19. if (! stream.openedOk())
  20. return {};
  21. const int numHeaderSectionBytes = 4;
  22. char headerSection [numHeaderSectionBytes];
  23. if (stream.read (headerSection, numHeaderSectionBytes) != numHeaderSectionBytes
  24. || headerSection[0] != 'i'
  25. || headerSection[1] != 'c'
  26. || headerSection[2] != 'n'
  27. || headerSection[3] != 's')
  28. return {};
  29. if (stream.read (headerSection, numHeaderSectionBytes) != numHeaderSectionBytes)
  30. return {};
  31. const auto dataSize = juce::ByteOrder::bigEndianInt (headerSection);
  32. if (dataSize <= 0)
  33. return {};
  34. OwnedArray<juce::ImageFileFormat> internalFormats;
  35. internalFormats.add (new PNGImageFormat());
  36. internalFormats.add (new JPEGImageFormat());
  37. Array<Image> images;
  38. auto maxWidth = 0;
  39. auto maxWidthIndex = -1;
  40. while (stream.getPosition() < dataSize)
  41. {
  42. const auto sectionStart = stream.getPosition();
  43. if (! stream.setPosition (sectionStart + 4))
  44. break;
  45. if (stream.read (headerSection, numHeaderSectionBytes) != numHeaderSectionBytes)
  46. break;
  47. const auto sectionSize = ByteOrder::bigEndianInt (headerSection);
  48. if (sectionSize <= 0)
  49. break;
  50. const auto sectionDataStart = stream.getPosition();
  51. for (auto* fmt : internalFormats)
  52. {
  53. if (fmt->canUnderstand (stream))
  54. {
  55. stream.setPosition (sectionDataStart);
  56. images.add (fmt->decodeImage (stream));
  57. const auto lastImageIndex = images.size() - 1;
  58. const auto lastWidth = images.getReference (lastImageIndex).getWidth();
  59. if (lastWidth > maxWidth)
  60. {
  61. maxWidthIndex = lastImageIndex;
  62. maxWidth = lastWidth;
  63. }
  64. }
  65. stream.setPosition (sectionDataStart);
  66. }
  67. stream.setPosition (sectionStart + sectionSize);
  68. }
  69. return maxWidthIndex == -1 ? juce::Image()
  70. : images.getReference (maxWidthIndex).rescaled (size, size, Graphics::ResamplingQuality::highResamplingQuality);
  71. }
  72. Image JUCE_API getIconFromApplication (const String& applicationPath, const int size)
  73. {
  74. Image hostIcon;
  75. if (CFStringRef pathCFString = CFStringCreateWithCString (kCFAllocatorDefault, applicationPath.toRawUTF8(), kCFStringEncodingUTF8))
  76. {
  77. if (CFURLRef url = CFURLCreateWithFileSystemPath (kCFAllocatorDefault, pathCFString, kCFURLPOSIXPathStyle, 1))
  78. {
  79. if (CFBundleRef appBundle = CFBundleCreate (kCFAllocatorDefault, url))
  80. {
  81. if (CFTypeRef infoValue = CFBundleGetValueForInfoDictionaryKey (appBundle, CFSTR("CFBundleIconFile")))
  82. {
  83. if (CFGetTypeID (infoValue) == CFStringGetTypeID())
  84. {
  85. CFStringRef iconFilename = reinterpret_cast<CFStringRef> (infoValue);
  86. CFStringRef resourceURLSuffix = CFStringHasSuffix (iconFilename, CFSTR(".icns")) ? nullptr : CFSTR("icns");
  87. if (CFURLRef iconURL = CFBundleCopyResourceURL (appBundle, iconFilename, resourceURLSuffix, nullptr))
  88. {
  89. if (CFStringRef iconPath = CFURLCopyFileSystemPath (iconURL, kCFURLPOSIXPathStyle))
  90. {
  91. File icnsFile (CFStringGetCStringPtr (iconPath, CFStringGetSystemEncoding()));
  92. hostIcon = getIconFromIcnsFile (icnsFile, size);
  93. CFRelease (iconPath);
  94. }
  95. CFRelease (iconURL);
  96. }
  97. }
  98. }
  99. CFRelease (appBundle);
  100. }
  101. CFRelease (url);
  102. }
  103. CFRelease (pathCFString);
  104. }
  105. return hostIcon;
  106. }
  107. } // namespace juce