Advertisement
TerusTheBird

masker: tga_v4.h

Jan 7th, 2025
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.26 KB | Source Code | 0 0
  1. #ifndef __TGAH
  2. #define __TGAH
  3.  
  4. #define __TGAH_verbose 0
  5.  
  6. #include <inttypes.h>
  7. #include <stdlib.h>
  8. #include <stdio.h>
  9. #include <string.h>
  10.  
  11. /* 17-Oct-2018  added a way to make a new TGA with the same settings as an existing tga */
  12. /* 18-Oct-2018  added rect data type and blit function */
  13. /* 29-Jan-2021  added _t to pixel types, tga_free now returns a pointer */
  14. /* 18-Nov-2023  added support for 8bpp grayscale TGAs */
  15. /* 31-Mar-2024  moved from color structs to uint32_t color */
  16. /* 10-Oct-2024  added line drawing to the base library */
  17. /* 26-Oct-2024  added optimized 8bpp-specific functions */
  18.  
  19. /*
  20. simple TGA header
  21.   /---------------- uint8_t  pixel type, 2 for direct RGB
  22.   |     /---------- int16_t  X origin, pretty much always 0
  23.   |     | /-------- int16_t  Y origin, pretty much always == height
  24.   |     | | /------ uint16_t width
  25.   |     | | | /---- uint16_t height
  26.   |     | | | | /-- uint8_t  bits-per-pixel
  27.   |     | | | | |/- uint8_t  bit-field: 00S0BBBB   B=bits per alpha  S=screen origin
  28.   |     | | | | ||
  29. 00P00000XXYYWWHHBC
  30.  
  31. RGBA values are stored as BGRA
  32. */
  33.  
  34. typedef uint32_t color_t;
  35.  
  36. #ifndef __sdl_gen_h
  37. // The 8 digital colors
  38. #define c_black  (0x000000FF)
  39. #define c_blue   (0x0000FFFF)
  40. #define c_green  (0x00FF00FF)
  41. #define c_cyan   (0x00FFFFFF)
  42. #define c_red    (0xFF0000FF)
  43. #define c_pink   (0xFF00FFFF)
  44. #define c_yellow (0xFFFF00FF)
  45. #define c_white  (0xFFFFFFFF)
  46.  
  47. // various shades of gray
  48. #define c_gray   (0xBCBCBCFF)
  49. #define c_gray25 (0x808080FF)
  50. #define c_gray50 (0x404040FF)
  51. #endif
  52.  
  53. typedef struct tga_t      tga_t;
  54. typedef struct tga_rect_t tga_rect_t;
  55. typedef struct tga_info_t tga_info_t;
  56.  
  57. struct tga_rect_t {
  58.     int16_t  x;
  59.     int16_t  y;
  60.     uint16_t w;
  61.     uint16_t h;
  62. };
  63.  
  64. struct tga_info_t {
  65.     tga_rect_t rect;
  66.     _Bool inverted;
  67.     uint8_t pixel_type, bits_per_pixel, bits_per_alpha;
  68. };
  69.  
  70. struct tga_t { /*  P=pixel type X=x origin Y=y origin W=width H=height B=bpp A=bpa */
  71.     uint16_t w;              // width
  72.     uint16_t h;              // height
  73.     uint8_t  bpp;            // bits per pixel (always 24 or 32, or 8 for grayscale)
  74.     uint8_t* pixels;         // either RGB8 or RGBA8, or Y8
  75.     uint8_t  ext_pixels  :1; // if set, the pixel buffer is external and shouldn't be freed
  76.     uint8_t  inverted    :1; // if set, the image is stored upside down
  77.     size_t   refcount;
  78. };
  79.  
  80. // color_t tga_new_color( uint8_t r, uint8_t g, uint8_t b, uint8_t a );
  81. #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) )
  82.  
  83. color_t tga_new_color_from_shade( uint8_t s );
  84.  
  85. // make a new TGA
  86. tga_t* tga_new( uint16_t w, uint16_t h, uint8_t bpp );
  87.  
  88. // to make a new TGA with the same size, pass 0 to w and h
  89. tga_t* tga_new_formatted( uint16_t w, uint16_t h, tga_t* tga_ref );
  90.  
  91.  
  92. tga_t* tga_new_from_data( uint16_t w, uint16_t h, uint8_t bpp, uint8_t inverted, uint8_t* data );
  93.  
  94. tga_t* tga_free( tga_t* t ); // frees a TGA
  95.  
  96. color_t tga_pixel_get( tga_t* t, uint16_t x, uint16_t y );
  97. void  tga_pixel_set( tga_t* t, uint16_t x, uint16_t y, color_t c );
  98.  
  99. uint8_t tga_pixel_get8( tga_t* t, uint16_t x, uint32_t y );
  100. void tga_pixel_set8( tga_t* t, uint16_t x, uint32_t y, uint8_t c );
  101.  
  102. // returns 0 on success, -1 on failure
  103. int tga_blit( tga_t* source, tga_t* dest, tga_rect_t rect_src, tga_rect_t rect_dest );
  104. int tga_blit_simple( tga_t* source, tga_t* dest, int dest_x, int dest_y );
  105. int tga_blit_simple_fast( tga_t* source, tga_t* dest );
  106.  
  107. int tga_rect_fill( tga_t* img, tga_rect_t rect, color_t c );
  108. int tga_fill( tga_t* img, color_t c );
  109.  
  110. int tga_fill8( tga_t* img, uint8_t c );
  111.  
  112. void tga_line( int32_t x0, int32_t y0, int32_t x1, int32_t y1, color_t color, tga_t* tga );
  113. void tga_line_thick( int32_t x0, int32_t y0, int32_t x1, int32_t y1, color_t color, tga_t* tga );
  114.  
  115. void tga_circle(        tga_t* tga, int x0, int y0, int radius, color_t color );
  116. void tga_circle_filled( tga_t* tga, int x0, int y0, int radius, color_t color );
  117.  
  118. void tga_copy( tga_t* source, tga_t* dest );
  119.  
  120. tga_t* tga_load( char* fname );
  121. void   tga_save( char* fname, tga_t* t );
  122. void print_tga_info( tga_t* t );
  123.  
  124. void tga_vflip( tga_t* t );
  125.  
  126. tga_info_t tga_load_info( char* fname );
  127. #define tga_info_bad( tga_info )  ((tga_info).rect.w == 0)
  128.  
  129. #endif
  130.  
  131.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement