From 00f2e911a73e99b1283306272ff433984d90486c Mon Sep 17 00:00:00 2001 From: Benoit Jacquier Date: Sat, 27 Aug 2022 16:07:21 +0200 Subject: [PATCH 1/4] Add support for basic TGA loading --- core/image/common.odin | 6 ++ core/image/tga/tga.odin | 146 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 151 insertions(+), 1 deletion(-) diff --git a/core/image/common.odin b/core/image/common.odin index baacd64d9..beb3f93ee 100644 --- a/core/image/common.odin +++ b/core/image/common.odin @@ -61,6 +61,7 @@ Image_Metadata :: union #shared_nil { ^Netpbm_Info, ^PNG_Info, ^QOI_Info, + ^TGA_Info, } @@ -168,6 +169,7 @@ Error :: union #shared_nil { General_Image_Error :: enum { None = 0, + Unsupported_Option, // File I/O Unable_To_Read_File, Unable_To_Write_File, @@ -390,6 +392,10 @@ TGA_Header :: struct #packed { } #assert(size_of(TGA_Header) == 18) +TGA_Info :: struct { + header: TGA_Header, +} + // Function to help with image buffer calculations compute_buffer_size :: proc(width, height, channels, depth: int, extra_row_bytes := int(0)) -> (size: int) { size = ((((channels * width * depth) + 7) >> 3) + extra_row_bytes) * height diff --git a/core/image/tga/tga.odin b/core/image/tga/tga.odin index 67a088eb5..9b56b9db4 100644 --- a/core/image/tga/tga.odin +++ b/core/image/tga/tga.odin @@ -14,6 +14,11 @@ import "core:mem" import "core:image" import "core:bytes" import "core:os" +import "core:compress" + +// TODO: alpha_premultiply support +// TODO: RLE decompression + Error :: image.Error Image :: image.Image @@ -98,4 +103,143 @@ save_to_file :: proc(output: string, img: ^Image, options := Options{}, allocato return nil if write_ok else .Unable_To_Write_File } -save :: proc{save_to_memory, save_to_file} \ No newline at end of file +save :: proc{save_to_memory, save_to_file} + +load_from_context :: proc(ctx: ^$C, options := Options{}, allocator := context.allocator) -> (img: ^Image, err: Error) { + context.allocator = allocator + options := options + + if .alpha_premultiply in options { + return nil, .Unsupported_Option + } + + if .info in options { + options |= {.return_metadata, .do_not_decompress_image} + options -= {.info} + } + + if .return_header in options && .return_metadata in options { + options -= {.return_header} + } + + header := image.read_data(ctx, image.TGA_Header) or_return + + // Header checks + if header.data_type_code != DATATYPE_UNCOMPRESSED_RGB { + return nil, .Unsupported_Format + } + if header.bits_per_pixel!=24 && header.bits_per_pixel!=32 { + return nil, .Unsupported_Format + } + if ( header.image_descriptor & IMAGE_DESCRIPTOR_INTERLEAVING_MASK ) != 0 { + return nil, .Unsupported_Format + } + + if (int(header.dimensions[0])*int(header.dimensions[1])) > image.MAX_DIMENSIONS { + return nil, .Image_Dimensions_Too_Large + } + + if img == nil { + img = new(Image) + } + + if .return_metadata in options { + info := new(image.TGA_Info) + info.header = header + img.metadata = info + } + src_channels := int(header.bits_per_pixel)/8 + img.which = .TGA + img.channels = .alpha_add_if_missing in options ? 4: src_channels + img.channels = .alpha_drop_if_present in options ? 3: img.channels + + img.depth = 8 + img.width = int(header.dimensions[0]) + img.height = int(header.dimensions[1]) + + if .do_not_decompress_image in options { + return img, nil + } + + // skip id + if _, e := compress.read_slice(ctx, int(header.id_length)); e!= .None { + destroy(img) + return nil, .Corrupt + } + + if !resize(&img.pixels.buf, img.channels * img.width * img.height) { + destroy(img) + return nil, .Unable_To_Allocate_Or_Resize + } + + origin_is_topleft := (header.image_descriptor & IMAGE_DESCRIPTOR_TOPLEFT_MASK ) != 0 + for y in 0.. (img: ^Image, err: Error) { + ctx := &compress.Context_Memory_Input{ + input_data = data, + } + + img, err = load_from_context(ctx, options, allocator) + return img, err +} + +load_from_file :: proc(filename: string, options := Options{}, allocator := context.allocator) -> (img: ^Image, err: Error) { + context.allocator = allocator + + data, ok := os.read_entire_file(filename) + defer delete(data) + + if ok { + return load_from_bytes(data, options) + } else { + return nil, .Unable_To_Read_File + } +} + +load :: proc{load_from_file, load_from_bytes, load_from_context} + +destroy :: proc(img: ^Image) { + if img == nil { + return + } + + bytes.buffer_destroy(&img.pixels) + if v, ok := img.metadata.(^image.TGA_Info); ok { + free(v) + } + + free(img) +} + +DATATYPE_UNCOMPRESSED_RGB :: 0x2 +IMAGE_DESCRIPTOR_INTERLEAVING_MASK :: (1<<6) | (1<<7) +IMAGE_DESCRIPTOR_TOPLEFT_MASK :: 1<<5 + +@(init, private) +_register :: proc() { + image.register(.TGA, load_from_bytes, destroy) +} \ No newline at end of file From 934131abf86a03e98aa924fc93720e68729f317e Mon Sep 17 00:00:00 2001 From: Benoit Jacquier Date: Sat, 27 Aug 2022 19:30:15 +0200 Subject: [PATCH 2/4] Add RLE supports for TGA loader --- core/image/common.odin | 7 ++++- core/image/tga/tga.odin | 68 ++++++++++++++++++++++++++++++----------- 2 files changed, 57 insertions(+), 18 deletions(-) diff --git a/core/image/common.odin b/core/image/common.odin index a627fa68b..f0cb491a2 100644 --- a/core/image/common.odin +++ b/core/image/common.odin @@ -378,10 +378,15 @@ QOI_Info :: struct { header: QOI_Header, } +TGA_Data_Type :: enum u8 { + Uncompressed_RGB = 2, + Compressed_RBB = 10 +} + TGA_Header :: struct #packed { id_length: u8, color_map_type: u8, - data_type_code: u8, + data_type_code: TGA_Data_Type, color_map_origin: u16le, color_map_length: u16le, color_map_depth: u8, diff --git a/core/image/tga/tga.odin b/core/image/tga/tga.odin index 9b56b9db4..6fd65b7d9 100644 --- a/core/image/tga/tga.odin +++ b/core/image/tga/tga.odin @@ -4,6 +4,7 @@ List of contributors: Jeroen van Rijn: Initial implementation. + Benoit Jacquier: tga loader */ @@ -17,7 +18,6 @@ import "core:os" import "core:compress" // TODO: alpha_premultiply support -// TODO: RLE decompression Error :: image.Error @@ -62,7 +62,7 @@ save_to_memory :: proc(output: ^bytes.Buffer, img: ^Image, options := Options{} } header := image.TGA_Header{ - data_type_code = 0x02, // Color, uncompressed. + data_type_code = .Uncompressed_RGB, dimensions = {u16le(img.width), u16le(img.height)}, bits_per_pixel = u8(img.depth * img.channels), image_descriptor = 1 << 5, // Origin is top left. @@ -125,8 +125,12 @@ load_from_context :: proc(ctx: ^$C, options := Options{}, allocator := context.a header := image.read_data(ctx, image.TGA_Header) or_return // Header checks - if header.data_type_code != DATATYPE_UNCOMPRESSED_RGB { - return nil, .Unsupported_Format + rle_encoding := false + + switch header.data_type_code { + case .Compressed_RBB: rle_encoding = true + case .Uncompressed_RGB: + case: return nil, .Unsupported_Format } if header.bits_per_pixel!=24 && header.bits_per_pixel!=32 { return nil, .Unsupported_Format @@ -173,26 +177,57 @@ load_from_context :: proc(ctx: ^$C, options := Options{}, allocator := context.a } origin_is_topleft := (header.image_descriptor & IMAGE_DESCRIPTOR_TOPLEFT_MASK ) != 0 + rle_repetition_count := 0 + read_pixel := true + is_packet_rle := false + pixel: [4]u8 for y in 0..>7) != 0 + rle_repetition_count = 1 + int(rle_cmd & 0x7F) + read_pixel = true + } else if is_packet_rle==false { + read_pixel = rle_repetition_count>0 } else { - dst[3] = 255 + read_pixel = false } } + // Read pixel + if read_pixel { + src, err := compress.read_slice(ctx, src_channels) + if err!=.None { + destroy(img) + return nil, .Corrupt + } + pixel[2] = src[0] + pixel[1] = src[1] + pixel[0] = src[2] + pixel[3] = src_channels==4 ? src[3] : 255 + if img.channels==4 { + if src_channels==4 { + dst[3] = src[3] + } else { + dst[3] = 255 + } + } + } + + // Write pixel + mem.copy(dst, mem.raw_data(&pixel), img.channels) dst = mem.ptr_offset(dst, img.channels) + rle_repetition_count -= 1 } } return img, nil @@ -235,7 +270,6 @@ destroy :: proc(img: ^Image) { free(img) } -DATATYPE_UNCOMPRESSED_RGB :: 0x2 IMAGE_DESCRIPTOR_INTERLEAVING_MASK :: (1<<6) | (1<<7) IMAGE_DESCRIPTOR_TOPLEFT_MASK :: 1<<5 From 6363013dd8537a6d8321b4ab9dd4b9f1a3c922b8 Mon Sep 17 00:00:00 2001 From: Benoit Jacquier Date: Sat, 27 Aug 2022 19:45:14 +0200 Subject: [PATCH 3/4] style fix --- core/image/common.odin | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/image/common.odin b/core/image/common.odin index f0cb491a2..7caba4fca 100644 --- a/core/image/common.odin +++ b/core/image/common.odin @@ -380,7 +380,7 @@ QOI_Info :: struct { TGA_Data_Type :: enum u8 { Uncompressed_RGB = 2, - Compressed_RBB = 10 + Compressed_RBB = 10, } TGA_Header :: struct #packed { From f74e281efadbbd3c042f2f3aed13f0552cd881dd Mon Sep 17 00:00:00 2001 From: Jeroen van Rijn Date: Sun, 28 Aug 2022 18:25:07 +0200 Subject: [PATCH 4/4] Various changes to TGA reader - Style changes - Change ptr usage to slice indexing - Add TGA Footer Also, add `peek_data` with offset to `compress`. --- core/compress/common.odin | 57 +++++++++++++++- core/image/common.odin | 17 ++++- core/image/tga/tga.odin | 139 ++++++++++++++++++++++++-------------- 3 files changed, 159 insertions(+), 54 deletions(-) diff --git a/core/compress/common.odin b/core/compress/common.odin index f4e378269..b42cbefff 100644 --- a/core/compress/common.odin +++ b/core/compress/common.odin @@ -294,6 +294,24 @@ peek_data_from_memory :: #force_inline proc(z: ^Context_Memory_Input, $T: typeid } } +@(optimization_mode="speed") +peek_data_at_offset_from_memory :: #force_inline proc(z: ^Context_Memory_Input, $T: typeid, #any_int offset: int) -> (res: T, err: io.Error) { + size :: size_of(T) + + #no_bounds_check { + if len(z.input_data) >= size + offset { + buf := z.input_data[offset:][:size] + return (^T)(&buf[0])^, .None + } + } + + if len(z.input_data) == 0 { + return T{}, .EOF + } else { + return T{}, .Short_Buffer + } +} + @(optimization_mode="speed") peek_data_from_stream :: #force_inline proc(z: ^Context_Stream_Input, $T: typeid) -> (res: T, err: io.Error) { size :: size_of(T) @@ -321,7 +339,44 @@ peek_data_from_stream :: #force_inline proc(z: ^Context_Stream_Input, $T: typeid return res, .None } -peek_data :: proc{peek_data_from_memory, peek_data_from_stream} +@(optimization_mode="speed") +peek_data_at_offset_from_stream :: #force_inline proc(z: ^Context_Stream_Input, $T: typeid, #any_int offset: int) -> (res: T, err: io.Error) { + size :: size_of(T) + + // Get current position to return to. + cur_pos, e1 := z.input->impl_seek(0, .Current) + if e1 != .None { + return T{}, e1 + } + + // Seek to offset. + pos, e2 := z.input->impl_seek(offset, .Start) + if e2 != .None { + return T{}, e2 + } + + r, e3 := io.to_reader_at(z.input) + if !e3 { + return T{}, .Empty + } + when size <= 128 { + b: [size]u8 + } else { + b := make([]u8, size, context.temp_allocator) + } + _, e4 := io.read_at(r, b[:], pos) + if e4 != .None { + return T{}, .Empty + } + + // Return read head to original position. + z.input->impl_seek(cur_pos, .Start) + + res = (^T)(&b[0])^ + return res, .None +} + +peek_data :: proc{peek_data_from_memory, peek_data_from_stream, peek_data_at_offset_from_memory, peek_data_at_offset_from_stream} diff --git a/core/image/common.odin b/core/image/common.odin index 7caba4fca..c79344445 100644 --- a/core/image/common.odin +++ b/core/image/common.odin @@ -46,7 +46,7 @@ Image :: struct { height: int, channels: int, depth: int, // Channel depth in bits, typically 8 or 16 - pixels: bytes.Buffer, + pixels: bytes.Buffer `fmt:"-"`, /* Some image loaders/writers can return/take an optional background color. For convenience, we return them as u16 so we don't need to switch on the type @@ -380,7 +380,7 @@ QOI_Info :: struct { TGA_Data_Type :: enum u8 { Uncompressed_RGB = 2, - Compressed_RBB = 10, + Compressed_RBB = 10, } TGA_Header :: struct #packed { @@ -397,8 +397,19 @@ TGA_Header :: struct #packed { } #assert(size_of(TGA_Header) == 18) +New_TGA_Signature :: "TRUEVISION-XFILE.\x00" + +TGA_Footer :: struct #packed { + extension_area_offset: u32le, + developer_directory_offset: u32le, + signature: [18]u8 `fmt:"s"`, // Should match signature if New TGA. +} +#assert(size_of(TGA_Footer) == 26) + TGA_Info :: struct { - header: TGA_Header, + header: TGA_Header, + image_id: string, + footer: Maybe(TGA_Footer), } // Function to help with image buffer calculations diff --git a/core/image/tga/tga.odin b/core/image/tga/tga.odin index 6fd65b7d9..d257d06ff 100644 --- a/core/image/tga/tga.odin +++ b/core/image/tga/tga.odin @@ -16,10 +16,12 @@ import "core:image" import "core:bytes" import "core:os" import "core:compress" +import "core:strings" +import "core:fmt" +_ :: fmt // TODO: alpha_premultiply support - Error :: image.Error Image :: image.Image Options :: image.Options @@ -122,6 +124,21 @@ load_from_context :: proc(ctx: ^$C, options := Options{}, allocator := context.a options -= {.return_header} } + // First check for a footer. + filesize := compress.input_size(ctx) or_return + + footer: image.TGA_Footer + have_valid_footer := false + + if filesize >= size_of(image.TGA_Header) + size_of(image.TGA_Footer) { + if f, f_err := compress.peek_data(ctx, image.TGA_Footer, filesize - i64(size_of(image.TGA_Footer))); f_err == .None { + if string(f.signature[:]) == image.New_TGA_Signature { + have_valid_footer = true + footer = f + } + } + } + header := image.read_data(ctx, image.TGA_Header) or_return // Header checks @@ -132,14 +149,16 @@ load_from_context :: proc(ctx: ^$C, options := Options{}, allocator := context.a case .Uncompressed_RGB: case: return nil, .Unsupported_Format } - if header.bits_per_pixel!=24 && header.bits_per_pixel!=32 { - return nil, .Unsupported_Format - } - if ( header.image_descriptor & IMAGE_DESCRIPTOR_INTERLEAVING_MASK ) != 0 { + + if header.bits_per_pixel != 24 && header.bits_per_pixel != 32 { return nil, .Unsupported_Format } - if (int(header.dimensions[0])*int(header.dimensions[1])) > image.MAX_DIMENSIONS { + if header.image_descriptor & IMAGE_DESCRIPTOR_INTERLEAVING_MASK != 0 { + return nil, .Unsupported_Format + } + + if int(header.dimensions[0]) * int(header.dimensions[1]) > image.MAX_DIMENSIONS { return nil, .Image_Dimensions_Too_Large } @@ -147,88 +166,104 @@ load_from_context :: proc(ctx: ^$C, options := Options{}, allocator := context.a img = new(Image) } + defer if err != nil { + destroy(img) + } + + src_channels := int(header.bits_per_pixel) / 8 + img.which = .TGA + img.channels = 4 if .alpha_add_if_missing in options else src_channels + img.channels = 3 if .alpha_drop_if_present in options else img.channels + + img.depth = 8 + img.width = int(header.dimensions[0]) + img.height = int(header.dimensions[1]) + + // Read Image ID if present + image_id := "" + if _id, e := compress.read_slice(ctx, int(header.id_length)); e != .None { + return nil, .Corrupt + } else { + if .return_metadata in options { + id := strings.trim_right_null(string(_id)) + image_id = strings.clone(id) + } + } + if .return_metadata in options { info := new(image.TGA_Info) - info.header = header + info.header = header + info.image_id = image_id + if have_valid_footer { + info.footer = footer + } img.metadata = info } - src_channels := int(header.bits_per_pixel)/8 - img.which = .TGA - img.channels = .alpha_add_if_missing in options ? 4: src_channels - img.channels = .alpha_drop_if_present in options ? 3: img.channels - - img.depth = 8 - img.width = int(header.dimensions[0]) - img.height = int(header.dimensions[1]) if .do_not_decompress_image in options { return img, nil } - // skip id - if _, e := compress.read_slice(ctx, int(header.id_length)); e!= .None { - destroy(img) - return nil, .Corrupt - } - if !resize(&img.pixels.buf, img.channels * img.width * img.height) { - destroy(img) - return nil, .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_topleft := header.image_descriptor & IMAGE_DESCRIPTOR_TOPLEFT_MASK != 0 rle_repetition_count := 0 - read_pixel := true - is_packet_rle := false - pixel: [4]u8 - for y in 0..>7) != 0 + is_packet_rle = (rle_cmd >> 7) != 0 rle_repetition_count = 1 + int(rle_cmd & 0x7F) read_pixel = true - } else if is_packet_rle==false { - read_pixel = rle_repetition_count>0 + } else if !is_packet_rle { + read_pixel = rle_repetition_count > 0 } else { read_pixel = false } } // Read pixel if read_pixel { - src, err := compress.read_slice(ctx, src_channels) - if err!=.None { - destroy(img) - return nil, .Corrupt + src, src_err := compress.read_slice(ctx, src_channels) + if src_err != .None { + return img, .Corrupt } + pixel[2] = src[0] pixel[1] = src[1] pixel[0] = src[2] - pixel[3] = src_channels==4 ? src[3] : 255 - if img.channels==4 { - if src_channels==4 { - dst[3] = src[3] + + pixel[3] = src_channels == 4 ? src[3] : 255 + if img.channels == 4 { + if src_channels == 4 { + img.pixels.buf[offset:][3] = src[3] } else { - dst[3] = 255 + img.pixels.buf[offset:][3] = 255 } } } // Write pixel - mem.copy(dst, mem.raw_data(&pixel), img.channels) - dst = mem.ptr_offset(dst, img.channels) + copy(img.pixels.buf[offset:], pixel[:img.channels]) + offset += img.channels rle_repetition_count -= 1 } + line += 1 if origin_is_topleft else -1 } return img, nil } @@ -258,15 +293,19 @@ load_from_file :: proc(filename: string, options := Options{}, allocator := cont load :: proc{load_from_file, load_from_bytes, load_from_context} destroy :: proc(img: ^Image) { - if img == nil { + if img == nil || img.width == 0 || img.height == 0 { return } bytes.buffer_destroy(&img.pixels) if v, ok := img.metadata.(^image.TGA_Info); ok { + delete(v.image_id) free(v) } + // Make destroy idempotent + img.width = 0 + img.height = 0 free(img) }