Skip to content
This repository was archived by the owner on Aug 22, 2024. It is now read-only.

Commit 85f9b92

Browse files
authored
Enabled implicit stride in k4a_image_create (#819)
* Enabled implicit stride in k4a_image_create * Added note on default parameters to standards.md
1 parent a8b1a8a commit 85f9b92

5 files changed

Lines changed: 59 additions & 26 deletions

File tree

docs/standards.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ allow the reader to quickly and easily tell the difference between internal and
4646
there is nothing the user can do to resolve the issue, then it is acceptable to crash. If there is action the
4747
user could take (not including an application restart or PC reboot), then returning an error specific to the recovery
4848
action is acceptable.
49+
* Don't use default parameters in public APIs (C++/C#)
4950

5051
## Clang-format
5152

include/k4a/k4a.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,7 @@ K4A_EXPORT float k4a_capture_get_temperature_c(k4a_capture_t capture_handle);
595595
*
596596
* \param stride_bytes
597597
* The number of bytes per horizontal line of the image.
598+
* If set to 0, the stride will be set to the minimum size given the \p format and \p width_pixels.
598599
*
599600
* \param image_handle
600601
* Pointer to store image handle in.

src/csharp/SDK/Image.cs

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public class Image : IMemoryOwner<byte>, IDisposable
4646
/// <param name="format">The pixel format of the image. Must be a format with a constant pixel size.</param>
4747
/// <param name="widthPixels">Width of the image in pixels.</param>
4848
/// <param name="heightPixels">Height of the image in pixels.</param>
49-
/// <param name="strideBytes">Stride of the image in bytes. Must be as large as the width times the size of a pixel.</param>
49+
/// <param name="strideBytes">Stride of the image in bytes. Must be as large as the width times the size of a pixel. Set to zero for the default if available for that format.</param>
5050
public Image(ImageFormat format, int widthPixels, int heightPixels, int strideBytes)
5151
{
5252
// Hook the native allocator and register this object.
@@ -69,26 +69,6 @@ public Image(ImageFormat format, int widthPixels, int heightPixels, int strideBy
6969
/// <param name="heightPixels">Height of the image in pixels.</param>
7070
public Image(ImageFormat format, int widthPixels, int heightPixels)
7171
{
72-
int pixelSize;
73-
switch (format)
74-
{
75-
case ImageFormat.ColorBGRA32:
76-
pixelSize = 4;
77-
break;
78-
case ImageFormat.Depth16:
79-
case ImageFormat.IR16:
80-
case ImageFormat.Custom16:
81-
pixelSize = 2;
82-
break;
83-
case ImageFormat.Custom8:
84-
pixelSize = 1;
85-
break;
86-
default:
87-
throw new AzureKinectException($"Unable to allocate array for format {format}");
88-
}
89-
90-
int stride_bytes = widthPixels * pixelSize;
91-
9272
// Hook the native allocator and register this object.
9373
// .Dispose() will be called on this object when the allocator is shut down.
9474
Allocator.Singleton.RegisterForDisposal(this);
@@ -98,7 +78,7 @@ public Image(ImageFormat format, int widthPixels, int heightPixels)
9878
format,
9979
widthPixels,
10080
heightPixels,
101-
stride_bytes,
81+
0,
10282
image_handle: out this.handle));
10383
#pragma warning restore CA2000 // Dispose objects before losing scope
10484
}

src/image/image.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,12 @@ k4a_result_t image_create(k4a_image_format_t format,
186186

187187
case K4A_IMAGE_FORMAT_COLOR_NV12:
188188
{
189+
if (stride_bytes == 0)
190+
{
191+
// If stride isn't specified, assume the minimum stride
192+
stride_bytes = width_pixels;
193+
}
194+
189195
if (height_pixels % 2 != 0)
190196
{
191197
LOG_ERROR("NV12 requires an even number of lines. Height %d is invalid.", height_pixels);
@@ -215,6 +221,12 @@ k4a_result_t image_create(k4a_image_format_t format,
215221
// 1 Byte per pixel
216222
case K4A_IMAGE_FORMAT_CUSTOM8:
217223
{
224+
if (stride_bytes == 0)
225+
{
226+
// If stride isn't specified, assume the minimum stride
227+
stride_bytes = width_pixels;
228+
}
229+
218230
if (stride_bytes < 1 * width_pixels)
219231
{
220232
LOG_ERROR("Insufficient stride (%d bytes) to represent image width (%d pixels).",
@@ -235,6 +247,12 @@ k4a_result_t image_create(k4a_image_format_t format,
235247
case K4A_IMAGE_FORMAT_IR16:
236248
case K4A_IMAGE_FORMAT_CUSTOM16:
237249
{
250+
if (stride_bytes == 0)
251+
{
252+
// If stride isn't specified, assume the minimum stride
253+
stride_bytes = width_pixels * 2;
254+
}
255+
238256
if (stride_bytes < 2 * width_pixels)
239257
{
240258
LOG_ERROR("Insufficient stride (%d bytes) to represent image width (%d pixels).",
@@ -253,6 +271,12 @@ k4a_result_t image_create(k4a_image_format_t format,
253271
// 2 Bytes per pixel
254272
case K4A_IMAGE_FORMAT_COLOR_YUY2:
255273
{
274+
if (stride_bytes == 0)
275+
{
276+
// If stride isn't specified, assume the minimum stride
277+
stride_bytes = width_pixels * 2;
278+
}
279+
256280
if (width_pixels % 2 != 0)
257281
{
258282
LOG_ERROR("YUY2 requires an even number of pixels per line. Width of %d is invalid.", width_pixels);
@@ -276,6 +300,12 @@ k4a_result_t image_create(k4a_image_format_t format,
276300
// 4 Bytes per pixel
277301
case K4A_IMAGE_FORMAT_COLOR_BGRA32:
278302
{
303+
if (stride_bytes == 0)
304+
{
305+
// If stride isn't specified, assume the minimum stride
306+
stride_bytes = width_pixels * 4;
307+
}
308+
279309
if (stride_bytes < 4 * width_pixels)
280310
{
281311
LOG_ERROR("Insufficient stride (%d bytes) to represent image width (%d pixels).",

tests/UnitTests/allocator_ut/allocator.cpp

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -190,10 +190,6 @@ TEST(allocator_ut, image_api_validation)
190190
ASSERT_EQ(K4A_RESULT_FAILED,
191191
image_create(K4A_IMAGE_FORMAT_COLOR_NV12, 10, 10, 1, ALLOCATION_SOURCE_USER, NULL));
192192

193-
// Stride of zero
194-
ASSERT_EQ(K4A_RESULT_FAILED,
195-
image_create(K4A_IMAGE_FORMAT_COLOR_NV12, 10, 10, 0, ALLOCATION_SOURCE_USER, &image));
196-
197193
// Stride length
198194
// Validate a valid length and an invalid length
199195

@@ -217,6 +213,11 @@ TEST(allocator_ut, image_api_validation)
217213
// Odd number of columns
218214
ASSERT_EQ(K4A_RESULT_FAILED,
219215
image_create(K4A_IMAGE_FORMAT_COLOR_NV12, 11, 10, 20, ALLOCATION_SOURCE_USER, &image));
216+
// Stride of zero (should succeed and infer the minimum stride)
217+
ASSERT_EQ(K4A_RESULT_SUCCEEDED,
218+
image_create(K4A_IMAGE_FORMAT_COLOR_NV12, 10, 10, 0, ALLOCATION_SOURCE_USER, &image));
219+
ASSERT_EQ(10, image_get_stride_bytes(image));
220+
image_dec_ref(image);
220221

221222
// YUY2
222223
// Minimum stride
@@ -240,6 +241,11 @@ TEST(allocator_ut, image_api_validation)
240241
// Odd number of columns
241242
ASSERT_EQ(K4A_RESULT_FAILED,
242243
image_create(K4A_IMAGE_FORMAT_COLOR_YUY2, 11, 10, 20, ALLOCATION_SOURCE_USER, &image));
244+
// Stride of zero (should succeed and infer the minimum stride)
245+
ASSERT_EQ(K4A_RESULT_SUCCEEDED,
246+
image_create(K4A_IMAGE_FORMAT_COLOR_YUY2, 10, 10, 0, ALLOCATION_SOURCE_USER, &image));
247+
ASSERT_EQ(10 * 2, image_get_stride_bytes(image));
248+
image_dec_ref(image);
243249

244250
// BGRA32
245251
// Minimum stride
@@ -250,6 +256,11 @@ TEST(allocator_ut, image_api_validation)
250256
// Insufficient stride
251257
ASSERT_EQ(K4A_RESULT_FAILED,
252258
image_create(K4A_IMAGE_FORMAT_COLOR_BGRA32, 10, 10, 39, ALLOCATION_SOURCE_USER, &image));
259+
// Stride of zero (should succeed and infer the minimum stride)
260+
ASSERT_EQ(K4A_RESULT_SUCCEEDED,
261+
image_create(K4A_IMAGE_FORMAT_COLOR_BGRA32, 10, 10, 0, ALLOCATION_SOURCE_USER, &image));
262+
ASSERT_EQ(10 * 4, image_get_stride_bytes(image));
263+
image_dec_ref(image);
253264

254265
// MJPEG (no length is valid)
255266
ASSERT_EQ(K4A_RESULT_FAILED,
@@ -264,6 +275,11 @@ TEST(allocator_ut, image_api_validation)
264275
// Insufficient stride
265276
ASSERT_EQ(K4A_RESULT_FAILED,
266277
image_create(K4A_IMAGE_FORMAT_DEPTH16, 10, 10, 19, ALLOCATION_SOURCE_USER, &image));
278+
// Stride of zero (should succeed and infer the minimum stride)
279+
ASSERT_EQ(K4A_RESULT_SUCCEEDED,
280+
image_create(K4A_IMAGE_FORMAT_DEPTH16, 10, 10, 0, ALLOCATION_SOURCE_USER, &image));
281+
ASSERT_EQ(10 * 2, image_get_stride_bytes(image));
282+
image_dec_ref(image);
267283

268284
// Custom8
269285
// Minimum stride
@@ -274,6 +290,11 @@ TEST(allocator_ut, image_api_validation)
274290
// Insufficient stride
275291
ASSERT_EQ(K4A_RESULT_FAILED,
276292
(int)image_create(K4A_IMAGE_FORMAT_CUSTOM8, 10, 10, 9, ALLOCATION_SOURCE_USER, &image));
293+
// Stride of zero (should succeed and infer the minimum stride)
294+
ASSERT_EQ(K4A_RESULT_SUCCEEDED,
295+
image_create(K4A_IMAGE_FORMAT_CUSTOM8, 10, 10, 0, ALLOCATION_SOURCE_USER, &image));
296+
ASSERT_EQ(10 * 1, image_get_stride_bytes(image));
297+
image_dec_ref(image);
277298

278299
// Height of zero
279300
ASSERT_EQ(K4A_RESULT_FAILED,

0 commit comments

Comments
 (0)