diff --git a/src/image.c b/src/image.c index 9401ebe0..b4b57976 100644 --- a/src/image.c +++ b/src/image.c @@ -2137,3 +2137,22 @@ void free_image(image m) free(m.data); } } + +// Fast copy data from a contiguous byte array into the image. +LIB_API void copy_image_from_bytes(image im, char *pdata) +{ + unsigned char *data = (unsigned char*)pdata; + int i, k, j; + int w = im.w; + int h = im.h; + int c = im.c; + for (k = 0; k < c; ++k) { + for (j = 0; j < h; ++j) { + for (i = 0; i < w; ++i) { + int dst_index = i + w * j + w * h*k; + int src_index = k + c * i + c * w*j; + im.data[dst_index] = (float)data[src_index] / 255.; + } + } + } +} \ No newline at end of file diff --git a/src/image.h b/src/image.h index 67743964..dd38d3ff 100644 --- a/src/image.h +++ b/src/image.h @@ -33,6 +33,7 @@ image random_crop_image(image im, int w, int h); image random_augment_image(image im, float angle, float aspect, int low, int high, int size); void random_distort_image(image im, float hue, float saturation, float exposure); //LIB_API image resize_image(image im, int w, int h); +LIB_API void copy_image_from_bytes(image im, char *pdata); void fill_image(image m, float s); void letterbox_image_into(image im, int w, int h, image boxed); //LIB_API image letterbox_image(image im, int w, int h);