libgta 1.2.1 Read and Write Generic Tagged Array (GTA) files

Examples written in C++: Introductory example
/* This file is in the public domain. */
#include <iostream>
#include <fstream>
#include <exception>
#include <vector>
#include <gta/gta.hpp>
int main(void)
{
try {
gta::header header;
/* Create a GTA that contains an RGB image with 256x128 pixels */
header.set_components(gta::uint8, gta::uint8, gta::uint8);
header.set_dimensions(256, 128);
std::vector<unsigned char> data(header.data_size());
for (uintmax_t y = 0; y < 128; y++) {
for (uintmax_t x = 0; x < 256; x++) {
unsigned char *pixel = static_cast<unsigned char *>(
header.element(&(data[0]), x, y));
pixel[0] = x;
pixel[1] = 2 * y;
pixel[2] = 128;
}
}
/* Set some tags (this is entirely optional) */
header.component_taglist(0).set("INTERPRETATION", "RED");
header.component_taglist(1).set("INTERPRETATION", "GREEN");
header.component_taglist(2).set("INTERPRETATION", "BLUE");
/* Write the GTA to a file */
std::ofstream ofs("rgb.gta", std::ios::out | std::ios::binary);
header.write_to(ofs);
header.write_data(ofs, &(data[0]));
ofs.close();
/* Reread the same file */
std::ifstream ifs("rgb.gta", std::ios::in | std::ios::binary);
header.read_from(ifs);
if (header.components() != 3
|| header.component_type(0) != gta::uint8
|| header.component_type(1) != gta::uint8
|| header.component_type(2) != gta::uint8) {
throw std::exception();
}
if (header.dimensions() != 2
|| header.dimension_size(0) != 256
|| header.dimension_size(1) != 128) {
throw std::exception();
}
header.read_data(ifs, &(data[0]));
}
catch (std::exception &e) {
std::cerr << e.what() << std::endl;
return 1;
}
return 0;
}
gta::header
The GTA header.
Definition: gta.hpp:702
gta::header::component_type
type component_type(uintmax_t i) const
Get the component type.
Definition: gta.hpp:974
gta::header::dimensions
uintmax_t dimensions() const
Get the number of dimensions.
Definition: gta.hpp:1013
gta::header::component_taglist
const taglist & component_taglist(uintmax_t i) const
Get the component tag list.
Definition: gta.hpp:994
gta::header::write_data
void write_data(custom_io &io, const void *data) const
Write the complete data.
Definition: gta.hpp:1434
gta::header::read_data
void read_data(custom_io &io, void *data) const
Read the complete data.
Definition: gta.hpp:1306
gta::header::set_components
void set_components(uintmax_t n, const type *types, const uintmax_t *sizes=NULL)
Set the components of an array element.
Definition: gta.hpp:1114
gta::header::dimension_size
uintmax_t dimension_size(uintmax_t i) const
Get the size in a dimension.
Definition: gta.hpp:1023
gta::header::set_dimensions
void set_dimensions(uintmax_t n, const uintmax_t *sizes)
Set the dimensions.
Definition: gta.hpp:1215
gta::header::data_size
uintmax_t data_size() const
Get the total size of the array.
Definition: gta.hpp:1061
gta::header::element
const void * element(const void *data, const uintmax_t *indices) const
Get an array element.
Definition: gta.hpp:1588
gta::header::write_to
void write_to(custom_io &io) const
Write a header.
Definition: gta.hpp:876
gta::header::read_from
void read_from(custom_io &io)
Read a header.
Definition: gta.hpp:819
gta::header::components
uintmax_t components() const
Get the number of components.
Definition: gta.hpp:964
gta::taglist::set
void set(const char *name, const char *value)
Set a tag.
Definition: gta.hpp:325
gta.hpp
The libgta C++ interface.
gta::uint8
@ uint8
uint8_t
Definition: gta.hpp:123