| |
| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
|
|
| |
| #define TAG_FLOAT 202021.25 |
| #define TAG_STRING "PIEH" |
|
|
|
|
| #include <stdio.h> |
| #include <stdlib.h> |
| #include <math.h> |
| #include "imageLib.h" |
| #include "flowIO.h" |
|
|
| |
| bool unknown_flow(float u, float v) { |
| return (fabs(u) > UNKNOWN_FLOW_THRESH) |
| || (fabs(v) > UNKNOWN_FLOW_THRESH) |
| || isnan(u) || isnan(v); |
| } |
|
|
| bool unknown_flow(float *f) { |
| return unknown_flow(f[0], f[1]); |
| } |
|
|
| |
| void ReadFlowFile(CFloatImage& img, const char* filename) |
| { |
| if (filename == NULL) |
| throw CError("ReadFlowFile: empty filename"); |
|
|
| char *dot = strrchr(filename, '.'); |
| if (strcmp(dot, ".flo") != 0) |
| throw CError("ReadFlowFile (%s): extension .flo expected", filename); |
|
|
| FILE *stream = fopen(filename, "rb"); |
| if (stream == 0) |
| throw CError("ReadFlowFile: could not open %s", filename); |
| |
| int width, height; |
| float tag; |
|
|
| if ((int)fread(&tag, sizeof(float), 1, stream) != 1 || |
| (int)fread(&width, sizeof(int), 1, stream) != 1 || |
| (int)fread(&height, sizeof(int), 1, stream) != 1) |
| throw CError("ReadFlowFile: problem reading file %s", filename); |
|
|
| if (tag != TAG_FLOAT) |
| throw CError("ReadFlowFile(%s): wrong tag (possibly due to big-endian machine?)", filename); |
|
|
| |
| if (width < 1 || width > 99999) |
| throw CError("ReadFlowFile(%s): illegal width %d", filename, width); |
|
|
| if (height < 1 || height > 99999) |
| throw CError("ReadFlowFile(%s): illegal height %d", filename, height); |
|
|
| int nBands = 2; |
| CShape sh(width, height, nBands); |
| img.ReAllocate(sh); |
|
|
| |
| int n = nBands * width; |
| for (int y = 0; y < height; y++) { |
| float* ptr = &img.Pixel(0, y, 0); |
| if ((int)fread(ptr, sizeof(float), n, stream) != n) |
| throw CError("ReadFlowFile(%s): file is too short", filename); |
| } |
|
|
| if (fgetc(stream) != EOF) |
| throw CError("ReadFlowFile(%s): file is too long", filename); |
|
|
| fclose(stream); |
| } |
|
|
| |
| void WriteFlowFile(CFloatImage img, const char* filename) |
| { |
| if (filename == NULL) |
| throw CError("WriteFlowFile: empty filename"); |
|
|
| char *dot = strrchr(filename, '.'); |
| if (dot == NULL) |
| throw CError("WriteFlowFile: extension required in filename '%s'", filename); |
|
|
| if (strcmp(dot, ".flo") != 0) |
| throw CError("WriteFlowFile: filename '%s' should have extension '.flo'", filename); |
|
|
| CShape sh = img.Shape(); |
| int width = sh.width, height = sh.height, nBands = sh.nBands; |
|
|
| if (nBands != 2) |
| throw CError("WriteFlowFile(%s): image must have 2 bands", filename); |
|
|
| FILE *stream = fopen(filename, "wb"); |
| if (stream == 0) |
| throw CError("WriteFlowFile: could not open %s", filename); |
|
|
| |
| fprintf(stream, TAG_STRING); |
| if ((int)fwrite(&width, sizeof(int), 1, stream) != 1 || |
| (int)fwrite(&height, sizeof(int), 1, stream) != 1) |
| throw CError("WriteFlowFile(%s): problem writing header", filename); |
|
|
| |
| int n = nBands * width; |
| for (int y = 0; y < height; y++) { |
| float* ptr = &img.Pixel(0, y, 0); |
| if ((int)fwrite(ptr, sizeof(float), n, stream) != n) |
| throw CError("WriteFlowFile(%s): problem writing data", filename); |
| } |
|
|
| fclose(stream); |
| } |
|
|
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|