|
| 1 | +// Copyright (c) Files Community |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +using System.Runtime.InteropServices; |
| 5 | +using Windows.Win32; |
| 6 | +using Windows.Win32.Foundation; |
| 7 | +using Windows.Win32.Graphics.Gdi; |
| 8 | +using Windows.Win32.Graphics.GdiPlus; |
| 9 | +using Windows.Win32.System.Com; |
| 10 | +using Windows.Win32.UI.Shell; |
| 11 | + |
| 12 | +namespace Files.App.Storage.Storables |
| 13 | +{ |
| 14 | + public static partial class WindowsStorableHelpers |
| 15 | + { |
| 16 | + private static (Guid Format, Guid Encorder)[]? GdiEncoders; |
| 17 | + |
| 18 | + /// <inheritdoc cref="GetThumbnail"/> |
| 19 | + public static async Task<byte[]> GetThumbnailAsync(this IWindowsStorable storable, int size, SIIGBF options) |
| 20 | + { |
| 21 | + return await STATask.Run(() => storable.GetThumbnail(size, options)); |
| 22 | + } |
| 23 | + |
| 24 | + /// <summary> |
| 25 | + /// Retrieves a thumbnail image data for the specified <paramref name="storable"/> using <see cref="IShellItemImageFactory"/>. |
| 26 | + /// </summary> |
| 27 | + /// <param name="storable">An object that implements <see cref="IWindowsStorable"/> and represents a shell item on Windows.</param> |
| 28 | + /// <param name="size">The desired size (in pixels) of the thumbnail (width and height are equal).</param> |
| 29 | + /// <param name="options">A combination of <see cref="SIIGBF"/> flags that specify how the thumbnail should be retrieved.</param> |
| 30 | + /// <returns>A byte array containing the thumbnail image in its native format (e.g., PNG, JPEG).</returns> |
| 31 | + /// <remarks>If the thumbnail is JPEG, this tries to decoded as a PNG instead because JPEG loses data.</remarks> |
| 32 | + public unsafe static byte[] GetThumbnail(this IWindowsStorable storable, int size, SIIGBF options) |
| 33 | + { |
| 34 | + using ComPtr<IShellItemImageFactory> pShellItemImageFactory = storable.ThisPtr.As<IShellItemImageFactory>(); |
| 35 | + if (pShellItemImageFactory.IsNull) |
| 36 | + return []; |
| 37 | + |
| 38 | + // Get HBITMAP |
| 39 | + HBITMAP hBitmap = default; |
| 40 | + HRESULT hr = pShellItemImageFactory.Get()->GetImage(new(size, size), options, &hBitmap); |
| 41 | + if (hr.ThrowIfFailedOnDebug().Failed) |
| 42 | + { |
| 43 | + if (!hBitmap.IsNull) PInvoke.DeleteObject(hBitmap); |
| 44 | + return []; |
| 45 | + } |
| 46 | + |
| 47 | + // Convert to GpBitmap of GDI+ |
| 48 | + GpBitmap* gpBitmap = default; |
| 49 | + PInvoke.GdipCreateBitmapFromHBITMAP(hBitmap, HPALETTE.Null, &gpBitmap); |
| 50 | + |
| 51 | + // Get an encoder for PNG |
| 52 | + Guid format = Guid.Empty; |
| 53 | + if (PInvoke.GdipGetImageRawFormat((GpImage*)gpBitmap, &format) is not Status.Ok) |
| 54 | + { |
| 55 | + if (gpBitmap is not null) PInvoke.GdipDisposeImage((GpImage*)gpBitmap); |
| 56 | + if (!hBitmap.IsNull) PInvoke.DeleteObject(hBitmap); |
| 57 | + return []; |
| 58 | + } |
| 59 | + |
| 60 | + Guid encoder = GetEncoderClsid(format); |
| 61 | + if (format == PInvoke.ImageFormatJPEG || encoder == Guid.Empty) |
| 62 | + { |
| 63 | + format = PInvoke.ImageFormatPNG; |
| 64 | + encoder = GetEncoderClsid(format); |
| 65 | + } |
| 66 | + |
| 67 | + using ComPtr<IStream> pStream = default; |
| 68 | + hr = PInvoke.CreateStreamOnHGlobal(HGLOBAL.Null, true, pStream.GetAddressOf()); |
| 69 | + if (hr.ThrowIfFailedOnDebug().Failed) |
| 70 | + { |
| 71 | + if (gpBitmap is not null) PInvoke.GdipDisposeImage((GpImage*)gpBitmap); |
| 72 | + if (!hBitmap.IsNull) PInvoke.DeleteObject(hBitmap); |
| 73 | + return []; |
| 74 | + } |
| 75 | + |
| 76 | + if (PInvoke.GdipSaveImageToStream((GpImage*)gpBitmap, pStream.Get(), &encoder, (EncoderParameters*)null) is not Status.Ok) |
| 77 | + { |
| 78 | + if (gpBitmap is not null) PInvoke.GdipDisposeImage((GpImage*)gpBitmap); |
| 79 | + if (!hBitmap.IsNull) PInvoke.DeleteObject(hBitmap); |
| 80 | + return []; |
| 81 | + } |
| 82 | + |
| 83 | + STATSTG stat = default; |
| 84 | + hr = pStream.Get()->Stat(&stat, (uint)STATFLAG.STATFLAG_NONAME); |
| 85 | + if (hr.ThrowIfFailedOnDebug().Failed) |
| 86 | + { |
| 87 | + if (gpBitmap is not null) PInvoke.GdipDisposeImage((GpImage*)gpBitmap); |
| 88 | + if (!hBitmap.IsNull) PInvoke.DeleteObject(hBitmap); |
| 89 | + return []; |
| 90 | + } |
| 91 | + |
| 92 | + ulong statSize = stat.cbSize & 0xFFFFFFFF; |
| 93 | + byte* RawThumbnailData = (byte*)NativeMemory.Alloc((nuint)statSize); |
| 94 | + |
| 95 | + pStream.Get()->Seek(0L, (SystemIO.SeekOrigin)STREAM_SEEK.STREAM_SEEK_SET, null); |
| 96 | + hr = pStream.Get()->Read(RawThumbnailData, (uint)statSize); |
| 97 | + if (hr.ThrowIfFailedOnDebug().Failed) |
| 98 | + { |
| 99 | + if (gpBitmap is not null) PInvoke.GdipDisposeImage((GpImage*)gpBitmap); |
| 100 | + if (!hBitmap.IsNull) PInvoke.DeleteObject(hBitmap); |
| 101 | + if (RawThumbnailData is not null) NativeMemory.Free(RawThumbnailData); |
| 102 | + return []; |
| 103 | + } |
| 104 | + |
| 105 | + byte[] thumbnailData = new ReadOnlySpan<byte>(RawThumbnailData, (int)statSize / sizeof(byte)).ToArray(); |
| 106 | + NativeMemory.Free(RawThumbnailData); |
| 107 | + |
| 108 | + return thumbnailData; |
| 109 | + |
| 110 | + Guid GetEncoderClsid(Guid format) |
| 111 | + { |
| 112 | + foreach ((Guid Format, Guid Encoder) in GetGdiEncoders()) |
| 113 | + if (Format == format) |
| 114 | + return Encoder; |
| 115 | + |
| 116 | + return Guid.Empty; |
| 117 | + } |
| 118 | + |
| 119 | + (Guid Format, Guid Encorder)[] GetGdiEncoders() |
| 120 | + { |
| 121 | + if (GdiEncoders is not null) |
| 122 | + return GdiEncoders; |
| 123 | + |
| 124 | + if (PInvoke.GdipGetImageEncodersSize(out var numEncoders, out var size) is not Status.Ok) |
| 125 | + return []; |
| 126 | + |
| 127 | + ImageCodecInfo* pImageCodecInfo = (ImageCodecInfo*)NativeMemory.Alloc(size); |
| 128 | + |
| 129 | + if (PInvoke.GdipGetImageEncoders(numEncoders, size, pImageCodecInfo) is not Status.Ok) |
| 130 | + return []; |
| 131 | + |
| 132 | + ReadOnlySpan<ImageCodecInfo> codecs = new(pImageCodecInfo, (int)numEncoders); |
| 133 | + GdiEncoders = new (Guid Format, Guid Encoder)[codecs.Length]; |
| 134 | + for (int index = 0; index < codecs.Length; index++) |
| 135 | + GdiEncoders[index] = (codecs[index].FormatID, codecs[index].Clsid); |
| 136 | + |
| 137 | + return GdiEncoders; |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + /// <inheritdoc cref="GetThumbnail"/> |
| 142 | + public static async Task<byte[]> GetThumbnailOverlayAsync(this IWindowsStorable storable) |
| 143 | + { |
| 144 | + return await STATask.Run(() => storable.GetThumbnailOverlay()); |
| 145 | + } |
| 146 | + |
| 147 | + /// <summary> |
| 148 | + /// Retrieves a thumbnail overlay image data for the specified <paramref name="storable"/> using <see cref="IShellIconOverlayManager"/>. |
| 149 | + /// </summary> |
| 150 | + /// <param name="storable">An object that implements <see cref="IWindowsStorable"/> and represents a shell item on Windows.</param> |
| 151 | + /// <returns>A byte array containing the thumbnail overlay image in its native format (e.g., PNG, JPEG).</returns> |
| 152 | + public unsafe static byte[] GetThumbnailOverlay(this IWindowsStorable storable) |
| 153 | + { |
| 154 | + return []; |
| 155 | + } |
| 156 | + } |
| 157 | +} |
0 commit comments