libgta 1.0.7
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_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;
}
The GTA header.
Definition: gta.hpp:711
type component_type(uintmax_t i) const
Get the component type.
Definition: gta.hpp:983
uintmax_t dimensions() const
Get the number of dimensions.
Definition: gta.hpp:1022
const taglist & component_taglist(uintmax_t i) const
Get the component tag list.
Definition: gta.hpp:1003
void set_compression(gta::compression compression)
Set the compression.
Definition: gta.hpp:1096
void write_data(custom_io &io, const void *data) const
Write the complete data.
Definition: gta.hpp:1442
void read_data(custom_io &io, void *data) const
Read the complete data.
Definition: gta.hpp:1314
void set_components(uintmax_t n, const type *types, const uintmax_t *sizes=NULL)
Set the components of an array element.
Definition: gta.hpp:1122
uintmax_t dimension_size(uintmax_t i) const
Get the size in a dimension.
Definition: gta.hpp:1032
void set_dimensions(uintmax_t n, const uintmax_t *sizes)
Set the dimensions.
Definition: gta.hpp:1223
uintmax_t data_size() const
Get the total size of the array.
Definition: gta.hpp:1070
const void * element(const void *data, const uintmax_t *indices) const
Get an array element.
Definition: gta.hpp:1596
void write_to(custom_io &io) const
Write a header.
Definition: gta.hpp:885
void read_from(custom_io &io)
Read a header.
Definition: gta.hpp:828
uintmax_t components() const
Get the number of components.
Definition: gta.hpp:973
void set(const char *name, const char *value)
Set a tag.
Definition: gta.hpp:334
The libgta C++ interface.
@ uint8
uint8_t
Definition: gta.hpp:124
@ bzip2
BZIP2 compression (moderate speed, good compression rates)
Definition: gta.hpp:168