Add utility to load P6 images
This commit is contained in:
parent
f024d51d85
commit
677627d8c9
58
src/pam.c
58
src/pam.c
@ -1,4 +1,8 @@
|
|||||||
#include "pam.h"
|
#include "pam.h"
|
||||||
|
#include "img.h"
|
||||||
|
#include "mem_arena.h"
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdint.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
@ -28,3 +32,57 @@ void write_p7_image(u64 width, u64 height, u8 *buf, const char *outfile) {
|
|||||||
|
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Image *load_p6_image(Arena *arena, const char *filename) {
|
||||||
|
Image *output = NULL;
|
||||||
|
|
||||||
|
if (!arena || !filename) {
|
||||||
|
goto RETURN_VALUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
FILE *fp = fopen(filename, "rb");
|
||||||
|
if (!fp) {
|
||||||
|
goto RETURN_VALUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
char type[4] = {0};
|
||||||
|
fread(type, 3, 1, fp);
|
||||||
|
if (strncmp(type, "P6\n", 4) != 0) {
|
||||||
|
goto CLOSE_FILE;
|
||||||
|
}
|
||||||
|
|
||||||
|
u64 w, h;
|
||||||
|
fscanf(fp, "%lu %lu\n", &w, &h);
|
||||||
|
|
||||||
|
u64 max;
|
||||||
|
fscanf(fp, "%lu\n", &max);
|
||||||
|
if (max > UINT8_MAX) {
|
||||||
|
goto CLOSE_FILE;
|
||||||
|
}
|
||||||
|
|
||||||
|
output = wapp_mem_arena_alloc(arena, sizeof(Image));
|
||||||
|
if (!output) {
|
||||||
|
goto CLOSE_FILE;
|
||||||
|
}
|
||||||
|
|
||||||
|
output->width = w;
|
||||||
|
output->height = h;
|
||||||
|
if (!init_buffer(arena, output)) {
|
||||||
|
goto CLOSE_FILE;
|
||||||
|
}
|
||||||
|
|
||||||
|
Colour colour = {0};
|
||||||
|
for (u64 y = 0; y < h; ++y) {
|
||||||
|
for (u64 x = 0; x < w; ++x) {
|
||||||
|
fread(&colour, 3, 1, fp);
|
||||||
|
colour.a = 255;
|
||||||
|
set_pixel(output, x, y, &colour);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
CLOSE_FILE:
|
||||||
|
fclose(fp);
|
||||||
|
|
||||||
|
RETURN_VALUE:
|
||||||
|
return output;
|
||||||
|
}
|
||||||
|
@ -2,7 +2,9 @@
|
|||||||
#define PAM_H
|
#define PAM_H
|
||||||
|
|
||||||
#include "aliases.h"
|
#include "aliases.h"
|
||||||
|
#include "img.h"
|
||||||
|
|
||||||
void write_p7_image(u64 width, u64 height, u8 *buf, const char *outfile);
|
void write_p7_image(u64 width, u64 height, u8 *buf, const char *outfile);
|
||||||
|
Image *load_p6_image(Arena *arena, const char *filename);
|
||||||
|
|
||||||
#endif // PAM_H
|
#endif // PAM_H
|
||||||
|
Loading…
Reference in New Issue
Block a user