Making a fast API compatible way to copy image data.

This can improve on the python array_to_image function
where we already have an allocated image struct and simply
need to copy the data into the correct format/shape without
reallocating.

Aside: I think array_to_image does two frees because the pointer
data_as returns doesn't own the memory that is ultimately freed
in free_image.
This commit is contained in:
John Aughey 2019-02-05 11:40:21 -06:00
parent 7e9416aa80
commit c00d3c92db
2 changed files with 20 additions and 0 deletions

View File

@ -2137,3 +2137,22 @@ void free_image(image m)
free(m.data); 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.;
}
}
}
}

View File

@ -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); 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); 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 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 fill_image(image m, float s);
void letterbox_image_into(image im, int w, int h, image boxed); void letterbox_image_into(image im, int w, int h, image boxed);
//LIB_API image letterbox_image(image im, int w, int h); //LIB_API image letterbox_image(image im, int w, int h);