HelenOS sources
This source file includes following definitions.
- decode_tga_header
- decode_tga
#include <stdlib.h>
#include <byteorder.h>
#include <align.h>
#include <stdbool.h>
#include <pixconv.h>
#include <gfx/bitmap.h>
#include <gfximage/tga.h>
#include <io/pixelmap.h>
typedef struct {
uint8_t id_length;
uint8_t cmap_type;
uint8_t img_type;
uint16_t cmap_first_entry;
uint16_t cmap_entries;
uint8_t cmap_bpp;
uint16_t startx;
uint16_t starty;
uint16_t width;
uint16_t height;
uint8_t img_bpp;
uint8_t img_descr;
} __attribute__((packed)) tga_header_t;
typedef enum {
CMAP_NOT_PRESENT = 0,
CMAP_PRESENT = 1,
CMAP_RESERVED_START = 2,
CMAP_PRIVATE_START = 128
} cmap_type_t;
typedef enum {
IMG_EMTPY = 0,
IMG_CMAP = 1,
IMG_BGRA = 2,
IMG_GRAY = 3,
IMG_CMAP_RLE = 9,
IMG_BGRA_RLE = 10,
IMG_GRAY_RLE = 11
} img_type_t;
typedef struct {
cmap_type_t cmap_type;
img_type_t img_type;
uint16_t cmap_first_entry;
uint16_t cmap_entries;
uint8_t cmap_bpp;
uint16_t startx;
uint16_t starty;
uint16_t width;
uint16_t height;
uint8_t img_bpp;
uint8_t img_alpha_bpp;
uint8_t img_alpha_dir;
void *id_data;
size_t id_length;
void *cmap_data;
size_t cmap_length;
void *img_data;
size_t img_length;
} tga_t;
static bool decode_tga_header(void *data, size_t size, tga_t *tga)
{
if (size < sizeof(tga_header_t))
return false;
tga_header_t *head = (tga_header_t *) data;
tga->id_data = data + sizeof(tga_header_t);
tga->id_length = head->id_length;
if (size < sizeof(tga_header_t) + tga->id_length)
return false;
tga->cmap_type = head->cmap_type;
tga->img_type = head->img_type;
tga->cmap_first_entry = uint16_t_le2host(head->cmap_first_entry);
tga->cmap_entries = uint16_t_le2host(head->cmap_entries);
tga->cmap_bpp = head->cmap_bpp;
tga->cmap_data = tga->id_data + tga->id_length;
tga->cmap_length = ALIGN_UP(tga->cmap_entries * tga->cmap_bpp, 8) >> 3;
if (size < sizeof(tga_header_t) + tga->id_length +
tga->cmap_length)
return false;
tga->startx = uint16_t_le2host(head->startx);
tga->starty = uint16_t_le2host(head->starty);
tga->width = uint16_t_le2host(head->width);
tga->height = uint16_t_le2host(head->height);
tga->img_bpp = head->img_bpp;
tga->img_alpha_bpp = head->img_descr & 0x0f;
tga->img_alpha_dir = (head->img_descr & 0xf0) >> 4;
tga->img_data = tga->cmap_data + tga->cmap_length;
uint64_t length = (uint64_t) tga->width * tga->height * tga->img_bpp;
if (length & 0x7)
length += 8;
length >>= 3;
if (length > SIZE_MAX - sizeof(tga_header_t) - tga->id_length -
tga->cmap_length)
return false;
tga->img_length = length;
if (size < sizeof(tga_header_t) + tga->id_length +
tga->cmap_length + tga->img_length)
return false;
return true;
}
errno_t decode_tga(gfx_context_t *gc, void *data, size_t size,
gfx_bitmap_t **rbitmap, gfx_rect_t *rrect)
{
gfx_bitmap_params_t params;
gfx_bitmap_alloc_t alloc;
gfx_bitmap_t *bitmap = NULL;
pixelmap_t pixelmap;
errno_t rc;
tga_t tga;
if (!decode_tga_header(data, size, &tga))
return EINVAL;
switch (tga.cmap_type) {
case CMAP_NOT_PRESENT:
break;
default:
return ENOTSUP;
}
switch (tga.img_type) {
case IMG_BGRA:
if (tga.img_bpp != 24)
return ENOTSUP;
break;
case IMG_GRAY:
if (tga.img_bpp != 8)
return ENOTSUP;
break;
default:
return ENOTSUP;
}
if (tga.img_alpha_bpp != 0)
return ENOTSUP;
sysarg_t twidth = tga.startx + tga.width;
sysarg_t theight = tga.starty + tga.height;
gfx_bitmap_params_init(¶ms);
params.rect.p1.x = twidth;
params.rect.p1.y = theight;
rc = gfx_bitmap_create(gc, ¶ms, NULL, &bitmap);
if (rc != EOK)
return rc;
rc = gfx_bitmap_get_alloc(bitmap, &alloc);
if (rc != EOK) {
gfx_bitmap_destroy(bitmap);
return rc;
}
pixelmap.width = twidth;
pixelmap.height = theight;
pixelmap.data = alloc.pixels;
switch (tga.img_type) {
case IMG_BGRA:
for (sysarg_t y = tga.starty; y < theight; y++) {
for (sysarg_t x = tga.startx; x < twidth; x++) {
size_t offset =
((y - tga.starty) * tga.width + (x - tga.startx)) * 3;
pixel_t pixel =
bgr_888_2pixel(((uint8_t *) tga.img_data) + offset);
pixelmap_put_pixel(&pixelmap, x, theight - y - 1, pixel);
}
}
break;
case IMG_GRAY:
for (sysarg_t y = tga.starty; y < theight; y++) {
for (sysarg_t x = tga.startx; x < twidth; x++) {
size_t offset =
(y - tga.starty) * tga.width + (x - tga.startx);
pixel_t pixel =
gray_8_2pixel(((uint8_t *) tga.img_data) + offset);
pixelmap_put_pixel(&pixelmap, x, theight - y - 1, pixel);
}
}
break;
default:
break;
}
*rbitmap = bitmap;
*rrect = params.rect;
return EOK;
}
HelenOS homepage, sources at GitHub