/* * translatexy input output xoffset yoffset: shift an image by the given offsets */ static int cmd_translatexy(int argc, char *argv[]) { img_t *image, *newimage; char *q; int rv; long dx, dy; if (argc < 4) return (EXIT_USAGE); image = img_read(argv[0]); if (image == NULL) return (EXIT_FAILURE); dx = strtol(argv[2], &q, 0); if (*q != '\0') warnx("x offset value truncated to %d", dx); dy = strtol(argv[3], &q, 0); if (*q != '\0') warnx("y offset value truncated to %d", dy); newimage = img_translatexy(image, dx, dy); if (newimage == NULL) { warn("failed to translate image"); img_free(image); return (EXIT_FAILURE); } rv = img_write(newimage, argv[1]); img_free(newimage); return (rv); }
void UndistortImages(const std::vector<image_t> &images, const fisheye_params_t &fisheye_params) { int num_images = (int) images.size(); for (int i = 0; i < num_images; i++) { img_t *img = LoadJPEG(images[i].name.c_str()); img_t *img_u; if (images[i].is_fisheye) { printf("Undistorting image %s\n", images[i].name.c_str()); img_u = UndistortImage(img, fisheye_params); } else { printf("Skipping image %s (not marked as fisheye).\n", images[i].name.c_str()); img_u = img_copy(img); } const std::string out = images[i].name.substr(0, images[i].name.length() - 3).append("fd.jpg"); WriteJPEG(img_u, (const char *) out.c_str()); img_free(img); img_free(img_u); } }
/* * and input1 input2 output: logical-and pixels of two images */ static int cmd_and(int argc, char *argv[]) { img_t *image, *mask; int rv; if (argc < 3) return (EXIT_USAGE); image = img_read(argv[0]); mask = img_read(argv[1]); if (mask == NULL || image == NULL) { img_free(image); return (EXIT_FAILURE); } if (image->img_width != mask->img_width || image->img_height != mask->img_height) { warnx("image dimensions do not match"); img_free(image); img_free(mask); return (EXIT_FAILURE); } img_and(image, mask); rv = img_write(image, argv[2]); img_free(image); img_free(mask); return (rv == 0 ? EXIT_SUCCESS : EXIT_FAILURE); }
img_t* img_edge(img_t* img, unsigned int min_dist) { img_t* dx = img_dx(img); img_t* dy = img_dy(img); img_t* m = img_similar(img); int i, j; unsigned char avg[3] = {127, 127, 127}; for(i = 0; i < m->w*img->h; ++i) { if (dist2(avg, dx->data + i * 3) > min_dist) { for(j = 0; j < 3; ++j) { m->data[i*3 + j] = ~0; } } else { for(j = 0; j < 3; ++j) { m->data[i*3 + j] = 0; } } } for(i = 0; i < m->w*img->h; ++i) { if (dist2(avg, dy->data + i * 3) > min_dist) { for(j = 0; j < 3; ++j) { m->data[i*3 + j] = ~0; } } } img_free(dx); img_free(dy); return m; }
/* * compare image mask: compute a difference score for the given image and mask. */ static int cmd_compare(int argc, char *argv[]) { img_t *image, *mask, *dbgmask; char *dbgfile = NULL; int rv; char c; while ((c = getopt(argc, argv, "s:")) != -1) { switch (c) { case 's': dbgfile = optarg; break; default: return (EXIT_USAGE); } } if (optind + 2 > argc) return (EXIT_USAGE); image = img_read(argv[optind++]); mask = img_read(argv[optind++]); if (mask == NULL || image == NULL) { img_free(image); return (EXIT_FAILURE); } if (image->img_width != mask->img_width || image->img_height != mask->img_height) { warnx("image dimensions do not match"); rv = EXIT_FAILURE; goto done; } (void) printf("%f\n", img_compare(image, mask, dbgfile ? &dbgmask : NULL)); if (dbgfile != NULL && dbgmask != NULL) { (void) img_write(dbgmask, dbgfile); img_free(dbgmask); } rv = EXIT_SUCCESS; done: img_free(image); img_free(mask); return (rv); }
int main(int argc, char *argv[]) { img_buf *i; if(!argv[1]) { printf("USAGE: %s name\n\t reads name where name is an image.\n", argv[0]); return 1; } printf("Loading file \"%s\"\n", argv[1]); i = img_load(argv[1]); if(!i) { printf("Failed to load.\n"); return 2; } img_free(i); printf("Happily free.\n"); return 0; }
void UndistortImage(const std::string &in, const camera_params_t &camera, const std::string &out) { printf("Undistorting image %s\n", in.c_str()); fflush(stdout); img_t *img = LoadJPEG(in.c_str()); int w = img->w; int h = img->h; img_t *img_out = img_new(w, h); double f2_inv = 1.0 / (camera.f * camera.f); for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { double x_c = x - 0.5 * w; double y_c = y - 0.5 * h; double r2 = (x_c * x_c + y_c * y_c) * f2_inv; double factor = 1.0 + camera.k[0] * r2 + camera.k[1] * r2 * r2; x_c *= factor; y_c *= factor; x_c += 0.5 * w; y_c += 0.5 * h; fcolor_t c; if (x_c >= 0 && x_c < w - 1 && y_c >= 0 && y_c < h - 1) { c = pixel_lerp(img, x_c, y_c); } else { c = fcolor_new(0.0, 0.0, 0.0); } img_set_pixel(img_out, x, y, iround(c.r), iround(c.g), iround(c.b)); } } // img_write_bmp_file(img_out, (char *) out.c_str()); WriteJPEG(img_out, (char *) out.c_str()); img_free(img); img_free(img_out); }
int write_data(const char* paths, const char* fname, unsigned char type) { FILE* in = fopen(paths, "r"); FILE* out = fopen(fname, "wb"); char buff[FILENAME_MAX]; unsigned int num_paths; unsigned int path_len; unsigned int type_size; void* val; void (*func) (img_t*, void*); img_t* img; int i; fscanf(in, "%u", &num_paths); fscanf(in, "%u", &path_len); path_len += 1; fwrite(&num_paths, sizeof(unsigned int), 1, out); fwrite(&type, sizeof(unsigned int), 1, out); fwrite(&path_len, sizeof(unsigned int), 1, out); switch (type) { case VAL_AVG: type_size = 1; func = img_avg; break; case VAL_UINT_RGB: type_size = 4; func = img_uint; case VAL_AVG_COL_4: type_size = 12; func = img_avg_col_4; } val = malloc(type_size); /* Read the trailing newlinew after path len */ fgets(buff, FILENAME_MAX, in); for (i = 0; i < num_paths; ++i) { fgets(buff, path_len + 2, in); buff[strlen(buff)-1] = 0; buff[path_len] = 0; printf("%s", buff); fwrite(buff, sizeof(char), path_len, out); img = img_new(buff); func(img, val); img_free(img); fwrite(val, type_size, 1, out); } fclose(in); fclose(out); return 0; }
int main(int argc, char **argv) { // bmp_t *b1, *b2, *bout; img_t *i1, *i2, **m; FILE *f; double zweight; int diameter; img_pyr_t *a_imgpyr, *b_imgpyr; #if 1 double tmin = 0, tmax = 1.0; int i, steps = 32; #else double tmin = 0.5, tmax = 0.5; int i, steps = 1; #endif img_dist_pyr_t *apyr, *bpyr; if (argc != 8) { printf("Usage: morphimg2 <zweight> <diameter> <in1.bmp> <in2.bmp> <in1.pyr> <in2.pyr> <out.bmp>\n"); return 1; } zweight = atof(argv[1]); diameter = atoi(argv[2]); #if 0 /* Read the first bitmap */ f = fopen(argv[3], "r"); if (f == NULL) { printf("Could not open file %s for reading\n", argv[3]); return 1; } b1 = read_bmp(f); fclose(f); /* Read the second bitmap */ f = fopen(argv[4], "r"); if (f == NULL) { printf("Could not open file %s for reading\n", argv[4]); return 1; } b2 = read_bmp(f); fclose(f); /* Convert the bitmaps to images */ if (b1 == NULL || b2 == NULL) { printf("Error reading bitmaps\n"); return 1; } i1 = bmp2img(b1), i2 = bmp2img(b2); if (i1 == NULL || i2 == NULL) { printf("Error in bmp2img conversion\n"); return 1; } #endif i1 = img_read_bmp_file(argv[3]); i2 = img_read_bmp_file(argv[4]); /* Read the first map */ f = open_file(argv[5], "r"); apyr = img_read_distance_pyramid(f); fclose(f); /* Read the second map */ f = open_file(argv[6], "r"); bpyr = img_read_distance_pyramid(f); fclose(f); set_ann_z_weight(zweight); a_imgpyr = img_create_gaussian_pyramid(i1, 0); b_imgpyr = img_create_gaussian_pyramid(i2, 0); #if 1 m = img_morph3(i1, i2, diameter, &(apyr->dmaps[0]), &(bpyr->dmaps[0]), zweight, 1, tmin, tmax, steps); for (i = 0; i < steps; i++) { char outfile[64]; #if 0 bout = img2bmp(m[i]); if (bout == NULL) { printf("Error in img2bmp conversion\n"); return 1; } #endif sprintf(outfile, "%s%03d.bmp", argv[7], i); f = fopen(outfile, "w"); if (f == NULL) { printf("Error opening %s for writing\n", outfile); return 1; } // write_bmp(f, bout); // fclose(f); img_write_bmp_file(m[i], outfile); // free_bmp(bout); } #else iout = img_morph(i1, i2); bout = img2bmp(iout); if (bout == NULL) { printf("Error in img2bmp conversion\n"); return 1; } f = fopen(argv[7], "w"); if (f == NULL) { printf("Error opening %s for writing\n", argv[7]); return 1; } write_bmp(f, bout); fclose(f); free_bmp(bout); #endif // free_bmp(b1); // free_bmp(b2); img_free(i1); img_free(i2); return 0; }
/* * frames input ...: emit events describing game state changes in video frames */ static int cmd_frames(int argc, char *argv[]) { DIR *dirp; struct dirent *entp; int nframes, rv, i, len; kv_emit_f emit; char c; char *q; img_t *image; kv_vidctx_t *kvp; char *framenames[MAX_FRAMES]; emit = kv_screen_print; while ((c = getopt(argc, argv, "j")) != -1) { switch (c) { case 'j': emit = kv_screen_json; break; case '?': default: return (EXIT_USAGE); } } argc -= optind; argv += optind; if (argc < 1) { warnx("missing directory name"); return (EXIT_USAGE); } if ((kvp = kv_vidctx_init(dirname((char *)kv_arg0), emit, NULL)) == NULL) return (EXIT_FAILURE); if ((dirp = opendir(argv[0])) == NULL) { kv_vidctx_free(kvp); warn("failed to opendir %s", argv[0]); return (EXIT_USAGE); } nframes = 0; rv = EXIT_FAILURE; while ((entp = readdir(dirp)) != NULL) { if (nframes >= MAX_FRAMES) { warnx("max %d frames supported", MAX_FRAMES); break; } if (strcmp(entp->d_name, ".") == 0 || strcmp(entp->d_name, "..") == 0) continue; if (strcmp(entp->d_name + strlen(entp->d_name) - sizeof (".png") + 1, ".png") != 0) continue; len = snprintf(NULL, 0, "%s/%s", argv[0], entp->d_name); if ((q = malloc(len + 1)) == NULL) { warn("malloc"); break; } (void) snprintf(q, len + 1, "%s/%s", argv[0], entp->d_name); framenames[nframes++] = q; } (void) closedir(dirp); if (entp != NULL) goto out; rv = EXIT_SUCCESS; qsort(framenames, nframes, sizeof (framenames[0]), qsort_strcmp); for (i = 0; i < nframes; i++) { image = img_read(framenames[i]); if (image == NULL) { warnx("failed to read %s", argv[i]); continue; } kv_vidctx_frame(framenames[i], i, i / KV_FRAMERATE * MILLISEC, image, kvp); img_free(image); } out: kv_vidctx_free(kvp); for (i = 0; i < nframes; i++) free(framenames[i]); return (rv); }