Compare commits

...

64 Commits

Author SHA1 Message Date
35a33e2fa6 Reformat 2024-05-26 17:56:32 +01:00
1c08881dfb Ensure pthread_join succeeded before returning true 2024-05-26 17:51:28 +01:00
7593eb4732 Add release build 2024-05-26 15:54:01 +01:00
ec2835e900 Read the image in a separate thread and ensure each strip is read in its own thread (#1)
Reviewed-on: #1
Co-authored-by: Abdelrahman <said.abdelrahman89@gmail.com>
Co-committed-by: Abdelrahman <said.abdelrahman89@gmail.com>
2024-05-26 14:44:12 +00:00
53c70156a6 Update .gitignore 2024-05-26 14:52:42 +01:00
ad4b7b40aa Update .gitignore 2024-05-26 14:52:00 +01:00
aa12b62e7e Fix compile script 2024-05-26 14:51:53 +01:00
5762ac33b0 Reorganise type definitions 2024-05-12 17:34:03 +01:00
efad22f21e Fix alpha multiplication for images with unassociated alpha 2024-05-06 00:54:25 +01:00
d278d7ee55 Multiply RGB when reading image with unassociated alpha 2024-05-06 00:37:23 +01:00
1c667f4128 Move all TIFF type definitions to the source file 2024-05-06 00:31:14 +01:00
c2391df946 Update main.c 2024-05-05 23:42:38 +01:00
b2f002b12f Increase MAIN_ARENA_CAPACITY 2024-05-05 23:41:14 +01:00
cd424bf074 Remove NULL header, ifd and image definitions 2024-05-05 23:33:53 +01:00
11798a0683 Main read functions return boolean 2024-05-05 23:26:02 +01:00
c2a55279fd Rename load_image_pixels to read_image_pixels 2024-05-05 22:57:47 +01:00
53f53874a6 Update 8-bit assertion 2024-05-05 22:51:17 +01:00
ba21bcb2f4 Switch internal reading functions to directly manipulate reader object 2024-05-05 22:46:13 +01:00
5bf78d3e01 Add comment 2024-05-05 22:13:37 +01:00
38862899d0 Read bits per sample 2024-05-05 22:05:39 +01:00
f0d4108a64 Add reading alpha 2024-05-05 21:21:48 +01:00
3e3c4d27fe Reorder type definitions 2024-05-05 19:47:20 +01:00
ed2737d330 Check field sizes against field type sizes instead of C primitives 2024-05-05 18:42:24 +01:00
9b7e07b9ad Update .gitignore 2024-05-05 16:28:30 +01:00
71b7c682db Rename read_fields to read_ifd_fields 2024-05-05 16:25:39 +01:00
a9143642a0 Declare TiffReader before attempting to open the file 2024-05-04 23:52:15 +01:00
d195086af8 Make functions internal 2024-05-04 23:48:01 +01:00
3f022acf9c Reorder functions 2024-05-04 23:46:24 +01:00
484d30d84c Add MAIN_ARENA_CAPACITY constant 2024-05-04 23:34:21 +01:00
de0235c0af Refactor reading the strip data fields into a function 2024-05-04 23:31:20 +01:00
b546ce9fa7 Calculate strip field size once 2024-05-04 22:47:23 +01:00
c98802542d Add TiffReader struct 2024-05-04 22:35:12 +01:00
9fdee24672 Refactor loading image data 2024-05-04 22:01:24 +01:00
cd698a3921 Add debug flag to compile script 2024-05-04 22:01:05 +01:00
9312c0fb6b Move TiffStrip array inside the TiffImage struct 2024-05-04 21:17:22 +01:00
51f79275de Assert image is RGB earlier 2024-05-04 20:59:57 +01:00
a508048211 Display loaded TIFF in SDL window 2024-05-04 19:15:34 +01:00
8f9edb441a Update .gitignore 2024-05-02 22:01:50 +01:00
4b52630c1e Implement reading RGB and RGBA images 2024-05-02 22:00:32 +01:00
c46535c20b Test implementation of read_strips 2024-05-01 23:53:04 +01:00
55720ac331 Fix bug with PPM writing 2024-05-01 23:52:46 +01:00
8f20c7b30a Update image creation function 2024-05-01 23:52:32 +01:00
69c13b83ea Read strip data 2024-05-01 00:17:53 +01:00
d508af1809 Refactor read_fields function 2024-04-30 22:17:17 +01:00
e371ca7160 Remove unnecessary comments 2024-04-30 22:01:55 +01:00
eaa6e52fbe Switch from using Allocator to Arena 2024-04-30 22:00:54 +01:00
3c6e5defb9 Fix compile script 2024-04-30 22:00:31 +01:00
a00e737652 Update compile script 2024-04-29 23:26:37 +01:00
45276ffcc1 Update submodule 2024-04-29 23:25:16 +01:00
2c1b844c64 Update .gitignore 2024-04-22 00:00:41 +01:00
ce7f30f7e3 Update submodules 2024-04-21 23:59:57 +01:00
38988120a3 Start implementing reading fields from scratch 2024-04-21 23:54:42 +01:00
8040dd7381 First botched attempt at reading image data :D 2024-04-21 18:55:02 +01:00
0bd448a8a9 Initial implementation of the reader 2024-04-21 02:39:34 +01:00
61f0182c3f Add macros to detect endianness 2024-04-21 02:39:10 +01:00
aae4909d30 Add libraries in compile script 2024-04-21 02:38:22 +01:00
29890a6135 Rename field types 2024-04-21 02:38:09 +01:00
7f6ffd0fea Add destroy_image function 2024-04-20 18:41:41 +01:00
3e0b1762d1 Define TiffFieldType struct 2024-04-20 18:26:42 +01:00
8b3babb3ad Add Image data structure 2024-04-20 18:26:06 +01:00
17263bf215 Update main.c to use wizapp stdlib and tiffread.h 2024-04-20 17:01:07 +01:00
5c1f80ffb6 Move tiffread definitions to separate header files 2024-04-20 17:00:36 +01:00
7584f926e0 Update .gitignore 2024-04-20 17:00:25 +01:00
02f081bf40 Add wizapp stdlib as submodule 2024-04-20 16:59:53 +01:00
13 changed files with 1267 additions and 104 deletions

4
.gitignore vendored
View File

@@ -1,5 +1,9 @@
.cache
refs
file_example*
big_endian*
float32_example.tiff
*.py
tiffread
test*
compile_commands.json

3
.gitmodules vendored Normal file
View File

@@ -0,0 +1,3 @@
[submodule "intern/wizapp"]
path = intern/wizapp
url = https://git.thewizardapprentice.com/abdelrahman/wizapp-stdlib.git

39
compile
View File

@@ -1,11 +1,42 @@
#!/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
CFLAGS="-g -Wall -Werror -pedantic -fsanitize=address -fsanitize=undefined"
INCLUDES="-Isrc"
if [[ $BUILD_TYPE == "release" ]]; then
CFLAGS="-O3 -Wall -Werror -pedantic"
else
CFLAGS="-g -Wall -Werror -pedantic -fsanitize=address -fsanitize=undefined -DDEBUG"
fi
INCLUDES="\
-I$(find ./src -type d | xargs -I{} echo -n "-I{} ") \
$(find intern/wizapp/src -type d | xargs -I{} echo -n "-I{} ") \
$(pkg-config --cflags sdl2) \
"
LIBS="\
-lm \
-pthread \
$(pkg-config --libs sdl2) \
"
SRC="\
./src/*.c \
$(find ./src -name "*.c" | xargs -I{} echo -n "{} ") \
$(find intern/wizapp/src -type f -name "*.c" | xargs -I{} echo -n "{} ") \
"
OUT=tiffread
( set -x ; $CC $CFLAGS $INCLUDES $SRC -o $OUT )
( set -x ; $CC $CFLAGS $INCLUDES $LIBS $SRC -o $OUT )

1
intern/wizapp Submodule

Submodule intern/wizapp added at b59aedad89

26
src/image/image.c Normal file
View File

@@ -0,0 +1,26 @@
#include "image.h"
#include "mem_arena.h"
#include <stddef.h>
#include <string.h>
Image *create_image(u64 width, u64 height, Pixel *data, Arena *arena) {
if (!arena) {
return NULL;
}
u64 pixel_count = width * height;
u64 buf_length = sizeof(Pixel) * pixel_count;
u64 alloc_size = sizeof(Image) + buf_length;
Image *img = wapp_mem_arena_alloc(arena, alloc_size);
if (!img) {
return NULL;
}
img->width = width;
img->height = height;
img->pixel_count = pixel_count;
memcpy(img->data, data, buf_length);
return img;
}

33
src/image/image.h Normal file
View File

@@ -0,0 +1,33 @@
#ifndef IMAGE_H
#define IMAGE_H
#include "mem_arena.h"
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
#include "aliases.h"
typedef struct pixel Pixel;
struct pixel {
u8 r;
u8 g;
u8 b;
u8 a;
};
typedef struct image Image;
struct image {
u64 width;
u64 height;
u64 pixel_count;
Pixel data[];
};
Image *create_image(u64 width, u64 height, Pixel *data, Arena *arena);
#ifdef __cplusplus
}
#endif // __cplusplus
#endif // !IMAGE_H

View File

@@ -1,97 +1,202 @@
#include "aliases.h"
#include "image.h"
#include "mem_arena.h"
#include "tiffread.h"
#include <SDL2/SDL.h>
#include <SDL2/SDL_events.h>
#include <SDL2/SDL_rect.h>
#include <SDL2/SDL_render.h>
#include <SDL2/SDL_surface.h>
#include <SDL2/SDL_video.h>
#include <netinet/in.h>
#include <stdint.h>
#include <pthread.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define TAG_MAX_COUNT (UINT16_MAX * sizeof(const char *))
// Technically, the type parameter is 2-bytes long, but there aren't so many
// types specified in the spec, so UINT8_MAX should be sufficient
#define TYPE_MAX_COUNT (UINT8_MAX * sizeof(const char *))
#define MAIN_ARENA_CAPACITY 50 * 1024 * 1024
// clang-format off
enum {
TIFF_ORDER_LITTLE_ENDIAN = 0x4949,
TIFF_ORDER_BIG_ENDIAN = 0x4d4d,
};
// clang-format on
#define WINDOW_WIDTH 800
#define WINDOW_HEIGHT 600
#define VIEW_AREA_WIDTH 700
#define VIEW_AREA_HEIGHT 500
typedef struct hdr TiffHdr;
struct hdr {
uint16_t order;
uint16_t magic;
uint32_t first_ifd_offset;
#define AMASK 0xff000000
#define BMASK 0x00ff0000
#define GMASK 0x0000ff00
#define RMASK 0x000000ff
#define WHITE 0xff
typedef struct point Point;
struct point {
u32 x;
u32 y;
};
enum tiff_public_tags {
#include "tiff_public_tags.inc"
};
internal void write_debug_ppm6(const Image *img);
internal void *load_tiff_image(void *args);
internal SDL_Rect get_dest_rect(SDL_Surface *surface);
const char *tag_names[TAG_MAX_COUNT] = {
#define TIFF_TAG_LOOKUP
#include "tiff_public_tags.inc"
#undef TIFF_TAG_LOOKUP
};
enum tiff_field_types {
#include "tiff_field_types.inc"
};
const char *filed_type_names[TYPE_MAX_COUNT] = {
#define TIFF_TYPE_LOOKUP
#include "tiff_field_types.inc"
#undef TIFF_TYPE_LOOKUP
};
typedef struct field TiffField;
struct field {
uint16_t tag;
uint16_t type;
uint32_t count;
uint32_t value_offset;
};
typedef struct IFD TiffIFD;
struct IFD {
uint16_t count;
TiffField *fields;
uint32_t next_ifd;
typedef struct img_thread_args ImgThreadArgs;
struct img_thread_args {
const char *filename;
Arena *arena;
};
int main(int argc, char *argv[]) {
const char *file_to_open = argc > 1 ? argv[1] : "./resources/test.tif";
int exit_code = EXIT_SUCCESS;
FILE *fp = fopen(file_to_open, "r");
TiffHdr header;
TiffIFD ifd = {0};
fread(&header, sizeof(TiffHdr), 1, fp);
printf("ORDER: %04x\n", header.order);
printf("MAGIC: %04x\n", header.magic);
fseek(fp, header.first_ifd_offset, SEEK_SET);
fread(&(ifd.count), sizeof(ifd.count), 1, fp);
printf("COUNT: %u\n", ifd.count);
TiffField fields[ifd.count];
memset(fields, 0, sizeof(TiffField) * ifd.count);
for (uint32_t i = 0; i < ifd.count; ++i) {
fread(fields + i, sizeof(TiffField), 1, fp);
printf("ENTRY %02u\n", i);
printf("\t TAG: %04u (%s)\n", fields[i].tag,
tag_names[fields[i].tag] ? tag_names[fields[i].tag] : "UNKNOWN");
printf("\t TYPE: 0x%04x (%s)\n", fields[i].type,
filed_type_names[fields[i].type] ? filed_type_names[fields[i].type]
: "UNKNOWN");
printf("\t COUNT: %04u\n", fields[i].count);
printf("\tVAL/OFF: %04u\n", fields[i].value_offset);
Arena *arena = NULL;
if (!wapp_mem_arena_init(&arena, MAIN_ARENA_CAPACITY)) {
return EXIT_FAILURE;
}
fclose(fp);
const char *file_to_open = argc > 1 ? argv[1] : "./resources/test.tif";
return 0;
pthread_t img_thread;
ImgThreadArgs args = {.filename = file_to_open, .arena = arena};
if (pthread_create(&img_thread, NULL, load_tiff_image, &args) != 0) {
exit_code = EXIT_FAILURE;
goto MAIN_DESTROY_ARENA;
}
if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
exit_code = EXIT_FAILURE;
goto MAIN_DESTROY_ARENA;
};
SDL_Window *window =
SDL_CreateWindow("Window", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_SHOWN);
if (!window) {
exit_code = EXIT_FAILURE;
goto MAIN_QUIT_SDL;
}
SDL_Renderer *renderer = SDL_CreateRenderer(
window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if (!renderer) {
exit_code = EXIT_FAILURE;
goto MAIN_DESTROY_WINDOW;
}
SDL_Surface *surface = NULL;
if (pthread_join(img_thread, (void **)&surface) != 0 || !surface) {
exit_code = EXIT_FAILURE;
goto MAIN_DESTROY_RENDERER;
}
SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, surface);
if (!texture) {
exit_code = EXIT_FAILURE;
goto MAIN_DESTROY_SURFACE;
}
SDL_Rect dest = get_dest_rect(surface);
bool running = true;
SDL_Event event = {0};
while (running) {
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_QUIT:
running = false;
break;
}
}
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, texture, NULL, &dest);
SDL_RenderPresent(renderer);
}
SDL_DestroyTexture(texture);
MAIN_DESTROY_SURFACE:
SDL_FreeSurface(surface);
MAIN_DESTROY_RENDERER:
SDL_DestroyRenderer(renderer);
MAIN_DESTROY_WINDOW:
SDL_DestroyWindow(window);
MAIN_QUIT_SDL:
SDL_Quit();
MAIN_DESTROY_ARENA:
wapp_mem_arena_destroy(&arena);
return exit_code;
}
internal void write_debug_ppm6(const Image *img) {
FILE *out = fopen("test.ppm", "wb");
char magic[] = {'P', '6', '\n'};
fwrite(magic, sizeof(magic), 1, out);
char size[128];
sprintf(size, "%lu %lu\n", img->width, img->height);
u64 size_length = strlen(size);
fwrite(size, size_length, 1, out);
char max[] = {'2', '5', '5', '\n'};
fwrite(max, sizeof(max), 1, out);
for (u64 i = 0; i < img->pixel_count; ++i) {
fwrite(&(img->data[i]), sizeof(u8), 3, out);
}
fclose(out);
}
internal void *load_tiff_image(void *args) {
ImgThreadArgs *img_args = (ImgThreadArgs *)args;
Image *img = read_baseline_tiff(img_args->filename, img_args->arena);
if (!img) {
return NULL;
}
write_debug_ppm6(img);
SDL_Surface *surface = SDL_CreateRGBSurface(0, img->width, img->height, 32,
RMASK, GMASK, BMASK, AMASK);
if (!surface) {
return NULL;
}
SDL_LockSurface(surface);
u64 position = 0;
for (u64 i = 0; i < img->pixel_count; i += surface->w) {
void *start = (void *)((uptr)(surface->pixels) + position);
memcpy(start, &(img->data[i]), surface->w * sizeof(Pixel));
position += surface->pitch;
}
SDL_UnlockSurface(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,
};
}

View File

@@ -0,0 +1,20 @@
#ifdef TIFF_TYPE_LOOKUP
#undef TIFF_TYPE
#define TIFF_TYPE(NAME, ID, VALUE, BYTES) [VALUE] = (TiffFieldType){.name = NAME, .byte_count = BYTES},
#else
#undef TIFF_TYPE
#define TIFF_TYPE(NAME, ID, VALUE, BYTES) ID = VALUE,
#endif /* ifdef TIFF_TYPE_LOOKUP */
TIFF_TYPE("UNSIGNED BYTE" , TIFF_FIELD_TYPE_BYTE , 0x0001, 1) // 8-bit unsigned integer
TIFF_TYPE("ASCII" , TIFF_FIELD_TYPE_ASCII , 0x0002, 1) // 8-bit byte that contains a 7-bit ASCII code; the last byte must be NUL (binary zero)
TIFF_TYPE("UNSIGNED SHORT" , TIFF_FIELD_TYPE_SHORT , 0x0003, 2) // 16-bit (2-byte) unsigned integer
TIFF_TYPE("UNSIGNED LONG" , TIFF_FIELD_TYPE_LONG , 0x0004, 4) // 32-bit (4-byte) unsigned integer
TIFF_TYPE("UNSIGNED RATIONAL", TIFF_FIELD_TYPE_RATIONAL , 0x0005, 8) // Two LONGs: the first represents the numerator of a fraction; the second, the denominator
TIFF_TYPE("SIGNED BYTE" , TIFF_FIELD_TYPE_SBYTE , 0x0006, 1) // An 8-bit signed (twos-complement) integer
TIFF_TYPE("UNDEFINED" , TIFF_FIELD_TYPE_UNDEFINED , 0x0007, 1) // An 8-bit byte that may contain anything, depending on the definition of the field
TIFF_TYPE("SIGNED SHORT" , TIFF_FIELD_TYPE_SSHORT , 0x0008, 2) // A 16-bit (2-byte) signed (twos-complement) integer
TIFF_TYPE("SIGNED LONG" , TIFF_FIELD_TYPE_SLONG , 0x0009, 4) // A 32-bit (4-byte) signed (twos-complement) integer
TIFF_TYPE("SIGNED RATIONAL" , TIFF_FIELD_TYPE_SRATIONAL , 0x000a, 8) // Two SLONGs: the first represents the numerator of a fraction, the second the denominator
TIFF_TYPE("FLOAT" , TIFF_FIELD_TYPE_FLOAT , 0x000b, 4) // Single precision (4-byte) IEEE format
TIFF_TYPE("DOUBLE" , TIFF_FIELD_TYPE_DOUBLE , 0x000c, 8) // Double precision (8-byte) IEEE format

936
src/tiff/tiffread.c Normal file
View File

@@ -0,0 +1,936 @@
#include "tiffread.h"
#include "aliases.h"
#include "endianness.h"
#include "image.h"
#include "mem_arena.h"
#include <assert.h>
#include <bits/pthreadtypes.h>
#include <math.h>
#include <netinet/in.h>
#include <pthread.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#define TIFF_MAGIC 0x002a
#define TIFF_FILENAME_MIN_LENGTH 6
#define TIFF_EXT_MAX_LENGTH 5
#define IS_TIFF_EXTENSION(EXT) \
(strncmp(EXT, ".tif", 4) == 0 || strncmp(EXT, ".tiff", 5) == 0)
#define NULL_TIFF_ALPHA ((TiffAlpha){0})
#define INVALID_SAMPLE_COUNT 0
#define INVALID_ROWS_PER_STRIP 0
#define INVALID_STRIP_COUNT 0
#define INVALID_ALPHA_OFFSET -1
#define TIFF_SHORT_BYTE_COUNT field_types[TIFF_FIELD_TYPE_SHORT].byte_count
#define TIFF_LONG_BYTE_COUNT field_types[TIFF_FIELD_TYPE_LONG].byte_count
#define RGB_SAMPLE_COUNT 3
#define MIN_BITS_PER_SAMPLE 8
#define TEMP_ARENA_CAPACITY (20 * 1024 * 1024)
#define TYPE_MAX_COUNT (UINT8_MAX * sizeof(const char *))
// 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 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;
};
typedef enum {
ALPHA_TYPE_UNDEFINED = 0,
ALPHA_TYPE_ASSOCIATED,
ALPHA_TYPE_UNASSOCIATED,
} AlphaType;
typedef struct tiff_alpha TiffAlpha;
struct tiff_alpha {
AlphaType type;
u32 sample_offset;
};
typedef struct tiff_sample_bits TiffSampleBits;
struct tiff_sample_bits {
u16 r;
u16 g;
u16 b;
u16 a;
};
typedef struct strip_data_field StripDataField;
struct strip_data_field {
u32 *strip_value;
const ShortLongValue *value_from_file;
u32 strip_index;
u32 length_in_bytes;
u16 type_byte_count;
};
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;
TiffAlpha alpha;
TiffSampleBits rgba_bits_per_sample;
TiffStrip *strips;
};
typedef struct tiff_reader TiffReader;
struct tiff_reader {
const char *filename;
FILE *fp;
TiffHdr header;
TiffIFD ifd;
TiffImage img;
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);
internal void read_alpha(TiffReader *reader);
internal void read_bits_per_sample(TiffReader *reader);
internal bool read_image_pixels(TiffReader *reader, Arena *arena);
internal bool read_strip_data(TiffReader *reader, Arena *arena);
internal bool read_strips(TiffReader *reader);
internal void *read_strip(void *arg);
internal void read_strip_data_field(const TiffReader *reader,
StripDataField *field);
internal bool read_field(const TiffField *field, TiffImage *img);
internal bool validate_image_type(const TiffImage *img);
internal void fread_with_offset(FILE *fp, void *dst, u64 count, u64 offset);
internal bool valid_tiff_file(const char *file);
internal f32 u8_normalise(u8 value);
internal u8 u8_denormalise(f32 value);
Image *read_baseline_tiff(const char *file, Arena *arena) {
Image *img_out = NULL;
if (!file || !arena || !valid_tiff_file(file)) {
goto READ_BASELINE_RETURN_IMG;
}
TiffReader reader = {.filename = file, .fp = fopen(file, "rb")};
if (!reader.fp) {
goto READ_BASELINE_RETURN_IMG;
}
Arena *temp_arena = NULL;
if (!wapp_mem_arena_init(&temp_arena, TEMP_ARENA_CAPACITY)) {
goto READ_BASELINE_FILE_CLEANUP;
}
if (!read_tiff_header(&reader)) {
goto READ_BASELINE_DESTROY_ARENA;
}
if (!read_ifd(&reader, temp_arena)) {
goto READ_BASELINE_DESTROY_ARENA;
}
if (!read_ifd_fields(&reader)) {
goto READ_BASELINE_DESTROY_ARENA;
}
assert((reader.img.type == TIFF_IMAGE_TYPE_RGB) &&
"Currently, only RGB images are supported");
bool image_is_8_bit =
(reader.img.rgba_bits_per_sample.r == MIN_BITS_PER_SAMPLE &&
reader.img.rgba_bits_per_sample.g == MIN_BITS_PER_SAMPLE &&
reader.img.rgba_bits_per_sample.b == MIN_BITS_PER_SAMPLE &&
reader.img.rgba_bits_per_sample.a == MIN_BITS_PER_SAMPLE);
assert(image_is_8_bit && "Currently, only 8-bit images are supported");
if (!read_image_pixels(&reader, temp_arena)) {
goto READ_BASELINE_DESTROY_ARENA;
}
img_out = create_image(reader.img.image_width, reader.img.image_length,
reader.pixels, arena);
READ_BASELINE_DESTROY_ARENA:
wapp_mem_arena_destroy(&temp_arena);
READ_BASELINE_FILE_CLEANUP:
fclose(reader.fp);
READ_BASELINE_RETURN_IMG:
return img_out;
}
internal bool read_tiff_header(TiffReader *reader) {
fread_with_offset(reader->fp, &(reader->header), sizeof(TiffHdr), 0);
switch (reader->header.order) {
case TIFF_ORDER_LITTLE_ENDIAN:
if (IS_BIG_ENDIAN) {
reader->header.magic = htons(reader->header.magic);
reader->header.first_ifd_offset = htonl(reader->header.first_ifd_offset);
}
break;
case TIFF_ORDER_BIG_ENDIAN:
if (IS_LITTLE_ENDIAN) {
reader->header.magic = ntohs(reader->header.magic);
reader->header.first_ifd_offset = ntohl(reader->header.first_ifd_offset);
}
break;
default:
return false;
}
if (reader->header.magic != TIFF_MAGIC) {
return false;
}
return true;
}
internal bool read_ifd(TiffReader *reader, Arena *arena) {
fread_with_offset(reader->fp, &(reader->ifd.count), sizeof(reader->ifd.count),
reader->header.first_ifd_offset);
switch (reader->header.order) {
case TIFF_ORDER_LITTLE_ENDIAN:
if (IS_BIG_ENDIAN) {
reader->ifd.count = htons(reader->ifd.count);
}
break;
case TIFF_ORDER_BIG_ENDIAN:
if (IS_LITTLE_ENDIAN) {
reader->ifd.count = ntohs(reader->ifd.count);
}
break;
}
u64 field_byte_count = sizeof(TiffField) * reader->ifd.count;
reader->ifd.fields =
(TiffField *)wapp_mem_arena_alloc(arena, field_byte_count);
if (!(reader->ifd.fields)) {
return false;
}
fread(reader->ifd.fields, field_byte_count, 1, reader->fp);
fread(&(reader->ifd.next_ifd), sizeof(reader->ifd.next_ifd), 1, reader->fp);
return true;
}
internal bool read_ifd_fields(TiffReader *reader) {
reader->img.type = TIFF_IMAGE_TYPE_BILEVEL;
for (u64 i = 0; i < reader->ifd.count; ++i) {
TiffField *field = &(reader->ifd.fields[i]);
switch (reader->header.order) {
case TIFF_ORDER_BIG_ENDIAN:
if (IS_LITTLE_ENDIAN) {
field->tag = ntohs(field->tag);
field->type = ntohs(field->type);
field->count = ntohl(field->count);
if (field_types[field->type].byte_count > 2 ||
field_types[field->type].byte_count * field->count > 4) {
field->value_offset = ntohl(field->value_offset);
} else {
field->value_offset = ntohs(field->value_offset);
}
}
break;
case TIFF_ORDER_LITTLE_ENDIAN:
if (IS_BIG_ENDIAN) {
field->tag = htons(field->tag);
field->type = htons(field->type);
field->count = htonl(field->count);
if (field_types[field->type].byte_count > 2 ||
field_types[field->type].byte_count * field->count > 4) {
field->value_offset = htonl(field->value_offset);
} else {
field->value_offset = htons(field->value_offset);
}
}
break;
}
if (!read_field(field, &(reader->img))) {
return false;
}
}
if (!validate_image_type(&(reader->img))) {
return false;
}
read_alpha(reader);
read_bits_per_sample(reader);
#ifdef DEBUG
// clang-format off
printf("SIZE (width, height) : %u, %u\n", reader->img.image_width, reader->img.image_length);
printf("SAMPLES (bits per sample, count, is offset) : %u, %u, %u\n", reader->img.bits_per_sample, reader->img.sample_count, reader->img.bits_per_sample_offset);
printf("EXTRA SAMPLES (samples, count, is offset) : %u, %u, %u\n", reader->img.extra_samples, reader->img.extra_samples_count, reader->img.extra_samples_offset);
printf("ALPHA (type, offset) : %u, %u\n", reader->img.alpha.type, reader->img.alpha.sample_offset);
printf("SAMPLES PER PIXEL : %u\n", reader->img.sample_count);
printf("PHOTOMETRIC INTERPRETATION : %u\n", reader->img.photometric_interpretation);
printf("ROWS PER STRIP (rows, strip count) : %u, %u\n", reader->img.rows_per_strip, reader->img.strip_count);
printf("STRIP OFFSETS (offsets, is offset) : %u, %u\n", reader->img.strip_offsets.long_val, reader->img.strip_offsets_offset);
printf("STRIP BYTES (byte count, is offset) : %u, %u\n", reader->img.strip_byte_counts.long_val, reader->img.strip_byte_counts_offset);
// clang-format on
#endif
return true;
}
internal void read_alpha(TiffReader *reader) {
reader->img.alpha = NULL_TIFF_ALPHA;
if (reader->img.extra_samples_count == 0) {
return;
}
u64 byte_count = TIFF_SHORT_BYTE_COUNT * reader->img.extra_samples_count;
u16 samples[reader->img.extra_samples_count];
memset(samples, 0, byte_count);
if (reader->img.extra_samples_offset) {
fread_with_offset(reader->fp, samples, byte_count,
reader->img.extra_samples);
switch (reader->header.order) {
case TIFF_ORDER_BIG_ENDIAN:
if (IS_LITTLE_ENDIAN) {
for (u64 i = 0; i < reader->img.extra_samples_count; ++i) {
samples[i] = ntohs(samples[i]);
}
}
break;
case TIFF_ORDER_LITTLE_ENDIAN:
if (IS_BIG_ENDIAN) {
for (u64 i = 0; i < reader->img.extra_samples_count; ++i) {
samples[i] = htons(samples[i]);
}
}
break;
}
} else {
memcpy(samples, &(reader->img.extra_samples), byte_count);
}
for (u32 i = 0; i < reader->img.extra_samples_count; ++i) {
u16 *sample = &(samples[i]);
if (*sample == TIFF_EXTRA_SAMPLE_ASSOCIATED_ALPHA ||
*sample == TIFF_EXTRA_SAMPLE_UNASSOCIATED_ALPHA) {
reader->img.alpha.type = *sample == TIFF_EXTRA_SAMPLE_ASSOCIATED_ALPHA
? ALPHA_TYPE_ASSOCIATED
: ALPHA_TYPE_UNASSOCIATED;
reader->img.alpha.sample_offset = i;
break;
}
}
}
internal void read_bits_per_sample(TiffReader *reader) {
TiffAlpha alpha = reader->img.alpha;
u64 main_samples = reader->img.sample_count - reader->img.extra_samples_count;
u64 byte_count = TIFF_SHORT_BYTE_COUNT * reader->img.sample_count;
u16 bits_per_sample[reader->img.sample_count];
memset(bits_per_sample, 0, byte_count);
if (reader->img.bits_per_sample_offset) {
fread_with_offset(reader->fp, bits_per_sample, byte_count,
reader->img.bits_per_sample);
switch (reader->header.order) {
case TIFF_ORDER_BIG_ENDIAN:
if (IS_LITTLE_ENDIAN) {
for (u64 i = 0; i < reader->img.sample_count; ++i) {
bits_per_sample[i] = ntohs(bits_per_sample[i]);
}
}
break;
case TIFF_ORDER_LITTLE_ENDIAN:
if (IS_BIG_ENDIAN) {
for (u64 i = 0; i < reader->img.sample_count; ++i) {
bits_per_sample[i] = htons(bits_per_sample[i]);
}
}
break;
}
} else {
memcpy(bits_per_sample, &(reader->img.bits_per_sample), byte_count);
}
TiffSampleBits *bits = &(reader->img.rgba_bits_per_sample);
*bits = (TiffSampleBits){0};
memcpy(bits, bits_per_sample, TIFF_SHORT_BYTE_COUNT * main_samples);
// Set missing samples if image doesn't have all RGB samples
if (main_samples < RGB_SAMPLE_COUNT) {
u64 count = RGB_SAMPLE_COUNT - main_samples;
for (u64 i = 0; i < count; ++i) {
u16 *value = &(((u16 *)(&bits))[main_samples + i]);
*value = MIN_BITS_PER_SAMPLE;
}
}
if (alpha.type == ALPHA_TYPE_UNDEFINED) {
bits->a = MIN_BITS_PER_SAMPLE;
} else {
void *alpha_sample = &(bits_per_sample[main_samples + alpha.sample_offset]);
memcpy(&(bits->a), alpha_sample, TIFF_SHORT_BYTE_COUNT);
}
bits->r = bits->r < MIN_BITS_PER_SAMPLE ? MIN_BITS_PER_SAMPLE : bits->r;
bits->g = bits->g < MIN_BITS_PER_SAMPLE ? MIN_BITS_PER_SAMPLE : bits->g;
bits->b = bits->b < MIN_BITS_PER_SAMPLE ? MIN_BITS_PER_SAMPLE : bits->b;
bits->a = bits->a < MIN_BITS_PER_SAMPLE ? MIN_BITS_PER_SAMPLE : bits->a;
}
internal bool read_image_pixels(TiffReader *reader, Arena *arena) {
reader->pixels = NULL;
u64 img_byte_count =
sizeof(Pixel) * reader->img.image_width * reader->img.image_length;
if (!read_strip_data(reader, arena)) {
return false;
}
#ifdef DEBUG
for (u32 i = 0; i < reader->img.strip_count; ++i) {
printf("%u, %u\n", reader->img.strips[i].offset,
reader->img.strips[i].byte_count);
}
#endif
reader->pixels = wapp_mem_arena_alloc(arena, img_byte_count);
if (!reader->pixels) {
return false;
}
return read_strips(reader);
}
internal bool read_strip_data(TiffReader *reader, Arena *arena) {
reader->img.strips =
wapp_mem_arena_alloc(arena, sizeof(TiffStrip) * reader->img.strip_count);
if (!(reader->img.strips)) {
return false;
}
u32 offsets_total_bytes =
reader->img.strip_count * reader->img.strip_offsets_type_byte_count;
u32 byte_count_total_bytes =
reader->img.strip_count * reader->img.strip_byte_count_type_byte_count;
if ((!(reader->img.strip_offsets_offset) &&
offsets_total_bytes > TIFF_LONG_BYTE_COUNT) ||
(!(reader->img.strip_byte_counts_offset) &&
byte_count_total_bytes > TIFF_LONG_BYTE_COUNT)) {
return false;
}
u16 offset_size = reader->img.strip_offsets_type_byte_count;
u16 counts_size = reader->img.strip_byte_count_type_byte_count;
for (u32 i = 0; i < reader->img.strip_count; ++i) {
TiffStrip *strip = &(reader->img.strips[i]);
StripDataField offsets = {
.strip_value = &(strip->offset),
.value_from_file = &(reader->img.strip_offsets),
.strip_index = i,
.length_in_bytes = offsets_total_bytes,
.type_byte_count = offset_size,
};
StripDataField counts = {
.strip_value = &(strip->byte_count),
.value_from_file = &(reader->img.strip_byte_counts),
.strip_index = i,
.length_in_bytes = byte_count_total_bytes,
.type_byte_count = counts_size,
};
read_strip_data_field(reader, &offsets);
read_strip_data_field(reader, &counts);
}
return true;
}
internal bool read_strips(TiffReader *reader) {
u64 position = 0;
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] = (StripThreadArgs){
.fp = fopen(reader->filename, "rb"),
.strip = strip,
.pixel_count = pixel_count,
.samples_per_pixel = reader->img.sample_count,
.main_samples =
reader->img.sample_count - reader->img.extra_samples_count,
.alpha = reader->img.alpha,
.pixel_write_start = &(reader->pixels[position]),
};
if (!args[i].fp) {
for (u64 j = 0; j < i; ++j) {
fclose(args[j].fp);
}
return false;
}
position += pixel_count;
}
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;
}
fclose(args[i].fp);
}
return image_loaded;
}
internal void *read_strip(void *arg) {
StripThreadArgs *args = (StripThreadArgs *)arg;
Pixel *p;
u64 write_offset = 0;
u64 read_offset;
u64 alpha_offset;
for (u64 j = 0; j < args->pixel_count; ++j) {
p = args->pixel_write_start + write_offset;
read_offset = args->strip->offset + j * args->samples_per_pixel;
alpha_offset = read_offset + args->main_samples + args->alpha.sample_offset;
fread_with_offset(args->fp, p, args->main_samples, read_offset);
if (args->alpha.type == ALPHA_TYPE_UNDEFINED) {
p->a = 255;
} else {
fread_with_offset(args->fp, &(p->a), 1, alpha_offset);
if (args->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);
}
}
++write_offset;
}
return NULL;
}
internal void read_strip_data_field(const TiffReader *reader,
StripDataField *field) {
u16 tiff_long_byte_count = field_types[TIFF_FIELD_TYPE_LONG].byte_count;
bool value_is_file_offset = field->length_in_bytes > tiff_long_byte_count;
u32 offset = field->type_byte_count * field->strip_index;
if (!value_is_file_offset) {
u8 *value = (u8 *)&(field->value_from_file->long_val);
memcpy(field->strip_value, value + offset, field->type_byte_count);
return;
}
fread_with_offset(reader->fp, field->strip_value, field->type_byte_count,
field->value_from_file->long_val + offset);
switch (reader->header.order) {
case TIFF_ORDER_BIG_ENDIAN:
if (IS_LITTLE_ENDIAN) {
*(field->strip_value) = field->type_byte_count > TIFF_SHORT_BYTE_COUNT
? ntohl(*(field->strip_value))
: ntohs(*(field->strip_value));
}
break;
case TIFF_ORDER_LITTLE_ENDIAN:
if (IS_BIG_ENDIAN) {
*(field->strip_value) = field->type_byte_count > TIFF_SHORT_BYTE_COUNT
? htonl(*(field->strip_value))
: htons(*(field->strip_value));
}
break;
}
}
internal bool read_field(const TiffField *field, TiffImage *img) {
switch (field->tag) {
case TIFF_PUBLIC_TAG_IMAGE_WIDTH:
if (field->count > 1) {
return false;
}
img->image_width = field->value_offset;
break;
case TIFF_PUBLIC_TAG_IMAGE_LENGTH:
if (field->count > 1) {
return false;
}
img->image_length = field->value_offset;
break;
case TIFF_PUBLIC_TAG_BITS_PER_SAMPLE:
img->type |= field->tag;
if (img->sample_count == INVALID_SAMPLE_COUNT) {
img->sample_count = field->count;
} else if (img->sample_count != field->count) {
return false;
}
img->bits_per_sample = field->value_offset;
img->bits_per_sample_offset =
img->sample_count * field_types[field->type].byte_count > 4;
break;
case TIFF_PUBLIC_TAG_COMPRESSION:
if (field->count > 1 ||
field->value_offset != TIFF_COMPRESSION_UNCOMPRESSED) {
return false;
}
img->compression = field->value_offset;
break;
case TIFF_PUBLIC_TAG_PHOTOMETRIC_INTERPRETATION:
if (field->count > 1) {
return false;
}
img->photometric_interpretation = field->value_offset;
break;
case TIFF_PUBLIC_TAG_STRIP_OFFSETS:
img->strip_offsets_type_byte_count = field_types[field->type].byte_count;
if (img->strip_offsets_type_byte_count == TIFF_SHORT_BYTE_COUNT) {
img->strip_offsets.short_val = field->value_offset;
} else if (img->strip_offsets_type_byte_count == TIFF_LONG_BYTE_COUNT) {
img->strip_offsets.long_val = field->value_offset;
}
if (img->strip_count == INVALID_STRIP_COUNT) {
img->strip_count = field->count;
} else if (img->strip_count != field->count) {
return false;
}
img->strip_offsets_offset =
img->strip_count * img->strip_offsets_type_byte_count > 4;
break;
case TIFF_PUBLIC_TAG_SAMPLES_PER_PIXEL:
img->type |= field->tag;
if (field->count > 1) {
return false;
}
img->sample_count = field->value_offset;
break;
case TIFF_PUBLIC_TAG_ROWS_PER_STRIP:
if (field->count > 1) {
return false;
}
img->rows_per_strip = field->value_offset;
if (img->image_length > 0) {
img->strip_count =
floor((f64)(img->image_length + img->rows_per_strip - 1) /
img->rows_per_strip);
} else {
img->strip_count = INVALID_STRIP_COUNT;
}
break;
case TIFF_PUBLIC_TAG_STRIP_BYTE_COUNTS:
img->strip_byte_count_type_byte_count = field_types[field->type].byte_count;
if (img->strip_byte_count_type_byte_count == TIFF_SHORT_BYTE_COUNT) {
img->strip_byte_counts.short_val = field->value_offset;
} else if (img->strip_byte_count_type_byte_count == TIFF_LONG_BYTE_COUNT) {
img->strip_byte_counts.long_val = field->value_offset;
}
if (img->strip_count == INVALID_STRIP_COUNT) {
img->strip_count = field->count;
} else if (img->strip_count != field->count) {
return false;
}
img->strip_byte_counts_offset =
img->strip_count * img->strip_byte_count_type_byte_count > 4;
break;
case TIFF_PUBLIC_TAG_COLOR_MAP:
img->type |= field->tag;
break;
case TIFF_PUBLIC_TAG_EXTRA_SAMPLES:
img->extra_samples_count = field->count;
img->extra_samples = field->value_offset;
img->extra_samples_offset =
img->extra_samples_count * field_types[field->type].byte_count > 4;
break;
case TIFF_PUBLIC_TAG_PLANAR_CONFIGURATION:
if (field->count > 1 || field->value_offset != TIFF_PLANAR_CONFIG_CHUNKY) {
return false;
}
break;
}
return true;
}
internal bool validate_image_type(const TiffImage *img) {
if (img->type < TIFF_IMAGE_TYPE_BILEVEL || img->type > TIFF_IMAGE_TYPE_RGB) {
return false;
}
switch (img->type) {
case TIFF_IMAGE_TYPE_BILEVEL:
case TIFF_IMAGE_TYPE_GRAYSCALE:
if (img->photometric_interpretation >
TIFF_PHOTOMETRIC_INTERPRETATION_BLACK_IS_ZERO) {
return false;
}
break;
case TIFF_IMAGE_TYPE_PALETTE:
if (img->photometric_interpretation !=
TIFF_PHOTOMETRIC_INTERPRETATION_RGB_PALETTE) {
return false;
}
break;
case TIFF_IMAGE_TYPE_RGB:
if (img->photometric_interpretation !=
TIFF_PHOTOMETRIC_INTERPRETATION_RGB) {
return false;
}
break;
default:
return false;
}
return true;
}
internal void fread_with_offset(FILE *fp, void *dst, u64 count, u64 offset) {
if (!fp || !dst) {
return;
}
fseek(fp, offset, SEEK_SET);
fread(dst, count, 1, fp);
}
internal bool valid_tiff_file(const char *file) {
u64 name_length = strlen(file);
if (name_length < TIFF_FILENAME_MIN_LENGTH) {
return false;
}
const char *ext = NULL;
for (u32 i = name_length - 1; i >= name_length - TIFF_FILENAME_MIN_LENGTH;
--i) {
if (file[i] == '.') {
ext = &(file[i]);
break;
}
}
if (!ext || !IS_TIFF_EXTENSION(ext)) {
return false;
}
return true;
}
internal f32 u8_normalise(u8 value) { return (f32)value / (f32)UINT8_MAX; }
internal u8 u8_denormalise(f32 value) { return (u8)(value * UINT8_MAX); }

