SoFunction
Updated on 2024-12-11

How to save tiff files in Vitis IDE using the third-party library libtiff

Purpose and thinking

A Vitis IDE bare-metal project that needs to save video frames non-destructively

Since the pixel data of each frame is in 16bit 1 channel bayer format, the only image format that satisfies this requirement seems to be the tiff format. The open source tiff library is libtiff, and if you want to use it in the barebones project of Vitis IDE, you need to cross-compile the Vitis IDE barebones cpu is arm v7 cortex a9, and the compiler used isarm-none-eabiCompile libtiff in Ubuntu with the arm-none-eabi compiler that comes with the corresponding version of petalinux, and finally add and use the compiled third-party libraries in the Vitis IDE.libtiff You can save the tiff file

libtiff cross-compile

Download the source code, unzip to ubuntu and configure the Makefile command./configure --prefix=/home/hammer/tiff-4.5.0/install --host=arm-none-eabi CFLAGS="-fno-exceptions --specs= -mcpu=cortex-a9 -mfpu=vfpv3 -mfloat-abi=hard"after thatmake & make install

The result after compiling, moved to windows

在这里插入图片描述

Add libtiff to the Vitis IDE and compile it

Open the IDE, create a new application, right-click on properties

在这里插入图片描述

gcc compiler Configure Include, path Add path to compiled include

在这里插入图片描述

gcc linker configures Libraries, path adds the tiff libraries, -l adds thetiff respond in singingm

在这里插入图片描述

在这里插入图片描述

Write one to test that the libtiff library function works.

#include <>
#include <>
#include <>
#define WIDTH 640
#define HEIGHT 480
#define BITS_PER_SAMPLE 8
#define SAMPLES_PER_PIXEL 1
int main() {
    // Assuming the framebuffer is a two-dimensional array storing pixel data
    // Here we use dynamically allocated memory to simulate the frame buffer.
    unsigned char** frameBuffer = (unsigned char**)malloc(HEIGHT * sizeof(unsigned char*));
    for (int i = 0; i < HEIGHT; i++) {
        frameBuffer[i] = (unsigned char*)malloc(WIDTH * sizeof(unsigned char));
    }
    // Assuming the frame buffer has been filled with pixel data
    // Here is an example only, using a simple grayscale image
    for (int y = 0; y < HEIGHT; y++) {
        for (int x = 0; x < WIDTH; x++) {
            frameBuffer[y][x] = (unsigned char)(x % 256);
        }
    }
    // Create a pointer to the TIFF file
    TIFF* tiff = TIFFOpen("", "w");
    if (tiff == NULL) {
        printf("Unable to create TIFF file\n");
        return 1;
    }
    // Set the TIFF parameters
    TIFFSetField(tiff, TIFFTAG_IMAGEWIDTH, WIDTH);
    TIFFSetField(tiff, TIFFTAG_IMAGELENGTH, HEIGHT);
    TIFFSetField(tiff, TIFFTAG_BITSPERSAMPLE, BITS_PER_SAMPLE);
    TIFFSetField(tiff, TIFFTAG_SAMPLESPERPIXEL, SAMPLES_PER_PIXEL);
    TIFFSetField(tiff, TIFFTAG_COMPRESSION, COMPRESSION_NONE);
    TIFFSetField(tiff, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
    TIFFSetField(tiff, TIFFTAG_ROWSPERSTRIP, 1);
    // Write pixel data line by line
    for (int y = 0; y < HEIGHT; y++) {
        if (TIFFWriteScanline(tiff, frameBuffer[y], y, 0) < 0) {
            printf("Failed to write TIFF file\n");
            TIFFClose(tiff);
            return 1;
        }
    }
    // Close the TIFF file
    TIFFClose(tiff);
    // Free frame buffer memory
    for (int i = 0; i < HEIGHT; i++) {
        free(frameBuffer[i]);
    }
    free(frameBuffer);
    printf("The frame buffer has been saved as \n.");
    return 0;
}

The fact that it compiles successfully means it works

在这里插入图片描述

Problems encountered

straightforward./configure --prefix=/home/hammer/tiff-4.5.0/install --host=arm-none-eabi will report an error

report an error1: undefined reference to _exit

There are discussions in the stack overflow communityMakefile linking: undefined reference to _exit

Based on the discussion, add the following to CFLAGS-fno-exceptions --specs= can immediately (do sth)

report an error2:tiff_app.elf uses VFP register arguments, (tif_close.o) does not

This means that the compiler does not have VFP floating-point operations, so you have to add the

 -mcpu=cortex-a9 -mfpu=vfpv3 -mfloat-abi=hard

report an error3:\(tif_aux.o): in function TIFFDefaultTransferFunction: tif_aux.c:(.text+0x5e4): undefined reference to `pow’

In C, the pow function is a standard function located in the math library, indicating a lack of links to the math library so in the Libraries section, add the link flag-lm

To this point this article on the Vitis IDE in the use of third-party libraries libtiff save tiff file is introduced to this article, more related to the Vitis IDE use libtiff save tiff file content, please search for my previous articles or continue to browse the following related articles I hope you will have more support for me in the future!