|
| 1 | +using System.Runtime.InteropServices; |
| 2 | +using api.Database.Models; |
| 3 | +using BitMiracle.LibTiff.Classic; |
| 4 | + |
| 5 | +namespace api.Services; |
| 6 | + |
| 7 | +public record ThermalImageData( |
| 8 | + byte[] FloatBytes, |
| 9 | + int Width, |
| 10 | + int Height, |
| 11 | + float MinTemperature, |
| 12 | + float MaxTemperature |
| 13 | +); |
| 14 | + |
| 15 | +public interface IThermalImageService |
| 16 | +{ |
| 17 | + Task<ThermalImageData> GetThermalImageDataAsync(BlobStorageLocation location); |
| 18 | +} |
| 19 | + |
| 20 | +public class ThermalImageService(IBlobStorageService blobStorageService) : IThermalImageService |
| 21 | +{ |
| 22 | + public async Task<ThermalImageData> GetThermalImageDataAsync(BlobStorageLocation location) |
| 23 | + { |
| 24 | + using var tiffStream = await blobStorageService.DownloadBlobAsync(location); |
| 25 | + |
| 26 | + var (temperatures, width, height) = ReadTiffTemperatures(tiffStream); |
| 27 | + |
| 28 | + float minTemp = float.MaxValue; |
| 29 | + float maxTemp = float.MinValue; |
| 30 | + for (int i = 0; i < temperatures.Length; i++) |
| 31 | + { |
| 32 | + float temp = temperatures[i]; |
| 33 | + if (temp < minTemp) |
| 34 | + minTemp = temp; |
| 35 | + if (temp > maxTemp) |
| 36 | + maxTemp = temp; |
| 37 | + } |
| 38 | + |
| 39 | + // Serialize float[] to little-endian bytes. |
| 40 | + var floatBytes = new byte[temperatures.Length * sizeof(float)]; |
| 41 | + MemoryMarshal.AsBytes(temperatures.AsSpan()).CopyTo(floatBytes); |
| 42 | + |
| 43 | + return new ThermalImageData(floatBytes, width, height, minTemp, maxTemp); |
| 44 | + } |
| 45 | + |
| 46 | + /// <summary> |
| 47 | + /// Read pixel data from a TIFF stream as float temperatures. |
| 48 | + /// Supports 32-bit float, 64-bit float, 16-bit unsigned, and 8-bit unsigned sample formats. |
| 49 | + /// </summary> |
| 50 | + private static (float[] temperatures, int width, int height) ReadTiffTemperatures( |
| 51 | + Stream tiffStream |
| 52 | + ) |
| 53 | + { |
| 54 | + using var tiff = Tiff.ClientOpen("thermal", "r", tiffStream, new TiffStream()); |
| 55 | + if (tiff == null) |
| 56 | + throw new InvalidOperationException("Failed to open TIFF stream."); |
| 57 | + |
| 58 | + int width = tiff.GetField(TiffTag.IMAGEWIDTH)[0].ToInt(); |
| 59 | + int height = tiff.GetField(TiffTag.IMAGELENGTH)[0].ToInt(); |
| 60 | + |
| 61 | + int bitsPerSample = 32; |
| 62 | + var bpsField = tiff.GetField(TiffTag.BITSPERSAMPLE); |
| 63 | + if (bpsField != null) |
| 64 | + bitsPerSample = bpsField[0].ToInt(); |
| 65 | + |
| 66 | + int sampleFormat = (int)SampleFormat.UINT; |
| 67 | + var sfField = tiff.GetField(TiffTag.SAMPLEFORMAT); |
| 68 | + if (sfField != null) |
| 69 | + sampleFormat = sfField[0].ToInt(); |
| 70 | + |
| 71 | + var temperatures = new float[width * height]; |
| 72 | + int scanlineSize = tiff.ScanlineSize(); |
| 73 | + byte[] buffer = new byte[scanlineSize]; |
| 74 | + |
| 75 | + for (int y = 0; y < height; y++) |
| 76 | + { |
| 77 | + tiff.ReadScanline(buffer, y); |
| 78 | + |
| 79 | + for (int x = 0; x < width; x++) |
| 80 | + { |
| 81 | + float value = (bitsPerSample, sampleFormat) switch |
| 82 | + { |
| 83 | + (32, (int)SampleFormat.IEEEFP) => BitConverter.ToSingle(buffer, x * 4), |
| 84 | + (64, (int)SampleFormat.IEEEFP) => (float)BitConverter.ToDouble(buffer, x * 8), |
| 85 | + (16, (int)SampleFormat.UINT or (int)SampleFormat.VOID) => BitConverter.ToUInt16( |
| 86 | + buffer, |
| 87 | + x * 2 |
| 88 | + ), |
| 89 | + (16, (int)SampleFormat.INT) => BitConverter.ToInt16(buffer, x * 2), |
| 90 | + (8, _) => buffer[x], |
| 91 | + _ => throw new NotSupportedException( |
| 92 | + $"Unsupported TIFF pixel format: {bitsPerSample} bits/sample, sample format {sampleFormat}." |
| 93 | + ), |
| 94 | + }; |
| 95 | + |
| 96 | + temperatures[y * width + x] = value; |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + return (temperatures, width, height); |
| 101 | + } |
| 102 | +} |
0 commit comments