Started from buttom go to the sky
This commit is contained in:
parent
96215449bd
commit
f811e55dea
4781 changed files with 10121 additions and 1743 deletions
73
stdme/src/img/qoi/qoi_decode.c
Normal file
73
stdme/src/img/qoi/qoi_decode.c
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* qoi_decode.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/25 22:31:40 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/12/25 22:35:44 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/img/qoi.h"
|
||||
#include "me/img/qoi/qoi_utils.h"
|
||||
#include "me/img/qoi/qoi_decode.h"
|
||||
|
||||
static inline void *qoi_decode_inner(t_decode_vals *vals, const t_u8 *bytes,
|
||||
t_i32 channels, t_i32 size)
|
||||
{
|
||||
t_u8 *pixels;
|
||||
|
||||
pixels = (t_u8 *)mem_alloc(vals->px_len);
|
||||
if (!pixels)
|
||||
return (NULL);
|
||||
mem_set_zero(vals->index, sizeof(vals->index));
|
||||
vals->px.v = 0;
|
||||
vals->px.rgba.a = 255;
|
||||
vals->chunks_len = size - (t_i32) sizeof(t_u8[8]);
|
||||
vals->px_pos = 0;
|
||||
while (vals->px_pos < vals->px_len)
|
||||
{
|
||||
if (vals->run > 0)
|
||||
vals->run--;
|
||||
else if (vals->p < vals->chunks_len)
|
||||
qoi_decode_inner_inner(vals, bytes);
|
||||
pixels[vals->px_pos + 0] = vals->px.rgba.r;
|
||||
pixels[vals->px_pos + 1] = vals->px.rgba.g;
|
||||
pixels[vals->px_pos + 2] = vals->px.rgba.b;
|
||||
if (channels == 4)
|
||||
pixels[vals->px_pos + 3] = vals->px.rgba.a;
|
||||
vals->px_pos += channels;
|
||||
}
|
||||
return (pixels);
|
||||
}
|
||||
|
||||
void *qoi_decode(const void *data, t_i32 size, t_qoi_desc *desc,
|
||||
t_i32 channels)
|
||||
{
|
||||
const t_u8 *bytes;
|
||||
t_decode_vals vals;
|
||||
|
||||
vals.p = 0;
|
||||
vals.run = 0;
|
||||
if (data == NULL || desc == NULL || (channels != 0 && channels != 3
|
||||
&& channels != 4) || size < QOI_HEADER_SIZE
|
||||
+ (t_i32) sizeof(t_u8[8]))
|
||||
return (NULL);
|
||||
bytes = (const t_u8 *)data;
|
||||
vals.header_magic = qoi_read_32(bytes, &vals.p);
|
||||
desc->width = qoi_read_32(bytes, &vals.p);
|
||||
desc->height = qoi_read_32(bytes, &vals.p);
|
||||
desc->channels = bytes[vals.p++];
|
||||
desc->colorspace = bytes[vals.p++];
|
||||
if (desc->width == 0 || desc->height == 0 || desc->channels < 3
|
||||
|| desc->channels > 4 || desc->colorspace > 1
|
||||
|| vals.header_magic != QOI_MAGIC || desc->height >= QOI_PIXELS_MAX
|
||||
/ desc->width)
|
||||
return (NULL);
|
||||
if (channels == 0)
|
||||
channels = desc->channels;
|
||||
vals.px_len = desc->width * desc->height * channels;
|
||||
return (qoi_decode_inner(&vals, bytes, channels, size));
|
||||
}
|
||||
29
stdme/src/img/qoi/qoi_encode.c
Normal file
29
stdme/src/img/qoi/qoi_encode.c
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* qoi_encode.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/25 22:38:42 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/12/25 22:56:06 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/img/qoi.h"
|
||||
#include "me/img/qoi/qoi_encode.h"
|
||||
#include "me/img/qoi/qoi_utils.h"
|
||||
|
||||
void *qoi_encode(const void *data, const t_qoi_desc *desc, t_i32 *out_len)
|
||||
{
|
||||
t_encode_vals vals;
|
||||
|
||||
if (data == NULL || out_len == NULL || desc == NULL || desc->width == 0
|
||||
|| desc->height == 0 || desc->channels < 3 || desc->channels > 4
|
||||
|| desc->colorspace > 1 || desc->height >= QOI_PIXELS_MAX / desc->width)
|
||||
return (NULL);
|
||||
vals = (t_encode_vals){0};
|
||||
vals.max_size = desc->width * desc->height * (desc->channels + 1)
|
||||
+ QOI_HEADER_SIZE + sizeof(t_u8[8]);
|
||||
return (qoi_encode_inner(&vals, desc, (const t_u8 *)data, out_len));
|
||||
}
|
||||
49
stdme/src/img/qoi/qoi_fs.c
Normal file
49
stdme/src/img/qoi/qoi_fs.c
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* qoi_fs.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/24 19:06:05 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/12/24 19:18:01 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/fs/close.h"
|
||||
#include "me/fs/open.h"
|
||||
#include "me/fs/read_to_vec.h"
|
||||
#include "me/fs/write.h"
|
||||
#include "me/img/qoi.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
t_i32 qoi_write(t_const_str filename, const void *data,
|
||||
const t_qoi_desc *desc)
|
||||
{
|
||||
t_file f;
|
||||
void *encoded;
|
||||
t_i32 size;
|
||||
|
||||
if (me_open(filename, false, true, &f))
|
||||
return (0);
|
||||
encoded = qoi_encode(data, desc, &size);
|
||||
if (!encoded)
|
||||
return (me_close(f, NULL), 0);
|
||||
if (me_write(f, encoded, size))
|
||||
return (me_close(f, NULL), 0);
|
||||
me_close(f, NULL);
|
||||
free(encoded);
|
||||
return (size);
|
||||
}
|
||||
|
||||
void *qoi_read(t_const_str filename, t_qoi_desc *desc, t_i32 channels)
|
||||
{
|
||||
t_vec_u8 out;
|
||||
void *pixels;
|
||||
|
||||
if (read_to_vec(filename, &out))
|
||||
return (NULL);
|
||||
pixels = qoi_decode(out.buffer, out.len, desc, channels);
|
||||
vec_u8_free(out);
|
||||
return (pixels);
|
||||
}
|
||||
36
stdme/src/img/qoi/qoi_utils.c
Normal file
36
stdme/src/img/qoi/qoi_utils.c
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* qoi_utils.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/24 19:02:03 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/12/24 19:02:42 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/img/qoi/qoi_utils.h"
|
||||
#include "me/types.h"
|
||||
|
||||
void qoi_write_32(t_u8 *bytes, t_i32 *p, t_u32 v)
|
||||
{
|
||||
bytes[(*p)++] = (0xff000000 & v) >> 24;
|
||||
bytes[(*p)++] = (0x00ff0000 & v) >> 16;
|
||||
bytes[(*p)++] = (0x0000ff00 & v) >> 8;
|
||||
bytes[(*p)++] = (0x000000ff & v);
|
||||
}
|
||||
|
||||
t_u32 qoi_read_32(const t_u8 *bytes, t_i32 *p)
|
||||
{
|
||||
t_u32 a;
|
||||
t_u32 b;
|
||||
t_u32 c;
|
||||
t_u32 d;
|
||||
|
||||
a = bytes[(*p)++];
|
||||
b = bytes[(*p)++];
|
||||
c = bytes[(*p)++];
|
||||
d = bytes[(*p)++];
|
||||
return (a << 24 | b << 16 | c << 8 | d);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue