#include <stdio.h> #include <stdlib.h> #include <tiffio.h>

#define ROWS 327 #define COLS 486 #define PIXEL_SIZE 5

int main(int argc, char* argv[]) { TIFF *tif; float *data; uint32 width, height; uint16 bits_per_sample, samples_per_pixel; tsize_t strip_size; unsigned long long total_fill_volume = 0, total_cut_volume = 0; int i, j;

if (argc < 2) {
    fprintf(stderr, "Usage: %s <input file>

", argv[0]); exit(1); }

tif = TIFFOpen(argv[1], "r");
if (!tif) {
    fprintf(stderr, "Error opening TIFF file

"); exit(1); }

TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &width);
TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &height);
TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &bits_per_sample);
TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &samples_per_pixel);

if (width != COLS || height != ROWS || bits_per_sample != 32 || samples_per_pixel != 1) {
    fprintf(stderr, "Invalid TIFF format

"); exit(1); }

strip_size = TIFFStripSize(tif);
data = (float*) _TIFFmalloc(strip_size);
if (!data) {
    fprintf(stderr, "Error allocating memory

"); exit(1); }

for (i = 0; i < height; i++) {
    TIFFReadEncodedStrip(tif, TIFFComputeStrip(tif, i, 0), data, strip_size);
    for (j = 0; j < width; j++) {
        float elevation = data[j];
        if (elevation > 0) {
            total_fill_volume += (PIXEL_SIZE * PIXEL_SIZE * elevation);
        } else {
            total_cut_volume += (PIXEL_SIZE * PIXEL_SIZE * (-elevation));
        }
    }
}

printf("Total fill volume: %llu cubic meters

", total_fill_volume); printf("Total cut volume: %llu cubic meters ", total_cut_volume);

_TIFFfree(data);
TIFFClose(tif);

return 0;

}

// 在 VS 中运行该程序并输出填方挖方结果的步骤:

// 1. 打开 Visual Studio,选择“新建项目”,选择“空项目”。 // 2. 在“解决方案资源管理器”中,右键单击项目,选择“添加”->“现有项”,选择该程序的源代码文件。 // 3. 将输出结果的printf语句修改为如下格式: // // printf("Total fill volume: %llu cubic meters ", total_fill_volume/1000000); // printf("Total cut volume: %llu cubic meters ", total_cut_volume/1000000); // // 这将输出以百万立方米为单位的填方挖方结果。 // 4. 点击“生成”->“生成解决方案”,编译并生成可执行文件。 // 5. 将输入文件放置在可执行文件的同一目录下,打开命令提示符,进入该目录。 // 6. 运行可执行文件,输入文件名作为命令行参数,例如: // // FillCut.exe input.tif // // 7. 程序将输出填方挖方结果。

C++ TIFF 文件处理 - 计算填方挖方体积

原文地址: https://www.cveoy.top/t/topic/fVic 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录