Skip to content

Commit e796830

Browse files
webcam: keep aspect ratio
1 parent 9b6a197 commit e796830

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

c_mpos/src/webcam.c

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,22 @@ typedef struct _webcam_obj_t {
3131
} webcam_obj_t;
3232

3333
static void yuyv_to_grayscale_240x240(unsigned char *yuyv, unsigned char *gray, int in_width, int in_height) {
34-
float x_ratio = (float)in_width / OUTPUT_WIDTH;
35-
float y_ratio = (float)in_height / OUTPUT_HEIGHT;
34+
// Crop to 480x480 centered region
35+
int crop_size = 480;
36+
int crop_x_offset = (in_width - crop_size) / 2; // Center the crop: (640 - 480) / 2 = 80
37+
int crop_y_offset = (in_height - crop_size) / 2; // Center the crop: (480 - 480) / 2 = 0
38+
39+
// Downscale ratios from 480x480 to 240x240
40+
float x_ratio = (float)crop_size / OUTPUT_WIDTH; // 480 / 240 = 2.0
41+
float y_ratio = (float)crop_size / OUTPUT_HEIGHT; // 480 / 240 = 2.0
3642

3743
for (int y = 0; y < OUTPUT_HEIGHT; y++) {
3844
for (int x = 0; x < OUTPUT_WIDTH; x++) {
39-
int src_x = (int)(x * x_ratio);
40-
int src_y = (int)(y * y_ratio);
41-
int src_index = (src_y * in_width + src_x) * 2;
42-
gray[y * OUTPUT_WIDTH + x] = yuyv[src_index];
45+
// Map output pixel to cropped region
46+
int src_x = (int)(x * x_ratio) + crop_x_offset; // Adjust for crop offset
47+
int src_y = (int)(y * y_ratio) + crop_y_offset; // Adjust for crop offset
48+
int src_index = (src_y * in_width + src_x) * 2; // YUYV uses 2 bytes per pixel
49+
gray[y * OUTPUT_WIDTH + x] = yuyv[src_index]; // Extract Y channel
4350
}
4451
}
4552
}

0 commit comments

Comments
 (0)