From 7a032cf9f9818cfb566e91b7f5926bd7278daf07 Mon Sep 17 00:00:00 2001 From: Jeroen van Rijn Date: Sat, 30 Apr 2022 21:57:14 +0200 Subject: [PATCH] [pbm] Also test PFM formats. --- tests/core/image/test_core_image.odin | 109 ++++++++++++++++++++++---- 1 file changed, 95 insertions(+), 14 deletions(-) diff --git a/tests/core/image/test_core_image.odin b/tests/core/image/test_core_image.odin index f1f1e1244..2ecc67151 100644 --- a/tests/core/image/test_core_image.odin +++ b/tests/core/image/test_core_image.odin @@ -1558,25 +1558,106 @@ run_png_suite :: proc(t: ^testing.T, suite: []PNG_Test) -> (subtotal: int) { { // Roundtrip through PBM to test the PBM encoders and decoders - prefer ASCII pbm_info, pbm_format_selected := pbm.autoselect_pbm_format_from_image(img, false) - pbm_buf, pbm_save_err := pbm.save_to_buffer(img, pbm_info) - defer delete(pbm_buf) - error = fmt.tprintf("%v test %v PBM save failed with %v.", file.file, count, pbm_save_err) - expect(t, pbm_save_err == nil, error) + // We already tested the binary formats above. + if pbm_info.header.format in pbm.ASCII { + pbm_buf, pbm_save_err := pbm.save_to_buffer(img, pbm_info) + defer delete(pbm_buf) - if pbm_save_err == nil { - // Try to load it again. - pbm_img, pbm_load_err := pbm.load(pbm_buf) - defer pbm.destroy(pbm_img) + error = fmt.tprintf("%v test %v PBM save failed with %v.", file.file, count, pbm_save_err) + expect(t, pbm_save_err == nil, error) - error = fmt.tprintf("%v test %v PBM load failed with %v.", file.file, count, pbm_load_err) - expect(t, pbm_load_err == nil, error) + if pbm_save_err == nil { + // Try to load it again. + pbm_img, pbm_load_err := pbm.load(pbm_buf) + defer pbm.destroy(pbm_img) - if pbm_load_err == nil { - pbm_hash := hash.crc32(pbm_img.pixels.buf[:]) + error = fmt.tprintf("%v test %v PBM load failed with %v.", file.file, count, pbm_load_err) + expect(t, pbm_load_err == nil, error) - error = fmt.tprintf("%v test %v PBM load hash is %08x, expected it match PNG's %08x with %v.", file.file, count, pbm_hash, png_hash, test.options) - expect(t, pbm_hash == png_hash, error) + if pbm_load_err == nil { + pbm_hash := hash.crc32(pbm_img.pixels.buf[:]) + + error = fmt.tprintf("%v test %v PBM load hash is %08x, expected it match PNG's %08x with %v.", file.file, count, pbm_hash, png_hash, test.options) + expect(t, pbm_hash == png_hash, error) + } + } + } + } + + { + // We still need to test Portable Float Maps + if (img.channels == 1 || img.channels == 3) && (img.depth == 8 || img.depth == 16) { + + // Make temporary float image + float_img := new(image.Image) + defer png.destroy(float_img) + + float_img.width = img.width + float_img.height = img.height + float_img.channels = img.channels + float_img.depth = 32 + + buffer_size := image.compute_buffer_size(img.width, img.height, img.channels, 32) + resize(&float_img.pixels.buf, buffer_size) + + pbm_info := pbm.Info { + header = { + width = img.width, + height = img.height, + channels = img.channels, + depth = img.depth, + maxval = 255 if img.depth == 8 else 65535, + little_endian = true if ODIN_ENDIAN == .Little else false, + scale = 1.0, + format = .Pf if img.channels == 1 else .PF, + }, + } + + // Transform data... + orig_float := mem.slice_data_cast([]f32, float_img.pixels.buf[:]) + + switch img.depth { + case 8: + for v, i in img.pixels.buf { + orig_float[i] = f32(v) / f32(256) + } + case 16: + wide := mem.slice_data_cast([]u16, img.pixels.buf[:]) + for v, i in wide { + orig_float[i] = f32(v) / f32(65536) + } + } + + float_pbm_buf, float_pbm_save_err := pbm.save_to_buffer(float_img, pbm_info) + defer delete(float_pbm_buf) + + error = fmt.tprintf("%v test %v save as PFM failed with %v", file.file, count, float_pbm_save_err) + expect(t, float_pbm_save_err == nil, error) + + if float_pbm_save_err == nil { + // Load float image and compare. + float_pbm_img, float_pbm_load_err := pbm.load(float_pbm_buf) + defer pbm.destroy(float_pbm_img) + + error = fmt.tprintf("%v test %v PFM load failed with %v", file.file, count, float_pbm_load_err) + expect(t, float_pbm_load_err == nil, error) + + load_float := mem.slice_data_cast([]f32, float_pbm_img.pixels.buf[:]) + + error = fmt.tprintf("%v test %v PFM load returned %v floats, expected %v", file.file, count, len(load_float), len(orig_float)) + expect(t, len(load_float) == len(orig_float), error) + + // Compare floats + equal := true + for orig, i in orig_float { + if orig != load_float[i] { + equal = false + break + } + } + error = fmt.tprintf("%v test %v PFM loaded floats to match", file.file, count) + expect(t, equal, error) } } }