1

我有多个 tiff 图像存储在一个 zip 文件中,并想在 c 中读取它们的像素值。我对c很陌生,所以请原谅狡猾的代码。我已经到了拥有char *tiff 文件内容的地步,但似乎无法弄清楚现在如何使用 libtiff(或类似的东西)处理它。libtiff 似乎要求我通过 TIFFOpen 一个文件名来打开。我可以将 tiff 写入临时文件,但感觉必须有更有效的方法。

到目前为止,我有:

#include <stdlib.h>
#include <stdio.h>
#include <zip.h>
#include <string.h>

int main()
{
    //Open the ZIP archive
    int err = 0;
    struct zip *z = zip_open("test.zip", 0, &err);

    // Determine how many files are inside and iterate through them
    int num_files = zip_get_num_entries(z, 0);
    printf("%u\n", num_files);

    int i;
    for (i=0; i < num_files; i++)
    {
        const char * filename;
        filename = zip_get_name(z, i, 0);

        // If the file name ends in .tif
        if (strlen(filename) > 4 && !strcmp(filename + strlen(filename) - 4, ".tif"))
        {
            printf("%s\n", filename);

            // Get information about file
            struct zip_stat st;
            zip_stat_init(&st);
            zip_stat(z, name, 0, &st);
            printf("%lld\n", st.size);

            // Allocate memory for decompressed contents
            char *contents;
            contents = (char *)malloc(st.size);

            // Read the file
            struct zip_file *f = zip_fopen(z, filename, 0);
            zip_fread(f, contents, st.size);
            zip_fclose(f);

            // Do something with the contents

            // Free memory
            free(contents);
        }
    }
    //And close the archive
    zip_close(z);
}

编辑:我的问题与这个问题类似,但那里接受的答案与 c++ 有关,我不知道如何将它翻译成直接 c。

4

0 回答 0