Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef __TGAH
- #define __TGAH
- #define __TGAH_verbose 0
- #include <inttypes.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- /* 17-Oct-2018 added a way to make a new TGA with the same settings as an existing tga */
- /* 18-Oct-2018 added rect data type and blit function */
- /* 29-Jan-2021 added _t to pixel types, tga_free now returns a pointer */
- /* 18-Nov-2023 added support for 8bpp grayscale TGAs */
- /* 31-Mar-2024 moved from color structs to uint32_t color */
- /* 10-Oct-2024 added line drawing to the base library */
- /* 26-Oct-2024 added optimized 8bpp-specific functions */
- /*
- simple TGA header
- /---------------- uint8_t pixel type, 2 for direct RGB
- | /---------- int16_t X origin, pretty much always 0
- | | /-------- int16_t Y origin, pretty much always == height
- | | | /------ uint16_t width
- | | | | /---- uint16_t height
- | | | | | /-- uint8_t bits-per-pixel
- | | | | | |/- uint8_t bit-field: 00S0BBBB B=bits per alpha S=screen origin
- | | | | | ||
- 00P00000XXYYWWHHBC
- RGBA values are stored as BGRA
- */
- typedef uint32_t color_t;
- #ifndef __sdl_gen_h
- // The 8 digital colors
- #define c_black (0x000000FF)
- #define c_blue (0x0000FFFF)
- #define c_green (0x00FF00FF)
- #define c_cyan (0x00FFFFFF)
- #define c_red (0xFF0000FF)
- #define c_pink (0xFF00FFFF)
- #define c_yellow (0xFFFF00FF)
- #define c_white (0xFFFFFFFF)
- // various shades of gray
- #define c_gray (0xBCBCBCFF)
- #define c_gray25 (0x808080FF)
- #define c_gray50 (0x404040FF)
- #endif
- typedef struct tga_t tga_t;
- typedef struct tga_rect_t tga_rect_t;
- typedef struct tga_info_t tga_info_t;
- struct tga_rect_t {
- int16_t x;
- int16_t y;
- uint16_t w;
- uint16_t h;
- };
- struct tga_info_t {
- tga_rect_t rect;
- _Bool inverted;
- uint8_t pixel_type, bits_per_pixel, bits_per_alpha;
- };
- struct tga_t { /* P=pixel type X=x origin Y=y origin W=width H=height B=bpp A=bpa */
- uint16_t w; // width
- uint16_t h; // height
- uint8_t bpp; // bits per pixel (always 24 or 32, or 8 for grayscale)
- uint8_t* pixels; // either RGB8 or RGBA8, or Y8
- uint8_t ext_pixels :1; // if set, the pixel buffer is external and shouldn't be freed
- uint8_t inverted :1; // if set, the image is stored upside down
- size_t refcount;
- };
- // color_t tga_new_color( uint8_t r, uint8_t g, uint8_t b, uint8_t a );
- #define tga_new_color( r, g, b, a ) ( ((((color_t)(r))<<24)&0xFF000000) | ((((color_t)(g))<<16)&0xFF0000) | ((((color_t)(b))<<8)&0xFF00) | (((color_t)(a))&0xFF) )
- color_t tga_new_color_from_shade( uint8_t s );
- // make a new TGA
- tga_t* tga_new( uint16_t w, uint16_t h, uint8_t bpp );
- // to make a new TGA with the same size, pass 0 to w and h
- tga_t* tga_new_formatted( uint16_t w, uint16_t h, tga_t* tga_ref );
- tga_t* tga_new_from_data( uint16_t w, uint16_t h, uint8_t bpp, uint8_t inverted, uint8_t* data );
- tga_t* tga_free( tga_t* t ); // frees a TGA
- color_t tga_pixel_get( tga_t* t, uint16_t x, uint16_t y );
- void tga_pixel_set( tga_t* t, uint16_t x, uint16_t y, color_t c );
- uint8_t tga_pixel_get8( tga_t* t, uint16_t x, uint32_t y );
- void tga_pixel_set8( tga_t* t, uint16_t x, uint32_t y, uint8_t c );
- // returns 0 on success, -1 on failure
- int tga_blit( tga_t* source, tga_t* dest, tga_rect_t rect_src, tga_rect_t rect_dest );
- int tga_blit_simple( tga_t* source, tga_t* dest, int dest_x, int dest_y );
- int tga_blit_simple_fast( tga_t* source, tga_t* dest );
- int tga_rect_fill( tga_t* img, tga_rect_t rect, color_t c );
- int tga_fill( tga_t* img, color_t c );
- int tga_fill8( tga_t* img, uint8_t c );
- void tga_line( int32_t x0, int32_t y0, int32_t x1, int32_t y1, color_t color, tga_t* tga );
- void tga_line_thick( int32_t x0, int32_t y0, int32_t x1, int32_t y1, color_t color, tga_t* tga );
- void tga_circle( tga_t* tga, int x0, int y0, int radius, color_t color );
- void tga_circle_filled( tga_t* tga, int x0, int y0, int radius, color_t color );
- void tga_copy( tga_t* source, tga_t* dest );
- tga_t* tga_load( char* fname );
- void tga_save( char* fname, tga_t* t );
- void print_tga_info( tga_t* t );
- void tga_vflip( tga_t* t );
- tga_info_t tga_load_info( char* fname );
- #define tga_info_bad( tga_info ) ((tga_info).rect.w == 0)
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement