Compare commits
4 Commits
19a87c5c4f
...
main
Author | SHA1 | Date | |
---|---|---|---|
35a33e2fa6 | |||
1c08881dfb | |||
7593eb4732 | |||
ec2835e900 |
21
compile
21
compile
@@ -1,7 +1,28 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
|
BUILD_TYPE="debug"
|
||||||
|
|
||||||
|
while [[ $# > 0 ]];do
|
||||||
|
case $1 in
|
||||||
|
--release)
|
||||||
|
BUILD_TYPE="release"
|
||||||
|
shift
|
||||||
|
;;
|
||||||
|
*|-*|--*)
|
||||||
|
echo "Unknown option $1"
|
||||||
|
exit 1
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
|
||||||
CC=clang
|
CC=clang
|
||||||
|
|
||||||
|
if [[ $BUILD_TYPE == "release" ]]; then
|
||||||
|
CFLAGS="-O3 -Wall -Werror -pedantic"
|
||||||
|
else
|
||||||
CFLAGS="-g -Wall -Werror -pedantic -fsanitize=address -fsanitize=undefined -DDEBUG"
|
CFLAGS="-g -Wall -Werror -pedantic -fsanitize=address -fsanitize=undefined -DDEBUG"
|
||||||
|
fi
|
||||||
|
|
||||||
INCLUDES="\
|
INCLUDES="\
|
||||||
-I$(find ./src -type d | xargs -I{} echo -n "-I{} ") \
|
-I$(find ./src -type d | xargs -I{} echo -n "-I{} ") \
|
||||||
$(find intern/wizapp/src -type d | xargs -I{} echo -n "-I{} ") \
|
$(find intern/wizapp/src -type d | xargs -I{} echo -n "-I{} ") \
|
||||||
|
24
src/main.c
24
src/main.c
@@ -4,6 +4,7 @@
|
|||||||
#include "tiffread.h"
|
#include "tiffread.h"
|
||||||
#include <SDL2/SDL.h>
|
#include <SDL2/SDL.h>
|
||||||
#include <SDL2/SDL_events.h>
|
#include <SDL2/SDL_events.h>
|
||||||
|
#include <SDL2/SDL_rect.h>
|
||||||
#include <SDL2/SDL_render.h>
|
#include <SDL2/SDL_render.h>
|
||||||
#include <SDL2/SDL_surface.h>
|
#include <SDL2/SDL_surface.h>
|
||||||
#include <SDL2/SDL_video.h>
|
#include <SDL2/SDL_video.h>
|
||||||
@@ -18,6 +19,8 @@
|
|||||||
|
|
||||||
#define WINDOW_WIDTH 800
|
#define WINDOW_WIDTH 800
|
||||||
#define WINDOW_HEIGHT 600
|
#define WINDOW_HEIGHT 600
|
||||||
|
#define VIEW_AREA_WIDTH 700
|
||||||
|
#define VIEW_AREA_HEIGHT 500
|
||||||
|
|
||||||
#define AMASK 0xff000000
|
#define AMASK 0xff000000
|
||||||
#define BMASK 0x00ff0000
|
#define BMASK 0x00ff0000
|
||||||
@@ -34,6 +37,7 @@ struct point {
|
|||||||
|
|
||||||
internal void write_debug_ppm6(const Image *img);
|
internal void write_debug_ppm6(const Image *img);
|
||||||
internal void *load_tiff_image(void *args);
|
internal void *load_tiff_image(void *args);
|
||||||
|
internal SDL_Rect get_dest_rect(SDL_Surface *surface);
|
||||||
|
|
||||||
typedef struct img_thread_args ImgThreadArgs;
|
typedef struct img_thread_args ImgThreadArgs;
|
||||||
struct img_thread_args {
|
struct img_thread_args {
|
||||||
@@ -90,12 +94,7 @@ int main(int argc, char *argv[]) {
|
|||||||
goto MAIN_DESTROY_SURFACE;
|
goto MAIN_DESTROY_SURFACE;
|
||||||
}
|
}
|
||||||
|
|
||||||
SDL_Rect dest = {
|
SDL_Rect dest = get_dest_rect(surface);
|
||||||
.h = surface->h,
|
|
||||||
.w = surface->w,
|
|
||||||
.x = (WINDOW_WIDTH - surface->w) / 2,
|
|
||||||
.y = (WINDOW_HEIGHT - surface->h) / 2,
|
|
||||||
};
|
|
||||||
|
|
||||||
bool running = true;
|
bool running = true;
|
||||||
|
|
||||||
@@ -188,3 +187,16 @@ internal void *load_tiff_image(void *args) {
|
|||||||
|
|
||||||
return surface;
|
return surface;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
internal SDL_Rect get_dest_rect(SDL_Surface *surface) {
|
||||||
|
f64 ratio = (f64)(surface->h) / (f64)(surface->w);
|
||||||
|
u64 width = surface->w <= VIEW_AREA_WIDTH ? surface->w : VIEW_AREA_WIDTH;
|
||||||
|
u64 height = width * ratio;
|
||||||
|
|
||||||
|
return (SDL_Rect){
|
||||||
|
.w = width,
|
||||||
|
.h = height,
|
||||||
|
.x = (WINDOW_WIDTH - width) / 2,
|
||||||
|
.y = (WINDOW_HEIGHT - height) / 2,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
@@ -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,47 @@ 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);
|
||||||
}
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
++threads_created;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (threads_created < reader->img.strip_count) {
|
|
||||||
for (u64 i = 0; i < strips_read; ++i) {
|
|
||||||
pthread_cancel(threads[i]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (u64 i = 0; i < reader->img.strip_count; ++i) {
|
position += pixel_count;
|
||||||
pthread_join(threads[i], NULL);
|
}
|
||||||
|
|
||||||
|
for (u64 i = 0; i < reader->img.strip_count; ++i) {
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool image_loaded = true;
|
||||||
|
|
||||||
|
for (u64 i = 0; i < reader->img.strip_count; ++i) {
|
||||||
|
if (pthread_join(threads[i], NULL) != 0) {
|
||||||
|
image_loaded = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
READ_STRIPS_CLOSE_FILES:
|
|
||||||
for (u64 i = 0; i < strips_read; ++i) {
|
|
||||||
fclose(args[i].fp);
|
fclose(args[i].fp);
|
||||||
}
|
}
|
||||||
|
|
||||||
return output;
|
return image_loaded;
|
||||||
}
|
}
|
||||||
|
|
||||||
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;
|
||||||
|
Reference in New Issue
Block a user