From 56bbdefef179c28ee20bd651c2fd5e1208cefb94 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nil=20=E2=98=BF?= Date: Tue, 8 Apr 2025 15:26:53 +0200 Subject: [PATCH 1/3] Fix: bugfix in reshape Fixed a bug in the read_image proc that swapped height and width --- src/arraymancer/io/io_image.nim | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/arraymancer/io/io_image.nim b/src/arraymancer/io/io_image.nim index 7c0479b1b..5dc9d78d7 100644 --- a/src/arraymancer/io/io_image.nim +++ b/src/arraymancer/io/io_image.nim @@ -51,8 +51,7 @@ proc read_image*(buffer: seq[byte]): Tensor[uint8] = let desired_channels = 0 # Channel autodetection let raw_pixels = loadFromMemory(buffer, width, height, channels, desired_channels) - result = raw_pixels.toTensor.reshape(width, height, channels).hwc_to_chw - + result = raw_pixels.toTensor.reshape(height, width, channels).hwc_to_chw template gen_write_image(proc_name: untyped): untyped {.dirty.}= From fe2a20df8d1d2c70554453fc897576dfb22f3378 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nil=20=E2=98=BF?= Date: Tue, 8 Apr 2025 15:35:53 +0200 Subject: [PATCH 2/3] Refactor: default enum & docs Replaced int with apposite stb_image enum & improved proc doc --- src/arraymancer/io/io_image.nim | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/arraymancer/io/io_image.nim b/src/arraymancer/io/io_image.nim index 5dc9d78d7..d047a105e 100644 --- a/src/arraymancer/io/io_image.nim +++ b/src/arraymancer/io/io_image.nim @@ -18,7 +18,7 @@ func chw_to_hwc[T](img: Tensor[T]): Tensor[T] {.inline.}= img.permute(1, 2, 0) proc read_image*(filepath: string): Tensor[uint8] = - ## Read an image file and loads it into a Tensor[uint8] of shape + ## Read an 8-bit image file and loads it into a Tensor[uint8] of shape ## Channel x Height x Width. Channel is 1 for greyscale, 3 for RGB. ## ## Supports JPEG, PNG, TGA, BMP, PSD, GIF, HDR, PIC, PNM @@ -31,13 +31,13 @@ proc read_image*(filepath: string): Tensor[uint8] = ## x.float32 / 255.0 var width, height, channels: int - let desired_channels = 0 # Channel autodetection + let desired_channels = Default # Channel autodetection let raw_pixels = load(filepath, width, height, channels, desired_channels) result = raw_pixels.toTensor.reshape(height, width, channels).hwc_to_chw proc read_image*(buffer: seq[byte]): Tensor[uint8] = - ## Read an image from a buffer and loads it into a Tensor[uint8] of shape + ## Read an 8-bit image from a buffer and loads it into a Tensor[uint8] of shape ## Channel x Height x Width. Channel is 1 for greyscale, 3 for RGB. ## ## Supports JPEG, PNG, TGA, BMP, PSD, GIF, HDR, PIC, PNM @@ -48,7 +48,7 @@ proc read_image*(buffer: seq[byte]): Tensor[uint8] = # but nim-stb_image only accept seq[bytes] (and convert it to pointer + length internally) var width, height, channels: int - let desired_channels = 0 # Channel autodetection + let desired_channels = Default # Channel autodetection let raw_pixels = loadFromMemory(buffer, width, height, channels, desired_channels) result = raw_pixels.toTensor.reshape(height, width, channels).hwc_to_chw From 9722f42610b6186600e9c0f122f07e874b7978af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nil=20=E2=98=BF?= Date: Tue, 8 Apr 2025 15:43:27 +0200 Subject: [PATCH 3/3] Feat: add read_image proc for 16-bit Added read_image_16 proc for reading 16-bit images from a file or buffer --- src/arraymancer/io/io_image.nim | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/arraymancer/io/io_image.nim b/src/arraymancer/io/io_image.nim index d047a105e..0dfd2dc08 100644 --- a/src/arraymancer/io/io_image.nim +++ b/src/arraymancer/io/io_image.nim @@ -53,6 +53,35 @@ proc read_image*(buffer: seq[byte]): Tensor[uint8] = let raw_pixels = loadFromMemory(buffer, width, height, channels, desired_channels) result = raw_pixels.toTensor.reshape(height, width, channels).hwc_to_chw +proc read_image_16*(filepath: string): Tensor[uint16] = + ## Read a 16-bit image file and loads it into a Tensor[uint16] of shape + ## Channel x Height x Width. Channel is 1 for greyscale, 3 for RGB. + ## + ## Supports JPEG, PNG, TGA, BMP, PSD, GIF, HDR, PIC, PNM + ## See stb_image https://github.com/nothings/stb/blob/master/stb_image.h + var width, height, channels: int + let desired_channels = Default # Channel autodetection + + let raw_pixels = load16(filepath, width, height, channels, desired_channels) + result = raw_pixels.toTensor.reshape(height, width, channels).hwc_to_chw + +proc read_image_16*(buffer: seq[byte]): Tensor[uint16] = + ## Read a 16-bit image from a buffer and loads it into a Tensor[uint16] of shape + ## Channel x Height x Width. Channel is 1 for greyscale, 3 for RGB. + ## + ## Supports JPEG, PNG, TGA, BMP, PSD, GIF, HDR, PIC, PNM + ## See stb_image https://github.com/nothings/stb/blob/master/stb_image.h + ## + + # TODO: ideally this should also accept pointer + length + # but nim-stb_image only accept seq[bytes] (and convert it to pointer + length internally) + + var width, height, channels: int + let desired_channels = Default # Channel autodetection + + let raw_pixels = load16FromMemory(buffer, width, height, channels, desired_channels) + result = raw_pixels.toTensor.reshape(height, width, channels).hwc_to_chw + template gen_write_image(proc_name: untyped): untyped {.dirty.}= proc proc_name*(img: Tensor[uint8], filepath: string) =