From 9312c0fb6b5d135afeb1b896c3475e5322fdd5a3 Mon Sep 17 00:00:00 2001 From: Abdelrahman Date: Sat, 4 May 2024 21:17:22 +0100 Subject: [PATCH] Move TiffStrip array inside the TiffImage struct --- src/tiff/tiffread.c | 38 ++++++++++++++++++-------------------- src/tiff/tiffread.h | 13 +++++++------ 2 files changed, 25 insertions(+), 26 deletions(-) diff --git a/src/tiff/tiffread.c b/src/tiff/tiffread.c index 550f618..5e28602 100644 --- a/src/tiff/tiffread.c +++ b/src/tiff/tiffread.c @@ -36,10 +36,9 @@ TiffHdr read_tiff_header(FILE *fp); TiffIFD read_ifd(FILE *fp, const TiffHdr *header, u32 offset, Arena *arena); TiffImage read_fields(FILE *fp, const TiffHdr *header, const TiffIFD *ifd); -bool read_strip_data(FILE *fp, const TiffHdr *header, const TiffImage *img, - TiffStrip *strips); -void read_strips(FILE *fp, const TiffImage *img, const TiffStrip *strips, - Pixel *buf); +bool read_strip_data(FILE *fp, const TiffHdr *header, TiffImage *img, + Arena *arena); +void read_strips(FILE *fp, const TiffImage *img, Pixel *buf); bool read_field(const TiffField *field, TiffImage *img, u32 *image_type_identifier); bool validate_image_type(const TiffImage *img, @@ -95,18 +94,12 @@ Image *read_baseline_tiff(const char *file, Arena *arena) { goto READ_BASELINE_FILE_CLEANUP; } - TiffStrip *strips = - wapp_mem_arena_alloc(temp, sizeof(TiffStrip) * img.strip_count); - if (!strips) { - goto READ_BASELINE_DESTROY_ARENA; - } - - if (!read_strip_data(fp, &header, &img, strips)) { + if (!read_strip_data(fp, &header, &img, temp)) { goto READ_BASELINE_DESTROY_ARENA; } for (u32 i = 0; i < img.strip_count; ++i) { - printf("%u, %u\n", strips[i].offset, strips[i].byte_count); + printf("%u, %u\n", img.strips[i].offset, img.strips[i].byte_count); } Pixel *buf = wapp_mem_arena_alloc(temp, img_byte_count); @@ -114,7 +107,7 @@ Image *read_baseline_tiff(const char *file, Arena *arena) { goto READ_BASELINE_DESTROY_ARENA; } - read_strips(fp, &img, strips, buf); + read_strips(fp, &img, buf); img_out = create_image(img.image_width, img.image_length, buf, arena); @@ -265,9 +258,15 @@ READ_FIELDS_RETURN_IMAGE: return img_out; } -bool read_strip_data(FILE *fp, const TiffHdr *header, const TiffImage *img, - TiffStrip *strips) { - if (!fp || !img || !strips) { +bool read_strip_data(FILE *fp, const TiffHdr *header, TiffImage *img, + Arena *arena) { + if (!fp || !img || !arena) { + return false; + } + + img->strips = + wapp_mem_arena_alloc(arena, sizeof(TiffStrip) * img->strip_count); + if (!img->strips) { return false; } @@ -283,7 +282,7 @@ bool read_strip_data(FILE *fp, const TiffHdr *header, const TiffImage *img, } for (u32 i = 0; i < img->strip_count; ++i) { - TiffStrip *strip = &(strips[i]); + TiffStrip *strip = &(img->strips[i]); u16 offset_size = img->strip_offsets_type_byte_count; u16 counts_size = img->strip_byte_count_type_byte_count; @@ -339,11 +338,10 @@ bool read_strip_data(FILE *fp, const TiffHdr *header, const TiffImage *img, return true; } -void read_strips(FILE *fp, const TiffImage *img, const TiffStrip *strips, - Pixel *buf) { +void read_strips(FILE *fp, const TiffImage *img, Pixel *buf) { u64 position = 0; for (u64 i = 0; i < img->strip_count; ++i) { - const TiffStrip *strip = &(strips[i]); + const TiffStrip *strip = &(img->strips[i]); for (u64 j = 0; j < strip->byte_count / img->sample_count; ++j) { Pixel *p = &(buf[position]); diff --git a/src/tiff/tiffread.h b/src/tiff/tiffread.h index 7d629c4..7a0c1c5 100644 --- a/src/tiff/tiffread.h +++ b/src/tiff/tiffread.h @@ -111,6 +111,12 @@ enum tiff_planar_configuration { }; // clang-format on +typedef struct tiff_strip TiffStrip; +struct tiff_strip { + u32 offset; + u32 byte_count; +}; + typedef struct tiff_image TiffImage; struct tiff_image { TiffImageType type; @@ -133,12 +139,7 @@ struct tiff_image { u32 extra_samples; u32 extra_samples_count; bool extra_samples_offset; -}; - -typedef struct tiff_strip TiffStrip; -struct tiff_strip { - u32 offset; - u32 byte_count; + TiffStrip *strips; }; Image *read_baseline_tiff(const char *file, Arena *arena);