1

I have multiple tiff images stored inside a zip file and would like to read their pixel values in c. I am very new to c so please forgive the dodgy code. I have got to the point where I have a char * with the contents of the tiff file but can't seem to work out how to now process that with libtiff (or something similar). libtiff seems to require that I pass TIFFOpen a filename to open. I could write the tiff to a temporary file but it feels like there must be a more efficient way.

So far I have:

#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);
}

EDIT: My question is similar to this one but the accepted answer there relates to c++ and I'm not sure how to translate it to straight c.

lac
  • 755
  • 10
  • 19
  • Save the tiff data to a temporary file and give it the path to that file? – Shawn Feb 20 '19 at 21:51
  • 1
    Possible duplicate of [C++ LibTiff - Read and Save file from and to Memory](https://stackoverflow.com/questions/4624144/c-libtiff-read-and-save-file-from-and-to-memory) – adrtam Feb 20 '19 at 21:52
  • @Shawn Yep I'm aware I can save to temporary file but I feel like thats likely to be slow compared to if I can do it all in memory. – lac Feb 20 '19 at 23:08
  • 2
    I'd say get it working first and see, especially if it only requires an hour or two of work to do so. – Michael Dorgan Feb 20 '19 at 23:23

0 Answers0