Skip to content

io_image read_image bugfix & add feature read_image_16 (bit) #681

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 33 additions & 5 deletions src/arraymancer/io/io_image.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -48,11 +48,39 @@ 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(width, height, channels).hwc_to_chw
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.}=

Expand Down