From 566119ff83b72a473f9dc8630876e414f2d44b8a Mon Sep 17 00:00:00 2001 From: Jeroen van Rijn Date: Thu, 6 Jun 2024 23:52:32 +0200 Subject: [PATCH] Add saving of 24 and 32-bit images to BMP format. --- core/image/bmp/bmp.odin | 96 +++++++++++++++++++++++++++++++++++++- core/image/bmp/bmp_os.odin | 15 ++++++ 2 files changed, 110 insertions(+), 1 deletion(-) diff --git a/core/image/bmp/bmp.odin b/core/image/bmp/bmp.odin index d9ecb55e5..64fc1d5a8 100644 --- a/core/image/bmp/bmp.odin +++ b/core/image/bmp/bmp.odin @@ -7,7 +7,6 @@ import "core:compress" import "core:mem" import "base:intrinsics" import "base:runtime" -@(require) import "core:fmt" Error :: image.Error Image :: image.Image @@ -19,6 +18,101 @@ RGBA_Pixel :: image.RGBA_Pixel FILE_HEADER_SIZE :: 14 INFO_STUB_SIZE :: FILE_HEADER_SIZE + size_of(image.BMP_Version) +save_to_buffer :: proc(output: ^bytes.Buffer, img: ^Image, options := Options{}, allocator := context.allocator) -> (err: Error) { + context.allocator = allocator + + if img == nil { + return .Invalid_Input_Image + } + + if output == nil { + return .Invalid_Output + } + + pixels := img.width * img.height + if pixels == 0 || pixels > image.MAX_DIMENSIONS { + return .Invalid_Input_Image + } + + // While the BMP spec (and our loader) support more fanciful image types, + // `bmp.save` supports only 3 and 4 channel images with a bit depth of 8. + if img.depth != 8 || img.channels < 3 || img.channels > 4 { + return .Invalid_Input_Image + } + + if img.channels * pixels != len(img.pixels.buf) { + return .Invalid_Input_Image + } + + // Calculate and allocate size. + header_size := u32le(image.BMP_Version.V3) + total_header_size := header_size + 14 // file header = 14 + pixel_count_bytes := u32le(align4(img.width * img.channels) * img.height) + + header := image.BMP_Header{ + // File header + magic = .Bitmap, + size = total_header_size + pixel_count_bytes, + _res1 = 0, + _res2 = 0, + pixel_offset = total_header_size, + // V3 + info_size = .V3, + width = i32le(img.width), + height = i32le(img.height), + planes = 1, + bpp = u16le(8 * img.channels), + compression = .RGB, + image_size = pixel_count_bytes, + pels_per_meter = {2835, 2835}, // 72 DPI + colors_used = 0, + colors_important = 0, + } + written := 0 + + if resize(&output.buf, int(header.size)) != nil { + return .Unable_To_Allocate_Or_Resize + } + + header_bytes := transmute([size_of(image.BMP_Header)]u8)header + written += int(total_header_size) + copy(output.buf[:], header_bytes[:written]) + + switch img.channels { + case 3: + row_bytes := img.width * img.channels + row_padded := align4(row_bytes) + pixels := mem.slice_data_cast([]RGB_Pixel, img.pixels.buf[:]) + for y in 0.. (img: ^Image, err: Error) { ctx := &compress.Context_Memory_Input{ input_data = data, diff --git a/core/image/bmp/bmp_os.odin b/core/image/bmp/bmp_os.odin index 2245db4fd..d20abc685 100644 --- a/core/image/bmp/bmp_os.odin +++ b/core/image/bmp/bmp_os.odin @@ -2,6 +2,7 @@ package core_image_bmp import "core:os" +import "core:bytes" load :: proc{load_from_file, load_from_bytes, load_from_context} @@ -16,4 +17,18 @@ load_from_file :: proc(filename: string, options := Options{}, allocator := cont } else { return nil, .Unable_To_Read_File } +} + +save :: proc{save_to_buffer, save_to_file} + +save_to_file :: proc(output: string, img: ^Image, options := Options{}, allocator := context.allocator) -> (err: Error) { + context.allocator = allocator + + out := &bytes.Buffer{} + defer bytes.buffer_destroy(out) + + save_to_buffer(out, img, options) or_return + write_ok := os.write_entire_file(output, out.buf[:]) + + return nil if write_ok else .Unable_To_Write_File } \ No newline at end of file