Compare commits

..

No commits in common. "eaa6e52fbe831f4112a0bccf89194c9b403df13c" and "a00e7376520ad884dea02bafd4d8dac2f1071e59" have entirely different histories.

6 changed files with 59 additions and 27 deletions

View File

@ -13,7 +13,7 @@ LIBS="\
"
SRC="\
$(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 *.x | xargs -I{} echo -n "{} ") \
"
OUT=tiffread

View File

@ -1,17 +1,22 @@
#include "image.h"
#include "mem_arena.h"
#include "mem_allocator.h"
#include "mem_libc.h"
#include <stddef.h>
#include <string.h>
Image *create_image(u64 width, u64 height, Pixel *data, Arena *arena) {
if (!arena) {
return NULL;
Image *create_image(u64 width, u64 height, Pixel *data,
const Allocator *allocator) {
Allocator alloc;
if (!allocator) {
alloc = wapp_mem_libc_allocator();
} else {
alloc = *allocator;
}
u64 buf_length = width * height;
u64 total_size = sizeof(Image) + sizeof(Pixel) * buf_length;
Image *img = wapp_mem_arena_alloc(arena, total_size);
Image *img = wapp_mem_allocator_alloc(&alloc, total_size);
if (!img) {
return NULL;
}
@ -23,3 +28,18 @@ Image *create_image(u64 width, u64 height, Pixel *data, Arena *arena) {
return img;
}
void destroy_image(Image **img, const Allocator *allocator) {
if (!img || !(*img)) {
return;
}
Allocator alloc;
if (!allocator) {
alloc = wapp_mem_libc_allocator();
} else {
alloc = *allocator;
}
wapp_mem_allocator_free(&alloc, (void **)img);
}

View File

@ -1,7 +1,7 @@
#ifndef IMAGE_H
#define IMAGE_H
#include "mem_arena.h"
#include "mem_allocator.h"
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
@ -24,7 +24,9 @@ struct image {
Pixel data[];
};
Image *create_image(u64 width, u64 height, Pixel *data, Arena *arena);
Image *create_image(u64 width, u64 height, Pixel *data,
const Allocator *allocator);
void destroy_image(Image **img, const Allocator *allocator);
#ifdef __cplusplus
}

View File

@ -1,6 +1,7 @@
#include "aliases.h"
#include "image.h"
#include "mem_arena.h"
#include "mem_allocator.h"
#include "mem_ctx.h"
#include "tiffread.h"
#include <SDL2/SDL.h>
#include <SDL2/SDL_events.h>
@ -34,17 +35,17 @@ Point point_from_index(u32 index, u32 w);
int main(int argc, char *argv[]) {
int exit_code = EXIT_SUCCESS;
Arena *arena = NULL;
if (!wapp_mem_arena_init(&arena, 10 * 1024 * 1024)) {
return EXIT_FAILURE;
}
wapp_mem_ctx_init(10 * 1024 * 1024, 2 * 1024 * 1024);
Allocator ctx_main_allocator = wapp_mem_ctx_allocator(CTX_DEST_BUFFER_MAIN);
Allocator ctx_temp_allocator = wapp_mem_ctx_allocator(CTX_DEST_BUFFER_TEMP);
(void)(ctx_temp_allocator);
const char *file_to_open = argc > 1 ? argv[1] : "./resources/test.tif";
Image *img = read_baseline_tiff(file_to_open, arena);
Image *img = read_baseline_tiff(file_to_open, &ctx_main_allocator);
if (!img) {
exit_code = EXIT_FAILURE;
goto MAIN_DESTROY_ARENA;
goto MAIN_FREE_CONTEXT;
}
FILE *out = fopen("test.ppm", "wb");
@ -157,8 +158,8 @@ int main(int argc, char *argv[]) {
// MAIN_QUIT_SDL:
// SDL_Quit();
MAIN_DESTROY_ARENA:
wapp_mem_arena_destroy(&arena);
MAIN_FREE_CONTEXT:
wapp_mem_ctx_free();
return exit_code;
}

View File

@ -2,7 +2,10 @@
#include "aliases.h"
#include "endianness.h"
#include "image.h"
#include "mem_allocator.h"
#include "mem_arena.h"
#include "mem_ctx.h"
#include "mem_libc.h"
#include <math.h>
#include <netinet/in.h>
#include <stdbool.h>
@ -36,11 +39,12 @@
// clang-format on
TiffHdr read_tiff_header(FILE *fp);
TiffIFD read_ifd(FILE *fp, const TiffHdr *header, u32 offset, Arena *arena);
TiffIFD read_ifd(FILE *fp, const TiffHdr *header, u32 offset,
const Allocator *allocator);
TiffImage read_fields(FILE *fp, const TiffHdr *header, const TiffIFD *ifd);
void read_from_file_with_offset(FILE *fp, void *dst, u64 count, u64 offset);
Image *read_baseline_tiff(const char *file, Arena *arena) {
Image *read_baseline_tiff(const char *file, const Allocator *allocator) {
Image *img_out = NULL;
if (!file) {
@ -75,7 +79,14 @@ Image *read_baseline_tiff(const char *file, Arena *arena) {
goto READ_BASELINE_FILE_CLEANUP;
}
TiffIFD ifd = read_ifd(fp, &header, header.first_ifd_offset, arena);
Allocator alloc;
if (!allocator) {
alloc = wapp_mem_libc_allocator();
} else {
alloc = *allocator;
}
TiffIFD ifd = read_ifd(fp, &header, header.first_ifd_offset, &alloc);
read_fields(fp, &header, &ifd);
@ -122,7 +133,8 @@ TiffHdr read_tiff_header(FILE *fp) {
return header;
}
TiffIFD read_ifd(FILE *fp, const TiffHdr *header, u32 offset, Arena *arena) {
TiffIFD read_ifd(FILE *fp, const TiffHdr *header, u32 offset,
const Allocator *allocator) {
if (!fp || !header) {
return NULL_TIFF_IFD;
}
@ -149,10 +161,8 @@ TiffIFD read_ifd(FILE *fp, const TiffHdr *header, u32 offset, Arena *arena) {
}
u64 field_byte_count = sizeof(TiffField) * ifd.count;
ifd.fields = (TiffField *)wapp_mem_arena_alloc(arena, field_byte_count);
if (!(ifd.fields)) {
return NULL_TIFF_IFD;
}
ifd.fields =
(TiffField *)wapp_mem_allocator_alloc(allocator, field_byte_count);
fread(ifd.fields, field_byte_count, 1, fp);
fread(&(ifd.next_ifd), sizeof(ifd.next_ifd), 1, fp);

View File

@ -1,7 +1,6 @@
#ifndef TIFFREAD_H
#define TIFFREAD_H
#include "mem_arena.h"
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
@ -125,7 +124,7 @@ struct tiff_image {
bool extra_samples_offset;
};
Image *read_baseline_tiff(const char *file, Arena *arena);
Image *read_baseline_tiff(const char *file, const Allocator *allocator);
#ifdef __cplusplus
}