Test implementation of read_strips

This commit is contained in:
Abdelrahman Said 2024-05-01 23:53:04 +01:00
parent 55720ac331
commit c46535c20b

View File

@ -36,6 +36,8 @@ TiffHdr read_tiff_header(FILE *fp);
TiffIFD read_ifd(FILE *fp, const TiffHdr *header, u32 offset, Arena *arena); TiffIFD read_ifd(FILE *fp, const TiffHdr *header, u32 offset, Arena *arena);
TiffImage read_fields(FILE *fp, const TiffHdr *header, const TiffIFD *ifd); TiffImage read_fields(FILE *fp, const TiffHdr *header, const TiffIFD *ifd);
bool read_strip_data(FILE *fp, const TiffImage *img, TiffStrip *strips); bool read_strip_data(FILE *fp, const TiffImage *img, TiffStrip *strips);
void read_strips(FILE *fp, const TiffImage *img, const TiffStrip *strips,
Pixel *buf);
bool read_field(const TiffField *field, TiffImage *img, bool read_field(const TiffField *field, TiffImage *img,
u32 *image_type_identifier); u32 *image_type_identifier);
bool validate_image_type(const TiffImage *img, bool validate_image_type(const TiffImage *img,
@ -80,9 +82,11 @@ Image *read_baseline_tiff(const char *file, Arena *arena) {
TiffIFD ifd = read_ifd(fp, &header, header.first_ifd_offset, arena); TiffIFD ifd = read_ifd(fp, &header, header.first_ifd_offset, arena);
TiffImage img = read_fields(fp, &header, &ifd); TiffImage img = read_fields(fp, &header, &ifd);
u64 img_byte_count = sizeof(Pixel) * img.image_width * img.image_length;
Arena *temp = NULL; Arena *temp = NULL;
if (!wapp_mem_arena_init(&temp, 1 * 1024 * 1024)) { u64 temp_size = 1 * 1024 * 1024 + img_byte_count;
if (!wapp_mem_arena_init(&temp, temp_size)) {
goto READ_BASELINE_FILE_CLEANUP; goto READ_BASELINE_FILE_CLEANUP;
} }
@ -100,6 +104,15 @@ Image *read_baseline_tiff(const char *file, Arena *arena) {
printf("%u, %u\n", strips[i].offset, strips[i].byte_count); printf("%u, %u\n", strips[i].offset, strips[i].byte_count);
} }
Pixel *buf = wapp_mem_arena_alloc(temp, img_byte_count);
if (!buf) {
goto READ_BASELINE_DESTROY_ARENA;
}
read_strips(fp, &img, strips, buf);
img_out = create_image(img.image_width, img.image_length, buf, arena);
READ_BASELINE_DESTROY_ARENA: READ_BASELINE_DESTROY_ARENA:
wapp_mem_arena_destroy(&temp); wapp_mem_arena_destroy(&temp);
@ -288,6 +301,21 @@ bool read_strip_data(FILE *fp, const TiffImage *img, TiffStrip *strips) {
return true; return true;
} }
void read_strips(FILE *fp, const TiffImage *img, const TiffStrip *strips,
Pixel *buf) {
for (u64 i = 0; i < img->strip_count; ++i) {
const TiffStrip *strip = &(strips[i]);
for (u64 j = 0; j < img->image_width * img->image_length; ++j) {
Pixel *p = &(buf[j]);
read_from_file_with_offset(fp, p, 3, strip->offset + j * 3);
p->a = 255;
}
}
}
bool read_field(const TiffField *field, TiffImage *img, bool read_field(const TiffField *field, TiffImage *img,
u32 *image_type_identifier) { u32 *image_type_identifier) {
switch (field->tag) { switch (field->tag) {