- fclose(fp);
-}
-
-void finish_one_read(GLuint width, GLuint height)
-{
- assert(!reads_in_progress.empty());
- ReadInProgress read = reads_in_progress.front();
- reads_in_progress.pop_front();
-
- unique_ptr<float[]> flow(new float[width * height * 2]);
- void *buf = glMapNamedBufferRange(read.pbo, 0, width * height * 2 * sizeof(float), GL_MAP_READ_BIT); // Blocks if the read isn't done yet.
- memcpy(flow.get(), buf, width * height * 2 * sizeof(float));
- glUnmapNamedBuffer(read.pbo);
- spare_pbos.push(read.pbo);
-
- flip_coordinate_system(flow.get(), width, height);
- if (!read.flow_filename.empty()) {
- write_flow(read.flow_filename.c_str(), flow.get(), width, height);
- fprintf(stderr, "%s %s -> %s\n", read.filename0.c_str(), read.filename1.c_str(), read.flow_filename.c_str());
- }
- if (!read.ppm_filename.empty()) {
- write_ppm(read.ppm_filename.c_str(), flow.get(), width, height);
- }
-}
-
-void schedule_read(GLuint tex, GLuint width, GLuint height, const char *filename0, const char *filename1, const char *flow_filename, const char *ppm_filename)
-{
- if (spare_pbos.empty()) {
- finish_one_read(width, height);
- }
- assert(!spare_pbos.empty());
- reads_in_progress.emplace_back(ReadInProgress{ spare_pbos.top(), filename0, filename1, flow_filename, ppm_filename });
- glBindBuffer(GL_PIXEL_PACK_BUFFER, spare_pbos.top());
- spare_pbos.pop();
- glGetTextureImage(tex, 0, GL_RG, GL_FLOAT, width * height * 2 * sizeof(float), nullptr);
- glBindBuffer(GL_PIXEL_PACK_BUFFER, 0);
-}
-
-void compute_flow_only(int argc, char **argv, int optind)
-{
- const char *filename0 = argc >= (optind + 1) ? argv[optind] : "test1499.png";
- const char *filename1 = argc >= (optind + 2) ? argv[optind + 1] : "test1500.png";
- const char *flow_filename = argc >= (optind + 3) ? argv[optind + 2] : "flow.flo";
-
- // Load pictures.
- unsigned width1, height1, width2, height2;
- GLuint tex0 = load_texture(filename0, &width1, &height1, WITHOUT_MIPMAPS);
- GLuint tex1 = load_texture(filename1, &width2, &height2, WITHOUT_MIPMAPS);
-
- if (width1 != width2 || height1 != height2) {
- fprintf(stderr, "Image dimensions don't match (%dx%d versus %dx%d)\n",
- width1, height1, width2, height2);
- exit(1);
- }
-
- // Set up some PBOs to do asynchronous readback.
- GLuint pbos[5];
- glCreateBuffers(5, pbos);
- for (int i = 0; i < 5; ++i) {
- glNamedBufferData(pbos[i], width1 * height1 * 2 * sizeof(float), nullptr, GL_STREAM_READ);
- spare_pbos.push(pbos[i]);
- }
-
- int levels = find_num_levels(width1, height1);
- GLuint tex0_gray, tex1_gray;
- glCreateTextures(GL_TEXTURE_2D, 1, &tex0_gray);
- glCreateTextures(GL_TEXTURE_2D, 1, &tex1_gray);
- glTextureStorage2D(tex0_gray, levels, GL_R8, width1, height1);
- glTextureStorage2D(tex1_gray, levels, GL_R8, width1, height1);
-
- GrayscaleConversion gray;
- gray.exec(tex0, tex0_gray, width1, height1);
- glDeleteTextures(1, &tex0);
- glGenerateTextureMipmap(tex0_gray);
-
- gray.exec(tex1, tex1_gray, width1, height1);
- glDeleteTextures(1, &tex1);
- glGenerateTextureMipmap(tex1_gray);
-
- DISComputeFlow compute_flow(width1, height1);
- GLuint final_tex = compute_flow.exec(tex0_gray, tex1_gray, DISComputeFlow::RESIZE_FLOW_TO_FULL_SIZE);
-
- schedule_read(final_tex, width1, height1, filename0, filename1, flow_filename, "flow.ppm");
- compute_flow.release_texture(final_tex);
-
- // See if there are more flows on the command line (ie., more than three arguments),
- // and if so, process them.
- int num_flows = (argc - optind) / 3;
- for (int i = 1; i < num_flows; ++i) {
- const char *filename0 = argv[optind + i * 3 + 0];
- const char *filename1 = argv[optind + i * 3 + 1];
- const char *flow_filename = argv[optind + i * 3 + 2];
- GLuint width, height;
- GLuint tex0 = load_texture(filename0, &width, &height, WITHOUT_MIPMAPS);
- if (width != width1 || height != height1) {
- fprintf(stderr, "%s: Image dimensions don't match (%dx%d versus %dx%d)\n",
- filename0, width, height, width1, height1);
- exit(1);
- }
- gray.exec(tex0, tex0_gray, width, height);
- glGenerateTextureMipmap(tex0_gray);
- glDeleteTextures(1, &tex0);
-
- GLuint tex1 = load_texture(filename1, &width, &height, WITHOUT_MIPMAPS);
- if (width != width1 || height != height1) {
- fprintf(stderr, "%s: Image dimensions don't match (%dx%d versus %dx%d)\n",
- filename1, width, height, width1, height1);
- exit(1);
- }
- gray.exec(tex1, tex1_gray, width, height);
- glGenerateTextureMipmap(tex1_gray);
- glDeleteTextures(1, &tex1);