📄 capture.c
字号:
} if (strcmp(arg_list[PAR_VERSION], argv[i]) == 0){ printf(VERSION); return -1; } } /* for */ if (!par.toStdout){ if (npar > 0){ /* the last is the filename */ par.fname = argv[argc-1]; } else { fprintf(stderr, "You must specify a name for the file" " to be written to or specify the option --stdout\n"); } } return 0; }/* ---------------------------------------------------------------- */unsigned int GetTickCount(){ struct tms t; return 1000*times(&t) / sysconf(_SC_CLK_TCK);}/* ---------------------------------------------------------------- */int main(int argc, char *argv[]){ int fhandler, fhandlerDest, fhandler_background; int min_after_background=0; int nb, nbw, inx; int frame; loff_t seek_atual; unsigned int tick_start, tick_end; long timeSpent; float frameRate; /* default values */ par.nframes = 1; par.toStdout = 0; par.forceFileFormat = 0; par.fileFormat = FILE_FORMAT_UNKNOWN; par.dev = "/dev/fps200"; par.fname = "stdout"; par.rect.x = 0; par.rect.y = 0; par.rect.width = FPS200_WIDTH; par.rect.height = FPS200_HEIGHT; par.background = NULL; if (parse_args(argc, argv) != 0) return -1; if (calculate_mean){ size_buffer_mean = sizeof(buffer_mean[0])*sizeof(buffer); buffer_mean = (typeof(buffer_mean))malloc(size_buffer_mean); if (!buffer_mean){ fprintf(stderr, "Error: could not allocate memory (%d bytes) to\n" "calculate the average image.\n", size_buffer_mean); goto error; } memset(buffer_mean, 0, size_buffer_mean); } if (!par.toStdout){ printf("Number of Frames: %d\n", par.nframes); printf("Destination File Name: %s\n", par.fname); printf("Source Device: %s\n\n", par.dev); printf("Dimension: x=%d y=%d width=%d height=%d\n", par.rect.x, par.rect.y, par.rect.width, par.rect.height); } if (par.toStdout) fhandlerDest = 1; else fhandlerDest = open(par.fname, O_CREAT | O_TRUNC | O_RDWR, S_IREAD | S_IWRITE); if (fhandlerDest >= 0) { fhandler = open(par.dev, 0, S_IREAD); if (fhandler >= 0){ /* select the default values just in case other program change them */ if (ioctl(fhandler, FPS200_IOCTL_RESET_PARAMETERS, 0) != 0){ fprintf(stderr, "Error trying to reset the parameters\n"); goto error; } /* change image dimensions */ if (par.rect.width % 64){ fprintf(stderr, "Error: the width of the image must" " be a multiple of 64\n"); goto error; } if ((par.rect.x+par.rect.width) > FPS200_WIDTH){ fprintf(stderr, "Error: the parameters x=%d and" " width=%d are outrange\n", par.rect.x, par.rect.width); goto error; } if ((par.rect.y+par.rect.height) > FPS200_HEIGHT){ fprintf(stderr, "Error: the parameters y=%d and" " height=%d are outrange\n", par.rect.y, par.rect.height); goto error; } if (ioctl(fhandler, FPS200_IOCTL_SETRECT, &par.rect) != 0){ fprintf(stderr, "Error trying to change the image dimensions\n"); goto error; } /* change the file using extension */ if (!par.forceFileFormat){ if (strcmp(&par.fname[strlen(par.fname)-4], ".pgm") == 0){ if (!par.toStdout) printf("Using the Portable Graymap format\n"); if (ioctl(fhandler, FPS200_IOCTL_SETFORMAT, FPS200_FILEFORMAT_PGM) != 0){ fprintf(stderr, "Error trying to change the image file format\n"); goto error; } } else if (strcmp(&par.fname[strlen(par.fname)-4], ".bmp") == 0){ if (!par.toStdout) printf("Using the Bitmap Format\n"); if (ioctl(fhandler, FPS200_IOCTL_SETFORMAT, FPS200_FILEFORMAT_BMP) != 0){ fprintf(stderr, "Error trying to change the image" " file format\n"); goto error; } } else if (strcmp(&par.fname[strlen(par.fname)-4], ".raw") == 0){ if (!par.toStdout) printf("Using the Raw Format\n"); if (ioctl(fhandler, FPS200_IOCTL_SETFORMAT, FPS200_FILEFORMAT_RAW) != 0){ fprintf(stderr, "Error trying to change the" " image file format\n"); goto error; } } else { if (strcmp(par.fname, "stdout") == 0){ fprintf(stderr, "When redirecting the output to stdout you " "must specify a file format.\nSee --help to more " "details.\n"); } else { fprintf(stderr, "No support for the file format " "specified (%s)\n", &par.fname[strlen(par.fname)-4]); } goto error; } } else { if (ioctl(fhandler, FPS200_IOCTL_SETFORMAT, par.fileFormat) != 0){ fprintf(stderr, "Error trying to change the" " image file format to %d %s\n", par.fileFormat, fileFormatName[par.fileFormat]); goto error; } } tick_start = GetTickCount(); for (frame=0; frame < par.nframes; frame++){ if ((nb=read(fhandler, buffer, sizeof(buffer))) < 0){ fprintf(stderr, "Error trying to read the image\n"); break; } else { if (!par.toStdout) printf("frame %d sucessfully read\n", frame); if (calculate_mean){ for (inx=0; inx < nb; inx++) buffer_mean[inx] = (buffer_mean[inx] * frame + (buffer[inx] & 0xff)) / (frame + 1); if (frame == (par.nframes-1)) { if (par.background){ fprintf(stderr, "using background image %s to " "compute the " "final image", par.background); fhandler_background = open(par.background, O_RDONLY); if (fhandler_background == -1){ fprintf(stderr, "Error: Could not open background" " image %s\n", par.background); perror("Error"); exit(-1); } if ((read(fhandler_background, buffer, sizeof(buffer))) != nb){ fprintf(stderr, "Error: Could not read %d bytes" " from the " "background image\n", nb); perror("Error"); exit(-1); } close(fhandler_background); min_after_background = 0; for (inx=0; inx < nb; inx++) { buffer_mean[inx] -= buffer[inx]; if (buffer_mean[inx] < min_after_background) min_after_background = buffer_mean[inx]; } min_after_background = -min_after_background; for (inx=0; inx < nb; inx++) { buffer_mean[inx] += min_after_background; if (buffer_mean[inx] > 255) buffer_mean[inx] = 255; } } for (inx=0; inx < nb; inx++) buffer[inx] = buffer_mean[inx]; } /* if it is the last frame */ } if (!calculate_mean || (frame == (par.nframes-1))) nbw=write(fhandlerDest, buffer, nb); if (nbw < 0){ fprintf(stderr, "Error: Could not perform the" " writing - code=%d\n", nbw); perror("Error: "); break; } } } /* for */ tick_end = GetTickCount(); /* estimates the frame rate */ if (!par.toStdout){ timeSpent = tick_end-tick_start; if (timeSpent > 0) printf("Total time: %lu ms\n", timeSpent); if (timeSpent > 0){ frameRate = 1000.0*(float)frame / (float)timeSpent; printf("Frame Rate: %.1f fps\n", frameRate); } } close(fhandler); } else { perror("Error trying to open the sensor"); if (errno == ENODEV){ fprintf(stderr, "The system is reporting that there is" " no device in %s.\n" "Maybe you forgot to install the driver using the insmod " "command or specified a\ndiferent minor number for it.\n", par.dev); } goto error; } close(fhandlerDest); } else { fprintf(stderr, "Error: Could not open %s for writing\n", par.fname); } if (buffer_mean) free(buffer_mean); return 0; error: if (fhandlerDest >= 0) unlink(par.fname); return -1;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -