Compare commits
34 Commits
9312c0fb6b
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 35a33e2fa6 | |||
| 1c08881dfb | |||
| 7593eb4732 | |||
| ec2835e900 | |||
| 53c70156a6 | |||
| ad4b7b40aa | |||
| aa12b62e7e | |||
| 5762ac33b0 | |||
| efad22f21e | |||
| d278d7ee55 | |||
| 1c667f4128 | |||
| c2391df946 | |||
| b2f002b12f | |||
| cd424bf074 | |||
| 11798a0683 | |||
| c2a55279fd | |||
| 53f53874a6 | |||
| ba21bcb2f4 | |||
| 5bf78d3e01 | |||
| 38862899d0 | |||
| f0d4108a64 | |||
| 3e3c4d27fe | |||
| ed2737d330 | |||
| 9b7e07b9ad | |||
| 71b7c682db | |||
| a9143642a0 | |||
| d195086af8 | |||
| 3f022acf9c | |||
| 484d30d84c | |||
| de0235c0af | |||
| b546ce9fa7 | |||
| c98802542d | |||
| 9fdee24672 | |||
| cd698a3921 |
3
.gitignore
vendored
3
.gitignore
vendored
@@ -2,7 +2,8 @@
|
|||||||
refs
|
refs
|
||||||
file_example*
|
file_example*
|
||||||
big_endian*
|
big_endian*
|
||||||
|
float32_example.tiff
|
||||||
*.py
|
*.py
|
||||||
tiffread
|
tiffread
|
||||||
test.ppm
|
test*
|
||||||
compile_commands.json
|
compile_commands.json
|
||||||
|
|||||||
28
compile
28
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
|
||||||
CFLAGS="-g -Wall -Werror -pedantic -fsanitize=address -fsanitize=undefined"
|
|
||||||
|
if [[ $BUILD_TYPE == "release" ]]; then
|
||||||
|
CFLAGS="-O3 -Wall -Werror -pedantic"
|
||||||
|
else
|
||||||
|
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{} ") \
|
||||||
@@ -9,11 +30,12 @@ INCLUDES="\
|
|||||||
"
|
"
|
||||||
LIBS="\
|
LIBS="\
|
||||||
-lm \
|
-lm \
|
||||||
|
-pthread \
|
||||||
$(pkg-config --libs sdl2) \
|
$(pkg-config --libs sdl2) \
|
||||||
"
|
"
|
||||||
SRC="\
|
SRC="\
|
||||||
$(find ./src -name *.c | xargs -I{} echo -n "{} ") \
|
$(find ./src -name "*.c" | xargs -I{} echo -n "{} ") \
|
||||||
$(find intern/wizapp/src -type f -name *.c | xargs -I{} echo -n "{} ") \
|
$(find intern/wizapp/src -type f -name "*.c" | xargs -I{} echo -n "{} ") \
|
||||||
"
|
"
|
||||||
OUT=tiffread
|
OUT=tiffread
|
||||||
|
|
||||||
|
|||||||
92
src/main.c
92
src/main.c
@@ -4,17 +4,23 @@
|
|||||||
#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>
|
||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
|
#include <pthread.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
|
#define MAIN_ARENA_CAPACITY 50 * 1024 * 1024
|
||||||
|
|
||||||
#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
|
||||||
@@ -29,28 +35,33 @@ struct point {
|
|||||||
u32 y;
|
u32 y;
|
||||||
};
|
};
|
||||||
|
|
||||||
Point point_from_index(u32 index, u32 w);
|
internal void write_debug_ppm6(const Image *img);
|
||||||
void write_debug_ppm6(const Image *img);
|
internal void *load_tiff_image(void *args);
|
||||||
SDL_Texture *image_to_texture(SDL_Renderer *renderer, const Image *img);
|
internal SDL_Rect get_dest_rect(SDL_Surface *surface);
|
||||||
|
|
||||||
|
typedef struct img_thread_args ImgThreadArgs;
|
||||||
|
struct img_thread_args {
|
||||||
|
const char *filename;
|
||||||
|
Arena *arena;
|
||||||
|
};
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
int exit_code = EXIT_SUCCESS;
|
int exit_code = EXIT_SUCCESS;
|
||||||
|
|
||||||
Arena *arena = NULL;
|
Arena *arena = NULL;
|
||||||
if (!wapp_mem_arena_init(&arena, 10 * 1024 * 1024)) {
|
if (!wapp_mem_arena_init(&arena, MAIN_ARENA_CAPACITY)) {
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
}
|
}
|
||||||
|
|
||||||
const char *file_to_open = argc > 1 ? argv[1] : "./resources/test.tif";
|
const char *file_to_open = argc > 1 ? argv[1] : "./resources/test.tif";
|
||||||
|
|
||||||
Image *img = read_baseline_tiff(file_to_open, arena);
|
pthread_t img_thread;
|
||||||
if (!img) {
|
ImgThreadArgs args = {.filename = file_to_open, .arena = arena};
|
||||||
|
if (pthread_create(&img_thread, NULL, load_tiff_image, &args) != 0) {
|
||||||
exit_code = EXIT_FAILURE;
|
exit_code = EXIT_FAILURE;
|
||||||
goto MAIN_DESTROY_ARENA;
|
goto MAIN_DESTROY_ARENA;
|
||||||
}
|
}
|
||||||
|
|
||||||
write_debug_ppm6(img);
|
|
||||||
|
|
||||||
if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
|
if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
|
||||||
exit_code = EXIT_FAILURE;
|
exit_code = EXIT_FAILURE;
|
||||||
goto MAIN_DESTROY_ARENA;
|
goto MAIN_DESTROY_ARENA;
|
||||||
@@ -71,18 +82,19 @@ int main(int argc, char *argv[]) {
|
|||||||
goto MAIN_DESTROY_WINDOW;
|
goto MAIN_DESTROY_WINDOW;
|
||||||
}
|
}
|
||||||
|
|
||||||
SDL_Texture *texture = image_to_texture(renderer, img);
|
SDL_Surface *surface = NULL;
|
||||||
if (!texture) {
|
if (pthread_join(img_thread, (void **)&surface) != 0 || !surface) {
|
||||||
exit_code = EXIT_FAILURE;
|
exit_code = EXIT_FAILURE;
|
||||||
goto MAIN_DESTROY_RENDERER;
|
goto MAIN_DESTROY_RENDERER;
|
||||||
}
|
}
|
||||||
|
|
||||||
SDL_Rect dest = {
|
SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, surface);
|
||||||
.h = img->height,
|
if (!texture) {
|
||||||
.w = img->width,
|
exit_code = EXIT_FAILURE;
|
||||||
.x = (WINDOW_WIDTH - img->width) / 2,
|
goto MAIN_DESTROY_SURFACE;
|
||||||
.y = (WINDOW_HEIGHT - img->height) / 2,
|
}
|
||||||
};
|
|
||||||
|
SDL_Rect dest = get_dest_rect(surface);
|
||||||
|
|
||||||
bool running = true;
|
bool running = true;
|
||||||
|
|
||||||
@@ -108,6 +120,9 @@ int main(int argc, char *argv[]) {
|
|||||||
|
|
||||||
SDL_DestroyTexture(texture);
|
SDL_DestroyTexture(texture);
|
||||||
|
|
||||||
|
MAIN_DESTROY_SURFACE:
|
||||||
|
SDL_FreeSurface(surface);
|
||||||
|
|
||||||
MAIN_DESTROY_RENDERER:
|
MAIN_DESTROY_RENDERER:
|
||||||
SDL_DestroyRenderer(renderer);
|
SDL_DestroyRenderer(renderer);
|
||||||
|
|
||||||
@@ -123,17 +138,7 @@ MAIN_DESTROY_ARENA:
|
|||||||
return exit_code;
|
return exit_code;
|
||||||
}
|
}
|
||||||
|
|
||||||
Point point_from_index(u32 index, u32 w) {
|
internal void write_debug_ppm6(const Image *img) {
|
||||||
Point out = {0};
|
|
||||||
|
|
||||||
out.x = index % w;
|
|
||||||
|
|
||||||
out.y = (index - (index % w)) / w;
|
|
||||||
|
|
||||||
return out;
|
|
||||||
}
|
|
||||||
|
|
||||||
void write_debug_ppm6(const Image *img) {
|
|
||||||
FILE *out = fopen("test.ppm", "wb");
|
FILE *out = fopen("test.ppm", "wb");
|
||||||
|
|
||||||
char magic[] = {'P', '6', '\n'};
|
char magic[] = {'P', '6', '\n'};
|
||||||
@@ -154,16 +159,19 @@ void write_debug_ppm6(const Image *img) {
|
|||||||
fclose(out);
|
fclose(out);
|
||||||
}
|
}
|
||||||
|
|
||||||
SDL_Texture *image_to_texture(SDL_Renderer *renderer, const Image *img) {
|
internal void *load_tiff_image(void *args) {
|
||||||
SDL_Texture *output = NULL;
|
ImgThreadArgs *img_args = (ImgThreadArgs *)args;
|
||||||
if (!renderer || !img) {
|
Image *img = read_baseline_tiff(img_args->filename, img_args->arena);
|
||||||
goto IMAGE_TO_TEXTURE_RETURN;
|
if (!img) {
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
write_debug_ppm6(img);
|
||||||
|
|
||||||
SDL_Surface *surface = SDL_CreateRGBSurface(0, img->width, img->height, 32,
|
SDL_Surface *surface = SDL_CreateRGBSurface(0, img->width, img->height, 32,
|
||||||
RMASK, GMASK, BMASK, AMASK);
|
RMASK, GMASK, BMASK, AMASK);
|
||||||
if (!surface) {
|
if (!surface) {
|
||||||
goto IMAGE_TO_TEXTURE_RETURN;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
SDL_LockSurface(surface);
|
SDL_LockSurface(surface);
|
||||||
@@ -177,10 +185,18 @@ SDL_Texture *image_to_texture(SDL_Renderer *renderer, const Image *img) {
|
|||||||
}
|
}
|
||||||
SDL_UnlockSurface(surface);
|
SDL_UnlockSurface(surface);
|
||||||
|
|
||||||
output = SDL_CreateTextureFromSurface(renderer, surface);
|
return surface;
|
||||||
|
}
|
||||||
SDL_FreeSurface(surface);
|
|
||||||
|
internal SDL_Rect get_dest_rect(SDL_Surface *surface) {
|
||||||
IMAGE_TO_TEXTURE_RETURN:
|
f64 ratio = (f64)(surface->h) / (f64)(surface->w);
|
||||||
return output;
|
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,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@@ -6,141 +6,7 @@
|
|||||||
extern "C" {
|
extern "C" {
|
||||||
#endif // __cplusplus
|
#endif // __cplusplus
|
||||||
|
|
||||||
#include "aliases.h"
|
|
||||||
#include "image.h"
|
#include "image.h"
|
||||||
#include <stdbool.h>
|
|
||||||
|
|
||||||
#define TYPE_MAX_COUNT (UINT8_MAX * sizeof(const char *))
|
|
||||||
|
|
||||||
typedef struct short_long_value ShortLongValue;
|
|
||||||
struct short_long_value {
|
|
||||||
union {
|
|
||||||
u16 short_val;
|
|
||||||
u32 long_val;
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
typedef struct hdr TiffHdr;
|
|
||||||
struct hdr {
|
|
||||||
u16 order;
|
|
||||||
u16 magic;
|
|
||||||
u32 first_ifd_offset;
|
|
||||||
};
|
|
||||||
|
|
||||||
typedef struct field TiffField;
|
|
||||||
struct field {
|
|
||||||
u16 tag;
|
|
||||||
u16 type;
|
|
||||||
u32 count;
|
|
||||||
u32 value_offset;
|
|
||||||
};
|
|
||||||
|
|
||||||
typedef struct IFD TiffIFD;
|
|
||||||
struct IFD {
|
|
||||||
u16 count;
|
|
||||||
TiffField *fields;
|
|
||||||
u32 next_ifd;
|
|
||||||
};
|
|
||||||
|
|
||||||
// clang-format off
|
|
||||||
enum tiff_byte_order {
|
|
||||||
TIFF_ORDER_LITTLE_ENDIAN = 0x4949,
|
|
||||||
TIFF_ORDER_BIG_ENDIAN = 0x4d4d,
|
|
||||||
};
|
|
||||||
|
|
||||||
enum tiff_public_tags {
|
|
||||||
#include "tiff_public_tags.inc"
|
|
||||||
};
|
|
||||||
|
|
||||||
enum tiff_field_types {
|
|
||||||
#include "tiff_field_types.inc"
|
|
||||||
};
|
|
||||||
|
|
||||||
typedef struct field_type TiffFieldType;
|
|
||||||
struct field_type {
|
|
||||||
const char *name;
|
|
||||||
u16 byte_count;
|
|
||||||
};
|
|
||||||
|
|
||||||
internal TiffFieldType field_types[TYPE_MAX_COUNT] = {
|
|
||||||
#define TIFF_TYPE_LOOKUP
|
|
||||||
#include "tiff_field_types.inc"
|
|
||||||
#undef TIFF_TYPE_LOOKUP
|
|
||||||
};
|
|
||||||
|
|
||||||
typedef enum tiff_image_type_names {
|
|
||||||
TIFF_IMAGE_TYPE_INVALID = 0,
|
|
||||||
TIFF_IMAGE_TYPE_BILEVEL = 1,
|
|
||||||
TIFF_IMAGE_TYPE_GRAYSCALE = TIFF_PUBLIC_TAG_BITS_PER_SAMPLE,
|
|
||||||
TIFF_IMAGE_TYPE_PALETTE = TIFF_PUBLIC_TAG_BITS_PER_SAMPLE | TIFF_PUBLIC_TAG_COLOR_MAP,
|
|
||||||
TIFF_IMAGE_TYPE_RGB = TIFF_PUBLIC_TAG_BITS_PER_SAMPLE | TIFF_PUBLIC_TAG_SAMPLES_PER_PIXEL,
|
|
||||||
} TiffImageType;
|
|
||||||
|
|
||||||
enum tiff_extra_samples {
|
|
||||||
TIFF_EXTRA_SAMPLE_UNSPECIFIED = 0x0000,
|
|
||||||
TIFF_EXTRA_SAMPLE_ASSOCIATED_ALPHA = 0x0001,
|
|
||||||
TIFF_EXTRA_SAMPLE_UNASSOCIATED_ALPHA = 0x0002,
|
|
||||||
};
|
|
||||||
|
|
||||||
enum tiff_compression {
|
|
||||||
TIFF_COMPRESSION_UNCOMPRESSED = 0x0001,
|
|
||||||
TIFF_COMPRESSION_CCITT_1D = 0x0002,
|
|
||||||
TIFF_COMPRESSION_GROUP_3_FAX = 0x0003,
|
|
||||||
TIFF_COMPRESSION_GROUP_4_FAX = 0x0004,
|
|
||||||
TIFF_COMPRESSION_LZW = 0x0005,
|
|
||||||
TIFF_COMPRESSION_JPEG = 0x0006,
|
|
||||||
TIFF_COMPRESSION_PACK_BITS = 0x8005,
|
|
||||||
};
|
|
||||||
|
|
||||||
enum tiff_photometric_interpretation {
|
|
||||||
TIFF_PHOTOMETRIC_INTERPRETATION_WHITE_IS_ZERO = 0x0000,
|
|
||||||
TIFF_PHOTOMETRIC_INTERPRETATION_BLACK_IS_ZERO = 0x0001,
|
|
||||||
TIFF_PHOTOMETRIC_INTERPRETATION_RGB = 0x0002,
|
|
||||||
TIFF_PHOTOMETRIC_INTERPRETATION_RGB_PALETTE = 0x0003,
|
|
||||||
TIFF_PHOTOMETRIC_INTERPRETATION_TRANSPARENCY_MASK = 0x0004,
|
|
||||||
TIFF_PHOTOMETRIC_INTERPRETATION_CMYK = 0x0005,
|
|
||||||
TIFF_PHOTOMETRIC_INTERPRETATION_YCbCr = 0x0006,
|
|
||||||
TIFF_PHOTOMETRIC_INTERPRETATION_CIELab = 0x0008,
|
|
||||||
|
|
||||||
TIFF_PHOTOMETRIC_INTERPRETATION_INVALID,
|
|
||||||
};
|
|
||||||
|
|
||||||
enum tiff_planar_configuration {
|
|
||||||
TIFF_PLANAR_CONFIG_CHUNKY = 0x0001,
|
|
||||||
TIFF_PLANAR_CONFIG_PLANAR = 0x0002,
|
|
||||||
};
|
|
||||||
// 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;
|
|
||||||
u32 image_width;
|
|
||||||
u32 image_length;
|
|
||||||
u32 bits_per_sample;
|
|
||||||
u32 sample_count;
|
|
||||||
bool bits_per_sample_offset;
|
|
||||||
u16 compression;
|
|
||||||
u16 photometric_interpretation;
|
|
||||||
u16 strip_offsets_type_byte_count;
|
|
||||||
ShortLongValue strip_offsets;
|
|
||||||
bool strip_offsets_offset;
|
|
||||||
u32 rows_per_strip;
|
|
||||||
u16 strip_byte_count_type_byte_count;
|
|
||||||
ShortLongValue strip_byte_counts;
|
|
||||||
bool strip_byte_counts_offset;
|
|
||||||
u32 strip_count;
|
|
||||||
u32 color_map;
|
|
||||||
u32 extra_samples;
|
|
||||||
u32 extra_samples_count;
|
|
||||||
bool extra_samples_offset;
|
|
||||||
TiffStrip *strips;
|
|
||||||
};
|
|
||||||
|
|
||||||
Image *read_baseline_tiff(const char *file, Arena *arena);
|
Image *read_baseline_tiff(const char *file, Arena *arena);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user