Merge pull request #1984 from Kelimion/tga

TGA improvements
This commit is contained in:
Jeroen van Rijn
2022-08-28 21:58:20 +02:00
committed by GitHub
2 changed files with 113 additions and 35 deletions
+7 -2
View File
@@ -379,8 +379,13 @@ QOI_Info :: struct {
} }
TGA_Data_Type :: enum u8 { TGA_Data_Type :: enum u8 {
Uncompressed_RGB = 2, No_Image_Data = 0,
Compressed_RBB = 10, Uncompressed_Color_Mapped = 1,
Uncompressed_RGB = 2,
Uncompressed_Black_White = 3,
Compressed_Color_Mapped = 9,
Compressed_RGB = 10,
Compressed_Black_White = 11,
} }
TGA_Header :: struct #packed { TGA_Header :: struct #packed {
+106 -33
View File
@@ -17,8 +17,6 @@ import "core:bytes"
import "core:os" import "core:os"
import "core:compress" import "core:compress"
import "core:strings" import "core:strings"
import "core:fmt"
_ :: fmt
// TODO: alpha_premultiply support // TODO: alpha_premultiply support
@@ -142,15 +140,58 @@ load_from_context :: proc(ctx: ^$C, options := Options{}, allocator := context.a
header := image.read_data(ctx, image.TGA_Header) or_return header := image.read_data(ctx, image.TGA_Header) or_return
// Header checks // Header checks
rle_encoding := false rle_encoding := false
color_mapped := false
src_channels := 0
dest_depth := header.bits_per_pixel
dest_channels := 0
switch header.data_type_code { #partial switch header.data_type_code {
case .Compressed_RBB: rle_encoding = true // Supported formats: RGB(A), RGB(A) RLE
case .Uncompressed_RGB: case .Compressed_RGB:
case: return nil, .Unsupported_Format rle_encoding = true
case .Uncompressed_RGB:
// Intentionally blank
case .Uncompressed_Color_Mapped:
color_mapped = true
case:
return nil, .Unsupported_Format
} }
if header.bits_per_pixel != 24 && header.bits_per_pixel != 32 { if color_mapped {
if header.color_map_type != 1 {
return nil, .Unsupported_Format
}
dest_depth = header.color_map_depth
// Expect LUT entry index to be 8 bits
if header.bits_per_pixel != 8 || header.color_map_origin != 0 || header.color_map_length > 256 {
return nil, .Unsupported_Format
}
}
switch dest_depth {
case 15: // B5G5R5
src_channels = 2
dest_channels = 3
if color_mapped {
return nil, .Unsupported_Format
}
case 16: // B5G5R5A1
src_channels = 2
dest_channels = 3 // Alpha bit is dodgy in TGA, so we ignore it.
if color_mapped {
return nil, .Unsupported_Format
}
case 24: // RGB8
src_channels = 3 if !color_mapped else 1
dest_channels = 3
case 32: // RGBA8
src_channels = 4 if !color_mapped else 1
dest_channels = 4
case:
return nil, .Unsupported_Format return nil, .Unsupported_Format
} }
@@ -170,9 +211,8 @@ load_from_context :: proc(ctx: ^$C, options := Options{}, allocator := context.a
destroy(img) destroy(img)
} }
src_channels := int(header.bits_per_pixel) / 8
img.which = .TGA img.which = .TGA
img.channels = 4 if .alpha_add_if_missing in options else src_channels img.channels = 4 if .alpha_add_if_missing in options else dest_channels
img.channels = 3 if .alpha_drop_if_present in options else img.channels img.channels = 3 if .alpha_drop_if_present in options else img.channels
img.depth = 8 img.depth = 8
@@ -182,7 +222,7 @@ load_from_context :: proc(ctx: ^$C, options := Options{}, allocator := context.a
// Read Image ID if present // Read Image ID if present
image_id := "" image_id := ""
if _id, e := compress.read_slice(ctx, int(header.id_length)); e != .None { if _id, e := compress.read_slice(ctx, int(header.id_length)); e != .None {
return nil, .Corrupt return img, .Corrupt
} else { } else {
if .return_metadata in options { if .return_metadata in options {
id := strings.trim_right_null(string(_id)) id := strings.trim_right_null(string(_id))
@@ -190,6 +230,32 @@ load_from_context :: proc(ctx: ^$C, options := Options{}, allocator := context.a
} }
} }
color_map := make([]RGBA_Pixel, header.color_map_length)
defer delete(color_map)
if color_mapped {
switch header.color_map_depth {
case 24:
for i in 0..<header.color_map_length {
if lut, lut_err := compress.read_data(ctx, RGB_Pixel); lut_err != .None {
return img, .Corrupt
} else {
color_map[i].rgb = lut
color_map[i].a = 255
}
}
case 32:
for i in 0..<header.color_map_length {
if lut, lut_err := compress.read_data(ctx, RGBA_Pixel); lut_err != .None {
return img, .Corrupt
} else {
color_map[i] = lut
}
}
}
}
if .return_metadata in options { if .return_metadata in options {
info := new(image.TGA_Info) info := new(image.TGA_Info)
info.header = header info.header = header
@@ -204,22 +270,23 @@ load_from_context :: proc(ctx: ^$C, options := Options{}, allocator := context.a
return img, nil return img, nil
} }
if !resize(&img.pixels.buf, img.channels * img.width * img.height) { if !resize(&img.pixels.buf, dest_channels * img.width * img.height) {
return img, .Unable_To_Allocate_Or_Resize return img, .Unable_To_Allocate_Or_Resize
} }
origin_is_topleft := header.image_descriptor & IMAGE_DESCRIPTOR_TOPLEFT_MASK != 0 origin_is_top := header.image_descriptor & IMAGE_DESCRIPTOR_TOP_MASK != 0
origin_is_left := header.image_descriptor & IMAGE_DESCRIPTOR_RIGHT_MASK == 0
rle_repetition_count := 0 rle_repetition_count := 0
read_pixel := true read_pixel := true
is_packet_rle := false is_packet_rle := false
pixel: [4]u8 pixel: RGBA_Pixel
stride := img.width * img.channels stride := img.width * dest_channels
line := 0 if origin_is_topleft else img.height - 1 line := 0 if origin_is_top else img.height - 1
for _ in 0..<img.height { for _ in 0..<img.height {
offset := line * stride offset := line * stride + (0 if origin_is_left else (stride - dest_channels))
for _ in 0..<img.width { for _ in 0..<img.width {
// handle RLE decoding // handle RLE decoding
if rle_encoding { if rle_encoding {
@@ -243,27 +310,32 @@ load_from_context :: proc(ctx: ^$C, options := Options{}, allocator := context.a
if src_err != .None { if src_err != .None {
return img, .Corrupt return img, .Corrupt
} }
switch src_channels {
pixel[2] = src[0] case 1:
pixel[1] = src[1] // Color mapped
pixel[0] = src[2] pixel = color_map[src[0]].bgra
case 2:
pixel[3] = src_channels == 4 ? src[3] : 255 assert(dest_depth == 16)
if img.channels == 4 { v := int(src[0]) | int(src[1]) << 8
if src_channels == 4 { b := u8( v & 31) << 3
img.pixels.buf[offset:][3] = src[3] g := u8((v >> 5) & 31) << 3
} else { r := u8((v >> 10) & 31) << 3
img.pixels.buf[offset:][3] = 255 pixel = {r, g, b, 255}
} case 3:
pixel = {src[2], src[1], src[0], 255}
case 4:
pixel = {src[2], src[1], src[0], src[3]}
case:
return img, .Corrupt
} }
} }
// Write pixel // Write pixel
copy(img.pixels.buf[offset:], pixel[:img.channels]) copy(img.pixels.buf[offset:], pixel[:dest_channels])
offset += img.channels offset += dest_channels if origin_is_left else -dest_channels
rle_repetition_count -= 1 rle_repetition_count -= 1
} }
line += 1 if origin_is_topleft else -1 line += 1 if origin_is_top else -1
} }
return img, nil return img, nil
} }
@@ -310,7 +382,8 @@ destroy :: proc(img: ^Image) {
} }
IMAGE_DESCRIPTOR_INTERLEAVING_MASK :: (1<<6) | (1<<7) IMAGE_DESCRIPTOR_INTERLEAVING_MASK :: (1<<6) | (1<<7)
IMAGE_DESCRIPTOR_TOPLEFT_MASK :: 1<<5 IMAGE_DESCRIPTOR_RIGHT_MASK :: 1<<4
IMAGE_DESCRIPTOR_TOP_MASK :: 1<<5
@(init, private) @(init, private)
_register :: proc() { _register :: proc() {