Skip to content

Commit

Permalink
supporting 16bits images
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin Lefaudeux committed Dec 17, 2024
1 parent e4ff54a commit cde9633
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
1 change: 1 addition & 0 deletions pkg/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type ImagePayload struct {
Height int // Useful to decode the current payload
Width int
Channels int
BitDepth int
}

type Sample struct {
Expand Down
15 changes: 14 additions & 1 deletion pkg/serdes.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ func imageFromBuffer(buffer []byte, transform *ARAwareTransform, aspect_ratio fl
// If requested, re-encode the image to a jpg or png
var img_bytes []byte
var channels int
bit_depth := int(-1)

Check failure on line 85 in pkg/serdes.go

View workflow job for this annotation

GitHub Actions / build_and_test

ineffectual assignment to bit_depth (ineffassign)

if pre_encode_image {
if err != nil {
return nil, -1., err
Expand All @@ -93,12 +95,15 @@ func imageFromBuffer(buffer []byte, transform *ARAwareTransform, aspect_ratio fl
if err != nil {
return nil, -1., err
}
bit_depth = 8
} else {
// Re-encode the image to a png
img_bytes, _, err = img.ExportPng(vips.NewPngExportParams())
png_export_params := vips.NewPngExportParams()
img_bytes, _, err = img.ExportPng(png_export_params)
if err != nil {
return nil, -1., err
}
bit_depth = png_export_params.Bitdepth
}
channels = -1 // Signal that we have encoded the image
} else {
Expand All @@ -107,6 +112,13 @@ func imageFromBuffer(buffer []byte, transform *ARAwareTransform, aspect_ratio fl
return nil, -1., err
}
channels = img.Bands()

// Define bit depth de facto, not exposed in the vips interface
bit_depth = len(img_bytes) / (width * height * channels)
}

if bit_depth == -1 {
panic("Bit depth not set")
}

img_payload := ImagePayload{
Expand All @@ -116,6 +128,7 @@ func imageFromBuffer(buffer []byte, transform *ARAwareTransform, aspect_ratio fl
Height: height,
Width: width,
Channels: channels,
BitDepth: bit_depth,
}

return &img_payload, aspect_ratio, nil
Expand Down
4 changes: 3 additions & 1 deletion python/go_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ def uint8_array_to_numpy(go_array):
# We export them from Go with a Channels dimension of -1 to mark them as dimensionless.
# Anything else is a valid number of channels and will thus lead to a reshape
num_final_channels = max(go_array.Channels, 1)
bit_depth = getattr(go_array, "BitDepth", 8)

length = (
go_array.Width * go_array.Height * num_final_channels
go_array.Width * go_array.Height * num_final_channels * bit_depth
if go_array.Channels > 0
else len(go_array.Data)
)
Expand Down

0 comments on commit cde9633

Please sign in to comment.