|
| 1 | +using Microsoft.ML.OnnxRuntime; |
| 2 | +using Microsoft.ML.OnnxRuntime.Tensors; |
| 3 | +using OnnxStack.Core; |
| 4 | +using SixLabors.ImageSharp; |
| 5 | +using SixLabors.ImageSharp.PixelFormats; |
| 6 | +using System; |
| 7 | + |
| 8 | +namespace OnnxStack.ImageUpscaler.Extensions |
| 9 | +{ |
| 10 | + internal static class ImageExtensions |
| 11 | + { |
| 12 | + |
| 13 | + /// <summary> |
| 14 | + /// Converts to DenseTensor. |
| 15 | + /// </summary> |
| 16 | + /// <param name="image">The image.</param> |
| 17 | + /// <param name="dimensions">The dimensions.</param> |
| 18 | + /// <returns></returns> |
| 19 | + public static DenseTensor<float> ToDenseTensor(this Image<Rgba32> image, ReadOnlySpan<int> dimensions) |
| 20 | + { |
| 21 | + using (image) |
| 22 | + { |
| 23 | + return ProcessPixels(image, dimensions); |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + |
| 28 | + /// <summary> |
| 29 | + /// Converts to Image. |
| 30 | + /// </summary> |
| 31 | + /// <param name="ortValue">The ort value.</param> |
| 32 | + /// <returns></returns> |
| 33 | + public static Image<Rgba32> ToImage(this OrtValue ortValue) |
| 34 | + { |
| 35 | + return ortValue.ToDenseTensor().ToImage(); |
| 36 | + } |
| 37 | + |
| 38 | + |
| 39 | + /// <summary> |
| 40 | + /// Converts to image. |
| 41 | + /// </summary> |
| 42 | + /// <param name="imageTensor">The image tensor.</param> |
| 43 | + /// <returns></returns> |
| 44 | + public static Image<Rgba32> ToImage(this DenseTensor<float> imageTensor) |
| 45 | + { |
| 46 | + var height = imageTensor.Dimensions[2]; |
| 47 | + var width = imageTensor.Dimensions[3]; |
| 48 | + var result = new Image<Rgba32>(width, height); |
| 49 | + for (var y = 0; y < height; y++) |
| 50 | + { |
| 51 | + for (var x = 0; x < width; x++) |
| 52 | + { |
| 53 | + result[x, y] = new Rgba32( |
| 54 | + CalculateByte(imageTensor, 0, y, x), |
| 55 | + CalculateByte(imageTensor, 1, y, x), |
| 56 | + CalculateByte(imageTensor, 2, y, x) |
| 57 | + ); |
| 58 | + } |
| 59 | + } |
| 60 | + return result; |
| 61 | + } |
| 62 | + |
| 63 | + |
| 64 | + /// <summary> |
| 65 | + /// Processes the pixels. |
| 66 | + /// </summary> |
| 67 | + /// <param name="image">The image.</param> |
| 68 | + /// <param name="dimensions">The dimensions.</param> |
| 69 | + /// <returns></returns> |
| 70 | + private static DenseTensor<float> ProcessPixels(Image<Rgba32> image, ReadOnlySpan<int> dimensions) |
| 71 | + { |
| 72 | + var width = dimensions[3]; |
| 73 | + var height = dimensions[2]; |
| 74 | + var channels = dimensions[1]; |
| 75 | + var imageArray = new DenseTensor<float>(new[] { 1, channels, height, width }); |
| 76 | + image.ProcessPixelRows(img => |
| 77 | + { |
| 78 | + for (int x = 0; x < width; x++) |
| 79 | + { |
| 80 | + for (int y = 0; y < height; y++) |
| 81 | + { |
| 82 | + var pixelSpan = img.GetRowSpan(y); |
| 83 | + imageArray[0, 0, y, x] = (pixelSpan[x].R / 255.0f); |
| 84 | + imageArray[0, 1, y, x] = (pixelSpan[x].G / 255.0f); |
| 85 | + imageArray[0, 2, y, x] = (pixelSpan[x].B / 255.0f); |
| 86 | + } |
| 87 | + } |
| 88 | + }); |
| 89 | + return imageArray; |
| 90 | + } |
| 91 | + |
| 92 | + |
| 93 | + /// <summary> |
| 94 | + /// Calculates the byte. |
| 95 | + /// </summary> |
| 96 | + /// <param name="imageTensor">The image tensor.</param> |
| 97 | + /// <param name="index">The index.</param> |
| 98 | + /// <param name="y">The y.</param> |
| 99 | + /// <param name="x">The x.</param> |
| 100 | + /// <returns></returns> |
| 101 | + private static byte CalculateByte(Tensor<float> imageTensor, int index, int y, int x) |
| 102 | + { |
| 103 | + return (byte)Math.Round(Math.Clamp(imageTensor[0, index, y, x], 0, 1) * 255); |
| 104 | + } |
| 105 | + } |
| 106 | +} |
0 commit comments