Compare commits
No commits in common. "eaa6e52fbe831f4112a0bccf89194c9b403df13c" and "a00e7376520ad884dea02bafd4d8dac2f1071e59" have entirely different histories.
eaa6e52fbe
...
a00e737652
2
compile
2
compile
@ -13,7 +13,7 @@ LIBS="\
|
|||||||
"
|
"
|
||||||
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 *.x | xargs -I{} echo -n "{} ") \
|
||||||
"
|
"
|
||||||
OUT=tiffread
|
OUT=tiffread
|
||||||
|
|
||||||
|
@ -1,17 +1,22 @@
|
|||||||
#include "image.h"
|
#include "image.h"
|
||||||
#include "mem_arena.h"
|
#include "mem_allocator.h"
|
||||||
|
#include "mem_libc.h"
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
Image *create_image(u64 width, u64 height, Pixel *data, Arena *arena) {
|
Image *create_image(u64 width, u64 height, Pixel *data,
|
||||||
if (!arena) {
|
const Allocator *allocator) {
|
||||||
return NULL;
|
Allocator alloc;
|
||||||
|
if (!allocator) {
|
||||||
|
alloc = wapp_mem_libc_allocator();
|
||||||
|
} else {
|
||||||
|
alloc = *allocator;
|
||||||
}
|
}
|
||||||
|
|
||||||
u64 buf_length = width * height;
|
u64 buf_length = width * height;
|
||||||
u64 total_size = sizeof(Image) + sizeof(Pixel) * buf_length;
|
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) {
|
if (!img) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
@ -23,3 +28,18 @@ Image *create_image(u64 width, u64 height, Pixel *data, Arena *arena) {
|
|||||||
|
|
||||||
return img;
|
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);
|
||||||
|
}
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
#ifndef IMAGE_H
|
#ifndef IMAGE_H
|
||||||
#define IMAGE_H
|
#define IMAGE_H
|
||||||
|
|
||||||
#include "mem_arena.h"
|
#include "mem_allocator.h"
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#endif // __cplusplus
|
#endif // __cplusplus
|
||||||
@ -24,7 +24,9 @@ struct image {
|
|||||||
Pixel data[];
|
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
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
|
19
src/main.c
19
src/main.c
@ -1,6 +1,7 @@
|
|||||||
#include "aliases.h"
|
#include "aliases.h"
|
||||||
#include "image.h"
|
#include "image.h"
|
||||||
#include "mem_arena.h"
|
#include "mem_allocator.h"
|
||||||
|
#include "mem_ctx.h"
|
||||||
#include "tiffread.h"
|
#include "tiffread.h"
|
||||||
#include <SDL2/SDL.h>
|
#include <SDL2/SDL.h>
|
||||||
#include <SDL2/SDL_events.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 main(int argc, char *argv[]) {
|
||||||
int exit_code = EXIT_SUCCESS;
|
int exit_code = EXIT_SUCCESS;
|
||||||
|
|
||||||
Arena *arena = NULL;
|
wapp_mem_ctx_init(10 * 1024 * 1024, 2 * 1024 * 1024);
|
||||||
if (!wapp_mem_arena_init(&arena, 10 * 1024 * 1024)) {
|
Allocator ctx_main_allocator = wapp_mem_ctx_allocator(CTX_DEST_BUFFER_MAIN);
|
||||||
return EXIT_FAILURE;
|
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";
|
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) {
|
if (!img) {
|
||||||
exit_code = EXIT_FAILURE;
|
exit_code = EXIT_FAILURE;
|
||||||
goto MAIN_DESTROY_ARENA;
|
goto MAIN_FREE_CONTEXT;
|
||||||
}
|
}
|
||||||
|
|
||||||
FILE *out = fopen("test.ppm", "wb");
|
FILE *out = fopen("test.ppm", "wb");
|
||||||
@ -157,8 +158,8 @@ int main(int argc, char *argv[]) {
|
|||||||
// MAIN_QUIT_SDL:
|
// MAIN_QUIT_SDL:
|
||||||
// SDL_Quit();
|
// SDL_Quit();
|
||||||
|
|
||||||
MAIN_DESTROY_ARENA:
|
MAIN_FREE_CONTEXT:
|
||||||
wapp_mem_arena_destroy(&arena);
|
wapp_mem_ctx_free();
|
||||||
|
|
||||||
return exit_code;
|
return exit_code;
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,10 @@
|
|||||||
#include "aliases.h"
|
#include "aliases.h"
|
||||||
#include "endianness.h"
|
#include "endianness.h"
|
||||||
#include "image.h"
|
#include "image.h"
|
||||||
|
#include "mem_allocator.h"
|
||||||
#include "mem_arena.h"
|
#include "mem_arena.h"
|
||||||
|
#include "mem_ctx.h"
|
||||||
|
#include "mem_libc.h"
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
@ -36,11 +39,12 @@
|
|||||||
// clang-format on
|
// clang-format on
|
||||||
|
|
||||||
TiffHdr read_tiff_header(FILE *fp);
|
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);
|
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);
|
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;
|
Image *img_out = NULL;
|
||||||
|
|
||||||
if (!file) {
|
if (!file) {
|
||||||
@ -75,7 +79,14 @@ Image *read_baseline_tiff(const char *file, Arena *arena) {
|
|||||||
goto READ_BASELINE_FILE_CLEANUP;
|
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);
|
read_fields(fp, &header, &ifd);
|
||||||
|
|
||||||
@ -122,7 +133,8 @@ TiffHdr read_tiff_header(FILE *fp) {
|
|||||||
return header;
|
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) {
|
if (!fp || !header) {
|
||||||
return NULL_TIFF_IFD;
|
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;
|
u64 field_byte_count = sizeof(TiffField) * ifd.count;
|
||||||
ifd.fields = (TiffField *)wapp_mem_arena_alloc(arena, field_byte_count);
|
ifd.fields =
|
||||||
if (!(ifd.fields)) {
|
(TiffField *)wapp_mem_allocator_alloc(allocator, field_byte_count);
|
||||||
return NULL_TIFF_IFD;
|
|
||||||
}
|
|
||||||
|
|
||||||
fread(ifd.fields, field_byte_count, 1, fp);
|
fread(ifd.fields, field_byte_count, 1, fp);
|
||||||
fread(&(ifd.next_ifd), sizeof(ifd.next_ifd), 1, fp);
|
fread(&(ifd.next_ifd), sizeof(ifd.next_ifd), 1, fp);
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
#ifndef TIFFREAD_H
|
#ifndef TIFFREAD_H
|
||||||
#define TIFFREAD_H
|
#define TIFFREAD_H
|
||||||
|
|
||||||
#include "mem_arena.h"
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#endif // __cplusplus
|
#endif // __cplusplus
|
||||||
@ -125,7 +124,7 @@ struct tiff_image {
|
|||||||
bool extra_samples_offset;
|
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
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user