Skip to content

Commit

Permalink
Added farbfeld parser
Browse files Browse the repository at this point in the history
Signed-off-by: alttabber <[email protected]>
  • Loading branch information
alttabber committed Nov 20, 2024
1 parent 6b9f0cb commit 84b18d4
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Fully customizable and lightweight image viewer for Wayland based display server
- PNM (built-in);
- TGA (built-in);
- QOI (built-in).
- Farbfeld (built-in).
- Fully customizable keyboard bindings, colors, and [many other](https://github.com/artemsen/swayimg/blob/master/extra/swayimgrc) parameters;
- Loading images from files and pipes;
- Gallery and viewer modes with slideshow and animation support;
Expand Down
1 change: 1 addition & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ sources = [
'src/formats/pnm.c',
'src/formats/qoi.c',
'src/formats/tga.c',
'src/formats/ff.c',
xdg_shell_h,
xdg_shell_c,
wp_content_type_v1_h,
Expand Down
72 changes: 72 additions & 0 deletions src/formats/ff.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// SPDX-License-Identifier: MIT
// farbfeld format decoder

#include "../loader.h"

#include <arpa/inet.h>
#include <string.h>

#define MAGIC_BYTES_LEN 8
#define MAGIC_BYTES "farbfeld"
#define PIXEL_BYTES 8
#define CHANNEL_BYTES 2
#define R_POS 0
#define G_POS 2
#define B_POS 4
#define A_POS 6

struct farbfeld_header {
uint8_t magic[MAGIC_BYTES_LEN];
uint32_t width;
uint32_t height;
};

uint32_t ff_size(struct farbfeld_header header)
{
return sizeof(struct farbfeld_header) +
header.width * header.height * sizeof(argb_t) * CHANNEL_BYTES;
}

enum loader_status decode_ff(struct image* ctx, const uint8_t* data,
size_t size)
{

if (size < sizeof(struct farbfeld_header)) {
return ldr_unsupported;
}

if (memcmp(data, MAGIC_BYTES, MAGIC_BYTES_LEN) != 0) {
return ldr_unsupported;
}

uint32_t width = *(const uint32_t*)&data[MAGIC_BYTES_LEN];
width = htonl(width);
uint32_t height = *(const uint32_t*)&data[MAGIC_BYTES_LEN + sizeof(width)];
height = htonl(height);

const struct farbfeld_header header = { MAGIC_BYTES, width, height };

if (ff_size(header) > size) {
return ldr_fmterror;
}

ctx->alpha = true;

if (!image_allocate_frame(ctx, header.width, header.height)) {
return ldr_fmterror;
}

struct pixmap* pm = &ctx->frames[0].pm;

size_t imax = min(size, ff_size(header));
for (size_t i = sizeof(struct farbfeld_header); i < imax;
i = i + PIXEL_BYTES) {
argb_t pixel = ARGB(data[i + A_POS], data[i + R_POS], data[i + G_POS],
data[i + B_POS]);
pm->data[(i - sizeof(struct farbfeld_header)) / PIXEL_BYTES] = pixel;
}

image_set_format(ctx, "farbfeld");

return ldr_success;
}
5 changes: 3 additions & 2 deletions src/loader.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
enum loader_status LOADER_FUNCTION(name)(struct image * ctx, \
const uint8_t* data, size_t size)

const char* supported_formats = "bmp, pnm, tga"
const char* supported_formats = "bmp, pnm, tga, ff"
#ifdef HAVE_LIBJPEG
", jpeg"
#endif
Expand Down Expand Up @@ -70,6 +70,7 @@ LOADER_DECLARE(bmp);
LOADER_DECLARE(pnm);
LOADER_DECLARE(qoi);
LOADER_DECLARE(tga);
LOADER_DECLARE(ff);
#ifdef HAVE_LIBEXR
LOADER_DECLARE(exr);
#endif
Expand Down Expand Up @@ -134,7 +135,7 @@ static const image_decoder decoders[] = {
#ifdef HAVE_LIBTIFF
&LOADER_FUNCTION(tiff),
#endif
&LOADER_FUNCTION(qoi), &LOADER_FUNCTION(tga),
&LOADER_FUNCTION(qoi), &LOADER_FUNCTION(ff), &LOADER_FUNCTION(tga)
};

/** Background thread loader queue. */
Expand Down
Binary file added test/data/image.ff
Binary file not shown.
1 change: 1 addition & 0 deletions test/loader_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ TEST_LOADER(bmp);
TEST_LOADER(pnm);
TEST_LOADER(qoi);
TEST_LOADER(tga);
TEST_LOADER(ff);
#ifdef HAVE_LIBEXR
// TEST_LOADER(exr);
#endif
Expand Down
1 change: 1 addition & 0 deletions test/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ sources = [
'../src/formats/pnm.c',
'../src/formats/qoi.c',
'../src/formats/tga.c',
'../src/formats/ff.c',
]
if exif.found()
sources += ['exif_test.cpp', '../src/exif.c']
Expand Down

0 comments on commit 84b18d4

Please sign in to comment.