17
src/tiff/tiffread.h Normal file
View File

@@ -0,0 +1,17 @@
#ifndef TIFFREAD_H
#define TIFFREAD_H
#include "mem_arena.h"
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
#include "image.h"
Image *read_baseline_tiff(const char *file, Arena *arena);
#ifdef __cplusplus
}
#endif // __cplusplus
#endif // !TIFFREAD_H

View File

@@ -1,20 +0,0 @@
#ifdef TIFF_TYPE_LOOKUP
#undef TIFF_TYPE
#define TIFF_TYPE(NAME, ID, VALUE) [VALUE] = NAME,
#else
#undef TIFF_TYPE
#define TIFF_TYPE(NAME, ID, VALUE) ID = VALUE,
#endif /* ifdef TIFF_TYPE_LOOKUP */
TIFF_TYPE("UNSIGNED BYTE" , TIFF_ENTRY_BYTE , 0x0001) // 8-bit unsigned integer
TIFF_TYPE("ASCII" , TIFF_ENTRY_ASCII , 0x0002) // 8-bit byte that contains a 7-bit ASCII code; the last byte must be NUL (binary zero)
TIFF_TYPE("UNSIGNED SHORT" , TIFF_ENTRY_SHORT , 0x0003) // 16-bit (2-byte) unsigned integer
TIFF_TYPE("UNSIGNED LONG" , TIFF_ENTRY_LONG , 0x0004) // 32-bit (4-byte) unsigned integer
TIFF_TYPE("UNSIGNED RATIONAL", TIFF_ENTRY_RATIONAL , 0x0005) // Two LONGs: the first represents the numerator of a fraction; the second, the denominator
TIFF_TYPE("SIGNED BYTE" , TIFF_ENTRY_SBYTE , 0x0006) // An 8-bit signed (twos-complement) integer
TIFF_TYPE("UNDEFINED" , TIFF_ENTRY_UNDEFINED , 0x0007) // An 8-bit byte that may contain anything, depending on the definition of the field
TIFF_TYPE("SIGNED SHORT" , TIFF_ENTRY_SSHORT , 0x0008) // A 16-bit (2-byte) signed (twos-complement) integer
TIFF_TYPE("SIGNED LONG" , TIFF_ENTRY_SLONG , 0x0009) // A 32-bit (4-byte) signed (twos-complement) integer
TIFF_TYPE("SIGNED RATIONAL" , TIFF_ENTRY_SRATIONAL , 0x000a) // Two SLONGs: the first represents the numerator of a fraction, the second the denominator
TIFF_TYPE("FLOAT" , TIFF_ENTRY_FLOAT , 0x000b) // Single precision (4-byte) IEEE format
TIFF_TYPE("DOUBLE" , TIFF_ENTRY_DOUBLE , 0x000c) // Double precision (8-byte) IEEE format

7
src/utils/endianness.h Normal file
View File

@@ -0,0 +1,7 @@
#ifndef ENDIANNESS_H
#define ENDIANNESS_H
#define IS_LITTLE_ENDIAN ((*(uint8_t *)(&(uint32_t){0x00010203})) == 0x03)
#define IS_BIG_ENDIAN ((*(uint8_t *)(&(uint32_t){0x00010203})) == 0x00)
#endif // !ENDIANNESS_H