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 <stdio.h>
#include <stdlib.h>
#include <gta/gta.h>
int main(void)
{
gta_type_t components[] = { GTA_UINT8, GTA_UINT8, GTA_UINT8 };
uintmax_t dimensions[] = { 256, 128 };
gta_header_t *header;
void *data;
uintmax_t x, y;
FILE *f;
/* Create a GTA that contains an RGB image with 256x128 pixels */
r = gta_create_header(&header);
if (r != GTA_OK) {
return 1;
}
r = gta_set_components(header, 3, components, NULL);
if (r != GTA_OK) {
return 1;
}
r = gta_set_dimensions(header, 2, dimensions);
if (r != GTA_OK) {
return 1;
}
data = malloc(gta_get_data_size(header));
if (!data) {
return 1;
}
for (y = 0; y < 128; y++) {
for (x = 0; x < 256; x++) {
uintmax_t indices[] = { x, y };
unsigned char *pixel = gta_get_element(header, data, indices);
pixel[0] = x;
pixel[1] = 2 * y;
pixel[2] = 128;
}
}
/* Set some tags (this is entirely optional) */
r = gta_set_tag(gta_get_component_taglist(header, 0), "INTERPRETATION", "RED");
if (r != GTA_OK) {
return 1;
}
r = gta_set_tag(gta_get_component_taglist(header, 1), "INTERPRETATION", "GREEN");
if (r != GTA_OK) {
return 1;
}
r = gta_set_tag(gta_get_component_taglist(header, 2), "INTERPRETATION", "BLUE");
if (r != GTA_OK) {
return 1;
}
/* Write the GTA to a file */
f = fopen("rgb.gta", "wb");
if (!f) {
return 1;
}
if (r != GTA_OK) {
return 1;
}
r = gta_write_data_to_stream(header, data, f);
if (r != GTA_OK) {
return 1;
}
if (fclose(f) != 0) {
return 1;
}
free(data);
/* Reread the same file */
f = fopen("rgb.gta", "rb");
if (!f) {
return 1;
}
r = gta_create_header(&header);
if (r != GTA_OK) {
return 1;
}
if (r != GTA_OK) {
return 1;
}
if (gta_get_components(header) != 3
|| gta_get_component_type(header, 2) != GTA_UINT8) {
return 1;
}
if (gta_get_dimensions(header) != 2
|| gta_get_dimension_size(header, 0) != 256
|| gta_get_dimension_size(header, 1) != 128) {
return 1;
}
data = malloc(gta_get_data_size(header));
if (!data) {
return 1;
}
r = gta_read_data_from_stream(header, data, f);
if (r != GTA_OK) {
return 1;
}
if (fclose(f) != 0) {
return 1;
}
free(data);
return 0;
}
The libgta C interface.
gta_type_t gta_get_component_type(const gta_header_t *restrict header, uintmax_t i)
Get the type of a component.
gta_result_t
GTA result type.
Definition gta.h:213
@ GTA_OK
Success / no error.
Definition gta.h:214
void gta_destroy_header(gta_header_t *restrict header)
Destroy a GTA header structure and free its resources.
gta_result_t gta_set_components(gta_header_t *restrict header, uintmax_t n, const gta_type_t *restrict types, const uintmax_t *restrict sizes)
Set the components of an array element.
uintmax_t gta_get_data_size(const gta_header_t *restrict header)
Get the total size of the array data.
uintmax_t gta_get_components(const gta_header_t *restrict header)
Get the number of components.
gta_result_t gta_write_header_to_stream(const gta_header_t *restrict header, FILE *restrict f)
Write a GTA header to a stream.
gta_result_t gta_write_data_to_stream(const gta_header_t *restrict header, const void *restrict data, FILE *restrict f)
Write the complete data to a stream.
gta_taglist_t * gta_get_component_taglist(gta_header_t *restrict header, uintmax_t i)
Get the tag list of a component (modifiable).
uintmax_t gta_get_dimension_size(const gta_header_t *restrict header, uintmax_t i)
Get the size in a dimension.
struct gta_internal_header_struct gta_header_t
The GTA header type.
Definition gta.h:193
gta_result_t gta_create_header(gta_header_t *restrict *restrict header)
Create a new GTA header structure and initialize it.
gta_result_t gta_set_tag(gta_taglist_t *restrict taglist, const char *restrict name, const char *restrict value)
Set a tag.
gta_result_t gta_read_header_from_stream(gta_header_t *restrict header, FILE *restrict f)
Read a GTA header from a stream.
gta_result_t gta_set_dimensions(gta_header_t *restrict header, uintmax_t n, const uintmax_t *restrict sizes)
Set the dimensions.
gta_result_t gta_read_data_from_stream(const gta_header_t *restrict header, void *restrict data, FILE *restrict f)
Read the complete data from a stream.
void * gta_get_element(const gta_header_t *restrict header, void *restrict data, const uintmax_t *restrict indices)
Get the address of an array element (modifiable).
gta_type_t
GTA data types.
Definition gta.h:269
@ GTA_UINT8
uint8_t
Definition gta.h:271
uintmax_t gta_get_dimensions(const gta_header_t *restrict header)
Get the number of dimensions.