Skip to content

Commit 52cd4a2

Browse files
committed
fix: corrupted canny image memory buffer
The sd_image_t struct is passed by value, so the data pointer can't be changed. Instead, just overwrite the image data area.
1 parent 35843c7 commit 52cd4a2

File tree

2 files changed

+5
-5
lines changed

2 files changed

+5
-5
lines changed

ggml_extend.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -368,12 +368,14 @@ __STATIC_INLINE__ float sigmoid(float x) {
368368

369369
// SPECIAL OPERATIONS WITH TENSORS
370370

371-
__STATIC_INLINE__ uint8_t* sd_tensor_to_image(struct ggml_tensor* input) {
371+
__STATIC_INLINE__ uint8_t* sd_tensor_to_image(struct ggml_tensor* input, uint8_t* image_data = nullptr) {
372372
int64_t width = input->ne[0];
373373
int64_t height = input->ne[1];
374374
int64_t channels = input->ne[2];
375375
GGML_ASSERT(channels == 3 && input->type == GGML_TYPE_F32);
376-
uint8_t* image_data = (uint8_t*)malloc(width * height * channels);
376+
if (image_data == nullptr) {
377+
image_data = (uint8_t*)malloc(width * height * channels);
378+
}
377379
for (int iy = 0; iy < height; iy++) {
378380
for (int ix = 0; ix < width; ix++) {
379381
for (int k = 0; k < channels; k++) {

preprocessing.hpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -218,9 +218,7 @@ bool preprocess_canny(sd_image_t img, float high_threshold, float low_threshold,
218218
ggml_tensor_set_f32(image, gray, ix, iy, 2);
219219
}
220220
}
221-
uint8_t* output = sd_tensor_to_image(image);
222-
free(img.data);
223-
img.data = output;
221+
sd_tensor_to_image(image, img.data);
224222
ggml_free(work_ctx);
225223
return true;
226224
}

0 commit comments

Comments
 (0)