diff --git a/src/tiff/tiffread.c b/src/tiff/tiffread.c index 1f9473a..4529720 100644 --- a/src/tiff/tiffread.c +++ b/src/tiff/tiffread.c @@ -210,6 +210,17 @@ struct tiff_reader { Pixel *pixels; }; +typedef struct strip_thread_args StripThreadArgs; +struct strip_thread_args { + FILE *fp; + const TiffStrip *strip; + u64 pixel_count; + u64 samples_per_pixel; + u64 main_samples; + TiffAlpha alpha; + Pixel *pixel_write_start; +}; + internal bool read_tiff_header(TiffReader *reader); internal bool read_ifd(TiffReader *reader, Arena *arena); internal bool read_ifd_fields(TiffReader *reader); @@ -587,68 +598,15 @@ internal bool read_strip_data(TiffReader *reader, Arena *arena) { return true; } -// internal void read_strips(TiffReader *reader) { -// u64 position = 0; -// u64 main_samples = reader->img.sample_count - -// reader->img.extra_samples_count; TiffAlpha alpha = reader->img.alpha; - -// Pixel *p; -// u64 start_offset; -// u64 alpha_offset; - -// for (u64 i = 0; i < reader->img.strip_count; ++i) { -// const TiffStrip *strip = &(reader->img.strips[i]); - -// for (u64 j = 0; j < strip->byte_count / reader->img.sample_count; ++j) { -// p = &(reader->pixels[position]); -// start_offset = strip->offset + j * reader->img.sample_count; -// alpha_offset = start_offset + main_samples + alpha.sample_offset; - -// fread_with_offset(reader->fp, p, main_samples, start_offset); - -// if (alpha.type == ALPHA_TYPE_UNDEFINED) { -// p->a = 255; -// } else { -// fread_with_offset(reader->fp, &(p->a), 1, alpha_offset); - -// if (alpha.type == ALPHA_TYPE_UNASSOCIATED) { -// f32 a_norm = u8_normalise(p->a); - -// p->r = u8_denormalise(u8_normalise(p->r) * a_norm); -// p->g = u8_denormalise(u8_normalise(p->g) * a_norm); -// p->b = u8_denormalise(u8_normalise(p->b) * a_norm); -// } -// } - -// ++position; -// } -// } -// } - -typedef struct strip_reader_args StripReaderArgs; -struct strip_reader_args { - FILE *fp; - const TiffStrip *strip; - u64 pixel_count; - u64 samples_per_pixel; - u64 main_samples; - TiffAlpha alpha; - Pixel *pixel_write_start; -}; - internal bool read_strips(TiffReader *reader) { - bool output = true; - u64 position = 0; - u64 strips_read = 0; - u64 threads_created = 0; - StripReaderArgs args[reader->img.strip_count]; + StripThreadArgs args[reader->img.strip_count]; pthread_t threads[reader->img.strip_count]; for (u64 i = 0; i < reader->img.strip_count; ++i) { const TiffStrip *strip = &(reader->img.strips[i]); u64 pixel_count = strip->byte_count / reader->img.sample_count; - args[i] = (StripReaderArgs){ + args[i] = (StripThreadArgs){ .fp = fopen(reader->filename, "rb"), .strip = strip, .pixel_count = pixel_count, @@ -660,45 +618,42 @@ internal bool read_strips(TiffReader *reader) { }; if (!args[i].fp) { - output = false; - goto READ_STRIPS_CLOSE_FILES; + for (u64 j = 0; j < i; ++j) { + fclose(args[j].fp); + } + + return false; } position += pixel_count; - ++strips_read; } for (u64 i = 0; i < reader->img.strip_count; ++i) { - if (pthread_create(&(threads[i]), NULL, read_strip, (void *)&(args[i])) != - 0) { - break; + if (pthread_create(&(threads[i]), NULL, read_strip, &(args[i])) != 0) { + for (u64 j = 0; j < i; ++j) { + pthread_cancel(threads[i]); + fclose(args[j].fp); + } + + // Close the remaining files + for (u64 j = i; j < reader->img.strip_count; ++j) { + fclose(args[j].fp); + } + + return false; } - - ++threads_created; - } - - if (threads_created < reader->img.strip_count) { - for (u64 i = 0; i < strips_read; ++i) { - pthread_cancel(threads[i]); - } - - return false; } for (u64 i = 0; i < reader->img.strip_count; ++i) { pthread_join(threads[i], NULL); - } - -READ_STRIPS_CLOSE_FILES: - for (u64 i = 0; i < strips_read; ++i) { fclose(args[i].fp); } - return output; + return true; } internal void *read_strip(void *arg) { - StripReaderArgs *args = (StripReaderArgs *)arg; + StripThreadArgs *args = (StripThreadArgs *)arg; Pixel *p; u64 write_offset = 0;