'How to open fli files

I'm new to C++ and was tasked with processing a fli file, but have no idea how to open them correctly. So far my code looks like this:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
    fstream newfile;
    newfile.open("testvid.fli", ios::in);object
    if (newfile.is_open()) {
        string tp;
        while (getline(newfile, tp)) {
            cout << tp << "\n";
        }
        newfile.close();
    }
    std::cin.ignore();
}

But it gives me gibberish. Can anyone help?



Solution 1:[1]

I haven't worked with .fli files before. Is it a FLIC file (used to store animations)? Then it makes sense that trying to reading them as strings produces gibberish. You could try either the Aseprite FLIC Library or LibFLIC.

EDIT: I used the Asperite's library and gif-h to convert a FLIC file to a GIF. Sorry for the inefficient code -- it's just a quick code to make it work.

#include <iostream>
#include <iomanip>

#include <cstdio>
#include <vector>
#include <string>

#include <gif.h>
#include <flic.h>


int main() {
    std::string fname_input { "../data/2noppaa.fli" };
    std::string fname_output { "../data/2noppaa.gif" };

    // Set up FLIC file
    FILE *f = std::fopen(fname_input.c_str(), "rb");
    flic::StdioFileInterface file(f);
    flic::Decoder decoder(&file);
    flic::Header header;

    if (!decoder.readHeader(header)) {
        std::cout << "Error: could not read header of FLIC file." << std::endl;
        return 2;
    }

    const size_t frame_size = header.width * header.height;

    // Set up FLIC reader
    std::vector<uint8_t> buffer(frame_size);
    flic::Frame frame;
    frame.pixels = &buffer[0];
    frame.rowstride = header.width;

    // Set up GIF writer
    GifWriter g;
    GifBegin(&g, fname_output.c_str(), header.width, header.height, 0);

    std::vector<uint8_t> gif_frame(frame_size * 4, 255);
    flic::Color flic_color;

    for (int i = 0; i < header.frames; ++i) {
        if (!decoder.readFrame(frame)) {
            break;
        }

        // Convert FLIC frame to GIF
        for (size_t j = 0; j < frame_size; ++j) {
            flic_color = frame.colormap[ frame.pixels[j] ];

            gif_frame.at(j*4) = flic_color.r;
            gif_frame.at(j*4 + 1) = flic_color.g;
            gif_frame.at(j*4 + 2) = flic_color.b;
        }
        
        GifWriteFrame(&g, gif_frame.data(), header.width, header.height, 0);
    }

    GifEnd(&g);
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1