📄 webcam.c
字号:
} if ((line & 1) == 0) { su -= width / 2; sv -= width / 2; } } imlib_image_put_back_data(data); return im;}/* This is a really simplistic approach. Speedups are welcomed. */Imlib_Imageconvert_yuv420i_to_imlib2(unsigned char *src, int width, int height){ int line, col, linewidth; int y, u, v, yy, vr = 0, ug = 0, vg = 0, ub = 0; int r, g, b; unsigned char *sy, *su, *sv; Imlib_Image im; DATA32 *data, *dest; im = imlib_create_image(width, height); imlib_context_set_image(im); data = imlib_image_get_data(); dest = data; linewidth = width + (width >> 1); sy = src; su = sy + 4; sv = su + linewidth; /* The biggest problem is the interlaced data, and the fact that odd add even lines have V and U data, resp. */ for (line = 0; line < height; line++) { for (col = 0; col < width; col++) { y = *sy++; yy = y << 8; if ((col & 1) == 0) { /* only at even colums we update the u/v data */ u = *su - 128; ug = 88 * u; ub = 454 * u; v = *sv - 128; vg = 183 * v; vr = 359 * v; su++; sv++; } if ((col & 3) == 3) { sy += 2; /* skip u/v */ su += 4; /* skip y */ sv += 4; /* skip y */ } r = (yy + vr) >> 8; g = (yy - ug - vg) >> 8; b = (yy + ub) >> 8; if (r < 0) r = 0; if (r > 255) r = 255; if (g < 0) g = 0; if (g > 255) g = 255; if (b < 0) b = 0; if (b > 255) b = 255; *dest = (r << 16) | (g << 8) | b | 0xff000000; dest++; } if (line & 1) { su += linewidth; sv += linewidth; } else { su -= linewidth; sv -= linewidth; } } imlib_image_put_back_data(data); return im;}Imlib_Imageconvert_rgb24_to_imlib2(unsigned char *mem, int width, int height){ Imlib_Image im; DATA32 *data, *dest; unsigned char *src; int i; im = imlib_create_image(width, height); imlib_context_set_image(im); data = imlib_image_get_data(); dest = data; src = mem; i = width * height; while (i--) { *dest = (src[2] << 16) | (src[1] << 8) | src[0] | 0xff000000; dest++; src += 3; } imlib_image_put_back_data(data); return im;}Imlib_Imagegrab_one(int *width, int *height){ Imlib_Image im; int i = 0; int j = lag_reduce; if (grab_fd == -1) grab_init(); if (j == 0) j++; while (j--) { if (ioctl(grab_fd, VIDIOCMCAPTURE, &grab_buf) == -1) { perror("ioctl VIDIOCMCAPTURE"); return NULL; } if (ioctl(grab_fd, VIDIOCSYNC, &i) == -1) { perror("ioctl VIDIOCSYNC"); return NULL; } } switch (device_palette) { case VIDEO_PALETTE_YUV420P: im = convert_yuv420p_to_imlib2(grab_data, grab_buf.width, grab_buf.height); break; case VIDEO_PALETTE_YUV420: im = convert_yuv420i_to_imlib2(grab_data, grab_buf.width, grab_buf.height); break; case VIDEO_PALETTE_RGB24: im = convert_rgb24_to_imlib2(grab_data, grab_buf.width, grab_buf.height); break; default: fprintf(stderr, "eeek"); exit(2); } if (close_dev) close_device(); if (im) { imlib_context_set_image(im); imlib_image_attach_data_value("quality", NULL, grab_quality, NULL); } *width = grab_buf.width; *height = grab_buf.height; return im;}char *get_message(void){ static char buffer[4096]; FILE *fp; fp = fopen(grab_infofile, "r"); if (fp) { fgets(buffer, sizeof(buffer), fp); fclose(fp); return buffer; } return NULL;}/* ---------------------------------------------------------------------- */voidadd_time_text(Imlib_Image image, char *message, int width, int height){ time_t t; struct tm *tm; char line[255], title_line[255]; int len; int x, y, w, h; time(&t); tm = localtime(&t); strftime(line, 254, grab_text, tm); if (title_text) strftime(title_line, 254, title_text, tm); if (message) strcat(line, message); line[127] = '\0'; len = strlen(line); if (line[len - 1] == '\n') line[--len] = '\0'; if (title_text && title_fn) { gib_imlib_get_text_size(title_fn, title_line, title_style, &w, &h, IMLIB_TEXT_TO_RIGHT); x = width - w - 2; y = 2; gib_imlib_image_fill_rectangle(image, x - 2, y - 1, w + 4, h + 2, bg_r, bg_g, bg_b, bg_a); gib_imlib_text_draw(image, title_fn, title_style, x, y, title_line, IMLIB_TEXT_TO_RIGHT, title_r, title_g, title_b, title_a); } if (line && text_fn) { gib_imlib_get_text_size(text_fn, line, text_style, &w, &h, IMLIB_TEXT_TO_RIGHT); x = 2; y = height - h - 2; gib_imlib_image_fill_rectangle(image, x - 2, y - 1, w + 4, h + 2, bg_r, bg_g, bg_b, bg_a); gib_imlib_text_draw(image, text_fn, text_style, x, y, line, IMLIB_TEXT_TO_RIGHT, text_r, text_g, text_b, text_a); }}/* ---------------------------------------------------------------------- */voiddo_postprocess(char *filename){ if (grab_postprocess) { char buf[4096]; camlog("executing postprocessing\n"); snprintf(buf, sizeof(buf), "%s %s", grab_postprocess, filename); system(buf); }}voidarchive_jpeg(Imlib_Image im){ char buffer[PATH_MAX]; char date[128]; char year[5]; char month[3]; char day[3]; time_t t; struct tm *tm; static int shot_counter = 0; Imlib_Image thumbnail_image; shot_counter++; if (grab_archive && archive_shot_every && shot_counter >= archive_shot_every) { time(&t); tm = localtime(&t); if (archive_subdirs) { strftime(date, 128, "%H%M%S", tm); strftime(year, 5, "%Y", tm); strftime(month, 3, "%m", tm); strftime(day, 3, "%d", tm); snprintf(buffer, sizeof(buffer), "%s/%s", grab_archive, year); if (access(buffer, F_OK) == -1) { mkdir(buffer, 0777); camlog("Created new archive subdir %s\n", buffer); } snprintf(buffer, sizeof(buffer), "%s/%s/%s", grab_archive, year, month); if (access(buffer, F_OK) == -1) { mkdir(buffer, 0777); camlog("Created new archive subdir %s\n", buffer); } snprintf(buffer, sizeof(buffer), "%s/%s/%s/%s", grab_archive, year, month, day); if (access(buffer, F_OK) == -1) { mkdir(buffer, 0777); camlog("Created new archive subdir %s\n", buffer); } snprintf(buffer, sizeof(buffer), "%s/%s/%s/%s/%s.%s", grab_archive, year, month, day, date, archive_ext); } else { strftime(date, 127, "%Y-%m-%d_%H%M%S", tm); snprintf(buffer, sizeof(buffer), "%s/webcam_%s.%s", grab_archive, date, archive_ext); } save_image(im, buffer); shot_counter = 0; /* * if archive thumbnails are enabled we save them here, * same filenames just in "archive_thumbnails_directory" directory * files are saved in the same format as archive */ if (archive_thumbnails_create && archive_thumbnails_width && archive_thumbnails_height) { if (archive_subdirs) { snprintf(buffer, sizeof(buffer), "%s/%s", archive_thumbnails_dir, year); if (access(buffer, F_OK) == -1) { mkdir(buffer, 0777); camlog("Created new archive subdir %s\n", buffer); } snprintf(buffer, sizeof(buffer), "%s/%s/%s", archive_thumbnails_dir, year, month); if (access(buffer, F_OK) == -1) { mkdir(buffer, 0777); camlog("Created new archive subdir %s\n", buffer); } snprintf(buffer, sizeof(buffer), "%s/%s/%s/%s", archive_thumbnails_dir, year, month, day); if (access(buffer, F_OK) == -1) { mkdir(buffer, 0777); camlog("Created new archive subdir %s\n", buffer); } snprintf(buffer, sizeof(buffer), "%s/%s/%s/%s/%s.%s", archive_thumbnails_dir, year, month, day, date, archive_ext); } else { snprintf(buffer, sizeof(buffer), "%s/webcam_%s.%s", archive_thumbnails_dir, date, archive_ext); } thumbnail_image = gib_imlib_create_cropped_scaled_image(im, 0, 0, gib_imlib_image_get_width(im), gib_imlib_image_get_height(im), archive_thumbnails_width, archive_thumbnails_height, 1); save_image(thumbnail_image, buffer); gib_imlib_free_image_and_decache(thumbnail_image); } }}voidcamlog(char *fmt, ...){ va_list args; time_t t; struct tm *tm; char date[128]; FILE *fp; if (!logfile) { va_start(args, fmt); fprintf(stderr, "camE: "); vfprintf(stderr, fmt, args); va_end(args); return; } fp = fopen(logfile, "a"); if (!fp) { fprintf(stderr, "can't open log file %s\n", logfile); exit(2); } time(&t); tm = localtime(&t); strftime(date, 127, "%d/%m %H:%M:%S", tm); fprintf(fp, "%s ", date); va_start(args, fmt); vfprintf(fp, fmt, args); va_end(args); fclose(fp);}intsave_image(Imlib_Image image, char *file){ Imlib_Load_Error err; gib_imlib_save_image_with_error_return(image, file, &err); if ((err) || (!image)) { switch (err) { case IMLIB_LOAD_ERROR_FILE_DOES_NOT_EXIST: camlog("Error saving image %s - File does not exist", file); break; case IMLIB_LOAD_ERROR_FILE_IS_DIRECTORY: camlog("Error saving image %s - Directory specified for image filename", file); break; case IMLIB_LOAD_ERROR_PERMISSION_DENIED_TO_READ: camlog("Error saving image %s - No read access to directory", file); break; case IMLIB_LOAD_ERROR_UNKNOWN: case IMLIB_LOAD_ERROR_NO_LOADER_FOR_FILE_FORMAT: camlog("Error saving image %s - No Imlib2 loader for that file format", file); break; case IMLIB_LOAD_ERROR_PATH_TOO_LONG: camlog("Error saving image %s - Path specified is too long", file); break; case IMLIB_LOAD_ERROR_PATH_COMPONENT_NON_EXISTANT: camlog("Error saving image %s - Path component does not exist", file); break; case IMLIB_LOAD_ERROR_PATH_COMPONENT_NOT_DIRECTORY: camlog("Error saving image %s - Path component is not a directory", file); break; case IMLIB_LOAD_ERROR_PATH_POINTS_OUTSIDE_ADDRESS_SPACE: camlog("Error saving image %s - Path points outside address space",
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -