Skip to content

Commit 82764d8

Browse files
authored
mtmd: fix crash when sending image under 2x2 pixels (ggml-org#21711)
1 parent 21a4933 commit 82764d8

1 file changed

Lines changed: 28 additions & 25 deletions

File tree

tools/mtmd/mtmd-image.cpp

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -198,35 +198,38 @@ struct img_tool {
198198
private:
199199
// Bilinear resize function
200200
static void resize_bilinear(const clip_image_u8 & src, clip_image_u8 & dst, int target_width, int target_height) {
201-
GGML_ASSERT(src.nx >= 2 && src.ny >= 2);
201+
if (src.nx == 0 || src.ny == 0) { dst.nx = dst.ny = 0; dst.buf.clear(); return; }
202+
if (target_width <= 0) target_width = 1;
203+
if (target_height <= 0) target_height = 1;
204+
202205
dst.nx = target_width;
203206
dst.ny = target_height;
204207
dst.buf.resize(3 * target_width * target_height);
205208

206-
float x_ratio = static_cast<float>(src.nx - 1) / target_width;
207-
float y_ratio = static_cast<float>(src.ny - 1) / target_height;
208-
209-
for (int y = 0; y < target_height; y++) {
210-
for (int x = 0; x < target_width; x++) {
211-
float px = x_ratio * x;
212-
float py = y_ratio * y;
213-
int x_floor = std::min(static_cast<int>(px), src.nx - 2);
214-
int y_floor = std::min(static_cast<int>(py), src.ny - 2);
215-
float x_lerp = px - x_floor;
216-
float y_lerp = py - y_floor;
217-
218-
for (int c = 0; c < 3; c++) {
219-
float top = lerp(
220-
static_cast<float>(src.buf[3 * (y_floor * src.nx + x_floor) + c]),
221-
static_cast<float>(src.buf[3 * (y_floor * src.nx + (x_floor + 1)) + c]),
222-
x_lerp
223-
);
224-
float bottom = lerp(
225-
static_cast<float>(src.buf[3 * ((y_floor + 1) * src.nx + x_floor) + c]),
226-
static_cast<float>(src.buf[3 * ((y_floor + 1) * src.nx + (x_floor + 1)) + c]),
227-
x_lerp
228-
);
229-
dst.buf[3 * (y * target_width + x) + c] = static_cast<uint8_t>(lerp(top, bottom, y_lerp));
209+
float x_ratio = target_width > 1 ? static_cast<float>(src.nx - 1) / (target_width - 1) : 0.0f;
210+
float y_ratio = target_height > 1 ? static_cast<float>(src.ny - 1) / (target_height - 1) : 0.0f;
211+
212+
for (int y = 0; y < target_height; ++y) {
213+
for (int x = 0; x < target_width; ++x) {
214+
float px = x * x_ratio;
215+
float py = y * y_ratio;
216+
217+
int x0 = std::min(static_cast<int>(px), src.nx - 1);
218+
int y0 = std::min(static_cast<int>(py), src.ny - 1);
219+
int x1 = std::min(x0 + 1, src.nx - 1);
220+
int y1 = std::min(y0 + 1, src.ny - 1);
221+
222+
float xf = px - x0;
223+
float yf = py - y0;
224+
225+
for (int c = 0; c < 3; ++c) {
226+
float top = lerp(static_cast<float>(src.buf[3 * (y0 * src.nx + x0) + c]),
227+
static_cast<float>(src.buf[3 * (y0 * src.nx + x1) + c]),
228+
xf);
229+
float bottom = lerp(static_cast<float>(src.buf[3 * (y1 * src.nx + x0) + c]),
230+
static_cast<float>(src.buf[3 * (y1 * src.nx + x1) + c]),
231+
xf);
232+
dst.buf[3 * (y * target_width + x) + c] = static_cast<uint8_t>(lerp(top, bottom, yf));
230233
}
231234
}
232235
}

0 commit comments

Comments
 (0)