Skip to content
/ tga Public

C++ library to read/write Truevision TGA/TARGA files

License

Notifications You must be signed in to change notification settings

aseprite/tga

Folders and files

NameName
Last commit message
Last commit date

Latest commit

cd3420c · Sep 20, 2023

History

23 Commits
Mar 30, 2021
Apr 7, 2021
Mar 5, 2021
Mar 5, 2022
Mar 29, 2021
Mar 5, 2022
Sep 20, 2023
Mar 5, 2022
Apr 7, 2021
Apr 7, 2021

Repository files navigation

Aseprite TGA Library

build MIT Licensed

Library to read/write Truevision TGA/TARGA files. Tested with libfuzzer.

Example:

#include "tga/tga.h"

#include <cstdio>
#include <vector>

int main(int argc, char* argv[])
{
  if (argc < 2)
    return 1;

  FILE* f = std::fopen(argv[1], "rb");
  tga::StdioFileInterface file(f);
  tga::Decoder decoder(&file);
  tga::Header header;
  if (!decoder.readHeader(header))
    return 2;

  tga::Image image;
  image.bytesPerPixel = header.bytesPerPixel();
  image.rowstride = header.width * header.bytesPerPixel();

  std::vector<uint8_t> buffer(image.rowstride * header.height);
  image.pixels = &buffer[0];

  if (!decoder.readImage(header, image, nullptr))
    return 3;

  // Optional post-process to fix the alpha channel in
  // some TGA files where alpha=0 for all pixels when
  // it shouldn't.
  decoder.postProcessImage(header, image);

  return 0;
}