Reorganise tiffread.c and reduce number of loops in read_strips
This commit is contained in:
parent
19a87c5c4f
commit
c564cabc66
@ -210,6 +210,17 @@ struct tiff_reader {
|
|||||||
Pixel *pixels;
|
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_tiff_header(TiffReader *reader);
|
||||||
internal bool read_ifd(TiffReader *reader, Arena *arena);
|
internal bool read_ifd(TiffReader *reader, Arena *arena);
|
||||||
internal bool read_ifd_fields(TiffReader *reader);
|
internal bool read_ifd_fields(TiffReader *reader);
|
||||||
@ -587,68 +598,15 @@ internal bool read_strip_data(TiffReader *reader, Arena *arena) {
|
|||||||
return true;
|
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) {
|
internal bool read_strips(TiffReader *reader) {
|
||||||
bool output = true;
|
|
||||||
|
|
||||||
u64 position = 0;
|
u64 position = 0;
|
||||||
u64 strips_read = 0;
|
StripThreadArgs args[reader->img.strip_count];
|
||||||
u64 threads_created = 0;
|
|
||||||
StripReaderArgs args[reader->img.strip_count];
|
|
||||||
pthread_t threads[reader->img.strip_count];
|
pthread_t threads[reader->img.strip_count];
|
||||||
|
|
||||||
for (u64 i = 0; i < reader->img.strip_count; ++i) {
|
for (u64 i = 0; i < reader->img.strip_count; ++i) {
|
||||||
const TiffStrip *strip = &(reader->img.strips[i]);
|
const TiffStrip *strip = &(reader->img.strips[i]);
|
||||||
u64 pixel_count = strip->byte_count / reader->img.sample_count;
|
u64 pixel_count = strip->byte_count / reader->img.sample_count;
|
||||||
args[i] = (StripReaderArgs){
|
args[i] = (StripThreadArgs){
|
||||||
.fp = fopen(reader->filename, "rb"),
|
.fp = fopen(reader->filename, "rb"),
|
||||||
.strip = strip,
|
.strip = strip,
|
||||||
.pixel_count = pixel_count,
|
.pixel_count = pixel_count,
|
||||||
@ -660,45 +618,42 @@ internal bool read_strips(TiffReader *reader) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
if (!args[i].fp) {
|
if (!args[i].fp) {
|
||||||
output = false;
|
for (u64 j = 0; j < i; ++j) {
|
||||||
goto READ_STRIPS_CLOSE_FILES;
|
fclose(args[j].fp);
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
position += pixel_count;
|
position += pixel_count;
|
||||||
++strips_read;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for (u64 i = 0; i < reader->img.strip_count; ++i) {
|
for (u64 i = 0; i < reader->img.strip_count; ++i) {
|
||||||
if (pthread_create(&(threads[i]), NULL, read_strip, (void *)&(args[i])) !=
|
if (pthread_create(&(threads[i]), NULL, read_strip, &(args[i])) != 0) {
|
||||||
0) {
|
for (u64 j = 0; j < i; ++j) {
|
||||||
break;
|
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) {
|
for (u64 i = 0; i < reader->img.strip_count; ++i) {
|
||||||
pthread_join(threads[i], NULL);
|
pthread_join(threads[i], NULL);
|
||||||
}
|
|
||||||
|
|
||||||
READ_STRIPS_CLOSE_FILES:
|
|
||||||
for (u64 i = 0; i < strips_read; ++i) {
|
|
||||||
fclose(args[i].fp);
|
fclose(args[i].fp);
|
||||||
}
|
}
|
||||||
|
|
||||||
return output;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
internal void *read_strip(void *arg) {
|
internal void *read_strip(void *arg) {
|
||||||
StripReaderArgs *args = (StripReaderArgs *)arg;
|
StripThreadArgs *args = (StripThreadArgs *)arg;
|
||||||
|
|
||||||
Pixel *p;
|
Pixel *p;
|
||||||
u64 write_offset = 0;
|
u64 write_offset = 0;
|
||||||
|
Loading…
Reference in New Issue
Block a user