#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>\n', argv[0]);
    exit(1);
}

tif = TIFFOpen(argv[1], 'r');
if (!tif) {
    fprintf(stderr, 'Error opening TIFF file\n');
    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\n');
    exit(1);
}

strip_size = TIFFStripSize(tif);
data = (float*) _TIFFmalloc(strip_size);
if (!data) {
    fprintf(stderr, 'Error allocating memory\n');
    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\n', total_fill_volume);
printf('Total cut volume: %llu cubic meters\n', total_cut_volume);

_TIFFfree(data);
TIFFClose(tif);

return 0;

}

如何在 Visual Studio 中运行代码:

  1. 打开 Visual Studio 软件。
  2. 点击 '文件' 菜单,选择 '新建' -> '项目'。
  3. 在 '新建项目' 对话框中,选择 'Visual C++' -> '空项目',设置项目名称和路径,点击 '确定' 按钮。
  4. 在 '解决方案资源管理器' 中,右键点击项目名称,选择 '添加' -> '现有项',选择要添加的源代码文件 (包含以上代码的 .c 文件)。
  5. 点击 '生成' 菜单,选择 '生成解决方案'。
  6. 在 '解决方案资源管理器' 中,右键点击项目名称,选择 '属性'。
  7. 在 '属性页' 中,选择 '配置属性' -> '常规',设置 '目标文件名' 和 '目标扩展名' (例如,将扩展名设置为 .exe)。
  8. 在 '属性页' 中,选择 '配置属性' -> 'C/C++' -> '常规',设置 '附加包含目录',将 LibTIFF 库的头文件路径包含进来。
  9. 在 '属性页' 中,选择 '配置属性' -> '链接器' -> '常规',设置 '附加库目录',将 LibTIFF 库的 .lib 文件路径包含进来。
  10. 在 '属性页' 中,选择 '配置属性' -> '链接器' -> '输入',在 '附加依赖项' 中添加 LibTIFF 库的 .lib 文件名 (例如,libtiff.lib)。
  11. 点击 '生成' 菜单,选择 '生成解决方案'。
  12. 在 '解决方案资源管理器' 中,右键点击项目名称,选择 '调试' -> '启动新实例'。
  13. 在弹出的对话框中,选择要运行的可执行文件,点击 '打开' 按钮。
  14. 程序会在控制台窗口中输出结果。

注意:

  • 在运行程序之前,请确保已正确安装 LibTIFF 库,并将库文件路径添加到项目属性中。
  • 将代码中的 ROWS 和 COLS 宏定义修改为实际 TIFF 图像的宽度和高度。
  • 将 PIXEL_SIZE 宏定义修改为实际像素的大小 (单位:米)。

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

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