Skip to content

Commit 9798f33

Browse files
tingloungxson
authored andcommitted
llava: build clip image from pixels (ggml-org#11999)
* llava: export function `clip_build_img_from_pixels` to build image from pixels decoded by other libraries instead of stb_image.h for better performance * Apply suggestions from code review --------- Co-authored-by: Xuan-Son Nguyen <[email protected]>
1 parent 01c1b81 commit 9798f33

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

examples/llava/clip.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1729,11 +1729,11 @@ void clip_image_f32_batch_free(struct clip_image_f32_batch * batch) {
17291729
}
17301730
}
17311731

1732-
static void build_clip_img_from_data(const stbi_uc * data, int nx, int ny, clip_image_u8 * img) {
1732+
void clip_build_img_from_pixels(const unsigned char * rgb_pixels, int nx, int ny, clip_image_u8 * img) {
17331733
img->nx = nx;
17341734
img->ny = ny;
17351735
img->buf.resize(3 * nx * ny);
1736-
memcpy(img->buf.data(), data, img->buf.size());
1736+
memcpy(img->buf.data(), rgb_pixels, img->buf.size());
17371737
}
17381738

17391739
bool clip_image_load_from_file(const char * fname, clip_image_u8 * img) {
@@ -1743,7 +1743,7 @@ bool clip_image_load_from_file(const char * fname, clip_image_u8 * img) {
17431743
LOG_ERR("%s: failed to load image '%s'\n", __func__, fname);
17441744
return false;
17451745
}
1746-
build_clip_img_from_data(data, nx, ny, img);
1746+
clip_build_img_from_pixels(data, nx, ny, img);
17471747
stbi_image_free(data);
17481748
return true;
17491749
}
@@ -1755,7 +1755,7 @@ bool clip_image_load_from_bytes(const unsigned char * bytes, size_t bytes_length
17551755
LOG_ERR("%s: failed to decode image bytes\n", __func__);
17561756
return false;
17571757
}
1758-
build_clip_img_from_data(data, nx, ny, img);
1758+
clip_build_img_from_pixels(data, nx, ny, img);
17591759
stbi_image_free(data);
17601760
return true;
17611761
}

examples/llava/clip.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ CLIP_API void clip_image_f32_free(struct clip_image_f32 * img);
7373
CLIP_API void clip_image_u8_batch_free (struct clip_image_u8_batch * batch);
7474
CLIP_API void clip_image_f32_batch_free(struct clip_image_f32_batch * batch);
7575

76+
/** build image from pixels decoded by other libraries instead of stb_image.h for better performance. The memory layout is RGBRGBRGB..., input buffer length must be 3*nx*ny bytes */
77+
CLIP_API void clip_build_img_from_pixels(const unsigned char * rgb_pixels, int nx, int ny, clip_image_u8 * img);
78+
7679
CLIP_API bool clip_image_load_from_file(const char * fname, struct clip_image_u8 * img);
7780

7881
/** interpret bytes as an image file with length bytes_length, and use the result to populate img */

0 commit comments

Comments
 (0)