osx.d 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * pixiv_down - CLI-based downloading tool for https://www.pixiv.net.
  3. * Copyright (C) 2024 Mio
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, version 3 of the License.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. */
  17. module pd.image_reader.osx;
  18. version(OSX):
  19. import std.string;
  20. public class ImageReader
  21. {
  22. this(string filename)
  23. {
  24. CFStringRef cfFilename = CFStringCreateWithCString(kCFAllocatorDefault, toStringz(filename), kCFStringEncodingUTF8);
  25. CFURLRef path = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, cfFilename, kCFURLPOSIXPathStyle, false);
  26. scope(exit) {
  27. CFRelease(cfFilename);
  28. CFRelease(path);
  29. }
  30. systemRef = CGImageSourceCreateWithURL(path, null);
  31. if (null is systemRef) {
  32. // TODO: Check errno and see if we can use strerror.
  33. throw new Exception("Unable to create image source.");
  34. }
  35. }
  36. void dispose()
  37. {
  38. if (this.disposed) {
  39. return;
  40. }
  41. if (null !is systemRef) {
  42. CFRelease(systemRef);
  43. }
  44. this.disposed = true;
  45. }
  46. CGImageSourceRef getSystemRef() {
  47. return this.systemRef;
  48. }
  49. private:
  50. CGImageSourceRef systemRef;
  51. bool disposed = false;
  52. }
  53. private:
  54. nothrow:
  55. extern(C):
  56. @nogc:
  57. alias CFTypeRef = void*;
  58. alias CFAllocatorRef = CFTypeRef;
  59. alias CFDictionaryRef = CFTypeRef;
  60. alias CFStringRef = CFTypeRef;
  61. alias CFURLRef = CFTypeRef;
  62. alias CFStringEncoding = int;
  63. alias CFURLPathStyle = int;
  64. alias CGImageSourceRef = CFTypeRef;
  65. enum kCFAllocatorDefault = null;
  66. enum : CFStringEncoding
  67. {
  68. kCFStringEncodingUTF8 = 0x08000100,
  69. }
  70. enum : CFURLPathStyle
  71. {
  72. kCFURLPOSIXPathStyle = 0,
  73. }
  74. extern __gshared CFStringRef kCGImagePropertyGIFDictionary;
  75. extern __gshared CFStringRef kCGImagePropertyGIFLoopCount;
  76. void CFRelease(void*);
  77. CFStringRef CFStringCreateWithCString(CFAllocatorRef allocator, const char *cStr, CFStringEncoding encoding);
  78. CFURLRef CFURLCreateWithFileSystemPath(CFAllocatorRef allocator, CFStringRef filePath, CFURLPathStyle pathStyle, bool isDirectory);
  79. CGImageSourceRef CGImageSourceCreateWithURL(CFURLRef url, CFDictionaryRef options);